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