vc1dec: Partial rewrite of vc1_pred_mv()
[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 1477 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
4344ce08
A
1478 int opposit, a_f, b_f, c_f;
1479 int16_t field_predA[2];
1480 int16_t field_predB[2];
1481 int16_t field_predC[2];
cad16562
MSS
1482 int a_valid, b_valid, c_valid;
1483 int hybridmv_thresh, y_bias = 0;
1484
1485 if (v->mv_mode == MV_PMODE_MIXED_MV ||
50f97219
KS
1486 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1487 mixedmv_pic = 1;
1488 else
1489 mixedmv_pic = 0;
b761659b
DB
1490 /* scale MV difference to be quad-pel */
1491 dmv_x <<= 1 - s->quarter_sample;
1492 dmv_y <<= 1 - s->quarter_sample;
1493
1494 wrap = s->b8_stride;
50f97219 1495 xy = s->block_index[n];
b761659b 1496
50f97219 1497 if (s->mb_intra) {
cad16562
MSS
1498 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1499 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1500 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1501 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
50f97219
KS
1502 if (mv1) { /* duplicate motion data for 1-MV block */
1503 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1504 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1505 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1506 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
cad16562
MSS
1507 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1508 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
c47d3835 1509 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
50f97219
KS
1510 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1511 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1512 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1513 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
cad16562
MSS
1514 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1515 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
b761659b
DB
1516 }
1517 return;
1518 }
1519
50f97219 1520 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
cad16562 1521 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
50f97219 1522 if (mv1) {
cad16562
MSS
1523 if (v->field_mode && mixedmv_pic)
1524 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1525 else
1526 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1527 } else {
b761659b 1528 //in 4-MV mode different blocks have different B predictor position
cad16562 1529 switch (n) {
b761659b
DB
1530 case 0:
1531 off = (s->mb_x > 0) ? -1 : 1;
1532 break;
1533 case 1:
1534 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1535 break;
1536 case 2:
1537 off = 1;
1538 break;
1539 case 3:
1540 off = -1;
1541 }
1542 }
cad16562
MSS
1543 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1544
50f97219 1545 a_valid = !s->first_slice_line || (n == 2 || n == 3);
cad16562 1546 b_valid = a_valid && (s->mb_width > 1);
50f97219 1547 c_valid = s->mb_x || (n == 1 || n == 3);
cad16562
MSS
1548 if (v->field_mode) {
1549 a_valid = a_valid && !is_intra[xy - wrap];
1550 b_valid = b_valid && !is_intra[xy - wrap + off];
1551 c_valid = c_valid && !is_intra[xy - 1];
1552 }
1553
1554 if (a_valid) {
4344ce08
A
1555 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1556 num_oppfield += a_f;
1557 num_samefield += 1 - a_f;
1558 field_predA[0] = A[0];
1559 field_predA[1] = A[1];
cad16562 1560 } else {
4344ce08
A
1561 field_predA[0] = field_predA[1] = 0;
1562 a_f = 0;
cad16562
MSS
1563 }
1564 if (c_valid) {
4344ce08
A
1565 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1566 num_oppfield += c_f;
1567 num_samefield += 1 - c_f;
1568 field_predC[0] = C[0];
1569 field_predC[1] = C[1];
cad16562 1570 } else {
4344ce08
A
1571 field_predC[0] = field_predC[1] = 0;
1572 c_f = 0;
cad16562
MSS
1573 }
1574 if (b_valid) {
4344ce08
A
1575 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1576 num_oppfield += b_f;
1577 num_samefield += 1 - b_f;
1578 field_predB[0] = B[0];
1579 field_predB[1] = B[1];
cad16562 1580 } else {
4344ce08
A
1581 field_predB[0] = field_predB[1] = 0;
1582 b_f = 0;
cad16562
MSS
1583 }
1584
1585 if (v->field_mode) {
1586 if (num_samefield <= num_oppfield)
1587 opposit = 1 - pred_flag;
1588 else
1589 opposit = pred_flag;
1590 } else
1591 opposit = 0;
1592 if (opposit) {
4344ce08
A
1593 if (a_valid && !a_f) {
1594 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1595 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1596 }
1597 if (b_valid && !b_f) {
1598 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1599 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1600 }
1601 if (c_valid && !c_f) {
1602 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1603 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1604 }
1605 v->mv_f[dir][xy + v->blocks_off] = 1;
cad16562
MSS
1606 v->ref_field_type[dir] = !v->cur_field_type;
1607 } else {
4344ce08
A
1608 if (a_valid && a_f) {
1609 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1610 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1611 }
1612 if (b_valid && b_f) {
1613 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1614 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1615 }
1616 if (c_valid && c_f) {
1617 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1618 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1619 }
1620 v->mv_f[dir][xy + v->blocks_off] = 0;
cad16562 1621 v->ref_field_type[dir] = v->cur_field_type;
b761659b 1622 }
cad16562 1623
4344ce08
A
1624 if (a_valid) {
1625 px = field_predA[0];
1626 py = field_predA[1];
1627 } else if (c_valid) {
1628 px = field_predC[0];
1629 py = field_predC[1];
1630 } else if (b_valid) {
1631 px = field_predB[0];
1632 py = field_predB[1];
1633 } else {
1634 px = 0;
1635 py = 0;
1636 }
1637
1638 if (num_samefield + num_oppfield > 1) {
1639 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1640 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1641 }
1642
b761659b 1643 /* Pullback MV as specified in 8.3.5.3.4 */
cad16562 1644 if (!v->field_mode) {
b761659b 1645 int qx, qy, X, Y;
50f97219
KS
1646 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1647 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1648 X = (s->mb_width << 6) - 4;
1649 Y = (s->mb_height << 6) - 4;
1650 if (mv1) {
1651 if (qx + px < -60) px = -60 - qx;
1652 if (qy + py < -60) py = -60 - qy;
b761659b 1653 } else {
50f97219
KS
1654 if (qx + px < -28) px = -28 - qx;
1655 if (qy + py < -28) py = -28 - qy;
b761659b 1656 }
50f97219
KS
1657 if (qx + px > X) px = X - qx;
1658 if (qy + py > Y) py = Y - qy;
b761659b 1659 }
cad16562
MSS
1660
1661 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1662 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1663 if (v->field_mode && !s->quarter_sample)
1664 hybridmv_thresh = 16;
b761659b 1665 else
cad16562
MSS
1666 hybridmv_thresh = 32;
1667 if (a_valid && c_valid) {
1668 if (is_intra[xy - wrap])
1669 sum = FFABS(px) + FFABS(py);
1670 else
4344ce08 1671 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
cad16562
MSS
1672 if (sum > hybridmv_thresh) {
1673 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
4344ce08
A
1674 px = field_predA[0];
1675 py = field_predA[1];
cad16562 1676 } else {
4344ce08
A
1677 px = field_predC[0];
1678 py = field_predC[1];
cad16562 1679 }
b761659b 1680 } else {
cad16562
MSS
1681 if (is_intra[xy - 1])
1682 sum = FFABS(px) + FFABS(py);
1683 else
4344ce08 1684 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
cad16562 1685 if (sum > hybridmv_thresh) {
50f97219 1686 if (get_bits1(&s->gb)) {
4344ce08
A
1687 px = field_predA[0];
1688 py = field_predA[1];
cad16562 1689 } else {
4344ce08
A
1690 px = field_predC[0];
1691 py = field_predC[1];
cad16562
MSS
1692 }
1693 }
b761659b 1694 }
cad16562
MSS
1695 }
1696 }
1697
1698 if (v->field_mode && !s->quarter_sample) {
1699 r_x <<= 1;
1700 r_y <<= 1;
1701 }
1702 if (v->field_mode && v->numref)
1703 r_y >>= 1;
1704 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1705 y_bias = 1;
1706 /* store MV using signed modulus of MV range defined in 4.11 */
1707 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;
1708 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
1709 if (mv1) { /* duplicate motion data for 1-MV block */
1710 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];
1711 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];
1712 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];
1713 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
1714 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];
1715 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 1716 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
cad16562
MSS
1717 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];
1718 }
1719}
1720
1721/** Predict and set motion vector for interlaced frame picture MBs
1722 */
50f97219
KS
1723static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1724 int mvn, int r_x, int r_y, uint8_t* is_intra)
cad16562
MSS
1725{
1726 MpegEncContext *s = &v->s;
1727 int xy, wrap, off = 0;
1728 int A[2], B[2], C[2];
1729 int px, py;
1730 int a_valid = 0, b_valid = 0, c_valid = 0;
1731 int field_a, field_b, field_c; // 0: same, 1: opposit
1732 int total_valid, num_samefield, num_oppfield;
1733 int pos_c, pos_b, n_adj;
1734
1735 wrap = s->b8_stride;
1736 xy = s->block_index[n];
1737
50f97219 1738 if (s->mb_intra) {
cad16562
MSS
1739 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1740 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1741 s->current_picture.f.motion_val[1][xy][0] = 0;
1742 s->current_picture.f.motion_val[1][xy][1] = 0;
50f97219
KS
1743 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1744 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1745 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1746 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1747 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
cad16562
MSS
1748 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1749 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1750 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
50f97219
KS
1751 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1752 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1753 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1754 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
cad16562
MSS
1755 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1756 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1757 }
1758 return;
1759 }
1760
1761 off = ((n == 0) || (n == 1)) ? 1 : -1;
1762 /* predict A */
1763 if (s->mb_x || (n == 1) || (n == 3)) {
1764 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
50f97219 1765 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
cad16562
MSS
1766 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1767 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1768 a_valid = 1;
1769 } else { // current block has frame mv and cand. has field MV (so average)
1770 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
50f97219 1771 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
cad16562 1772 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
50f97219 1773 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
cad16562
MSS
1774 a_valid = 1;
1775 }
1776 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1777 a_valid = 0;
1778 A[0] = A[1] = 0;
1779 }
50f97219
KS
1780 } else
1781 A[0] = A[1] = 0;
cad16562
MSS
1782 /* Predict B and C */
1783 B[0] = B[1] = C[0] = C[1] = 0;
1784 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1785 if (!s->first_slice_line) {
1786 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1787 b_valid = 1;
50f97219
KS
1788 n_adj = n | 2;
1789 pos_b = s->block_index[n_adj] - 2 * wrap;
cad16562
MSS
1790 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1791 n_adj = (n & 2) | (n & 1);
1792 }
1793 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1794 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1795 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
50f97219
KS
1796 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1797 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
cad16562
MSS
1798 }
1799 }
1800 if (s->mb_width > 1) {
1801 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1802 c_valid = 1;
50f97219
KS
1803 n_adj = 2;
1804 pos_c = s->block_index[2] - 2 * wrap + 2;
cad16562
MSS
1805 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1806 n_adj = n & 2;
1807 }
50f97219
KS
1808 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1809 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
cad16562
MSS
1810 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1811 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1812 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1813 }
1814 if (s->mb_x == s->mb_width - 1) {
1815 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1816 c_valid = 1;
50f97219
KS
1817 n_adj = 3;
1818 pos_c = s->block_index[3] - 2 * wrap - 2;
cad16562
MSS
1819 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1820 n_adj = n | 1;
1821 }
50f97219
KS
1822 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1823 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
cad16562 1824 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
50f97219
KS
1825 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1826 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
cad16562 1827 }
50f97219
KS
1828 } else
1829 c_valid = 0;
cad16562
MSS
1830 }
1831 }
1832 }
1833 }
1834 } else {
50f97219 1835 pos_b = s->block_index[1];
cad16562 1836 b_valid = 1;
50f97219
KS
1837 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1838 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1839 pos_c = s->block_index[0];
cad16562 1840 c_valid = 1;
50f97219
KS
1841 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1842 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
cad16562
MSS
1843 }
1844
1845 total_valid = a_valid + b_valid + c_valid;
1846 // check if predictor A is out of bounds
50f97219 1847 if (!s->mb_x && !(n == 1 || n == 3)) {
cad16562
MSS
1848 A[0] = A[1] = 0;
1849 }
1850 // check if predictor B is out of bounds
1851 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1852 B[0] = B[1] = C[0] = C[1] = 0;
1853 }
1854 if (!v->blk_mv_type[xy]) {
50f97219 1855 if (s->mb_width == 1) {
cad16562
MSS
1856 px = B[0];
1857 py = B[1];
b761659b 1858 } else {
cad16562
MSS
1859 if (total_valid >= 2) {
1860 px = mid_pred(A[0], B[0], C[0]);
1861 py = mid_pred(A[1], B[1], C[1]);
1862 } else if (total_valid) {
1863 if (a_valid) { px = A[0]; py = A[1]; }
1864 if (b_valid) { px = B[0]; py = B[1]; }
1865 if (c_valid) { px = C[0]; py = C[1]; }
50f97219
KS
1866 } else
1867 px = py = 0;
cad16562
MSS
1868 }
1869 } else {
1870 if (a_valid)
1871 field_a = (A[1] & 4) ? 1 : 0;
1872 else
1873 field_a = 0;
1874 if (b_valid)
1875 field_b = (B[1] & 4) ? 1 : 0;
1876 else
1877 field_b = 0;
1878 if (c_valid)
1879 field_c = (C[1] & 4) ? 1 : 0;
1880 else
1881 field_c = 0;
1882
50f97219 1883 num_oppfield = field_a + field_b + field_c;
cad16562
MSS
1884 num_samefield = total_valid - num_oppfield;
1885 if (total_valid == 3) {
1886 if ((num_samefield == 3) || (num_oppfield == 3)) {
1887 px = mid_pred(A[0], B[0], C[0]);
1888 py = mid_pred(A[1], B[1], C[1]);
1889 } else if (num_samefield >= num_oppfield) {
1890 /* take one MV from same field set depending on priority
1891 the check for B may not be necessary */
50f97219
KS
1892 px = !field_a ? A[0] : B[0];
1893 py = !field_a ? A[1] : B[1];
cad16562 1894 } else {
50f97219
KS
1895 px = field_a ? A[0] : B[0];
1896 py = field_a ? A[1] : B[1];
cad16562
MSS
1897 }
1898 } else if (total_valid == 2) {
1899 if (num_samefield >= num_oppfield) {
1900 if (!field_a && a_valid) {
b761659b
DB
1901 px = A[0];
1902 py = A[1];
cad16562
MSS
1903 } else if (!field_b && b_valid) {
1904 px = B[0];
1905 py = B[1];
1906 } else if (c_valid) {
1907 px = C[0];
1908 py = C[1];
1909 } else px = py = 0;
1910 } else {
1911 if (field_a && a_valid) {
1912 px = A[0];
1913 py = A[1];
1914 } else if (field_b && b_valid) {
1915 px = B[0];
1916 py = B[1];
1917 } else if (c_valid) {
b761659b
DB
1918 px = C[0];
1919 py = C[1];
1920 }
1921 }
cad16562
MSS
1922 } else if (total_valid == 1) {
1923 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1924 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
50f97219
KS
1925 } else
1926 px = py = 0;
b761659b 1927 }
cad16562 1928
b761659b 1929 /* store MV using signed modulus of MV range defined in 4.11 */
657ccb5a
DB
1930 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;
1931 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
1932 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1933 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1934 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1935 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1936 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
657ccb5a
DB
1937 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1938 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
cad16562
MSS
1939 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1940 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1941 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1942 s->mv[0][n + 1][0] = s->mv[0][n][0];
1943 s->mv[0][n + 1][1] = s->mv[0][n][1];
b761659b
DB
1944 }
1945}
1946
1947/** Motion compensation for direct or interpolated blocks in B-frames
1948 */
1949static void vc1_interp_mc(VC1Context *v)
1950{
1951 MpegEncContext *s = &v->s;
1952 DSPContext *dsp = &v->s.dsp;
1953 uint8_t *srcY, *srcU, *srcV;
1954 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
cad16562
MSS
1955 int off, off_uv;
1956 int v_edge_pos = s->v_edge_pos >> v->field_mode;
b761659b 1957
cad16562
MSS
1958 if (!v->field_mode && !v->s.next_picture.f.data[0])
1959 return;
b761659b 1960
50f97219
KS
1961 mx = s->mv[1][0][0];
1962 my = s->mv[1][0][1];
b761659b
DB
1963 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1964 uvmy = (my + ((my & 3) == 3)) >> 1;
cad16562
MSS
1965 if (v->field_mode) {
1966 if (v->cur_field_type != v->ref_field_type[1])
50f97219 1967 my = my - 2 + 4 * v->cur_field_type;
cad16562
MSS
1968 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1969 }
50f97219
KS
1970 if (v->fastuvmc) {
1971 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1972 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
b761659b 1973 }
657ccb5a
DB
1974 srcY = s->next_picture.f.data[0];
1975 srcU = s->next_picture.f.data[1];
1976 srcV = s->next_picture.f.data[2];
b761659b 1977
50f97219
KS
1978 src_x = s->mb_x * 16 + (mx >> 2);
1979 src_y = s->mb_y * 16 + (my >> 2);
1980 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1981 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
b761659b 1982
50f97219 1983 if (v->profile != PROFILE_ADVANCED) {
b761659b
DB
1984 src_x = av_clip( src_x, -16, s->mb_width * 16);
1985 src_y = av_clip( src_y, -16, s->mb_height * 16);
1986 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1987 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
50f97219 1988 } else {
b761659b
DB
1989 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1990 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1991 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1992 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1993 }
1994
50f97219 1995 srcY += src_y * s->linesize + src_x;
b761659b
DB
1996 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1997 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1998
cad16562
MSS
1999 if (v->field_mode && v->ref_field_type[1]) {
2000 srcY += s->current_picture_ptr->f.linesize[0];
2001 srcU += s->current_picture_ptr->f.linesize[1];
2002 srcV += s->current_picture_ptr->f.linesize[2];
2003 }
2004
b761659b 2005 /* for grayscale we should not try to read from unknown area */
50f97219 2006 if (s->flags & CODEC_FLAG_GRAY) {
b761659b
DB
2007 srcU = s->edge_emu_buffer + 18 * s->linesize;
2008 srcV = s->edge_emu_buffer + 18 * s->linesize;
2009 }
2010
50f97219
KS
2011 if (v->rangeredfrm
2012 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
2013 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
2014 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
b761659b
DB
2015
2016 srcY -= s->mspel * (1 + s->linesize);
50f97219
KS
2017 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
2018 17 + s->mspel * 2, 17 + s->mspel * 2,
2019 src_x - s->mspel, src_y - s->mspel,
2020 s->h_edge_pos, v_edge_pos);
b761659b 2021 srcY = s->edge_emu_buffer;
50f97219
KS
2022 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
2023 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
2024 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
2025 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
b761659b
DB
2026 srcU = uvbuf;
2027 srcV = uvbuf + 16;
2028 /* if we deal with range reduction we need to scale source blocks */
50f97219 2029 if (v->rangeredfrm) {
b761659b
DB
2030 int i, j;
2031 uint8_t *src, *src2;
2032
2033 src = srcY;
50f97219
KS
2034 for (j = 0; j < 17 + s->mspel * 2; j++) {
2035 for (i = 0; i < 17 + s->mspel * 2; i++)
2036 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
2037 src += s->linesize;
2038 }
50f97219
KS
2039 src = srcU;
2040 src2 = srcV;
2041 for (j = 0; j < 9; j++) {
2042 for (i = 0; i < 9; i++) {
2043 src[i] = ((src[i] - 128) >> 1) + 128;
b761659b
DB
2044 src2[i] = ((src2[i] - 128) >> 1) + 128;
2045 }
50f97219 2046 src += s->uvlinesize;
b761659b
DB
2047 src2 += s->uvlinesize;
2048 }
2049 }
2050 srcY += s->mspel * (1 + s->linesize);
2051 }
2052
cad16562 2053 if (v->field_mode && v->cur_field_type) {
50f97219 2054 off = s->current_picture_ptr->f.linesize[0];
cad16562
MSS
2055 off_uv = s->current_picture_ptr->f.linesize[1];
2056 } else {
50f97219 2057 off = 0;
cad16562
MSS
2058 off_uv = 0;
2059 }
2060
50f97219 2061 if (s->mspel) {
b761659b 2062 dxy = ((my & 3) << 2) | (mx & 3);
cad16562
MSS
2063 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2064 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
b761659b 2065 srcY += s->linesize * 8;
cad16562
MSS
2066 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2067 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
b761659b
DB
2068 } else { // hpel mc
2069 dxy = (my & 2) | ((mx & 2) >> 1);
2070
50f97219 2071 if (!v->rnd)
cad16562 2072 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b 2073 else
cad16562 2074 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
b761659b
DB
2075 }
2076
50f97219 2077 if (s->flags & CODEC_FLAG_GRAY) return;
b761659b 2078 /* Chroma MC always uses qpel blilinear */
50f97219
KS
2079 uvmx = (uvmx & 3) << 1;
2080 uvmy = (uvmy & 3) << 1;
2081 if (!v->rnd) {
cad16562
MSS
2082 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2083 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
50f97219 2084 } else {
cad16562
MSS
2085 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2086 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
b761659b
DB
2087 }
2088}
2089
2090static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2091{
2092 int n = bfrac;
2093
2094#if B_FRACTION_DEN==256
50f97219 2095 if (inv)
b761659b 2096 n -= 256;
50f97219 2097 if (!qs)
b761659b
DB
2098 return 2 * ((value * n + 255) >> 9);
2099 return (value * n + 128) >> 8;
2100#else
50f97219 2101 if (inv)
b761659b 2102 n -= B_FRACTION_DEN;
50f97219 2103 if (!qs)
b761659b
DB
2104 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2105 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2106#endif
2107}
2108
50f97219
KS
2109static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
2110 int qs, int qs_last)
cad16562
MSS
2111{
2112 int n = bfrac;
2113
2114 if (inv)
2115 n -= 256;
2116 n <<= !qs_last;
2117 if (!qs)
2118 return (value * n + 255) >> 9;
2119 else
2120 return (value * n + 128) >> 8;
2121}
2122
b761659b
DB
2123/** Reconstruct motion vector for B-frame and do motion compensation
2124 */
50f97219
KS
2125static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2126 int direct, int mode)
b761659b 2127{
50f97219 2128 if (v->use_ic) {
b761659b 2129 v->mv_mode2 = v->mv_mode;
50f97219 2130 v->mv_mode = MV_PMODE_INTENSITY_COMP;
b761659b 2131 }
50f97219 2132 if (direct) {
b761659b
DB
2133 vc1_mc_1mv(v, 0);
2134 vc1_interp_mc(v);
50f97219
KS
2135 if (v->use_ic)
2136 v->mv_mode = v->mv_mode2;
b761659b
DB
2137 return;
2138 }
50f97219 2139 if (mode == BMV_TYPE_INTERPOLATED) {
b761659b
DB
2140 vc1_mc_1mv(v, 0);
2141 vc1_interp_mc(v);
50f97219
KS
2142 if (v->use_ic)
2143 v->mv_mode = v->mv_mode2;
b761659b
DB
2144 return;
2145 }
2146
50f97219
KS
2147 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2148 v->mv_mode = v->mv_mode2;
b761659b 2149 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
50f97219
KS
2150 if (v->use_ic)
2151 v->mv_mode = v->mv_mode2;
b761659b
DB
2152}
2153
50f97219
KS
2154static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2155 int direct, int mvtype)
b761659b
DB
2156{
2157 MpegEncContext *s = &v->s;
2158 int xy, wrap, off = 0;
2159 int16_t *A, *B, *C;
2160 int px, py;
2161 int sum;
2162 int r_x, r_y;
2163 const uint8_t *is_intra = v->mb_type[0];
2164
2165 r_x = v->range_x;
2166 r_y = v->range_y;
2167 /* scale MV difference to be quad-pel */
2168 dmv_x[0] <<= 1 - s->quarter_sample;
2169 dmv_y[0] <<= 1 - s->quarter_sample;
2170 dmv_x[1] <<= 1 - s->quarter_sample;
2171 dmv_y[1] <<= 1 - s->quarter_sample;
2172
2173 wrap = s->b8_stride;
2174 xy = s->block_index[0];
2175
50f97219 2176 if (s->mb_intra) {
cad16562
MSS
2177 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2178 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2179 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2180 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
b761659b
DB
2181 return;
2182 }
cad16562
MSS
2183 if (!v->field_mode) {
2184 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2185 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2186 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2187 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
b761659b 2188
cad16562
MSS
2189 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2190 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));
2191 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));
2192 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));
2193 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));
2194 }
50f97219 2195 if (direct) {
cad16562
MSS
2196 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2197 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2198 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2199 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
b761659b
DB
2200 return;
2201 }
2202
50f97219
KS
2203 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2204 C = s->current_picture.f.motion_val[0][xy - 2];
2205 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
b761659b 2206 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
50f97219 2207 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
b761659b 2208
50f97219
KS
2209 if (!s->mb_x) C[0] = C[1] = 0;
2210 if (!s->first_slice_line) { // predictor A is not out of bounds
2211 if (s->mb_width == 1) {
b761659b
DB
2212 px = A[0];
2213 py = A[1];
2214 } else {
2215 px = mid_pred(A[0], B[0], C[0]);
2216 py = mid_pred(A[1], B[1], C[1]);
2217 }
50f97219 2218 } else if (s->mb_x) { // predictor C is not out of bounds
b761659b
DB
2219 px = C[0];
2220 py = C[1];
2221 } else {
2222 px = py = 0;
2223 }
2224 /* Pullback MV as specified in 8.3.5.3.4 */
2225 {
2226 int qx, qy, X, Y;
50f97219 2227 if (v->profile < PROFILE_ADVANCED) {
b761659b
DB
2228 qx = (s->mb_x << 5);
2229 qy = (s->mb_y << 5);
50f97219
KS
2230 X = (s->mb_width << 5) - 4;
2231 Y = (s->mb_height << 5) - 4;
2232 if (qx + px < -28) px = -28 - qx;
2233 if (qy + py < -28) py = -28 - qy;
2234 if (qx + px > X) px = X - qx;
2235 if (qy + py > Y) py = Y - qy;
b761659b
DB
2236 } else {
2237 qx = (s->mb_x << 6);
2238 qy = (s->mb_y << 6);
50f97219
KS
2239 X = (s->mb_width << 6) - 4;
2240 Y = (s->mb_height << 6) - 4;
2241 if (qx + px < -60) px = -60 - qx;
2242 if (qy + py < -60) py = -60 - qy;
2243 if (qx + px > X) px = X - qx;
2244 if (qy + py > Y) py = Y - qy;
b761659b
DB
2245 }
2246 }
2247 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
50f97219
KS
2248 if (0 && !s->first_slice_line && s->mb_x) {
2249 if (is_intra[xy - wrap])
b761659b
DB
2250 sum = FFABS(px) + FFABS(py);
2251 else
2252 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
50f97219
KS
2253 if (sum > 32) {
2254 if (get_bits1(&s->gb)) {
b761659b
DB
2255 px = A[0];
2256 py = A[1];
2257 } else {
2258 px = C[0];
2259 py = C[1];
2260 }
2261 } else {
50f97219 2262 if (is_intra[xy - 2])
b761659b
DB
2263 sum = FFABS(px) + FFABS(py);
2264 else
2265 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
50f97219
KS
2266 if (sum > 32) {
2267 if (get_bits1(&s->gb)) {
b761659b
DB
2268 px = A[0];
2269 py = A[1];
2270 } else {
2271 px = C[0];
2272 py = C[1];
2273 }
2274 }
2275 }
2276 }
2277 /* store MV using signed modulus of MV range defined in 4.11 */
2278 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2279 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2280 }
50f97219
KS
2281 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2282 C = s->current_picture.f.motion_val[1][xy - 2];
2283 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
b761659b 2284 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
50f97219 2285 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
b761659b 2286
50f97219
KS
2287 if (!s->mb_x)
2288 C[0] = C[1] = 0;
2289 if (!s->first_slice_line) { // predictor A is not out of bounds
2290 if (s->mb_width == 1) {
b761659b
DB
2291 px = A[0];
2292 py = A[1];
2293 } else {
2294 px = mid_pred(A[0], B[0], C[0]);
2295 py = mid_pred(A[1], B[1], C[1]);
2296 }
50f97219 2297 } else if (s->mb_x) { // predictor C is not out of bounds
b761659b
DB
2298 px = C[0];
2299 py = C[1];
2300 } else {
2301 px = py = 0;
2302 }
2303 /* Pullback MV as specified in 8.3.5.3.4 */
2304 {
2305 int qx, qy, X, Y;
50f97219 2306 if (v->profile < PROFILE_ADVANCED) {
b761659b
DB
2307 qx = (s->mb_x << 5);
2308 qy = (s->mb_y << 5);
50f97219
KS
2309 X = (s->mb_width << 5) - 4;
2310 Y = (s->mb_height << 5) - 4;
2311 if (qx + px < -28) px = -28 - qx;
2312 if (qy + py < -28) py = -28 - qy;
2313 if (qx + px > X) px = X - qx;
2314 if (qy + py > Y) py = Y - qy;
b761659b
DB
2315 } else {
2316 qx = (s->mb_x << 6);
2317 qy = (s->mb_y << 6);
50f97219
KS
2318 X = (s->mb_width << 6) - 4;
2319 Y = (s->mb_height << 6) - 4;
2320 if (qx + px < -60) px = -60 - qx;
2321 if (qy + py < -60) py = -60 - qy;
2322 if (qx + px > X) px = X - qx;
2323 if (qy + py > Y) py = Y - qy;
b761659b
DB
2324 }
2325 }
2326 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
50f97219
KS
2327 if (0 && !s->first_slice_line && s->mb_x) {
2328 if (is_intra[xy - wrap])
b761659b
DB
2329 sum = FFABS(px) + FFABS(py);
2330 else
2331 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
50f97219
KS
2332 if (sum > 32) {
2333 if (get_bits1(&s->gb)) {
b761659b
DB
2334 px = A[0];
2335 py = A[1];
2336 } else {
2337 px = C[0];
2338 py = C[1];
2339 }
2340 } else {
50f97219 2341 if (is_intra[xy - 2])
b761659b
DB
2342 sum = FFABS(px) + FFABS(py);
2343 else
2344 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
50f97219
KS
2345 if (sum > 32) {
2346 if (get_bits1(&s->gb)) {
b761659b
DB
2347 px = A[0];
2348 py = A[1];
2349 } else {
2350 px = C[0];
2351 py = C[1];
2352 }
2353 }
2354 }
2355 }
2356 /* store MV using signed modulus of MV range defined in 4.11 */
2357
2358 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2359 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2360 }
657ccb5a
DB
2361 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2362 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2363 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2364 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
b761659b
DB
2365}
2366
cad16562
MSS
2367static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2368{
2369 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2370 MpegEncContext *s = &v->s;
2371 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2372
2373 if (v->bmvtype == BMV_TYPE_DIRECT) {
2374 int total_opp, k, f;
2375 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
50f97219
KS
2376 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2377 v->bfraction, 0, s->quarter_sample, v->qs_last);
2378 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2379 v->bfraction, 0, s->quarter_sample, v->qs_last);
2380 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2381 v->bfraction, 1, s->quarter_sample, v->qs_last);
2382 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2383 v->bfraction, 1, s->quarter_sample, v->qs_last);
cad16562
MSS
2384
2385 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2386 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2387 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2388 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2389 f = (total_opp > 2) ? 1 : 0;
2390 } else {
2391 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2392 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2393 f = 0;
2394 }
2395 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2396 for (k = 0; k < 4; k++) {
2397 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2398 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2399 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2400 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2401 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2402 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2403 }
2404 return;
2405 }
2406 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2407 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);
2408 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);
2409 return;
2410 }
2411 if (dir) { // backward
2412 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);
2413 if (n == 3 || mv1) {
50f97219 2414 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
2415 }
2416 } else { // forward
2417 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);
2418 if (n == 3 || mv1) {
50f97219 2419 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
2420 }
2421 }
2422}
2423
b761659b
DB
2424/** Get predicted DC value for I-frames only
2425 * prediction dir: left=0, top=1
2426 * @param s MpegEncContext
2427 * @param overlap flag indicating that overlap filtering is used
2428 * @param pq integer part of picture quantizer
2429 * @param[in] n block index in the current MB
2430 * @param dc_val_ptr Pointer to DC predictor
2431 * @param dir_ptr Prediction direction for use in AC prediction
2432 */
2433static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
50f97219 2434 int16_t **dc_val_ptr, int *dir_ptr)
b761659b
DB
2435{
2436 int a, b, c, wrap, pred, scale;
2437 int16_t *dc_val;
2438 static const uint16_t dcpred[32] = {
50f97219
KS
2439 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2440 114, 102, 93, 85, 79, 73, 68, 64,
2441 60, 57, 54, 51, 49, 47, 45, 43,
2442 41, 39, 38, 37, 35, 34, 33
b761659b
DB
2443 };
2444
2445 /* find prediction - wmv3_dc_scale always used here in fact */
50f97219
KS
2446 if (n < 4) scale = s->y_dc_scale;
2447 else scale = s->c_dc_scale;
b761659b 2448
50f97219
KS
2449 wrap = s->block_wrap[n];
2450 dc_val = s->dc_val[0] + s->block_index[n];
b761659b
DB
2451
2452 /* B A
2453 * C X
2454 */
2455 c = dc_val[ - 1];
2456 b = dc_val[ - 1 - wrap];
2457 a = dc_val[ - wrap];
2458
50f97219 2459 if (pq < 9 || !overlap) {
b761659b 2460 /* Set outer values */
50f97219
KS
2461 if (s->first_slice_line && (n != 2 && n != 3))
2462 b = a = dcpred[scale];
2463 if (s->mb_x == 0 && (n != 1 && n != 3))
2464 b = c = dcpred[scale];
2465 } else {
b761659b 2466 /* Set outer values */
50f97219
KS
2467 if (s->first_slice_line && (n != 2 && n != 3))
2468 b = a = 0;
2469 if (s->mb_x == 0 && (n != 1 && n != 3))
2470 b = c = 0;
b761659b
DB
2471 }
2472
2473 if (abs(a - b) <= abs(b - c)) {
50f97219
KS
2474 pred = c;
2475 *dir_ptr = 1; // left
b761659b 2476 } else {
50f97219
KS
2477 pred = a;
2478 *dir_ptr = 0; // top
b761659b
DB
2479 }
2480
2481 /* update predictor */
2482 *dc_val_ptr = &dc_val[0];
2483 return pred;
2484}
2485
2486
2487/** Get predicted DC value
2488 * prediction dir: left=0, top=1
2489 * @param s MpegEncContext
2490 * @param overlap flag indicating that overlap filtering is used
2491 * @param pq integer part of picture quantizer
2492 * @param[in] n block index in the current MB
2493 * @param a_avail flag indicating top block availability
2494 * @param c_avail flag indicating left block availability
2495 * @param dc_val_ptr Pointer to DC predictor
2496 * @param dir_ptr Prediction direction for use in AC prediction
2497 */
2498static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2499 int a_avail, int c_avail,
2500 int16_t **dc_val_ptr, int *dir_ptr)
2501{
2502 int a, b, c, wrap, pred;
2503 int16_t *dc_val;
2504 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2505 int q1, q2 = 0;
2506
2507 wrap = s->block_wrap[n];
50f97219 2508 dc_val = s->dc_val[0] + s->block_index[n];
b761659b
DB
2509
2510 /* B A
2511 * C X
2512 */
2513 c = dc_val[ - 1];
2514 b = dc_val[ - 1 - wrap];
2515 a = dc_val[ - wrap];
2516 /* scale predictors if needed */
657ccb5a 2517 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219 2518 if (c_avail && (n != 1 && n != 3)) {
657ccb5a 2519 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
50f97219 2520 if (q2 && q2 != q1)
b761659b
DB
2521 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2522 }
50f97219 2523 if (a_avail && (n != 2 && n != 3)) {
657ccb5a 2524 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
50f97219 2525 if (q2 && q2 != q1)
b761659b
DB
2526 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2527 }
50f97219 2528 if (a_avail && c_avail && (n != 3)) {
b761659b 2529 int off = mb_pos;
50f97219
KS
2530 if (n != 1)
2531 off--;
2532 if (n != 2)
2533 off -= s->mb_stride;
657ccb5a 2534 q2 = s->current_picture.f.qscale_table[off];
50f97219 2535 if (q2 && q2 != q1)
b761659b
DB
2536 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
2537 }
2538
50f97219
KS
2539 if (a_avail && c_avail) {
2540 if (abs(a - b) <= abs(b - c)) {
2541 pred = c;
2542 *dir_ptr = 1; // left
b761659b 2543 } else {
50f97219
KS
2544 pred = a;
2545 *dir_ptr = 0; // top
2546 }
2547 } else if (a_avail) {
2548 pred = a;
2549 *dir_ptr = 0; // top
2550 } else if (c_avail) {
2551 pred = c;
2552 *dir_ptr = 1; // left
b761659b 2553 } else {
50f97219
KS
2554 pred = 0;
2555 *dir_ptr = 1; // left
b761659b
DB
2556 }
2557
2558 /* update predictor */
2559 *dc_val_ptr = &dc_val[0];
2560 return pred;
2561}
2562
2563/** @} */ // Block group
2564
2565/**
21a19b79 2566 * @name VC1 Macroblock-level functions in Simple/Main Profiles
b761659b
DB
2567 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2568 * @{
2569 */
2570
50f97219
KS
2571static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2572 uint8_t **coded_block_ptr)
b761659b
DB
2573{
2574 int xy, wrap, pred, a, b, c;
2575
50f97219 2576 xy = s->block_index[n];
b761659b
DB
2577 wrap = s->b8_stride;
2578
2579 /* B C
2580 * A X
2581 */
2582 a = s->coded_block[xy - 1 ];
2583 b = s->coded_block[xy - 1 - wrap];
2584 c = s->coded_block[xy - wrap];
2585
2586 if (b == c) {
2587 pred = a;
2588 } else {
2589 pred = c;
2590 }
2591
2592 /* store value */
2593 *coded_block_ptr = &s->coded_block[xy];
2594
2595 return pred;
2596}
2597
2598/**
2599 * Decode one AC coefficient
2600 * @param v The VC1 context
2601 * @param last Last coefficient
2602 * @param skip How much zero coefficients to skip
2603 * @param value Decoded AC coefficient value
2604 * @param codingset set of VLC to decode data
2605 * @see 8.1.3.4
2606 */
50f97219
KS
2607static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2608 int *value, int codingset)
b761659b
DB
2609{
2610 GetBitContext *gb = &v->s.gb;
2611 int index, escape, run = 0, level = 0, lst = 0;
2612
2613 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2614 if (index != vc1_ac_sizes[codingset] - 1) {
50f97219 2615 run = vc1_index_decode_table[codingset][index][0];
b761659b 2616 level = vc1_index_decode_table[codingset][index][1];
50f97219
KS
2617 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2618 if (get_bits1(gb))
b761659b
DB
2619 level = -level;
2620 } else {
2621 escape = decode210(gb);
2622 if (escape != 2) {
2623 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
50f97219 2624 run = vc1_index_decode_table[codingset][index][0];
b761659b 2625 level = vc1_index_decode_table[codingset][index][1];
50f97219
KS
2626 lst = index >= vc1_last_decode_table[codingset];
2627 if (escape == 0) {
2628 if (lst)
b761659b
DB
2629 level += vc1_last_delta_level_table[codingset][run];
2630 else
2631 level += vc1_delta_level_table[codingset][run];
2632 } else {
50f97219 2633 if (lst)
b761659b
DB
2634 run += vc1_last_delta_run_table[codingset][level] + 1;
2635 else
2636 run += vc1_delta_run_table[codingset][level] + 1;
2637 }
50f97219 2638 if (get_bits1(gb))
b761659b
DB
2639 level = -level;
2640 } else {
2641 int sign;
2642 lst = get_bits1(gb);
50f97219
KS
2643 if (v->s.esc3_level_length == 0) {
2644 if (v->pq < 8 || v->dquantfrm) { // table 59
b761659b 2645 v->s.esc3_level_length = get_bits(gb, 3);
50f97219 2646 if (!v->s.esc3_level_length)
b761659b 2647 v->s.esc3_level_length = get_bits(gb, 2) + 8;
50f97219 2648 } else { // table 60
b761659b
DB
2649 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2650 }
2651 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2652 }
50f97219
KS
2653 run = get_bits(gb, v->s.esc3_run_length);
2654 sign = get_bits1(gb);
b761659b 2655 level = get_bits(gb, v->s.esc3_level_length);
50f97219 2656 if (sign)
b761659b
DB
2657 level = -level;
2658 }
2659 }
2660
50f97219
KS
2661 *last = lst;
2662 *skip = run;
b761659b
DB
2663 *value = level;
2664}
2665
2666/** Decode intra block in intra frames - should be faster than decode_intra_block
2667 * @param v VC1Context
2668 * @param block block to decode
2669 * @param[in] n subblock index
2670 * @param coded are AC coeffs present or not
2671 * @param codingset set of VLC to decode data
2672 */
50f97219
KS
2673static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2674 int coded, int codingset)
b761659b
DB
2675{
2676 GetBitContext *gb = &v->s.gb;
2677 MpegEncContext *s = &v->s;
2678 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2679 int i;
2680 int16_t *dc_val;
2681 int16_t *ac_val, *ac_val2;
2682 int dcdiff;
2683
2684 /* Get DC differential */
2685 if (n < 4) {
2686 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2687 } else {
2688 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2689 }
50f97219 2690 if (dcdiff < 0) {
b761659b
DB
2691 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2692 return -1;
2693 }
50f97219
KS
2694 if (dcdiff) {
2695 if (dcdiff == 119 /* ESC index value */) {
b761659b 2696 /* TODO: Optimize */
50f97219 2697 if (v->pq == 1) dcdiff = get_bits(gb, 10);
b761659b 2698 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
2699 else dcdiff = get_bits(gb, 8);
2700 } else {
b761659b 2701 if (v->pq == 1)
50f97219 2702 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 2703 else if (v->pq == 2)
50f97219 2704 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
2705 }
2706 if (get_bits1(gb))
2707 dcdiff = -dcdiff;
2708 }
2709
2710 /* Prediction */
2711 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2712 *dc_val = dcdiff;
2713
2714 /* Store the quantized DC coeff, used for prediction */
2715 if (n < 4) {
2716 block[0] = dcdiff * s->y_dc_scale;
2717 } else {
2718 block[0] = dcdiff * s->c_dc_scale;
2719 }
2720 /* Skip ? */
2721 if (!coded) {
2722 goto not_coded;
2723 }
2724
50f97219 2725 // AC Decoding
b761659b
DB
2726 i = 1;
2727
2728 {
2729 int last = 0, skip, value;
0724a674 2730 const uint8_t *zz_table;
b761659b
DB
2731 int scale;
2732 int k;
2733
2734 scale = v->pq * 2 + v->halfpq;
2735
50f97219
KS
2736 if (v->s.ac_pred) {
2737 if (!dc_pred_dir)
1da6ea39 2738 zz_table = v->zz_8x8[2];
b761659b 2739 else
1da6ea39 2740 zz_table = v->zz_8x8[3];
b761659b 2741 } else
1da6ea39 2742 zz_table = v->zz_8x8[1];
b761659b 2743
50f97219 2744 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b 2745 ac_val2 = ac_val;
50f97219 2746 if (dc_pred_dir) // left
b761659b 2747 ac_val -= 16;
50f97219 2748 else // top
b761659b
DB
2749 ac_val -= 16 * s->block_wrap[n];
2750
2751 while (!last) {
2752 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2753 i += skip;
50f97219 2754 if (i > 63)
b761659b
DB
2755 break;
2756 block[zz_table[i++]] = value;
2757 }
2758
2759 /* apply AC prediction if needed */
50f97219
KS
2760 if (s->ac_pred) {
2761 if (dc_pred_dir) { // left
2762 for (k = 1; k < 8; k++)
58bb6b7d 2763 block[k << v->left_blk_sh] += ac_val[k];
50f97219
KS
2764 } else { // top
2765 for (k = 1; k < 8; k++)
58bb6b7d 2766 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
2767 }
2768 }
2769 /* save AC coeffs for further prediction */
50f97219 2770 for (k = 1; k < 8; k++) {
58bb6b7d
RB
2771 ac_val2[k] = block[k << v->left_blk_sh];
2772 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
2773 }
2774
2775 /* scale AC coeffs */
50f97219
KS
2776 for (k = 1; k < 64; k++)
2777 if (block[k]) {
b761659b 2778 block[k] *= scale;
50f97219 2779 if (!v->pquantizer)
b761659b
DB
2780 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2781 }
2782
50f97219 2783 if (s->ac_pred) i = 63;
b761659b
DB
2784 }
2785
2786not_coded:
50f97219 2787 if (!coded) {
b761659b 2788 int k, scale;
50f97219 2789 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b
DB
2790 ac_val2 = ac_val;
2791
2792 i = 0;
2793 scale = v->pq * 2 + v->halfpq;
2794 memset(ac_val2, 0, 16 * 2);
50f97219 2795 if (dc_pred_dir) { // left
b761659b 2796 ac_val -= 16;
50f97219 2797 if (s->ac_pred)
b761659b 2798 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 2799 } else { // top
b761659b 2800 ac_val -= 16 * s->block_wrap[n];
50f97219 2801 if (s->ac_pred)
b761659b
DB
2802 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2803 }
2804
2805 /* apply AC prediction if needed */
50f97219
KS
2806 if (s->ac_pred) {
2807 if (dc_pred_dir) { //left
2808 for (k = 1; k < 8; k++) {
58bb6b7d 2809 block[k << v->left_blk_sh] = ac_val[k] * scale;
50f97219 2810 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 2811 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
b761659b 2812 }
50f97219
KS
2813 } else { // top
2814 for (k = 1; k < 8; k++) {
58bb6b7d 2815 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
50f97219 2816 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 2817 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
b761659b
DB
2818 }
2819 }
2820 i = 63;
2821 }
2822 }
2823 s->block_last_index[n] = i;
2824
2825 return 0;
2826}
2827
2828/** Decode intra block in intra frames - should be faster than decode_intra_block
2829 * @param v VC1Context
2830 * @param block block to decode
2831 * @param[in] n subblock number
2832 * @param coded are AC coeffs present or not
2833 * @param codingset set of VLC to decode data
2834 * @param mquant quantizer value for this macroblock
2835 */
50f97219
KS
2836static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2837 int coded, int codingset, int mquant)
b761659b
DB
2838{
2839 GetBitContext *gb = &v->s.gb;
2840 MpegEncContext *s = &v->s;
2841 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2842 int i;
2843 int16_t *dc_val;
2844 int16_t *ac_val, *ac_val2;
2845 int dcdiff;
2846 int a_avail = v->a_avail, c_avail = v->c_avail;
2847 int use_pred = s->ac_pred;
2848 int scale;
2849 int q1, q2 = 0;
2850 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2851
2852 /* Get DC differential */
2853 if (n < 4) {
2854 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2855 } else {
2856 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2857 }
50f97219 2858 if (dcdiff < 0) {
b761659b
DB
2859 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2860 return -1;
2861 }
50f97219
KS
2862 if (dcdiff) {
2863 if (dcdiff == 119 /* ESC index value */) {
b761659b 2864 /* TODO: Optimize */
50f97219 2865 if (mquant == 1) dcdiff = get_bits(gb, 10);
b761659b 2866 else if (mquant == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
2867 else dcdiff = get_bits(gb, 8);
2868 } else {
b761659b 2869 if (mquant == 1)
50f97219 2870 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 2871 else if (mquant == 2)
50f97219 2872 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
2873 }
2874 if (get_bits1(gb))
2875 dcdiff = -dcdiff;
2876 }
2877
2878 /* Prediction */
2879 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2880 *dc_val = dcdiff;
2881
2882 /* Store the quantized DC coeff, used for prediction */
2883 if (n < 4) {
2884 block[0] = dcdiff * s->y_dc_scale;
2885 } else {
2886 block[0] = dcdiff * s->c_dc_scale;
2887 }
2888
2889 //AC Decoding
2890 i = 1;
2891
2892 /* check if AC is needed at all */
50f97219
KS
2893 if (!a_avail && !c_avail)
2894 use_pred = 0;
2895 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
b761659b
DB
2896 ac_val2 = ac_val;
2897
2898 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2899
50f97219 2900 if (dc_pred_dir) // left
b761659b 2901 ac_val -= 16;
50f97219 2902 else // top
b761659b
DB
2903 ac_val -= 16 * s->block_wrap[n];
2904
657ccb5a 2905 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219
KS
2906 if ( dc_pred_dir && c_avail && mb_pos)
2907 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2908 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2909 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2910 if ( dc_pred_dir && n == 1)
2911 q2 = q1;
2912 if (!dc_pred_dir && n == 2)
2913 q2 = q1;
2914 if (n == 3)
2915 q2 = q1;
2916
2917 if (coded) {
b761659b 2918 int last = 0, skip, value;
0724a674 2919 const uint8_t *zz_table;
b761659b
DB
2920 int k;
2921
50f97219 2922 if (v->s.ac_pred) {
cad16562
MSS
2923 if (!use_pred && v->fcm == 1) {
2924 zz_table = v->zzi_8x8;
2925 } else {
50f97219 2926 if (!dc_pred_dir) // top
cad16562 2927 zz_table = v->zz_8x8[2];
50f97219 2928 else // left
cad16562
MSS
2929 zz_table = v->zz_8x8[3];
2930 }
2931 } else {
2932 if (v->fcm != 1)
2933 zz_table = v->zz_8x8[1];
b761659b 2934 else
cad16562
MSS
2935 zz_table = v->zzi_8x8;
2936 }
b761659b
DB
2937
2938 while (!last) {
2939 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2940 i += skip;
50f97219 2941 if (i > 63)
b761659b
DB
2942 break;
2943 block[zz_table[i++]] = value;
2944 }
2945
2946 /* apply AC prediction if needed */
50f97219 2947 if (use_pred) {
b761659b 2948 /* scale predictors if needed*/
50f97219 2949 if (q2 && q1 != q2) {
b761659b
DB
2950 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2951 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2952
50f97219
KS
2953 if (dc_pred_dir) { // left
2954 for (k = 1; k < 8; k++)
58bb6b7d 2955 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
50f97219
KS
2956 } else { // top
2957 for (k = 1; k < 8; k++)
58bb6b7d 2958 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b
DB
2959 }
2960 } else {
50f97219
KS
2961 if (dc_pred_dir) { //left
2962 for (k = 1; k < 8; k++)
58bb6b7d 2963 block[k << v->left_blk_sh] += ac_val[k];
b761659b 2964 } else { //top
50f97219 2965 for (k = 1; k < 8; k++)
58bb6b7d 2966 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
2967 }
2968 }
2969 }
2970 /* save AC coeffs for further prediction */
50f97219 2971 for (k = 1; k < 8; k++) {
58bb6b7d
RB
2972 ac_val2[k ] = block[k << v->left_blk_sh];
2973 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
2974 }
2975
2976 /* scale AC coeffs */
50f97219
KS
2977 for (k = 1; k < 64; k++)
2978 if (block[k]) {
b761659b 2979 block[k] *= scale;
50f97219 2980 if (!v->pquantizer)
b761659b
DB
2981 block[k] += (block[k] < 0) ? -mquant : mquant;
2982 }
2983
50f97219 2984 if (use_pred) i = 63;
b761659b
DB
2985 } else { // no AC coeffs
2986 int k;
2987
2988 memset(ac_val2, 0, 16 * 2);
50f97219
KS
2989 if (dc_pred_dir) { // left
2990 if (use_pred) {
b761659b 2991 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 2992 if (q2 && q1 != q2) {
b761659b
DB
2993 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2994 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 2995 for (k = 1; k < 8; k++)
b761659b
DB
2996 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2997 }
2998 }
50f97219
KS
2999 } else { // top
3000 if (use_pred) {
b761659b 3001 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50f97219 3002 if (q2 && q1 != q2) {
b761659b
DB
3003 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3004 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3005 for (k = 1; k < 8; k++)
b761659b
DB
3006 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3007 }
3008 }
3009 }
3010
3011 /* apply AC prediction if needed */
50f97219
KS
3012 if (use_pred) {
3013 if (dc_pred_dir) { // left
3014 for (k = 1; k < 8; k++) {
58bb6b7d 3015 block[k << v->left_blk_sh] = ac_val2[k] * scale;
50f97219 3016 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 3017 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
b761659b 3018 }
50f97219
KS
3019 } else { // top
3020 for (k = 1; k < 8; k++) {
58bb6b7d 3021 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
50f97219 3022 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 3023 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
b761659b
DB
3024 }
3025 }
3026 i = 63;
3027 }
3028 }
3029 s->block_last_index[n] = i;
3030
3031 return 0;
3032}
3033
3034/** Decode intra block in inter frames - more generic version than vc1_decode_i_block
3035 * @param v VC1Context
3036 * @param block block to decode
3037 * @param[in] n subblock index
3038 * @param coded are AC coeffs present or not
3039 * @param mquant block quantizer
3040 * @param codingset set of VLC to decode data
3041 */
50f97219
KS
3042static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
3043 int coded, int mquant, int codingset)
b761659b
DB
3044{
3045 GetBitContext *gb = &v->s.gb;
3046 MpegEncContext *s = &v->s;
3047 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3048 int i;
3049 int16_t *dc_val;
3050 int16_t *ac_val, *ac_val2;
3051 int dcdiff;
3052 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3053 int a_avail = v->a_avail, c_avail = v->c_avail;
3054 int use_pred = s->ac_pred;
3055 int scale;
3056 int q1, q2 = 0;
3057
010f98f9
JGG
3058 s->dsp.clear_block(block);
3059
b761659b 3060 /* XXX: Guard against dumb values of mquant */
50f97219 3061 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
b761659b
DB
3062
3063 /* Set DC scale - y and c use the same */
3064 s->y_dc_scale = s->y_dc_scale_table[mquant];
3065 s->c_dc_scale = s->c_dc_scale_table[mquant];
3066
3067 /* Get DC differential */
3068 if (n < 4) {
3069 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3070 } else {
3071 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3072 }
50f97219 3073 if (dcdiff < 0) {
b761659b
DB
3074 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3075 return -1;
3076 }
50f97219
KS
3077 if (dcdiff) {
3078 if (dcdiff == 119 /* ESC index value */) {
b761659b 3079 /* TODO: Optimize */
50f97219 3080 if (mquant == 1) dcdiff = get_bits(gb, 10);
b761659b 3081 else if (mquant == 2) dcdiff = get_bits(gb, 9);
50f97219
KS
3082 else dcdiff = get_bits(gb, 8);
3083 } else {
b761659b 3084 if (mquant == 1)
50f97219 3085 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
b761659b 3086 else if (mquant == 2)
50f97219 3087 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
b761659b
DB
3088 }
3089 if (get_bits1(gb))
3090 dcdiff = -dcdiff;
3091 }
3092
3093 /* Prediction */
3094 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3095 *dc_val = dcdiff;
3096
3097 /* Store the quantized DC coeff, used for prediction */
3098
3099 if (n < 4) {
3100 block[0] = dcdiff * s->y_dc_scale;
3101 } else {
3102 block[0] = dcdiff * s->c_dc_scale;
3103 }
3104
3105 //AC Decoding
3106 i = 1;
3107
3108 /* check if AC is needed at all and adjust direction if needed */
50f97219
KS
3109 if (!a_avail) dc_pred_dir = 1;
3110 if (!c_avail) dc_pred_dir = 0;
3111 if (!a_avail && !c_avail) use_pred = 0;
b761659b
DB
3112 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3113 ac_val2 = ac_val;
3114
3115 scale = mquant * 2 + v->halfpq;
3116
50f97219 3117 if (dc_pred_dir) //left
b761659b
DB
3118 ac_val -= 16;
3119 else //top
3120 ac_val -= 16 * s->block_wrap[n];
3121
657ccb5a 3122 q1 = s->current_picture.f.qscale_table[mb_pos];
50f97219
KS
3123 if (dc_pred_dir && c_avail && mb_pos)
3124 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3125 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3126 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3127 if ( dc_pred_dir && n == 1)
3128 q2 = q1;
3129 if (!dc_pred_dir && n == 2)
3130 q2 = q1;
3131 if (n == 3) q2 = q1;
b761659b 3132
50f97219 3133 if (coded) {
b761659b 3134 int last = 0, skip, value;
b761659b
DB
3135 int k;
3136
b761659b
DB
3137 while (!last) {
3138 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3139 i += skip;
50f97219 3140 if (i > 63)
b761659b 3141 break;
cad16562
MSS
3142 if (v->fcm == 0)
3143 block[v->zz_8x8[0][i++]] = value;
3144 else {
50f97219
KS
3145 if (use_pred && (v->fcm == 1)) {
3146 if (!dc_pred_dir) // top
cad16562 3147 block[v->zz_8x8[2][i++]] = value;
50f97219 3148 else // left
cad16562
MSS
3149 block[v->zz_8x8[3][i++]] = value;
3150 } else {
3151 block[v->zzi_8x8[i++]] = value;
3152 }
3153 }
b761659b
DB
3154 }
3155
3156 /* apply AC prediction if needed */
50f97219 3157 if (use_pred) {
b761659b 3158 /* scale predictors if needed*/
50f97219 3159 if (q2 && q1 != q2) {
b761659b
DB
3160 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3161 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3162
50f97219
KS
3163 if (dc_pred_dir) { // left
3164 for (k = 1; k < 8; k++)
58bb6b7d 3165 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b 3166 } else { //top
50f97219 3167 for (k = 1; k < 8; k++)
58bb6b7d 3168 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
b761659b
DB
3169 }
3170 } else {
50f97219
KS
3171 if (dc_pred_dir) { // left
3172 for (k = 1; k < 8; k++)
58bb6b7d 3173 block[k << v->left_blk_sh] += ac_val[k];
50f97219
KS
3174 } else { // top
3175 for (k = 1; k < 8; k++)
58bb6b7d 3176 block[k << v->top_blk_sh] += ac_val[k + 8];
b761659b
DB
3177 }
3178 }
3179 }
3180 /* save AC coeffs for further prediction */
50f97219 3181 for (k = 1; k < 8; k++) {
58bb6b7d
RB
3182 ac_val2[k ] = block[k << v->left_blk_sh];
3183 ac_val2[k + 8] = block[k << v->top_blk_sh];
b761659b
DB
3184 }
3185
3186 /* scale AC coeffs */
50f97219
KS
3187 for (k = 1; k < 64; k++)
3188 if (block[k]) {
b761659b 3189 block[k] *= scale;
50f97219 3190 if (!v->pquantizer)
b761659b
DB
3191 block[k] += (block[k] < 0) ? -mquant : mquant;
3192 }
3193
50f97219 3194 if (use_pred) i = 63;
b761659b
DB
3195 } else { // no AC coeffs
3196 int k;
3197
3198 memset(ac_val2, 0, 16 * 2);
50f97219
KS
3199 if (dc_pred_dir) { // left
3200 if (use_pred) {
b761659b 3201 memcpy(ac_val2, ac_val, 8 * 2);
50f97219 3202 if (q2 && q1 != q2) {
b761659b
DB
3203 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3204 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3205 for (k = 1; k < 8; k++)
b761659b
DB
3206 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3207 }
3208 }
50f97219
KS
3209 } else { // top
3210 if (use_pred) {
b761659b 3211 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
50f97219 3212 if (q2 && q1 != q2) {
b761659b
DB
3213 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3214 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
50f97219 3215 for (k = 1; k < 8; k++)
b761659b
DB
3216 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3217 }
3218 }
3219 }
3220
3221 /* apply AC prediction if needed */
50f97219
KS
3222 if (use_pred) {
3223 if (dc_pred_dir) { // left
3224 for (k = 1; k < 8; k++) {
58bb6b7d 3225 block[k << v->left_blk_sh] = ac_val2[k] * scale;
50f97219 3226 if (!v->pquantizer && block[k << v->left_blk_sh])
58bb6b7d 3227 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
b761659b 3228 }
50f97219
KS
3229 } else { // top
3230 for (k = 1; k < 8; k++) {
58bb6b7d 3231 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
50f97219 3232 if (!v->pquantizer && block[k << v->top_blk_sh])
58bb6b7d 3233 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
b761659b
DB
3234 }
3235 }
3236 i = 63;
3237 }
3238 }
3239 s->block_last_index[n] = i;
3240
3241 return 0;
3242}
3243
3244/** Decode P block
3245 */
50f97219
KS
3246static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3247 int mquant, int ttmb, int first_block,
3248 uint8_t *dst, int linesize, int skip_block,
3249 int *ttmb_out)
b761659b
DB
3250{
3251 MpegEncContext *s = &v->s;
3252 GetBitContext *gb = &s->gb;
3253 int i, j;
3254 int subblkpat = 0;
3255 int scale, off, idx, last, skip, value;
3256 int ttblk = ttmb & 7;
3257 int pat = 0;
3258
010f98f9
JGG
3259 s->dsp.clear_block(block);
3260
50f97219 3261 if (ttmb == -1) {
b761659b
DB
3262 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)];
3263 }
50f97219 3264 if (ttblk == TT_4X4) {
b761659b
DB
3265 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3266 }
50f97219 3267 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
42ff9d7a
KS
3268 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3269 || (!v->res_rtm_flag && !first_block))) {
b761659b 3270 subblkpat = decode012(gb);
50f97219
KS
3271 if (subblkpat)
3272 subblkpat ^= 3; // swap decoded pattern bits
3273 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3274 ttblk = TT_8X4;
3275 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3276 ttblk = TT_4X8;
b761659b
DB
3277 }
3278 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3279
3280 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
50f97219 3281 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
b761659b 3282 subblkpat = 2 - (ttblk == TT_8X4_TOP);
50f97219 3283 ttblk = TT_8X4;
b761659b 3284 }
50f97219 3285 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
b761659b 3286 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
50f97219 3287 ttblk = TT_4X8;
b761659b 3288 }
50f97219 3289 switch (ttblk) {
b761659b 3290 case TT_8X8:
50f97219
KS
3291 pat = 0xF;
3292 i = 0;
b761659b
DB
3293 last = 0;
3294 while (!last) {
3295 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3296 i += skip;
50f97219 3297 if (i > 63)
b761659b 3298 break;
cad16562
MSS
3299 if (!v->interlace)
3300 idx = v->zz_8x8[0][i++];