vdpau: remove old-style decoders
[libav.git] / libavcodec / vc1dec.c
1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 *
7 * This file is part of Libav.
8 *
9 * Libav is free software; you can redistribute it and/or
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 *
14 * Libav is distributed in the hope that it will be useful,
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
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 decoder
27 */
28
29 #include "internal.h"
30 #include "avcodec.h"
31 #include "error_resilience.h"
32 #include "mpegvideo.h"
33 #include "h263.h"
34 #include "h264chroma.h"
35 #include "vc1.h"
36 #include "vc1data.h"
37 #include "vc1acdata.h"
38 #include "msmpeg4data.h"
39 #include "unary.h"
40 #include "mathops.h"
41
42 #undef NDEBUG
43 #include <assert.h>
44
45 #define MB_INTRA_VLC_BITS 9
46 #define DC_VLC_BITS 9
47
48
49 // offset tables for interlaced picture MVDATA decoding
50 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
51 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
52
53 /***********************************************************************/
54 /**
55 * @name VC-1 Bitplane decoding
56 * @see 8.7, p56
57 * @{
58 */
59
60 /**
61 * Imode types
62 * @{
63 */
64 enum Imode {
65 IMODE_RAW,
66 IMODE_NORM2,
67 IMODE_DIFF2,
68 IMODE_NORM6,
69 IMODE_DIFF6,
70 IMODE_ROWSKIP,
71 IMODE_COLSKIP
72 };
73 /** @} */ //imode defines
74
75 static void init_block_index(VC1Context *v)
76 {
77 MpegEncContext *s = &v->s;
78 ff_init_block_index(s);
79 if (v->field_mode && !(v->second_field ^ v->tff)) {
80 s->dest[0] += s->current_picture_ptr->f.linesize[0];
81 s->dest[1] += s->current_picture_ptr->f.linesize[1];
82 s->dest[2] += s->current_picture_ptr->f.linesize[2];
83 }
84 }
85
86 /** @} */ //Bitplane group
87
88 static void vc1_put_signed_blocks_clamped(VC1Context *v)
89 {
90 MpegEncContext *s = &v->s;
91 int topleft_mb_pos, top_mb_pos;
92 int stride_y, fieldtx = 0;
93 int v_dist;
94
95 /* The put pixels loop is always one MB row behind the decoding loop,
96 * because we can only put pixels when overlap filtering is done, and
97 * for filtering of the bottom edge of a MB, we need the next MB row
98 * present as well.
99 * Within the row, the put pixels loop is also one MB col behind the
100 * decoding loop. The reason for this is again, because for filtering
101 * of the right MB edge, we need the next MB present. */
102 if (!s->first_slice_line) {
103 if (s->mb_x) {
104 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
105 if (v->fcm == ILACE_FRAME)
106 fieldtx = v->fieldtx_plane[topleft_mb_pos];
107 stride_y = s->linesize << fieldtx;
108 v_dist = (16 - fieldtx) >> (fieldtx == 0);
109 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
110 s->dest[0] - 16 * s->linesize - 16,
111 stride_y);
112 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
113 s->dest[0] - 16 * s->linesize - 8,
114 stride_y);
115 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
116 s->dest[0] - v_dist * s->linesize - 16,
117 stride_y);
118 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
119 s->dest[0] - v_dist * s->linesize - 8,
120 stride_y);
121 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
122 s->dest[1] - 8 * s->uvlinesize - 8,
123 s->uvlinesize);
124 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
125 s->dest[2] - 8 * s->uvlinesize - 8,
126 s->uvlinesize);
127 }
128 if (s->mb_x == s->mb_width - 1) {
129 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
130 if (v->fcm == ILACE_FRAME)
131 fieldtx = v->fieldtx_plane[top_mb_pos];
132 stride_y = s->linesize << fieldtx;
133 v_dist = fieldtx ? 15 : 8;
134 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
135 s->dest[0] - 16 * s->linesize,
136 stride_y);
137 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
138 s->dest[0] - 16 * s->linesize + 8,
139 stride_y);
140 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
141 s->dest[0] - v_dist * s->linesize,
142 stride_y);
143 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
144 s->dest[0] - v_dist * s->linesize + 8,
145 stride_y);
146 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
147 s->dest[1] - 8 * s->uvlinesize,
148 s->uvlinesize);
149 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
150 s->dest[2] - 8 * s->uvlinesize,
151 s->uvlinesize);
152 }
153 }
154
155 #define inc_blk_idx(idx) do { \
156 idx++; \
157 if (idx >= v->n_allocated_blks) \
158 idx = 0; \
159 } while (0)
160
161 inc_blk_idx(v->topleft_blk_idx);
162 inc_blk_idx(v->top_blk_idx);
163 inc_blk_idx(v->left_blk_idx);
164 inc_blk_idx(v->cur_blk_idx);
165 }
166
167 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
168 {
169 MpegEncContext *s = &v->s;
170 int j;
171 if (!s->first_slice_line) {
172 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
173 if (s->mb_x)
174 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
176 for (j = 0; j < 2; j++) {
177 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
178 if (s->mb_x)
179 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
180 }
181 }
182 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
183
184 if (s->mb_y == s->end_mb_y - 1) {
185 if (s->mb_x) {
186 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
187 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
188 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
189 }
190 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
191 }
192 }
193
194 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
195 {
196 MpegEncContext *s = &v->s;
197 int j;
198
199 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
200 * means it runs two rows/cols behind the decoding loop. */
201 if (!s->first_slice_line) {
202 if (s->mb_x) {
203 if (s->mb_y >= s->start_mb_y + 2) {
204 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
205
206 if (s->mb_x >= 2)
207 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
208 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
209 for (j = 0; j < 2; j++) {
210 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
211 if (s->mb_x >= 2) {
212 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
213 }
214 }
215 }
216 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
217 }
218
219 if (s->mb_x == s->mb_width - 1) {
220 if (s->mb_y >= s->start_mb_y + 2) {
221 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
222
223 if (s->mb_x)
224 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
225 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
226 for (j = 0; j < 2; j++) {
227 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
228 if (s->mb_x >= 2) {
229 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
230 }
231 }
232 }
233 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
234 }
235
236 if (s->mb_y == s->end_mb_y) {
237 if (s->mb_x) {
238 if (s->mb_x >= 2)
239 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
241 if (s->mb_x >= 2) {
242 for (j = 0; j < 2; j++) {
243 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
244 }
245 }
246 }
247
248 if (s->mb_x == s->mb_width - 1) {
249 if (s->mb_x)
250 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
251 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
252 if (s->mb_x) {
253 for (j = 0; j < 2; j++) {
254 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
255 }
256 }
257 }
258 }
259 }
260 }
261
262 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
263 {
264 MpegEncContext *s = &v->s;
265 int mb_pos;
266
267 if (v->condover == CONDOVER_NONE)
268 return;
269
270 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
271
272 /* Within a MB, the horizontal overlap always runs before the vertical.
273 * To accomplish that, we run the H on left and internal borders of the
274 * currently decoded MB. Then, we wait for the next overlap iteration
275 * to do H overlap on the right edge of this MB, before moving over and
276 * running the V overlap. Therefore, the V overlap makes us trail by one
277 * MB col and the H overlap filter makes us trail by one MB row. This
278 * is reflected in the time at which we run the put_pixels loop. */
279 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
280 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
281 v->over_flags_plane[mb_pos - 1])) {
282 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
283 v->block[v->cur_blk_idx][0]);
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
285 v->block[v->cur_blk_idx][2]);
286 if (!(s->flags & CODEC_FLAG_GRAY)) {
287 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
288 v->block[v->cur_blk_idx][4]);
289 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
290 v->block[v->cur_blk_idx][5]);
291 }
292 }
293 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
294 v->block[v->cur_blk_idx][1]);
295 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
296 v->block[v->cur_blk_idx][3]);
297
298 if (s->mb_x == s->mb_width - 1) {
299 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
300 v->over_flags_plane[mb_pos - s->mb_stride])) {
301 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
302 v->block[v->cur_blk_idx][0]);
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
304 v->block[v->cur_blk_idx][1]);
305 if (!(s->flags & CODEC_FLAG_GRAY)) {
306 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
307 v->block[v->cur_blk_idx][4]);
308 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
309 v->block[v->cur_blk_idx][5]);
310 }
311 }
312 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
313 v->block[v->cur_blk_idx][2]);
314 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
315 v->block[v->cur_blk_idx][3]);
316 }
317 }
318 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
319 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
320 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
321 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
322 v->block[v->left_blk_idx][0]);
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
324 v->block[v->left_blk_idx][1]);
325 if (!(s->flags & CODEC_FLAG_GRAY)) {
326 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
327 v->block[v->left_blk_idx][4]);
328 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
329 v->block[v->left_blk_idx][5]);
330 }
331 }
332 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
333 v->block[v->left_blk_idx][2]);
334 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
335 v->block[v->left_blk_idx][3]);
336 }
337 }
338
339 /** Do motion compensation over 1 macroblock
340 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
341 */
342 static void vc1_mc_1mv(VC1Context *v, int dir)
343 {
344 MpegEncContext *s = &v->s;
345 H264ChromaContext *h264chroma = &v->h264chroma;
346 uint8_t *srcY, *srcU, *srcV;
347 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
348 int v_edge_pos = s->v_edge_pos >> v->field_mode;
349 int i;
350 uint8_t (*luty)[256], (*lutuv)[256];
351 int use_ic;
352
353 if ((!v->field_mode ||
354 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
355 !v->s.last_picture.f.data[0])
356 return;
357
358 mx = s->mv[dir][0][0];
359 my = s->mv[dir][0][1];
360
361 // store motion vectors for further use in B frames
362 if (s->pict_type == AV_PICTURE_TYPE_P) {
363 for (i = 0; i < 4; i++) {
364 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
365 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
366 }
367 }
368
369 uvmx = (mx + ((mx & 3) == 3)) >> 1;
370 uvmy = (my + ((my & 3) == 3)) >> 1;
371 v->luma_mv[s->mb_x][0] = uvmx;
372 v->luma_mv[s->mb_x][1] = uvmy;
373
374 if (v->field_mode &&
375 v->cur_field_type != v->ref_field_type[dir]) {
376 my = my - 2 + 4 * v->cur_field_type;
377 uvmy = uvmy - 2 + 4 * v->cur_field_type;
378 }
379
380 // fastuvmc shall be ignored for interlaced frame picture
381 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
382 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
383 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
384 }
385 if (!dir) {
386 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
387 srcY = s->current_picture.f.data[0];
388 srcU = s->current_picture.f.data[1];
389 srcV = s->current_picture.f.data[2];
390 luty = v->curr_luty;
391 lutuv = v->curr_lutuv;
392 use_ic = v->curr_use_ic;
393 } else {
394 srcY = s->last_picture.f.data[0];
395 srcU = s->last_picture.f.data[1];
396 srcV = s->last_picture.f.data[2];
397 luty = v->last_luty;
398 lutuv = v->last_lutuv;
399 use_ic = v->last_use_ic;
400 }
401 } else {
402 srcY = s->next_picture.f.data[0];
403 srcU = s->next_picture.f.data[1];
404 srcV = s->next_picture.f.data[2];
405 luty = v->next_luty;
406 lutuv = v->next_lutuv;
407 use_ic = v->next_use_ic;
408 }
409
410 src_x = s->mb_x * 16 + (mx >> 2);
411 src_y = s->mb_y * 16 + (my >> 2);
412 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
413 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
414
415 if (v->profile != PROFILE_ADVANCED) {
416 src_x = av_clip( src_x, -16, s->mb_width * 16);
417 src_y = av_clip( src_y, -16, s->mb_height * 16);
418 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
419 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
420 } else {
421 src_x = av_clip( src_x, -17, s->avctx->coded_width);
422 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
423 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
424 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
425 }
426
427 srcY += src_y * s->linesize + src_x;
428 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
429 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
430
431 if (v->field_mode && v->ref_field_type[dir]) {
432 srcY += s->current_picture_ptr->f.linesize[0];
433 srcU += s->current_picture_ptr->f.linesize[1];
434 srcV += s->current_picture_ptr->f.linesize[2];
435 }
436
437 /* for grayscale we should not try to read from unknown area */
438 if (s->flags & CODEC_FLAG_GRAY) {
439 srcU = s->edge_emu_buffer + 18 * s->linesize;
440 srcV = s->edge_emu_buffer + 18 * s->linesize;
441 }
442
443 if (v->rangeredfrm || use_ic
444 || s->h_edge_pos < 22 || v_edge_pos < 22
445 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
446 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
447 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
448
449 srcY -= s->mspel * (1 + s->linesize);
450 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
451 17 + s->mspel * 2, 17 + s->mspel * 2,
452 src_x - s->mspel, src_y - s->mspel,
453 s->h_edge_pos, v_edge_pos);
454 srcY = s->edge_emu_buffer;
455 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
456 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
457 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
458 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
459 srcU = uvbuf;
460 srcV = uvbuf + 16;
461 /* if we deal with range reduction we need to scale source blocks */
462 if (v->rangeredfrm) {
463 int i, j;
464 uint8_t *src, *src2;
465
466 src = srcY;
467 for (j = 0; j < 17 + s->mspel * 2; j++) {
468 for (i = 0; i < 17 + s->mspel * 2; i++)
469 src[i] = ((src[i] - 128) >> 1) + 128;
470 src += s->linesize;
471 }
472 src = srcU;
473 src2 = srcV;
474 for (j = 0; j < 9; j++) {
475 for (i = 0; i < 9; i++) {
476 src[i] = ((src[i] - 128) >> 1) + 128;
477 src2[i] = ((src2[i] - 128) >> 1) + 128;
478 }
479 src += s->uvlinesize;
480 src2 += s->uvlinesize;
481 }
482 }
483 /* if we deal with intensity compensation we need to scale source blocks */
484 if (use_ic) {
485 int i, j;
486 uint8_t *src, *src2;
487
488 src = srcY;
489 for (j = 0; j < 17 + s->mspel * 2; j++) {
490 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
491 for (i = 0; i < 17 + s->mspel * 2; i++)
492 src[i] = luty[f][src[i]];
493 src += s->linesize;
494 }
495 src = srcU;
496 src2 = srcV;
497 for (j = 0; j < 9; j++) {
498 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
499 for (i = 0; i < 9; i++) {
500 src[i] = lutuv[f][src[i]];
501 src2[i] = lutuv[f][src2[i]];
502 }
503 src += s->uvlinesize;
504 src2 += s->uvlinesize;
505 }
506 }
507 srcY += s->mspel * (1 + s->linesize);
508 }
509
510 if (s->mspel) {
511 dxy = ((my & 3) << 2) | (mx & 3);
512 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
513 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
514 srcY += s->linesize * 8;
515 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
516 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
517 } else { // hpel mc - always used for luma
518 dxy = (my & 2) | ((mx & 2) >> 1);
519 if (!v->rnd)
520 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
521 else
522 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
523 }
524
525 if (s->flags & CODEC_FLAG_GRAY) return;
526 /* Chroma MC always uses qpel bilinear */
527 uvmx = (uvmx & 3) << 1;
528 uvmy = (uvmy & 3) << 1;
529 if (!v->rnd) {
530 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
531 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
532 } else {
533 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
534 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
535 }
536 }
537
538 static inline int median4(int a, int b, int c, int d)
539 {
540 if (a < b) {
541 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
542 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
543 } else {
544 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
545 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
546 }
547 }
548
549 /** Do motion compensation for 4-MV macroblock - luminance block
550 */
551 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
552 {
553 MpegEncContext *s = &v->s;
554 uint8_t *srcY;
555 int dxy, mx, my, src_x, src_y;
556 int off;
557 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
558 int v_edge_pos = s->v_edge_pos >> v->field_mode;
559 uint8_t (*luty)[256];
560 int use_ic;
561
562 if ((!v->field_mode ||
563 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
564 !v->s.last_picture.f.data[0])
565 return;
566
567 mx = s->mv[dir][n][0];
568 my = s->mv[dir][n][1];
569
570 if (!dir) {
571 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
572 srcY = s->current_picture.f.data[0];
573 luty = v->curr_luty;
574 use_ic = v->curr_use_ic;
575 } else {
576 srcY = s->last_picture.f.data[0];
577 luty = v->last_luty;
578 use_ic = v->last_use_ic;
579 }
580 } else {
581 srcY = s->next_picture.f.data[0];
582 luty = v->next_luty;
583 use_ic = v->next_use_ic;
584 }
585
586 if (v->field_mode) {
587 if (v->cur_field_type != v->ref_field_type[dir])
588 my = my - 2 + 4 * v->cur_field_type;
589 }
590
591 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
592 int same_count = 0, opp_count = 0, k;
593 int chosen_mv[2][4][2], f;
594 int tx, ty;
595 for (k = 0; k < 4; k++) {
596 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
597 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
598 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
599 opp_count += f;
600 same_count += 1 - f;
601 }
602 f = opp_count > same_count;
603 switch (f ? opp_count : same_count) {
604 case 4:
605 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
606 chosen_mv[f][2][0], chosen_mv[f][3][0]);
607 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
608 chosen_mv[f][2][1], chosen_mv[f][3][1]);
609 break;
610 case 3:
611 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
612 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
613 break;
614 case 2:
615 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
616 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
617 break;
618 }
619 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
620 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
621 for (k = 0; k < 4; k++)
622 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
623 }
624
625 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
626 int qx, qy;
627 int width = s->avctx->coded_width;
628 int height = s->avctx->coded_height >> 1;
629 if (s->pict_type == AV_PICTURE_TYPE_P) {
630 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
631 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
632 }
633 qx = (s->mb_x * 16) + (mx >> 2);
634 qy = (s->mb_y * 8) + (my >> 3);
635
636 if (qx < -17)
637 mx -= 4 * (qx + 17);
638 else if (qx > width)
639 mx -= 4 * (qx - width);
640 if (qy < -18)
641 my -= 8 * (qy + 18);
642 else if (qy > height + 1)
643 my -= 8 * (qy - height - 1);
644 }
645
646 if ((v->fcm == ILACE_FRAME) && fieldmv)
647 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
648 else
649 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
650
651 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
652 if (!fieldmv)
653 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
654 else
655 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
656
657 if (v->profile != PROFILE_ADVANCED) {
658 src_x = av_clip(src_x, -16, s->mb_width * 16);
659 src_y = av_clip(src_y, -16, s->mb_height * 16);
660 } else {
661 src_x = av_clip(src_x, -17, s->avctx->coded_width);
662 if (v->fcm == ILACE_FRAME) {
663 if (src_y & 1)
664 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
665 else
666 src_y = av_clip(src_y, -18, s->avctx->coded_height);
667 } else {
668 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
669 }
670 }
671
672 srcY += src_y * s->linesize + src_x;
673 if (v->field_mode && v->ref_field_type[dir])
674 srcY += s->current_picture_ptr->f.linesize[0];
675
676 if (fieldmv && !(src_y & 1))
677 v_edge_pos--;
678 if (fieldmv && (src_y & 1) && src_y < 4)
679 src_y--;
680 if (v->rangeredfrm || use_ic
681 || s->h_edge_pos < 13 || v_edge_pos < 23
682 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
683 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
684 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
685 /* check emulate edge stride and offset */
686 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
687 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
688 src_x - s->mspel, src_y - (s->mspel << fieldmv),
689 s->h_edge_pos, v_edge_pos);
690 srcY = s->edge_emu_buffer;
691 /* if we deal with range reduction we need to scale source blocks */
692 if (v->rangeredfrm) {
693 int i, j;
694 uint8_t *src;
695
696 src = srcY;
697 for (j = 0; j < 9 + s->mspel * 2; j++) {
698 for (i = 0; i < 9 + s->mspel * 2; i++)
699 src[i] = ((src[i] - 128) >> 1) + 128;
700 src += s->linesize << fieldmv;
701 }
702 }
703 /* if we deal with intensity compensation we need to scale source blocks */
704 if (use_ic) {
705 int i, j;
706 uint8_t *src;
707
708 src = srcY;
709 for (j = 0; j < 9 + s->mspel * 2; j++) {
710 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
711 for (i = 0; i < 9 + s->mspel * 2; i++)
712 src[i] = luty[f][src[i]];
713 src += s->linesize << fieldmv;
714 }
715 }
716 srcY += s->mspel * (1 + (s->linesize << fieldmv));
717 }
718
719 if (s->mspel) {
720 dxy = ((my & 3) << 2) | (mx & 3);
721 if (avg)
722 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
723 else
724 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
725 } else { // hpel mc - always used for luma
726 dxy = (my & 2) | ((mx & 2) >> 1);
727 if (!v->rnd)
728 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
729 else
730 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
731 }
732 }
733
734 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
735 {
736 int idx, i;
737 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
738
739 idx = ((a[3] != flag) << 3)
740 | ((a[2] != flag) << 2)
741 | ((a[1] != flag) << 1)
742 | (a[0] != flag);
743 if (!idx) {
744 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
745 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
746 return 4;
747 } else if (count[idx] == 1) {
748 switch (idx) {
749 case 0x1:
750 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
751 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
752 return 3;
753 case 0x2:
754 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
755 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
756 return 3;
757 case 0x4:
758 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
759 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
760 return 3;
761 case 0x8:
762 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
763 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
764 return 3;
765 }
766 } else if (count[idx] == 2) {
767 int t1 = 0, t2 = 0;
768 for (i = 0; i < 3; i++)
769 if (!a[i]) {
770 t1 = i;
771 break;
772 }
773 for (i = t1 + 1; i < 4; i++)
774 if (!a[i]) {
775 t2 = i;
776 break;
777 }
778 *tx = (mvx[t1] + mvx[t2]) / 2;
779 *ty = (mvy[t1] + mvy[t2]) / 2;
780 return 2;
781 } else {
782 return 0;
783 }
784 return -1;
785 }
786
787 /** Do motion compensation for 4-MV macroblock - both chroma blocks
788 */
789 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
790 {
791 MpegEncContext *s = &v->s;
792 H264ChromaContext *h264chroma = &v->h264chroma;
793 uint8_t *srcU, *srcV;
794 int uvmx, uvmy, uvsrc_x, uvsrc_y;
795 int k, tx = 0, ty = 0;
796 int mvx[4], mvy[4], intra[4], mv_f[4];
797 int valid_count;
798 int chroma_ref_type = v->cur_field_type;
799 int v_edge_pos = s->v_edge_pos >> v->field_mode;
800 uint8_t (*lutuv)[256];
801 int use_ic;
802
803 if (!v->field_mode && !v->s.last_picture.f.data[0])
804 return;
805 if (s->flags & CODEC_FLAG_GRAY)
806 return;
807
808 for (k = 0; k < 4; k++) {
809 mvx[k] = s->mv[dir][k][0];
810 mvy[k] = s->mv[dir][k][1];
811 intra[k] = v->mb_type[0][s->block_index[k]];
812 if (v->field_mode)
813 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
814 }
815
816 /* calculate chroma MV vector from four luma MVs */
817 if (!v->field_mode || (v->field_mode && !v->numref)) {
818 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
819 chroma_ref_type = v->reffield;
820 if (!valid_count) {
821 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
822 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
823 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
824 return; //no need to do MC for intra blocks
825 }
826 } else {
827 int dominant = 0;
828 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
829 dominant = 1;
830 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
831 if (dominant)
832 chroma_ref_type = !v->cur_field_type;
833 }
834 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
835 return;
836 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
837 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
838 uvmx = (tx + ((tx & 3) == 3)) >> 1;
839 uvmy = (ty + ((ty & 3) == 3)) >> 1;
840
841 v->luma_mv[s->mb_x][0] = uvmx;
842 v->luma_mv[s->mb_x][1] = uvmy;
843
844 if (v->fastuvmc) {
845 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
846 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
847 }
848 // Field conversion bias
849 if (v->cur_field_type != chroma_ref_type)
850 uvmy += 2 - 4 * chroma_ref_type;
851
852 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
853 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
854
855 if (v->profile != PROFILE_ADVANCED) {
856 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
857 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
858 } else {
859 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
860 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
861 }
862
863 if (!dir) {
864 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
865 srcU = s->current_picture.f.data[1];
866 srcV = s->current_picture.f.data[2];
867 lutuv = v->curr_lutuv;
868 use_ic = v->curr_use_ic;
869 } else {
870 srcU = s->last_picture.f.data[1];
871 srcV = s->last_picture.f.data[2];
872 lutuv = v->last_lutuv;
873 use_ic = v->last_use_ic;
874 }
875 } else {
876 srcU = s->next_picture.f.data[1];
877 srcV = s->next_picture.f.data[2];
878 lutuv = v->next_lutuv;
879 use_ic = v->next_use_ic;
880 }
881
882 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
883 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
884
885 if (v->field_mode) {
886 if (chroma_ref_type) {
887 srcU += s->current_picture_ptr->f.linesize[1];
888 srcV += s->current_picture_ptr->f.linesize[2];
889 }
890 }
891
892 if (v->rangeredfrm || use_ic
893 || s->h_edge_pos < 18 || v_edge_pos < 18
894 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
895 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
896 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
897 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
898 s->h_edge_pos >> 1, v_edge_pos >> 1);
899 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
900 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
901 s->h_edge_pos >> 1, v_edge_pos >> 1);
902 srcU = s->edge_emu_buffer;
903 srcV = s->edge_emu_buffer + 16;
904
905 /* if we deal with range reduction we need to scale source blocks */
906 if (v->rangeredfrm) {
907 int i, j;
908 uint8_t *src, *src2;
909
910 src = srcU;
911 src2 = srcV;
912 for (j = 0; j < 9; j++) {
913 for (i = 0; i < 9; i++) {
914 src[i] = ((src[i] - 128) >> 1) + 128;
915 src2[i] = ((src2[i] - 128) >> 1) + 128;
916 }
917 src += s->uvlinesize;
918 src2 += s->uvlinesize;
919 }
920 }
921 /* if we deal with intensity compensation we need to scale source blocks */
922 if (use_ic) {
923 int i, j;
924 uint8_t *src, *src2;
925
926 src = srcU;
927 src2 = srcV;
928 for (j = 0; j < 9; j++) {
929 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
930 for (i = 0; i < 9; i++) {
931 src[i] = lutuv[f][src[i]];
932 src2[i] = lutuv[f][src2[i]];
933 }
934 src += s->uvlinesize;
935 src2 += s->uvlinesize;
936 }
937 }
938 }
939
940 /* Chroma MC always uses qpel bilinear */
941 uvmx = (uvmx & 3) << 1;
942 uvmy = (uvmy & 3) << 1;
943 if (!v->rnd) {
944 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
945 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
946 } else {
947 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
948 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
949 }
950 }
951
952 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
953 */
954 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
955 {
956 MpegEncContext *s = &v->s;
957 H264ChromaContext *h264chroma = &v->h264chroma;
958 uint8_t *srcU, *srcV;
959 int uvsrc_x, uvsrc_y;
960 int uvmx_field[4], uvmy_field[4];
961 int i, off, tx, ty;
962 int fieldmv = v->blk_mv_type[s->block_index[0]];
963 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
964 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
965 int v_edge_pos = s->v_edge_pos >> 1;
966 int use_ic;
967 uint8_t (*lutuv)[256];
968
969 if (s->flags & CODEC_FLAG_GRAY)
970 return;
971
972 for (i = 0; i < 4; i++) {
973 int d = i < 2 ? dir: dir2;
974 tx = s->mv[d][i][0];
975 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
976 ty = s->mv[d][i][1];
977 if (fieldmv)
978 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
979 else
980 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
981 }
982
983 for (i = 0; i < 4; i++) {
984 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
985 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
986 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
987 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
988 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
989 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
990 if (i < 2 ? dir : dir2) {
991 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
992 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
993 lutuv = v->next_lutuv;
994 use_ic = v->next_use_ic;
995 } else {
996 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
997 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
998 lutuv = v->last_lutuv;
999 use_ic = v->last_use_ic;
1000 }
1001 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1002 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1003
1004 if (fieldmv && !(uvsrc_y & 1))
1005 v_edge_pos--;
1006 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1007 uvsrc_y--;
1008 if (use_ic
1009 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1010 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1011 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1012 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1013 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1014 s->h_edge_pos >> 1, v_edge_pos);
1015 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1016 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1017 s->h_edge_pos >> 1, v_edge_pos);
1018 srcU = s->edge_emu_buffer;
1019 srcV = s->edge_emu_buffer + 16;
1020
1021 /* if we deal with intensity compensation we need to scale source blocks */
1022 if (use_ic) {
1023 int i, j;
1024 uint8_t *src, *src2;
1025
1026 src = srcU;
1027 src2 = srcV;
1028 for (j = 0; j < 5; j++) {
1029 int f = (uvsrc_y + (j << fieldmv)) & 1;
1030 for (i = 0; i < 5; i++) {
1031 src[i] = lutuv[f][src[i]];
1032 src2[i] = lutuv[f][src2[i]];
1033 }
1034 src += s->uvlinesize << fieldmv;
1035 src2 += s->uvlinesize << fieldmv;
1036 }
1037 }
1038 }
1039 if (avg) {
1040 if (!v->rnd) {
1041 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1042 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1043 } else {
1044 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1045 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1046 }
1047 } else {
1048 if (!v->rnd) {
1049 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1050 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1051 } else {
1052 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]);
1053 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]);
1054 }
1055 }
1056 }
1057 }
1058
1059 /***********************************************************************/
1060 /**
1061 * @name VC-1 Block-level functions
1062 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1063 * @{
1064 */
1065
1066 /**
1067 * @def GET_MQUANT
1068 * @brief Get macroblock-level quantizer scale
1069 */
1070 #define GET_MQUANT() \
1071 if (v->dquantfrm) { \
1072 int edges = 0; \
1073 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1074 if (v->dqbilevel) { \
1075 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1076 } else { \
1077 mqdiff = get_bits(gb, 3); \
1078 if (mqdiff != 7) \
1079 mquant = v->pq + mqdiff; \
1080 else \
1081 mquant = get_bits(gb, 5); \
1082 } \
1083 } \
1084 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1085 edges = 1 << v->dqsbedge; \
1086 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1087 edges = (3 << v->dqsbedge) % 15; \
1088 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1089 edges = 15; \
1090 if ((edges&1) && !s->mb_x) \
1091 mquant = v->altpq; \
1092 if ((edges&2) && s->first_slice_line) \
1093 mquant = v->altpq; \
1094 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1095 mquant = v->altpq; \
1096 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1097 mquant = v->altpq; \
1098 if (!mquant || mquant > 31) { \
1099 av_log(v->s.avctx, AV_LOG_ERROR, \
1100 "Overriding invalid mquant %d\n", mquant); \
1101 mquant = 1; \
1102 } \
1103 }
1104
1105 /**
1106 * @def GET_MVDATA(_dmv_x, _dmv_y)
1107 * @brief Get MV differentials
1108 * @see MVDATA decoding from 8.3.5.2, p(1)20
1109 * @param _dmv_x Horizontal differential for decoded MV
1110 * @param _dmv_y Vertical differential for decoded MV
1111 */
1112 #define GET_MVDATA(_dmv_x, _dmv_y) \
1113 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1114 VC1_MV_DIFF_VLC_BITS, 2); \
1115 if (index > 36) { \
1116 mb_has_coeffs = 1; \
1117 index -= 37; \
1118 } else \
1119 mb_has_coeffs = 0; \
1120 s->mb_intra = 0; \
1121 if (!index) { \
1122 _dmv_x = _dmv_y = 0; \
1123 } else if (index == 35) { \
1124 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1125 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1126 } else if (index == 36) { \
1127 _dmv_x = 0; \
1128 _dmv_y = 0; \
1129 s->mb_intra = 1; \
1130 } else { \
1131 index1 = index % 6; \
1132 if (!s->quarter_sample && index1 == 5) val = 1; \
1133 else val = 0; \
1134 if (size_table[index1] - val > 0) \
1135 val = get_bits(gb, size_table[index1] - val); \
1136 else val = 0; \
1137 sign = 0 - (val&1); \
1138 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1139 \
1140 index1 = index / 6; \
1141 if (!s->quarter_sample && index1 == 5) val = 1; \
1142 else val = 0; \
1143 if (size_table[index1] - val > 0) \
1144 val = get_bits(gb, size_table[index1] - val); \
1145 else val = 0; \
1146 sign = 0 - (val & 1); \
1147 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1148 }
1149
1150 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1151 int *dmv_y, int *pred_flag)
1152 {
1153 int index, index1;
1154 int extend_x = 0, extend_y = 0;
1155 GetBitContext *gb = &v->s.gb;
1156 int bits, esc;
1157 int val, sign;
1158 const int* offs_tab;
1159
1160 if (v->numref) {
1161 bits = VC1_2REF_MVDATA_VLC_BITS;
1162 esc = 125;
1163 } else {
1164 bits = VC1_1REF_MVDATA_VLC_BITS;
1165 esc = 71;
1166 }
1167 switch (v->dmvrange) {
1168 case 1:
1169 extend_x = 1;
1170 break;
1171 case 2:
1172 extend_y = 1;
1173 break;
1174 case 3:
1175 extend_x = extend_y = 1;
1176 break;
1177 }
1178 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1179 if (index == esc) {
1180 *dmv_x = get_bits(gb, v->k_x);
1181 *dmv_y = get_bits(gb, v->k_y);
1182 if (v->numref) {
1183 if (pred_flag) {
1184 *pred_flag = *dmv_y & 1;
1185 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1186 } else {
1187 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1188 }
1189 }
1190 }
1191 else {
1192 if (extend_x)
1193 offs_tab = offset_table2;
1194 else
1195 offs_tab = offset_table1;
1196 index1 = (index + 1) % 9;
1197 if (index1 != 0) {
1198 val = get_bits(gb, index1 + extend_x);
1199 sign = 0 -(val & 1);
1200 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1201 } else
1202 *dmv_x = 0;
1203 if (extend_y)
1204 offs_tab = offset_table2;
1205 else
1206 offs_tab = offset_table1;
1207 index1 = (index + 1) / 9;
1208 if (index1 > v->numref) {
1209 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1210 sign = 0 - (val & 1);
1211 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1212 } else
1213 *dmv_y = 0;
1214 if (v->numref && pred_flag)
1215 *pred_flag = index1 & 1;
1216 }
1217 }
1218
1219 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1220 {
1221 int scaledvalue, refdist;
1222 int scalesame1, scalesame2;
1223 int scalezone1_x, zone1offset_x;
1224 int table_index = dir ^ v->second_field;
1225
1226 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1227 refdist = v->refdist;
1228 else
1229 refdist = dir ? v->brfd : v->frfd;
1230 if (refdist > 3)
1231 refdist = 3;
1232 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1233 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1234 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1235 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1236
1237 if (FFABS(n) > 255)
1238 scaledvalue = n;
1239 else {
1240 if (FFABS(n) < scalezone1_x)
1241 scaledvalue = (n * scalesame1) >> 8;
1242 else {
1243 if (n < 0)
1244 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1245 else
1246 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1247 }
1248 }
1249 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1250 }
1251
1252 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1253 {
1254 int scaledvalue, refdist;
1255 int scalesame1, scalesame2;
1256 int scalezone1_y, zone1offset_y;
1257 int table_index = dir ^ v->second_field;
1258
1259 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1260 refdist = v->refdist;
1261 else
1262 refdist = dir ? v->brfd : v->frfd;
1263 if (refdist > 3)
1264 refdist = 3;
1265 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1266 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1267 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1268 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1269
1270 if (FFABS(n) > 63)
1271 scaledvalue = n;
1272 else {
1273 if (FFABS(n) < scalezone1_y)
1274 scaledvalue = (n * scalesame1) >> 8;
1275 else {
1276 if (n < 0)
1277 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1278 else
1279 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1280 }
1281 }
1282
1283 if (v->cur_field_type && !v->ref_field_type[dir])
1284 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1285 else
1286 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1287 }
1288
1289 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1290 {
1291 int scalezone1_x, zone1offset_x;
1292 int scaleopp1, scaleopp2, brfd;
1293 int scaledvalue;
1294
1295 brfd = FFMIN(v->brfd, 3);
1296 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1297 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1298 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1299 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1300
1301 if (FFABS(n) > 255)
1302 scaledvalue = n;
1303 else {
1304 if (FFABS(n) < scalezone1_x)
1305 scaledvalue = (n * scaleopp1) >> 8;
1306 else {
1307 if (n < 0)
1308 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1309 else
1310 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1311 }
1312 }
1313 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1314 }
1315
1316 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1317 {
1318 int scalezone1_y, zone1offset_y;
1319 int scaleopp1, scaleopp2, brfd;
1320 int scaledvalue;
1321
1322 brfd = FFMIN(v->brfd, 3);
1323 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1324 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1325 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1326 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1327
1328 if (FFABS(n) > 63)
1329 scaledvalue = n;
1330 else {
1331 if (FFABS(n) < scalezone1_y)
1332 scaledvalue = (n * scaleopp1) >> 8;
1333 else {
1334 if (n < 0)
1335 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1336 else
1337 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1338 }
1339 }
1340 if (v->cur_field_type && !v->ref_field_type[dir]) {
1341 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1342 } else {
1343 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1344 }
1345 }
1346
1347 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1348 int dim, int dir)
1349 {
1350 int brfd, scalesame;
1351 int hpel = 1 - v->s.quarter_sample;
1352
1353 n >>= hpel;
1354 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1355 if (dim)
1356 n = scaleforsame_y(v, i, n, dir) << hpel;
1357 else
1358 n = scaleforsame_x(v, n, dir) << hpel;
1359 return n;
1360 }
1361 brfd = FFMIN(v->brfd, 3);
1362 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1363
1364 n = (n * scalesame >> 8) << hpel;
1365 return n;
1366 }
1367
1368 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1369 int dim, int dir)
1370 {
1371 int refdist, scaleopp;
1372 int hpel = 1 - v->s.quarter_sample;
1373
1374 n >>= hpel;
1375 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1376 if (dim)
1377 n = scaleforopp_y(v, n, dir) << hpel;
1378 else
1379 n = scaleforopp_x(v, n) << hpel;
1380 return n;
1381 }
1382 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1383 refdist = FFMIN(v->refdist, 3);
1384 else
1385 refdist = dir ? v->brfd : v->frfd;
1386 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1387
1388 n = (n * scaleopp >> 8) << hpel;
1389 return n;
1390 }
1391
1392 /** Predict and set motion vector
1393 */
1394 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1395 int mv1, int r_x, int r_y, uint8_t* is_intra,
1396 int pred_flag, int dir)
1397 {
1398 MpegEncContext *s = &v->s;
1399 int xy, wrap, off = 0;
1400 int16_t *A, *B, *C;
1401 int px, py;
1402 int sum;
1403 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1404 int opposite, a_f, b_f, c_f;
1405 int16_t field_predA[2];
1406 int16_t field_predB[2];
1407 int16_t field_predC[2];
1408 int a_valid, b_valid, c_valid;
1409 int hybridmv_thresh, y_bias = 0;
1410
1411 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1412 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1413 mixedmv_pic = 1;
1414 else
1415 mixedmv_pic = 0;
1416 /* scale MV difference to be quad-pel */
1417 dmv_x <<= 1 - s->quarter_sample;
1418 dmv_y <<= 1 - s->quarter_sample;
1419
1420 wrap = s->b8_stride;
1421 xy = s->block_index[n];
1422
1423 if (s->mb_intra) {
1424 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1425 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1426 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1427 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1428 if (mv1) { /* duplicate motion data for 1-MV block */
1429 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1430 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1431 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1432 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1433 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1434 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1435 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1436 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1437 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1438 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1439 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1440 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1441 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1442 }
1443 return;
1444 }
1445
1446 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1447 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1448 if (mv1) {
1449 if (v->field_mode && mixedmv_pic)
1450 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1451 else
1452 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1453 } else {
1454 //in 4-MV mode different blocks have different B predictor position
1455 switch (n) {
1456 case 0:
1457 off = (s->mb_x > 0) ? -1 : 1;
1458 break;
1459 case 1:
1460 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1461 break;
1462 case 2:
1463 off = 1;
1464 break;
1465 case 3:
1466 off = -1;
1467 }
1468 }
1469 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1470
1471 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1472 b_valid = a_valid && (s->mb_width > 1);
1473 c_valid = s->mb_x || (n == 1 || n == 3);
1474 if (v->field_mode) {
1475 a_valid = a_valid && !is_intra[xy - wrap];
1476 b_valid = b_valid && !is_intra[xy - wrap + off];
1477 c_valid = c_valid && !is_intra[xy - 1];
1478 }
1479
1480 if (a_valid) {
1481 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1482 num_oppfield += a_f;
1483 num_samefield += 1 - a_f;
1484 field_predA[0] = A[0];
1485 field_predA[1] = A[1];
1486 } else {
1487 field_predA[0] = field_predA[1] = 0;
1488 a_f = 0;
1489 }
1490 if (b_valid) {
1491 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1492 num_oppfield += b_f;
1493 num_samefield += 1 - b_f;
1494 field_predB[0] = B[0];
1495 field_predB[1] = B[1];
1496 } else {
1497 field_predB[0] = field_predB[1] = 0;
1498 b_f = 0;
1499 }
1500 if (c_valid) {
1501 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1502 num_oppfield += c_f;
1503 num_samefield += 1 - c_f;
1504 field_predC[0] = C[0];
1505 field_predC[1] = C[1];
1506 } else {
1507 field_predC[0] = field_predC[1] = 0;
1508 c_f = 0;
1509 }
1510
1511 if (v->field_mode) {
1512 if (!v->numref)
1513 // REFFIELD determines if the last field or the second-last field is
1514 // to be used as reference
1515 opposite = 1 - v->reffield;
1516 else {
1517 if (num_samefield <= num_oppfield)
1518 opposite = 1 - pred_flag;
1519 else
1520 opposite = pred_flag;
1521 }
1522 } else
1523 opposite = 0;
1524 if (opposite) {
1525 if (a_valid && !a_f) {
1526 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1527 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1528 }
1529 if (b_valid && !b_f) {
1530 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1531 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1532 }
1533 if (c_valid && !c_f) {
1534 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1535 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1536 }
1537 v->mv_f[dir][xy + v->blocks_off] = 1;
1538 v->ref_field_type[dir] = !v->cur_field_type;
1539 } else {
1540 if (a_valid && a_f) {
1541 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1542 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1543 }
1544 if (b_valid && b_f) {
1545 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1546 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1547 }
1548 if (c_valid && c_f) {
1549 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1550 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1551 }
1552 v->mv_f[dir][xy + v->blocks_off] = 0;
1553 v->ref_field_type[dir] = v->cur_field_type;
1554 }
1555
1556 if (a_valid) {
1557 px = field_predA[0];
1558 py = field_predA[1];
1559 } else if (c_valid) {
1560 px = field_predC[0];
1561 py = field_predC[1];
1562 } else if (b_valid) {
1563 px = field_predB[0];
1564 py = field_predB[1];
1565 } else {
1566 px = 0;
1567 py = 0;
1568 }
1569
1570 if (num_samefield + num_oppfield > 1) {
1571 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1572 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1573 }
1574
1575 /* Pullback MV as specified in 8.3.5.3.4 */
1576 if (!v->field_mode) {
1577 int qx, qy, X, Y;
1578 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1579 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1580 X = (s->mb_width << 6) - 4;
1581 Y = (s->mb_height << 6) - 4;
1582 if (mv1) {
1583 if (qx + px < -60) px = -60 - qx;
1584 if (qy + py < -60) py = -60 - qy;
1585 } else {
1586 if (qx + px < -28) px = -28 - qx;
1587 if (qy + py < -28) py = -28 - qy;
1588 }
1589 if (qx + px > X) px = X - qx;
1590 if (qy + py > Y) py = Y - qy;
1591 }
1592
1593 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1594 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1595 hybridmv_thresh = 32;
1596 if (a_valid && c_valid) {
1597 if (is_intra[xy - wrap])
1598 sum = FFABS(px) + FFABS(py);
1599 else
1600 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1601 if (sum > hybridmv_thresh) {
1602 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1603 px = field_predA[0];
1604 py = field_predA[1];
1605 } else {
1606 px = field_predC[0];
1607 py = field_predC[1];
1608 }
1609 } else {
1610 if (is_intra[xy - 1])
1611 sum = FFABS(px) + FFABS(py);
1612 else
1613 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1614 if (sum > hybridmv_thresh) {
1615 if (get_bits1(&s->gb)) {
1616 px = field_predA[0];
1617 py = field_predA[1];
1618 } else {
1619 px = field_predC[0];
1620 py = field_predC[1];
1621 }
1622 }
1623 }
1624 }
1625 }
1626
1627 if (v->field_mode && v->numref)
1628 r_y >>= 1;
1629 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1630 y_bias = 1;
1631 /* store MV using signed modulus of MV range defined in 4.11 */
1632 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1633 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1634 if (mv1) { /* duplicate motion data for 1-MV block */
1635 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1636 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1637 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1638 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1639 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1640 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1641 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1642 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];
1643 }
1644 }
1645
1646 /** Predict and set motion vector for interlaced frame picture MBs
1647 */
1648 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1649 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1650 {
1651 MpegEncContext *s = &v->s;
1652 int xy, wrap, off = 0;
1653 int A[2], B[2], C[2];
1654 int px, py;
1655 int a_valid = 0, b_valid = 0, c_valid = 0;
1656 int field_a, field_b, field_c; // 0: same, 1: opposit
1657 int total_valid, num_samefield, num_oppfield;
1658 int pos_c, pos_b, n_adj;
1659
1660 wrap = s->b8_stride;
1661 xy = s->block_index[n];
1662
1663 if (s->mb_intra) {
1664 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1665 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1666 s->current_picture.motion_val[1][xy][0] = 0;
1667 s->current_picture.motion_val[1][xy][1] = 0;
1668 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1669 s->current_picture.motion_val[0][xy + 1][0] = 0;
1670 s->current_picture.motion_val[0][xy + 1][1] = 0;
1671 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1672 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1673 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1674 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1675 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1676 s->current_picture.motion_val[1][xy + 1][0] = 0;
1677 s->current_picture.motion_val[1][xy + 1][1] = 0;
1678 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1679 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1680 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1681 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1682 }
1683 return;
1684 }
1685
1686 off = ((n == 0) || (n == 1)) ? 1 : -1;
1687 /* predict A */
1688 if (s->mb_x || (n == 1) || (n == 3)) {
1689 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1690 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1691 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1692 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1693 a_valid = 1;
1694 } else { // current block has frame mv and cand. has field MV (so average)
1695 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1696 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1697 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1698 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1699 a_valid = 1;
1700 }
1701 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1702 a_valid = 0;
1703 A[0] = A[1] = 0;
1704 }
1705 } else
1706 A[0] = A[1] = 0;
1707 /* Predict B and C */
1708 B[0] = B[1] = C[0] = C[1] = 0;
1709 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1710 if (!s->first_slice_line) {
1711 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1712 b_valid = 1;
1713 n_adj = n | 2;
1714 pos_b = s->block_index[n_adj] - 2 * wrap;
1715 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1716 n_adj = (n & 2) | (n & 1);
1717 }
1718 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1719 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1720 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1721 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1722 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1723 }
1724 }
1725 if (s->mb_width > 1) {
1726 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1727 c_valid = 1;
1728 n_adj = 2;
1729 pos_c = s->block_index[2] - 2 * wrap + 2;
1730 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1731 n_adj = n & 2;
1732 }
1733 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1734 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1735 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1736 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1737 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1738 }
1739 if (s->mb_x == s->mb_width - 1) {
1740 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1741 c_valid = 1;
1742 n_adj = 3;
1743 pos_c = s->block_index[3] - 2 * wrap - 2;
1744 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1745 n_adj = n | 1;
1746 }
1747 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1748 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1749 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1750 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1751 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1752 }
1753 } else
1754 c_valid = 0;
1755 }
1756 }
1757 }
1758 }
1759 } else {
1760 pos_b = s->block_index[1];
1761 b_valid = 1;
1762 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1763 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1764 pos_c = s->block_index[0];
1765 c_valid = 1;
1766 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1767 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1768 }
1769
1770 total_valid = a_valid + b_valid + c_valid;
1771 // check if predictor A is out of bounds
1772 if (!s->mb_x && !(n == 1 || n == 3)) {
1773 A[0] = A[1] = 0;
1774 }
1775 // check if predictor B is out of bounds
1776 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1777 B[0] = B[1] = C[0] = C[1] = 0;
1778 }
1779 if (!v->blk_mv_type[xy]) {
1780 if (s->mb_width == 1) {
1781 px = B[0];
1782 py = B[1];
1783 } else {
1784 if (total_valid >= 2) {
1785 px = mid_pred(A[0], B[0], C[0]);
1786 py = mid_pred(A[1], B[1], C[1]);
1787 } else if (total_valid) {
1788 if (a_valid) { px = A[0]; py = A[1]; }
1789 if (b_valid) { px = B[0]; py = B[1]; }
1790 if (c_valid) { px = C[0]; py = C[1]; }
1791 } else
1792 px = py = 0;
1793 }
1794 } else {
1795 if (a_valid)
1796 field_a = (A[1] & 4) ? 1 : 0;
1797 else
1798 field_a = 0;
1799 if (b_valid)
1800 field_b = (B[1] & 4) ? 1 : 0;
1801 else
1802 field_b = 0;
1803 if (c_valid)
1804 field_c = (C[1] & 4) ? 1 : 0;
1805 else
1806 field_c = 0;
1807
1808 num_oppfield = field_a + field_b + field_c;
1809 num_samefield = total_valid - num_oppfield;
1810 if (total_valid == 3) {
1811 if ((num_samefield == 3) || (num_oppfield == 3)) {
1812 px = mid_pred(A[0], B[0], C[0]);
1813 py = mid_pred(A[1], B[1], C[1]);
1814 } else if (num_samefield >= num_oppfield) {
1815 /* take one MV from same field set depending on priority
1816 the check for B may not be necessary */
1817 px = !field_a ? A[0] : B[0];
1818 py = !field_a ? A[1] : B[1];
1819 } else {
1820 px = field_a ? A[0] : B[0];
1821 py = field_a ? A[1] : B[1];
1822 }
1823 } else if (total_valid == 2) {
1824 if (num_samefield >= num_oppfield) {
1825 if (!field_a && a_valid) {
1826 px = A[0];
1827 py = A[1];
1828 } else if (!field_b && b_valid) {
1829 px = B[0];
1830 py = B[1];
1831 } else if (c_valid) {
1832 px = C[0];
1833 py = C[1];
1834 } else px = py = 0;
1835 } else {
1836 if (field_a && a_valid) {
1837 px = A[0];
1838 py = A[1];
1839 } else if (field_b && b_valid) {
1840 px = B[0];
1841 py = B[1];
1842 } else if (c_valid) {
1843 px = C[0];
1844 py = C[1];
1845 }
1846 }
1847 } else if (total_valid == 1) {
1848 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1849 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1850 } else
1851 px = py = 0;
1852 }
1853
1854 /* store MV using signed modulus of MV range defined in 4.11 */
1855 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1856 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1857 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1858 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1859 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1860 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1861 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1862 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1863 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1864 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1865 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1866 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1867 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1868 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1869 }
1870 }
1871
1872 /** Motion compensation for direct or interpolated blocks in B-frames
1873 */
1874 static void vc1_interp_mc(VC1Context *v)
1875 {
1876 MpegEncContext *s = &v->s;
1877 H264ChromaContext *h264chroma = &v->h264chroma;
1878 uint8_t *srcY, *srcU, *srcV;
1879 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1880 int off, off_uv;
1881 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1882 int use_ic = v->next_use_ic;
1883
1884 if (!v->field_mode && !v->s.next_picture.f.data[0])
1885 return;
1886
1887 mx = s->mv[1][0][0];
1888 my = s->mv[1][0][1];
1889 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1890 uvmy = (my + ((my & 3) == 3)) >> 1;
1891 if (v->field_mode) {
1892 if (v->cur_field_type != v->ref_field_type[1])
1893 my = my - 2 + 4 * v->cur_field_type;
1894 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1895 }
1896 if (v->fastuvmc) {
1897 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1898 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1899 }
1900 srcY = s->next_picture.f.data[0];
1901 srcU = s->next_picture.f.data[1];
1902 srcV = s->next_picture.f.data[2];
1903
1904 src_x = s->mb_x * 16 + (mx >> 2);
1905 src_y = s->mb_y * 16 + (my >> 2);
1906 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1907 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1908
1909 if (v->profile != PROFILE_ADVANCED) {
1910 src_x = av_clip( src_x, -16, s->mb_width * 16);
1911 src_y = av_clip( src_y, -16, s->mb_height * 16);
1912 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1913 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1914 } else {
1915 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1916 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1917 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1918 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1919 }
1920
1921 srcY += src_y * s->linesize + src_x;
1922 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1923 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1924
1925 if (v->field_mode && v->ref_field_type[1]) {
1926 srcY += s->current_picture_ptr->f.linesize[0];
1927 srcU += s->current_picture_ptr->f.linesize[1];
1928 srcV += s->current_picture_ptr->f.linesize[2];
1929 }
1930
1931 /* for grayscale we should not try to read from unknown area */
1932 if (s->flags & CODEC_FLAG_GRAY) {
1933 srcU = s->edge_emu_buffer + 18 * s->linesize;
1934 srcV = s->edge_emu_buffer + 18 * s->linesize;
1935 }
1936
1937 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1938 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1939 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1940 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1941
1942 srcY -= s->mspel * (1 + s->linesize);
1943 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1944 17 + s->mspel * 2, 17 + s->mspel * 2,
1945 src_x - s->mspel, src_y - s->mspel,
1946 s->h_edge_pos, v_edge_pos);
1947 srcY = s->edge_emu_buffer;
1948 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1949 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1950 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1951 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1952 srcU = uvbuf;
1953 srcV = uvbuf + 16;
1954 /* if we deal with range reduction we need to scale source blocks */
1955 if (v->rangeredfrm) {
1956 int i, j;
1957 uint8_t *src, *src2;
1958
1959 src = srcY;
1960 for (j = 0; j < 17 + s->mspel * 2; j++) {
1961 for (i = 0; i < 17 + s->mspel * 2; i++)
1962 src[i] = ((src[i] - 128) >> 1) + 128;
1963 src += s->linesize;
1964 }
1965 src = srcU;
1966 src2 = srcV;
1967 for (j = 0; j < 9; j++) {
1968 for (i = 0; i < 9; i++) {
1969 src[i] = ((src[i] - 128) >> 1) + 128;
1970 src2[i] = ((src2[i] - 128) >> 1) + 128;
1971 }
1972 src += s->uvlinesize;
1973 src2 += s->uvlinesize;
1974 }
1975 }
1976
1977 if (use_ic) {
1978 uint8_t (*luty )[256] = v->next_luty;
1979 uint8_t (*lutuv)[256] = v->next_lutuv;
1980 int i, j;
1981 uint8_t *src, *src2;
1982
1983 src = srcY;
1984 for (j = 0; j < 17 + s->mspel * 2; j++) {
1985 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
1986 for (i = 0; i < 17 + s->mspel * 2; i++)
1987 src[i] = luty[f][src[i]];
1988 src += s->linesize;
1989 }
1990 src = srcU;
1991 src2 = srcV;
1992 for (j = 0; j < 9; j++) {
1993 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
1994 for (i = 0; i < 9; i++) {
1995 src[i] = lutuv[f][src[i]];
1996 src2[i] = lutuv[f][src2[i]];
1997 }
1998 src += s->uvlinesize;
1999 src2 += s->uvlinesize;
2000 }
2001 }
2002 srcY += s->mspel * (1 + s->linesize);
2003 }
2004
2005 off = 0;
2006 off_uv = 0;
2007
2008 if (s->mspel) {
2009 dxy = ((my & 3) << 2) | (mx & 3);
2010 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2011 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2012 srcY += s->linesize * 8;
2013 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2014 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2015 } else { // hpel mc
2016 dxy = (my & 2) | ((mx & 2) >> 1);
2017
2018 if (!v->rnd)
2019 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2020 else
2021 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2022 }
2023
2024 if (s->flags & CODEC_FLAG_GRAY) return;
2025 /* Chroma MC always uses qpel blilinear */
2026 uvmx = (uvmx & 3) << 1;
2027 uvmy = (uvmy & 3) << 1;
2028 if (!v->rnd) {
2029 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2030 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2031 } else {
2032 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2033 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2034 }
2035 }
2036
2037 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2038 {
2039 int n = bfrac;
2040
2041 #if B_FRACTION_DEN==256
2042 if (inv)
2043 n -= 256;
2044 if (!qs)
2045 return 2 * ((value * n + 255) >> 9);
2046 return (value * n + 128) >> 8;
2047 #else
2048 if (inv)
2049 n -= B_FRACTION_DEN;
2050 if (!qs)
2051 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2052 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2053 #endif
2054 }
2055
2056 /** Reconstruct motion vector for B-frame and do motion compensation
2057 */
2058 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2059 int direct, int mode)
2060 {
2061 if (direct) {
2062 vc1_mc_1mv(v, 0);
2063 vc1_interp_mc(v);
2064 return;
2065 }
2066 if (mode == BMV_TYPE_INTERPOLATED) {
2067 vc1_mc_1mv(v, 0);
2068 vc1_interp_mc(v);
2069 return;
2070 }
2071
2072 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2073 }
2074
2075 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2076 int direct, int mvtype)
2077 {
2078 MpegEncContext *s = &v->s;
2079 int xy, wrap, off = 0;
2080 int16_t *A, *B, *C;
2081 int px, py;
2082 int sum;
2083 int r_x, r_y;
2084 const uint8_t *is_intra = v->mb_type[0];
2085
2086 r_x = v->range_x;
2087 r_y = v->range_y;
2088 /* scale MV difference to be quad-pel */
2089 dmv_x[0] <<= 1 - s->quarter_sample;
2090 dmv_y[0] <<= 1 - s->quarter_sample;
2091 dmv_x[1] <<= 1 - s->quarter_sample;
2092 dmv_y[1] <<= 1 - s->quarter_sample;
2093
2094 wrap = s->b8_stride;
2095 xy = s->block_index[0];
2096
2097 if (s->mb_intra) {
2098 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2099 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2100 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2101 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2102 return;
2103 }
2104 if (!v->field_mode) {
2105 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2106 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2107 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2108 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2109
2110 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2111 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));
2112 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));
2113 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));
2114 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));
2115 }
2116 if (direct) {
2117 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2118 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2119 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2120 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2121 return;
2122 }
2123
2124 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2125 C = s->current_picture.motion_val[0][xy - 2];
2126 A = s->current_picture.motion_val[0][xy - wrap * 2];
2127 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2128 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2129
2130 if (!s->mb_x) C[0] = C[1] = 0;
2131 if (!s->first_slice_line) { // predictor A is not out of bounds
2132 if (s->mb_width == 1) {
2133 px = A[0];
2134 py = A[1];
2135 } else {
2136 px = mid_pred(A[0], B[0], C[0]);
2137 py = mid_pred(A[1], B[1], C[1]);
2138 }
2139 } else if (s->mb_x) { // predictor C is not out of bounds
2140 px = C[0];
2141 py = C[1];
2142 } else {
2143 px = py = 0;
2144 }
2145 /* Pullback MV as specified in 8.3.5.3.4 */
2146 {
2147 int qx, qy, X, Y;
2148 if (v->profile < PROFILE_ADVANCED) {
2149 qx = (s->mb_x << 5);
2150 qy = (s->mb_y << 5);
2151 X = (s->mb_width << 5) - 4;
2152 Y = (s->mb_height << 5) - 4;
2153 if (qx + px < -28) px = -28 - qx;
2154 if (qy + py < -28) py = -28 - qy;
2155 if (qx + px > X) px = X - qx;
2156 if (qy + py > Y) py = Y - qy;
2157 } else {
2158 qx = (s->mb_x << 6);
2159 qy = (s->mb_y << 6);
2160 X = (s->mb_width << 6) - 4;
2161 Y = (s->mb_height << 6) - 4;
2162 if (qx + px < -60) px = -60 - qx;
2163 if (qy + py < -60) py = -60 - qy;
2164 if (qx + px > X) px = X - qx;
2165 if (qy + py > Y) py = Y - qy;
2166 }
2167 }
2168 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2169 if (0 && !s->first_slice_line && s->mb_x) {
2170 if (is_intra[xy - wrap])
2171 sum = FFABS(px) + FFABS(py);
2172 else
2173 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2174 if (sum > 32) {
2175 if (get_bits1(&s->gb)) {
2176 px = A[0];
2177 py = A[1];
2178 } else {
2179 px = C[0];
2180 py = C[1];
2181 }
2182 } else {
2183 if (is_intra[xy - 2])
2184 sum = FFABS(px) + FFABS(py);
2185 else
2186 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2187 if (sum > 32) {
2188 if (get_bits1(&s->gb)) {
2189 px = A[0];
2190 py = A[1];
2191 } else {
2192 px = C[0];
2193 py = C[1];
2194 }
2195 }
2196 }
2197 }
2198 /* store MV using signed modulus of MV range defined in 4.11 */
2199 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2200 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2201 }
2202 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2203 C = s->current_picture.motion_val[1][xy - 2];
2204 A = s->current_picture.motion_val[1][xy - wrap * 2];
2205 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2206 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2207
2208 if (!s->mb_x)
2209 C[0] = C[1] = 0;
2210 if (!s->first_slice_line) { // predictor A is not out of bounds
2211 if (s->mb_width == 1) {
2212 px = A[0];
2213 py = A[1];
2214 } else {
2215 px = mid_pred(A[0], B[0], C[0]);
2216 py = mid_pred(A[1], B[1], C[1]);
2217 }
2218 } else if (s->mb_x) { // predictor C is not out of bounds
2219 px = C[0];
2220 py = C[1];
2221 } else {
2222 px = py = 0;
2223 }
2224 /* Pullback MV as specified in 8.3.5.3.4 */
2225 {
2226 int qx, qy, X, Y;
2227 if (v->profile < PROFILE_ADVANCED) {
2228 qx = (s->mb_x << 5);
2229 qy = (s->mb_y << 5);
2230 X = (s->mb_width << 5) - 4;
2231 Y = (s->mb_height << 5) - 4;
2232 if (qx + px < -28) px = -28 - qx;
2233 if (qy + py < -28) py = -28 - qy;
2234 if (qx + px > X) px = X - qx;
2235 if (qy + py > Y) py = Y - qy;
2236 } else {
2237 qx = (s->mb_x << 6);
2238 qy = (s->mb_y << 6);
2239 X = (s->mb_width << 6) - 4;
2240 Y = (s->mb_height << 6) - 4;
2241 if (qx + px < -60) px = -60 - qx;
2242 if (qy + py < -60) py = -60 - qy;
2243 if (qx + px > X) px = X - qx;
2244 if (qy + py > Y) py = Y - qy;
2245 }
2246 }
2247 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2248 if (0 && !s->first_slice_line && s->mb_x) {
2249 if (is_intra[xy - wrap])
2250 sum = FFABS(px) + FFABS(py);
2251 else
2252 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2253 if (sum > 32) {
2254 if (get_bits1(&s->gb)) {
2255 px = A[0];
2256 py = A[1];
2257 } else {
2258 px = C[0];
2259 py = C[1];
2260 }
2261 } else {
2262 if (is_intra[xy - 2])
2263 sum = FFABS(px) + FFABS(py);
2264 else
2265 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2266 if (sum > 32) {
2267 if (get_bits1(&s->gb)) {
2268 px = A[0];
2269 py = A[1];
2270 } else {
2271 px = C[0];
2272 py = C[1];
2273 }
2274 }
2275 }
2276 }
2277 /* store MV using signed modulus of MV range defined in 4.11 */
2278
2279 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2280 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2281 }
2282 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2283 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2284 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2285 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2286 }
2287
2288 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2289 {
2290 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2291 MpegEncContext *s = &v->s;
2292 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2293
2294 if (v->bmvtype == BMV_TYPE_DIRECT) {
2295 int total_opp, k, f;
2296 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2297 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2298 v->bfraction, 0, s->quarter_sample);
2299 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2300 v->bfraction, 0, s->quarter_sample);
2301 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2302 v->bfraction, 1, s->quarter_sample);
2303 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2304 v->bfraction, 1, s->quarter_sample);
2305
2306 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2307 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2308 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2309 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2310 f = (total_opp > 2) ? 1 : 0;
2311 } else {
2312 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2313 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2314 f = 0;
2315 }
2316 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2317 for (k = 0; k < 4; k++) {
2318 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2319 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2320 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2321 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2322 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2323 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2324 }
2325 return;
2326 }
2327 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2328 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);
2329 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);
2330 return;
2331 }
2332 if (dir) { // backward
2333 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);
2334 if (n == 3 || mv1) {
2335 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2336 }
2337 } else { // forward
2338 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);
2339 if (n == 3 || mv1) {
2340 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2341 }
2342 }
2343 }
2344
2345 /** Get predicted DC value for I-frames only
2346 * prediction dir: left=0, top=1
2347 * @param s MpegEncContext
2348 * @param overlap flag indicating that overlap filtering is used
2349 * @param pq integer part of picture quantizer
2350 * @param[in] n block index in the current MB
2351 * @param dc_val_ptr Pointer to DC predictor
2352 * @param dir_ptr Prediction direction for use in AC prediction
2353 */
2354 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2355 int16_t **dc_val_ptr, int *dir_ptr)
2356 {
2357 int a, b, c, wrap, pred, scale;
2358 int16_t *dc_val;
2359 static const uint16_t dcpred[32] = {
2360 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2361 114, 102, 93, 85, 79, 73, 68, 64,
2362 60, 57, 54, 51, 49, 47, 45, 43,
2363 41, 39, 38, 37, 35, 34, 33
2364 };
2365
2366 /* find prediction - wmv3_dc_scale always used here in fact */
2367 if (n < 4) scale = s->y_dc_scale;
2368 else scale = s->c_dc_scale;
2369
2370 wrap = s->block_wrap[n];
2371 dc_val = s->dc_val[0] + s->block_index[n];
2372
2373 /* B A
2374 * C X
2375 */
2376 c = dc_val[ - 1];
2377 b = dc_val[ - 1 - wrap];
2378 a = dc_val[ - wrap];
2379
2380 if (pq < 9 || !overlap) {
2381 /* Set outer values */
2382 if (s->first_slice_line && (n != 2 && n != 3))
2383 b = a = dcpred[scale];
2384 if (s->mb_x == 0 && (n != 1 && n != 3))
2385 b = c = dcpred[scale];
2386 } else {
2387 /* Set outer values */
2388 if (s->first_slice_line && (n != 2 && n != 3))
2389 b = a = 0;
2390 if (s->mb_x == 0 && (n != 1 && n != 3))
2391 b = c = 0;
2392 }
2393
2394 if (abs(a - b) <= abs(b - c)) {
2395 pred = c;
2396 *dir_ptr = 1; // left
2397 } else {
2398 pred = a;
2399 *dir_ptr = 0; // top
2400 }
2401
2402 /* update predictor */
2403 *dc_val_ptr = &dc_val[0];
2404 return pred;
2405 }
2406
2407
2408 /** Get predicted DC value
2409 * prediction dir: left=0, top=1
2410 * @param s MpegEncContext
2411 * @param overlap flag indicating that overlap filtering is used
2412 * @param pq integer part of picture quantizer
2413 * @param[in] n block index in the current MB
2414 * @param a_avail flag indicating top block availability
2415 * @param c_avail flag indicating left block availability
2416 * @param dc_val_ptr Pointer to DC predictor
2417 * @param dir_ptr Prediction direction for use in AC prediction
2418 */
2419 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2420 int a_avail, int c_avail,
2421 int16_t **dc_val_ptr, int *dir_ptr)
2422 {
2423 int a, b, c, wrap, pred;
2424 int16_t *dc_val;
2425 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2426 int q1, q2 = 0;
2427 int dqscale_index;
2428
2429 wrap = s->block_wrap[n];
2430 dc_val = s->dc_val[0] + s->block_index[n];
2431
2432 /* B A
2433 * C X
2434 */
2435 c = dc_val[ - 1];
2436 b = dc_val[ - 1 - wrap];
2437 a = dc_val[ - wrap];
2438 /* scale predictors if needed */
2439 q1 = s->current_picture.qscale_table[mb_pos];
2440 dqscale_index = s->y_dc_scale_table[q1] - 1;
2441 if (dqscale_index < 0)
2442 return 0;
2443 if (c_avail && (n != 1 && n != 3)) {
2444 q2 = s->current_picture.qscale_table[mb_pos - 1];
2445 if (q2 && q2 != q1)
2446 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2447 }
2448 if (a_avail && (n != 2 && n != 3)) {
2449 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2450 if (q2 && q2 != q1)
2451 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2452 }
2453 if (a_avail && c_avail && (n != 3)) {
2454 int off = mb_pos;
2455 if (n != 1)
2456 off--;
2457 if (n != 2)
2458 off -= s->mb_stride;
2459 q2 = s->current_picture.qscale_table[off];
2460 if (q2 && q2 != q1)
2461 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2462 }
2463
2464 if (a_avail && c_avail) {
2465 if (abs(a - b) <= abs(b - c)) {
2466 pred = c;
2467 *dir_ptr = 1; // left
2468 } else {
2469 pred = a;
2470 *dir_ptr = 0; // top
2471 }
2472 } else if (a_avail) {
2473 pred = a;
2474 *dir_ptr = 0; // top
2475 } else if (c_avail) {
2476 pred = c;
2477 *dir_ptr = 1; // left
2478 } else {
2479 pred = 0;
2480 *dir_ptr = 1; // left
2481 }
2482
2483 /* update predictor */
2484 *dc_val_ptr = &dc_val[0];
2485 return pred;
2486 }
2487
2488 /** @} */ // Block group
2489
2490 /**
2491 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2492 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2493 * @{
2494 */
2495
2496 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2497 uint8_t **coded_block_ptr)
2498 {
2499 int xy, wrap, pred, a, b, c;
2500
2501 xy = s->block_index[n];
2502 wrap = s->b8_stride;
2503
2504 /* B C
2505 * A X
2506 */
2507 a = s->coded_block[xy - 1 ];
2508 b = s->coded_block[xy - 1 - wrap];
2509 c = s->coded_block[xy - wrap];
2510
2511 if (b == c) {
2512 pred = a;
2513 } else {
2514 pred = c;
2515 }
2516
2517 /* store value */
2518 *coded_block_ptr = &s->coded_block[xy];
2519
2520 return pred;
2521 }
2522
2523 /**
2524 * Decode one AC coefficient
2525 * @param v The VC1 context
2526 * @param last Last coefficient
2527 * @param skip How much zero coefficients to skip
2528 * @param value Decoded AC coefficient value
2529 * @param codingset set of VLC to decode data
2530 * @see 8.1.3.4
2531 */
2532 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2533 int *value, int codingset)
2534 {
2535 GetBitContext *gb = &v->s.gb;
2536 int index, escape, run = 0, level = 0, lst = 0;
2537
2538 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2539 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2540 run = vc1_index_decode_table[codingset][index][0];
2541 level = vc1_index_decode_table[codingset][index][1];
2542 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2543 if (get_bits1(gb))
2544 level = -level;
2545 } else {
2546 escape = decode210(gb);
2547 if (escape != 2) {
2548 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2549 run = vc1_index_decode_table[codingset][index][0];
2550 level = vc1_index_decode_table[codingset][index][1];
2551 lst = index >= vc1_last_decode_table[codingset];
2552 if (escape == 0) {
2553 if (lst)
2554 level += vc1_last_delta_level_table[codingset][run];
2555 else
2556 level += vc1_delta_level_table[codingset][run];
2557 } else {
2558 if (lst)
2559 run += vc1_last_delta_run_table[codingset][level] + 1;
2560 else
2561 run += vc1_delta_run_table[codingset][level] + 1;
2562 }
2563 if (get_bits1(gb))
2564 level = -level;
2565 } else {
2566 int sign;
2567 lst = get_bits1(gb);
2568 if (v->s.esc3_level_length == 0) {
2569 if (v->pq < 8 || v->dquantfrm) { // table 59
2570 v->s.esc3_level_length = get_bits(gb, 3);
2571 if (!v->s.esc3_level_length)
2572 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2573 } else { // table 60
2574 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2575 }
2576 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2577 }
2578 run = get_bits(gb, v->s.esc3_run_length);
2579 sign = get_bits1(gb);
2580 level = get_bits(gb, v->s.esc3_level_length);
2581 if (sign)
2582 level = -level;
2583 }
2584 }
2585
2586 *last = lst;
2587 *skip = run;
2588 *value = level;
2589 }
2590
2591 /** Decode intra block in intra frames - should be faster than decode_intra_block
2592 * @param v VC1Context
2593 * @param block block to decode
2594 * @param[in] n subblock index
2595 * @param coded are AC coeffs present or not
2596 * @param codingset set of VLC to decode data
2597 */
2598 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2599 int coded, int codingset)
2600 {
2601 GetBitContext *gb = &v->s.gb;
2602 MpegEncContext *s = &v->s;
2603 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2604 int i;
2605 int16_t *dc_val;
2606 int16_t *ac_val, *ac_val2;
2607 int dcdiff;
2608
2609 /* Get DC differential */
2610 if (n < 4) {
2611 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2612 } else {
2613 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2614 }
2615 if (dcdiff < 0) {
2616 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2617 return -1;
2618 }
2619 if (dcdiff) {
2620 if (dcdiff == 119 /* ESC index value */) {
2621 /* TODO: Optimize */
2622 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2623 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2624 else dcdiff = get_bits(gb, 8);
2625 } else {
2626 if (v->pq == 1)
2627 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2628 else if (v->pq == 2)
2629 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2630 }
2631 if (get_bits1(gb))
2632 dcdiff = -dcdiff;
2633 }
2634
2635 /* Prediction */
2636 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2637 *dc_val = dcdiff;