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