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