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