96055e664cb028d7609f799f52c8ecd99efc4256
[libav.git] / libavcodec / vc1dec.c
1 /*
2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 /**
24 * @file libavcodec/vc1dec.c
25 * VC-1 and WMV3 decoder
26 *
27 */
28 #include "internal.h"
29 #include "dsputil.h"
30 #include "avcodec.h"
31 #include "mpegvideo.h"
32 #include "vc1.h"
33 #include "vc1data.h"
34 #include "vc1acdata.h"
35 #include "msmpeg4data.h"
36 #include "unary.h"
37 #include "simple_idct.h"
38 #include "mathops.h"
39 #include "vdpau_internal.h"
40
41 #undef NDEBUG
42 #include <assert.h>
43
44 #define MB_INTRA_VLC_BITS 9
45 #define DC_VLC_BITS 9
46 #define AC_VLC_BITS 9
47 static const uint16_t table_mb_intra[64][2];
48
49
50 static const uint16_t vlc_offs[] = {
51 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
52 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620,
53 9262, 10202, 10756, 11310, 12228, 15078
54 };
55
56 /**
57 * Init VC-1 specific tables and VC1Context members
58 * @param v The VC1Context to initialize
59 * @return Status
60 */
61 static int vc1_init_common(VC1Context *v)
62 {
63 static int done = 0;
64 int i = 0;
65 static VLC_TYPE vlc_table[15078][2];
66
67 v->hrd_rate = v->hrd_buffer = NULL;
68
69 /* VLC tables */
70 if(!done)
71 {
72 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
73 ff_vc1_bfraction_bits, 1, 1,
74 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
75 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
76 ff_vc1_norm2_bits, 1, 1,
77 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
78 INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
79 ff_vc1_norm6_bits, 1, 1,
80 ff_vc1_norm6_codes, 2, 2, 556);
81 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
82 ff_vc1_imode_bits, 1, 1,
83 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
84 for (i=0; i<3; i++)
85 {
86 ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]];
87 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0];
88 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
89 ff_vc1_ttmb_bits[i], 1, 1,
90 ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
91 ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]];
92 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1];
93 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
94 ff_vc1_ttblk_bits[i], 1, 1,
95 ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
96 ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]];
97 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2];
98 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
99 ff_vc1_subblkpat_bits[i], 1, 1,
100 ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
101 }
102 for(i=0; i<4; i++)
103 {
104 ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]];
105 ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9];
106 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
107 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
108 ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
109 ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]];
110 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10];
111 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
112 ff_vc1_cbpcy_p_bits[i], 1, 1,
113 ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
114 ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]];
115 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11];
116 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
117 ff_vc1_mv_diff_bits[i], 1, 1,
118 ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
119 }
120 for(i=0; i<8; i++){
121 ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
122 ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
123 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
124 &vc1_ac_tables[i][0][1], 8, 4,
125 &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
126 }
127 //FIXME: switching to INIT_VLC_STATIC() results in incorrect decoding
128 init_vlc(&ff_msmp4_mb_i_vlc, MB_INTRA_VLC_BITS, 64,
129 &ff_msmp4_mb_i_table[0][1], 4, 2,
130 &ff_msmp4_mb_i_table[0][0], 4, 2, INIT_VLC_USE_STATIC);
131 done = 1;
132 }
133
134 /* Other defaults */
135 v->pq = -1;
136 v->mvrange = 0; /* 7.1.1.18, p80 */
137
138 return 0;
139 }
140
141 /***********************************************************************/
142 /**
143 * @defgroup vc1bitplane VC-1 Bitplane decoding
144 * @see 8.7, p56
145 * @{
146 */
147
148 /**
149 * Imode types
150 * @{
151 */
152 enum Imode {
153 IMODE_RAW,
154 IMODE_NORM2,
155 IMODE_DIFF2,
156 IMODE_NORM6,
157 IMODE_DIFF6,
158 IMODE_ROWSKIP,
159 IMODE_COLSKIP
160 };
161 /** @} */ //imode defines
162
163
164 /** @} */ //Bitplane group
165
166 static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
167 {
168 int i, j;
169 if(!s->first_slice_line)
170 s->dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
171 s->dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
172 for(i = !s->mb_x*8; i < 16; i += 8)
173 s->dsp.vc1_h_loop_filter16(s->dest[0] + i, s->linesize, pq);
174 for(j = 0; j < 2; j++){
175 if(!s->first_slice_line)
176 s->dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
177 if(s->mb_x)
178 s->dsp.vc1_h_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
179 }
180 }
181
182 /** Put block onto picture
183 */
184 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
185 {
186 uint8_t *Y;
187 int ys, us, vs;
188 DSPContext *dsp = &v->s.dsp;
189
190 if(v->rangeredfrm) {
191 int i, j, k;
192 for(k = 0; k < 6; k++)
193 for(j = 0; j < 8; j++)
194 for(i = 0; i < 8; i++)
195 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
196
197 }
198 ys = v->s.current_picture.linesize[0];
199 us = v->s.current_picture.linesize[1];
200 vs = v->s.current_picture.linesize[2];
201 Y = v->s.dest[0];
202
203 dsp->put_pixels_clamped(block[0], Y, ys);
204 dsp->put_pixels_clamped(block[1], Y + 8, ys);
205 Y += ys * 8;
206 dsp->put_pixels_clamped(block[2], Y, ys);
207 dsp->put_pixels_clamped(block[3], Y + 8, ys);
208
209 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
210 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
211 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
212 }
213 }
214
215 /** Do motion compensation over 1 macroblock
216 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
217 */
218 static void vc1_mc_1mv(VC1Context *v, int dir)
219 {
220 MpegEncContext *s = &v->s;
221 DSPContext *dsp = &v->s.dsp;
222 uint8_t *srcY, *srcU, *srcV;
223 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
224
225 if(!v->s.last_picture.data[0])return;
226
227 mx = s->mv[dir][0][0];
228 my = s->mv[dir][0][1];
229
230 // store motion vectors for further use in B frames
231 if(s->pict_type == FF_P_TYPE) {
232 s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
233 s->current_picture.motion_val[1][s->block_index[0]][1] = my;
234 }
235 uvmx = (mx + ((mx & 3) == 3)) >> 1;
236 uvmy = (my + ((my & 3) == 3)) >> 1;
237 if(v->fastuvmc) {
238 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
239 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
240 }
241 if(!dir) {
242 srcY = s->last_picture.data[0];
243 srcU = s->last_picture.data[1];
244 srcV = s->last_picture.data[2];
245 } else {
246 srcY = s->next_picture.data[0];
247 srcU = s->next_picture.data[1];
248 srcV = s->next_picture.data[2];
249 }
250
251 src_x = s->mb_x * 16 + (mx >> 2);
252 src_y = s->mb_y * 16 + (my >> 2);
253 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
254 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
255
256 if(v->profile != PROFILE_ADVANCED){
257 src_x = av_clip( src_x, -16, s->mb_width * 16);
258 src_y = av_clip( src_y, -16, s->mb_height * 16);
259 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
260 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
261 }else{
262 src_x = av_clip( src_x, -17, s->avctx->coded_width);
263 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
264 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
265 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
266 }
267
268 srcY += src_y * s->linesize + src_x;
269 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
270 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
271
272 /* for grayscale we should not try to read from unknown area */
273 if(s->flags & CODEC_FLAG_GRAY) {
274 srcU = s->edge_emu_buffer + 18 * s->linesize;
275 srcV = s->edge_emu_buffer + 18 * s->linesize;
276 }
277
278 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
279 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
280 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
281 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
282
283 srcY -= s->mspel * (1 + s->linesize);
284 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
285 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
286 srcY = s->edge_emu_buffer;
287 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
288 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
289 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
290 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
291 srcU = uvbuf;
292 srcV = uvbuf + 16;
293 /* if we deal with range reduction we need to scale source blocks */
294 if(v->rangeredfrm) {
295 int i, j;
296 uint8_t *src, *src2;
297
298 src = srcY;
299 for(j = 0; j < 17 + s->mspel*2; j++) {
300 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
301 src += s->linesize;
302 }
303 src = srcU; src2 = srcV;
304 for(j = 0; j < 9; j++) {
305 for(i = 0; i < 9; i++) {
306 src[i] = ((src[i] - 128) >> 1) + 128;
307 src2[i] = ((src2[i] - 128) >> 1) + 128;
308 }
309 src += s->uvlinesize;
310 src2 += s->uvlinesize;
311 }
312 }
313 /* if we deal with intensity compensation we need to scale source blocks */
314 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
315 int i, j;
316 uint8_t *src, *src2;
317
318 src = srcY;
319 for(j = 0; j < 17 + s->mspel*2; j++) {
320 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
321 src += s->linesize;
322 }
323 src = srcU; src2 = srcV;
324 for(j = 0; j < 9; j++) {
325 for(i = 0; i < 9; i++) {
326 src[i] = v->lutuv[src[i]];
327 src2[i] = v->lutuv[src2[i]];
328 }
329 src += s->uvlinesize;
330 src2 += s->uvlinesize;
331 }
332 }
333 srcY += s->mspel * (1 + s->linesize);
334 }
335
336 if(s->mspel) {
337 dxy = ((my & 3) << 2) | (mx & 3);
338 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
339 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
340 srcY += s->linesize * 8;
341 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
342 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
343 } else { // hpel mc - always used for luma
344 dxy = (my & 2) | ((mx & 2) >> 1);
345
346 if(!v->rnd)
347 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
348 else
349 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
350 }
351
352 if(s->flags & CODEC_FLAG_GRAY) return;
353 /* Chroma MC always uses qpel bilinear */
354 uvmx = (uvmx&3)<<1;
355 uvmy = (uvmy&3)<<1;
356 if(!v->rnd){
357 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
358 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
359 }else{
360 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
361 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
362 }
363 }
364
365 /** Do motion compensation for 4-MV macroblock - luminance block
366 */
367 static void vc1_mc_4mv_luma(VC1Context *v, int n)
368 {
369 MpegEncContext *s = &v->s;
370 DSPContext *dsp = &v->s.dsp;
371 uint8_t *srcY;
372 int dxy, mx, my, src_x, src_y;
373 int off;
374
375 if(!v->s.last_picture.data[0])return;
376 mx = s->mv[0][n][0];
377 my = s->mv[0][n][1];
378 srcY = s->last_picture.data[0];
379
380 off = s->linesize * 4 * (n&2) + (n&1) * 8;
381
382 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
383 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
384
385 if(v->profile != PROFILE_ADVANCED){
386 src_x = av_clip( src_x, -16, s->mb_width * 16);
387 src_y = av_clip( src_y, -16, s->mb_height * 16);
388 }else{
389 src_x = av_clip( src_x, -17, s->avctx->coded_width);
390 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
391 }
392
393 srcY += src_y * s->linesize + src_x;
394
395 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
396 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
397 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
398 srcY -= s->mspel * (1 + s->linesize);
399 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
400 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
401 srcY = s->edge_emu_buffer;
402 /* if we deal with range reduction we need to scale source blocks */
403 if(v->rangeredfrm) {
404 int i, j;
405 uint8_t *src;
406
407 src = srcY;
408 for(j = 0; j < 9 + s->mspel*2; j++) {
409 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
410 src += s->linesize;
411 }
412 }
413 /* if we deal with intensity compensation we need to scale source blocks */
414 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
415 int i, j;
416 uint8_t *src;
417
418 src = srcY;
419 for(j = 0; j < 9 + s->mspel*2; j++) {
420 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
421 src += s->linesize;
422 }
423 }
424 srcY += s->mspel * (1 + s->linesize);
425 }
426
427 if(s->mspel) {
428 dxy = ((my & 3) << 2) | (mx & 3);
429 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
430 } else { // hpel mc - always used for luma
431 dxy = (my & 2) | ((mx & 2) >> 1);
432 if(!v->rnd)
433 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
434 else
435 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
436 }
437 }
438
439 static inline int median4(int a, int b, int c, int d)
440 {
441 if(a < b) {
442 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
443 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
444 } else {
445 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
446 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
447 }
448 }
449
450
451 /** Do motion compensation for 4-MV macroblock - both chroma blocks
452 */
453 static void vc1_mc_4mv_chroma(VC1Context *v)
454 {
455 MpegEncContext *s = &v->s;
456 DSPContext *dsp = &v->s.dsp;
457 uint8_t *srcU, *srcV;
458 int uvmx, uvmy, uvsrc_x, uvsrc_y;
459 int i, idx, tx = 0, ty = 0;
460 int mvx[4], mvy[4], intra[4];
461 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
462
463 if(!v->s.last_picture.data[0])return;
464 if(s->flags & CODEC_FLAG_GRAY) return;
465
466 for(i = 0; i < 4; i++) {
467 mvx[i] = s->mv[0][i][0];
468 mvy[i] = s->mv[0][i][1];
469 intra[i] = v->mb_type[0][s->block_index[i]];
470 }
471
472 /* calculate chroma MV vector from four luma MVs */
473 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
474 if(!idx) { // all blocks are inter
475 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
476 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
477 } else if(count[idx] == 1) { // 3 inter blocks
478 switch(idx) {
479 case 0x1:
480 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
481 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
482 break;
483 case 0x2:
484 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
485 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
486 break;
487 case 0x4:
488 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
489 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
490 break;
491 case 0x8:
492 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
493 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
494 break;
495 }
496 } else if(count[idx] == 2) {
497 int t1 = 0, t2 = 0;
498 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
499 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
500 tx = (mvx[t1] + mvx[t2]) / 2;
501 ty = (mvy[t1] + mvy[t2]) / 2;
502 } else {
503 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
504 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
505 return; //no need to do MC for inter blocks
506 }
507
508 s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
509 s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
510 uvmx = (tx + ((tx&3) == 3)) >> 1;
511 uvmy = (ty + ((ty&3) == 3)) >> 1;
512 if(v->fastuvmc) {
513 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
514 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
515 }
516
517 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
518 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
519
520 if(v->profile != PROFILE_ADVANCED){
521 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
522 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
523 }else{
524 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
525 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
526 }
527
528 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
529 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
530 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
531 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
532 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
533 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
534 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
535 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
536 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
537 srcU = s->edge_emu_buffer;
538 srcV = s->edge_emu_buffer + 16;
539
540 /* if we deal with range reduction we need to scale source blocks */
541 if(v->rangeredfrm) {
542 int i, j;
543 uint8_t *src, *src2;
544
545 src = srcU; src2 = srcV;
546 for(j = 0; j < 9; j++) {
547 for(i = 0; i < 9; i++) {
548 src[i] = ((src[i] - 128) >> 1) + 128;
549 src2[i] = ((src2[i] - 128) >> 1) + 128;
550 }
551 src += s->uvlinesize;
552 src2 += s->uvlinesize;
553 }
554 }
555 /* if we deal with intensity compensation we need to scale source blocks */
556 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
557 int i, j;
558 uint8_t *src, *src2;
559
560 src = srcU; src2 = srcV;
561 for(j = 0; j < 9; j++) {
562 for(i = 0; i < 9; i++) {
563 src[i] = v->lutuv[src[i]];
564 src2[i] = v->lutuv[src2[i]];
565 }
566 src += s->uvlinesize;
567 src2 += s->uvlinesize;
568 }
569 }
570 }
571
572 /* Chroma MC always uses qpel bilinear */
573 uvmx = (uvmx&3)<<1;
574 uvmy = (uvmy&3)<<1;
575 if(!v->rnd){
576 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
577 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
578 }else{
579 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
580 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
581 }
582 }
583
584 /***********************************************************************/
585 /**
586 * @defgroup vc1block VC-1 Block-level functions
587 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
588 * @{
589 */
590
591 /**
592 * @def GET_MQUANT
593 * @brief Get macroblock-level quantizer scale
594 */
595 #define GET_MQUANT() \
596 if (v->dquantfrm) \
597 { \
598 int edges = 0; \
599 if (v->dqprofile == DQPROFILE_ALL_MBS) \
600 { \
601 if (v->dqbilevel) \
602 { \
603 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
604 } \
605 else \
606 { \
607 mqdiff = get_bits(gb, 3); \
608 if (mqdiff != 7) mquant = v->pq + mqdiff; \
609 else mquant = get_bits(gb, 5); \
610 } \
611 } \
612 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
613 edges = 1 << v->dqsbedge; \
614 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
615 edges = (3 << v->dqsbedge) % 15; \
616 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
617 edges = 15; \
618 if((edges&1) && !s->mb_x) \
619 mquant = v->altpq; \
620 if((edges&2) && s->first_slice_line) \
621 mquant = v->altpq; \
622 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
623 mquant = v->altpq; \
624 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
625 mquant = v->altpq; \
626 }
627
628 /**
629 * @def GET_MVDATA(_dmv_x, _dmv_y)
630 * @brief Get MV differentials
631 * @see MVDATA decoding from 8.3.5.2, p(1)20
632 * @param _dmv_x Horizontal differential for decoded MV
633 * @param _dmv_y Vertical differential for decoded MV
634 */
635 #define GET_MVDATA(_dmv_x, _dmv_y) \
636 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
637 VC1_MV_DIFF_VLC_BITS, 2); \
638 if (index > 36) \
639 { \
640 mb_has_coeffs = 1; \
641 index -= 37; \
642 } \
643 else mb_has_coeffs = 0; \
644 s->mb_intra = 0; \
645 if (!index) { _dmv_x = _dmv_y = 0; } \
646 else if (index == 35) \
647 { \
648 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
649 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
650 } \
651 else if (index == 36) \
652 { \
653 _dmv_x = 0; \
654 _dmv_y = 0; \
655 s->mb_intra = 1; \
656 } \
657 else \
658 { \
659 index1 = index%6; \
660 if (!s->quarter_sample && index1 == 5) val = 1; \
661 else val = 0; \
662 if(size_table[index1] - val > 0) \
663 val = get_bits(gb, size_table[index1] - val); \
664 else val = 0; \
665 sign = 0 - (val&1); \
666 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
667 \
668 index1 = index/6; \
669 if (!s->quarter_sample && index1 == 5) val = 1; \
670 else val = 0; \
671 if(size_table[index1] - val > 0) \
672 val = get_bits(gb, size_table[index1] - val); \
673 else val = 0; \
674 sign = 0 - (val&1); \
675 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
676 }
677
678 /** Predict and set motion vector
679 */
680 static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
681 {
682 int xy, wrap, off = 0;
683 int16_t *A, *B, *C;
684 int px, py;
685 int sum;
686
687 /* scale MV difference to be quad-pel */
688 dmv_x <<= 1 - s->quarter_sample;
689 dmv_y <<= 1 - s->quarter_sample;
690
691 wrap = s->b8_stride;
692 xy = s->block_index[n];
693
694 if(s->mb_intra){
695 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
696 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
697 s->current_picture.motion_val[1][xy][0] = 0;
698 s->current_picture.motion_val[1][xy][1] = 0;
699 if(mv1) { /* duplicate motion data for 1-MV block */
700 s->current_picture.motion_val[0][xy + 1][0] = 0;
701 s->current_picture.motion_val[0][xy + 1][1] = 0;
702 s->current_picture.motion_val[0][xy + wrap][0] = 0;
703 s->current_picture.motion_val[0][xy + wrap][1] = 0;
704 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
705 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
706 s->current_picture.motion_val[1][xy + 1][0] = 0;
707 s->current_picture.motion_val[1][xy + 1][1] = 0;
708 s->current_picture.motion_val[1][xy + wrap][0] = 0;
709 s->current_picture.motion_val[1][xy + wrap][1] = 0;
710 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
711 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
712 }
713 return;
714 }
715
716 C = s->current_picture.motion_val[0][xy - 1];
717 A = s->current_picture.motion_val[0][xy - wrap];
718 if(mv1)
719 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
720 else {
721 //in 4-MV mode different blocks have different B predictor position
722 switch(n){
723 case 0:
724 off = (s->mb_x > 0) ? -1 : 1;
725 break;
726 case 1:
727 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
728 break;
729 case 2:
730 off = 1;
731 break;
732 case 3:
733 off = -1;
734 }
735 }
736 B = s->current_picture.motion_val[0][xy - wrap + off];
737
738 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
739 if(s->mb_width == 1) {
740 px = A[0];
741 py = A[1];
742 } else {
743 px = mid_pred(A[0], B[0], C[0]);
744 py = mid_pred(A[1], B[1], C[1]);
745 }
746 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
747 px = C[0];
748 py = C[1];
749 } else {
750 px = py = 0;
751 }
752 /* Pullback MV as specified in 8.3.5.3.4 */
753 {
754 int qx, qy, X, Y;
755 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
756 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
757 X = (s->mb_width << 6) - 4;
758 Y = (s->mb_height << 6) - 4;
759 if(mv1) {
760 if(qx + px < -60) px = -60 - qx;
761 if(qy + py < -60) py = -60 - qy;
762 } else {
763 if(qx + px < -28) px = -28 - qx;
764 if(qy + py < -28) py = -28 - qy;
765 }
766 if(qx + px > X) px = X - qx;
767 if(qy + py > Y) py = Y - qy;
768 }
769 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
770 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
771 if(is_intra[xy - wrap])
772 sum = FFABS(px) + FFABS(py);
773 else
774 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
775 if(sum > 32) {
776 if(get_bits1(&s->gb)) {
777 px = A[0];
778 py = A[1];
779 } else {
780 px = C[0];
781 py = C[1];
782 }
783 } else {
784 if(is_intra[xy - 1])
785 sum = FFABS(px) + FFABS(py);
786 else
787 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
788 if(sum > 32) {
789 if(get_bits1(&s->gb)) {
790 px = A[0];
791 py = A[1];
792 } else {
793 px = C[0];
794 py = C[1];
795 }
796 }
797 }
798 }
799 /* store MV using signed modulus of MV range defined in 4.11 */
800 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
801 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
802 if(mv1) { /* duplicate motion data for 1-MV block */
803 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
804 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
805 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
806 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
807 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
808 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
809 }
810 }
811
812 /** Motion compensation for direct or interpolated blocks in B-frames
813 */
814 static void vc1_interp_mc(VC1Context *v)
815 {
816 MpegEncContext *s = &v->s;
817 DSPContext *dsp = &v->s.dsp;
818 uint8_t *srcY, *srcU, *srcV;
819 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
820
821 if(!v->s.next_picture.data[0])return;
822
823 mx = s->mv[1][0][0];
824 my = s->mv[1][0][1];
825 uvmx = (mx + ((mx & 3) == 3)) >> 1;
826 uvmy = (my + ((my & 3) == 3)) >> 1;
827 if(v->fastuvmc) {
828 uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
829 uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
830 }
831 srcY = s->next_picture.data[0];
832 srcU = s->next_picture.data[1];
833 srcV = s->next_picture.data[2];
834
835 src_x = s->mb_x * 16 + (mx >> 2);
836 src_y = s->mb_y * 16 + (my >> 2);
837 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
838 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
839
840 if(v->profile != PROFILE_ADVANCED){
841 src_x = av_clip( src_x, -16, s->mb_width * 16);
842 src_y = av_clip( src_y, -16, s->mb_height * 16);
843 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
844 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
845 }else{
846 src_x = av_clip( src_x, -17, s->avctx->coded_width);
847 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
848 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
849 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
850 }
851
852 srcY += src_y * s->linesize + src_x;
853 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
854 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
855
856 /* for grayscale we should not try to read from unknown area */
857 if(s->flags & CODEC_FLAG_GRAY) {
858 srcU = s->edge_emu_buffer + 18 * s->linesize;
859 srcV = s->edge_emu_buffer + 18 * s->linesize;
860 }
861
862 if(v->rangeredfrm
863 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
864 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
865 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
866
867 srcY -= s->mspel * (1 + s->linesize);
868 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
869 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
870 srcY = s->edge_emu_buffer;
871 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
872 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
873 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
874 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
875 srcU = uvbuf;
876 srcV = uvbuf + 16;
877 /* if we deal with range reduction we need to scale source blocks */
878 if(v->rangeredfrm) {
879 int i, j;
880 uint8_t *src, *src2;
881
882 src = srcY;
883 for(j = 0; j < 17 + s->mspel*2; j++) {
884 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
885 src += s->linesize;
886 }
887 src = srcU; src2 = srcV;
888 for(j = 0; j < 9; j++) {
889 for(i = 0; i < 9; i++) {
890 src[i] = ((src[i] - 128) >> 1) + 128;
891 src2[i] = ((src2[i] - 128) >> 1) + 128;
892 }
893 src += s->uvlinesize;
894 src2 += s->uvlinesize;
895 }
896 }
897 srcY += s->mspel * (1 + s->linesize);
898 }
899
900 if(s->mspel) {
901 dxy = ((my & 3) << 2) | (mx & 3);
902 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
903 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
904 srcY += s->linesize * 8;
905 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
906 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
907 } else { // hpel mc
908 dxy = (my & 2) | ((mx & 2) >> 1);
909
910 if(!v->rnd)
911 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
912 else
913 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
914 }
915
916 if(s->flags & CODEC_FLAG_GRAY) return;
917 /* Chroma MC always uses qpel blilinear */
918 uvmx = (uvmx&3)<<1;
919 uvmy = (uvmy&3)<<1;
920 if(!v->rnd){
921 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
922 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
923 }else{
924 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
925 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
926 }
927 }
928
929 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
930 {
931 int n = bfrac;
932
933 #if B_FRACTION_DEN==256
934 if(inv)
935 n -= 256;
936 if(!qs)
937 return 2 * ((value * n + 255) >> 9);
938 return (value * n + 128) >> 8;
939 #else
940 if(inv)
941 n -= B_FRACTION_DEN;
942 if(!qs)
943 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
944 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
945 #endif
946 }
947
948 /** Reconstruct motion vector for B-frame and do motion compensation
949 */
950 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
951 {
952 if(v->use_ic) {
953 v->mv_mode2 = v->mv_mode;
954 v->mv_mode = MV_PMODE_INTENSITY_COMP;
955 }
956 if(direct) {
957 vc1_mc_1mv(v, 0);
958 vc1_interp_mc(v);
959 if(v->use_ic) v->mv_mode = v->mv_mode2;
960 return;
961 }
962 if(mode == BMV_TYPE_INTERPOLATED) {
963 vc1_mc_1mv(v, 0);
964 vc1_interp_mc(v);
965 if(v->use_ic) v->mv_mode = v->mv_mode2;
966 return;
967 }
968
969 if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
970 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
971 if(v->use_ic) v->mv_mode = v->mv_mode2;
972 }
973
974 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
975 {
976 MpegEncContext *s = &v->s;
977 int xy, wrap, off = 0;
978 int16_t *A, *B, *C;
979 int px, py;
980 int sum;
981 int r_x, r_y;
982 const uint8_t *is_intra = v->mb_type[0];
983
984 r_x = v->range_x;
985 r_y = v->range_y;
986 /* scale MV difference to be quad-pel */
987 dmv_x[0] <<= 1 - s->quarter_sample;
988 dmv_y[0] <<= 1 - s->quarter_sample;
989 dmv_x[1] <<= 1 - s->quarter_sample;
990 dmv_y[1] <<= 1 - s->quarter_sample;
991
992 wrap = s->b8_stride;
993 xy = s->block_index[0];
994
995 if(s->mb_intra) {
996 s->current_picture.motion_val[0][xy][0] =
997 s->current_picture.motion_val[0][xy][1] =
998 s->current_picture.motion_val[1][xy][0] =
999 s->current_picture.motion_val[1][xy][1] = 0;
1000 return;
1001 }
1002 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1003 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1004 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1005 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1006
1007 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1008 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));
1009 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));
1010 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));
1011 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));
1012 if(direct) {
1013 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1014 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1015 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1016 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1017 return;
1018 }
1019
1020 if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1021 C = s->current_picture.motion_val[0][xy - 2];
1022 A = s->current_picture.motion_val[0][xy - wrap*2];
1023 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1024 B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1025
1026 if(!s->mb_x) C[0] = C[1] = 0;
1027 if(!s->first_slice_line) { // predictor A is not out of bounds
1028 if(s->mb_width == 1) {
1029 px = A[0];
1030 py = A[1];
1031 } else {
1032 px = mid_pred(A[0], B[0], C[0]);
1033 py = mid_pred(A[1], B[1], C[1]);
1034 }
1035 } else if(s->mb_x) { // predictor C is not out of bounds
1036 px = C[0];
1037 py = C[1];
1038 } else {
1039 px = py = 0;
1040 }
1041 /* Pullback MV as specified in 8.3.5.3.4 */
1042 {
1043 int qx, qy, X, Y;
1044 if(v->profile < PROFILE_ADVANCED) {
1045 qx = (s->mb_x << 5);
1046 qy = (s->mb_y << 5);
1047 X = (s->mb_width << 5) - 4;
1048 Y = (s->mb_height << 5) - 4;
1049 if(qx + px < -28) px = -28 - qx;
1050 if(qy + py < -28) py = -28 - qy;
1051 if(qx + px > X) px = X - qx;
1052 if(qy + py > Y) py = Y - qy;
1053 } else {
1054 qx = (s->mb_x << 6);
1055 qy = (s->mb_y << 6);
1056 X = (s->mb_width << 6) - 4;
1057 Y = (s->mb_height << 6) - 4;
1058 if(qx + px < -60) px = -60 - qx;
1059 if(qy + py < -60) py = -60 - qy;
1060 if(qx + px > X) px = X - qx;
1061 if(qy + py > Y) py = Y - qy;
1062 }
1063 }
1064 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1065 if(0 && !s->first_slice_line && s->mb_x) {
1066 if(is_intra[xy - wrap])
1067 sum = FFABS(px) + FFABS(py);
1068 else
1069 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1070 if(sum > 32) {
1071 if(get_bits1(&s->gb)) {
1072 px = A[0];
1073 py = A[1];
1074 } else {
1075 px = C[0];
1076 py = C[1];
1077 }
1078 } else {
1079 if(is_intra[xy - 2])
1080 sum = FFABS(px) + FFABS(py);
1081 else
1082 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1083 if(sum > 32) {
1084 if(get_bits1(&s->gb)) {
1085 px = A[0];
1086 py = A[1];
1087 } else {
1088 px = C[0];
1089 py = C[1];
1090 }
1091 }
1092 }
1093 }
1094 /* store MV using signed modulus of MV range defined in 4.11 */
1095 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
1096 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
1097 }
1098 if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1099 C = s->current_picture.motion_val[1][xy - 2];
1100 A = s->current_picture.motion_val[1][xy - wrap*2];
1101 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1102 B = s->current_picture.motion_val[1][xy - wrap*2 + off];
1103
1104 if(!s->mb_x) C[0] = C[1] = 0;
1105 if(!s->first_slice_line) { // predictor A is not out of bounds
1106 if(s->mb_width == 1) {
1107 px = A[0];
1108 py = A[1];
1109 } else {
1110 px = mid_pred(A[0], B[0], C[0]);
1111 py = mid_pred(A[1], B[1], C[1]);
1112 }
1113 } else if(s->mb_x) { // predictor C is not out of bounds
1114 px = C[0];
1115 py = C[1];
1116 } else {
1117 px = py = 0;
1118 }
1119 /* Pullback MV as specified in 8.3.5.3.4 */
1120 {
1121 int qx, qy, X, Y;
1122 if(v->profile < PROFILE_ADVANCED) {
1123 qx = (s->mb_x << 5);
1124 qy = (s->mb_y << 5);
1125 X = (s->mb_width << 5) - 4;
1126 Y = (s->mb_height << 5) - 4;
1127 if(qx + px < -28) px = -28 - qx;
1128 if(qy + py < -28) py = -28 - qy;
1129 if(qx + px > X) px = X - qx;
1130 if(qy + py > Y) py = Y - qy;
1131 } else {
1132 qx = (s->mb_x << 6);
1133 qy = (s->mb_y << 6);
1134 X = (s->mb_width << 6) - 4;
1135 Y = (s->mb_height << 6) - 4;
1136 if(qx + px < -60) px = -60 - qx;
1137 if(qy + py < -60) py = -60 - qy;
1138 if(qx + px > X) px = X - qx;
1139 if(qy + py > Y) py = Y - qy;
1140 }
1141 }
1142 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1143 if(0 && !s->first_slice_line && s->mb_x) {
1144 if(is_intra[xy - wrap])
1145 sum = FFABS(px) + FFABS(py);
1146 else
1147 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1148 if(sum > 32) {
1149 if(get_bits1(&s->gb)) {
1150 px = A[0];
1151 py = A[1];
1152 } else {
1153 px = C[0];
1154 py = C[1];
1155 }
1156 } else {
1157 if(is_intra[xy - 2])
1158 sum = FFABS(px) + FFABS(py);
1159 else
1160 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1161 if(sum > 32) {
1162 if(get_bits1(&s->gb)) {
1163 px = A[0];
1164 py = A[1];
1165 } else {
1166 px = C[0];
1167 py = C[1];
1168 }
1169 }
1170 }
1171 }
1172 /* store MV using signed modulus of MV range defined in 4.11 */
1173
1174 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1175 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1176 }
1177 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1178 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1179 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1180 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1181 }
1182
1183 /** Get predicted DC value for I-frames only
1184 * prediction dir: left=0, top=1
1185 * @param s MpegEncContext
1186 * @param overlap flag indicating that overlap filtering is used
1187 * @param pq integer part of picture quantizer
1188 * @param[in] n block index in the current MB
1189 * @param dc_val_ptr Pointer to DC predictor
1190 * @param dir_ptr Prediction direction for use in AC prediction
1191 */
1192 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1193 int16_t **dc_val_ptr, int *dir_ptr)
1194 {
1195 int a, b, c, wrap, pred, scale;
1196 int16_t *dc_val;
1197 static const uint16_t dcpred[32] = {
1198 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1199 114, 102, 93, 85, 79, 73, 68, 64,
1200 60, 57, 54, 51, 49, 47, 45, 43,
1201 41, 39, 38, 37, 35, 34, 33
1202 };
1203
1204 /* find prediction - wmv3_dc_scale always used here in fact */
1205 if (n < 4) scale = s->y_dc_scale;
1206 else scale = s->c_dc_scale;
1207
1208 wrap = s->block_wrap[n];
1209 dc_val= s->dc_val[0] + s->block_index[n];
1210
1211 /* B A
1212 * C X
1213 */
1214 c = dc_val[ - 1];
1215 b = dc_val[ - 1 - wrap];
1216 a = dc_val[ - wrap];
1217
1218 if (pq < 9 || !overlap)
1219 {
1220 /* Set outer values */
1221 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1222 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1223 }
1224 else
1225 {
1226 /* Set outer values */
1227 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1228 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1229 }
1230
1231 if (abs(a - b) <= abs(b - c)) {
1232 pred = c;
1233 *dir_ptr = 1;//left
1234 } else {
1235 pred = a;
1236 *dir_ptr = 0;//top
1237 }
1238
1239 /* update predictor */
1240 *dc_val_ptr = &dc_val[0];
1241 return pred;
1242 }
1243
1244
1245 /** Get predicted DC value
1246 * prediction dir: left=0, top=1
1247 * @param s MpegEncContext
1248 * @param overlap flag indicating that overlap filtering is used
1249 * @param pq integer part of picture quantizer
1250 * @param[in] n block index in the current MB
1251 * @param a_avail flag indicating top block availability
1252 * @param c_avail flag indicating left block availability
1253 * @param dc_val_ptr Pointer to DC predictor
1254 * @param dir_ptr Prediction direction for use in AC prediction
1255 */
1256 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1257 int a_avail, int c_avail,
1258 int16_t **dc_val_ptr, int *dir_ptr)
1259 {
1260 int a, b, c, wrap, pred;
1261 int16_t *dc_val;
1262 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1263 int q1, q2 = 0;
1264
1265 wrap = s->block_wrap[n];
1266 dc_val= s->dc_val[0] + s->block_index[n];
1267
1268 /* B A
1269 * C X
1270 */
1271 c = dc_val[ - 1];
1272 b = dc_val[ - 1 - wrap];
1273 a = dc_val[ - wrap];
1274 /* scale predictors if needed */
1275 q1 = s->current_picture.qscale_table[mb_pos];
1276 if(c_avail && (n!= 1 && n!=3)) {
1277 q2 = s->current_picture.qscale_table[mb_pos - 1];
1278 if(q2 && q2 != q1)
1279 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1280 }
1281 if(a_avail && (n!= 2 && n!=3)) {
1282 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1283 if(q2 && q2 != q1)
1284 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1285 }
1286 if(a_avail && c_avail && (n!=3)) {
1287 int off = mb_pos;
1288 if(n != 1) off--;
1289 if(n != 2) off -= s->mb_stride;
1290 q2 = s->current_picture.qscale_table[off];
1291 if(q2 && q2 != q1)
1292 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1293 }
1294
1295 if(a_avail && c_avail) {
1296 if(abs(a - b) <= abs(b - c)) {
1297 pred = c;
1298 *dir_ptr = 1;//left
1299 } else {
1300 pred = a;
1301 *dir_ptr = 0;//top
1302 }
1303 } else if(a_avail) {
1304 pred = a;
1305 *dir_ptr = 0;//top
1306 } else if(c_avail) {
1307 pred = c;
1308 *dir_ptr = 1;//left
1309 } else {
1310 pred = 0;
1311 *dir_ptr = 1;//left
1312 }
1313
1314 /* update predictor */
1315 *dc_val_ptr = &dc_val[0];
1316 return pred;
1317 }
1318
1319 /** @} */ // Block group
1320
1321 /**
1322 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1323 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1324 * @{
1325 */
1326
1327 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1328 {
1329 int xy, wrap, pred, a, b, c;
1330
1331 xy = s->block_index[n];
1332 wrap = s->b8_stride;
1333
1334 /* B C
1335 * A X
1336 */
1337 a = s->coded_block[xy - 1 ];
1338 b = s->coded_block[xy - 1 - wrap];
1339 c = s->coded_block[xy - wrap];
1340
1341 if (b == c) {
1342 pred = a;
1343 } else {
1344 pred = c;
1345 }
1346
1347 /* store value */
1348 *coded_block_ptr = &s->coded_block[xy];
1349
1350 return pred;
1351 }
1352
1353 /**
1354 * Decode one AC coefficient
1355 * @param v The VC1 context
1356 * @param last Last coefficient
1357 * @param skip How much zero coefficients to skip
1358 * @param value Decoded AC coefficient value
1359 * @param codingset set of VLC to decode data
1360 * @see 8.1.3.4
1361 */
1362 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1363 {
1364 GetBitContext *gb = &v->s.gb;
1365 int index, escape, run = 0, level = 0, lst = 0;
1366
1367 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1368 if (index != vc1_ac_sizes[codingset] - 1) {
1369 run = vc1_index_decode_table[codingset][index][0];
1370 level = vc1_index_decode_table[codingset][index][1];
1371 lst = index >= vc1_last_decode_table[codingset];
1372 if(get_bits1(gb))
1373 level = -level;
1374 } else {
1375 escape = decode210(gb);
1376 if (escape != 2) {
1377 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1378 run = vc1_index_decode_table[codingset][index][0];
1379 level = vc1_index_decode_table[codingset][index][1];
1380 lst = index >= vc1_last_decode_table[codingset];
1381 if(escape == 0) {
1382 if(lst)
1383 level += vc1_last_delta_level_table[codingset][run];
1384 else
1385 level += vc1_delta_level_table[codingset][run];
1386 } else {
1387 if(lst)
1388 run += vc1_last_delta_run_table[codingset][level] + 1;
1389 else
1390 run += vc1_delta_run_table[codingset][level] + 1;
1391 }
1392 if(get_bits1(gb))
1393 level = -level;
1394 } else {
1395 int sign;
1396 lst = get_bits1(gb);
1397 if(v->s.esc3_level_length == 0) {
1398 if(v->pq < 8 || v->dquantfrm) { // table 59
1399 v->s.esc3_level_length = get_bits(gb, 3);
1400 if(!v->s.esc3_level_length)
1401 v->s.esc3_level_length = get_bits(gb, 2) + 8;
1402 } else { //table 60
1403 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
1404 }
1405 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1406 }
1407 run = get_bits(gb, v->s.esc3_run_length);
1408 sign = get_bits1(gb);
1409 level = get_bits(gb, v->s.esc3_level_length);
1410 if(sign)
1411 level = -level;
1412 }
1413 }
1414
1415 *last = lst;
1416 *skip = run;
1417 *value = level;
1418 }
1419
1420 /** Decode intra block in intra frames - should be faster than decode_intra_block
1421 * @param v VC1Context
1422 * @param block block to decode
1423 * @param[in] n subblock index
1424 * @param coded are AC coeffs present or not
1425 * @param codingset set of VLC to decode data
1426 */
1427 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1428 {
1429 GetBitContext *gb = &v->s.gb;
1430 MpegEncContext *s = &v->s;
1431 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1432 int i;
1433 int16_t *dc_val;
1434 int16_t *ac_val, *ac_val2;
1435 int dcdiff;
1436
1437 /* Get DC differential */
1438 if (n < 4) {
1439 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1440 } else {
1441 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1442 }
1443 if (dcdiff < 0){
1444 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1445 return -1;
1446 }
1447 if (dcdiff)
1448 {
1449 if (dcdiff == 119 /* ESC index value */)
1450 {
1451 /* TODO: Optimize */
1452 if (v->pq == 1) dcdiff = get_bits(gb, 10);
1453 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1454 else dcdiff = get_bits(gb, 8);
1455 }
1456 else
1457 {
1458 if (v->pq == 1)
1459 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1460 else if (v->pq == 2)
1461 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1462 }
1463 if (get_bits1(gb))
1464 dcdiff = -dcdiff;
1465 }
1466
1467 /* Prediction */
1468 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1469 *dc_val = dcdiff;
1470
1471 /* Store the quantized DC coeff, used for prediction */
1472 if (n < 4) {
1473 block[0] = dcdiff * s->y_dc_scale;
1474 } else {
1475 block[0] = dcdiff * s->c_dc_scale;
1476 }
1477 /* Skip ? */
1478 if (!coded) {
1479 goto not_coded;
1480 }
1481
1482 //AC Decoding
1483 i = 1;
1484
1485 {
1486 int last = 0, skip, value;
1487 const int8_t *zz_table;
1488 int scale;
1489 int k;
1490
1491 scale = v->pq * 2 + v->halfpq;
1492
1493 if(v->s.ac_pred) {
1494 if(!dc_pred_dir)
1495 zz_table = wmv1_scantable[2];
1496 else
1497 zz_table = wmv1_scantable[3];
1498 } else
1499 zz_table = wmv1_scantable[1];
1500
1501 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1502 ac_val2 = ac_val;
1503 if(dc_pred_dir) //left
1504 ac_val -= 16;
1505 else //top
1506 ac_val -= 16 * s->block_wrap[n];
1507
1508 while (!last) {
1509 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1510 i += skip;
1511 if(i > 63)
1512 break;
1513 block[zz_table[i++]] = value;
1514 }
1515
1516 /* apply AC prediction if needed */
1517 if(s->ac_pred) {
1518 if(dc_pred_dir) { //left
1519 for(k = 1; k < 8; k++)
1520 block[k << 3] += ac_val[k];
1521 } else { //top
1522 for(k = 1; k < 8; k++)
1523 block[k] += ac_val[k + 8];
1524 }
1525 }
1526 /* save AC coeffs for further prediction */
1527 for(k = 1; k < 8; k++) {
1528 ac_val2[k] = block[k << 3];
1529 ac_val2[k + 8] = block[k];
1530 }
1531
1532 /* scale AC coeffs */
1533 for(k = 1; k < 64; k++)
1534 if(block[k]) {
1535 block[k] *= scale;
1536 if(!v->pquantizer)
1537 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1538 }
1539
1540 if(s->ac_pred) i = 63;
1541 }
1542
1543 not_coded:
1544 if(!coded) {
1545 int k, scale;
1546 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1547 ac_val2 = ac_val;
1548
1549 i = 0;
1550 scale = v->pq * 2 + v->halfpq;
1551 memset(ac_val2, 0, 16 * 2);
1552 if(dc_pred_dir) {//left
1553 ac_val -= 16;
1554 if(s->ac_pred)
1555 memcpy(ac_val2, ac_val, 8 * 2);
1556 } else {//top
1557 ac_val -= 16 * s->block_wrap[n];
1558 if(s->ac_pred)
1559 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1560 }
1561
1562 /* apply AC prediction if needed */
1563 if(s->ac_pred) {
1564 if(dc_pred_dir) { //left
1565 for(k = 1; k < 8; k++) {
1566 block[k << 3] = ac_val[k] * scale;
1567 if(!v->pquantizer && block[k << 3])
1568 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1569 }
1570 } else { //top
1571 for(k = 1; k < 8; k++) {
1572 block[k] = ac_val[k + 8] * scale;
1573 if(!v->pquantizer && block[k])
1574 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1575 }
1576 }
1577 i = 63;
1578 }
1579 }
1580 s->block_last_index[n] = i;
1581
1582 return 0;
1583 }
1584
1585 /** Decode intra block in intra frames - should be faster than decode_intra_block
1586 * @param v VC1Context
1587 * @param block block to decode
1588 * @param[in] n subblock number
1589 * @param coded are AC coeffs present or not
1590 * @param codingset set of VLC to decode data
1591 * @param mquant quantizer value for this macroblock
1592 */
1593 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
1594 {
1595 GetBitContext *gb = &v->s.gb;
1596 MpegEncContext *s = &v->s;
1597 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1598 int i;
1599 int16_t *dc_val;
1600 int16_t *ac_val, *ac_val2;
1601 int dcdiff;
1602 int a_avail = v->a_avail, c_avail = v->c_avail;
1603 int use_pred = s->ac_pred;
1604 int scale;
1605 int q1, q2 = 0;
1606 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1607
1608 /* Get DC differential */
1609 if (n < 4) {
1610 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1611 } else {
1612 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1613 }
1614 if (dcdiff < 0){
1615 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1616 return -1;
1617 }
1618 if (dcdiff)
1619 {
1620 if (dcdiff == 119 /* ESC index value */)
1621 {
1622 /* TODO: Optimize */
1623 if (mquant == 1) dcdiff = get_bits(gb, 10);
1624 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1625 else dcdiff = get_bits(gb, 8);
1626 }
1627 else
1628 {
1629 if (mquant == 1)
1630 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1631 else if (mquant == 2)
1632 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1633 }
1634 if (get_bits1(gb))
1635 dcdiff = -dcdiff;
1636 }
1637
1638 /* Prediction */
1639 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1640 *dc_val = dcdiff;
1641
1642 /* Store the quantized DC coeff, used for prediction */
1643 if (n < 4) {
1644 block[0] = dcdiff * s->y_dc_scale;
1645 } else {
1646 block[0] = dcdiff * s->c_dc_scale;
1647 }
1648
1649 //AC Decoding
1650 i = 1;
1651
1652 /* check if AC is needed at all */
1653 if(!a_avail && !c_avail) use_pred = 0;
1654 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1655 ac_val2 = ac_val;
1656
1657 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
1658
1659 if(dc_pred_dir) //left
1660 ac_val -= 16;
1661 else //top
1662 ac_val -= 16 * s->block_wrap[n];
1663
1664 q1 = s->current_picture.qscale_table[mb_pos];
1665 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1666 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1667 if(dc_pred_dir && n==1) q2 = q1;
1668 if(!dc_pred_dir && n==2) q2 = q1;
1669 if(n==3) q2 = q1;
1670
1671 if(coded) {
1672 int last = 0, skip, value;
1673 const int8_t *zz_table;
1674 int k;
1675
1676 if(v->s.ac_pred) {
1677 if(!dc_pred_dir)
1678 zz_table = wmv1_scantable[2];
1679 else
1680 zz_table = wmv1_scantable[3];
1681 } else
1682 zz_table = wmv1_scantable[1];
1683
1684 while (!last) {
1685 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1686 i += skip;
1687 if(i > 63)
1688 break;
1689 block[zz_table[i++]] = value;
1690 }
1691
1692 /* apply AC prediction if needed */
1693 if(use_pred) {
1694 /* scale predictors if needed*/
1695 if(q2 && q1!=q2) {
1696 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1697 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1698
1699 if(dc_pred_dir) { //left
1700 for(k = 1; k < 8; k++)
1701 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1702 } else { //top
1703 for(k = 1; k < 8; k++)
1704 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1705 }
1706 } else {
1707 if(dc_pred_dir) { //left
1708 for(k = 1; k < 8; k++)
1709 block[k << 3] += ac_val[k];
1710 } else { //top
1711 for(k = 1; k < 8; k++)
1712 block[k] += ac_val[k + 8];
1713 }
1714 }
1715 }
1716 /* save AC coeffs for further prediction */
1717 for(k = 1; k < 8; k++) {
1718 ac_val2[k] = block[k << 3];
1719 ac_val2[k + 8] = block[k];
1720 }
1721
1722 /* scale AC coeffs */
1723 for(k = 1; k < 64; k++)
1724 if(block[k]) {
1725 block[k] *= scale;
1726 if(!v->pquantizer)
1727 block[k] += (block[k] < 0) ? -mquant : mquant;
1728 }
1729
1730 if(use_pred) i = 63;
1731 } else { // no AC coeffs
1732 int k;
1733
1734 memset(ac_val2, 0, 16 * 2);
1735 if(dc_pred_dir) {//left
1736 if(use_pred) {
1737 memcpy(ac_val2, ac_val, 8 * 2);
1738 if(q2 && q1!=q2) {
1739 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1740 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1741 for(k = 1; k < 8; k++)
1742 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1743 }
1744 }
1745 } else {//top
1746 if(use_pred) {
1747 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1748 if(q2 && q1!=q2) {
1749 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1750 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1751 for(k = 1; k < 8; k++)
1752 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1753 }
1754 }
1755 }
1756
1757 /* apply AC prediction if needed */
1758 if(use_pred) {
1759 if(dc_pred_dir) { //left
1760 for(k = 1; k < 8; k++) {
1761 block[k << 3] = ac_val2[k] * scale;
1762 if(!v->pquantizer && block[k << 3])
1763 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1764 }
1765 } else { //top
1766 for(k = 1; k < 8; k++) {
1767 block[k] = ac_val2[k + 8] * scale;
1768 if(!v->pquantizer && block[k])
1769 block[k] += (block[k] < 0) ? -mquant : mquant;
1770 }
1771 }
1772 i = 63;
1773 }
1774 }
1775 s->block_last_index[n] = i;
1776
1777 return 0;
1778 }
1779
1780 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1781 * @param v VC1Context
1782 * @param block block to decode
1783 * @param[in] n subblock index
1784 * @param coded are AC coeffs present or not
1785 * @param mquant block quantizer
1786 * @param codingset set of VLC to decode data
1787 */
1788 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1789 {
1790 GetBitContext *gb = &v->s.gb;
1791 MpegEncContext *s = &v->s;
1792 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1793 int i;
1794 int16_t *dc_val;
1795 int16_t *ac_val, *ac_val2;
1796 int dcdiff;
1797 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1798 int a_avail = v->a_avail, c_avail = v->c_avail;
1799 int use_pred = s->ac_pred;
1800 int scale;
1801 int q1, q2 = 0;
1802
1803 s->dsp.clear_block(block);
1804
1805 /* XXX: Guard against dumb values of mquant */
1806 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1807
1808 /* Set DC scale - y and c use the same */
1809 s->y_dc_scale = s->y_dc_scale_table[mquant];
1810 s->c_dc_scale = s->c_dc_scale_table[mquant];
1811
1812 /* Get DC differential */
1813 if (n < 4) {
1814 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1815 } else {
1816 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1817 }
1818 if (dcdiff < 0){
1819 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1820 return -1;
1821 }
1822 if (dcdiff)
1823 {
1824 if (dcdiff == 119 /* ESC index value */)
1825 {
1826 /* TODO: Optimize */
1827 if (mquant == 1) dcdiff = get_bits(gb, 10);
1828 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1829 else dcdiff = get_bits(gb, 8);
1830 }
1831 else
1832 {
1833 if (mquant == 1)
1834 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1835 else if (mquant == 2)
1836 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1837 }
1838 if (get_bits1(gb))
1839 dcdiff = -dcdiff;
1840 }
1841
1842 /* Prediction */
1843 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1844 *dc_val = dcdiff;
1845
1846 /* Store the quantized DC coeff, used for prediction */
1847
1848 if (n < 4) {
1849 block[0] = dcdiff * s->y_dc_scale;
1850 } else {
1851 block[0] = dcdiff * s->c_dc_scale;
1852 }
1853
1854 //AC Decoding
1855 i = 1;
1856
1857 /* check if AC is needed at all and adjust direction if needed */
1858 if(!a_avail) dc_pred_dir = 1;
1859 if(!c_avail) dc_pred_dir = 0;
1860 if(!a_avail && !c_avail) use_pred = 0;
1861 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1862 ac_val2 = ac_val;
1863
1864 scale = mquant * 2 + v->halfpq;
1865
1866 if(dc_pred_dir) //left
1867 ac_val -= 16;
1868 else //top
1869 ac_val -= 16 * s->block_wrap[n];
1870
1871 q1 = s->current_picture.qscale_table[mb_pos];
1872 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1873 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1874 if(dc_pred_dir && n==1) q2 = q1;
1875 if(!dc_pred_dir && n==2) q2 = q1;
1876 if(n==3) q2 = q1;
1877
1878 if(coded) {
1879 int last = 0, skip, value;
1880 const int8_t *zz_table;
1881 int k;
1882
1883 zz_table = wmv1_scantable[0];
1884
1885 while (!last) {
1886 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1887 i += skip;
1888 if(i > 63)
1889 break;
1890 block[zz_table[i++]] = value;
1891 }
1892
1893 /* apply AC prediction if needed */
1894 if(use_pred) {
1895 /* scale predictors if needed*/
1896 if(q2 && q1!=q2) {
1897 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1898 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1899
1900 if(dc_pred_dir) { //left
1901 for(k = 1; k < 8; k++)
1902 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1903 } else { //top
1904 for(k = 1; k < 8; k++)
1905 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1906 }
1907 } else {
1908 if(dc_pred_dir) { //left
1909 for(k = 1; k < 8; k++)
1910 block[k << 3] += ac_val[k];
1911 } else { //top
1912 for(k = 1; k < 8; k++)
1913 block[k] += ac_val[k + 8];
1914 }
1915 }
1916 }
1917 /* save AC coeffs for further prediction */
1918 for(k = 1; k < 8; k++) {
1919 ac_val2[k] = block[k << 3];
1920 ac_val2[k + 8] = block[k];
1921 }
1922
1923 /* scale AC coeffs */
1924 for(k = 1; k < 64; k++)
1925 if(block[k]) {
1926 block[k] *= scale;
1927 if(!v->pquantizer)
1928 block[k] += (block[k] < 0) ? -mquant : mquant;
1929 }
1930
1931 if(use_pred) i = 63;
1932 } else { // no AC coeffs
1933 int k;
1934
1935 memset(ac_val2, 0, 16 * 2);
1936 if(dc_pred_dir) {//left
1937 if(use_pred) {
1938 memcpy(ac_val2, ac_val, 8 * 2);
1939 if(q2 && q1!=q2) {
1940 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1941 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1942 for(k = 1; k < 8; k++)
1943 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1944 }
1945 }
1946 } else {//top
1947 if(use_pred) {
1948 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1949 if(q2 && q1!=q2) {
1950 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1951 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1952 for(k = 1; k < 8; k++)
1953 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1954 }
1955 }
1956 }
1957
1958 /* apply AC prediction if needed */
1959 if(use_pred) {
1960 if(dc_pred_dir) { //left
1961 for(k = 1; k < 8; k++) {
1962 block[k << 3] = ac_val2[k] * scale;
1963 if(!v->pquantizer && block[k << 3])
1964 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1965 }
1966 } else { //top
1967 for(k = 1; k < 8; k++) {
1968 block[k] = ac_val2[k + 8] * scale;
1969 if(!v->pquantizer && block[k])
1970 block[k] += (block[k] < 0) ? -mquant : mquant;
1971 }
1972 }
1973 i = 63;
1974 }
1975 }
1976 s->block_last_index[n] = i;
1977
1978 return 0;
1979 }
1980
1981 /** Decode P block
1982 */
1983 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1984 uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1985 {
1986 MpegEncContext *s = &v->s;
1987 GetBitContext *gb = &s->gb;
1988 int i, j;
1989 int subblkpat = 0;
1990 int scale, off, idx, last, skip, value;
1991 int ttblk = ttmb & 7;
1992 int pat = 0;
1993
1994 s->dsp.clear_block(block);
1995
1996 if(ttmb == -1) {
1997 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)];
1998 }
1999 if(ttblk == TT_4X4) {
2000 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
2001 }
2002 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2003 subblkpat = decode012(gb);
2004 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2005 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2006 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2007 }
2008 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2009
2010 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2011 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2012 subblkpat = 2 - (ttblk == TT_8X4_TOP);
2013 ttblk = TT_8X4;
2014 }
2015 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2016 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2017 ttblk = TT_4X8;
2018 }
2019 switch(ttblk) {
2020 case TT_8X8:
2021 pat = 0xF;
2022 i = 0;
2023 last = 0;
2024 while (!last) {
2025 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2026 i += skip;
2027 if(i > 63)
2028 break;
2029 idx = wmv1_scantable[0][i++];
2030 block[idx] = value * scale;
2031 if(!v->pquantizer)
2032 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2033 }
2034 if(!skip_block){
2035 if(i==1)
2036 s->dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
2037 else{
2038 s->dsp.vc1_inv_trans_8x8(block);
2039 s->dsp.add_pixels_clamped(block, dst, linesize);
2040 }
2041 if(apply_filter && cbp_top & 0xC)
2042 s->dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2043 if(apply_filter && cbp_left & 0xA)
2044 s->dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2045 }
2046 break;
2047 case TT_4X4:
2048 pat = ~subblkpat & 0xF;
2049 for(j = 0; j < 4; j++) {
2050 last = subblkpat & (1 << (3 - j));
2051 i = 0;
2052 off = (j & 1) * 4 + (j & 2) * 16;
2053 while (!last) {
2054 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2055 i += skip;
2056 if(i > 15)
2057 break;
2058 idx = ff_vc1_simple_progressive_4x4_zz[i++];
2059 block[idx + off] = value * scale;
2060 if(!v->pquantizer)
2061 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2062 }
2063 if(!(subblkpat & (1 << (3 - j))) && !skip_block){
2064 if(i==1)
2065 s->dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2066 else
2067 s->dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2068 if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
2069 s->dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2070 if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
2071 s->dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2072 }
2073 }
2074 break;
2075 case TT_8X4:
2076 pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
2077 for(j = 0; j < 2; j++) {
2078 last = subblkpat & (1 << (1 - j));
2079 i = 0;
2080 off = j * 32;
2081 while (!last) {
2082 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2083 i += skip;
2084 if(i > 31)
2085 break;
2086 idx = v->zz_8x4[i++]+off;
2087 block[idx] = value * scale;
2088 if(!v->pquantizer)
2089 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2090 }
2091 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2092 if(i==1)
2093 s->dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
2094 else
2095 s->dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
2096 if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
2097 s->dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
2098 if(apply_filter && cbp_left & (2 << j))
2099 s->dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
2100 }
2101 }
2102 break;
2103 case TT_4X8:
2104 pat = ~(subblkpat*5) & 0xF;
2105 for(j = 0; j < 2; j++) {
2106 last = subblkpat & (1 << (1 - j));
2107 i = 0;
2108 off = j * 4;
2109 while (!last) {
2110 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2111 i += skip;
2112 if(i > 31)
2113 break;
2114 idx = v->zz_4x8[i++]+off;
2115 block[idx] = value * scale;
2116 if(!v->pquantizer)
2117 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2118 }
2119 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2120 if(i==1)
2121 s->dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
2122 else
2123 s->dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
2124 if(apply_filter && cbp_top & (2 << j))
2125 s->dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
2126 if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
2127 s->dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
2128 }
2129 }
2130 break;
2131 }
2132 return pat;
2133 }
2134
2135 /** @} */ // Macroblock group
2136
2137 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
2138 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2139
2140 /** Decode one P-frame MB (in Simple/Main profile)
2141 */
2142 static int vc1_decode_p_mb(VC1Context *v)
2143 {
2144 MpegEncContext *s = &v->s;
2145 GetBitContext *gb = &s->gb;
2146 int i, j;
2147 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2148 int cbp; /* cbp decoding stuff */
2149 int mqdiff, mquant; /* MB quantization */
2150 int ttmb = v->ttfrm; /* MB Transform type */
2151
2152 int mb_has_coeffs = 1; /* last_flag */
2153 int dmv_x, dmv_y; /* Differential MV components */
2154 int index, index1; /* LUT indexes */
2155 int val, sign; /* temp values */
2156 int first_block = 1;
2157 int dst_idx, off;
2158 int skipped, fourmv;
2159 int block_cbp = 0, pat;
2160 int apply_loop_filter;
2161
2162 mquant = v->pq; /* Loosy initialization */
2163
2164 if (v->mv_type_is_raw)
2165 fourmv = get_bits1(gb);
2166 else
2167 fourmv = v->mv_type_mb_plane[mb_pos];
2168 if (v->skip_is_raw)
2169 skipped = get_bits1(gb);
2170 else
2171 skipped = v->s.mbskip_table[mb_pos];
2172
2173 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2174 if (!fourmv) /* 1MV mode */
2175 {
2176 if (!skipped)
2177 {
2178 GET_MVDATA(dmv_x, dmv_y);
2179
2180 if (s->mb_intra) {
2181 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2182 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2183 }
2184 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2185 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2186
2187 /* FIXME Set DC val for inter block ? */
2188 if (s->mb_intra && !mb_has_coeffs)
2189 {
2190 GET_MQUANT();
2191 s->ac_pred = get_bits1(gb);
2192 cbp = 0;
2193 }
2194 else if (mb_has_coeffs)
2195 {
2196 if (s->mb_intra) s->ac_pred = get_bits1(gb);
2197 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2198 GET_MQUANT();
2199 }
2200 else
2201 {
2202 mquant = v->pq;
2203 cbp = 0;
2204 }
2205 s->current_picture.qscale_table[mb_pos] = mquant;
2206
2207 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2208 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2209 VC1_TTMB_VLC_BITS, 2);
2210 if(!s->mb_intra) vc1_mc_1mv(v, 0);
2211 dst_idx = 0;
2212 for (i=0; i<6; i++)
2213 {
2214 s->dc_val[0][s->block_index[i]] = 0;
2215 dst_idx += i >> 2;
2216 val = ((cbp >> (5 - i)) & 1);
2217 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2218 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2219 if(s->mb_intra) {
2220 /* check if prediction blocks A and C are available */
2221 v->a_avail = v->c_avail = 0;
2222 if(i == 2 || i == 3 || !s->first_slice_line)
2223 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2224 if(i == 1 || i == 3 || s->mb_x)
2225 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2226
2227 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2228 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2229 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2230 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2231 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2232 if(v->pq >= 9 && v->overlap) {
2233 if(v->c_avail)
2234 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2235 if(v->a_avail)
2236 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2237 }
2238 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2239 int left_cbp, top_cbp;
2240 if(i & 4){
2241 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2242 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2243 }else{
2244 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2245 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2246 }
2247 if(left_cbp & 0xC)
2248 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2249 if(top_cbp & 0xA)
2250 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2251 }
2252 block_cbp |= 0xF << (i << 2);
2253 } else if(val) {
2254 int left_cbp = 0, top_cbp = 0, filter = 0;
2255 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2256 filter = 1;
2257 if(i & 4){
2258 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2259 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2260 }else{
2261 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2262 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2263 }
2264 if(left_cbp & 0xC)
2265 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2266 if(top_cbp & 0xA)
2267 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2268 }
2269 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2270 block_cbp |= pat << (i << 2);
2271 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2272 first_block = 0;
2273 }
2274 }
2275 }
2276 else //Skipped
2277 {
2278 s->mb_intra = 0;
2279 for(i = 0; i < 6; i++) {
2280 v->mb_type[0][s->block_index[i]] = 0;
2281 s->dc_val[0][s->block_index[i]] = 0;
2282 }
2283 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2284 s->current_picture.qscale_table[mb_pos] = 0;
2285 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2286 vc1_mc_1mv(v, 0);
2287 return 0;
2288 }
2289 } //1MV mode
2290 else //4MV mode
2291 {
2292 if (!skipped /* unskipped MB */)
2293 {
2294 int intra_count = 0, coded_inter = 0;
2295 int is_intra[6], is_coded[6];
2296 /* Get CBPCY */
2297 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2298 for (i=0; i<6; i++)
2299 {
2300 val = ((cbp >> (5 - i)) & 1);
2301 s->dc_val[0][s->block_index[i]] = 0;
2302 s->mb_intra = 0;
2303 if(i < 4) {
2304 dmv_x = dmv_y = 0;
2305 s->mb_intra = 0;
2306 mb_has_coeffs = 0;
2307 if(val) {
2308 GET_MVDATA(dmv_x, dmv_y);
2309 }
2310 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2311 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2312 intra_count += s->mb_intra;
2313 is_intra[i] = s->mb_intra;
2314 is_coded[i] = mb_has_coeffs;
2315 }
2316 if(i&4){
2317 is_intra[i] = (intra_count >= 3);
2318 is_coded[i] = val;
2319 }
2320 if(i == 4) vc1_mc_4mv_chroma(v);
2321 v->mb_type[0][s->block_index[i]] = is_intra[i];
2322 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2323 }
2324 // if there are no coded blocks then don't do anything more
2325 if(!intra_count && !coded_inter) return 0;
2326 dst_idx = 0;
2327 GET_MQUANT();
2328 s->current_picture.qscale_table[mb_pos] = mquant;
2329 /* test if block is intra and has pred */
2330 {
2331 int intrapred = 0;
2332 for(i=0; i<6; i++)
2333 if(is_intra[i]) {
2334 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2335 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2336 intrapred = 1;
2337 break;
2338 }
2339 }
2340 if(intrapred)s->ac_pred = get_bits1(gb);
2341 else s->ac_pred = 0;
2342 }
2343 if (!v->ttmbf && coded_inter)
2344 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2345 for (i=0; i<6; i++)
2346 {
2347 dst_idx += i >> 2;
2348 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2349 s->mb_intra = is_intra[i];
2350 if (is_intra[i]) {
2351 /* check if prediction blocks A and C are available */
2352 v->a_avail = v->c_avail = 0;
2353 if(i == 2 || i == 3 || !s->first_slice_line)
2354 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2355 if(i == 1 || i == 3 || s->mb_x)
2356 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2357
2358 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2359 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2360 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2361 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2362 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2363 if(v->pq >= 9 && v->overlap) {
2364 if(v->c_avail)
2365 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2366 if(v->a_avail)
2367 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2368 }
2369 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2370 int left_cbp, top_cbp;
2371 if(i & 4){
2372 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2373 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2374 }else{
2375 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2376 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2377 }
2378 if(left_cbp & 0xC)
2379 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2380 if(top_cbp & 0xA)
2381 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2382 }
2383 block_cbp |= 0xF << (i << 2);
2384 } else if(is_coded[i]) {
2385 int left_cbp = 0, top_cbp = 0, filter = 0;
2386 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2387 filter = 1;
2388 if(i & 4){
2389 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2390 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2391 }else{
2392 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2393 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2394 }
2395 if(left_cbp & 0xC)
2396 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2397 if(top_cbp & 0xA)
2398 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2399 }
2400 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2401 block_cbp |= pat << (i << 2);
2402 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2403 first_block = 0;
2404 }
2405 }
2406 return 0;
2407 }
2408 else //Skipped MB
2409 {
2410 s->mb_intra = 0;
2411 s->current_picture.qscale_table[mb_pos] = 0;
2412 for (i=0; i<6; i++) {
2413 v->mb_type[0][s->block_index[i]] = 0;
2414 s->dc_val[0][s->block_index[i]] = 0;
2415 }
2416 for (i=0; i<4; i++)
2417 {
2418 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2419 vc1_mc_4mv_luma(v, i);
2420 }
2421 vc1_mc_4mv_chroma(v);
2422 s->current_picture.qscale_table[mb_pos] = 0;
2423 return 0;
2424 }
2425 }
2426 v->cbp[s->mb_x] = block_cbp;
2427
2428 /* Should never happen */
2429 return -1;
2430 }
2431
2432 /** Decode one B-frame MB (in Main profile)
2433 */
2434 static void vc1_decode_b_mb(VC1Context *v)
2435 {
2436 MpegEncContext *s = &v->s;
2437 GetBitContext *gb = &s->gb;
2438 int i, j;
2439 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2440 int cbp = 0; /* cbp decoding stuff */
2441 int mqdiff, mquant; /* MB quantization */
2442 int ttmb = v->ttfrm; /* MB Transform type */
2443 int mb_has_coeffs = 0; /* last_flag */
2444 int index, index1; /* LUT indexes */
2445 int val, sign; /* temp values */
2446 int first_block = 1;
2447 int dst_idx, off;
2448 int skipped, direct;
2449 int dmv_x[2], dmv_y[2];
2450 int bmvtype = BMV_TYPE_BACKWARD;
2451
2452 mquant = v->pq; /* Loosy initialization */
2453 s->mb_intra = 0;
2454
2455 if (v->dmb_is_raw)
2456 direct = get_bits1(gb);
2457 else
2458 direct = v->direct_mb_plane[mb_pos];
2459 if (v->skip_is_raw)
2460 skipped = get_bits1(gb);
2461 else
2462 skipped = v->s.mbskip_table[mb_pos];
2463
2464 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2465 for(i = 0; i < 6; i++) {
2466 v->mb_type[0][s->block_index[i]] = 0;
2467 s->dc_val[0][s->block_index[i]] = 0;
2468 }
2469 s->current_picture.qscale_table[mb_pos] = 0;
2470
2471 if (!direct) {
2472 if (!skipped) {
2473 GET_MVDATA(dmv_x[0], dmv_y[0]);
2474 dmv_x[1] = dmv_x[0];
2475 dmv_y[1] = dmv_y[0];
2476 }
2477 if(skipped || !s->mb_intra) {
2478 bmvtype = decode012(gb);
2479 switch(bmvtype) {
2480 case 0:
2481 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2482 break;
2483 case 1:
2484 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2485 break;
2486 case 2:
2487 bmvtype = BMV_TYPE_INTERPOLATED;
2488 dmv_x[0] = dmv_y[0] = 0;
2489 }
2490 }
2491 }
2492 for(i = 0; i < 6; i++)
2493 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2494
2495 if (skipped) {
2496 if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2497 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2498 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2499 return;
2500 }
2501 if (direct) {
2502 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2503 GET_MQUANT();
2504 s->mb_intra = 0;
2505 s->current_picture.qscale_table[mb_pos] = mquant;
2506 if(!v->ttmbf)
2507 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2508 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2509 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2510 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2511 } else {
2512 if(!mb_has_coeffs && !s->mb_intra) {
2513 /* no coded blocks - effectively skipped */
2514 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2515 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2516 return;
2517 }
2518 if(s->mb_intra && !mb_has_coeffs) {
2519 GET_MQUANT();
2520 s->current_picture.qscale_table[mb_pos] = mquant;
2521 s->ac_pred = get_bits1(gb);
2522 cbp = 0;
2523 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2524 } else {
2525 if(bmvtype == BMV_TYPE_INTERPOLATED) {
2526 GET_MVDATA(dmv_x[0], dmv_y[0]);
2527 if(!mb_has_coeffs) {
2528 /* interpolated skipped block */
2529 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2530 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2531 return;
2532 }
2533 }
2534 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2535 if(!s->mb_intra) {
2536 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2537 }
2538 if(s->mb_intra)
2539 s->ac_pred = get_bits1(gb);
2540 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2541 GET_MQUANT();
2542 s->current_picture.qscale_table[mb_pos] = mquant;
2543 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2544 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2545 }
2546 }
2547 dst_idx = 0;
2548 for (i=0; i<6; i++)
2549 {
2550 s->dc_val[0][s->block_index[i]] = 0;
2551 dst_idx += i >> 2;
2552 val = ((cbp >> (5 - i)) & 1);
2553 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2554 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2555 if(s->mb_intra) {
2556 /* check if prediction blocks A and C are available */
2557 v->a_avail = v->c_avail = 0;
2558 if(i == 2 || i == 3 || !s->first_slice_line)
2559 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2560 if(i == 1 || i == 3 || s->mb_x)
2561 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2562
2563 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2564 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2565 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2566 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2567 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2568 } else if(val) {
2569 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
2570 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2571 first_block = 0;
2572 }
2573 }
2574 }
2575
2576 /** Decode blocks of I-frame
2577 */
2578 static void vc1_decode_i_blocks(VC1Context *v)
2579 {
2580 int k, j;
2581 MpegEncContext *s = &v->s;
2582 int cbp, val;
2583 uint8_t *coded_val;
2584 int mb_pos;
2585
2586 /* select codingmode used for VLC tables selection */
2587 switch(v->y_ac_table_index){
2588 case 0:
2589 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2590 break;
2591 case 1:
2592 v->codingset = CS_HIGH_MOT_INTRA;
2593 break;
2594 case 2:
2595 v->codingset = CS_MID_RATE_INTRA;
2596 break;
2597 }
2598
2599 switch(v->c_ac_table_index){
2600 case 0:
2601 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2602 break;
2603 case 1:
2604 v->codingset2 = CS_HIGH_MOT_INTER;
2605 break;
2606 case 2:
2607 v->codingset2 = CS_MID_RATE_INTER;
2608 break;
2609 }
2610
2611 /* Set DC scale - y and c use the same */
2612 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2613 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2614
2615 //do frame decode
2616 s->mb_x = s->mb_y = 0;
2617 s->mb_intra = 1;
2618 s->first_slice_line = 1;
2619 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2620 s->mb_x = 0;
2621 ff_init_block_index(s);
2622 for(; s->mb_x < s->mb_width; s->mb_x++) {
2623 ff_update_block_index(s);
2624 s->dsp.clear_blocks(s->block[0]);
2625 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2626 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2627 s->current_picture.qscale_table[mb_pos] = v->pq;
2628 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2629 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2630
2631 // do actual MB decoding and displaying
2632 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2633 v->s.ac_pred = get_bits1(&v->s.gb);
2634
2635 for(k = 0; k < 6; k++) {
2636 val = ((cbp >> (5 - k)) & 1);
2637
2638 if (k < 4) {
2639 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2640 val = val ^ pred;
2641 *coded_val = val;
2642 }
2643 cbp |= val << (5 - k);
2644
2645 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2646
2647 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2648 if(v->pq >= 9 && v->overlap) {
2649 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2650 }
2651 }
2652
2653 vc1_put_block(v, s->block);
2654 if(v->pq >= 9 && v->overlap) {
2655 if(s->mb_x) {
2656 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2657 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2658 if(!(s->flags & CODEC_FLAG_GRAY)) {
2659 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2660 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2661 }
2662 }
2663 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2664 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2665 if(!s->first_slice_line) {
2666 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2667 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2668 if(!(s->flags & CODEC_FLAG_GRAY)) {
2669 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2670 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2671 }
2672 }
2673 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2674 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2675 }
2676 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2677
2678 if(get_bits_count(&s->gb) > v->bits) {
2679 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2680 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2681 return;
2682 }
2683 }
2684 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2685 s->first_slice_line = 0;
2686 }
2687 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2688 }
2689
2690 /** Decode blocks of I-frame for advanced profile
2691 */
2692 static void vc1_decode_i_blocks_adv(VC1Context *v)
2693 {
2694 int k, j;
2695 MpegEncContext *s = &v->s;
2696 int cbp, val;
2697 uint8_t *coded_val;
2698 int mb_pos;
2699 int mquant = v->pq;
2700 int mqdiff;
2701 int overlap;
2702 GetBitContext *gb = &s->gb;
2703
2704 /* select codingmode used for VLC tables selection */
2705 switch(v->y_ac_table_index){
2706 case 0:
2707 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2708 break;
2709 case 1:
2710 v->codingset = CS_HIGH_MOT_INTRA;
2711 break;
2712 case 2:
2713 v->codingset = CS_MID_RATE_INTRA;
2714 break;
2715 }
2716
2717 switch(v->c_ac_table_index){
2718 case 0:
2719 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2720 break;
2721 case 1:
2722 v->codingset2 = CS_HIGH_MOT_INTER;
2723 break;
2724 case 2:
2725 v->codingset2 = CS_MID_RATE_INTER;
2726 break;
2727 }
2728
2729 //do frame decode
2730 s->mb_x = s->mb_y = 0;
2731 s->mb_intra = 1;
2732 s->first_slice_line = 1;
2733 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2734 s->mb_x = 0;
2735 ff_init_block_index(s);
2736 for(;s->mb_x < s->mb_width; s->mb_x++) {
2737 ff_update_block_index(s);
2738 s->dsp.clear_blocks(s->block[0]);
2739 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2740 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2741 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2742 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2743
2744 // do actual MB decoding and displaying
2745 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2746 if(v->acpred_is_raw)
2747 v->s.ac_pred = get_bits1(&v->s.gb);
2748 else
2749 v->s.ac_pred = v->acpred_plane[mb_pos];
2750
2751 if(v->condover == CONDOVER_SELECT) {
2752 if(v->overflg_is_raw)
2753 overlap = get_bits1(&v->s.gb);
2754 else
2755 overlap = v->over_flags_plane[mb_pos];
2756 } else
2757 overlap = (v->condover == CONDOVER_ALL);
2758
2759 GET_MQUANT();
2760
2761 s->current_picture.qscale_table[mb_pos] = mquant;
2762 /* Set DC scale - y and c use the same */
2763 s->y_dc_scale = s->y_dc_scale_table[mquant];
2764 s->c_dc_scale = s->c_dc_scale_table[mquant];
2765
2766 for(k = 0; k < 6; k++) {
2767 val = ((cbp >> (5 - k)) & 1);
2768
2769 if (k < 4) {
2770 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2771 val = val ^ pred;
2772 *coded_val = val;
2773 }
2774 cbp |= val << (5 - k);
2775
2776 v->a_avail = !s->first_slice_line || (k==2 || k==3);
2777 v->c_avail = !!s->mb_x || (k==1 || k==3);
2778
2779 vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
2780
2781 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2782 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2783 }
2784
2785 vc1_put_block(v, s->block);
2786 if(overlap) {
2787 if(s->mb_x) {
2788 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2789 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2790 if(!(s->flags & CODEC_FLAG_GRAY)) {
2791 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2792 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2793 }
2794 }
2795 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2796 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2797 if(!s->first_slice_line) {
2798 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2799 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2800 if(!(s->flags & CODEC_FLAG_GRAY)) {
2801 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2802 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2803 }
2804 }
2805 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2806 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2807 }
2808 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2809
2810 if(get_bits_count(&s->gb) > v->bits) {
2811 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2812 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2813 return;
2814 }
2815 }
2816 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2817 s->first_slice_line = 0;
2818 }
2819 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2820 }
2821
2822 static void vc1_decode_p_blocks(VC1Context *v)
2823 {
2824 MpegEncContext *s = &v->s;
2825
2826 /* select codingmode used for VLC tables selection */
2827 switch(v->c_ac_table_index){
2828 case 0:
2829 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2830 break;
2831 case 1:
2832 v->codingset = CS_HIGH_MOT_INTRA;
2833 break;
2834 case 2:
2835 v->codingset = CS_MID_RATE_INTRA;
2836 break;
2837 }
2838
2839 switch(v->c_ac_table_index){
2840 case 0:
2841 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2842 break;
2843 case 1:
2844 v->codingset2 = CS_HIGH_MOT_INTER;
2845 break;
2846 case 2:
2847 v->codingset2 = CS_MID_RATE_INTER;
2848 break;
2849 }
2850
2851 s->first_slice_line = 1;
2852 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2853 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2854 s->mb_x = 0;
2855 ff_init_block_index(s);
2856 for(; s->mb_x < s->mb_width; s->mb_x++) {
2857 ff_update_block_index(s);
2858
2859 vc1_decode_p_mb(v);
2860 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2861 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2862 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2863 return;
2864 }
2865 }
2866 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2867 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2868 s->first_slice_line = 0;
2869 }
2870 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2871 }
2872
2873 static void vc1_decode_b_blocks(VC1Context *v)
2874 {
2875 MpegEncContext *s = &v->s;
2876
2877 /* select codingmode used for VLC tables selection */
2878 switch(v->c_ac_table_index){
2879 case 0:
2880 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2881 break;
2882 case 1:
2883 v->codingset = CS_HIGH_MOT_INTRA;
2884 break;
2885 case 2:
2886 v->codingset = CS_MID_RATE_INTRA;
2887 break;
2888 }
2889
2890 switch(v->c_ac_table_index){
2891 case 0:
2892 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2893 break;
2894 case 1:
2895 v->codingset2 = CS_HIGH_MOT_INTER;
2896 break;
2897 case 2:
2898 v->codingset2 = CS_MID_RATE_INTER;
2899 break;
2900 }
2901
2902 s->first_slice_line = 1;
2903 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2904 s->mb_x = 0;
2905 ff_init_block_index(s);
2906 for(; s->mb_x < s->mb_width; s->mb_x++) {
2907 ff_update_block_index(s);
2908
2909 vc1_decode_b_mb(v);
2910 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2911 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2912 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2913 return;
2914 }
2915 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2916 }
2917 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2918 s->first_slice_line = 0;
2919 }
2920 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2921 }
2922
2923 static void vc1_decode_skip_blocks(VC1Context *v)
2924 {
2925 MpegEncContext *s = &v->s;
2926
2927 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2928 s->first_slice_line = 1;
2929 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2930 s->mb_x = 0;
2931 ff_init_block_index(s);
2932 ff_update_block_index(s);
2933 memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2934 memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2935 memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2936 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2937 s->first_slice_line = 0;
2938 }
2939 s->pict_type = FF_P_TYPE;
2940 }
2941
2942 static void vc1_decode_blocks(VC1Context *v)
2943 {
2944
2945 v->s.esc3_level_length = 0;
2946 if(v->x8_type){
2947 ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
2948 }else{
2949
2950 switch(v->s.pict_type) {
2951 case FF_I_TYPE:
2952 if(v->profile == PROFILE_ADVANCED)
2953 vc1_decode_i_blocks_adv(v);
2954 else
2955 vc1_decode_i_blocks(v);
2956 break;
2957 case FF_P_TYPE:
2958 if(v->p_frame_skipped)
2959 vc1_decode_skip_blocks(v);
2960 else
2961 vc1_decode_p_blocks(v);
2962 break;
2963 case FF_B_TYPE:
2964 if(v->bi_type){
2965 if(v->profile == PROFILE_ADVANCED)
2966 vc1_decode_i_blocks_adv(v);
2967 else
2968 vc1_decode_i_blocks(v);
2969 }else
2970 vc1_decode_b_blocks(v);
2971 break;
2972 }
2973 }
2974 }
2975
2976 /** Initialize a VC1/WMV3 decoder
2977 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2978 * @todo TODO: Decypher remaining bits in extra_data
2979 */
2980 static av_cold int vc1_decode_init(AVCodecContext *avctx)
2981 {
2982 VC1Context *v = avctx->priv_data;
2983 MpegEncContext *s = &v->s;
2984 GetBitContext gb;
2985
2986 if (!avctx->extradata_size || !avctx->extradata) return -1;
2987 if (!(avctx->flags & CODEC_FLAG_GRAY))
2988 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
2989 else
2990 avctx->pix_fmt = PIX_FMT_GRAY8;
2991 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2992 v->s.avctx = avctx;
2993 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2994 v->s.flags |= CODEC_FLAG_EMU_EDGE;
2995
2996 if(avctx->idct_algo==FF_IDCT_AUTO){
2997 avctx->idct_algo=FF_IDCT_WMV2;
2998 }
2999
3000 if(ff_h263_decode_init(avctx) < 0)
3001 return -1;
3002 if (vc1_init_common(v) < 0) return -1;
3003
3004 avctx->coded_width = avctx->width;
3005 avctx->coded_height = avctx->height;
3006 if (avctx->codec_id == CODEC_ID_WMV3)
3007 {
3008 int count = 0;
3009
3010 // looks like WMV3 has a sequence header stored in the extradata
3011 // advanced sequence header may be before the first frame
3012 // the last byte of the extradata is a version number, 1 for the
3013 // samples we can decode
3014
3015 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3016
3017 if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3018 return -1;
3019
3020 count = avctx->extradata_size*8 - get_bits_count(&gb);
3021 if (count>0)
3022 {
3023 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3024 count, get_bits(&gb, count));
3025 }
3026 else if (count < 0)
3027 {
3028 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3029 }
3030 } else { // VC1/WVC1
3031 const uint8_t *start = avctx->extradata;
3032 uint8_t *end = avctx->extradata + avctx->extradata_size;
3033 const uint8_t *next;
3034 int size, buf2_size;
3035 uint8_t *buf2 = NULL;
3036 int seq_initialized = 0, ep_initialized = 0;
3037
3038 if(avctx->extradata_size < 16) {
3039 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3040 return -1;
3041 }
3042
3043 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3044 if(start[0]) start++; // in WVC1 extradata first byte is its size
3045 next = start;
3046 for(; next < end; start = next){
3047 next = find_next_marker(start + 4, end);
3048 size = next - start - 4;
3049 if(size <= 0) continue;
3050 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3051 init_get_bits(&gb, buf2, buf2_size * 8);
3052 switch(AV_RB32(start)){
3053 case VC1_CODE_SEQHDR:
3054 if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3055 av_free(buf2);
3056 return -1;
3057 }
3058 seq_initialized = 1;
3059 break;
3060 case VC1_CODE_ENTRYPOINT:
3061 if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3062 av_free(buf2);
3063 return -1;
3064 }
3065 ep_initialized = 1;
3066