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