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