]> git.sesse.net Git - ffmpeg/blob - libavcodec/vc1dec.c
avcodec/x86/rv40dsp_init: fix () in macros
[ffmpeg] / 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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; 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 "mpegutils.h"
33 #include "mpegvideo.h"
34 #include "h263.h"
35 #include "h264chroma.h"
36 #include "qpeldsp.h"
37 #include "vc1.h"
38 #include "vc1data.h"
39 #include "vc1acdata.h"
40 #include "msmpeg4data.h"
41 #include "unary.h"
42 #include "mathops.h"
43 #include "vdpau_internal.h"
44 #include "libavutil/avassert.h"
45
46 #undef NDEBUG
47 #include <assert.h>
48
49 #define MB_INTRA_VLC_BITS 9
50 #define DC_VLC_BITS 9
51
52
53 // offset tables for interlaced picture MVDATA decoding
54 static const int offset_table1[9] = {  0,  1,  2,  4,  8, 16, 32,  64, 128 };
55 static const int offset_table2[9] = {  0,  1,  3,  7, 15, 31, 63, 127, 255 };
56
57 /***********************************************************************/
58 /**
59  * @name VC-1 Bitplane decoding
60  * @see 8.7, p56
61  * @{
62  */
63
64
65 static void init_block_index(VC1Context *v)
66 {
67     MpegEncContext *s = &v->s;
68     ff_init_block_index(s);
69     if (v->field_mode && !(v->second_field ^ v->tff)) {
70         s->dest[0] += s->current_picture_ptr->f->linesize[0];
71         s->dest[1] += s->current_picture_ptr->f->linesize[1];
72         s->dest[2] += s->current_picture_ptr->f->linesize[2];
73     }
74 }
75
76 /** @} */ //Bitplane group
77
78 static void vc1_put_signed_blocks_clamped(VC1Context *v)
79 {
80     MpegEncContext *s = &v->s;
81     int topleft_mb_pos, top_mb_pos;
82     int stride_y, fieldtx = 0;
83     int v_dist;
84
85     /* The put pixels loop is always one MB row behind the decoding loop,
86      * because we can only put pixels when overlap filtering is done, and
87      * for filtering of the bottom edge of a MB, we need the next MB row
88      * present as well.
89      * Within the row, the put pixels loop is also one MB col behind the
90      * decoding loop. The reason for this is again, because for filtering
91      * of the right MB edge, we need the next MB present. */
92     if (!s->first_slice_line) {
93         if (s->mb_x) {
94             topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
95             if (v->fcm == ILACE_FRAME)
96                 fieldtx = v->fieldtx_plane[topleft_mb_pos];
97             stride_y       = s->linesize << fieldtx;
98             v_dist         = (16 - fieldtx) >> (fieldtx == 0);
99             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
100                                              s->dest[0] - 16 * s->linesize - 16,
101                                              stride_y);
102             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
103                                              s->dest[0] - 16 * s->linesize - 8,
104                                              stride_y);
105             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
106                                              s->dest[0] - v_dist * s->linesize - 16,
107                                              stride_y);
108             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
109                                              s->dest[0] - v_dist * s->linesize - 8,
110                                              stride_y);
111             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
112                                              s->dest[1] - 8 * s->uvlinesize - 8,
113                                              s->uvlinesize);
114             s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
115                                              s->dest[2] - 8 * s->uvlinesize - 8,
116                                              s->uvlinesize);
117         }
118         if (s->mb_x == s->mb_width - 1) {
119             top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
120             if (v->fcm == ILACE_FRAME)
121                 fieldtx = v->fieldtx_plane[top_mb_pos];
122             stride_y   = s->linesize << fieldtx;
123             v_dist     = fieldtx ? 15 : 8;
124             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
125                                              s->dest[0] - 16 * s->linesize,
126                                              stride_y);
127             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
128                                              s->dest[0] - 16 * s->linesize + 8,
129                                              stride_y);
130             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
131                                              s->dest[0] - v_dist * s->linesize,
132                                              stride_y);
133             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
134                                              s->dest[0] - v_dist * s->linesize + 8,
135                                              stride_y);
136             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
137                                              s->dest[1] - 8 * s->uvlinesize,
138                                              s->uvlinesize);
139             s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
140                                              s->dest[2] - 8 * s->uvlinesize,
141                                              s->uvlinesize);
142         }
143     }
144
145 #define inc_blk_idx(idx) do { \
146         idx++; \
147         if (idx >= v->n_allocated_blks) \
148             idx = 0; \
149     } while (0)
150
151     inc_blk_idx(v->topleft_blk_idx);
152     inc_blk_idx(v->top_blk_idx);
153     inc_blk_idx(v->left_blk_idx);
154     inc_blk_idx(v->cur_blk_idx);
155 }
156
157 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
158 {
159     MpegEncContext *s = &v->s;
160     int j;
161     if (!s->first_slice_line) {
162         v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
163         if (s->mb_x)
164             v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
165         v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
166         for (j = 0; j < 2; j++) {
167             v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
168             if (s->mb_x)
169                 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
170         }
171     }
172     v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
173
174     if (s->mb_y == s->end_mb_y - 1) {
175         if (s->mb_x) {
176             v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
177             v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
178             v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
179         }
180         v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
181     }
182 }
183
184 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
185 {
186     MpegEncContext *s = &v->s;
187     int j;
188
189     /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
190      * means it runs two rows/cols behind the decoding loop. */
191     if (!s->first_slice_line) {
192         if (s->mb_x) {
193             if (s->mb_y >= s->start_mb_y + 2) {
194                 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
195
196                 if (s->mb_x >= 2)
197                     v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
198                 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
199                 for (j = 0; j < 2; j++) {
200                     v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
201                     if (s->mb_x >= 2) {
202                         v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
203                     }
204                 }
205             }
206             v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
207         }
208
209         if (s->mb_x == s->mb_width - 1) {
210             if (s->mb_y >= s->start_mb_y + 2) {
211                 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
212
213                 if (s->mb_x)
214                     v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
215                 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
216                 for (j = 0; j < 2; j++) {
217                     v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
218                     if (s->mb_x >= 2) {
219                         v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
220                     }
221                 }
222             }
223             v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
224         }
225
226         if (s->mb_y == s->end_mb_y) {
227             if (s->mb_x) {
228                 if (s->mb_x >= 2)
229                     v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
230                 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
231                 if (s->mb_x >= 2) {
232                     for (j = 0; j < 2; j++) {
233                         v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
234                     }
235                 }
236             }
237
238             if (s->mb_x == s->mb_width - 1) {
239                 if (s->mb_x)
240                     v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
241                 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
242                 if (s->mb_x) {
243                     for (j = 0; j < 2; j++) {
244                         v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
245                     }
246                 }
247             }
248         }
249     }
250 }
251
252 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
253 {
254     MpegEncContext *s = &v->s;
255     int mb_pos;
256
257     if (v->condover == CONDOVER_NONE)
258         return;
259
260     mb_pos = s->mb_x + s->mb_y * s->mb_stride;
261
262     /* Within a MB, the horizontal overlap always runs before the vertical.
263      * To accomplish that, we run the H on left and internal borders of the
264      * currently decoded MB. Then, we wait for the next overlap iteration
265      * to do H overlap on the right edge of this MB, before moving over and
266      * running the V overlap. Therefore, the V overlap makes us trail by one
267      * MB col and the H overlap filter makes us trail by one MB row. This
268      * is reflected in the time at which we run the put_pixels loop. */
269     if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
270         if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
271                         v->over_flags_plane[mb_pos - 1])) {
272             v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
273                                       v->block[v->cur_blk_idx][0]);
274             v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
275                                       v->block[v->cur_blk_idx][2]);
276             if (!(s->flags & CODEC_FLAG_GRAY)) {
277                 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
278                                           v->block[v->cur_blk_idx][4]);
279                 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
280                                           v->block[v->cur_blk_idx][5]);
281             }
282         }
283         v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
284                                   v->block[v->cur_blk_idx][1]);
285         v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
286                                   v->block[v->cur_blk_idx][3]);
287
288         if (s->mb_x == s->mb_width - 1) {
289             if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
290                                          v->over_flags_plane[mb_pos - s->mb_stride])) {
291                 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
292                                           v->block[v->cur_blk_idx][0]);
293                 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
294                                           v->block[v->cur_blk_idx][1]);
295                 if (!(s->flags & CODEC_FLAG_GRAY)) {
296                     v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
297                                               v->block[v->cur_blk_idx][4]);
298                     v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
299                                               v->block[v->cur_blk_idx][5]);
300                 }
301             }
302             v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
303                                       v->block[v->cur_blk_idx][2]);
304             v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
305                                       v->block[v->cur_blk_idx][3]);
306         }
307     }
308     if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
309         if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
310                                      v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
311             v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
312                                       v->block[v->left_blk_idx][0]);
313             v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
314                                       v->block[v->left_blk_idx][1]);
315             if (!(s->flags & CODEC_FLAG_GRAY)) {
316                 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
317                                           v->block[v->left_blk_idx][4]);
318                 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
319                                           v->block[v->left_blk_idx][5]);
320             }
321         }
322         v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
323                                   v->block[v->left_blk_idx][2]);
324         v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
325                                   v->block[v->left_blk_idx][3]);
326     }
327 }
328
329 /** Do motion compensation over 1 macroblock
330  * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
331  */
332 static void vc1_mc_1mv(VC1Context *v, int dir)
333 {
334     MpegEncContext *s = &v->s;
335     H264ChromaContext *h264chroma = &v->h264chroma;
336     uint8_t *srcY, *srcU, *srcV;
337     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
338     int v_edge_pos = s->v_edge_pos >> v->field_mode;
339     int i;
340     uint8_t (*luty)[256], (*lutuv)[256];
341     int use_ic;
342
343     if ((!v->field_mode ||
344          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
345         !v->s.last_picture.f->data[0])
346         return;
347
348     mx = s->mv[dir][0][0];
349     my = s->mv[dir][0][1];
350
351     // store motion vectors for further use in B frames
352     if (s->pict_type == AV_PICTURE_TYPE_P) {
353         for (i = 0; i < 4; i++) {
354             s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
355             s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
356         }
357     }
358
359     uvmx = (mx + ((mx & 3) == 3)) >> 1;
360     uvmy = (my + ((my & 3) == 3)) >> 1;
361     v->luma_mv[s->mb_x][0] = uvmx;
362     v->luma_mv[s->mb_x][1] = uvmy;
363
364     if (v->field_mode &&
365         v->cur_field_type != v->ref_field_type[dir]) {
366         my   = my   - 2 + 4 * v->cur_field_type;
367         uvmy = uvmy - 2 + 4 * v->cur_field_type;
368     }
369
370     // fastuvmc shall be ignored for interlaced frame picture
371     if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
372         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
373         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
374     }
375     if (!dir) {
376         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
377             srcY = s->current_picture.f->data[0];
378             srcU = s->current_picture.f->data[1];
379             srcV = s->current_picture.f->data[2];
380             luty  = v->curr_luty;
381             lutuv = v->curr_lutuv;
382             use_ic = *v->curr_use_ic;
383         } else {
384             srcY = s->last_picture.f->data[0];
385             srcU = s->last_picture.f->data[1];
386             srcV = s->last_picture.f->data[2];
387             luty  = v->last_luty;
388             lutuv = v->last_lutuv;
389             use_ic = v->last_use_ic;
390         }
391     } else {
392         srcY = s->next_picture.f->data[0];
393         srcU = s->next_picture.f->data[1];
394         srcV = s->next_picture.f->data[2];
395         luty  = v->next_luty;
396         lutuv = v->next_lutuv;
397         use_ic = v->next_use_ic;
398     }
399
400     if (!srcY || !srcU) {
401         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
402         return;
403     }
404
405     src_x   = s->mb_x * 16 + (mx   >> 2);
406     src_y   = s->mb_y * 16 + (my   >> 2);
407     uvsrc_x = s->mb_x *  8 + (uvmx >> 2);
408     uvsrc_y = s->mb_y *  8 + (uvmy >> 2);
409
410     if (v->profile != PROFILE_ADVANCED) {
411         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
412         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
413         uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
414         uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
415     } else {
416         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
417         src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
418         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
419         uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
420     }
421
422     srcY += src_y   * s->linesize   + src_x;
423     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
424     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
425
426     if (v->field_mode && v->ref_field_type[dir]) {
427         srcY += s->current_picture_ptr->f->linesize[0];
428         srcU += s->current_picture_ptr->f->linesize[1];
429         srcV += s->current_picture_ptr->f->linesize[2];
430     }
431
432     /* for grayscale we should not try to read from unknown area */
433     if (s->flags & CODEC_FLAG_GRAY) {
434         srcU = s->edge_emu_buffer + 18 * s->linesize;
435         srcV = s->edge_emu_buffer + 18 * s->linesize;
436     }
437
438     if (v->rangeredfrm || use_ic
439         || s->h_edge_pos < 22 || v_edge_pos < 22
440         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
441         || (unsigned)(src_y - 1)        > v_edge_pos    - (my&3) - 16 - 3) {
442         uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
443         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
444
445         srcY -= s->mspel * (1 + s->linesize);
446         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
447                                  s->linesize, s->linesize,
448                                  17 + s->mspel * 2, 17 + s->mspel * 2,
449                                  src_x - s->mspel, src_y - s->mspel,
450                                  s->h_edge_pos, v_edge_pos);
451         srcY = s->edge_emu_buffer;
452         s->vdsp.emulated_edge_mc(ubuf, srcU,
453                                  s->uvlinesize, s->uvlinesize,
454                                  8 + 1, 8 + 1,
455                                  uvsrc_x, uvsrc_y,
456                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
457         s->vdsp.emulated_edge_mc(vbuf, srcV,
458                                  s->uvlinesize, s->uvlinesize,
459                                  8 + 1, 8 + 1,
460                                  uvsrc_x, uvsrc_y,
461                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
462         srcU = ubuf;
463         srcV = vbuf;
464         /* if we deal with range reduction we need to scale source blocks */
465         if (v->rangeredfrm) {
466             int i, j;
467             uint8_t *src, *src2;
468
469             src = srcY;
470             for (j = 0; j < 17 + s->mspel * 2; j++) {
471                 for (i = 0; i < 17 + s->mspel * 2; i++)
472                     src[i] = ((src[i] - 128) >> 1) + 128;
473                 src += s->linesize;
474             }
475             src  = srcU;
476             src2 = srcV;
477             for (j = 0; j < 9; j++) {
478                 for (i = 0; i < 9; i++) {
479                     src[i]  = ((src[i]  - 128) >> 1) + 128;
480                     src2[i] = ((src2[i] - 128) >> 1) + 128;
481                 }
482                 src  += s->uvlinesize;
483                 src2 += s->uvlinesize;
484             }
485         }
486         /* if we deal with intensity compensation we need to scale source blocks */
487         if (use_ic) {
488             int i, j;
489             uint8_t *src, *src2;
490
491             src = srcY;
492             for (j = 0; j < 17 + s->mspel * 2; j++) {
493                 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
494                 for (i = 0; i < 17 + s->mspel * 2; i++)
495                     src[i] = luty[f][src[i]];
496                 src += s->linesize;
497             }
498             src  = srcU;
499             src2 = srcV;
500             for (j = 0; j < 9; j++) {
501                 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
502                 for (i = 0; i < 9; i++) {
503                     src[i]  = lutuv[f][src[i]];
504                     src2[i] = lutuv[f][src2[i]];
505                 }
506                 src  += s->uvlinesize;
507                 src2 += s->uvlinesize;
508             }
509         }
510         srcY += s->mspel * (1 + s->linesize);
511     }
512
513     if (s->mspel) {
514         dxy = ((my & 3) << 2) | (mx & 3);
515         v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0]    , srcY    , s->linesize, v->rnd);
516     } else { // hpel mc - always used for luma
517         dxy = (my & 2) | ((mx & 2) >> 1);
518         if (!v->rnd)
519             s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
520         else
521             s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
522     }
523
524     if (s->flags & CODEC_FLAG_GRAY) return;
525     /* Chroma MC always uses qpel bilinear */
526     uvmx = (uvmx & 3) << 1;
527     uvmy = (uvmy & 3) << 1;
528     if (!v->rnd) {
529         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
530         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
531     } else {
532         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
533         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
534     }
535 }
536
537 static inline int median4(int a, int b, int c, int d)
538 {
539     if (a < b) {
540         if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
541         else       return (FFMIN(b, c) + FFMAX(a, d)) / 2;
542     } else {
543         if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
544         else       return (FFMIN(a, c) + FFMAX(b, d)) / 2;
545     }
546 }
547
548 /** Do motion compensation for 4-MV macroblock - luminance block
549  */
550 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
551 {
552     MpegEncContext *s = &v->s;
553     uint8_t *srcY;
554     int dxy, mx, my, src_x, src_y;
555     int off;
556     int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
557     int v_edge_pos = s->v_edge_pos >> v->field_mode;
558     uint8_t (*luty)[256];
559     int use_ic;
560
561     if ((!v->field_mode ||
562          (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
563         !v->s.last_picture.f->data[0])
564         return;
565
566     mx = s->mv[dir][n][0];
567     my = s->mv[dir][n][1];
568
569     if (!dir) {
570         if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
571             srcY = s->current_picture.f->data[0];
572             luty = v->curr_luty;
573             use_ic = *v->curr_use_ic;
574         } else {
575             srcY = s->last_picture.f->data[0];
576             luty = v->last_luty;
577             use_ic = v->last_use_ic;
578         }
579     } else {
580         srcY = s->next_picture.f->data[0];
581         luty = v->next_luty;
582         use_ic = v->next_use_ic;
583     }
584
585     if (!srcY) {
586         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
587         return;
588     }
589
590     if (v->field_mode) {
591         if (v->cur_field_type != v->ref_field_type[dir])
592             my = my - 2 + 4 * v->cur_field_type;
593     }
594
595     if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
596         int same_count = 0, opp_count = 0, k;
597         int chosen_mv[2][4][2], f;
598         int tx, ty;
599         for (k = 0; k < 4; k++) {
600             f = v->mv_f[0][s->block_index[k] + v->blocks_off];
601             chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
602             chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
603             opp_count  += f;
604             same_count += 1 - f;
605         }
606         f = opp_count > same_count;
607         switch (f ? opp_count : same_count) {
608         case 4:
609             tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
610                          chosen_mv[f][2][0], chosen_mv[f][3][0]);
611             ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
612                          chosen_mv[f][2][1], chosen_mv[f][3][1]);
613             break;
614         case 3:
615             tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
616             ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
617             break;
618         case 2:
619             tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
620             ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
621             break;
622         default:
623             av_assert0(0);
624         }
625         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
626         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
627         for (k = 0; k < 4; k++)
628             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
629     }
630
631     if (v->fcm == ILACE_FRAME) {  // not sure if needed for other types of picture
632         int qx, qy;
633         int width  = s->avctx->coded_width;
634         int height = s->avctx->coded_height >> 1;
635         if (s->pict_type == AV_PICTURE_TYPE_P) {
636             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
637             s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
638         }
639         qx = (s->mb_x * 16) + (mx >> 2);
640         qy = (s->mb_y *  8) + (my >> 3);
641
642         if (qx < -17)
643             mx -= 4 * (qx + 17);
644         else if (qx > width)
645             mx -= 4 * (qx - width);
646         if (qy < -18)
647             my -= 8 * (qy + 18);
648         else if (qy > height + 1)
649             my -= 8 * (qy - height - 1);
650     }
651
652     if ((v->fcm == ILACE_FRAME) && fieldmv)
653         off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
654     else
655         off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
656
657     src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
658     if (!fieldmv)
659         src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
660     else
661         src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
662
663     if (v->profile != PROFILE_ADVANCED) {
664         src_x = av_clip(src_x, -16, s->mb_width  * 16);
665         src_y = av_clip(src_y, -16, s->mb_height * 16);
666     } else {
667         src_x = av_clip(src_x, -17, s->avctx->coded_width);
668         if (v->fcm == ILACE_FRAME) {
669             if (src_y & 1)
670                 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
671             else
672                 src_y = av_clip(src_y, -18, s->avctx->coded_height);
673         } else {
674             src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
675         }
676     }
677
678     srcY += src_y * s->linesize + src_x;
679     if (v->field_mode && v->ref_field_type[dir])
680         srcY += s->current_picture_ptr->f->linesize[0];
681
682     if (fieldmv && !(src_y & 1))
683         v_edge_pos--;
684     if (fieldmv && (src_y & 1) && src_y < 4)
685         src_y--;
686     if (v->rangeredfrm || use_ic
687         || s->h_edge_pos < 13 || v_edge_pos < 23
688         || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
689         || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
690         srcY -= s->mspel * (1 + (s->linesize << fieldmv));
691         /* check emulate edge stride and offset */
692         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
693                                  s->linesize, s->linesize,
694                                  9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
695                                  src_x - s->mspel, src_y - (s->mspel << fieldmv),
696                                  s->h_edge_pos, v_edge_pos);
697         srcY = s->edge_emu_buffer;
698         /* if we deal with range reduction we need to scale source blocks */
699         if (v->rangeredfrm) {
700             int i, j;
701             uint8_t *src;
702
703             src = srcY;
704             for (j = 0; j < 9 + s->mspel * 2; j++) {
705                 for (i = 0; i < 9 + s->mspel * 2; i++)
706                     src[i] = ((src[i] - 128) >> 1) + 128;
707                 src += s->linesize << fieldmv;
708             }
709         }
710         /* if we deal with intensity compensation we need to scale source blocks */
711         if (use_ic) {
712             int i, j;
713             uint8_t *src;
714
715             src = srcY;
716             for (j = 0; j < 9 + s->mspel * 2; j++) {
717                 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
718                 for (i = 0; i < 9 + s->mspel * 2; i++)
719                     src[i] = luty[f][src[i]];
720                 src += s->linesize << fieldmv;
721             }
722         }
723         srcY += s->mspel * (1 + (s->linesize << fieldmv));
724     }
725
726     if (s->mspel) {
727         dxy = ((my & 3) << 2) | (mx & 3);
728         if (avg)
729             v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
730         else
731             v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
732     } else { // hpel mc - always used for luma
733         dxy = (my & 2) | ((mx & 2) >> 1);
734         if (!v->rnd)
735             s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
736         else
737             s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
738     }
739 }
740
741 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
742 {
743     int idx, i;
744     static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
745
746     idx =  ((a[3] != flag) << 3)
747          | ((a[2] != flag) << 2)
748          | ((a[1] != flag) << 1)
749          |  (a[0] != flag);
750     if (!idx) {
751         *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
752         *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
753         return 4;
754     } else if (count[idx] == 1) {
755         switch (idx) {
756         case 0x1:
757             *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
758             *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
759             return 3;
760         case 0x2:
761             *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
762             *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
763             return 3;
764         case 0x4:
765             *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
766             *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
767             return 3;
768         case 0x8:
769             *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
770             *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
771             return 3;
772         }
773     } else if (count[idx] == 2) {
774         int t1 = 0, t2 = 0;
775         for (i = 0; i < 3; i++)
776             if (!a[i]) {
777                 t1 = i;
778                 break;
779             }
780         for (i = t1 + 1; i < 4; i++)
781             if (!a[i]) {
782                 t2 = i;
783                 break;
784             }
785         *tx = (mvx[t1] + mvx[t2]) / 2;
786         *ty = (mvy[t1] + mvy[t2]) / 2;
787         return 2;
788     } else {
789         return 0;
790     }
791     return -1;
792 }
793
794 /** Do motion compensation for 4-MV macroblock - both chroma blocks
795  */
796 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
797 {
798     MpegEncContext *s = &v->s;
799     H264ChromaContext *h264chroma = &v->h264chroma;
800     uint8_t *srcU, *srcV;
801     int uvmx, uvmy, uvsrc_x, uvsrc_y;
802     int k, tx = 0, ty = 0;
803     int mvx[4], mvy[4], intra[4], mv_f[4];
804     int valid_count;
805     int chroma_ref_type = v->cur_field_type;
806     int v_edge_pos = s->v_edge_pos >> v->field_mode;
807     uint8_t (*lutuv)[256];
808     int use_ic;
809
810     if (!v->field_mode && !v->s.last_picture.f->data[0])
811         return;
812     if (s->flags & CODEC_FLAG_GRAY)
813         return;
814
815     for (k = 0; k < 4; k++) {
816         mvx[k] = s->mv[dir][k][0];
817         mvy[k] = s->mv[dir][k][1];
818         intra[k] = v->mb_type[0][s->block_index[k]];
819         if (v->field_mode)
820             mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
821     }
822
823     /* calculate chroma MV vector from four luma MVs */
824     if (!v->field_mode || (v->field_mode && !v->numref)) {
825         valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
826         chroma_ref_type = v->reffield;
827         if (!valid_count) {
828             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
829             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
830             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
831             return; //no need to do MC for intra blocks
832         }
833     } else {
834         int dominant = 0;
835         if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
836             dominant = 1;
837         valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
838         if (dominant)
839             chroma_ref_type = !v->cur_field_type;
840     }
841     if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
842         return;
843     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
844     s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
845     uvmx = (tx + ((tx & 3) == 3)) >> 1;
846     uvmy = (ty + ((ty & 3) == 3)) >> 1;
847
848     v->luma_mv[s->mb_x][0] = uvmx;
849     v->luma_mv[s->mb_x][1] = uvmy;
850
851     if (v->fastuvmc) {
852         uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
853         uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
854     }
855     // Field conversion bias
856     if (v->cur_field_type != chroma_ref_type)
857         uvmy += 2 - 4 * chroma_ref_type;
858
859     uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
860     uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
861
862     if (v->profile != PROFILE_ADVANCED) {
863         uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width  * 8);
864         uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
865     } else {
866         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
867         uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
868     }
869
870     if (!dir) {
871         if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
872             srcU = s->current_picture.f->data[1];
873             srcV = s->current_picture.f->data[2];
874             lutuv = v->curr_lutuv;
875             use_ic = *v->curr_use_ic;
876         } else {
877             srcU = s->last_picture.f->data[1];
878             srcV = s->last_picture.f->data[2];
879             lutuv = v->last_lutuv;
880             use_ic = v->last_use_ic;
881         }
882     } else {
883         srcU = s->next_picture.f->data[1];
884         srcV = s->next_picture.f->data[2];
885         lutuv = v->next_lutuv;
886         use_ic = v->next_use_ic;
887     }
888
889     if (!srcU) {
890         av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
891         return;
892     }
893
894     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
895     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
896
897     if (v->field_mode) {
898         if (chroma_ref_type) {
899             srcU += s->current_picture_ptr->f->linesize[1];
900             srcV += s->current_picture_ptr->f->linesize[2];
901         }
902     }
903
904     if (v->rangeredfrm || use_ic
905         || s->h_edge_pos < 18 || v_edge_pos < 18
906         || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
907         || (unsigned)uvsrc_y > (v_edge_pos    >> 1) - 9) {
908         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
909                                  s->uvlinesize, s->uvlinesize,
910                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
911                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
912         s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
913                                  s->uvlinesize, s->uvlinesize,
914                                  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
915                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
916         srcU = s->edge_emu_buffer;
917         srcV = s->edge_emu_buffer + 16;
918
919         /* if we deal with range reduction we need to scale source blocks */
920         if (v->rangeredfrm) {
921             int i, j;
922             uint8_t *src, *src2;
923
924             src  = srcU;
925             src2 = srcV;
926             for (j = 0; j < 9; j++) {
927                 for (i = 0; i < 9; i++) {
928                     src[i]  = ((src[i]  - 128) >> 1) + 128;
929                     src2[i] = ((src2[i] - 128) >> 1) + 128;
930                 }
931                 src  += s->uvlinesize;
932                 src2 += s->uvlinesize;
933             }
934         }
935         /* if we deal with intensity compensation we need to scale source blocks */
936         if (use_ic) {
937             int i, j;
938             uint8_t *src, *src2;
939
940             src  = srcU;
941             src2 = srcV;
942             for (j = 0; j < 9; j++) {
943                 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
944                 for (i = 0; i < 9; i++) {
945                     src[i]  = lutuv[f][src[i]];
946                     src2[i] = lutuv[f][src2[i]];
947                 }
948                 src  += s->uvlinesize;
949                 src2 += s->uvlinesize;
950             }
951         }
952     }
953
954     /* Chroma MC always uses qpel bilinear */
955     uvmx = (uvmx & 3) << 1;
956     uvmy = (uvmy & 3) << 1;
957     if (!v->rnd) {
958         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
959         h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
960     } else {
961         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
962         v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
963     }
964 }
965
966 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
967  */
968 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
969 {
970     MpegEncContext *s = &v->s;
971     H264ChromaContext *h264chroma = &v->h264chroma;
972     uint8_t *srcU, *srcV;
973     int uvsrc_x, uvsrc_y;
974     int uvmx_field[4], uvmy_field[4];
975     int i, off, tx, ty;
976     int fieldmv = v->blk_mv_type[s->block_index[0]];
977     static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
978     int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
979     int v_edge_pos = s->v_edge_pos >> 1;
980     int use_ic;
981     uint8_t (*lutuv)[256];
982
983     if (s->flags & CODEC_FLAG_GRAY)
984         return;
985
986     for (i = 0; i < 4; i++) {
987         int d = i < 2 ? dir: dir2;
988         tx = s->mv[d][i][0];
989         uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
990         ty = s->mv[d][i][1];
991         if (fieldmv)
992             uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
993         else
994             uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
995     }
996
997     for (i = 0; i < 4; i++) {
998         off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
999         uvsrc_x = s->mb_x * 8 +  (i & 1) * 4           + (uvmx_field[i] >> 2);
1000         uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1001         // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1002         uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width  >> 1);
1003         uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1004         if (i < 2 ? dir : dir2) {
1005             srcU = s->next_picture.f->data[1];
1006             srcV = s->next_picture.f->data[2];
1007             lutuv  = v->next_lutuv;
1008             use_ic = v->next_use_ic;
1009         } else {
1010             srcU = s->last_picture.f->data[1];
1011             srcV = s->last_picture.f->data[2];
1012             lutuv  = v->last_lutuv;
1013             use_ic = v->last_use_ic;
1014         }
1015         if (!srcU)
1016             return;
1017         srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1018         srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1019         uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1020         uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1021
1022         if (fieldmv && !(uvsrc_y & 1))
1023             v_edge_pos = (s->v_edge_pos >> 1) - 1;
1024
1025         if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1026             uvsrc_y--;
1027         if (use_ic
1028             || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1029             || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1030             || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1031             s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
1032                                      s->uvlinesize, s->uvlinesize,
1033                                      5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1034                                      s->h_edge_pos >> 1, v_edge_pos);
1035             s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
1036                                      s->uvlinesize, s->uvlinesize,
1037                                      5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1038                                      s->h_edge_pos >> 1, v_edge_pos);
1039             srcU = s->edge_emu_buffer;
1040             srcV = s->edge_emu_buffer + 16;
1041
1042             /* if we deal with intensity compensation we need to scale source blocks */
1043             if (use_ic) {
1044                 int i, j;
1045                 uint8_t *src, *src2;
1046
1047                 src  = srcU;
1048                 src2 = srcV;
1049                 for (j = 0; j < 5; j++) {
1050                     int f = (uvsrc_y + (j << fieldmv)) & 1;
1051                     for (i = 0; i < 5; i++) {
1052                         src[i]  = lutuv[f][src[i]];
1053                         src2[i] = lutuv[f][src2[i]];
1054                     }
1055                     src  += s->uvlinesize << fieldmv;
1056                     src2 += s->uvlinesize << fieldmv;
1057                 }
1058             }
1059         }
1060         if (avg) {
1061             if (!v->rnd) {
1062                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1063                 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1064             } else {
1065                 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]);
1066                 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]);
1067             }
1068         } else {
1069             if (!v->rnd) {
1070                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1071                 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1072             } else {
1073                 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]);
1074                 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]);
1075             }
1076         }
1077     }
1078 }
1079
1080 /***********************************************************************/
1081 /**
1082  * @name VC-1 Block-level functions
1083  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1084  * @{
1085  */
1086
1087 /**
1088  * @def GET_MQUANT
1089  * @brief Get macroblock-level quantizer scale
1090  */
1091 #define GET_MQUANT()                                           \
1092     if (v->dquantfrm) {                                        \
1093         int edges = 0;                                         \
1094         if (v->dqprofile == DQPROFILE_ALL_MBS) {               \
1095             if (v->dqbilevel) {                                \
1096                 mquant = (get_bits1(gb)) ? v->altpq : v->pq;   \
1097             } else {                                           \
1098                 mqdiff = get_bits(gb, 3);                      \
1099                 if (mqdiff != 7)                               \
1100                     mquant = v->pq + mqdiff;                   \
1101                 else                                           \
1102                     mquant = get_bits(gb, 5);                  \
1103             }                                                  \
1104         }                                                      \
1105         if (v->dqprofile == DQPROFILE_SINGLE_EDGE)             \
1106             edges = 1 << v->dqsbedge;                          \
1107         else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES)       \
1108             edges = (3 << v->dqsbedge) % 15;                   \
1109         else if (v->dqprofile == DQPROFILE_FOUR_EDGES)         \
1110             edges = 15;                                        \
1111         if ((edges&1) && !s->mb_x)                             \
1112             mquant = v->altpq;                                 \
1113         if ((edges&2) && s->first_slice_line)                  \
1114             mquant = v->altpq;                                 \
1115         if ((edges&4) && s->mb_x == (s->mb_width - 1))         \
1116             mquant = v->altpq;                                 \
1117         if ((edges&8) && s->mb_y == (s->mb_height - 1))        \
1118             mquant = v->altpq;                                 \
1119         if (!mquant || mquant > 31) {                          \
1120             av_log(v->s.avctx, AV_LOG_ERROR,                   \
1121                    "Overriding invalid mquant %d\n", mquant);  \
1122             mquant = 1;                                        \
1123         }                                                      \
1124     }
1125
1126 /**
1127  * @def GET_MVDATA(_dmv_x, _dmv_y)
1128  * @brief Get MV differentials
1129  * @see MVDATA decoding from 8.3.5.2, p(1)20
1130  * @param _dmv_x Horizontal differential for decoded MV
1131  * @param _dmv_y Vertical differential for decoded MV
1132  */
1133 #define GET_MVDATA(_dmv_x, _dmv_y)                                      \
1134     index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1135                          VC1_MV_DIFF_VLC_BITS, 2);                      \
1136     if (index > 36) {                                                   \
1137         mb_has_coeffs = 1;                                              \
1138         index -= 37;                                                    \
1139     } else                                                              \
1140         mb_has_coeffs = 0;                                              \
1141     s->mb_intra = 0;                                                    \
1142     if (!index) {                                                       \
1143         _dmv_x = _dmv_y = 0;                                            \
1144     } else if (index == 35) {                                           \
1145         _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample);          \
1146         _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample);          \
1147     } else if (index == 36) {                                           \
1148         _dmv_x = 0;                                                     \
1149         _dmv_y = 0;                                                     \
1150         s->mb_intra = 1;                                                \
1151     } else {                                                            \
1152         index1 = index % 6;                                             \
1153         if (!s->quarter_sample && index1 == 5) val = 1;                 \
1154         else                                   val = 0;                 \
1155         if (size_table[index1] - val > 0)                               \
1156             val = get_bits(gb, size_table[index1] - val);               \
1157         else                                   val = 0;                 \
1158         sign = 0 - (val&1);                                             \
1159         _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign;     \
1160                                                                         \
1161         index1 = index / 6;                                             \
1162         if (!s->quarter_sample && index1 == 5) val = 1;                 \
1163         else                                   val = 0;                 \
1164         if (size_table[index1] - val > 0)                               \
1165             val = get_bits(gb, size_table[index1] - val);               \
1166         else                                   val = 0;                 \
1167         sign = 0 - (val & 1);                                           \
1168         _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign;   \
1169     }
1170
1171 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1172                                                    int *dmv_y, int *pred_flag)
1173 {
1174     int index, index1;
1175     int extend_x = 0, extend_y = 0;
1176     GetBitContext *gb = &v->s.gb;
1177     int bits, esc;
1178     int val, sign;
1179     const int* offs_tab;
1180
1181     if (v->numref) {
1182         bits = VC1_2REF_MVDATA_VLC_BITS;
1183         esc  = 125;
1184     } else {
1185         bits = VC1_1REF_MVDATA_VLC_BITS;
1186         esc  = 71;
1187     }
1188     switch (v->dmvrange) {
1189     case 1:
1190         extend_x = 1;
1191         break;
1192     case 2:
1193         extend_y = 1;
1194         break;
1195     case 3:
1196         extend_x = extend_y = 1;
1197         break;
1198     }
1199     index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1200     if (index == esc) {
1201         *dmv_x = get_bits(gb, v->k_x);
1202         *dmv_y = get_bits(gb, v->k_y);
1203         if (v->numref) {
1204             if (pred_flag) {
1205                 *pred_flag = *dmv_y & 1;
1206                 *dmv_y     = (*dmv_y + *pred_flag) >> 1;
1207             } else {
1208                 *dmv_y     = (*dmv_y + (*dmv_y & 1)) >> 1;
1209             }
1210         }
1211     }
1212     else {
1213         av_assert0(index < esc);
1214         if (extend_x)
1215             offs_tab = offset_table2;
1216         else
1217             offs_tab = offset_table1;
1218         index1 = (index + 1) % 9;
1219         if (index1 != 0) {
1220             val    = get_bits(gb, index1 + extend_x);
1221             sign   = 0 -(val & 1);
1222             *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1223         } else
1224             *dmv_x = 0;
1225         if (extend_y)
1226             offs_tab = offset_table2;
1227         else
1228             offs_tab = offset_table1;
1229         index1 = (index + 1) / 9;
1230         if (index1 > v->numref) {
1231             val    = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1232             sign   = 0 - (val & 1);
1233             *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1234         } else
1235             *dmv_y = 0;
1236         if (v->numref && pred_flag)
1237             *pred_flag = index1 & 1;
1238     }
1239 }
1240
1241 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1242 {
1243     int scaledvalue, refdist;
1244     int scalesame1, scalesame2;
1245     int scalezone1_x, zone1offset_x;
1246     int table_index = dir ^ v->second_field;
1247
1248     if (v->s.pict_type != AV_PICTURE_TYPE_B)
1249         refdist = v->refdist;
1250     else
1251         refdist = dir ? v->brfd : v->frfd;
1252     if (refdist > 3)
1253         refdist = 3;
1254     scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1255     scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1256     scalezone1_x  = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1257     zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1258
1259     if (FFABS(n) > 255)
1260         scaledvalue = n;
1261     else {
1262         if (FFABS(n) < scalezone1_x)
1263             scaledvalue = (n * scalesame1) >> 8;
1264         else {
1265             if (n < 0)
1266                 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1267             else
1268                 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1269         }
1270     }
1271     return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1272 }
1273
1274 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1275 {
1276     int scaledvalue, refdist;
1277     int scalesame1, scalesame2;
1278     int scalezone1_y, zone1offset_y;
1279     int table_index = dir ^ v->second_field;
1280
1281     if (v->s.pict_type != AV_PICTURE_TYPE_B)
1282         refdist = v->refdist;
1283     else
1284         refdist = dir ? v->brfd : v->frfd;
1285     if (refdist > 3)
1286         refdist = 3;
1287     scalesame1    = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1288     scalesame2    = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1289     scalezone1_y  = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1290     zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1291
1292     if (FFABS(n) > 63)
1293         scaledvalue = n;
1294     else {
1295         if (FFABS(n) < scalezone1_y)
1296             scaledvalue = (n * scalesame1) >> 8;
1297         else {
1298             if (n < 0)
1299                 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1300             else
1301                 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1302         }
1303     }
1304
1305     if (v->cur_field_type && !v->ref_field_type[dir])
1306         return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1307     else
1308         return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1309 }
1310
1311 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1312 {
1313     int scalezone1_x, zone1offset_x;
1314     int scaleopp1, scaleopp2, brfd;
1315     int scaledvalue;
1316
1317     brfd = FFMIN(v->brfd, 3);
1318     scalezone1_x  = ff_vc1_b_field_mvpred_scales[3][brfd];
1319     zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1320     scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
1321     scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
1322
1323     if (FFABS(n) > 255)
1324         scaledvalue = n;
1325     else {
1326         if (FFABS(n) < scalezone1_x)
1327             scaledvalue = (n * scaleopp1) >> 8;
1328         else {
1329             if (n < 0)
1330                 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1331             else
1332                 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1333         }
1334     }
1335     return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1336 }
1337
1338 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1339 {
1340     int scalezone1_y, zone1offset_y;
1341     int scaleopp1, scaleopp2, brfd;
1342     int scaledvalue;
1343
1344     brfd = FFMIN(v->brfd, 3);
1345     scalezone1_y  = ff_vc1_b_field_mvpred_scales[4][brfd];
1346     zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1347     scaleopp1     = ff_vc1_b_field_mvpred_scales[1][brfd];
1348     scaleopp2     = ff_vc1_b_field_mvpred_scales[2][brfd];
1349
1350     if (FFABS(n) > 63)
1351         scaledvalue = n;
1352     else {
1353         if (FFABS(n) < scalezone1_y)
1354             scaledvalue = (n * scaleopp1) >> 8;
1355         else {
1356             if (n < 0)
1357                 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1358             else
1359                 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1360         }
1361     }
1362     if (v->cur_field_type && !v->ref_field_type[dir]) {
1363         return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1364     } else {
1365         return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1366     }
1367 }
1368
1369 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1370                                          int dim, int dir)
1371 {
1372     int brfd, scalesame;
1373     int hpel = 1 - v->s.quarter_sample;
1374
1375     n >>= hpel;
1376     if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1377         if (dim)
1378             n = scaleforsame_y(v, i, n, dir) << hpel;
1379         else
1380             n = scaleforsame_x(v, n, dir) << hpel;
1381         return n;
1382     }
1383     brfd      = FFMIN(v->brfd, 3);
1384     scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1385
1386     n = (n * scalesame >> 8) << hpel;
1387     return n;
1388 }
1389
1390 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1391                                         int dim, int dir)
1392 {
1393     int refdist, scaleopp;
1394     int hpel = 1 - v->s.quarter_sample;
1395
1396     n >>= hpel;
1397     if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1398         if (dim)
1399             n = scaleforopp_y(v, n, dir) << hpel;
1400         else
1401             n = scaleforopp_x(v, n) << hpel;
1402         return n;
1403     }
1404     if (v->s.pict_type != AV_PICTURE_TYPE_B)
1405         refdist = FFMIN(v->refdist, 3);
1406     else
1407         refdist = dir ? v->brfd : v->frfd;
1408     scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1409
1410     n = (n * scaleopp >> 8) << hpel;
1411     return n;
1412 }
1413
1414 /** Predict and set motion vector
1415  */
1416 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1417                                int mv1, int r_x, int r_y, uint8_t* is_intra,
1418                                int pred_flag, int dir)
1419 {
1420     MpegEncContext *s = &v->s;
1421     int xy, wrap, off = 0;
1422     int16_t *A, *B, *C;
1423     int px, py;
1424     int sum;
1425     int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1426     int opposite, a_f, b_f, c_f;
1427     int16_t field_predA[2];
1428     int16_t field_predB[2];
1429     int16_t field_predC[2];
1430     int a_valid, b_valid, c_valid;
1431     int hybridmv_thresh, y_bias = 0;
1432
1433     if (v->mv_mode == MV_PMODE_MIXED_MV ||
1434         ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1435         mixedmv_pic = 1;
1436     else
1437         mixedmv_pic = 0;
1438     /* scale MV difference to be quad-pel */
1439     dmv_x <<= 1 - s->quarter_sample;
1440     dmv_y <<= 1 - s->quarter_sample;
1441
1442     wrap = s->b8_stride;
1443     xy   = s->block_index[n];
1444
1445     if (s->mb_intra) {
1446         s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1447         s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1448         s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1449         s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1450         if (mv1) { /* duplicate motion data for 1-MV block */
1451             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0]        = 0;
1452             s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1]        = 0;
1453             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0]     = 0;
1454             s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1]     = 0;
1455             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1456             s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1457             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1458             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0]        = 0;
1459             s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1]        = 0;
1460             s->current_picture.motion_val[1][xy + wrap][0]                     = 0;
1461             s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1]     = 0;
1462             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1463             s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1464         }
1465         return;
1466     }
1467
1468     C = s->current_picture.motion_val[dir][xy -    1 + v->blocks_off];
1469     A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1470     if (mv1) {
1471         if (v->field_mode && mixedmv_pic)
1472             off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1473         else
1474             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1475     } else {
1476         //in 4-MV mode different blocks have different B predictor position
1477         switch (n) {
1478         case 0:
1479             off = (s->mb_x > 0) ? -1 : 1;
1480             break;
1481         case 1:
1482             off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1483             break;
1484         case 2:
1485             off = 1;
1486             break;
1487         case 3:
1488             off = -1;
1489         }
1490     }
1491     B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1492
1493     a_valid = !s->first_slice_line || (n == 2 || n == 3);
1494     b_valid = a_valid && (s->mb_width > 1);
1495     c_valid = s->mb_x || (n == 1 || n == 3);
1496     if (v->field_mode) {
1497         a_valid = a_valid && !is_intra[xy - wrap];
1498         b_valid = b_valid && !is_intra[xy - wrap + off];
1499         c_valid = c_valid && !is_intra[xy - 1];
1500     }
1501
1502     if (a_valid) {
1503         a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1504         num_oppfield  += a_f;
1505         num_samefield += 1 - a_f;
1506         field_predA[0] = A[0];
1507         field_predA[1] = A[1];
1508     } else {
1509         field_predA[0] = field_predA[1] = 0;
1510         a_f = 0;
1511     }
1512     if (b_valid) {
1513         b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1514         num_oppfield  += b_f;
1515         num_samefield += 1 - b_f;
1516         field_predB[0] = B[0];
1517         field_predB[1] = B[1];
1518     } else {
1519         field_predB[0] = field_predB[1] = 0;
1520         b_f = 0;
1521     }
1522     if (c_valid) {
1523         c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1524         num_oppfield  += c_f;
1525         num_samefield += 1 - c_f;
1526         field_predC[0] = C[0];
1527         field_predC[1] = C[1];
1528     } else {
1529         field_predC[0] = field_predC[1] = 0;
1530         c_f = 0;
1531     }
1532
1533     if (v->field_mode) {
1534         if (!v->numref)
1535             // REFFIELD determines if the last field or the second-last field is
1536             // to be used as reference
1537             opposite = 1 - v->reffield;
1538         else {
1539             if (num_samefield <= num_oppfield)
1540                 opposite = 1 - pred_flag;
1541             else
1542                 opposite = pred_flag;
1543         }
1544     } else
1545         opposite = 0;
1546     if (opposite) {
1547         if (a_valid && !a_f) {
1548             field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1549             field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1550         }
1551         if (b_valid && !b_f) {
1552             field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1553             field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1554         }
1555         if (c_valid && !c_f) {
1556             field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1557             field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1558         }
1559         v->mv_f[dir][xy + v->blocks_off] = 1;
1560         v->ref_field_type[dir] = !v->cur_field_type;
1561     } else {
1562         if (a_valid && a_f) {
1563             field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1564             field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1565         }
1566         if (b_valid && b_f) {
1567             field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1568             field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1569         }
1570         if (c_valid && c_f) {
1571             field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1572             field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1573         }
1574         v->mv_f[dir][xy + v->blocks_off] = 0;
1575         v->ref_field_type[dir] = v->cur_field_type;
1576     }
1577
1578     if (a_valid) {
1579         px = field_predA[0];
1580         py = field_predA[1];
1581     } else if (c_valid) {
1582         px = field_predC[0];
1583         py = field_predC[1];
1584     } else if (b_valid) {
1585         px = field_predB[0];
1586         py = field_predB[1];
1587     } else {
1588         px = 0;
1589         py = 0;
1590     }
1591
1592     if (num_samefield + num_oppfield > 1) {
1593         px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1594         py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1595     }
1596
1597     /* Pullback MV as specified in 8.3.5.3.4 */
1598     if (!v->field_mode) {
1599         int qx, qy, X, Y;
1600         qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1601         qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1602         X  = (s->mb_width  << 6) - 4;
1603         Y  = (s->mb_height << 6) - 4;
1604         if (mv1) {
1605             if (qx + px < -60) px = -60 - qx;
1606             if (qy + py < -60) py = -60 - qy;
1607         } else {
1608             if (qx + px < -28) px = -28 - qx;
1609             if (qy + py < -28) py = -28 - qy;
1610         }
1611         if (qx + px > X) px = X - qx;
1612         if (qy + py > Y) py = Y - qy;
1613     }
1614
1615     if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1616         /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1617         hybridmv_thresh = 32;
1618         if (a_valid && c_valid) {
1619             if (is_intra[xy - wrap])
1620                 sum = FFABS(px) + FFABS(py);
1621             else
1622                 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1623             if (sum > hybridmv_thresh) {
1624                 if (get_bits1(&s->gb)) {     // read HYBRIDPRED bit
1625                     px = field_predA[0];
1626                     py = field_predA[1];
1627                 } else {
1628                     px = field_predC[0];
1629                     py = field_predC[1];
1630                 }
1631             } else {
1632                 if (is_intra[xy - 1])
1633                     sum = FFABS(px) + FFABS(py);
1634                 else
1635                     sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1636                 if (sum > hybridmv_thresh) {
1637                     if (get_bits1(&s->gb)) {
1638                         px = field_predA[0];
1639                         py = field_predA[1];
1640                     } else {
1641                         px = field_predC[0];
1642                         py = field_predC[1];
1643                     }
1644                 }
1645             }
1646         }
1647     }
1648
1649     if (v->field_mode && v->numref)
1650         r_y >>= 1;
1651     if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1652         y_bias = 1;
1653     /* store MV using signed modulus of MV range defined in 4.11 */
1654     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;
1655     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;
1656     if (mv1) { /* duplicate motion data for 1-MV block */
1657         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1658         s->current_picture.motion_val[dir][xy +    1 +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1659         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1660         s->current_picture.motion_val[dir][xy + wrap +     v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1661         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1662         s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1663         v->mv_f[dir][xy +    1 + v->blocks_off] = v->mv_f[dir][xy +            v->blocks_off];
1664         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];
1665     }
1666 }
1667
1668 /** Predict and set motion vector for interlaced frame picture MBs
1669  */
1670 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1671                                      int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1672 {
1673     MpegEncContext *s = &v->s;
1674     int xy, wrap, off = 0;
1675     int A[2], B[2], C[2];
1676     int px = 0, py = 0;
1677     int a_valid = 0, b_valid = 0, c_valid = 0;
1678     int field_a, field_b, field_c; // 0: same, 1: opposit
1679     int total_valid, num_samefield, num_oppfield;
1680     int pos_c, pos_b, n_adj;
1681
1682     wrap = s->b8_stride;
1683     xy = s->block_index[n];
1684
1685     if (s->mb_intra) {
1686         s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1687         s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1688         s->current_picture.motion_val[1][xy][0] = 0;
1689         s->current_picture.motion_val[1][xy][1] = 0;
1690         if (mvn == 1) { /* duplicate motion data for 1-MV block */
1691             s->current_picture.motion_val[0][xy + 1][0]        = 0;
1692             s->current_picture.motion_val[0][xy + 1][1]        = 0;
1693             s->current_picture.motion_val[0][xy + wrap][0]     = 0;
1694             s->current_picture.motion_val[0][xy + wrap][1]     = 0;
1695             s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1696             s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1697             v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1698             s->current_picture.motion_val[1][xy + 1][0]        = 0;
1699             s->current_picture.motion_val[1][xy + 1][1]        = 0;
1700             s->current_picture.motion_val[1][xy + wrap][0]     = 0;
1701             s->current_picture.motion_val[1][xy + wrap][1]     = 0;
1702             s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1703             s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1704         }
1705         return;
1706     }
1707
1708     off = ((n == 0) || (n == 1)) ? 1 : -1;
1709     /* predict A */
1710     if (s->mb_x || (n == 1) || (n == 3)) {
1711         if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1712             || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1713             A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1714             A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1715             a_valid = 1;
1716         } else { // current block has frame mv and cand. has field MV (so average)
1717             A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1718                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1719             A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1720                     + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1721             a_valid = 1;
1722         }
1723         if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1724             a_valid = 0;
1725             A[0] = A[1] = 0;
1726         }
1727     } else
1728         A[0] = A[1] = 0;
1729     /* Predict B and C */
1730     B[0] = B[1] = C[0] = C[1] = 0;
1731     if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1732         if (!s->first_slice_line) {
1733             if (!v->is_intra[s->mb_x - s->mb_stride]) {
1734                 b_valid = 1;
1735                 n_adj   = n | 2;
1736                 pos_b   = s->block_index[n_adj] - 2 * wrap;
1737                 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1738                     n_adj = (n & 2) | (n & 1);
1739                 }
1740                 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1741                 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1742                 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1743                     B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1744                     B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1745                 }
1746             }
1747             if (s->mb_width > 1) {
1748                 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1749                     c_valid = 1;
1750                     n_adj   = 2;
1751                     pos_c   = s->block_index[2] - 2 * wrap + 2;
1752                     if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1753                         n_adj = n & 2;
1754                     }
1755                     C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1756                     C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1757                     if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1758                         C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1759                         C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1760                     }
1761                     if (s->mb_x == s->mb_width - 1) {
1762                         if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1763                             c_valid = 1;
1764                             n_adj   = 3;
1765                             pos_c   = s->block_index[3] - 2 * wrap - 2;
1766                             if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1767                                 n_adj = n | 1;
1768                             }
1769                             C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1770                             C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1771                             if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1772                                 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1773                                 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1774                             }
1775                         } else
1776                             c_valid = 0;
1777                     }
1778                 }
1779             }
1780         }
1781     } else {
1782         pos_b   = s->block_index[1];
1783         b_valid = 1;
1784         B[0]    = s->current_picture.motion_val[dir][pos_b][0];
1785         B[1]    = s->current_picture.motion_val[dir][pos_b][1];
1786         pos_c   = s->block_index[0];
1787         c_valid = 1;
1788         C[0]    = s->current_picture.motion_val[dir][pos_c][0];
1789         C[1]    = s->current_picture.motion_val[dir][pos_c][1];
1790     }
1791
1792     total_valid = a_valid + b_valid + c_valid;
1793     // check if predictor A is out of bounds
1794     if (!s->mb_x && !(n == 1 || n == 3)) {
1795         A[0] = A[1] = 0;
1796     }
1797     // check if predictor B is out of bounds
1798     if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1799         B[0] = B[1] = C[0] = C[1] = 0;
1800     }
1801     if (!v->blk_mv_type[xy]) {
1802         if (s->mb_width == 1) {
1803             px = B[0];
1804             py = B[1];
1805         } else {
1806             if (total_valid >= 2) {
1807                 px = mid_pred(A[0], B[0], C[0]);
1808                 py = mid_pred(A[1], B[1], C[1]);
1809             } else if (total_valid) {
1810                 if      (a_valid) { px = A[0]; py = A[1]; }
1811                 else if (b_valid) { px = B[0]; py = B[1]; }
1812                 else              { px = C[0]; py = C[1]; }
1813             }
1814         }
1815     } else {
1816         if (a_valid)
1817             field_a = (A[1] & 4) ? 1 : 0;
1818         else
1819             field_a = 0;
1820         if (b_valid)
1821             field_b = (B[1] & 4) ? 1 : 0;
1822         else
1823             field_b = 0;
1824         if (c_valid)
1825             field_c = (C[1] & 4) ? 1 : 0;
1826         else
1827             field_c = 0;
1828
1829         num_oppfield  = field_a + field_b + field_c;
1830         num_samefield = total_valid - num_oppfield;
1831         if (total_valid == 3) {
1832             if ((num_samefield == 3) || (num_oppfield == 3)) {
1833                 px = mid_pred(A[0], B[0], C[0]);
1834                 py = mid_pred(A[1], B[1], C[1]);
1835             } else if (num_samefield >= num_oppfield) {
1836                 /* take one MV from same field set depending on priority
1837                 the check for B may not be necessary */
1838                 px = !field_a ? A[0] : B[0];
1839                 py = !field_a ? A[1] : B[1];
1840             } else {
1841                 px =  field_a ? A[0] : B[0];
1842                 py =  field_a ? A[1] : B[1];
1843             }
1844         } else if (total_valid == 2) {
1845             if (num_samefield >= num_oppfield) {
1846                 if (!field_a && a_valid) {
1847                     px = A[0];
1848                     py = A[1];
1849                 } else if (!field_b && b_valid) {
1850                     px = B[0];
1851                     py = B[1];
1852                 } else /*if (c_valid)*/ {
1853                     av_assert1(c_valid);
1854                     px = C[0];
1855                     py = C[1];
1856                 } /*else px = py = 0;*/
1857             } else {
1858                 if (field_a && a_valid) {
1859                     px = A[0];
1860                     py = A[1];
1861                 } else /*if (field_b && b_valid)*/ {
1862                     av_assert1(field_b && b_valid);
1863                     px = B[0];
1864                     py = B[1];
1865                 } /*else if (c_valid) {
1866                     px = C[0];
1867                     py = C[1];
1868                 }*/
1869             }
1870         } else if (total_valid == 1) {
1871             px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1872             py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1873         }
1874     }
1875
1876     /* store MV using signed modulus of MV range defined in 4.11 */
1877     s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1878     s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1879     if (mvn == 1) { /* duplicate motion data for 1-MV block */
1880         s->current_picture.motion_val[dir][xy +    1    ][0] = s->current_picture.motion_val[dir][xy][0];
1881         s->current_picture.motion_val[dir][xy +    1    ][1] = s->current_picture.motion_val[dir][xy][1];
1882         s->current_picture.motion_val[dir][xy + wrap    ][0] = s->current_picture.motion_val[dir][xy][0];
1883         s->current_picture.motion_val[dir][xy + wrap    ][1] = s->current_picture.motion_val[dir][xy][1];
1884         s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1885         s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1886     } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1887         s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1888         s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1889         s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1890         s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1891     }
1892 }
1893
1894 /** Motion compensation for direct or interpolated blocks in B-frames
1895  */
1896 static void vc1_interp_mc(VC1Context *v)
1897 {
1898     MpegEncContext *s = &v->s;
1899     H264ChromaContext *h264chroma = &v->h264chroma;
1900     uint8_t *srcY, *srcU, *srcV;
1901     int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1902     int off, off_uv;
1903     int v_edge_pos = s->v_edge_pos >> v->field_mode;
1904     int use_ic = v->next_use_ic;
1905
1906     if (!v->field_mode && !v->s.next_picture.f->data[0])
1907         return;
1908
1909     mx   = s->mv[1][0][0];
1910     my   = s->mv[1][0][1];
1911     uvmx = (mx + ((mx & 3) == 3)) >> 1;
1912     uvmy = (my + ((my & 3) == 3)) >> 1;
1913     if (v->field_mode) {
1914         if (v->cur_field_type != v->ref_field_type[1]) {
1915             my   = my   - 2 + 4 * v->cur_field_type;
1916             uvmy = uvmy - 2 + 4 * v->cur_field_type;
1917         }
1918     }
1919     if (v->fastuvmc) {
1920         uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1921         uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1922     }
1923     srcY = s->next_picture.f->data[0];
1924     srcU = s->next_picture.f->data[1];
1925     srcV = s->next_picture.f->data[2];
1926
1927     src_x   = s->mb_x * 16 + (mx   >> 2);
1928     src_y   = s->mb_y * 16 + (my   >> 2);
1929     uvsrc_x = s->mb_x *  8 + (uvmx >> 2);
1930     uvsrc_y = s->mb_y *  8 + (uvmy >> 2);
1931
1932     if (v->profile != PROFILE_ADVANCED) {
1933         src_x   = av_clip(  src_x, -16, s->mb_width  * 16);
1934         src_y   = av_clip(  src_y, -16, s->mb_height * 16);
1935         uvsrc_x = av_clip(uvsrc_x,  -8, s->mb_width  *  8);
1936         uvsrc_y = av_clip(uvsrc_y,  -8, s->mb_height *  8);
1937     } else {
1938         src_x   = av_clip(  src_x, -17, s->avctx->coded_width);
1939         src_y   = av_clip(  src_y, -18, s->avctx->coded_height + 1);
1940         uvsrc_x = av_clip(uvsrc_x,  -8, s->avctx->coded_width  >> 1);
1941         uvsrc_y = av_clip(uvsrc_y,  -8, s->avctx->coded_height >> 1);
1942     }
1943
1944     srcY += src_y   * s->linesize   + src_x;
1945     srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1946     srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1947
1948     if (v->field_mode && v->ref_field_type[1]) {
1949         srcY += s->current_picture_ptr->f->linesize[0];
1950         srcU += s->current_picture_ptr->f->linesize[1];
1951         srcV += s->current_picture_ptr->f->linesize[2];
1952     }
1953
1954     /* for grayscale we should not try to read from unknown area */
1955     if (s->flags & CODEC_FLAG_GRAY) {
1956         srcU = s->edge_emu_buffer + 18 * s->linesize;
1957         srcV = s->edge_emu_buffer + 18 * s->linesize;
1958     }
1959
1960     if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1961         || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1962         || (unsigned)(src_y - 1) > v_edge_pos    - (my & 3) - 16 - 3) {
1963         uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
1964         uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
1965
1966         srcY -= s->mspel * (1 + s->linesize);
1967         s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
1968                                  s->linesize, s->linesize,
1969                                  17 + s->mspel * 2, 17 + s->mspel * 2,
1970                                  src_x - s->mspel, src_y - s->mspel,
1971                                  s->h_edge_pos, v_edge_pos);
1972         srcY = s->edge_emu_buffer;
1973         s->vdsp.emulated_edge_mc(ubuf, srcU,
1974                                  s->uvlinesize, s->uvlinesize,
1975                                  8 + 1, 8 + 1,
1976                                  uvsrc_x, uvsrc_y,
1977                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
1978         s->vdsp.emulated_edge_mc(vbuf, srcV,
1979                                  s->uvlinesize, s->uvlinesize,
1980                                  8 + 1, 8 + 1,
1981                                  uvsrc_x, uvsrc_y,
1982                                  s->h_edge_pos >> 1, v_edge_pos >> 1);
1983         srcU = ubuf;
1984         srcV = vbuf;
1985         /* if we deal with range reduction we need to scale source blocks */
1986         if (v->rangeredfrm) {
1987             int i, j;
1988             uint8_t *src, *src2;
1989
1990             src = srcY;
1991             for (j = 0; j < 17 + s->mspel * 2; j++) {
1992                 for (i = 0; i < 17 + s->mspel * 2; i++)
1993                     src[i] = ((src[i] - 128) >> 1) + 128;
1994                 src += s->linesize;
1995             }
1996             src = srcU;
1997             src2 = srcV;
1998             for (j = 0; j < 9; j++) {
1999                 for (i = 0; i < 9; i++) {
2000                     src[i]  = ((src[i]  - 128) >> 1) + 128;
2001                     src2[i] = ((src2[i] - 128) >> 1) + 128;
2002                 }
2003                 src  += s->uvlinesize;
2004                 src2 += s->uvlinesize;
2005             }
2006         }
2007
2008         if (use_ic) {
2009             uint8_t (*luty )[256] = v->next_luty;
2010             uint8_t (*lutuv)[256] = v->next_lutuv;
2011             int i, j;
2012             uint8_t *src, *src2;
2013
2014             src = srcY;
2015             for (j = 0; j < 17 + s->mspel * 2; j++) {
2016                 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2017                 for (i = 0; i < 17 + s->mspel * 2; i++)
2018                     src[i] = luty[f][src[i]];
2019                 src += s->linesize;
2020             }
2021             src  = srcU;
2022             src2 = srcV;
2023             for (j = 0; j < 9; j++) {
2024                 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2025                 for (i = 0; i < 9; i++) {
2026                     src[i]  = lutuv[f][src[i]];
2027                     src2[i] = lutuv[f][src2[i]];
2028                 }
2029                 src  += s->uvlinesize;
2030                 src2 += s->uvlinesize;
2031             }
2032         }
2033         srcY += s->mspel * (1 + s->linesize);
2034     }
2035
2036     off    = 0;
2037     off_uv = 0;
2038
2039     if (s->mspel) {
2040         dxy = ((my & 3) << 2) | (mx & 3);
2041         v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0] + off    , srcY    , s->linesize, v->rnd);
2042     } else { // hpel mc
2043         dxy = (my & 2) | ((mx & 2) >> 1);
2044
2045         if (!v->rnd)
2046             s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2047         else
2048             s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2049     }
2050
2051     if (s->flags & CODEC_FLAG_GRAY) return;
2052     /* Chroma MC always uses qpel blilinear */
2053     uvmx = (uvmx & 3) << 1;
2054     uvmy = (uvmy & 3) << 1;
2055     if (!v->rnd) {
2056         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2057         h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2058     } else {
2059         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2060         v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2061     }
2062 }
2063
2064 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2065 {
2066     int n = bfrac;
2067
2068 #if B_FRACTION_DEN==256
2069     if (inv)
2070         n -= 256;
2071     if (!qs)
2072         return 2 * ((value * n + 255) >> 9);
2073     return (value * n + 128) >> 8;
2074 #else
2075     if (inv)
2076         n -= B_FRACTION_DEN;
2077     if (!qs)
2078         return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2079     return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2080 #endif
2081 }
2082
2083 /** Reconstruct motion vector for B-frame and do motion compensation
2084  */
2085 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2086                             int direct, int mode)
2087 {
2088     if (direct) {
2089         vc1_mc_1mv(v, 0);
2090         vc1_interp_mc(v);
2091         return;
2092     }
2093     if (mode == BMV_TYPE_INTERPOLATED) {
2094         vc1_mc_1mv(v, 0);
2095         vc1_interp_mc(v);
2096         return;
2097     }
2098
2099     vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2100 }
2101
2102 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2103                                  int direct, int mvtype)
2104 {
2105     MpegEncContext *s = &v->s;
2106     int xy, wrap, off = 0;
2107     int16_t *A, *B, *C;
2108     int px, py;
2109     int sum;
2110     int r_x, r_y;
2111     const uint8_t *is_intra = v->mb_type[0];
2112
2113     av_assert0(!v->field_mode);
2114
2115     r_x = v->range_x;
2116     r_y = v->range_y;
2117     /* scale MV difference to be quad-pel */
2118     dmv_x[0] <<= 1 - s->quarter_sample;
2119     dmv_y[0] <<= 1 - s->quarter_sample;
2120     dmv_x[1] <<= 1 - s->quarter_sample;
2121     dmv_y[1] <<= 1 - s->quarter_sample;
2122
2123     wrap = s->b8_stride;
2124     xy = s->block_index[0];
2125
2126     if (s->mb_intra) {
2127         s->current_picture.motion_val[0][xy][0] =
2128         s->current_picture.motion_val[0][xy][1] =
2129         s->current_picture.motion_val[1][xy][0] =
2130         s->current_picture.motion_val[1][xy][1] = 0;
2131         return;
2132     }
2133         if (direct && s->next_picture_ptr->field_picture)
2134             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2135
2136         s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2137         s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2138         s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2139         s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2140
2141         /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2142         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));
2143         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));
2144         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));
2145         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));
2146     if (direct) {
2147         s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2148         s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2149         s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2150         s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2151         return;
2152     }
2153
2154     if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2155         C   = s->current_picture.motion_val[0][xy - 2];
2156         A   = s->current_picture.motion_val[0][xy - wrap * 2];
2157         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2158         B   = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2159
2160         if (!s->mb_x) C[0] = C[1] = 0;
2161         if (!s->first_slice_line) { // predictor A is not out of bounds
2162             if (s->mb_width == 1) {
2163                 px = A[0];
2164                 py = A[1];
2165             } else {
2166                 px = mid_pred(A[0], B[0], C[0]);
2167                 py = mid_pred(A[1], B[1], C[1]);
2168             }
2169         } else if (s->mb_x) { // predictor C is not out of bounds
2170             px = C[0];
2171             py = C[1];
2172         } else {
2173             px = py = 0;
2174         }
2175         /* Pullback MV as specified in 8.3.5.3.4 */
2176         {
2177             int qx, qy, X, Y;
2178             if (v->profile < PROFILE_ADVANCED) {
2179                 qx = (s->mb_x << 5);
2180                 qy = (s->mb_y << 5);
2181                 X  = (s->mb_width  << 5) - 4;
2182                 Y  = (s->mb_height << 5) - 4;
2183                 if (qx + px < -28) px = -28 - qx;
2184                 if (qy + py < -28) py = -28 - qy;
2185                 if (qx + px > X) px = X - qx;
2186                 if (qy + py > Y) py = Y - qy;
2187             } else {
2188                 qx = (s->mb_x << 6);
2189                 qy = (s->mb_y << 6);
2190                 X  = (s->mb_width  << 6) - 4;
2191                 Y  = (s->mb_height << 6) - 4;
2192                 if (qx + px < -60) px = -60 - qx;
2193                 if (qy + py < -60) py = -60 - qy;
2194                 if (qx + px > X) px = X - qx;
2195                 if (qy + py > Y) py = Y - qy;
2196             }
2197         }
2198         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2199         if (0 && !s->first_slice_line && s->mb_x) {
2200             if (is_intra[xy - wrap])
2201                 sum = FFABS(px) + FFABS(py);
2202             else
2203                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2204             if (sum > 32) {
2205                 if (get_bits1(&s->gb)) {
2206                     px = A[0];
2207                     py = A[1];
2208                 } else {
2209                     px = C[0];
2210                     py = C[1];
2211                 }
2212             } else {
2213                 if (is_intra[xy - 2])
2214                     sum = FFABS(px) + FFABS(py);
2215                 else
2216                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2217                 if (sum > 32) {
2218                     if (get_bits1(&s->gb)) {
2219                         px = A[0];
2220                         py = A[1];
2221                     } else {
2222                         px = C[0];
2223                         py = C[1];
2224                     }
2225                 }
2226             }
2227         }
2228         /* store MV using signed modulus of MV range defined in 4.11 */
2229         s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2230         s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2231     }
2232     if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2233         C   = s->current_picture.motion_val[1][xy - 2];
2234         A   = s->current_picture.motion_val[1][xy - wrap * 2];
2235         off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2236         B   = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2237
2238         if (!s->mb_x)
2239             C[0] = C[1] = 0;
2240         if (!s->first_slice_line) { // predictor A is not out of bounds
2241             if (s->mb_width == 1) {
2242                 px = A[0];
2243                 py = A[1];
2244             } else {
2245                 px = mid_pred(A[0], B[0], C[0]);
2246                 py = mid_pred(A[1], B[1], C[1]);
2247             }
2248         } else if (s->mb_x) { // predictor C is not out of bounds
2249             px = C[0];
2250             py = C[1];
2251         } else {
2252             px = py = 0;
2253         }
2254         /* Pullback MV as specified in 8.3.5.3.4 */
2255         {
2256             int qx, qy, X, Y;
2257             if (v->profile < PROFILE_ADVANCED) {
2258                 qx = (s->mb_x << 5);
2259                 qy = (s->mb_y << 5);
2260                 X  = (s->mb_width  << 5) - 4;
2261                 Y  = (s->mb_height << 5) - 4;
2262                 if (qx + px < -28) px = -28 - qx;
2263                 if (qy + py < -28) py = -28 - qy;
2264                 if (qx + px > X) px = X - qx;
2265                 if (qy + py > Y) py = Y - qy;
2266             } else {
2267                 qx = (s->mb_x << 6);
2268                 qy = (s->mb_y << 6);
2269                 X  = (s->mb_width  << 6) - 4;
2270                 Y  = (s->mb_height << 6) - 4;
2271                 if (qx + px < -60) px = -60 - qx;
2272                 if (qy + py < -60) py = -60 - qy;
2273                 if (qx + px > X) px = X - qx;
2274                 if (qy + py > Y) py = Y - qy;
2275             }
2276         }
2277         /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2278         if (0 && !s->first_slice_line && s->mb_x) {
2279             if (is_intra[xy - wrap])
2280                 sum = FFABS(px) + FFABS(py);
2281             else
2282                 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2283             if (sum > 32) {
2284                 if (get_bits1(&s->gb)) {
2285                     px = A[0];
2286                     py = A[1];
2287                 } else {
2288                     px = C[0];
2289                     py = C[1];
2290                 }
2291             } else {
2292                 if (is_intra[xy - 2])
2293                     sum = FFABS(px) + FFABS(py);
2294                 else
2295                     sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2296                 if (sum > 32) {
2297                     if (get_bits1(&s->gb)) {
2298                         px = A[0];
2299                         py = A[1];
2300                     } else {
2301                         px = C[0];
2302                         py = C[1];
2303                     }
2304                 }
2305             }
2306         }
2307         /* store MV using signed modulus of MV range defined in 4.11 */
2308
2309         s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2310         s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2311     }
2312     s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2313     s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2314     s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2315     s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2316 }
2317
2318 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2319 {
2320     int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2321     MpegEncContext *s = &v->s;
2322     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2323
2324     if (v->bmvtype == BMV_TYPE_DIRECT) {
2325         int total_opp, k, f;
2326         if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2327             s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2328                                       v->bfraction, 0, s->quarter_sample);
2329             s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2330                                       v->bfraction, 0, s->quarter_sample);
2331             s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2332                                       v->bfraction, 1, s->quarter_sample);
2333             s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2334                                       v->bfraction, 1, s->quarter_sample);
2335
2336             total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2337                       + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2338                       + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2339                       + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2340             f = (total_opp > 2) ? 1 : 0;
2341         } else {
2342             s->mv[0][0][0] = s->mv[0][0][1] = 0;
2343             s->mv[1][0][0] = s->mv[1][0][1] = 0;
2344             f = 0;
2345         }
2346         v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2347         for (k = 0; k < 4; k++) {
2348             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2349             s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2350             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2351             s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2352             v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2353             v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2354         }
2355         return;
2356     }
2357     if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2358         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);
2359         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);
2360         return;
2361     }
2362     if (dir) { // backward
2363         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);
2364         if (n == 3 || mv1) {
2365             vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0],   1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2366         }
2367     } else { // forward
2368         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);
2369         if (n == 3 || mv1) {
2370             vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1],   1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2371         }
2372     }
2373 }
2374
2375 /** Get predicted DC value for I-frames only
2376  * prediction dir: left=0, top=1
2377  * @param s MpegEncContext
2378  * @param overlap flag indicating that overlap filtering is used
2379  * @param pq integer part of picture quantizer
2380  * @param[in] n block index in the current MB
2381  * @param dc_val_ptr Pointer to DC predictor
2382  * @param dir_ptr Prediction direction for use in AC prediction
2383  */
2384 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2385                                 int16_t **dc_val_ptr, int *dir_ptr)
2386 {
2387     int a, b, c, wrap, pred, scale;
2388     int16_t *dc_val;
2389     static const uint16_t dcpred[32] = {
2390         -1, 1024,  512,  341,  256,  205,  171,  146,  128,
2391              114,  102,   93,   85,   79,   73,   68,   64,
2392               60,   57,   54,   51,   49,   47,   45,   43,
2393               41,   39,   38,   37,   35,   34,   33
2394     };
2395
2396     /* find prediction - wmv3_dc_scale always used here in fact */
2397     if (n < 4) scale = s->y_dc_scale;
2398     else       scale = s->c_dc_scale;
2399
2400     wrap   = s->block_wrap[n];
2401     dc_val = s->dc_val[0] + s->block_index[n];
2402
2403     /* B A
2404      * C X
2405      */
2406     c = dc_val[ - 1];
2407     b = dc_val[ - 1 - wrap];
2408     a = dc_val[ - wrap];
2409
2410     if (pq < 9 || !overlap) {
2411         /* Set outer values */
2412         if (s->first_slice_line && (n != 2 && n != 3))
2413             b = a = dcpred[scale];
2414         if (s->mb_x == 0 && (n != 1 && n != 3))
2415             b = c = dcpred[scale];
2416     } else {
2417         /* Set outer values */
2418         if (s->first_slice_line && (n != 2 && n != 3))
2419             b = a = 0;
2420         if (s->mb_x == 0 && (n != 1 && n != 3))
2421             b = c = 0;
2422     }
2423
2424     if (abs(a - b) <= abs(b - c)) {
2425         pred     = c;
2426         *dir_ptr = 1; // left
2427     } else {
2428         pred     = a;
2429         *dir_ptr = 0; // top
2430     }
2431
2432     /* update predictor */
2433     *dc_val_ptr = &dc_val[0];
2434     return pred;
2435 }
2436
2437
2438 /** Get predicted DC value
2439  * prediction dir: left=0, top=1
2440  * @param s MpegEncContext
2441  * @param overlap flag indicating that overlap filtering is used
2442  * @param pq integer part of picture quantizer
2443  * @param[in] n block index in the current MB
2444  * @param a_avail flag indicating top block availability
2445  * @param c_avail flag indicating left block availability
2446  * @param dc_val_ptr Pointer to DC predictor
2447  * @param dir_ptr Prediction direction for use in AC prediction
2448  */
2449 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2450                               int a_avail, int c_avail,
2451                               int16_t **dc_val_ptr, int *dir_ptr)
2452 {
2453     int a, b, c, wrap, pred;
2454     int16_t *dc_val;
2455     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2456     int q1, q2 = 0;
2457     int dqscale_index;
2458
2459     wrap = s->block_wrap[n];
2460     dc_val = s->dc_val[0] + s->block_index[n];
2461
2462     /* B A
2463      * C X
2464      */
2465     c = dc_val[ - 1];
2466     b = dc_val[ - 1 - wrap];
2467     a = dc_val[ - wrap];
2468     /* scale predictors if needed */
2469     q1 = s->current_picture.qscale_table[mb_pos];
2470     dqscale_index = s->y_dc_scale_table[q1] - 1;
2471     if (dqscale_index < 0)
2472         return 0;
2473     if (c_avail && (n != 1 && n != 3)) {
2474         q2 = s->current_picture.qscale_table[mb_pos - 1];
2475         if (q2 && q2 != q1)
2476             c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2477     }
2478     if (a_avail && (n != 2 && n != 3)) {
2479         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2480         if (q2 && q2 != q1)
2481             a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2482     }
2483     if (a_avail && c_avail && (n != 3)) {
2484         int off = mb_pos;
2485         if (n != 1)
2486             off--;
2487         if (n != 2)
2488             off -= s->mb_stride;
2489         q2 = s->current_picture.qscale_table[off];
2490         if (q2 && q2 != q1)
2491             b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2492     }
2493
2494     if (a_avail && c_avail) {
2495         if (abs(a - b) <= abs(b - c)) {
2496             pred     = c;
2497             *dir_ptr = 1; // left
2498         } else {
2499             pred     = a;
2500             *dir_ptr = 0; // top
2501         }
2502     } else if (a_avail) {
2503         pred     = a;
2504         *dir_ptr = 0; // top
2505     } else if (c_avail) {
2506         pred     = c;
2507         *dir_ptr = 1; // left
2508     } else {
2509         pred     = 0;
2510         *dir_ptr = 1; // left
2511     }
2512
2513     /* update predictor */
2514     *dc_val_ptr = &dc_val[0];
2515     return pred;
2516 }
2517
2518 /** @} */ // Block group
2519
2520 /**
2521  * @name VC1 Macroblock-level functions in Simple/Main Profiles
2522  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2523  * @{
2524  */
2525
2526 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2527                                        uint8_t **coded_block_ptr)
2528 {
2529     int xy, wrap, pred, a, b, c;
2530
2531     xy   = s->block_index[n];
2532     wrap = s->b8_stride;
2533
2534     /* B C
2535      * A X
2536      */
2537     a = s->coded_block[xy - 1       ];
2538     b = s->coded_block[xy - 1 - wrap];
2539     c = s->coded_block[xy     - wrap];
2540
2541     if (b == c) {
2542         pred = a;
2543     } else {
2544         pred = c;
2545     }
2546
2547     /* store value */
2548     *coded_block_ptr = &s->coded_block[xy];
2549
2550     return pred;
2551 }
2552
2553 /**
2554  * Decode one AC coefficient
2555  * @param v The VC1 context
2556  * @param last Last coefficient
2557  * @param skip How much zero coefficients to skip
2558  * @param value Decoded AC coefficient value
2559  * @param codingset set of VLC to decode data
2560  * @see 8.1.3.4
2561  */
2562 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2563                                 int *value, int codingset)
2564 {
2565     GetBitContext *gb = &v->s.gb;
2566     int index, escape, run = 0, level = 0, lst = 0;
2567
2568     index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2569     if (index != ff_vc1_ac_sizes[codingset] - 1) {
2570         run   = vc1_index_decode_table[codingset][index][0];
2571         level = vc1_index_decode_table[codingset][index][1];
2572         lst   = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2573         if (get_bits1(gb))
2574             level = -level;
2575     } else {
2576         escape = decode210(gb);
2577         if (escape != 2) {
2578             index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2579             run   = vc1_index_decode_table[codingset][index][0];
2580             level = vc1_index_decode_table[codingset][index][1];
2581             lst   = index >= vc1_last_decode_table[codingset];
2582             if (escape == 0) {
2583                 if (lst)
2584                     level += vc1_last_delta_level_table[codingset][run];
2585                 else
2586                     level += vc1_delta_level_table[codingset][run];
2587             } else {
2588                 if (lst)
2589                     run += vc1_last_delta_run_table[codingset][level] + 1;
2590                 else
2591                     run += vc1_delta_run_table[codingset][level] + 1;
2592             }
2593             if (get_bits1(gb))
2594                 level = -level;
2595         } else {
2596             int sign;
2597             lst = get_bits1(gb);
2598             if (v->s.esc3_level_length == 0) {
2599                 if (v->pq < 8 || v->dquantfrm) { // table 59
2600                     v->s.esc3_level_length = get_bits(gb, 3);
2601                     if (!v->s.esc3_level_length)
2602                         v->s.esc3_level_length = get_bits(gb, 2) + 8;
2603                 } else { // table 60
2604                     v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2605                 }
2606                 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2607             }
2608             run   = get_bits(gb, v->s.esc3_run_length);
2609             sign  = get_bits1(gb);
2610             level = get_bits(gb, v->s.esc3_level_length);
2611             if (sign)
2612                 level = -level;
2613         }
2614     }
2615
2616     *last  = lst;
2617     *skip  = run;
2618     *value = level;
2619 }
2620
2621 /** Decode intra block in intra frames - should be faster than decode_intra_block
2622  * @param v VC1Context
2623  * @param block block to decode
2624  * @param[in] n subblock index
2625  * @param coded are AC coeffs present or not
2626  * @param codingset set of VLC to decode data
2627  */
2628 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2629                               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         if (dcdiff == 119 /* ESC index value */) {
2651             /* TODO: Optimize */
2652             if (v->pq == 1)      dcdiff = get_bits(gb, 10);
2653             else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2654             else                 dcdiff = get_bits(gb, 8);
2655         } else {
2656             if (v->pq == 1)
2657                 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2658             else if (v->pq == 2)
2659                 dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
2660         }
2661         if (get_bits1(gb))
2662             dcdiff = -dcdiff;
2663     }
2664
2665     /* Prediction */
2666     dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2667     *dc_val = dcdiff;
2668
2669     /* Store the quantized DC coeff, used for prediction */
2670     if (n < 4) {
2671         block[0] = dcdiff * s->y_dc_scale;
2672     } else {
2673         block[0] = dcdiff * s->c_dc_scale;
2674     }
2675     /* Skip ? */
2676     if (!coded) {
2677         goto not_coded;
2678     }
2679
2680     // AC Decoding
2681     i = 1;
2682
2683     {
2684         int last = 0, skip, value;
2685         const uint8_t *zz_table;
2686         int scale;
2687         int k;
2688
2689         scale = v->pq * 2 + v->halfpq;
2690
2691         if (v->s.ac_pred) {
2692             if (!dc_pred_dir)
2693                 zz_table = v->zz_8x8[2];
2694             else
2695                 zz_table = v->zz_8x8[3];
2696         } else
2697             zz_table = v->zz_8x8[1];
2698
2699         ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2700         ac_val2 = ac_val;
2701         if (dc_pred_dir) // left
2702             ac_val -= 16;
2703         else // top
2704             ac_val -= 16 * s->block_wrap[n];
2705
2706         while (!last) {
2707             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2708             i += skip;
2709             if (i > 63)
2710                 break;
2711             block[zz_table[i++]] = value;
2712         }
2713
2714         /* apply AC prediction if needed */
2715         if (s->ac_pred) {
2716             if (dc_pred_dir) { // left
2717                 for (k = 1; k < 8; k++)
2718                     block[k << v->left_blk_sh] += ac_val[k];
2719             } else { // top
2720                 for (k = 1; k < 8; k++)
2721                     block[k << v->top_blk_sh] += ac_val[k + 8];
2722             }
2723         }
2724         /* save AC coeffs for further prediction */
2725         for (k = 1; k < 8; k++) {
2726             ac_val2[k]     = block[k << v->left_blk_sh];
2727             ac_val2[k + 8] = block[k << v->top_blk_sh];
2728         }
2729
2730         /* scale AC coeffs */
2731         for (k = 1; k < 64; k++)
2732             if (block[k]) {
2733                 block[k] *= scale;
2734                 if (!v->pquantizer)
2735                     block[k] += (block[k] < 0) ? -v->pq : v->pq;
2736             }
2737
2738         if (s->ac_pred) i = 63;
2739     }
2740
2741 not_coded:
2742     if (!coded) {
2743         int k, scale;
2744         ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2745         ac_val2 = ac_val;
2746
2747         i = 0;
2748         scale = v->pq * 2 + v->halfpq;
2749         memset(ac_val2, 0, 16 * 2);
2750         if (dc_pred_dir) { // left
2751             ac_val -= 16;
2752             if (s->ac_pred)
2753                 memcpy(ac_val2, ac_val, 8 * 2);
2754         } else { // top
2755             ac_val -= 16 * s->block_wrap[n];
2756             if (s->ac_pred)
2757                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2758         }
2759
2760         /* apply AC prediction if needed */
2761         if (s->ac_pred) {
2762             if (dc_pred_dir) { //left
2763                 for (k = 1; k < 8; k++) {
2764                     block[k << v->left_blk_sh] = ac_val[k] * scale;
2765                     if (!v->pquantizer && block[k << v->left_blk_sh])
2766                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2767                 }
2768             } else { // top
2769                 for (k = 1; k < 8; k++) {
2770                     block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2771                     if (!v->pquantizer && block[k << v->top_blk_sh])
2772                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2773                 }
2774             }
2775             i = 63;
2776         }
2777     }
2778     s->block_last_index[n] = i;
2779
2780     return 0;
2781 }
2782
2783 /** Decode intra block in intra frames - should be faster than decode_intra_block
2784  * @param v VC1Context
2785  * @param block block to decode
2786  * @param[in] n subblock number
2787  * @param coded are AC coeffs present or not
2788  * @param codingset set of VLC to decode data
2789  * @param mquant quantizer value for this macroblock
2790  */
2791 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2792                                   int coded, int codingset, int mquant)
2793 {
2794     GetBitContext *gb = &v->s.gb;
2795     MpegEncContext *s = &v->s;
2796     int dc_pred_dir = 0; /* Direction of the DC prediction used */
2797     int i;
2798     int16_t *dc_val = NULL;
2799     int16_t *ac_val, *ac_val2;
2800     int dcdiff;
2801     int a_avail = v->a_avail, c_avail = v->c_avail;
2802     int use_pred = s->ac_pred;
2803     int scale;
2804     int q1, q2 = 0;
2805     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2806
2807     /* Get DC differential */
2808     if (n < 4) {
2809         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2810     } else {
2811         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2812     }
2813     if (dcdiff < 0) {
2814         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2815         return -1;
2816     }
2817     if (dcdiff) {
2818         if (dcdiff == 119 /* ESC index value */) {
2819             /* TODO: Optimize */
2820             if (mquant == 1)      dcdiff = get_bits(gb, 10);
2821             else if (mquant == 2) dcdiff = get_bits(gb, 9);
2822             else                  dcdiff = get_bits(gb, 8);
2823         } else {
2824             if (mquant == 1)
2825                 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2826             else if (mquant == 2)
2827                 dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
2828         }
2829         if (get_bits1(gb))
2830             dcdiff = -dcdiff;
2831     }
2832
2833     /* Prediction */
2834     dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2835     *dc_val = dcdiff;
2836
2837     /* Store the quantized DC coeff, used for prediction */
2838     if (n < 4) {
2839         block[0] = dcdiff * s->y_dc_scale;
2840     } else {
2841         block[0] = dcdiff * s->c_dc_scale;
2842     }
2843
2844     //AC Decoding
2845     i = 1;
2846
2847     /* check if AC is needed at all */
2848     if (!a_avail && !c_avail)
2849         use_pred = 0;
2850     ac_val  = s->ac_val[0][0] + s->block_index[n] * 16;
2851     ac_val2 = ac_val;
2852
2853     scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2854
2855     if (dc_pred_dir) // left
2856         ac_val -= 16;
2857     else // top
2858         ac_val -= 16 * s->block_wrap[n];
2859
2860     q1 = s->current_picture.qscale_table[mb_pos];
2861     if ( dc_pred_dir && c_avail && mb_pos)
2862         q2 = s->current_picture.qscale_table[mb_pos - 1];
2863     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2864         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2865     if ( dc_pred_dir && n == 1)
2866         q2 = q1;
2867     if (!dc_pred_dir && n == 2)
2868         q2 = q1;
2869     if (n == 3)
2870         q2 = q1;
2871
2872     if (coded) {
2873         int last = 0, skip, value;
2874         const uint8_t *zz_table;
2875         int k;
2876
2877         if (v->s.ac_pred) {
2878             if (!use_pred && v->fcm == ILACE_FRAME) {
2879                 zz_table = v->zzi_8x8;
2880             } else {
2881                 if (!dc_pred_dir) // top
2882                     zz_table = v->zz_8x8[2];
2883                 else // left
2884                     zz_table = v->zz_8x8[3];
2885             }
2886         } else {
2887             if (v->fcm != ILACE_FRAME)
2888                 zz_table = v->zz_8x8[1];
2889             else
2890                 zz_table = v->zzi_8x8;
2891         }
2892
2893         while (!last) {
2894             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2895             i += skip;
2896             if (i > 63)
2897                 break;
2898             block[zz_table[i++]] = value;
2899         }
2900
2901         /* apply AC prediction if needed */
2902         if (use_pred) {
2903             /* scale predictors if needed*/
2904             if (q2 && q1 != q2) {
2905                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2906                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2907
2908                 if (q1 < 1)
2909                     return AVERROR_INVALIDDATA;
2910                 if (dc_pred_dir) { // left
2911                     for (k = 1; k < 8; k++)
2912                         block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2913                 } else { // top
2914                     for (k = 1; k < 8; k++)
2915                         block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2916                 }
2917             } else {
2918                 if (dc_pred_dir) { //left
2919                     for (k = 1; k < 8; k++)
2920                         block[k << v->left_blk_sh] += ac_val[k];
2921                 } else { //top
2922                     for (k = 1; k < 8; k++)
2923                         block[k << v->top_blk_sh] += ac_val[k + 8];
2924                 }
2925             }
2926         }
2927         /* save AC coeffs for further prediction */
2928         for (k = 1; k < 8; k++) {
2929             ac_val2[k    ] = block[k << v->left_blk_sh];
2930             ac_val2[k + 8] = block[k << v->top_blk_sh];
2931         }
2932
2933         /* scale AC coeffs */
2934         for (k = 1; k < 64; k++)
2935             if (block[k]) {
2936                 block[k] *= scale;
2937                 if (!v->pquantizer)
2938                     block[k] += (block[k] < 0) ? -mquant : mquant;
2939             }
2940
2941         if (use_pred) i = 63;
2942     } else { // no AC coeffs
2943         int k;
2944
2945         memset(ac_val2, 0, 16 * 2);
2946         if (dc_pred_dir) { // left
2947             if (use_pred) {
2948                 memcpy(ac_val2, ac_val, 8 * 2);
2949                 if (q2 && q1 != q2) {
2950                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2951                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2952                     if (q1 < 1)
2953                         return AVERROR_INVALIDDATA;
2954                     for (k = 1; k < 8; k++)
2955                         ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2956                 }
2957             }
2958         } else { // top
2959             if (use_pred) {
2960                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2961                 if (q2 && q1 != q2) {
2962                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2963                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2964                     if (q1 < 1)
2965                         return AVERROR_INVALIDDATA;
2966                     for (k = 1; k < 8; k++)
2967                         ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2968                 }
2969             }
2970         }
2971
2972         /* apply AC prediction if needed */
2973         if (use_pred) {
2974             if (dc_pred_dir) { // left
2975                 for (k = 1; k < 8; k++) {
2976                     block[k << v->left_blk_sh] = ac_val2[k] * scale;
2977                     if (!v->pquantizer && block[k << v->left_blk_sh])
2978                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2979                 }
2980             } else { // top
2981                 for (k = 1; k < 8; k++) {
2982                     block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2983                     if (!v->pquantizer && block[k << v->top_blk_sh])
2984                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2985                 }
2986             }
2987             i = 63;
2988         }
2989     }
2990     s->block_last_index[n] = i;
2991
2992     return 0;
2993 }
2994
2995 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2996  * @param v VC1Context
2997  * @param block block to decode
2998  * @param[in] n subblock index
2999  * @param coded are AC coeffs present or not
3000  * @param mquant block quantizer
3001  * @param codingset set of VLC to decode data
3002  */
3003 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
3004                                   int coded, int mquant, int codingset)
3005 {
3006     GetBitContext *gb = &v->s.gb;
3007     MpegEncContext *s = &v->s;
3008     int dc_pred_dir = 0; /* Direction of the DC prediction used */
3009     int i;
3010     int16_t *dc_val = NULL;
3011     int16_t *ac_val, *ac_val2;
3012     int dcdiff;
3013     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3014     int a_avail = v->a_avail, c_avail = v->c_avail;
3015     int use_pred = s->ac_pred;
3016     int scale;
3017     int q1, q2 = 0;
3018
3019     s->bdsp.clear_block(block);
3020
3021     /* XXX: Guard against dumb values of mquant */
3022     mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3023
3024     /* Set DC scale - y and c use the same */
3025     s->y_dc_scale = s->y_dc_scale_table[mquant];
3026     s->c_dc_scale = s->c_dc_scale_table[mquant];
3027
3028     /* Get DC differential */
3029     if (n < 4) {
3030         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3031     } else {
3032         dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3033     }
3034     if (dcdiff < 0) {
3035         av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3036         return -1;
3037     }
3038     if (dcdiff) {
3039         if (dcdiff == 119 /* ESC index value */) {
3040             /* TODO: Optimize */
3041             if (mquant == 1)      dcdiff = get_bits(gb, 10);
3042             else if (mquant == 2) dcdiff = get_bits(gb, 9);
3043             else                  dcdiff = get_bits(gb, 8);
3044         } else {
3045             if (mquant == 1)
3046                 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3047             else if (mquant == 2)
3048                 dcdiff = (dcdiff << 1) + get_bits1(gb)   - 1;
3049         }
3050         if (get_bits1(gb))
3051             dcdiff = -dcdiff;
3052     }
3053
3054     /* Prediction */
3055     dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3056     *dc_val = dcdiff;
3057
3058     /* Store the quantized DC coeff, used for prediction */
3059
3060     if (n < 4) {
3061         block[0] = dcdiff * s->y_dc_scale;
3062     } else {
3063         block[0] = dcdiff * s->c_dc_scale;
3064     }
3065
3066     //AC Decoding
3067     i = 1;
3068
3069     /* check if AC is needed at all and adjust direction if needed */
3070     if (!a_avail) dc_pred_dir = 1;
3071     if (!c_avail) dc_pred_dir = 0;
3072     if (!a_avail && !c_avail) use_pred = 0;
3073     ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3074     ac_val2 = ac_val;
3075
3076     scale = mquant * 2 + v->halfpq;
3077
3078     if (dc_pred_dir) //left
3079         ac_val -= 16;
3080     else //top
3081         ac_val -= 16 * s->block_wrap[n];
3082
3083     q1 = s->current_picture.qscale_table[mb_pos];
3084     if (dc_pred_dir && c_avail && mb_pos)
3085         q2 = s->current_picture.qscale_table[mb_pos - 1];
3086     if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3087         q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3088     if ( dc_pred_dir && n == 1)
3089         q2 = q1;
3090     if (!dc_pred_dir && n == 2)
3091         q2 = q1;
3092     if (n == 3) q2 = q1;
3093
3094     if (coded) {
3095         int last = 0, skip, value;
3096         int k;
3097
3098         while (!last) {
3099             vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3100             i += skip;
3101             if (i > 63)
3102                 break;
3103             if (v->fcm == PROGRESSIVE)
3104                 block[v->zz_8x8[0][i++]] = value;
3105             else {
3106                 if (use_pred && (v->fcm == ILACE_FRAME)) {
3107                     if (!dc_pred_dir) // top
3108                         block[v->zz_8x8[2][i++]] = value;
3109                     else // left
3110                         block[v->zz_8x8[3][i++]] = value;
3111                 } else {
3112                     block[v->zzi_8x8[i++]] = value;
3113                 }
3114             }
3115         }
3116
3117         /* apply AC prediction if needed */
3118         if (use_pred) {
3119             /* scale predictors if needed*/
3120             if (q2 && q1 != q2) {
3121                 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3122                 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3123
3124                 if (q1 < 1)
3125                     return AVERROR_INVALIDDATA;
3126                 if (dc_pred_dir) { // left
3127                     for (k = 1; k < 8; k++)
3128                         block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3129                 } else { //top
3130                     for (k = 1; k < 8; k++)
3131                         block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3132                 }
3133             } else {
3134                 if (dc_pred_dir) { // left
3135                     for (k = 1; k < 8; k++)
3136                         block[k << v->left_blk_sh] += ac_val[k];
3137                 } else { // top
3138                     for (k = 1; k < 8; k++)
3139                         block[k << v->top_blk_sh] += ac_val[k + 8];
3140                 }
3141             }
3142         }
3143         /* save AC coeffs for further prediction */
3144         for (k = 1; k < 8; k++) {
3145             ac_val2[k    ] = block[k << v->left_blk_sh];
3146             ac_val2[k + 8] = block[k << v->top_blk_sh];
3147         }
3148
3149         /* scale AC coeffs */
3150         for (k = 1; k < 64; k++)
3151             if (block[k]) {
3152                 block[k] *= scale;
3153                 if (!v->pquantizer)
3154                     block[k] += (block[k] < 0) ? -mquant : mquant;
3155             }
3156
3157         if (use_pred) i = 63;
3158     } else { // no AC coeffs
3159         int k;
3160
3161         memset(ac_val2, 0, 16 * 2);
3162         if (dc_pred_dir) { // left
3163             if (use_pred) {
3164                 memcpy(ac_val2, ac_val, 8 * 2);
3165                 if (q2 && q1 != q2) {
3166                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3167                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3168                     if (q1 < 1)
3169                         return AVERROR_INVALIDDATA;
3170                     for (k = 1; k < 8; k++)
3171                         ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3172                 }
3173             }
3174         } else { // top
3175             if (use_pred) {
3176                 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3177                 if (q2 && q1 != q2) {
3178                     q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3179                     q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3180                     if (q1 < 1)
3181                         return AVERROR_INVALIDDATA;
3182                     for (k = 1; k < 8; k++)
3183                         ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3184                 }
3185             }
3186         }
3187
3188         /* apply AC prediction if needed */
3189         if (use_pred) {
3190             if (dc_pred_dir) { // left
3191                 for (k = 1; k < 8; k++) {
3192                     block[k << v->left_blk_sh] = ac_val2[k] * scale;
3193                     if (!v->pquantizer && block[k << v->left_blk_sh])
3194                         block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3195                 }
3196             } else { // top
3197                 for (k = 1; k < 8; k++) {
3198                     block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3199                     if (!v->pquantizer && block[k << v->top_blk_sh])
3200                         block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3201                 }
3202             }
3203             i = 63;
3204         }
3205     }
3206     s->block_last_index[n] = i;
3207
3208     return 0;
3209 }
3210
3211 /** Decode P block
3212  */
3213 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3214                               int mquant, int ttmb, int first_block,
3215                               uint8_t *dst, int linesize, int skip_block,
3216                               int *ttmb_out)
3217 {
3218     MpegEncContext *s = &v->s;
3219     GetBitContext *gb = &s->gb;
3220     int i, j;
3221     int subblkpat = 0;
3222     int scale, off, idx, last, skip, value;
3223     int ttblk = ttmb & 7;
3224     int pat = 0;
3225
3226     s->bdsp.clear_block(block);
3227
3228     if (ttmb == -1) {
3229         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)];
3230     }
3231     if (ttblk == TT_4X4) {
3232         subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3233     }
3234     if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3235         && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3236             || (!v->res_rtm_flag && !first_block))) {
3237         subblkpat = decode012(gb);
3238         if (subblkpat)
3239             subblkpat ^= 3; // swap decoded pattern bits
3240         if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3241             ttblk = TT_8X4;
3242         if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3243             ttblk = TT_4X8;
3244     }
3245     scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3246
3247     // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3248     if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3249         subblkpat = 2 - (ttblk == TT_8X4_TOP);
3250         ttblk     = TT_8X4;
3251     }
3252     if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3253         subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3254         ttblk     = TT_4X8;
3255     }
3256     switch (ttblk) {
3257     case TT_8X8:
3258         pat  = 0xF;
3259         i    = 0;
3260         last = 0;
3261         while (!last) {
3262             vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3263             i += skip;
3264             if (i > 63)
3265                 break;
3266             if (!v->fcm)
3267                 idx = v->zz_8x8[0][i++];
3268             else
3269                 idx = v->zzi_8x8[i++];
3270             block[idx] = value * scale;
3271             if (!v->pquantizer)
3272                 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3273         }
3274         if (!skip_block) {
3275             if (i == 1)
3276                 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3277             else {
3278                 v->vc1dsp.vc1_inv_trans_8x8(block);
3279                 s->dsp.add_pixels_clamped(block, dst, linesize);
3280             }
3281         }
3282         break;
3283     case TT_4X4:
3284         pat = ~subblkpat & 0xF;
3285         for (j = 0; j < 4; j++) {
3286             last = subblkpat & (1 << (3 - j));
3287             i    = 0;
3288             off  = (j & 1) * 4 + (j & 2) * 16;
3289             while (!last) {
3290                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3291                 i += skip;
3292                 if (i > 15)
3293                     break;
3294                 if (!v->fcm)
3295                     idx = ff_vc1_simple_progressive_4x4_zz[i++];
3296                 else
3297                     idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3298                 block[idx + off] = value * scale;
3299                 if (!v->pquantizer)
3300                     block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3301             }
3302             if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3303                 if (i == 1)
3304                     v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3305                 else
3306                     v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) *  2 * linesize, linesize, block + off);
3307             }
3308         }
3309         break;
3310     case TT_8X4:
3311         pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3312         for (j = 0; j < 2; j++) {
3313             last = subblkpat & (1 << (1 - j));
3314             i    = 0;
3315             off  = j * 32;
3316             while (!last) {
3317                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3318                 i += skip;
3319                 if (i > 31)
3320                     break;
3321                 if (!v->fcm)
3322                     idx = v->zz_8x4[i++] + off;
3323                 else
3324                     idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3325                 block[idx] = value * scale;
3326                 if (!v->pquantizer)
3327                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
3328             }
3329             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3330                 if (i == 1)
3331                     v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3332                 else
3333                     v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3334             }
3335         }
3336         break;
3337     case TT_4X8:
3338         pat = ~(subblkpat * 5) & 0xF;
3339         for (j = 0; j < 2; j++) {
3340             last = subblkpat & (1 << (1 - j));
3341             i    = 0;
3342             off  = j * 4;
3343             while (!last) {
3344                 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3345                 i += skip;
3346                 if (i > 31)
3347                     break;
3348                 if (!v->fcm)
3349                     idx = v->zz_4x8[i++] + off;
3350                 else
3351                     idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3352                 block[idx] = value * scale;
3353                 if (!v->pquantizer)
3354                     block[idx] += (block[idx] < 0) ? -mquant : mquant;
3355             }
3356             if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3357                 if (i == 1)
3358                     v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3359                 else
3360                     v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3361             }
3362         }
3363         break;
3364     }
3365     if (ttmb_out)
3366         *ttmb_out |= ttblk << (n * 4);
3367     return pat;
3368 }
3369
3370 /** @} */ // Macroblock group
3371
3372 static const int size_table  [6] = { 0, 2, 3, 4,  5,  8 };
3373 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3374
3375 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3376 {
3377     MpegEncContext *s  = &v->s;
3378     int mb_cbp         = v->cbp[s->mb_x - s->mb_stride],
3379         block_cbp      = mb_cbp      >> (block_num * 4), bottom_cbp,
3380         mb_is_intra    = v->is_intra[s->mb_x - s->mb_stride],
3381         block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3382     int idx, linesize  = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3383     uint8_t *dst;
3384
3385     if (block_num > 3) {
3386         dst      = s->dest[block_num - 3];
3387     } else {
3388         dst      = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3389     }
3390     if (s->mb_y != s->end_mb_y || block_num < 2) {
3391         int16_t (*mv)[2];
3392         int mv_stride;
3393
3394         if (block_num > 3) {
3395             bottom_cbp      = v->cbp[s->mb_x]      >> (block_num * 4);
3396             bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3397             mv              = &v->luma_mv[s->mb_x - s->mb_stride];
3398             mv_stride       = s->mb_stride;
3399         } else {
3400             bottom_cbp      = (block_num < 2) ? (mb_cbp               >> ((block_num + 2) * 4))
3401                                               : (v->cbp[s->mb_x]      >> ((block_num - 2) * 4));
3402             bottom_is_intra = (block_num < 2) ? (mb_is_intra          >> ((block_num + 2) * 4))
3403                                               : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3404             mv_stride       = s->b8_stride;
3405             mv              = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3406         }
3407
3408         if (bottom_is_intra & 1 || block_is_intra & 1 ||
3409             mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3410             v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3411         } else {
3412             idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3413             if (idx == 3) {
3414                 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3415             } else if (idx) {
3416                 if (idx == 1)
3417                     v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3418                 else
3419                     v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
3420             }
3421         }
3422     }
3423
3424     dst -= 4 * linesize;
3425     ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3426     if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3427         idx = (block_cbp | (block_cbp >> 2)) & 3;
3428         if (idx == 3) {
3429             v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3430         } else if (idx) {
3431             if (idx == 1)
3432                 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3433             else
3434                 v->vc1dsp.vc1_v_loop_filter4(dst,     linesize, v->pq);
3435         }
3436     }
3437 }
3438
3439 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3440 {
3441     MpegEncContext *s  = &v->s;
3442     int mb_cbp         = v->cbp[s->mb_x - 1 - s->mb_stride],
3443         block_cbp      = mb_cbp      >> (block_num * 4), right_cbp,
3444         mb_is_intra    = v->is_intra[s->mb_x - 1 - s->mb_stride],
3445         block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3446     int idx, linesize  = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3447     uint8_t *dst;
3448
3449     if (block_num > 3) {
3450         dst = s->dest[block_num - 3] - 8 * linesize;
3451     } else {
3452         dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3453     }
3454
3455     if (s->mb_x != s->mb_width || !(block_num & 5)) {
3456         int16_t (*mv)[2];
3457
3458         if (block_num > 3) {
3459             right_cbp      = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3460             right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3461             mv             = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3462         } else {
3463             right_cbp      = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride]      >> ((block_num - 1) * 4))
3464                                              : (mb_cbp                              >> ((block_num + 1) * 4));
3465             right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3466                                              : (mb_is_intra                         >> ((block_num + 1) * 4));
3467             mv             = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3468         }
3469         if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3470             v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3471         } else {
3472             idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3473             if (idx == 5) {
3474                 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3475             } else if (idx) {
3476                 if (idx == 1)
3477                     v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3478                 else
3479                     v->vc1dsp.vc1_h_loop_filter4(dst,                linesize, v->pq);
3480             }
3481         }
3482     }
3483
3484     dst -= 4;
3485     ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3486     if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3487         idx = (block_cbp | (block_cbp >> 1)) & 5;
3488         if (idx == 5) {
3489             v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3490         } else if (idx) {
3491             if (idx == 1)
3492                 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3493             else
3494                 v->vc1dsp.vc1_h_loop_filter4(dst,                linesize, v->pq);
3495         }
3496     }
3497 }
3498
3499 static void vc1_apply_p_loop_filter(VC1Context *v)
3500 {
3501     MpegEncContext *s = &v->s;
3502     int i;
3503
3504     for (i = 0; i < 6; i++) {
3505         vc1_apply_p_v_loop_filter(v, i);
3506     }
3507
3508     /* V always precedes H, therefore we run H one MB before V;
3509      * at the end of a row, we catch up to complete the row */
3510     if (s->mb_x) {
3511         for (i = 0; i < 6; i++) {
3512             vc1_apply_p_h_loop_filter(v, i);
3513         }
3514         if (s->mb_x == s->mb_width - 1) {
3515             s->mb_x++;
3516             ff_update_block_index(s);
3517             for (i = 0; i < 6; i++) {
3518                 vc1_apply_p_h_loop_filter(v, i);
3519             }
3520         }
3521     }
3522 }
3523
3524 /** Decode one P-frame MB
3525  */
3526 static int vc1_decode_p_mb(VC1Context *v)
3527 {
3528     MpegEncContext *s = &v->s;
3529     GetBitContext *gb = &s->gb;
3530     int i, j;
3531     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3532     int cbp; /* cbp decoding stuff */
3533     int mqdiff, mquant; /* MB quantization */
3534     int ttmb = v->ttfrm; /* MB Transform type */
3535
3536     int mb_has_coeffs = 1; /* last_flag */
3537     int dmv_x, dmv_y; /* Differential MV components */
3538     int index, index1; /* LUT indexes */
3539     int val, sign; /* temp values */
3540     int first_block = 1;
3541     int dst_idx, off;
3542     int skipped, fourmv;
3543     int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3544
3545     mquant = v->pq; /* lossy initialization */
3546
3547     if (v->mv_type_is_raw)
3548         fourmv = get_bits1(gb);
3549     else
3550         fourmv = v->mv_type_mb_plane[mb_pos];
3551     if (v->skip_is_raw)
3552         skipped = get_bits1(gb);
3553     else
3554         skipped = v->s.mbskip_table[mb_pos];
3555
3556     if (!fourmv) { /* 1MV mode */
3557         if (!skipped) {
3558             GET_MVDATA(dmv_x, dmv_y);
3559
3560             if (s->mb_intra) {
3561                 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3562                 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3563             }
3564             s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3565             vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3566
3567             /* FIXME Set DC val for inter block ? */
3568             if (s->mb_intra && !mb_has_coeffs) {
3569                 GET_MQUANT();
3570                 s->ac_pred = get_bits1(gb);
3571                 cbp        = 0;
3572             } else if (mb_has_coeffs) {
3573                 if (s->mb_intra)
3574                     s->ac_pred = get_bits1(gb);
3575                 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3576                 GET_MQUANT();
3577             } else {
3578                 mquant = v->pq;
3579                 cbp    = 0;
3580             }
3581             s->current_picture.qscale_table[mb_pos] = mquant;
3582
3583             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3584                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3585                                 VC1_TTMB_VLC_BITS, 2);
3586             if (!s->mb_intra) vc1_mc_1mv(v, 0);
3587             dst_idx = 0;
3588             for (i = 0; i < 6; i++) {
3589                 s->dc_val[0][s->block_index[i]] = 0;
3590                 dst_idx += i >> 2;
3591                 val = ((cbp >> (5 - i)) & 1);
3592                 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3593                 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3594                 if (s->mb_intra) {
3595                     /* check if prediction blocks A and C are available */
3596                     v->a_avail = v->c_avail = 0;
3597                     if (i == 2 || i == 3 || !s->first_slice_line)
3598                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3599                     if (i == 1 || i == 3 || s->mb_x)
3600                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3601
3602                     vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3603                                            (i & 4) ? v->codingset2 : v->codingset);
3604                     if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3605                         continue;
3606                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3607                     if (v->rangeredfrm)
3608                         for (j = 0; j < 64; j++)
3609                             s->block[i][j] <<= 1;
3610                     s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3611                     if (v->pq >= 9 && v->overlap) {
3612                         if (v->c_avail)
3613                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3614                         if (v->a_avail)
3615                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3616                     }
3617                     block_cbp   |= 0xF << (i << 2);
3618                     block_intra |= 1 << i;
3619                 } else if (val) {
3620                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3621                                              s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3622                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3623                     block_cbp |= pat << (i << 2);
3624                     if (!v->ttmbf && ttmb < 8)
3625                         ttmb = -1;
3626                     first_block = 0;
3627                 }
3628             }
3629         } else { // skipped
3630             s->mb_intra = 0;
3631             for (i = 0; i < 6; i++) {
3632                 v->mb_type[0][s->block_index[i]] = 0;
3633                 s->dc_val[0][s->block_index[i]]  = 0;
3634             }
3635             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
3636             s->current_picture.qscale_table[mb_pos] = 0;
3637             vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3638             vc1_mc_1mv(v, 0);
3639         }
3640     } else { // 4MV mode
3641         if (!skipped /* unskipped MB */) {
3642             int intra_count = 0, coded_inter = 0;
3643             int is_intra[6], is_coded[6];
3644             /* Get CBPCY */
3645             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3646             for (i = 0; i < 6; i++) {
3647                 val = ((cbp >> (5 - i)) & 1);
3648                 s->dc_val[0][s->block_index[i]] = 0;
3649                 s->mb_intra                     = 0;
3650                 if (i < 4) {
3651                     dmv_x = dmv_y = 0;
3652                     s->mb_intra   = 0;
3653                     mb_has_coeffs = 0;
3654                     if (val) {
3655                         GET_MVDATA(dmv_x, dmv_y);
3656                     }
3657                     vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3658                     if (!s->mb_intra)
3659                         vc1_mc_4mv_luma(v, i, 0, 0);
3660                     intra_count += s->mb_intra;
3661                     is_intra[i]  = s->mb_intra;
3662                     is_coded[i]  = mb_has_coeffs;
3663                 }
3664                 if (i & 4) {
3665                     is_intra[i] = (intra_count >= 3);
3666                     is_coded[i] = val;
3667                 }
3668                 if (i == 4)
3669                     vc1_mc_4mv_chroma(v, 0);
3670                 v->mb_type[0][s->block_index[i]] = is_intra[i];
3671                 if (!coded_inter)
3672                     coded_inter = !is_intra[i] & is_coded[i];
3673             }
3674             // if there are no coded blocks then don't do anything more
3675             dst_idx = 0;
3676             if (!intra_count && !coded_inter)
3677                 goto end;
3678             GET_MQUANT();
3679             s->current_picture.qscale_table[mb_pos] = mquant;
3680             /* test if block is intra and has pred */
3681             {
3682                 int intrapred = 0;
3683                 for (i = 0; i < 6; i++)
3684                     if (is_intra[i]) {
3685                         if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3686                             || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3687                             intrapred = 1;
3688                             break;
3689                         }
3690                     }
3691                 if (intrapred)
3692                     s->ac_pred = get_bits1(gb);
3693                 else
3694                     s->ac_pred = 0;
3695             }
3696             if (!v->ttmbf && coded_inter)
3697                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3698             for (i = 0; i < 6; i++) {
3699                 dst_idx    += i >> 2;
3700                 off         = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3701                 s->mb_intra = is_intra[i];
3702                 if (is_intra[i]) {
3703                     /* check if prediction blocks A and C are available */
3704                     v->a_avail = v->c_avail = 0;
3705                     if (i == 2 || i == 3 || !s->first_slice_line)
3706                         v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3707                     if (i == 1 || i == 3 || s->mb_x)
3708                         v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3709
3710                     vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3711                                            (i & 4) ? v->codingset2 : v->codingset);
3712                     if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3713                         continue;
3714                     v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3715                     if (v->rangeredfrm)
3716                         for (j = 0; j < 64; j++)
3717                             s->block[i][j] <<= 1;
3718                     s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3719                                                      (i & 4) ? s->uvlinesize : s->linesize);
3720                     if (v->pq >= 9 && v->overlap) {
3721                         if (v->c_avail)
3722                             v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3723                         if (v->a_avail)
3724                             v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3725                     }
3726                     block_cbp   |= 0xF << (i << 2);
3727                     block_intra |= 1 << i;
3728                 } else if (is_coded[i]) {
3729                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3730                                              first_block, s->dest[dst_idx] + off,
3731                                              (i & 4) ? s->uvlinesize : s->linesize,
3732                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3733                                              &block_tt);
3734                     block_cbp |= pat << (i << 2);
3735                     if (!v->ttmbf && ttmb < 8)
3736                         ttmb = -1;
3737                     first_block = 0;
3738                 }
3739             }
3740         } else { // skipped MB
3741             s->mb_intra                               = 0;
3742             s->current_picture.qscale_table[mb_pos] = 0;
3743             for (i = 0; i < 6; i++) {
3744                 v->mb_type[0][s->block_index[i]] = 0;
3745                 s->dc_val[0][s->block_index[i]]  = 0;
3746             }
3747             for (i = 0; i < 4; i++) {
3748                 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3749                 vc1_mc_4mv_luma(v, i, 0, 0);
3750             }
3751             vc1_mc_4mv_chroma(v, 0);
3752             s->current_picture.qscale_table[mb_pos] = 0;
3753         }
3754     }
3755 end:
3756     v->cbp[s->mb_x]      = block_cbp;
3757     v->ttblk[s->mb_x]    = block_tt;
3758     v->is_intra[s->mb_x] = block_intra;
3759
3760     return 0;
3761 }
3762
3763 /* Decode one macroblock in an interlaced frame p picture */
3764
3765 static int vc1_decode_p_mb_intfr(VC1Context *v)
3766 {
3767     MpegEncContext *s = &v->s;
3768     GetBitContext *gb = &s->gb;
3769     int i;
3770     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3771     int cbp = 0; /* cbp decoding stuff */
3772     int mqdiff, mquant; /* MB quantization */
3773     int ttmb = v->ttfrm; /* MB Transform type */
3774
3775     int mb_has_coeffs = 1; /* last_flag */
3776     int dmv_x, dmv_y; /* Differential MV components */
3777     int val; /* temp value */
3778     int first_block = 1;
3779     int dst_idx, off;
3780     int skipped, fourmv = 0, twomv = 0;
3781     int block_cbp = 0, pat, block_tt = 0;
3782     int idx_mbmode = 0, mvbp;
3783     int stride_y, fieldtx;
3784
3785     mquant = v->pq; /* Lossy initialization */
3786
3787     if (v->skip_is_raw)
3788         skipped = get_bits1(gb);
3789     else
3790         skipped = v->s.mbskip_table[mb_pos];
3791     if (!skipped) {
3792         if (v->fourmvswitch)
3793             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3794         else
3795             idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3796         switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3797         /* store the motion vector type in a flag (useful later) */
3798         case MV_PMODE_INTFR_4MV:
3799             fourmv = 1;
3800             v->blk_mv_type[s->block_index[0]] = 0;
3801             v->blk_mv_type[s->block_index[1]] = 0;
3802             v->blk_mv_type[s->block_index[2]] = 0;
3803             v->blk_mv_type[s->block_index[3]] = 0;
3804             break;
3805         case MV_PMODE_INTFR_4MV_FIELD:
3806             fourmv = 1;
3807             v->blk_mv_type[s->block_index[0]] = 1;
3808             v->blk_mv_type[s->block_index[1]] = 1;
3809             v->blk_mv_type[s->block_index[2]] = 1;
3810             v->blk_mv_type[s->block_index[3]] = 1;
3811             break;
3812         case MV_PMODE_INTFR_2MV_FIELD:
3813             twomv = 1;
3814             v->blk_mv_type[s->block_index[0]] = 1;
3815             v->blk_mv_type[s->block_index[1]] = 1;
3816             v->blk_mv_type[s->block_index[2]] = 1;
3817             v->blk_mv_type[s->block_index[3]] = 1;
3818             break;
3819         case MV_PMODE_INTFR_1MV:
3820             v->blk_mv_type[s->block_index[0]] = 0;
3821             v->blk_mv_type[s->block_index[1]] = 0;
3822             v->blk_mv_type[s->block_index[2]] = 0;
3823             v->blk_mv_type[s->block_index[3]] = 0;
3824             break;
3825         }
3826         if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3827             for (i = 0; i < 4; i++) {
3828                 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3829                 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3830             }
3831             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
3832             s->mb_intra = v->is_intra[s->mb_x] = 1;
3833             for (i = 0; i < 6; i++)
3834                 v->mb_type[0][s->block_index[i]] = 1;
3835             fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3836             mb_has_coeffs = get_bits1(gb);
3837             if (mb_has_coeffs)
3838                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3839             v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3840             GET_MQUANT();
3841             s->current_picture.qscale_table[mb_pos] = mquant;
3842             /* Set DC scale - y and c use the same (not sure if necessary here) */
3843             s->y_dc_scale = s->y_dc_scale_table[mquant];
3844             s->c_dc_scale = s->c_dc_scale_table[mquant];
3845             dst_idx = 0;
3846             for (i = 0; i < 6; i++) {
3847                 s->dc_val[0][s->block_index[i]] = 0;
3848                 dst_idx += i >> 2;
3849                 val = ((cbp >> (5 - i)) & 1);
3850                 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3851                 v->a_avail = v->c_avail = 0;
3852                 if (i == 2 || i == 3 || !s->first_slice_line)
3853                     v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3854                 if (i == 1 || i == 3 || s->mb_x)
3855                     v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3856
3857                 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3858                                        (i & 4) ? v->codingset2 : v->codingset);
3859                 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3860                 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3861                 if (i < 4) {
3862                     stride_y = s->linesize << fieldtx;
3863                     off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3864                 } else {
3865                     stride_y = s->uvlinesize;
3866                     off = 0;
3867                 }
3868                 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3869                 //TODO: loop filter
3870             }
3871
3872         } else { // inter MB
3873             mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3874             if (mb_has_coeffs)
3875                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3876             if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3877                 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3878             } else {
3879                 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3880                     || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3881                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3882                 }
3883             }
3884             s->mb_intra = v->is_intra[s->mb_x] = 0;
3885             for (i = 0; i < 6; i++)
3886                 v->mb_type[0][s->block_index[i]] = 0;
3887             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3888             /* for all motion vector read MVDATA and motion compensate each block */
3889             dst_idx = 0;
3890             if (fourmv) {
3891                 mvbp = v->fourmvbp;
3892                 for (i = 0; i < 6; i++) {
3893                     if (i < 4) {
3894                         dmv_x = dmv_y = 0;
3895                         val   = ((mvbp >> (3 - i)) & 1);
3896                         if (val) {
3897                             get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3898                         }
3899                         vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3900                         vc1_mc_4mv_luma(v, i, 0, 0);
3901                     } else if (i == 4) {
3902                         vc1_mc_4mv_chroma4(v, 0, 0, 0);
3903                     }
3904                 }
3905             } else if (twomv) {
3906                 mvbp  = v->twomvbp;
3907                 dmv_x = dmv_y = 0;
3908                 if (mvbp & 2) {
3909                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3910                 }
3911                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3912                 vc1_mc_4mv_luma(v, 0, 0, 0);
3913                 vc1_mc_4mv_luma(v, 1, 0, 0);
3914                 dmv_x = dmv_y = 0;
3915                 if (mvbp & 1) {
3916                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3917                 }
3918                 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3919                 vc1_mc_4mv_luma(v, 2, 0, 0);
3920                 vc1_mc_4mv_luma(v, 3, 0, 0);
3921                 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3922             } else {
3923                 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3924                 dmv_x = dmv_y = 0;
3925                 if (mvbp) {
3926                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3927                 }
3928                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3929                 vc1_mc_1mv(v, 0);
3930             }
3931             if (cbp)
3932                 GET_MQUANT();  // p. 227
3933             s->current_picture.qscale_table[mb_pos] = mquant;
3934             if (!v->ttmbf && cbp)
3935                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3936             for (i = 0; i < 6; i++) {
3937                 s->dc_val[0][s->block_index[i]] = 0;
3938                 dst_idx += i >> 2;
3939                 val = ((cbp >> (5 - i)) & 1);
3940                 if (!fieldtx)
3941                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3942                 else
3943                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3944                 if (val) {
3945                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3946                                              first_block, s->dest[dst_idx] + off,
3947                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3948                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3949                     block_cbp |= pat << (i << 2);
3950                     if (!v->ttmbf && ttmb < 8)
3951                         ttmb = -1;
3952                     first_block = 0;
3953                 }
3954             }
3955         }
3956     } else { // skipped
3957         s->mb_intra = v->is_intra[s->mb_x] = 0;
3958         for (i = 0; i < 6; i++) {
3959             v->mb_type[0][s->block_index[i]] = 0;
3960             s->dc_val[0][s->block_index[i]] = 0;
3961         }
3962         s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
3963         s->current_picture.qscale_table[mb_pos] = 0;
3964         v->blk_mv_type[s->block_index[0]] = 0;
3965         v->blk_mv_type[s->block_index[1]] = 0;
3966         v->blk_mv_type[s->block_index[2]] = 0;
3967         v->blk_mv_type[s->block_index[3]] = 0;
3968         vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3969         vc1_mc_1mv(v, 0);
3970     }
3971     if (s->mb_x == s->mb_width - 1)
3972         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3973     return 0;
3974 }
3975
3976 static int vc1_decode_p_mb_intfi(VC1Context *v)
3977 {
3978     MpegEncContext *s = &v->s;
3979     GetBitContext *gb = &s->gb;
3980     int i;
3981     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3982     int cbp = 0; /* cbp decoding stuff */
3983     int mqdiff, mquant; /* MB quantization */
3984     int ttmb = v->ttfrm; /* MB Transform type */
3985
3986     int mb_has_coeffs = 1; /* last_flag */
3987     int dmv_x, dmv_y; /* Differential MV components */
3988     int val; /* temp values */
3989     int first_block = 1;
3990     int dst_idx, off;
3991     int pred_flag = 0;
3992     int block_cbp = 0, pat, block_tt = 0;
3993     int idx_mbmode = 0;
3994
3995     mquant = v->pq; /* Lossy initialization */
3996
3997     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3998     if (idx_mbmode <= 1) { // intra MB
3999         s->mb_intra = v->is_intra[s->mb_x] = 1;
4000         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4001         s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4002         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4003         GET_MQUANT();
4004         s->current_picture.qscale_table[mb_pos] = mquant;
4005         /* Set DC scale - y and c use the same (not sure if necessary here) */
4006         s->y_dc_scale = s->y_dc_scale_table[mquant];
4007         s->c_dc_scale = s->c_dc_scale_table[mquant];
4008         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
4009         mb_has_coeffs = idx_mbmode & 1;
4010         if (mb_has_coeffs)
4011             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4012         dst_idx = 0;
4013         for (i = 0; i < 6; i++) {
4014             s->dc_val[0][s->block_index[i]]  = 0;
4015             v->mb_type[0][s->block_index[i]] = 1;
4016             dst_idx += i >> 2;
4017             val = ((cbp >> (5 - i)) & 1);
4018             v->a_avail = v->c_avail = 0;
4019             if (i == 2 || i == 3 || !s->first_slice_line)
4020                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4021             if (i == 1 || i == 3 || s->mb_x)
4022                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4023
4024             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4025                                    (i & 4) ? v->codingset2 : v->codingset);
4026             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4027                 continue;
4028             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4029             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4030             s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4031             // TODO: loop filter
4032         }
4033     } else {
4034         s->mb_intra = v->is_intra[s->mb_x] = 0;
4035         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4036         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4037         if (idx_mbmode <= 5) { // 1-MV
4038             dmv_x = dmv_y = pred_flag = 0;
4039             if (idx_mbmode & 1) {
4040                 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4041             }
4042             vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4043             vc1_mc_1mv(v, 0);
4044             mb_has_coeffs = !(idx_mbmode & 2);
4045         } else { // 4-MV
4046             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4047             for (i = 0; i < 6; i++) {
4048                 if (i < 4) {
4049                     dmv_x = dmv_y = pred_flag = 0;
4050                     val   = ((v->fourmvbp >> (3 - i)) & 1);
4051                     if (val) {
4052                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4053                     }
4054                     vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4055                     vc1_mc_4mv_luma(v, i, 0, 0);
4056                 } else if (i == 4)
4057                     vc1_mc_4mv_chroma(v, 0);
4058             }
4059             mb_has_coeffs = idx_mbmode & 1;
4060         }
4061         if (mb_has_coeffs)
4062             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4063         if (cbp) {
4064             GET_MQUANT();
4065         }
4066         s->current_picture.qscale_table[mb_pos] = mquant;
4067         if (!v->ttmbf && cbp) {
4068             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4069         }
4070         dst_idx = 0;
4071         for (i = 0; i < 6; i++) {
4072             s->dc_val[0][s->block_index[i]] = 0;
4073             dst_idx += i >> 2;
4074             val = ((cbp >> (5 - i)) & 1);
4075             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4076             if (val) {
4077                 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4078                                          first_block, s->dest[dst_idx] + off,
4079                                          (i & 4) ? s->uvlinesize : s->linesize,
4080                                          (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4081                                          &block_tt);
4082                 block_cbp |= pat << (i << 2);
4083                 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4084                 first_block = 0;
4085             }
4086         }
4087     }
4088     if (s->mb_x == s->mb_width - 1)
4089         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4090     return 0;
4091 }
4092
4093 /** Decode one B-frame MB (in Main profile)
4094  */
4095 static void vc1_decode_b_mb(VC1Context *v)
4096 {
4097     MpegEncContext *s = &v->s;
4098     GetBitContext *gb = &s->gb;
4099     int i, j;
4100     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4101     int cbp = 0; /* cbp decoding stuff */
4102     int mqdiff, mquant; /* MB quantization */
4103     int ttmb = v->ttfrm; /* MB Transform type */
4104     int mb_has_coeffs = 0; /* last_flag */
4105     int index, index1; /* LUT indexes */
4106     int val, sign; /* temp values */
4107     int first_block = 1;
4108     int dst_idx, off;
4109     int skipped, direct;
4110     int dmv_x[2], dmv_y[2];
4111     int bmvtype = BMV_TYPE_BACKWARD;
4112
4113     mquant      = v->pq; /* lossy initialization */
4114     s->mb_intra = 0;
4115
4116     if (v->dmb_is_raw)
4117         direct = get_bits1(gb);
4118     else
4119         direct = v->direct_mb_plane[mb_pos];
4120     if (v->skip_is_raw)
4121         skipped = get_bits1(gb);
4122     else
4123         skipped = v->s.mbskip_table[mb_pos];
4124
4125     dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4126     for (i = 0; i < 6; i++) {
4127         v->mb_type[0][s->block_index[i]] = 0;
4128         s->dc_val[0][s->block_index[i]]  = 0;
4129     }
4130     s->current_picture.qscale_table[mb_pos] = 0;
4131
4132     if (!direct) {
4133         if (!skipped) {
4134             GET_MVDATA(dmv_x[0], dmv_y[0]);
4135             dmv_x[1] = dmv_x[0];
4136             dmv_y[1] = dmv_y[0];
4137         }
4138         if (skipped || !s->mb_intra) {
4139             bmvtype = decode012(gb);
4140             switch (bmvtype) {
4141             case 0:
4142                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4143                 break;
4144             case 1:
4145                 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4146                 break;
4147             case 2:
4148                 bmvtype  = BMV_TYPE_INTERPOLATED;
4149                 dmv_x[0] = dmv_y[0] = 0;
4150             }
4151         }
4152     }
4153     for (i = 0; i < 6; i++)
4154         v->mb_type[0][s->block_index[i]] = s->mb_intra;
4155
4156     if (skipped) {
4157         if (direct)
4158             bmvtype = BMV_TYPE_INTERPOLATED;
4159         vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4160         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4161         return;
4162     }
4163     if (direct) {
4164         cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4165         GET_MQUANT();
4166         s->mb_intra = 0;
4167         s->current_picture.qscale_table[mb_pos] = mquant;
4168         if (!v->ttmbf)
4169             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4170         dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4171         vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4172         vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4173     } else {
4174         if (!mb_has_coeffs && !s->mb_intra) {
4175             /* no coded blocks - effectively skipped */
4176             vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4177             vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4178             return;
4179         }
4180         if (s->mb_intra && !mb_has_coeffs) {
4181             GET_MQUANT();
4182             s->current_picture.qscale_table[mb_pos] = mquant;
4183             s->ac_pred = get_bits1(gb);
4184             cbp = 0;
4185             vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4186         } else {
4187             if (bmvtype == BMV_TYPE_INTERPOLATED) {
4188                 GET_MVDATA(dmv_x[0], dmv_y[0]);
4189                 if (!mb_has_coeffs) {
4190                     /* interpolated skipped block */
4191                     vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4192                     vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4193                     return;
4194                 }
4195             }
4196             vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4197             if (!s->mb_intra) {
4198                 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4199             }
4200             if (s->mb_intra)
4201                 s->ac_pred = get_bits1(gb);
4202             cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4203             GET_MQUANT();
4204             s->current_picture.qscale_table[mb_pos] = mquant;
4205             if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4206                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4207         }
4208     }
4209     dst_idx = 0;
4210     for (i = 0; i < 6; i++) {
4211         s->dc_val[0][s->block_index[i]] = 0;
4212         dst_idx += i >> 2;
4213         val = ((cbp >> (5 - i)) & 1);
4214         off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4215         v->mb_type[0][s->block_index[i]] = s->mb_intra;
4216         if (s->mb_intra) {
4217             /* check if prediction blocks A and C are available */
4218             v->a_avail = v->c_avail = 0;
4219             if (i == 2 || i == 3 || !s->first_slice_line)
4220                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4221             if (i == 1 || i == 3 || s->mb_x)
4222                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4223
4224             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4225                                    (i & 4) ? v->codingset2 : v->codingset);
4226             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4227                 continue;
4228             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4229             if (v->rangeredfrm)
4230                 for (j = 0; j < 64; j++)
4231                     s->block[i][j] <<= 1;
4232             s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4233         } else if (val) {
4234             vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4235                                first_block, s->dest[dst_idx] + off,
4236                                (i & 4) ? s->uvlinesize : s->linesize,
4237                                (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4238             if (!v->ttmbf && ttmb < 8)
4239                 ttmb = -1;
4240             first_block = 0;
4241         }
4242     }
4243 }
4244
4245 /** Decode one B-frame MB (in interlaced field B picture)
4246  */
4247 static void vc1_decode_b_mb_intfi(VC1Context *v)
4248 {
4249     MpegEncContext *s = &v->s;
4250     GetBitContext *gb = &s->gb;
4251     int i, j;
4252     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4253     int cbp = 0; /* cbp decoding stuff */
4254     int mqdiff, mquant; /* MB quantization */
4255     int ttmb = v->ttfrm; /* MB Transform type */
4256     int mb_has_coeffs = 0; /* last_flag */
4257     int val; /* temp value */
4258     int first_block = 1;
4259     int dst_idx, off;
4260     int fwd;
4261     int dmv_x[2], dmv_y[2], pred_flag[2];
4262     int bmvtype = BMV_TYPE_BACKWARD;
4263     int idx_mbmode;
4264
4265     mquant      = v->pq; /* Lossy initialization */
4266     s->mb_intra = 0;
4267
4268     idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4269     if (idx_mbmode <= 1) { // intra MB
4270         s->mb_intra = v->is_intra[s->mb_x] = 1;
4271         s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4272         s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4273         s->current_picture.mb_type[mb_pos + v->mb_off]         = MB_TYPE_INTRA;
4274         GET_MQUANT();
4275         s->current_picture.qscale_table[mb_pos] = mquant;
4276         /* Set DC scale - y and c use the same (not sure if necessary here) */
4277         s->y_dc_scale = s->y_dc_scale_table[mquant];
4278         s->c_dc_scale = s->c_dc_scale_table[mquant];
4279         v->s.ac_pred  = v->acpred_plane[mb_pos] = get_bits1(gb);
4280         mb_has_coeffs = idx_mbmode & 1;
4281         if (mb_has_coeffs)
4282             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4283         dst_idx = 0;
4284         for (i = 0; i < 6; i++) {
4285             s->dc_val[0][s->block_index[i]] = 0;
4286             dst_idx += i >> 2;
4287             val = ((cbp >> (5 - i)) & 1);
4288             v->mb_type[0][s->block_index[i]] = s->mb_intra;
4289             v->a_avail                       = v->c_avail = 0;
4290             if (i == 2 || i == 3 || !s->first_slice_line)
4291                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4292             if (i == 1 || i == 3 || s->mb_x)
4293                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4294
4295             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4296                                    (i & 4) ? v->codingset2 : v->codingset);
4297             if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4298                 continue;
4299             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4300             if (v->rangeredfrm)
4301                 for (j = 0; j < 64; j++)
4302                     s->block[i][j] <<= 1;
4303             off  = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4304             s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4305             // TODO: yet to perform loop filter
4306         }
4307     } else {
4308         s->mb_intra = v->is_intra[s->mb_x] = 0;
4309         s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4310         for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4311         if (v->fmb_is_raw)
4312             fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4313         else
4314             fwd = v->forward_mb_plane[mb_pos];
4315         if (idx_mbmode <= 5) { // 1-MV
4316             int interpmvp = 0;
4317             dmv_x[0]     = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4318             pred_flag[0] = pred_flag[1] = 0;
4319             if (fwd)
4320                 bmvtype = BMV_TYPE_FORWARD;
4321             else {
4322                 bmvtype = decode012(gb);
4323                 switch (bmvtype) {
4324                 case 0:
4325                     bmvtype = BMV_TYPE_BACKWARD;
4326                     break;
4327                 case 1:
4328                     bmvtype = BMV_TYPE_DIRECT;
4329                     break;
4330                 case 2:
4331                     bmvtype   = BMV_TYPE_INTERPOLATED;
4332                     interpmvp = get_bits1(gb);
4333                 }
4334             }
4335             v->bmvtype = bmvtype;
4336             if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4337                 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4338             }
4339             if (interpmvp) {
4340                 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4341             }
4342             if (bmvtype == BMV_TYPE_DIRECT) {
4343                 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4344                 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4345                 if (!s->next_picture_ptr->field_picture) {
4346                     av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
4347                     return;
4348                 }
4349             }
4350             vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4351             vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4352             mb_has_coeffs = !(idx_mbmode & 2);
4353         } else { // 4-MV
4354             if (fwd)
4355                 bmvtype = BMV_TYPE_FORWARD;
4356             v->bmvtype  = bmvtype;
4357             v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4358             for (i = 0; i < 6; i++) {
4359                 if (i < 4) {
4360                     dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4361                     dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4362                     val = ((v->fourmvbp >> (3 - i)) & 1);
4363                     if (val) {
4364                         get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4365                                                  &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4366                                              &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4367                     }
4368                     vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4369                     vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4370                 } else if (i == 4)
4371                     vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4372             }
4373             mb_has_coeffs = idx_mbmode & 1;
4374         }
4375         if (mb_has_coeffs)
4376             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4377         if (cbp) {
4378             GET_MQUANT();
4379         }
4380         s->current_picture.qscale_table[mb_pos] = mquant;
4381         if (!v->ttmbf && cbp) {
4382             ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4383         }
4384         dst_idx = 0;
4385         for (i = 0; i < 6; i++) {
4386             s->dc_val[0][s->block_index[i]] = 0;
4387             dst_idx += i >> 2;
4388             val = ((cbp >> (5 - i)) & 1);
4389             off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4390             if (val) {
4391                 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4392                                    first_block, s->dest[dst_idx] + off,
4393                                    (i & 4) ? s->uvlinesize : s->linesize,
4394                                    (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4395                 if (!v->ttmbf && ttmb < 8)
4396                     ttmb = -1;
4397                 first_block = 0;
4398             }
4399         }
4400     }
4401 }
4402
4403 /** Decode one B-frame MB (in interlaced frame B picture)
4404  */
4405 static int vc1_decode_b_mb_intfr(VC1Context *v)
4406 {
4407     MpegEncContext *s = &v->s;
4408     GetBitContext *gb = &s->gb;
4409     int i, j;
4410     int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4411     int cbp = 0; /* cbp decoding stuff */
4412     int mqdiff, mquant; /* MB quantization */
4413     int ttmb = v->ttfrm; /* MB Transform type */
4414     int mvsw = 0; /* motion vector switch */
4415     int mb_has_coeffs = 1; /* last_flag */
4416     int dmv_x, dmv_y; /* Differential MV components */
4417     int val; /* temp value */
4418     int first_block = 1;
4419     int dst_idx, off;
4420     int skipped, direct, twomv = 0;
4421     int block_cbp = 0, pat, block_tt = 0;
4422     int idx_mbmode = 0, mvbp;
4423     int stride_y, fieldtx;
4424     int bmvtype = BMV_TYPE_BACKWARD;
4425     int dir, dir2;
4426
4427     mquant = v->pq; /* Lossy initialization */
4428     s->mb_intra = 0;
4429     if (v->skip_is_raw)
4430         skipped = get_bits1(gb);
4431     else
4432         skipped = v->s.mbskip_table[mb_pos];
4433
4434     if (!skipped) {
4435         idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4436         if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4437             twomv = 1;
4438             v->blk_mv_type[s->block_index[0]] = 1;
4439             v->blk_mv_type[s->block_index[1]] = 1;
4440             v->blk_mv_type[s->block_index[2]] = 1;
4441             v->blk_mv_type[s->block_index[3]] = 1;
4442         } else {
4443             v->blk_mv_type[s->block_index[0]] = 0;
4444             v->blk_mv_type[s->block_index[1]] = 0;
4445             v->blk_mv_type[s->block_index[2]] = 0;
4446             v->blk_mv_type[s->block_index[3]] = 0;
4447         }
4448     }
4449
4450     if (v->dmb_is_raw)
4451         direct = get_bits1(gb);
4452     else
4453         direct = v->direct_mb_plane[mb_pos];
4454
4455     if (direct) {
4456         if (s->next_picture_ptr->field_picture)
4457             av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
4458         s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
4459         s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
4460         s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
4461         s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
4462
4463         if (twomv) {
4464             s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
4465             s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
4466             s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
4467             s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
4468
4469             for (i = 1; i < 4; i += 2) {
4470                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4471                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4472                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4473                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4474             }
4475         } else {
4476             for (i = 1; i < 4; i++) {
4477                 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4478                 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4479                 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4480                 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4481             }
4482         }
4483     }
4484
4485     if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4486         for (i = 0; i < 4; i++) {
4487             s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4488             s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4489             s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4490             s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4491         }
4492         s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4493         s->mb_intra = v->is_intra[s->mb_x] = 1;
4494         for (i = 0; i < 6; i++)
4495             v->mb_type[0][s->block_index[i]] = 1;
4496         fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4497         mb_has_coeffs = get_bits1(gb);
4498         if (mb_has_coeffs)
4499             cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4500         v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4501         GET_MQUANT();
4502         s->current_picture.qscale_table[mb_pos] = mquant;
4503         /* Set DC scale - y and c use the same (not sure if necessary here) */
4504         s->y_dc_scale = s->y_dc_scale_table[mquant];
4505         s->c_dc_scale = s->c_dc_scale_table[mquant];
4506         dst_idx = 0;
4507         for (i = 0; i < 6; i++) {
4508             s->dc_val[0][s->block_index[i]] = 0;
4509             dst_idx += i >> 2;
4510             val = ((cbp >> (5 - i)) & 1);
4511             v->mb_type[0][s->block_index[i]] = s->mb_intra;
4512             v->a_avail = v->c_avail = 0;
4513             if (i == 2 || i == 3 || !s->first_slice_line)
4514                 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4515             if (i == 1 || i == 3 || s->mb_x)
4516                 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4517
4518             vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4519                                    (i & 4) ? v->codingset2 : v->codingset);
4520             if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4521                 continue;
4522             v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4523             if (i < 4) {
4524                 stride_y = s->linesize << fieldtx;
4525                 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4526             } else {
4527                 stride_y = s->uvlinesize;
4528                 off = 0;
4529             }
4530             s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4531         }
4532     } else {
4533         s->mb_intra = v->is_intra[s->mb_x] = 0;
4534         if (!direct) {
4535             if (skipped || !s->mb_intra) {
4536                 bmvtype = decode012(gb);
4537                 switch (bmvtype) {
4538                 case 0:
4539                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4540                     break;
4541                 case 1:
4542                     bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4543                     break;
4544                 case 2:
4545                     bmvtype  = BMV_TYPE_INTERPOLATED;
4546                 }
4547             }
4548
4549             if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4550                 mvsw = get_bits1(gb);
4551         }
4552
4553         if (!skipped) { // inter MB
4554             mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4555             if (mb_has_coeffs)
4556                 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4557             if (!direct) {
4558                 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
4559                     v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4560                 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
4561                     v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4562                 }
4563             }
4564
4565             for (i = 0; i < 6; i++)
4566                 v->mb_type[0][s->block_index[i]] = 0;
4567             fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4568             /* for all motion vector read MVDATA and motion compensate each block */
4569             dst_idx = 0;
4570             if (direct) {
4571                 if (twomv) {
4572                     for (i = 0; i < 4; i++) {
4573                         vc1_mc_4mv_luma(v, i, 0, 0);
4574                         vc1_mc_4mv_luma(v, i, 1, 1);
4575                     }
4576                     vc1_mc_4mv_chroma4(v, 0, 0, 0);
4577                     vc1_mc_4mv_chroma4(v, 1, 1, 1);
4578                 } else {
4579                     vc1_mc_1mv(v, 0);
4580                     vc1_interp_mc(v);
4581                 }
4582             } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4583                 mvbp = v->fourmvbp;
4584                 for (i = 0; i < 4; i++) {
4585                     dir = i==1 || i==3;
4586                     dmv_x = dmv_y = 0;
4587                     val = ((mvbp >> (3 - i)) & 1);
4588                     if (val)
4589                         get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4590                     j = i > 1 ? 2 : 0;
4591                     vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4592                     vc1_mc_4mv_luma(v, j, dir, dir);
4593                     vc1_mc_4mv_luma(v, j+1, dir, dir);
4594                 }
4595
4596                 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4597                 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4598             } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4599                 mvbp = v->twomvbp;
4600                 dmv_x = dmv_y = 0;
4601                 if (mvbp & 2)
4602                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4603
4604                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4605                 vc1_mc_1mv(v, 0);
4606
4607                 dmv_x = dmv_y = 0;
4608                 if (mvbp & 1)
4609                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4610
4611                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4612                 vc1_interp_mc(v);
4613             } else if (twomv) {
4614                 dir = bmvtype == BMV_TYPE_BACKWARD;
4615                 dir2 = dir;
4616                 if (mvsw)
4617                     dir2 = !dir;
4618                 mvbp = v->twomvbp;
4619                 dmv_x = dmv_y = 0;
4620                 if (mvbp & 2)
4621                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4622                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4623
4624                 dmv_x = dmv_y = 0;
4625                 if (mvbp & 1)
4626                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4627                 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4628
4629                 if (mvsw) {
4630                     for (i = 0; i < 2; i++) {
4631                         s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
4632                         s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
4633                         s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
4634                         s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
4635                     }
4636                 } else {
4637                     vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4638                     vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4639                 }
4640
4641                 vc1_mc_4mv_luma(v, 0, dir, 0);
4642                 vc1_mc_4mv_luma(v, 1, dir, 0);
4643                 vc1_mc_4mv_luma(v, 2, dir2, 0);
4644                 vc1_mc_4mv_luma(v, 3, dir2, 0);
4645                 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4646             } else {
4647                 dir = bmvtype == BMV_TYPE_BACKWARD;
4648
4649                 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4650                 dmv_x = dmv_y = 0;
4651                 if (mvbp)
4652                     get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4653
4654                 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4655                 v->blk_mv_type[s->block_index[0]] = 1;
4656                 v->blk_mv_type[s->block_index[1]] = 1;
4657                 v->blk_mv_type[s->block_index[2]] = 1;
4658                 v->blk_mv_type[s->block_index[3]] = 1;
4659                 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4660                 for (i = 0; i < 2; i++) {
4661                     s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
4662                     s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
4663                 }
4664                 vc1_mc_1mv(v, dir);
4665             }
4666
4667             if (cbp)
4668                 GET_MQUANT();  // p. 227
4669             s->current_picture.qscale_table[mb_pos] = mquant;
4670             if (!v->ttmbf && cbp)
4671                 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4672             for (i = 0; i < 6; i++) {
4673                 s->dc_val[0][s->block_index[i]] = 0;
4674                 dst_idx += i >> 2;
4675                 val = ((cbp >> (5 - i)) & 1);
4676                 if (!fieldtx)
4677                     off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4678                 else
4679                     off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4680                 if (val) {
4681                     pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4682                                              first_block, s->dest[dst_idx] + off,
4683                                              (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4684                                              (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4685                     block_cbp |= pat << (i << 2);
4686                     if (!v->ttmbf && ttmb < 8)
4687                         ttmb = -1;
4688                     first_block = 0;
4689                 }
4690             }
4691
4692         } else { // skipped
4693             dir = 0;
4694             for (i = 0; i < 6; i++) {
4695                 v->mb_type[0][s->block_index[i]] = 0;
4696                 s->dc_val[0][s->block_index[i]] = 0;
4697             }
4698             s->current_picture.mb_type[mb_pos]      = MB_TYPE_SKIP;
4699             s->current_picture.qscale_table[mb_pos] = 0;
4700             v->blk_mv_type[s->block_index[0]] = 0;
4701             v->blk_mv_type[s->block_index[1]] = 0;
4702             v->blk_mv_type[s->block_index[2]] = 0;
4703             v->blk_mv_type[s->block_index[3]] = 0;
4704
4705             if (!direct) {
4706                 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4707                     vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4708                     vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4709                 } else {
4710                     dir = bmvtype == BMV_TYPE_BACKWARD;
4711                     vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4712                     if (mvsw) {
4713                         int dir2 = dir;
4714                         if (mvsw)
4715                             dir2 = !dir;
4716                         for (i = 0; i < 2; i++) {
4717                             s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
4718                             s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
4719                             s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
4720                             s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
4721                         }
4722                     } else {
4723                         v->blk_mv_type[s->block_index[0]] = 1;
4724                         v->blk_mv_type[s->block_index[1]] = 1;
4725                         v->blk_mv_type[s->block_index[2]] = 1;
4726                         v->blk_mv_type[s->block_index[3]] = 1;
4727                         vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4728                         for (i = 0; i < 2; i++) {
4729                             s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
4730                             s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
4731                         }
4732                     }
4733                 }
4734             }
4735
4736             vc1_mc_1mv(v, dir);
4737             if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4738                 vc1_interp_mc(v);
4739             }
4740         }
4741     }
4742     if (s->mb_x == s->mb_width - 1)
4743         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4744     v->cbp[s->mb_x]      = block_cbp;
4745     v->ttblk[s->mb_x]    = block_tt;
4746     return 0;
4747 }
4748
4749 /** Decode blocks of I-frame
4750  */
4751 static void vc1_decode_i_blocks(VC1Context *v)
4752 {
4753     int k, j;
4754     MpegEncContext *s = &v->s;
4755     int cbp, val;
4756     uint8_t *coded_val;
4757     int mb_pos;
4758
4759     /* select codingmode used for VLC tables selection */
4760     switch (v->y_ac_table_index) {
4761     case 0:
4762         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4763         break;
4764     case 1:
4765         v->codingset = CS_HIGH_MOT_INTRA;
4766         break;
4767     case 2:
4768         v->codingset = CS_MID_RATE_INTRA;
4769         break;
4770     }
4771
4772     switch (v->c_ac_table_index) {
4773     case 0:
4774         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4775         break;
4776     case 1:
4777         v->codingset2 = CS_HIGH_MOT_INTER;
4778         break;
4779     case 2:
4780         v->codingset2 = CS_MID_RATE_INTER;
4781         break;
4782     }
4783
4784     /* Set DC scale - y and c use the same */
4785     s->y_dc_scale = s->y_dc_scale_table[v->pq];
4786     s->c_dc_scale = s->c_dc_scale_table[v->pq];
4787
4788     //do frame decode
4789     s->mb_x = s->mb_y = 0;
4790     s->mb_intra         = 1;
4791     s->first_slice_line = 1;
4792     for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4793         s->mb_x = 0;
4794         init_block_index(v);
4795         for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4796             uint8_t *dst[6];
4797             ff_update_block_index(s);
4798             dst[0] = s->dest[0];
4799             dst[1] = dst[0] + 8;
4800             dst[2] = s->dest[0] + s->linesize * 8;
4801             dst[3] = dst[2] + 8;
4802             dst[4] = s->dest[1];
4803             dst[5] = s->dest[2];
4804             s->bdsp.clear_blocks(s->block[0]);
4805             mb_pos = s->mb_x + s->mb_y * s->mb_width;
4806             s->current_picture.mb_type[mb_pos]                     = MB_TYPE_INTRA;
4807             s->current_picture.qscale_table[mb_pos]                = v->pq;
4808             s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4809             s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4810
4811             // do actual MB decoding and displaying
4812             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4813             v->s.ac_pred = get_bits1(&v->s.gb);
4814
4815             for (k = 0; k < 6; k++) {
4816                 val = ((cbp >> (5 - k)) & 1);
4817
4818                 if (k < 4) {
4819                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
4820                     val        = val ^ pred;
4821                     *coded_val = val;
4822                 }
4823                 cbp |= val << (5 - k);
4824
4825                 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4826
4827                 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4828                     continue;
4829                 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4830                 if (v->pq >= 9 && v->overlap) {
4831                     if (v->rangeredfrm)
4832                         for (j = 0; j < 64; j++)
4833                             s->block[k][j] <<= 1;
4834                     s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4835                 } else {
4836                     if (v->rangeredfrm)
4837                         for (j = 0; j < 64; j++)
4838                             s->block[k][j] = (s->block[k][j] - 64) << 1;
4839                     s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4840                 }
4841             }
4842
4843             if (v->pq >= 9 && v->overlap) {
4844                 if (s->mb_x) {
4845                     v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4846                     v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4847                     if (!(s->flags & CODEC_FLAG_GRAY)) {
4848                         v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4849                         v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4850                     }
4851                 }
4852                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4853                 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4854                 if (!s->first_slice_line) {
4855                     v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4856                     v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4857                     if (!(s->flags & CODEC_FLAG_GRAY)) {
4858                         v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4859                         v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4860                     }
4861                 }
4862                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4863                 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4864             }
4865             if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4866
4867             if (get_bits_count(&s->gb) > v->bits) {
4868                 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4869                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4870                        get_bits_count(&s->gb), v->bits);
4871                 return;
4872             }
4873         }
4874         if (!v->s.loop_filter)
4875             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4876         else if (s->mb_y)
4877             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4878
4879         s->first_slice_line = 0;
4880     }
4881     if (v->s.loop_filter)
4882         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4883
4884     /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4885      * profile, these only differ are when decoding MSS2 rectangles. */
4886     ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4887 }
4888
4889 /** Decode blocks of I-frame for advanced profile
4890  */
4891 static void vc1_decode_i_blocks_adv(VC1Context *v)
4892 {
4893     int k;
4894     MpegEncContext *s = &v->s;
4895     int cbp, val;
4896     uint8_t *coded_val;
4897     int mb_pos;
4898     int mquant = v->pq;
4899     int mqdiff;
4900     GetBitContext *gb = &s->gb;
4901
4902     /* select codingmode used for VLC tables selection */
4903     switch (v->y_ac_table_index) {
4904     case 0:
4905         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4906         break;
4907     case 1:
4908         v->codingset = CS_HIGH_MOT_INTRA;
4909         break;
4910     case 2:
4911         v->codingset = CS_MID_RATE_INTRA;
4912         break;
4913     }
4914
4915     switch (v->c_ac_table_index) {
4916     case 0:
4917         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4918         break;
4919     case 1:
4920         v->codingset2 = CS_HIGH_MOT_INTER;
4921         break;
4922     case 2:
4923         v->codingset2 = CS_MID_RATE_INTER;
4924         break;
4925     }
4926
4927     // do frame decode
4928     s->mb_x             = s->mb_y = 0;
4929     s->mb_intra         = 1;
4930     s->first_slice_line = 1;
4931     s->mb_y             = s->start_mb_y;
4932     if (s->start_mb_y) {
4933         s->mb_x = 0;
4934         init_block_index(v);
4935         memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4936                (1 + s->b8_stride) * sizeof(*s->coded_block));
4937     }
4938     for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4939         s->mb_x = 0;
4940         init_block_index(v);
4941         for (;s->mb_x < s->mb_width; s->mb_x++) {
4942             int16_t (*block)[64] = v->block[v->cur_blk_idx];
4943             ff_update_block_index(s);
4944             s->bdsp.clear_blocks(block[0]);
4945             mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4946             s->current_picture.mb_type[mb_pos + v->mb_off]                         = MB_TYPE_INTRA;
4947             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4948             s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4949
4950             // do actual MB decoding and displaying
4951             if (v->fieldtx_is_raw)
4952                 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4953             cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4954             if ( v->acpred_is_raw)
4955                 v->s.ac_pred = get_bits1(&v->s.gb);
4956             else
4957                 v->s.ac_pred = v->acpred_plane[mb_pos];
4958
4959             if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4960                 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4961
4962             GET_MQUANT();
4963
4964             s->current_picture.qscale_table[mb_pos] = mquant;
4965             /* Set DC scale - y and c use the same */
4966             s->y_dc_scale = s->y_dc_scale_table[mquant];
4967             s->c_dc_scale = s->c_dc_scale_table[mquant];
4968
4969             for (k = 0; k < 6; k++) {
4970                 val = ((cbp >> (5 - k)) & 1);
4971
4972                 if (k < 4) {
4973                     int pred   = vc1_coded_block_pred(&v->s, k, &coded_val);
4974                     val        = val ^ pred;
4975                     *coded_val = val;
4976                 }
4977                 cbp |= val << (5 - k);
4978
4979                 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4980                 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4981
4982                 vc1_decode_i_block_adv(v, block[k], k, val,
4983                                        (k < 4) ? v->codingset : v->codingset2, mquant);
4984
4985                 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4986                     continue;
4987                 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4988             }
4989
4990             vc1_smooth_overlap_filter_iblk(v);
4991             vc1_put_signed_blocks_clamped(v);
4992             if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4993
4994             if (get_bits_count(&s->gb) > v->bits) {
4995                 // TODO: may need modification to handle slice coding
4996                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4997                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4998                        get_bits_count(&s->gb), v->bits);
4999                 return;
5000             }
5001         }
5002         if (!v->s.loop_filter)
5003             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5004         else if (s->mb_y)
5005             ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
5006         s->first_slice_line = 0;
5007     }
5008
5009     /* raw bottom MB row */
5010     s->mb_x = 0;
5011     init_block_index(v);
5012
5013     for (;s->mb_x < s->mb_width; s->mb_x++) {
5014         ff_update_block_index(s);
5015         vc1_put_signed_blocks_clamped(v);
5016         if (v->s.loop_filter)
5017             vc1_loop_filter_iblk_delayed(v, v->pq);
5018     }
5019     if (v->s.loop_filter)
5020         ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5021     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5022                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5023 }
5024
5025 static void vc1_decode_p_blocks(VC1Context *v)
5026 {
5027     MpegEncContext *s = &v->s;
5028     int apply_loop_filter;
5029
5030     /* select codingmode used for VLC tables selection */
5031     switch (v->c_ac_table_index) {
5032     case 0:
5033         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5034         break;
5035     case 1:
5036         v->codingset = CS_HIGH_MOT_INTRA;
5037         break;
5038     case 2:
5039         v->codingset = CS_MID_RATE_INTRA;
5040         break;
5041     }
5042
5043     switch (v->c_ac_table_index) {
5044     case 0:
5045         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5046         break;
5047     case 1:
5048         v->codingset2 = CS_HIGH_MOT_INTER;
5049         break;
5050     case 2:
5051         v->codingset2 = CS_MID_RATE_INTER;
5052         break;
5053     }
5054
5055     apply_loop_filter   = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5056                           v->fcm == PROGRESSIVE;
5057     s->first_slice_line = 1;
5058     memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5059     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5060         s->mb_x = 0;
5061         init_block_index(v);
5062         for (; s->mb_x < s->mb_width; s->mb_x++) {
5063             ff_update_block_index(s);
5064
5065             if (v->fcm == ILACE_FIELD)
5066                 vc1_decode_p_mb_intfi(v);
5067             else if (v->fcm == ILACE_FRAME)
5068                 vc1_decode_p_mb_intfr(v);
5069             else vc1_decode_p_mb(v);
5070             if (s->mb_y != s->start_mb_y && apply_loop_filter)
5071                 vc1_apply_p_loop_filter(v);
5072             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5073                 // TODO: may need modification to handle slice coding
5074                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5075                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5076                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5077                 return;
5078             }
5079         }
5080         memmove(v->cbp_base,      v->cbp,      sizeof(v->cbp_base[0])      * s->mb_stride);
5081         memmove(v->ttblk_base,    v->ttblk,    sizeof(v->ttblk_base[0])    * s->mb_stride);
5082         memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5083         memmove(v->luma_mv_base,  v->luma_mv,  sizeof(v->luma_mv_base[0])  * s->mb_stride);
5084         if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5085         s->first_slice_line = 0;
5086     }
5087     if (apply_loop_filter) {
5088         s->mb_x = 0;
5089         init_block_index(v);
5090         for (; s->mb_x < s->mb_width; s->mb_x++) {
5091             ff_update_block_index(s);
5092             vc1_apply_p_loop_filter(v);
5093         }
5094     }
5095     if (s->end_mb_y >= s->start_mb_y)
5096         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5097     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5098                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5099 }
5100
5101 static void vc1_decode_b_blocks(VC1Context *v)
5102 {
5103     MpegEncContext *s = &v->s;
5104
5105     /* select codingmode used for VLC tables selection */
5106     switch (v->c_ac_table_index) {
5107     case 0:
5108         v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5109         break;
5110     case 1:
5111         v->codingset = CS_HIGH_MOT_INTRA;
5112         break;
5113     case 2:
5114         v->codingset = CS_MID_RATE_INTRA;
5115         break;
5116     }
5117
5118     switch (v->c_ac_table_index) {
5119     case 0:
5120         v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5121         break;
5122     case 1:
5123         v->codingset2 = CS_HIGH_MOT_INTER;
5124         break;
5125     case 2:
5126         v->codingset2 = CS_MID_RATE_INTER;
5127         break;
5128     }
5129
5130     s->first_slice_line = 1;
5131     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5132         s->mb_x = 0;
5133         init_block_index(v);
5134         for (; s->mb_x < s->mb_width; s->mb_x++) {
5135             ff_update_block_index(s);
5136
5137             if (v->fcm == ILACE_FIELD)
5138                 vc1_decode_b_mb_intfi(v);
5139             else if (v->fcm == ILACE_FRAME)
5140                 vc1_decode_b_mb_intfr(v);
5141             else
5142                 vc1_decode_b_mb(v);
5143             if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5144                 // TODO: may need modification to handle slice coding
5145                 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5146                 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5147                        get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5148                 return;
5149             }
5150             if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5151         }
5152         if (!v->s.loop_filter)
5153             ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5154         else if (s->mb_y)
5155             ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5156         s->first_slice_line = 0;
5157     }
5158     if (v->s.loop_filter)
5159         ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5160     ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5161                     (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5162 }
5163
5164 static void vc1_decode_skip_blocks(VC1Context *v)
5165 {
5166     MpegEncContext *s = &v->s;
5167
5168     if (!v->s.last_picture.f->data[0])
5169         return;
5170
5171     ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5172     s->first_slice_line = 1;
5173     for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5174         s->mb_x = 0;
5175         init_block_index(v);
5176         ff_update_block_index(s);
5177         memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize,   s->linesize   * 16);
5178         memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
5179         memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y *  8 * s->uvlinesize, s->uvlinesize *  8);
5180         ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5181         s->first_slice_line = 0;
5182     }
5183     s->pict_type = AV_PICTURE_TYPE_P;
5184 }
5185
5186 void ff_vc1_decode_blocks(VC1Context *v)
5187 {
5188
5189     v->s.esc3_level_length = 0;
5190     if (v->x8_type) {
5191         ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5192     } else {
5193         v->cur_blk_idx     =  0;
5194         v->left_blk_idx    = -1;
5195         v->topleft_blk_idx =  1;
5196         v->top_blk_idx     =  2;
5197         switch (v->s.pict_type) {
5198         case AV_PICTURE_TYPE_I:
5199             if (v->profile == PROFILE_ADVANCED)
5200                 vc1_decode_i_blocks_adv(v);
5201             else
5202                 vc1_decode_i_blocks(v);
5203             break;
5204         case AV_PICTURE_TYPE_P:
5205             if (v->p_frame_skipped)
5206                 vc1_decode_skip_blocks(v);
5207             else
5208                 vc1_decode_p_blocks(v);
5209             break;
5210         case AV_PICTURE_TYPE_B:
5211             if (v->bi_type) {
5212                 if (v->profile == PROFILE_ADVANCED)
5213                     vc1_decode_i_blocks_adv(v);
5214                 else
5215                     vc1_decode_i_blocks(v);
5216             } else
5217                 vc1_decode_b_blocks(v);
5218             break;
5219         }
5220     }
5221 }
5222
5223 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5224
5225 typedef struct {
5226     /**
5227      * Transform coefficients for both sprites in 16.16 fixed point format,
5228      * in the order they appear in the bitstream:
5229      *  x scale
5230      *  rotation 1 (unused)
5231      *  x offset
5232      *  rotation 2 (unused)
5233      *  y scale
5234      *  y offset
5235      *  alpha
5236      */
5237     int coefs[2][7];
5238
5239     int effect_type, effect_flag;
5240     int effect_pcount1, effect_pcount2;   ///< amount of effect parameters stored in effect_params
5241     int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5242 } SpriteData;
5243
5244 static inline int get_fp_val(GetBitContext* gb)
5245 {
5246     return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5247 }
5248
5249 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5250 {
5251     c[1] = c[3] = 0;
5252
5253     switch (get_bits(gb, 2)) {
5254     case 0:
5255         c[0] = 1 << 16;
5256         c[2] = get_fp_val(gb);
5257         c[4] = 1 << 16;
5258         break;
5259     case 1:
5260         c[0] = c[4] = get_fp_val(gb);
5261         c[2] = get_fp_val(gb);
5262         break;
5263     case 2:
5264         c[0] = get_fp_val(gb);
5265         c[2] = get_fp_val(gb);
5266         c[4] = get_fp_val(gb);
5267         break;
5268     case 3:
5269         c[0] = get_fp_val(gb);
5270         c[1] = get_fp_val(gb);
5271         c[2] = get_fp_val(gb);
5272         c[3] = get_fp_val(gb);
5273         c[4] = get_fp_val(gb);
5274         break;
5275     }
5276     c[5] = get_fp_val(gb);
5277     if (get_bits1(gb))
5278         c[6] = get_fp_val(gb);
5279     else
5280         c[6] = 1 << 16;
5281 }
5282
5283 static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5284 {
5285     AVCodecContext *avctx = v->s.avctx;
5286     int sprite, i;
5287
5288     for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5289         vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5290         if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5291             avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5292         av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5293         for (i = 0; i < 7; i++)
5294             av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5295                    sd->coefs[sprite][i] / (1<<16),
5296                    (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5297         av_log(avctx, AV_LOG_DEBUG, "\n");
5298     }
5299
5300     skip_bits(gb, 2);
5301     if (sd->effect_type = get_bits_long(gb, 30)) {
5302         switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5303         case 7:
5304             vc1_sprite_parse_transform(gb, sd->effect_params1);
5305             break;
5306         case 14:
5307             vc1_sprite_parse_transform(gb, sd->effect_params1);
5308             vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5309             break;
5310         default:
5311             for (i = 0; i < sd->effect_pcount1; i++)
5312                 sd->effect_params1[i] = get_fp_val(gb);
5313         }
5314         if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5315             // effect 13 is simple alpha blending and matches the opacity above
5316             av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5317             for (i = 0; i < sd->effect_pcount1; i++)
5318                 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5319                        sd->effect_params1[i] / (1 << 16),
5320                        (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5321             av_log(avctx, AV_LOG_DEBUG, "\n");
5322         }
5323
5324         sd->effect_pcount2 = get_bits(gb, 16);
5325         if (sd->effect_pcount2 > 10) {
5326             av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5327             return AVERROR_INVALIDDATA;
5328         } else if (sd->effect_pcount2) {
5329             i = -1;
5330             av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5331             while (++i < sd->effect_pcount2) {
5332                 sd->effect_params2[i] = get_fp_val(gb);
5333                 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5334                        sd->effect_params2[i] / (1 << 16),
5335                        (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5336             }
5337             av_log(avctx, AV_LOG_DEBUG, "\n");
5338         }
5339     }
5340     if (sd->effect_flag = get_bits1(gb))
5341         av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5342
5343     if (get_bits_count(gb) >= gb->size_in_bits +
5344        (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
5345         av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5346         return AVERROR_INVALIDDATA;
5347     }
5348     if (get_bits_count(gb) < gb->size_in_bits - 8)
5349         av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5350
5351     return 0;
5352 }
5353
5354 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5355 {
5356     int i, plane, row, sprite;
5357     int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5358     uint8_t* src_h[2][2];
5359     int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5360     int ysub[2];
5361     MpegEncContext *s = &v->s;
5362
5363     for (i = 0; i <= v->two_sprites; i++) {
5364         xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5365         xadv[i] = sd->coefs[i][0];
5366         if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5367             xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5368
5369         yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5370         yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5371     }
5372     alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5373
5374     for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5375         int width = v->output_width>>!!plane;
5376
5377         for (row = 0; row < v->output_height>>!!plane; row++) {
5378             uint8_t *dst = v->sprite_output_frame->data[plane] +
5379                            v->sprite_output_frame->linesize[plane] * row;
5380
5381             for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5382                 uint8_t *iplane = s->current_picture.f->data[plane];
5383                 int      iline  = s->current_picture.f->linesize[plane];
5384                 int      ycoord = yoff[sprite] + yadv[sprite] * row;
5385                 int      yline  = ycoord >> 16;
5386                 int      next_line;
5387                 ysub[sprite] = ycoord & 0xFFFF;
5388                 if (sprite) {
5389                     iplane = s->last_picture.f->data[plane];
5390                     iline  = s->last_picture.f->linesize[plane];
5391                 }
5392                 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5393                 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5394                         src_h[sprite][0] = iplane + (xoff[sprite] >> 16) +  yline      * iline;
5395                     if (ysub[sprite])
5396                         src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5397                 } else {
5398                     if (sr_cache[sprite][0] != yline) {
5399                         if (sr_cache[sprite][1] == yline) {
5400                             FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5401                             FFSWAP(int,        sr_cache[sprite][0],   sr_cache[sprite][1]);
5402                         } else {
5403                             v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5404                             sr_cache[sprite][0] = yline;
5405                         }
5406                     }
5407                     if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5408                         v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5409                                            iplane + next_line, xoff[sprite],
5410                                            xadv[sprite], width);
5411                         sr_cache[sprite][1] = yline + 1;
5412                     }
5413                     src_h[sprite][0] = v->sr_rows[sprite][0];
5414                     src_h[sprite][1] = v->sr_rows[sprite][1];
5415                 }
5416             }
5417
5418             if (!v->two_sprites) {
5419                 if (ysub[0]) {
5420                     v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5421                 } else {
5422                     memcpy(dst, src_h[0][0], width);
5423                 }
5424             } else {
5425                 if (ysub[0] && ysub[1]) {
5426                     v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5427                                                        src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5428                 } else if (ysub[0]) {
5429                     v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5430                                                        src_h[1][0], alpha, width);
5431                 } else if (ysub[1]) {
5432                     v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5433                                                        src_h[0][0], (1<<16)-1-alpha, width);
5434                 } else {
5435                     v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5436                 }
5437             }
5438         }
5439
5440         if (!plane) {
5441             for (i = 0; i <= v->two_sprites; i++) {
5442                 xoff[i] >>= 1;
5443                 yoff[i] >>= 1;
5444             }
5445         }
5446
5447     }
5448 }
5449
5450
5451 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5452 {
5453     int ret;
5454     MpegEncContext *s     = &v->s;
5455     AVCodecContext *avctx = s->avctx;
5456     SpriteData sd;
5457
5458     memset(&sd, 0, sizeof(sd));
5459
5460     ret = vc1_parse_sprites(v, gb, &sd);
5461     if (ret < 0)
5462         return ret;
5463
5464     if (!s->current_picture.f->data[0]) {
5465         av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5466         return -1;
5467     }
5468
5469     if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
5470         av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5471         v->two_sprites = 0;
5472     }
5473
5474     av_frame_unref(v->sprite_output_frame);
5475     if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
5476         return ret;
5477
5478     vc1_draw_sprites(v, &sd);
5479
5480     return 0;
5481 }
5482
5483 static void vc1_sprite_flush(AVCodecContext *avctx)
5484 {
5485     VC1Context *v     = avctx->priv_data;
5486     MpegEncContext *s = &v->s;
5487     AVFrame *f = s->current_picture.f;
5488     int plane, i;
5489
5490     /* Windows Media Image codecs have a convergence interval of two keyframes.
5491        Since we can't enforce it, clear to black the missing sprite. This is
5492        wrong but it looks better than doing nothing. */
5493
5494     if (f->data[0])
5495         for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5496             for (i = 0; i < v->sprite_height>>!!plane; i++)
5497                 memset(f->data[plane] + i * f->linesize[plane],
5498                        plane ? 128 : 0, f->linesize[plane]);
5499 }
5500
5501 #endif
5502
5503 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5504 {
5505     MpegEncContext *s = &v->s;
5506     int i;
5507     int mb_height = FFALIGN(s->mb_height, 2);
5508
5509     /* Allocate mb bitplanes */
5510     v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
5511     v->direct_mb_plane  = av_malloc (s->mb_stride * mb_height);
5512     v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
5513     v->fieldtx_plane    = av_mallocz(s->mb_stride * mb_height);
5514     v->acpred_plane     = av_malloc (s->mb_stride * mb_height);
5515     v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
5516
5517     v->n_allocated_blks = s->mb_width + 2;
5518     v->block            = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5519     v->cbp_base         = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5520     v->cbp              = v->cbp_base + s->mb_stride;
5521     v->ttblk_base       = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5522     v->ttblk            = v->ttblk_base + s->mb_stride;
5523     v->is_intra_base    = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5524     v->is_intra         = v->is_intra_base + s->mb_stride;
5525     v->luma_mv_base     = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5526     v->luma_mv          = v->luma_mv_base + s->mb_stride;
5527
5528     /* allocate block type info in that way so it could be used with s->block_index[] */
5529     v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5530     v->mb_type[0]   = v->mb_type_base + s->b8_stride + 1;
5531     v->mb_type[1]   = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
5532     v->mb_type[2]   = v->mb_type[1] + s->mb_stride * (mb_height + 1);
5533
5534     /* allocate memory to store block level MV info */
5535     v->blk_mv_type_base = av_mallocz(     s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5536     v->blk_mv_type      = v->blk_mv_type_base + s->b8_stride + 1;
5537     v->mv_f_base        = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5538     v->mv_f[0]          = v->mv_f_base + s->b8_stride + 1;
5539     v->mv_f[1]          = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5540     v->mv_f_next_base   = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5541     v->mv_f_next[0]     = v->mv_f_next_base + s->b8_stride + 1;
5542     v->mv_f_next[1]     = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5543
5544     /* Init coded blocks info */
5545     if (v->profile == PROFILE_ADVANCED) {
5546 //        if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5547 //            return -1;
5548 //        if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5549 //            return -1;
5550     }
5551
5552     ff_intrax8_common_init(&v->x8,s);
5553
5554     if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5555         for (i = 0; i < 4; i++)
5556             if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
5557                 return AVERROR(ENOMEM);
5558     }
5559
5560     if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5561         !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5562         !v->mb_type_base) {
5563         av_freep(&v->mv_type_mb_plane);
5564         av_freep(&v->direct_mb_plane);
5565         av_freep(&v->acpred_plane);
5566         av_freep(&v->over_flags_plane);
5567         av_freep(&v->block);
5568         av_freep(&v->cbp_base);
5569         av_freep(&v->ttblk_base);
5570         av_freep(&v->is_intra_base);
5571         av_freep(&v->luma_mv_base);
5572         av_freep(&v->mb_type_base);
5573         return AVERROR(ENOMEM);
5574     }
5575
5576     return 0;
5577 }
5578
5579 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5580 {
5581     int i;
5582     for (i = 0; i < 64; i++) {
5583 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
5584         v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5585         v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5586         v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5587         v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5588         v->zzi_8x8[i]   = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5589     }
5590     v->left_blk_sh = 0;
5591     v->top_blk_sh  = 3;
5592 }
5593
5594 /** Initialize a VC1/WMV3 decoder
5595  * @todo TODO: Handle VC-1 IDUs (Transport level?)
5596  * @todo TODO: Decypher remaining bits in extra_data
5597  */
5598 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5599 {
5600     VC1Context *v = avctx->priv_data;
5601     MpegEncContext *s = &v->s;
5602     GetBitContext gb;
5603     int ret;
5604
5605     /* save the container output size for WMImage */
5606     v->output_width  = avctx->width;
5607     v->output_height = avctx->height;
5608
5609     if (!avctx->extradata_size || !avctx->extradata)
5610         return -1;
5611     if (!(avctx->flags & CODEC_FLAG_GRAY))
5612         avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
5613     else
5614         avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5615     v->s.avctx = avctx;
5616
5617     if ((ret = ff_vc1_init_common(v)) < 0)
5618         return ret;
5619     // ensure static VLC tables are initialized
5620     if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
5621         return ret;
5622     if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
5623         return ret;
5624     // Hack to ensure the above functions will be called
5625     // again once we know all necessary settings.
5626     // That this is necessary might indicate a bug.
5627     ff_vc1_decode_end(avctx);
5628
5629     ff_blockdsp_init(&s->bdsp, avctx);
5630     ff_h264chroma_init(&v->h264chroma, 8);
5631     ff_qpeldsp_init(&s->qdsp);
5632
5633     if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5634         int count = 0;
5635
5636         // looks like WMV3 has a sequence header stored in the extradata
5637         // advanced sequence header may be before the first frame
5638         // the last byte of the extradata is a version number, 1 for the
5639         // samples we can decode
5640
5641         init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5642
5643         if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
5644           return ret;
5645
5646         count = avctx->extradata_size*8 - get_bits_count(&gb);
5647         if (count > 0) {
5648             av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5649                    count, get_bits(&gb, count));
5650         } else if (count < 0) {
5651             av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5652         }
5653     } else { // VC1/WVC1/WVP2
5654         const uint8_t *start = avctx->extradata;
5655         uint8_t *end = avctx->extradata + avctx->extradata_size;
5656         const uint8_t *next;
5657         int size, buf2_size;
5658         uint8_t *buf2 = NULL;
5659         int seq_initialized = 0, ep_initialized = 0;
5660
5661         if (avctx->extradata_size < 16) {
5662             av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5663             return -1;
5664         }
5665
5666         buf2  = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5667         start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5668         next  = start;
5669         for (; next < end; start = next) {
5670             next = find_next_marker(start + 4, end);
5671             size = next - start - 4;
5672             if (size <= 0)
5673                 continue;
5674             buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5675             init_get_bits(&gb, buf2, buf2_size * 8);
5676             switch (AV_RB32(start)) {
5677             case VC1_CODE_SEQHDR:
5678                 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
5679                     av_free(buf2);
5680                     return ret;
5681                 }
5682                 seq_initialized = 1;
5683                 break;
5684             case VC1_CODE_ENTRYPOINT:
5685                 if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
5686                     av_free(buf2);
5687                     return ret;
5688                 }
5689                 ep_initialized = 1;
5690                 break;
5691             }
5692         }
5693         av_free(buf2);
5694         if (!seq_initialized || !ep_initialized) {
5695             av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5696             return -1;
5697         }
5698         v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5699     }
5700
5701     v->sprite_output_frame = av_frame_alloc();
5702     if (!v->sprite_output_frame)
5703         return AVERROR(ENOMEM);
5704
5705     avctx->profile = v->profile;
5706     if (v->profile == PROFILE_ADVANCED)
5707         avctx->level = v->level;
5708
5709     avctx->has_b_frames = !!avctx->max_b_frames;
5710
5711     s->mb_width  = (avctx->coded_width  + 15) >> 4;
5712     s->mb_height = (avctx->coded_height + 15) >> 4;
5713
5714     if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5715         ff_vc1_init_transposed_scantables(v);
5716     } else {
5717         memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5718         v->left_blk_sh = 3;
5719         v->top_blk_sh  = 0;
5720     }
5721
5722     if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5723         v->sprite_width  = avctx->coded_width;
5724         v->sprite_height = avctx->coded_height;
5725
5726         avctx->coded_width  = avctx->width  = v->output_width;
5727         avctx->coded_height = avctx->height = v->output_height;
5728
5729         // prevent 16.16 overflows
5730         if (v->sprite_width  > 1 << 14 ||
5731             v->sprite_height > 1 << 14 ||
5732             v->output_width  > 1 << 14 ||
5733             v->output_height > 1 << 14) return -1;
5734
5735         if ((v->sprite_width&1) || (v->sprite_height&1)) {
5736             avpriv_request_sample(avctx, "odd sprites support");
5737             return AVERROR_PATCHWELCOME;
5738         }
5739     }
5740     return 0;
5741 }
5742
5743 /** Close a VC1/WMV3 decoder
5744  * @warning Initial try at using MpegEncContext stuff
5745  */
5746 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5747 {
5748     VC1Context *v = avctx->priv_data;
5749     int i;
5750
5751     av_frame_free(&v->sprite_output_frame);
5752
5753     for (i = 0; i < 4; i++)
5754         av_freep(&v->sr_rows[i >> 1][i & 1]);
5755     av_freep(&v->hrd_rate);
5756     av_freep(&v->hrd_buffer);
5757     ff_MPV_common_end(&v->s);
5758     av_freep(&v->mv_type_mb_plane);
5759     av_freep(&v->direct_mb_plane);
5760     av_freep(&v->forward_mb_plane);
5761     av_freep(&v->fieldtx_plane);
5762     av_freep(&v->acpred_plane);
5763     av_freep(&v->over_flags_plane);
5764     av_freep(&v->mb_type_base);
5765     av_freep(&v->blk_mv_type_base);
5766     av_freep(&v->mv_f_base);
5767     av_freep(&v->mv_f_next_base);
5768     av_freep(&v->block);
5769     av_freep(&v->cbp_base);
5770     av_freep(&v->ttblk_base);
5771     av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5772     av_freep(&v->luma_mv_base);
5773     ff_intrax8_common_end(&v->x8);
5774     return 0;
5775 }
5776
5777
5778 /** Decode a VC1/WMV3 frame
5779  * @todo TODO: Handle VC-1 IDUs (Transport level?)
5780  */
5781 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5782                             int *got_frame, AVPacket *avpkt)
5783 {
5784     const uint8_t *buf = avpkt->data;
5785     int buf_size = avpkt->size, n_slices = 0, i, ret;
5786     VC1Context *v = avctx->priv_data;
5787     MpegEncContext *s = &v->s;
5788     AVFrame *pict = data;
5789     uint8_t *buf2 = NULL;
5790     const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5791     int mb_height, n_slices1=-1;
5792     struct {
5793         uint8_t *buf;
5794         GetBitContext gb;
5795         int mby_start;
5796     } *slices = NULL, *tmp;
5797
5798     v->second_field = 0;
5799
5800     if(s->flags & CODEC_FLAG_LOW_DELAY)
5801         s->low_delay = 1;
5802
5803     /* no supplementary picture */
5804     if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5805         /* special case for last picture */
5806         if (s->low_delay == 0 && s->next_picture_ptr) {
5807             if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
5808                 return ret;
5809             s->next_picture_ptr = NULL;
5810
5811             *got_frame = 1;
5812         }
5813
5814         return buf_size;
5815     }
5816
5817     if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5818         if (v->profile < PROFILE_ADVANCED)
5819             avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5820         else
5821             avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5822     }
5823
5824     //for advanced profile we may need to parse and unescape data
5825     if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5826         int buf_size2 = 0;
5827         buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5828         if (!buf2)
5829             return AVERROR(ENOMEM);
5830
5831         if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5832             const uint8_t *start, *end, *next;
5833             int size;
5834
5835             next = buf;
5836             for (start = buf, end = buf + buf_size; next < end; start = next) {
5837                 next = find_next_marker(start + 4, end);
5838                 size = next - start - 4;
5839                 if (size <= 0) continue;
5840                 switch (AV_RB32(start)) {
5841                 case VC1_CODE_FRAME:
5842                     if (avctx->hwaccel ||
5843                         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5844                         buf_start = start;
5845                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5846                     break;
5847                 case VC1_CODE_FIELD: {
5848                     int buf_size3;
5849                     if (avctx->hwaccel ||
5850                         s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5851                         buf_start_second_field = start;
5852                     tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5853                     if (!tmp)
5854                         goto err;
5855                     slices = tmp;
5856                     slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5857                     if (!slices[n_slices].buf)
5858                         goto err;
5859                     buf_size3 = vc1_unescape_buffer(start + 4, size,
5860                                                     slices[n_slices].buf);
5861                     init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5862                                   buf_size3 << 3);
5863                     /* assuming that the field marker is at the exact middle,
5864                        hope it's correct */
5865                     slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5866                     n_slices1 = n_slices - 1; // index of the last slice of the first field
5867                     n_slices++;
5868                     break;
5869                 }
5870                 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5871                     buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5872                     init_get_bits(&s->gb, buf2, buf_size2 * 8);
5873                     ff_vc1_decode_entry_point(avctx, v, &s->gb);
5874                     break;
5875                 case VC1_CODE_SLICE: {
5876                     int buf_size3;
5877                     tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5878                     if (!tmp)
5879                         goto err;
5880                     slices = tmp;
5881                     slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5882                     if (!slices[n_slices].buf)
5883                         goto err;
5884                     buf_size3 = vc1_unescape_buffer(start + 4, size,
5885                                                     slices[n_slices].buf);
5886                     init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5887                                   buf_size3 << 3);
5888                     slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5889                     n_slices++;
5890                     break;
5891                 }
5892                 }
5893             }
5894         } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5895             const uint8_t *divider;
5896             int buf_size3;
5897
5898             divider = find_next_marker(buf, buf + buf_size);
5899             if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5900                 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5901                 goto err;
5902             } else { // found field marker, unescape second field
5903                 if (avctx->hwaccel ||
5904                     s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5905                     buf_start_second_field = divider;
5906                 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5907                 if (!tmp)
5908                     goto err;
5909                 slices = tmp;
5910                 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5911                 if (!slices[n_slices].buf)
5912                     goto err;
5913                 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5914                 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5915                               buf_size3 << 3);
5916                 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5917                 n_slices1 = n_slices - 1;
5918                 n_slices++;
5919             }
5920             buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5921         } else {
5922             buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5923         }
5924         init_get_bits(&s->gb, buf2, buf_size2*8);
5925     } else
5926         init_get_bits(&s->gb, buf, buf_size*8);
5927
5928     if (v->res_sprite) {
5929         v->new_sprite  = !get_bits1(&s->gb);
5930         v->two_sprites =  get_bits1(&s->gb);
5931         /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5932            we're using the sprite compositor. These are intentionally kept separate
5933            so you can get the raw sprites by using the wmv3 decoder for WMVP or
5934            the vc1 one for WVP2 */
5935         if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5936             if (v->new_sprite) {
5937                 // switch AVCodecContext parameters to those of the sprites
5938                 avctx->width  = avctx->coded_width  = v->sprite_width;
5939                 avctx->height = avctx->coded_height = v->sprite_height;
5940             } else {
5941                 goto image;
5942             }
5943         }
5944     }
5945
5946     if (s->context_initialized &&
5947         (s->width  != avctx->coded_width ||
5948          s->height != avctx->coded_height)) {
5949         ff_vc1_decode_end(avctx);
5950     }
5951
5952     if (!s->context_initialized) {
5953         if (ff_msmpeg4_decode_init(avctx) < 0)
5954             goto err;
5955         if (ff_vc1_decode_init_alloc_tables(v) < 0) {
5956             ff_MPV_common_end(s);
5957             goto err;
5958         }
5959
5960         s->low_delay = !avctx->has_b_frames || v->res_sprite;
5961
5962         if (v->profile == PROFILE_ADVANCED) {
5963             if(avctx->coded_width<=1 || avctx->coded_height<=1)
5964                 goto err;
5965             s->h_edge_pos = avctx->coded_width;
5966             s->v_edge_pos = avctx->coded_height;
5967         }
5968     }
5969
5970     // do parse frame header
5971     v->pic_header_flag = 0;
5972     v->first_pic_header_flag = 1;
5973     if (v->profile < PROFILE_ADVANCED) {
5974         if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5975             goto err;
5976         }
5977     } else {
5978         if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5979             goto err;
5980         }
5981     }
5982     v->first_pic_header_flag = 0;
5983
5984     if (avctx->debug & FF_DEBUG_PICT_INFO)
5985         av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5986
5987     if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5988         && s->pict_type != AV_PICTURE_TYPE_I) {
5989         av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5990         goto err;
5991     }
5992
5993     if ((s->mb_height >> v->field_mode) == 0) {
5994         av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5995         goto err;
5996     }
5997
5998     // for skipping the frame
5999     s->current_picture.f->pict_type = s->pict_type;
6000     s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
6001
6002     /* skip B-frames if we don't have reference frames */
6003     if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
6004         av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
6005         goto end;
6006     }
6007     if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
6008         (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
6009          avctx->skip_frame >= AVDISCARD_ALL) {
6010         goto end;
6011     }
6012
6013     if (s->next_p_frame_damaged) {
6014         if (s->pict_type == AV_PICTURE_TYPE_B)
6015             goto end;
6016         else
6017             s->next_p_frame_damaged = 0;
6018     }
6019
6020     if (ff_MPV_frame_start(s, avctx) < 0) {
6021         goto err;
6022     }
6023
6024     v->s.current_picture_ptr->field_picture = v->field_mode;
6025     v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
6026     v->s.current_picture_ptr->f->top_field_first  = v->tff;
6027
6028     // process pulldown flags
6029     s->current_picture_ptr->f->repeat_pict = 0;
6030     // Pulldown flags are only valid when 'broadcast' has been set.
6031     // So ticks_per_frame will be 2
6032     if (v->rff) {
6033         // repeat field
6034         s->current_picture_ptr->f->repeat_pict = 1;
6035     } else if (v->rptfrm) {
6036         // repeat frames
6037         s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
6038     }
6039
6040     s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
6041     s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
6042
6043     if ((CONFIG_VC1_VDPAU_DECODER)
6044         &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6045         if (v->field_mode && buf_start_second_field) {
6046             ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6047             ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6048         } else {
6049             ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6050         }
6051     } else if (avctx->hwaccel) {
6052         if (v->field_mode && buf_start_second_field) {
6053             // decode first field
6054             s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6055             if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6056                 goto err;
6057             if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6058                 goto err;
6059             if (avctx->hwaccel->end_frame(avctx) < 0)
6060                 goto err;
6061
6062             // decode second field
6063             s->gb = slices[n_slices1 + 1].gb;
6064             s->picture_structure = PICT_TOP_FIELD + v->tff;
6065             v->second_field = 1;
6066             v->pic_header_flag = 0;
6067             if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6068                 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6069                 goto err;
6070             }
6071             v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
6072
6073             if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6074                 goto err;
6075             if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6076                 goto err;
6077             if (avctx->hwaccel->end_frame(avctx) < 0)
6078                 goto err;
6079         } else {
6080             s->picture_structure = PICT_FRAME;
6081             if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6082                 goto err;
6083             if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6084                 goto err;
6085             if (avctx->hwaccel->end_frame(avctx) < 0)
6086                 goto err;
6087         }
6088     } else {
6089         int header_ret = 0;
6090
6091         ff_mpeg_er_frame_start(s);
6092
6093         v->bits = buf_size * 8;
6094         v->end_mb_x = s->mb_width;
6095         if (v->field_mode) {
6096             s->current_picture.f->linesize[0] <<= 1;
6097             s->current_picture.f->linesize[1] <<= 1;
6098             s->current_picture.f->linesize[2] <<= 1;
6099             s->linesize                      <<= 1;
6100             s->uvlinesize                    <<= 1;
6101         }
6102         mb_height = s->mb_height >> v->field_mode;
6103
6104         av_assert0 (mb_height > 0);
6105
6106         for (i = 0; i <= n_slices; i++) {
6107             if (i > 0 &&  slices[i - 1].mby_start >= mb_height) {
6108                 if (v->field_mode <= 0) {
6109                     av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6110                            "picture boundary (%d >= %d)\n", i,
6111                            slices[i - 1].mby_start, mb_height);
6112                     continue;
6113                 }
6114                 v->second_field = 1;
6115                 av_assert0((s->mb_height & 1) == 0);
6116                 v->blocks_off   = s->b8_stride * (s->mb_height&~1);
6117                 v->mb_off       = s->mb_stride * s->mb_height >> 1;
6118             } else {
6119                 v->second_field = 0;
6120                 v->blocks_off   = 0;
6121                 v->mb_off       = 0;
6122             }
6123             if (i) {
6124                 v->pic_header_flag = 0;
6125                 if (v->field_mode && i == n_slices1 + 2) {
6126                     if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6127                         av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6128                         if (avctx->err_recognition & AV_EF_EXPLODE)
6129                             goto err;
6130                         continue;
6131                     }
6132                 } else if (get_bits1(&s->gb)) {
6133                     v->pic_header_flag = 1;
6134                     if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6135                         av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6136                         if (avctx->err_recognition & AV_EF_EXPLODE)
6137                             goto err;
6138                         continue;
6139                     }
6140                 }
6141             }
6142             if (header_ret < 0)
6143                 continue;
6144             s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6145             if (!v->field_mode || v->second_field)
6146                 s->end_mb_y = (i == n_slices     ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6147             else {
6148                 if (i >= n_slices) {
6149                     av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6150                     continue;
6151                 }
6152                 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6153             }
6154             if (s->end_mb_y <= s->start_mb_y) {
6155                 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6156                 continue;
6157             }
6158             if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6159                 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6160                 continue;
6161             }
6162             ff_vc1_decode_blocks(v);
6163             if (i != n_slices)
6164                 s->gb = slices[i].gb;
6165         }
6166         if (v->field_mode) {
6167             v->second_field = 0;
6168             s->current_picture.f->linesize[0] >>= 1;
6169             s->current_picture.f->linesize[1] >>= 1;
6170             s->current_picture.f->linesize[2] >>= 1;
6171             s->linesize                      >>= 1;
6172             s->uvlinesize                    >>= 1;
6173             if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6174                 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6175                 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6176             }
6177         }
6178         av_dlog(s->avctx, "Consumed %i/%i bits\n",
6179                 get_bits_count(&s->gb), s->gb.size_in_bits);
6180 //  if (get_bits_count(&s->gb) > buf_size * 8)
6181 //      return -1;
6182         if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6183             goto err;
6184         if (!v->field_mode)
6185             ff_er_frame_end(&s->er);
6186     }
6187
6188     ff_MPV_frame_end(s);
6189
6190     if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6191 image:
6192         avctx->width  = avctx->coded_width  = v->output_width;
6193         avctx->height = avctx->coded_height = v->output_height;
6194         if (avctx->skip_frame >= AVDISCARD_NONREF)
6195             goto end;
6196 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6197         if (vc1_decode_sprites(v, &s->gb))
6198             goto err;
6199 #endif
6200         if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
6201             goto err;
6202         *got_frame = 1;
6203     } else {
6204         if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6205             if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
6206                 goto err;
6207             ff_print_debug_info(s, s->current_picture_ptr, pict);
6208             *got_frame = 1;
6209         } else if (s->last_picture_ptr != NULL) {
6210             if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
6211                 goto err;
6212             ff_print_debug_info(s, s->last_picture_ptr, pict);
6213             *got_frame = 1;
6214         }
6215     }
6216
6217 end:
6218     av_free(buf2);
6219     for (i = 0; i < n_slices; i++)
6220         av_free(slices[i].buf);
6221     av_free(slices);
6222     return buf_size;
6223
6224 err:
6225     av_free(buf2);
6226     for (i = 0; i < n_slices; i++)
6227         av_free(slices[i].buf);
6228     av_free(slices);
6229     return -1;
6230 }
6231
6232
6233 static const AVProfile profiles[] = {
6234     { FF_PROFILE_VC1_SIMPLE,   "Simple"   },
6235     { FF_PROFILE_VC1_MAIN,     "Main"     },
6236     { FF_PROFILE_VC1_COMPLEX,  "Complex"  },
6237     { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6238     { FF_PROFILE_UNKNOWN },
6239 };
6240
6241 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6242 #if CONFIG_VC1_DXVA2_HWACCEL
6243     AV_PIX_FMT_DXVA2_VLD,
6244 #endif
6245 #if CONFIG_VC1_VAAPI_HWACCEL
6246     AV_PIX_FMT_VAAPI_VLD,
6247 #endif
6248 #if CONFIG_VC1_VDPAU_HWACCEL
6249     AV_PIX_FMT_VDPAU,
6250 #endif
6251     AV_PIX_FMT_YUV420P,
6252     AV_PIX_FMT_NONE
6253 };
6254
6255 AVCodec ff_vc1_decoder = {
6256     .name           = "vc1",
6257     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6258     .type           = AVMEDIA_TYPE_VIDEO,
6259     .id             = AV_CODEC_ID_VC1,
6260     .priv_data_size = sizeof(VC1Context),
6261     .init           = vc1_decode_init,
6262     .close          = ff_vc1_decode_end,
6263     .decode         = vc1_decode_frame,
6264     .flush          = ff_mpeg_flush,
6265     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6266     .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
6267     .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6268 };
6269
6270 #if CONFIG_WMV3_DECODER
6271 AVCodec ff_wmv3_decoder = {
6272     .name           = "wmv3",
6273     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6274     .type           = AVMEDIA_TYPE_VIDEO,
6275     .id             = AV_CODEC_ID_WMV3,
6276     .priv_data_size = sizeof(VC1Context),
6277     .init           = vc1_decode_init,
6278     .close          = ff_vc1_decode_end,
6279     .decode         = vc1_decode_frame,
6280     .flush          = ff_mpeg_flush,
6281     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6282     .pix_fmts       = vc1_hwaccel_pixfmt_list_420,
6283     .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6284 };
6285 #endif
6286
6287 #if CONFIG_WMV3_VDPAU_DECODER
6288 AVCodec ff_wmv3_vdpau_decoder = {
6289     .name           = "wmv3_vdpau",
6290     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6291     .type           = AVMEDIA_TYPE_VIDEO,
6292     .id             = AV_CODEC_ID_WMV3,
6293     .priv_data_size = sizeof(VC1Context),
6294     .init           = vc1_decode_init,
6295     .close          = ff_vc1_decode_end,
6296     .decode         = vc1_decode_frame,
6297     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6298     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6299     .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6300 };
6301 #endif
6302
6303 #if CONFIG_VC1_VDPAU_DECODER
6304 AVCodec ff_vc1_vdpau_decoder = {
6305     .name           = "vc1_vdpau",
6306     .long_name      = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6307     .type           = AVMEDIA_TYPE_VIDEO,
6308     .id             = AV_CODEC_ID_VC1,
6309     .priv_data_size = sizeof(VC1Context),
6310     .init           = vc1_decode_init,
6311     .close          = ff_vc1_decode_end,
6312     .decode         = vc1_decode_frame,
6313     .capabilities   = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6314     .pix_fmts       = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6315     .profiles       = NULL_IF_CONFIG_SMALL(profiles)
6316 };
6317 #endif
6318
6319 #if CONFIG_WMV3IMAGE_DECODER
6320 AVCodec ff_wmv3image_decoder = {
6321     .name           = "wmv3image",
6322     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6323     .type           = AVMEDIA_TYPE_VIDEO,
6324     .id             = AV_CODEC_ID_WMV3IMAGE,
6325     .priv_data_size = sizeof(VC1Context),
6326     .init           = vc1_decode_init,
6327     .close          = ff_vc1_decode_end,
6328     .decode         = vc1_decode_frame,
6329     .capabilities   = CODEC_CAP_DR1,
6330     .flush          = vc1_sprite_flush,
6331     .pix_fmts       = (const enum AVPixelFormat[]) {
6332         AV_PIX_FMT_YUV420P,
6333         AV_PIX_FMT_NONE
6334     },
6335 };
6336 #endif
6337
6338 #if CONFIG_VC1IMAGE_DECODER
6339 AVCodec ff_vc1image_decoder = {
6340     .name           = "vc1image",
6341     .long_name      = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6342     .type           = AVMEDIA_TYPE_VIDEO,
6343     .id             = AV_CODEC_ID_VC1IMAGE,
6344     .priv_data_size = sizeof(VC1Context),
6345     .init           = vc1_decode_init,
6346     .close          = ff_vc1_decode_end,
6347     .decode         = vc1_decode_frame,
6348     .capabilities   = CODEC_CAP_DR1,
6349     .flush          = vc1_sprite_flush,
6350     .pix_fmts       = (const enum AVPixelFormat[]) {
6351         AV_PIX_FMT_YUV420P,
6352         AV_PIX_FMT_NONE
6353     },
6354 };
6355 #endif