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