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
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24 * @file libavcodec/vc1dec.c
25 * VC-1 and WMV3 decoder
31 #include "mpegvideo.h"
34 #include "vc1acdata.h"
35 #include "msmpeg4data.h"
37 #include "simple_idct.h"
39 #include "vdpau_internal.h"
44 #define MB_INTRA_VLC_BITS 9
47 static const uint16_t table_mb_intra
[64][2];
50 static const uint16_t vlc_offs
[] = {
51 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
52 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620,
53 9262, 10202, 10756, 11310, 12228, 15078
57 * Init VC-1 specific tables and VC1Context members
58 * @param v The VC1Context to initialize
61 static int vc1_init_common(VC1Context
*v
)
65 static VLC_TYPE vlc_table
[15078][2];
67 v
->hrd_rate
= v
->hrd_buffer
= NULL
;
72 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc
, VC1_BFRACTION_VLC_BITS
, 23,
73 ff_vc1_bfraction_bits
, 1, 1,
74 ff_vc1_bfraction_codes
, 1, 1, 1 << VC1_BFRACTION_VLC_BITS
);
75 INIT_VLC_STATIC(&ff_vc1_norm2_vlc
, VC1_NORM2_VLC_BITS
, 4,
76 ff_vc1_norm2_bits
, 1, 1,
77 ff_vc1_norm2_codes
, 1, 1, 1 << VC1_NORM2_VLC_BITS
);
78 INIT_VLC_STATIC(&ff_vc1_norm6_vlc
, VC1_NORM6_VLC_BITS
, 64,
79 ff_vc1_norm6_bits
, 1, 1,
80 ff_vc1_norm6_codes
, 2, 2, 556);
81 INIT_VLC_STATIC(&ff_vc1_imode_vlc
, VC1_IMODE_VLC_BITS
, 7,
82 ff_vc1_imode_bits
, 1, 1,
83 ff_vc1_imode_codes
, 1, 1, 1 << VC1_IMODE_VLC_BITS
);
86 ff_vc1_ttmb_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+0]];
87 ff_vc1_ttmb_vlc
[i
].table_allocated
= vlc_offs
[i
*3+1] - vlc_offs
[i
*3+0];
88 init_vlc(&ff_vc1_ttmb_vlc
[i
], VC1_TTMB_VLC_BITS
, 16,
89 ff_vc1_ttmb_bits
[i
], 1, 1,
90 ff_vc1_ttmb_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
91 ff_vc1_ttblk_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+1]];
92 ff_vc1_ttblk_vlc
[i
].table_allocated
= vlc_offs
[i
*3+2] - vlc_offs
[i
*3+1];
93 init_vlc(&ff_vc1_ttblk_vlc
[i
], VC1_TTBLK_VLC_BITS
, 8,
94 ff_vc1_ttblk_bits
[i
], 1, 1,
95 ff_vc1_ttblk_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
96 ff_vc1_subblkpat_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+2]];
97 ff_vc1_subblkpat_vlc
[i
].table_allocated
= vlc_offs
[i
*3+3] - vlc_offs
[i
*3+2];
98 init_vlc(&ff_vc1_subblkpat_vlc
[i
], VC1_SUBBLKPAT_VLC_BITS
, 15,
99 ff_vc1_subblkpat_bits
[i
], 1, 1,
100 ff_vc1_subblkpat_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
104 ff_vc1_4mv_block_pattern_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+9]];
105 ff_vc1_4mv_block_pattern_vlc
[i
].table_allocated
= vlc_offs
[i
*3+10] - vlc_offs
[i
*3+9];
106 init_vlc(&ff_vc1_4mv_block_pattern_vlc
[i
], VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 16,
107 ff_vc1_4mv_block_pattern_bits
[i
], 1, 1,
108 ff_vc1_4mv_block_pattern_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
109 ff_vc1_cbpcy_p_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+10]];
110 ff_vc1_cbpcy_p_vlc
[i
].table_allocated
= vlc_offs
[i
*3+11] - vlc_offs
[i
*3+10];
111 init_vlc(&ff_vc1_cbpcy_p_vlc
[i
], VC1_CBPCY_P_VLC_BITS
, 64,
112 ff_vc1_cbpcy_p_bits
[i
], 1, 1,
113 ff_vc1_cbpcy_p_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
114 ff_vc1_mv_diff_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
*3+11]];
115 ff_vc1_mv_diff_vlc
[i
].table_allocated
= vlc_offs
[i
*3+12] - vlc_offs
[i
*3+11];
116 init_vlc(&ff_vc1_mv_diff_vlc
[i
], VC1_MV_DIFF_VLC_BITS
, 73,
117 ff_vc1_mv_diff_bits
[i
], 1, 1,
118 ff_vc1_mv_diff_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
121 ff_vc1_ac_coeff_table
[i
].table
= &vlc_table
[vlc_offs
[i
+21]];
122 ff_vc1_ac_coeff_table
[i
].table_allocated
= vlc_offs
[i
+22] - vlc_offs
[i
+21];
123 init_vlc(&ff_vc1_ac_coeff_table
[i
], AC_VLC_BITS
, vc1_ac_sizes
[i
],
124 &vc1_ac_tables
[i
][0][1], 8, 4,
125 &vc1_ac_tables
[i
][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC
);
127 //FIXME: switching to INIT_VLC_STATIC() results in incorrect decoding
128 init_vlc(&ff_msmp4_mb_i_vlc
, MB_INTRA_VLC_BITS
, 64,
129 &ff_msmp4_mb_i_table
[0][1], 4, 2,
130 &ff_msmp4_mb_i_table
[0][0], 4, 2, INIT_VLC_USE_STATIC
);
136 v
->mvrange
= 0; /* 7.1.1.18, p80 */
141 /***********************************************************************/
143 * @defgroup vc1bitplane VC-1 Bitplane decoding
161 /** @} */ //imode defines
164 /** @} */ //Bitplane group
166 static void vc1_loop_filter_iblk(MpegEncContext
*s
, int pq
)
169 if(!s
->first_slice_line
)
170 s
->dsp
.vc1_v_loop_filter16(s
->dest
[0], s
->linesize
, pq
);
171 s
->dsp
.vc1_v_loop_filter16(s
->dest
[0] + 8*s
->linesize
, s
->linesize
, pq
);
172 for(i
= !s
->mb_x
*8; i
< 16; i
+= 8)
173 s
->dsp
.vc1_h_loop_filter16(s
->dest
[0] + i
, s
->linesize
, pq
);
174 for(j
= 0; j
< 2; j
++){
175 if(!s
->first_slice_line
)
176 s
->dsp
.vc1_v_loop_filter8(s
->dest
[j
+1], s
->uvlinesize
, pq
);
178 s
->dsp
.vc1_h_loop_filter8(s
->dest
[j
+1], s
->uvlinesize
, pq
);
182 /** Put block onto picture
184 static void vc1_put_block(VC1Context
*v
, DCTELEM block
[6][64])
188 DSPContext
*dsp
= &v
->s
.dsp
;
192 for(k
= 0; k
< 6; k
++)
193 for(j
= 0; j
< 8; j
++)
194 for(i
= 0; i
< 8; i
++)
195 block
[k
][i
+ j
*8] = ((block
[k
][i
+ j
*8] - 128) << 1) + 128;
198 ys
= v
->s
.current_picture
.linesize
[0];
199 us
= v
->s
.current_picture
.linesize
[1];
200 vs
= v
->s
.current_picture
.linesize
[2];
203 dsp
->put_pixels_clamped(block
[0], Y
, ys
);
204 dsp
->put_pixels_clamped(block
[1], Y
+ 8, ys
);
206 dsp
->put_pixels_clamped(block
[2], Y
, ys
);
207 dsp
->put_pixels_clamped(block
[3], Y
+ 8, ys
);
209 if(!(v
->s
.flags
& CODEC_FLAG_GRAY
)) {
210 dsp
->put_pixels_clamped(block
[4], v
->s
.dest
[1], us
);
211 dsp
->put_pixels_clamped(block
[5], v
->s
.dest
[2], vs
);
215 /** Do motion compensation over 1 macroblock
216 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
218 static void vc1_mc_1mv(VC1Context
*v
, int dir
)
220 MpegEncContext
*s
= &v
->s
;
221 DSPContext
*dsp
= &v
->s
.dsp
;
222 uint8_t *srcY
, *srcU
, *srcV
;
223 int dxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
225 if(!v
->s
.last_picture
.data
[0])return;
227 mx
= s
->mv
[dir
][0][0];
228 my
= s
->mv
[dir
][0][1];
230 // store motion vectors for further use in B frames
231 if(s
->pict_type
== FF_P_TYPE
) {
232 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = mx
;
233 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = my
;
235 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
236 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
238 uvmx
= uvmx
+ ((uvmx
<0)?(uvmx
&1):-(uvmx
&1));
239 uvmy
= uvmy
+ ((uvmy
<0)?(uvmy
&1):-(uvmy
&1));
242 srcY
= s
->last_picture
.data
[0];
243 srcU
= s
->last_picture
.data
[1];
244 srcV
= s
->last_picture
.data
[2];
246 srcY
= s
->next_picture
.data
[0];
247 srcU
= s
->next_picture
.data
[1];
248 srcV
= s
->next_picture
.data
[2];
251 src_x
= s
->mb_x
* 16 + (mx
>> 2);
252 src_y
= s
->mb_y
* 16 + (my
>> 2);
253 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
254 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
256 if(v
->profile
!= PROFILE_ADVANCED
){
257 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
258 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
259 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
260 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
262 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
263 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
264 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
265 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
268 srcY
+= src_y
* s
->linesize
+ src_x
;
269 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
270 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
272 /* for grayscale we should not try to read from unknown area */
273 if(s
->flags
& CODEC_FLAG_GRAY
) {
274 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
275 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
278 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
279 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 16 - s
->mspel
*3
280 || (unsigned)(src_y
- s
->mspel
) > s
->v_edge_pos
- (my
&3) - 16 - s
->mspel
*3){
281 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
283 srcY
-= s
->mspel
* (1 + s
->linesize
);
284 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 17+s
->mspel
*2, 17+s
->mspel
*2,
285 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
286 srcY
= s
->edge_emu_buffer
;
287 ff_emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8+1, 8+1,
288 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
289 ff_emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
290 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
293 /* if we deal with range reduction we need to scale source blocks */
299 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
300 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
303 src
= srcU
; src2
= srcV
;
304 for(j
= 0; j
< 9; j
++) {
305 for(i
= 0; i
< 9; i
++) {
306 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
307 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
309 src
+= s
->uvlinesize
;
310 src2
+= s
->uvlinesize
;
313 /* if we deal with intensity compensation we need to scale source blocks */
314 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
319 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
320 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = v
->luty
[src
[i
]];
323 src
= srcU
; src2
= srcV
;
324 for(j
= 0; j
< 9; j
++) {
325 for(i
= 0; i
< 9; i
++) {
326 src
[i
] = v
->lutuv
[src
[i
]];
327 src2
[i
] = v
->lutuv
[src2
[i
]];
329 src
+= s
->uvlinesize
;
330 src2
+= s
->uvlinesize
;
333 srcY
+= s
->mspel
* (1 + s
->linesize
);
337 dxy
= ((my
& 3) << 2) | (mx
& 3);
338 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] , srcY
, s
->linesize
, v
->rnd
);
339 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8, srcY
+ 8, s
->linesize
, v
->rnd
);
340 srcY
+= s
->linesize
* 8;
341 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
, srcY
, s
->linesize
, v
->rnd
);
342 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
343 } else { // hpel mc - always used for luma
344 dxy
= (my
& 2) | ((mx
& 2) >> 1);
347 dsp
->put_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
349 dsp
->put_no_rnd_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
352 if(s
->flags
& CODEC_FLAG_GRAY
) return;
353 /* Chroma MC always uses qpel bilinear */
357 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
358 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
360 dsp
->put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
361 dsp
->put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
365 /** Do motion compensation for 4-MV macroblock - luminance block
367 static void vc1_mc_4mv_luma(VC1Context
*v
, int n
)
369 MpegEncContext
*s
= &v
->s
;
370 DSPContext
*dsp
= &v
->s
.dsp
;
372 int dxy
, mx
, my
, src_x
, src_y
;
375 if(!v
->s
.last_picture
.data
[0])return;
378 srcY
= s
->last_picture
.data
[0];
380 off
= s
->linesize
* 4 * (n
&2) + (n
&1) * 8;
382 src_x
= s
->mb_x
* 16 + (n
&1) * 8 + (mx
>> 2);
383 src_y
= s
->mb_y
* 16 + (n
&2) * 4 + (my
>> 2);
385 if(v
->profile
!= PROFILE_ADVANCED
){
386 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
387 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
389 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
390 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
393 srcY
+= src_y
* s
->linesize
+ src_x
;
395 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
396 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 8 - s
->mspel
*2
397 || (unsigned)(src_y
- s
->mspel
) > s
->v_edge_pos
- (my
&3) - 8 - s
->mspel
*2){
398 srcY
-= s
->mspel
* (1 + s
->linesize
);
399 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 9+s
->mspel
*2, 9+s
->mspel
*2,
400 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
401 srcY
= s
->edge_emu_buffer
;
402 /* if we deal with range reduction we need to scale source blocks */
408 for(j
= 0; j
< 9 + s
->mspel
*2; j
++) {
409 for(i
= 0; i
< 9 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
413 /* if we deal with intensity compensation we need to scale source blocks */
414 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
419 for(j
= 0; j
< 9 + s
->mspel
*2; j
++) {
420 for(i
= 0; i
< 9 + s
->mspel
*2; i
++) src
[i
] = v
->luty
[src
[i
]];
424 srcY
+= s
->mspel
* (1 + s
->linesize
);
428 dxy
= ((my
& 3) << 2) | (mx
& 3);
429 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, v
->rnd
);
430 } else { // hpel mc - always used for luma
431 dxy
= (my
& 2) | ((mx
& 2) >> 1);
433 dsp
->put_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
435 dsp
->put_no_rnd_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
439 static inline int median4(int a
, int b
, int c
, int d
)
442 if(c
< d
) return (FFMIN(b
, d
) + FFMAX(a
, c
)) / 2;
443 else return (FFMIN(b
, c
) + FFMAX(a
, d
)) / 2;
445 if(c
< d
) return (FFMIN(a
, d
) + FFMAX(b
, c
)) / 2;
446 else return (FFMIN(a
, c
) + FFMAX(b
, d
)) / 2;
451 /** Do motion compensation for 4-MV macroblock - both chroma blocks
453 static void vc1_mc_4mv_chroma(VC1Context
*v
)
455 MpegEncContext
*s
= &v
->s
;
456 DSPContext
*dsp
= &v
->s
.dsp
;
457 uint8_t *srcU
, *srcV
;
458 int uvmx
, uvmy
, uvsrc_x
, uvsrc_y
;
459 int i
, idx
, tx
= 0, ty
= 0;
460 int mvx
[4], mvy
[4], intra
[4];
461 static const int count
[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
463 if(!v
->s
.last_picture
.data
[0])return;
464 if(s
->flags
& CODEC_FLAG_GRAY
) return;
466 for(i
= 0; i
< 4; i
++) {
467 mvx
[i
] = s
->mv
[0][i
][0];
468 mvy
[i
] = s
->mv
[0][i
][1];
469 intra
[i
] = v
->mb_type
[0][s
->block_index
[i
]];
472 /* calculate chroma MV vector from four luma MVs */
473 idx
= (intra
[3] << 3) | (intra
[2] << 2) | (intra
[1] << 1) | intra
[0];
474 if(!idx
) { // all blocks are inter
475 tx
= median4(mvx
[0], mvx
[1], mvx
[2], mvx
[3]);
476 ty
= median4(mvy
[0], mvy
[1], mvy
[2], mvy
[3]);
477 } else if(count
[idx
] == 1) { // 3 inter blocks
480 tx
= mid_pred(mvx
[1], mvx
[2], mvx
[3]);
481 ty
= mid_pred(mvy
[1], mvy
[2], mvy
[3]);
484 tx
= mid_pred(mvx
[0], mvx
[2], mvx
[3]);
485 ty
= mid_pred(mvy
[0], mvy
[2], mvy
[3]);
488 tx
= mid_pred(mvx
[0], mvx
[1], mvx
[3]);
489 ty
= mid_pred(mvy
[0], mvy
[1], mvy
[3]);
492 tx
= mid_pred(mvx
[0], mvx
[1], mvx
[2]);
493 ty
= mid_pred(mvy
[0], mvy
[1], mvy
[2]);
496 } else if(count
[idx
] == 2) {
498 for(i
=0; i
<3;i
++) if(!intra
[i
]) {t1
= i
; break;}
499 for(i
= t1
+1; i
<4; i
++)if(!intra
[i
]) {t2
= i
; break;}
500 tx
= (mvx
[t1
] + mvx
[t2
]) / 2;
501 ty
= (mvy
[t1
] + mvy
[t2
]) / 2;
503 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
504 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
505 return; //no need to do MC for inter blocks
508 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = tx
;
509 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = ty
;
510 uvmx
= (tx
+ ((tx
&3) == 3)) >> 1;
511 uvmy
= (ty
+ ((ty
&3) == 3)) >> 1;
513 uvmx
= uvmx
+ ((uvmx
<0)?(uvmx
&1):-(uvmx
&1));
514 uvmy
= uvmy
+ ((uvmy
<0)?(uvmy
&1):-(uvmy
&1));
517 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
518 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
520 if(v
->profile
!= PROFILE_ADVANCED
){
521 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
522 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
524 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
525 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
528 srcU
= s
->last_picture
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
529 srcV
= s
->last_picture
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
530 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
531 || (unsigned)uvsrc_x
> (s
->h_edge_pos
>> 1) - 9
532 || (unsigned)uvsrc_y
> (s
->v_edge_pos
>> 1) - 9){
533 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcU
, s
->uvlinesize
, 8+1, 8+1,
534 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
535 ff_emulated_edge_mc(s
->edge_emu_buffer
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
536 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
537 srcU
= s
->edge_emu_buffer
;
538 srcV
= s
->edge_emu_buffer
+ 16;
540 /* if we deal with range reduction we need to scale source blocks */
545 src
= srcU
; src2
= srcV
;
546 for(j
= 0; j
< 9; j
++) {
547 for(i
= 0; i
< 9; i
++) {
548 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
549 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
551 src
+= s
->uvlinesize
;
552 src2
+= s
->uvlinesize
;
555 /* if we deal with intensity compensation we need to scale source blocks */
556 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
560 src
= srcU
; src2
= srcV
;
561 for(j
= 0; j
< 9; j
++) {
562 for(i
= 0; i
< 9; i
++) {
563 src
[i
] = v
->lutuv
[src
[i
]];
564 src2
[i
] = v
->lutuv
[src2
[i
]];
566 src
+= s
->uvlinesize
;
567 src2
+= s
->uvlinesize
;
572 /* Chroma MC always uses qpel bilinear */
576 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
577 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
579 dsp
->put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
580 dsp
->put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
584 /***********************************************************************/
586 * @defgroup vc1block VC-1 Block-level functions
587 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
593 * @brief Get macroblock-level quantizer scale
595 #define GET_MQUANT() \
599 if (v->dqprofile == DQPROFILE_ALL_MBS) \
603 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
607 mqdiff = get_bits(gb, 3); \
608 if (mqdiff != 7) mquant = v->pq + mqdiff; \
609 else mquant = get_bits(gb, 5); \
612 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
613 edges = 1 << v->dqsbedge; \
614 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
615 edges = (3 << v->dqsbedge) % 15; \
616 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
618 if((edges&1) && !s->mb_x) \
620 if((edges&2) && s->first_slice_line) \
622 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
624 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
629 * @def GET_MVDATA(_dmv_x, _dmv_y)
630 * @brief Get MV differentials
631 * @see MVDATA decoding from 8.3.5.2, p(1)20
632 * @param _dmv_x Horizontal differential for decoded MV
633 * @param _dmv_y Vertical differential for decoded MV
635 #define GET_MVDATA(_dmv_x, _dmv_y) \
636 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
637 VC1_MV_DIFF_VLC_BITS, 2); \
643 else mb_has_coeffs = 0; \
645 if (!index) { _dmv_x = _dmv_y = 0; } \
646 else if (index == 35) \
648 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
649 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
651 else if (index == 36) \
660 if (!s->quarter_sample && index1 == 5) val = 1; \
662 if(size_table[index1] - val > 0) \
663 val = get_bits(gb, size_table[index1] - val); \
665 sign = 0 - (val&1); \
666 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
669 if (!s->quarter_sample && index1 == 5) val = 1; \
671 if(size_table[index1] - val > 0) \
672 val = get_bits(gb, size_table[index1] - val); \
674 sign = 0 - (val&1); \
675 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
678 /** Predict and set motion vector
680 static inline void vc1_pred_mv(MpegEncContext
*s
, int n
, int dmv_x
, int dmv_y
, int mv1
, int r_x
, int r_y
, uint8_t* is_intra
)
682 int xy
, wrap
, off
= 0;
687 /* scale MV difference to be quad-pel */
688 dmv_x
<<= 1 - s
->quarter_sample
;
689 dmv_y
<<= 1 - s
->quarter_sample
;
692 xy
= s
->block_index
[n
];
695 s
->mv
[0][n
][0] = s
->current_picture
.motion_val
[0][xy
][0] = 0;
696 s
->mv
[0][n
][1] = s
->current_picture
.motion_val
[0][xy
][1] = 0;
697 s
->current_picture
.motion_val
[1][xy
][0] = 0;
698 s
->current_picture
.motion_val
[1][xy
][1] = 0;
699 if(mv1
) { /* duplicate motion data for 1-MV block */
700 s
->current_picture
.motion_val
[0][xy
+ 1][0] = 0;
701 s
->current_picture
.motion_val
[0][xy
+ 1][1] = 0;
702 s
->current_picture
.motion_val
[0][xy
+ wrap
][0] = 0;
703 s
->current_picture
.motion_val
[0][xy
+ wrap
][1] = 0;
704 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][0] = 0;
705 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][1] = 0;
706 s
->current_picture
.motion_val
[1][xy
+ 1][0] = 0;
707 s
->current_picture
.motion_val
[1][xy
+ 1][1] = 0;
708 s
->current_picture
.motion_val
[1][xy
+ wrap
][0] = 0;
709 s
->current_picture
.motion_val
[1][xy
+ wrap
][1] = 0;
710 s
->current_picture
.motion_val
[1][xy
+ wrap
+ 1][0] = 0;
711 s
->current_picture
.motion_val
[1][xy
+ wrap
+ 1][1] = 0;
716 C
= s
->current_picture
.motion_val
[0][xy
- 1];
717 A
= s
->current_picture
.motion_val
[0][xy
- wrap
];
719 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -1 : 2;
721 //in 4-MV mode different blocks have different B predictor position
724 off
= (s
->mb_x
> 0) ? -1 : 1;
727 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -1 : 1;
736 B
= s
->current_picture
.motion_val
[0][xy
- wrap
+ off
];
738 if(!s
->first_slice_line
|| (n
==2 || n
==3)) { // predictor A is not out of bounds
739 if(s
->mb_width
== 1) {
743 px
= mid_pred(A
[0], B
[0], C
[0]);
744 py
= mid_pred(A
[1], B
[1], C
[1]);
746 } else if(s
->mb_x
|| (n
==1 || n
==3)) { // predictor C is not out of bounds
752 /* Pullback MV as specified in 8.3.5.3.4 */
755 qx
= (s
->mb_x
<< 6) + ((n
==1 || n
==3) ? 32 : 0);
756 qy
= (s
->mb_y
<< 6) + ((n
==2 || n
==3) ? 32 : 0);
757 X
= (s
->mb_width
<< 6) - 4;
758 Y
= (s
->mb_height
<< 6) - 4;
760 if(qx
+ px
< -60) px
= -60 - qx
;
761 if(qy
+ py
< -60) py
= -60 - qy
;
763 if(qx
+ px
< -28) px
= -28 - qx
;
764 if(qy
+ py
< -28) py
= -28 - qy
;
766 if(qx
+ px
> X
) px
= X
- qx
;
767 if(qy
+ py
> Y
) py
= Y
- qy
;
769 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
770 if((!s
->first_slice_line
|| (n
==2 || n
==3)) && (s
->mb_x
|| (n
==1 || n
==3))) {
771 if(is_intra
[xy
- wrap
])
772 sum
= FFABS(px
) + FFABS(py
);
774 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
776 if(get_bits1(&s
->gb
)) {
785 sum
= FFABS(px
) + FFABS(py
);
787 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
789 if(get_bits1(&s
->gb
)) {
799 /* store MV using signed modulus of MV range defined in 4.11 */
800 s
->mv
[0][n
][0] = s
->current_picture
.motion_val
[0][xy
][0] = ((px
+ dmv_x
+ r_x
) & ((r_x
<< 1) - 1)) - r_x
;
801 s
->mv
[0][n
][1] = s
->current_picture
.motion_val
[0][xy
][1] = ((py
+ dmv_y
+ r_y
) & ((r_y
<< 1) - 1)) - r_y
;
802 if(mv1
) { /* duplicate motion data for 1-MV block */
803 s
->current_picture
.motion_val
[0][xy
+ 1][0] = s
->current_picture
.motion_val
[0][xy
][0];
804 s
->current_picture
.motion_val
[0][xy
+ 1][1] = s
->current_picture
.motion_val
[0][xy
][1];
805 s
->current_picture
.motion_val
[0][xy
+ wrap
][0] = s
->current_picture
.motion_val
[0][xy
][0];
806 s
->current_picture
.motion_val
[0][xy
+ wrap
][1] = s
->current_picture
.motion_val
[0][xy
][1];
807 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][0] = s
->current_picture
.motion_val
[0][xy
][0];
808 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][1] = s
->current_picture
.motion_val
[0][xy
][1];
812 /** Motion compensation for direct or interpolated blocks in B-frames
814 static void vc1_interp_mc(VC1Context
*v
)
816 MpegEncContext
*s
= &v
->s
;
817 DSPContext
*dsp
= &v
->s
.dsp
;
818 uint8_t *srcY
, *srcU
, *srcV
;
819 int dxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
821 if(!v
->s
.next_picture
.data
[0])return;
825 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
826 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
828 uvmx
= uvmx
+ ((uvmx
<0)?-(uvmx
&1):(uvmx
&1));
829 uvmy
= uvmy
+ ((uvmy
<0)?-(uvmy
&1):(uvmy
&1));
831 srcY
= s
->next_picture
.data
[0];
832 srcU
= s
->next_picture
.data
[1];
833 srcV
= s
->next_picture
.data
[2];
835 src_x
= s
->mb_x
* 16 + (mx
>> 2);
836 src_y
= s
->mb_y
* 16 + (my
>> 2);
837 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
838 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
840 if(v
->profile
!= PROFILE_ADVANCED
){
841 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
842 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
843 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
844 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
846 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
847 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
848 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
849 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
852 srcY
+= src_y
* s
->linesize
+ src_x
;
853 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
854 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
856 /* for grayscale we should not try to read from unknown area */
857 if(s
->flags
& CODEC_FLAG_GRAY
) {
858 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
859 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
863 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 16 - s
->mspel
*3
864 || (unsigned)(src_y
- s
->mspel
) > s
->v_edge_pos
- (my
&3) - 16 - s
->mspel
*3){
865 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
867 srcY
-= s
->mspel
* (1 + s
->linesize
);
868 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 17+s
->mspel
*2, 17+s
->mspel
*2,
869 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
870 srcY
= s
->edge_emu_buffer
;
871 ff_emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8+1, 8+1,
872 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
873 ff_emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
874 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
877 /* if we deal with range reduction we need to scale source blocks */
883 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
884 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
887 src
= srcU
; src2
= srcV
;
888 for(j
= 0; j
< 9; j
++) {
889 for(i
= 0; i
< 9; i
++) {
890 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
891 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
893 src
+= s
->uvlinesize
;
894 src2
+= s
->uvlinesize
;
897 srcY
+= s
->mspel
* (1 + s
->linesize
);
901 dxy
= ((my
& 3) << 2) | (mx
& 3);
902 dsp
->avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] , srcY
, s
->linesize
, v
->rnd
);
903 dsp
->avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8, srcY
+ 8, s
->linesize
, v
->rnd
);
904 srcY
+= s
->linesize
* 8;
905 dsp
->avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
, srcY
, s
->linesize
, v
->rnd
);
906 dsp
->avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
908 dxy
= (my
& 2) | ((mx
& 2) >> 1);
911 dsp
->avg_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
913 dsp
->avg_no_rnd_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
916 if(s
->flags
& CODEC_FLAG_GRAY
) return;
917 /* Chroma MC always uses qpel blilinear */
921 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
922 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
924 dsp
->avg_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
925 dsp
->avg_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
929 static av_always_inline
int scale_mv(int value
, int bfrac
, int inv
, int qs
)
933 #if B_FRACTION_DEN==256
937 return 2 * ((value
* n
+ 255) >> 9);
938 return (value
* n
+ 128) >> 8;
943 return 2 * ((value
* n
+ B_FRACTION_DEN
- 1) / (2 * B_FRACTION_DEN
));
944 return (value
* n
+ B_FRACTION_DEN
/2) / B_FRACTION_DEN
;
948 /** Reconstruct motion vector for B-frame and do motion compensation
950 static inline void vc1_b_mc(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2], int direct
, int mode
)
953 v
->mv_mode2
= v
->mv_mode
;
954 v
->mv_mode
= MV_PMODE_INTENSITY_COMP
;
959 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
962 if(mode
== BMV_TYPE_INTERPOLATED
) {
965 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
969 if(v
->use_ic
&& (mode
== BMV_TYPE_BACKWARD
)) v
->mv_mode
= v
->mv_mode2
;
970 vc1_mc_1mv(v
, (mode
== BMV_TYPE_BACKWARD
));
971 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
974 static inline void vc1_pred_b_mv(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2], int direct
, int mvtype
)
976 MpegEncContext
*s
= &v
->s
;
977 int xy
, wrap
, off
= 0;
982 const uint8_t *is_intra
= v
->mb_type
[0];
986 /* scale MV difference to be quad-pel */
987 dmv_x
[0] <<= 1 - s
->quarter_sample
;
988 dmv_y
[0] <<= 1 - s
->quarter_sample
;
989 dmv_x
[1] <<= 1 - s
->quarter_sample
;
990 dmv_y
[1] <<= 1 - s
->quarter_sample
;
993 xy
= s
->block_index
[0];
996 s
->current_picture
.motion_val
[0][xy
][0] =
997 s
->current_picture
.motion_val
[0][xy
][1] =
998 s
->current_picture
.motion_val
[1][xy
][0] =
999 s
->current_picture
.motion_val
[1][xy
][1] = 0;
1002 s
->mv
[0][0][0] = scale_mv(s
->next_picture
.motion_val
[1][xy
][0], v
->bfraction
, 0, s
->quarter_sample
);
1003 s
->mv
[0][0][1] = scale_mv(s
->next_picture
.motion_val
[1][xy
][1], v
->bfraction
, 0, s
->quarter_sample
);
1004 s
->mv
[1][0][0] = scale_mv(s
->next_picture
.motion_val
[1][xy
][0], v
->bfraction
, 1, s
->quarter_sample
);
1005 s
->mv
[1][0][1] = scale_mv(s
->next_picture
.motion_val
[1][xy
][1], v
->bfraction
, 1, s
->quarter_sample
);
1007 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1008 s
->mv
[0][0][0] = av_clip(s
->mv
[0][0][0], -60 - (s
->mb_x
<< 6), (s
->mb_width
<< 6) - 4 - (s
->mb_x
<< 6));
1009 s
->mv
[0][0][1] = av_clip(s
->mv
[0][0][1], -60 - (s
->mb_y
<< 6), (s
->mb_height
<< 6) - 4 - (s
->mb_y
<< 6));
1010 s
->mv
[1][0][0] = av_clip(s
->mv
[1][0][0], -60 - (s
->mb_x
<< 6), (s
->mb_width
<< 6) - 4 - (s
->mb_x
<< 6));
1011 s
->mv
[1][0][1] = av_clip(s
->mv
[1][0][1], -60 - (s
->mb_y
<< 6), (s
->mb_height
<< 6) - 4 - (s
->mb_y
<< 6));
1013 s
->current_picture
.motion_val
[0][xy
][0] = s
->mv
[0][0][0];
1014 s
->current_picture
.motion_val
[0][xy
][1] = s
->mv
[0][0][1];
1015 s
->current_picture
.motion_val
[1][xy
][0] = s
->mv
[1][0][0];
1016 s
->current_picture
.motion_val
[1][xy
][1] = s
->mv
[1][0][1];
1020 if((mvtype
== BMV_TYPE_FORWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
1021 C
= s
->current_picture
.motion_val
[0][xy
- 2];
1022 A
= s
->current_picture
.motion_val
[0][xy
- wrap
*2];
1023 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -2 : 2;
1024 B
= s
->current_picture
.motion_val
[0][xy
- wrap
*2 + off
];
1026 if(!s
->mb_x
) C
[0] = C
[1] = 0;
1027 if(!s
->first_slice_line
) { // predictor A is not out of bounds
1028 if(s
->mb_width
== 1) {
1032 px
= mid_pred(A
[0], B
[0], C
[0]);
1033 py
= mid_pred(A
[1], B
[1], C
[1]);
1035 } else if(s
->mb_x
) { // predictor C is not out of bounds
1041 /* Pullback MV as specified in 8.3.5.3.4 */
1044 if(v
->profile
< PROFILE_ADVANCED
) {
1045 qx
= (s
->mb_x
<< 5);
1046 qy
= (s
->mb_y
<< 5);
1047 X
= (s
->mb_width
<< 5) - 4;
1048 Y
= (s
->mb_height
<< 5) - 4;
1049 if(qx
+ px
< -28) px
= -28 - qx
;
1050 if(qy
+ py
< -28) py
= -28 - qy
;
1051 if(qx
+ px
> X
) px
= X
- qx
;
1052 if(qy
+ py
> Y
) py
= Y
- qy
;
1054 qx
= (s
->mb_x
<< 6);
1055 qy
= (s
->mb_y
<< 6);
1056 X
= (s
->mb_width
<< 6) - 4;
1057 Y
= (s
->mb_height
<< 6) - 4;
1058 if(qx
+ px
< -60) px
= -60 - qx
;
1059 if(qy
+ py
< -60) py
= -60 - qy
;
1060 if(qx
+ px
> X
) px
= X
- qx
;
1061 if(qy
+ py
> Y
) py
= Y
- qy
;
1064 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1065 if(0 && !s
->first_slice_line
&& s
->mb_x
) {
1066 if(is_intra
[xy
- wrap
])
1067 sum
= FFABS(px
) + FFABS(py
);
1069 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
1071 if(get_bits1(&s
->gb
)) {
1079 if(is_intra
[xy
- 2])
1080 sum
= FFABS(px
) + FFABS(py
);
1082 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
1084 if(get_bits1(&s
->gb
)) {
1094 /* store MV using signed modulus of MV range defined in 4.11 */
1095 s
->mv
[0][0][0] = ((px
+ dmv_x
[0] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
1096 s
->mv
[0][0][1] = ((py
+ dmv_y
[0] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
1098 if((mvtype
== BMV_TYPE_BACKWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
1099 C
= s
->current_picture
.motion_val
[1][xy
- 2];
1100 A
= s
->current_picture
.motion_val
[1][xy
- wrap
*2];
1101 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -2 : 2;
1102 B
= s
->current_picture
.motion_val
[1][xy
- wrap
*2 + off
];
1104 if(!s
->mb_x
) C
[0] = C
[1] = 0;
1105 if(!s
->first_slice_line
) { // predictor A is not out of bounds
1106 if(s
->mb_width
== 1) {
1110 px
= mid_pred(A
[0], B
[0], C
[0]);
1111 py
= mid_pred(A
[1], B
[1], C
[1]);
1113 } else if(s
->mb_x
) { // predictor C is not out of bounds
1119 /* Pullback MV as specified in 8.3.5.3.4 */
1122 if(v
->profile
< PROFILE_ADVANCED
) {
1123 qx
= (s
->mb_x
<< 5);
1124 qy
= (s
->mb_y
<< 5);
1125 X
= (s
->mb_width
<< 5) - 4;
1126 Y
= (s
->mb_height
<< 5) - 4;
1127 if(qx
+ px
< -28) px
= -28 - qx
;
1128 if(qy
+ py
< -28) py
= -28 - qy
;
1129 if(qx
+ px
> X
) px
= X
- qx
;
1130 if(qy
+ py
> Y
) py
= Y
- qy
;
1132 qx
= (s
->mb_x
<< 6);
1133 qy
= (s
->mb_y
<< 6);
1134 X
= (s
->mb_width
<< 6) - 4;
1135 Y
= (s
->mb_height
<< 6) - 4;
1136 if(qx
+ px
< -60) px
= -60 - qx
;
1137 if(qy
+ py
< -60) py
= -60 - qy
;
1138 if(qx
+ px
> X
) px
= X
- qx
;
1139 if(qy
+ py
> Y
) py
= Y
- qy
;
1142 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1143 if(0 && !s
->first_slice_line
&& s
->mb_x
) {
1144 if(is_intra
[xy
- wrap
])
1145 sum
= FFABS(px
) + FFABS(py
);
1147 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
1149 if(get_bits1(&s
->gb
)) {
1157 if(is_intra
[xy
- 2])
1158 sum
= FFABS(px
) + FFABS(py
);
1160 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
1162 if(get_bits1(&s
->gb
)) {
1172 /* store MV using signed modulus of MV range defined in 4.11 */
1174 s
->mv
[1][0][0] = ((px
+ dmv_x
[1] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
1175 s
->mv
[1][0][1] = ((py
+ dmv_y
[1] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
1177 s
->current_picture
.motion_val
[0][xy
][0] = s
->mv
[0][0][0];
1178 s
->current_picture
.motion_val
[0][xy
][1] = s
->mv
[0][0][1];
1179 s
->current_picture
.motion_val
[1][xy
][0] = s
->mv
[1][0][0];
1180 s
->current_picture
.motion_val
[1][xy
][1] = s
->mv
[1][0][1];
1183 /** Get predicted DC value for I-frames only
1184 * prediction dir: left=0, top=1
1185 * @param s MpegEncContext
1186 * @param overlap flag indicating that overlap filtering is used
1187 * @param pq integer part of picture quantizer
1188 * @param[in] n block index in the current MB
1189 * @param dc_val_ptr Pointer to DC predictor
1190 * @param dir_ptr Prediction direction for use in AC prediction
1192 static inline int vc1_i_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
1193 int16_t **dc_val_ptr
, int *dir_ptr
)
1195 int a
, b
, c
, wrap
, pred
, scale
;
1197 static const uint16_t dcpred
[32] = {
1198 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1199 114, 102, 93, 85, 79, 73, 68, 64,
1200 60, 57, 54, 51, 49, 47, 45, 43,
1201 41, 39, 38, 37, 35, 34, 33
1204 /* find prediction - wmv3_dc_scale always used here in fact */
1205 if (n
< 4) scale
= s
->y_dc_scale
;
1206 else scale
= s
->c_dc_scale
;
1208 wrap
= s
->block_wrap
[n
];
1209 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
1215 b
= dc_val
[ - 1 - wrap
];
1216 a
= dc_val
[ - wrap
];
1218 if (pq
< 9 || !overlap
)
1220 /* Set outer values */
1221 if (s
->first_slice_line
&& (n
!=2 && n
!=3)) b
=a
=dcpred
[scale
];
1222 if (s
->mb_x
== 0 && (n
!=1 && n
!=3)) b
=c
=dcpred
[scale
];
1226 /* Set outer values */
1227 if (s
->first_slice_line
&& (n
!=2 && n
!=3)) b
=a
=0;
1228 if (s
->mb_x
== 0 && (n
!=1 && n
!=3)) b
=c
=0;
1231 if (abs(a
- b
) <= abs(b
- c
)) {
1239 /* update predictor */
1240 *dc_val_ptr
= &dc_val
[0];
1245 /** Get predicted DC value
1246 * prediction dir: left=0, top=1
1247 * @param s MpegEncContext
1248 * @param overlap flag indicating that overlap filtering is used
1249 * @param pq integer part of picture quantizer
1250 * @param[in] n block index in the current MB
1251 * @param a_avail flag indicating top block availability
1252 * @param c_avail flag indicating left block availability
1253 * @param dc_val_ptr Pointer to DC predictor
1254 * @param dir_ptr Prediction direction for use in AC prediction
1256 static inline int vc1_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
1257 int a_avail
, int c_avail
,
1258 int16_t **dc_val_ptr
, int *dir_ptr
)
1260 int a
, b
, c
, wrap
, pred
;
1262 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1265 wrap
= s
->block_wrap
[n
];
1266 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
1272 b
= dc_val
[ - 1 - wrap
];
1273 a
= dc_val
[ - wrap
];
1274 /* scale predictors if needed */
1275 q1
= s
->current_picture
.qscale_table
[mb_pos
];
1276 if(c_avail
&& (n
!= 1 && n
!=3)) {
1277 q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
1279 c
= (c
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
1281 if(a_avail
&& (n
!= 2 && n
!=3)) {
1282 q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
1284 a
= (a
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
1286 if(a_avail
&& c_avail
&& (n
!=3)) {
1289 if(n
!= 2) off
-= s
->mb_stride
;
1290 q2
= s
->current_picture
.qscale_table
[off
];
1292 b
= (b
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
1295 if(a_avail
&& c_avail
) {
1296 if(abs(a
- b
) <= abs(b
- c
)) {
1303 } else if(a_avail
) {
1306 } else if(c_avail
) {
1314 /* update predictor */
1315 *dc_val_ptr
= &dc_val
[0];
1319 /** @} */ // Block group
1322 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1323 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1327 static inline int vc1_coded_block_pred(MpegEncContext
* s
, int n
, uint8_t **coded_block_ptr
)
1329 int xy
, wrap
, pred
, a
, b
, c
;
1331 xy
= s
->block_index
[n
];
1332 wrap
= s
->b8_stride
;
1337 a
= s
->coded_block
[xy
- 1 ];
1338 b
= s
->coded_block
[xy
- 1 - wrap
];
1339 c
= s
->coded_block
[xy
- wrap
];
1348 *coded_block_ptr
= &s
->coded_block
[xy
];
1354 * Decode one AC coefficient
1355 * @param v The VC1 context
1356 * @param last Last coefficient
1357 * @param skip How much zero coefficients to skip
1358 * @param value Decoded AC coefficient value
1359 * @param codingset set of VLC to decode data
1362 static void vc1_decode_ac_coeff(VC1Context
*v
, int *last
, int *skip
, int *value
, int codingset
)
1364 GetBitContext
*gb
= &v
->s
.gb
;
1365 int index
, escape
, run
= 0, level
= 0, lst
= 0;
1367 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
1368 if (index
!= vc1_ac_sizes
[codingset
] - 1) {
1369 run
= vc1_index_decode_table
[codingset
][index
][0];
1370 level
= vc1_index_decode_table
[codingset
][index
][1];
1371 lst
= index
>= vc1_last_decode_table
[codingset
];
1375 escape
= decode210(gb
);
1377 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
1378 run
= vc1_index_decode_table
[codingset
][index
][0];
1379 level
= vc1_index_decode_table
[codingset
][index
][1];
1380 lst
= index
>= vc1_last_decode_table
[codingset
];
1383 level
+= vc1_last_delta_level_table
[codingset
][run
];
1385 level
+= vc1_delta_level_table
[codingset
][run
];
1388 run
+= vc1_last_delta_run_table
[codingset
][level
] + 1;
1390 run
+= vc1_delta_run_table
[codingset
][level
] + 1;
1396 lst
= get_bits1(gb
);
1397 if(v
->s
.esc3_level_length
== 0) {
1398 if(v
->pq
< 8 || v
->dquantfrm
) { // table 59
1399 v
->s
.esc3_level_length
= get_bits(gb
, 3);
1400 if(!v
->s
.esc3_level_length
)
1401 v
->s
.esc3_level_length
= get_bits(gb
, 2) + 8;
1403 v
->s
.esc3_level_length
= get_unary(gb
, 1, 6) + 2;
1405 v
->s
.esc3_run_length
= 3 + get_bits(gb
, 2);
1407 run
= get_bits(gb
, v
->s
.esc3_run_length
);
1408 sign
= get_bits1(gb
);
1409 level
= get_bits(gb
, v
->s
.esc3_level_length
);
1420 /** Decode intra block in intra frames - should be faster than decode_intra_block
1421 * @param v VC1Context
1422 * @param block block to decode
1423 * @param[in] n subblock index
1424 * @param coded are AC coeffs present or not
1425 * @param codingset set of VLC to decode data
1427 static int vc1_decode_i_block(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int codingset
)
1429 GetBitContext
*gb
= &v
->s
.gb
;
1430 MpegEncContext
*s
= &v
->s
;
1431 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
1434 int16_t *ac_val
, *ac_val2
;
1437 /* Get DC differential */
1439 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1441 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1444 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
1449 if (dcdiff
== 119 /* ESC index value */)
1451 /* TODO: Optimize */
1452 if (v
->pq
== 1) dcdiff
= get_bits(gb
, 10);
1453 else if (v
->pq
== 2) dcdiff
= get_bits(gb
, 9);
1454 else dcdiff
= get_bits(gb
, 8);
1459 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
1460 else if (v
->pq
== 2)
1461 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
1468 dcdiff
+= vc1_i_pred_dc(&v
->s
, v
->overlap
, v
->pq
, n
, &dc_val
, &dc_pred_dir
);
1471 /* Store the quantized DC coeff, used for prediction */
1473 block
[0] = dcdiff
* s
->y_dc_scale
;
1475 block
[0] = dcdiff
* s
->c_dc_scale
;
1486 int last
= 0, skip
, value
;
1487 const int8_t *zz_table
;
1491 scale
= v
->pq
* 2 + v
->halfpq
;
1495 zz_table
= wmv1_scantable
[2];
1497 zz_table
= wmv1_scantable
[3];
1499 zz_table
= wmv1_scantable
[1];
1501 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
1503 if(dc_pred_dir
) //left
1506 ac_val
-= 16 * s
->block_wrap
[n
];
1509 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
1513 block
[zz_table
[i
++]] = value
;
1516 /* apply AC prediction if needed */
1518 if(dc_pred_dir
) { //left
1519 for(k
= 1; k
< 8; k
++)
1520 block
[k
<< 3] += ac_val
[k
];
1522 for(k
= 1; k
< 8; k
++)
1523 block
[k
] += ac_val
[k
+ 8];
1526 /* save AC coeffs for further prediction */
1527 for(k
= 1; k
< 8; k
++) {
1528 ac_val2
[k
] = block
[k
<< 3];
1529 ac_val2
[k
+ 8] = block
[k
];
1532 /* scale AC coeffs */
1533 for(k
= 1; k
< 64; k
++)
1537 block
[k
] += (block
[k
] < 0) ? -v
->pq
: v
->pq
;
1540 if(s
->ac_pred
) i
= 63;
1546 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
1550 scale
= v
->pq
* 2 + v
->halfpq
;
1551 memset(ac_val2
, 0, 16 * 2);
1552 if(dc_pred_dir
) {//left
1555 memcpy(ac_val2
, ac_val
, 8 * 2);
1557 ac_val
-= 16 * s
->block_wrap
[n
];
1559 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
1562 /* apply AC prediction if needed */
1564 if(dc_pred_dir
) { //left
1565 for(k
= 1; k
< 8; k
++) {
1566 block
[k
<< 3] = ac_val
[k
] * scale
;
1567 if(!v
->pquantizer
&& block
[k
<< 3])
1568 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -v
->pq
: v
->pq
;
1571 for(k
= 1; k
< 8; k
++) {
1572 block
[k
] = ac_val
[k
+ 8] * scale
;
1573 if(!v
->pquantizer
&& block
[k
])
1574 block
[k
] += (block
[k
] < 0) ? -v
->pq
: v
->pq
;
1580 s
->block_last_index
[n
] = i
;
1585 /** Decode intra block in intra frames - should be faster than decode_intra_block
1586 * @param v VC1Context
1587 * @param block block to decode
1588 * @param[in] n subblock number
1589 * @param coded are AC coeffs present or not
1590 * @param codingset set of VLC to decode data
1591 * @param mquant quantizer value for this macroblock
1593 static int vc1_decode_i_block_adv(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int codingset
, int mquant
)
1595 GetBitContext
*gb
= &v
->s
.gb
;
1596 MpegEncContext
*s
= &v
->s
;
1597 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
1600 int16_t *ac_val
, *ac_val2
;
1602 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
1603 int use_pred
= s
->ac_pred
;
1606 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1608 /* Get DC differential */
1610 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1612 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1615 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
1620 if (dcdiff
== 119 /* ESC index value */)
1622 /* TODO: Optimize */
1623 if (mquant
== 1) dcdiff
= get_bits(gb
, 10);
1624 else if (mquant
== 2) dcdiff
= get_bits(gb
, 9);
1625 else dcdiff
= get_bits(gb
, 8);
1630 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
1631 else if (mquant
== 2)
1632 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
1639 dcdiff
+= vc1_pred_dc(&v
->s
, v
->overlap
, mquant
, n
, v
->a_avail
, v
->c_avail
, &dc_val
, &dc_pred_dir
);
1642 /* Store the quantized DC coeff, used for prediction */
1644 block
[0] = dcdiff
* s
->y_dc_scale
;
1646 block
[0] = dcdiff
* s
->c_dc_scale
;
1652 /* check if AC is needed at all */
1653 if(!a_avail
&& !c_avail
) use_pred
= 0;
1654 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
1657 scale
= mquant
* 2 + ((mquant
== v
->pq
) ? v
->halfpq
: 0);
1659 if(dc_pred_dir
) //left
1662 ac_val
-= 16 * s
->block_wrap
[n
];
1664 q1
= s
->current_picture
.qscale_table
[mb_pos
];
1665 if(dc_pred_dir
&& c_avail
&& mb_pos
) q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
1666 if(!dc_pred_dir
&& a_avail
&& mb_pos
>= s
->mb_stride
) q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
1667 if(dc_pred_dir
&& n
==1) q2
= q1
;
1668 if(!dc_pred_dir
&& n
==2) q2
= q1
;
1672 int last
= 0, skip
, value
;
1673 const int8_t *zz_table
;
1678 zz_table
= wmv1_scantable
[2];
1680 zz_table
= wmv1_scantable
[3];
1682 zz_table
= wmv1_scantable
[1];
1685 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
1689 block
[zz_table
[i
++]] = value
;
1692 /* apply AC prediction if needed */
1694 /* scale predictors if needed*/
1696 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1697 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1699 if(dc_pred_dir
) { //left
1700 for(k
= 1; k
< 8; k
++)
1701 block
[k
<< 3] += (ac_val
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1703 for(k
= 1; k
< 8; k
++)
1704 block
[k
] += (ac_val
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1707 if(dc_pred_dir
) { //left
1708 for(k
= 1; k
< 8; k
++)
1709 block
[k
<< 3] += ac_val
[k
];
1711 for(k
= 1; k
< 8; k
++)
1712 block
[k
] += ac_val
[k
+ 8];
1716 /* save AC coeffs for further prediction */
1717 for(k
= 1; k
< 8; k
++) {
1718 ac_val2
[k
] = block
[k
<< 3];
1719 ac_val2
[k
+ 8] = block
[k
];
1722 /* scale AC coeffs */
1723 for(k
= 1; k
< 64; k
++)
1727 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
1730 if(use_pred
) i
= 63;
1731 } else { // no AC coeffs
1734 memset(ac_val2
, 0, 16 * 2);
1735 if(dc_pred_dir
) {//left
1737 memcpy(ac_val2
, ac_val
, 8 * 2);
1739 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1740 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1741 for(k
= 1; k
< 8; k
++)
1742 ac_val2
[k
] = (ac_val2
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1747 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
1749 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1750 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1751 for(k
= 1; k
< 8; k
++)
1752 ac_val2
[k
+ 8] = (ac_val2
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1757 /* apply AC prediction if needed */
1759 if(dc_pred_dir
) { //left
1760 for(k
= 1; k
< 8; k
++) {
1761 block
[k
<< 3] = ac_val2
[k
] * scale
;
1762 if(!v
->pquantizer
&& block
[k
<< 3])
1763 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -mquant
: mquant
;
1766 for(k
= 1; k
< 8; k
++) {
1767 block
[k
] = ac_val2
[k
+ 8] * scale
;
1768 if(!v
->pquantizer
&& block
[k
])
1769 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
1775 s
->block_last_index
[n
] = i
;
1780 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1781 * @param v VC1Context
1782 * @param block block to decode
1783 * @param[in] n subblock index
1784 * @param coded are AC coeffs present or not
1785 * @param mquant block quantizer
1786 * @param codingset set of VLC to decode data
1788 static int vc1_decode_intra_block(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int mquant
, int codingset
)
1790 GetBitContext
*gb
= &v
->s
.gb
;
1791 MpegEncContext
*s
= &v
->s
;
1792 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
1795 int16_t *ac_val
, *ac_val2
;
1797 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
1798 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
1799 int use_pred
= s
->ac_pred
;
1803 s
->dsp
.clear_block(block
);
1805 /* XXX: Guard against dumb values of mquant */
1806 mquant
= (mquant
< 1) ? 0 : ( (mquant
>31) ? 31 : mquant
);
1808 /* Set DC scale - y and c use the same */
1809 s
->y_dc_scale
= s
->y_dc_scale_table
[mquant
];
1810 s
->c_dc_scale
= s
->c_dc_scale_table
[mquant
];
1812 /* Get DC differential */
1814 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1816 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
1819 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
1824 if (dcdiff
== 119 /* ESC index value */)
1826 /* TODO: Optimize */
1827 if (mquant
== 1) dcdiff
= get_bits(gb
, 10);
1828 else if (mquant
== 2) dcdiff
= get_bits(gb
, 9);
1829 else dcdiff
= get_bits(gb
, 8);
1834 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
1835 else if (mquant
== 2)
1836 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
1843 dcdiff
+= vc1_pred_dc(&v
->s
, v
->overlap
, mquant
, n
, a_avail
, c_avail
, &dc_val
, &dc_pred_dir
);
1846 /* Store the quantized DC coeff, used for prediction */
1849 block
[0] = dcdiff
* s
->y_dc_scale
;
1851 block
[0] = dcdiff
* s
->c_dc_scale
;
1857 /* check if AC is needed at all and adjust direction if needed */
1858 if(!a_avail
) dc_pred_dir
= 1;
1859 if(!c_avail
) dc_pred_dir
= 0;
1860 if(!a_avail
&& !c_avail
) use_pred
= 0;
1861 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
1864 scale
= mquant
* 2 + v
->halfpq
;
1866 if(dc_pred_dir
) //left
1869 ac_val
-= 16 * s
->block_wrap
[n
];
1871 q1
= s
->current_picture
.qscale_table
[mb_pos
];
1872 if(dc_pred_dir
&& c_avail
&& mb_pos
) q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
1873 if(!dc_pred_dir
&& a_avail
&& mb_pos
>= s
->mb_stride
) q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
1874 if(dc_pred_dir
&& n
==1) q2
= q1
;
1875 if(!dc_pred_dir
&& n
==2) q2
= q1
;
1879 int last
= 0, skip
, value
;
1880 const int8_t *zz_table
;
1883 zz_table
= wmv1_scantable
[0];
1886 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
1890 block
[zz_table
[i
++]] = value
;
1893 /* apply AC prediction if needed */
1895 /* scale predictors if needed*/
1897 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1898 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1900 if(dc_pred_dir
) { //left
1901 for(k
= 1; k
< 8; k
++)
1902 block
[k
<< 3] += (ac_val
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1904 for(k
= 1; k
< 8; k
++)
1905 block
[k
] += (ac_val
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1908 if(dc_pred_dir
) { //left
1909 for(k
= 1; k
< 8; k
++)
1910 block
[k
<< 3] += ac_val
[k
];
1912 for(k
= 1; k
< 8; k
++)
1913 block
[k
] += ac_val
[k
+ 8];
1917 /* save AC coeffs for further prediction */
1918 for(k
= 1; k
< 8; k
++) {
1919 ac_val2
[k
] = block
[k
<< 3];
1920 ac_val2
[k
+ 8] = block
[k
];
1923 /* scale AC coeffs */
1924 for(k
= 1; k
< 64; k
++)
1928 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
1931 if(use_pred
) i
= 63;
1932 } else { // no AC coeffs
1935 memset(ac_val2
, 0, 16 * 2);
1936 if(dc_pred_dir
) {//left
1938 memcpy(ac_val2
, ac_val
, 8 * 2);
1940 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1941 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1942 for(k
= 1; k
< 8; k
++)
1943 ac_val2
[k
] = (ac_val2
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1948 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
1950 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
1951 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
1952 for(k
= 1; k
< 8; k
++)
1953 ac_val2
[k
+ 8] = (ac_val2
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
1958 /* apply AC prediction if needed */
1960 if(dc_pred_dir
) { //left
1961 for(k
= 1; k
< 8; k
++) {
1962 block
[k
<< 3] = ac_val2
[k
] * scale
;
1963 if(!v
->pquantizer
&& block
[k
<< 3])
1964 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -mquant
: mquant
;
1967 for(k
= 1; k
< 8; k
++) {
1968 block
[k
] = ac_val2
[k
+ 8] * scale
;
1969 if(!v
->pquantizer
&& block
[k
])
1970 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
1976 s
->block_last_index
[n
] = i
;
1983 static int vc1_decode_p_block(VC1Context
*v
, DCTELEM block
[64], int n
, int mquant
, int ttmb
, int first_block
,
1984 uint8_t *dst
, int linesize
, int skip_block
, int apply_filter
, int cbp_top
, int cbp_left
)
1986 MpegEncContext
*s
= &v
->s
;
1987 GetBitContext
*gb
= &s
->gb
;
1990 int scale
, off
, idx
, last
, skip
, value
;
1991 int ttblk
= ttmb
& 7;
1994 s
->dsp
.clear_block(block
);
1997 ttblk
= ff_vc1_ttblk_to_tt
[v
->tt_index
][get_vlc2(gb
, ff_vc1_ttblk_vlc
[v
->tt_index
].table
, VC1_TTBLK_VLC_BITS
, 1)];
1999 if(ttblk
== TT_4X4
) {
2000 subblkpat
= ~(get_vlc2(gb
, ff_vc1_subblkpat_vlc
[v
->tt_index
].table
, VC1_SUBBLKPAT_VLC_BITS
, 1) + 1);
2002 if((ttblk
!= TT_8X8
&& ttblk
!= TT_4X4
) && (v
->ttmbf
|| (ttmb
!= -1 && (ttmb
& 8) && !first_block
))) {
2003 subblkpat
= decode012(gb
);
2004 if(subblkpat
) subblkpat
^= 3; //swap decoded pattern bits
2005 if(ttblk
== TT_8X4_TOP
|| ttblk
== TT_8X4_BOTTOM
) ttblk
= TT_8X4
;
2006 if(ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
) ttblk
= TT_4X8
;
2008 scale
= 2 * mquant
+ ((v
->pq
== mquant
) ? v
->halfpq
: 0);
2010 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2011 if(ttblk
== TT_8X4_TOP
|| ttblk
== TT_8X4_BOTTOM
) {
2012 subblkpat
= 2 - (ttblk
== TT_8X4_TOP
);
2015 if(ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
) {
2016 subblkpat
= 2 - (ttblk
== TT_4X8_LEFT
);
2025 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
2029 idx
= wmv1_scantable
[0][i
++];
2030 block
[idx
] = value
* scale
;
2032 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
2036 s
->dsp
.vc1_inv_trans_8x8_dc(dst
, linesize
, block
);
2038 s
->dsp
.vc1_inv_trans_8x8(block
);
2039 s
->dsp
.add_pixels_clamped(block
, dst
, linesize
);
2041 if(apply_filter
&& cbp_top
& 0xC)
2042 s
->dsp
.vc1_v_loop_filter8(dst
, linesize
, v
->pq
);
2043 if(apply_filter
&& cbp_left
& 0xA)
2044 s
->dsp
.vc1_h_loop_filter8(dst
, linesize
, v
->pq
);
2048 pat
= ~subblkpat
& 0xF;
2049 for(j
= 0; j
< 4; j
++) {
2050 last
= subblkpat
& (1 << (3 - j
));
2052 off
= (j
& 1) * 4 + (j
& 2) * 16;
2054 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
2058 idx
= ff_vc1_simple_progressive_4x4_zz
[i
++];
2059 block
[idx
+ off
] = value
* scale
;
2061 block
[idx
+ off
] += (block
[idx
+ off
] < 0) ? -mquant
: mquant
;
2063 if(!(subblkpat
& (1 << (3 - j
))) && !skip_block
){
2065 s
->dsp
.vc1_inv_trans_4x4_dc(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, block
+ off
);
2067 s
->dsp
.vc1_inv_trans_4x4(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, block
+ off
);
2068 if(apply_filter
&& (j
&2 ? pat
& (1<<(j
-2)) : (cbp_top
& (1 << (j
+ 2)))))
2069 s
->dsp
.vc1_v_loop_filter4(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, v
->pq
);
2070 if(apply_filter
&& (j
&1 ? pat
& (1<<(j
-1)) : (cbp_left
& (1 << (j
+ 1)))))
2071 s
->dsp
.vc1_h_loop_filter4(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, v
->pq
);
2076 pat
= ~((subblkpat
& 2)*6 + (subblkpat
& 1)*3) & 0xF;
2077 for(j
= 0; j
< 2; j
++) {
2078 last
= subblkpat
& (1 << (1 - j
));
2082 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
2086 idx
= v
->zz_8x4
[i
++]+off
;
2087 block
[idx
] = value
* scale
;
2089 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
2091 if(!(subblkpat
& (1 << (1 - j
))) && !skip_block
){
2093 s
->dsp
.vc1_inv_trans_8x4_dc(dst
+ j
*4*linesize
, linesize
, block
+ off
);
2095 s
->dsp
.vc1_inv_trans_8x4(dst
+ j
*4*linesize
, linesize
, block
+ off
);
2096 if(apply_filter
&& j
? pat
& 0x3 : (cbp_top
& 0xC))
2097 s
->dsp
.vc1_v_loop_filter8(dst
+ j
*4*linesize
, linesize
, v
->pq
);
2098 if(apply_filter
&& cbp_left
& (2 << j
))
2099 s
->dsp
.vc1_h_loop_filter4(dst
+ j
*4*linesize
, linesize
, v
->pq
);
2104 pat
= ~(subblkpat
*5) & 0xF;
2105 for(j
= 0; j
< 2; j
++) {
2106 last
= subblkpat
& (1 << (1 - j
));
2110 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
2114 idx
= v
->zz_4x8
[i
++]+off
;
2115 block
[idx
] = value
* scale
;
2117 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
2119 if(!(subblkpat
& (1 << (1 - j
))) && !skip_block
){
2121 s
->dsp
.vc1_inv_trans_4x8_dc(dst
+ j
*4, linesize
, block
+ off
);
2123 s
->dsp
.vc1_inv_trans_4x8(dst
+ j
*4, linesize
, block
+ off
);
2124 if(apply_filter
&& cbp_top
& (2 << j
))
2125 s
->dsp
.vc1_v_loop_filter4(dst
+ j
*4, linesize
, v
->pq
);
2126 if(apply_filter
&& j
? pat
& 0x5 : (cbp_left
& 0xA))
2127 s
->dsp
.vc1_h_loop_filter8(dst
+ j
*4, linesize
, v
->pq
);
2135 /** @} */ // Macroblock group
2137 static const int size_table
[6] = { 0, 2, 3, 4, 5, 8 };
2138 static const int offset_table
[6] = { 0, 1, 3, 7, 15, 31 };
2140 /** Decode one P-frame MB (in Simple/Main profile)
2142 static int vc1_decode_p_mb(VC1Context
*v
)
2144 MpegEncContext
*s
= &v
->s
;
2145 GetBitContext
*gb
= &s
->gb
;
2147 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2148 int cbp
; /* cbp decoding stuff */
2149 int mqdiff
, mquant
; /* MB quantization */
2150 int ttmb
= v
->ttfrm
; /* MB Transform type */
2152 int mb_has_coeffs
= 1; /* last_flag */
2153 int dmv_x
, dmv_y
; /* Differential MV components */
2154 int index
, index1
; /* LUT indexes */
2155 int val
, sign
; /* temp values */
2156 int first_block
= 1;
2158 int skipped
, fourmv
;
2159 int block_cbp
= 0, pat
;
2160 int apply_loop_filter
;
2162 mquant
= v
->pq
; /* Loosy initialization */
2164 if (v
->mv_type_is_raw
)
2165 fourmv
= get_bits1(gb
);
2167 fourmv
= v
->mv_type_mb_plane
[mb_pos
];
2169 skipped
= get_bits1(gb
);
2171 skipped
= v
->s
.mbskip_table
[mb_pos
];
2173 apply_loop_filter
= s
->loop_filter
&& !(s
->avctx
->skip_loop_filter
>= AVDISCARD_NONKEY
);
2174 if (!fourmv
) /* 1MV mode */
2178 GET_MVDATA(dmv_x
, dmv_y
);
2181 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
2182 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
2184 s
->current_picture
.mb_type
[mb_pos
] = s
->mb_intra
? MB_TYPE_INTRA
: MB_TYPE_16x16
;
2185 vc1_pred_mv(s
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
2187 /* FIXME Set DC val for inter block ? */
2188 if (s
->mb_intra
&& !mb_has_coeffs
)
2191 s
->ac_pred
= get_bits1(gb
);
2194 else if (mb_has_coeffs
)
2196 if (s
->mb_intra
) s
->ac_pred
= get_bits1(gb
);
2197 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2205 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2207 if (!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
2208 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
,
2209 VC1_TTMB_VLC_BITS
, 2);
2210 if(!s
->mb_intra
) vc1_mc_1mv(v
, 0);
2214 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2216 val
= ((cbp
>> (5 - i
)) & 1);
2217 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
2218 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
2220 /* check if prediction blocks A and C are available */
2221 v
->a_avail
= v
->c_avail
= 0;
2222 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
2223 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
2224 if(i
== 1 || i
== 3 || s
->mb_x
)
2225 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
2227 vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
2228 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
2229 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2230 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
2231 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2232 if(v
->pq
>= 9 && v
->overlap
) {
2234 s
->dsp
.vc1_h_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2236 s
->dsp
.vc1_v_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2238 if(apply_loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
2239 int left_cbp
, top_cbp
;
2241 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
2242 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
2244 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
2245 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
2248 s
->dsp
.vc1_v_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2250 s
->dsp
.vc1_h_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2252 block_cbp
|= 0xF << (i
<< 2);
2254 int left_cbp
= 0, top_cbp
= 0, filter
= 0;
2255 if(apply_loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
2258 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
2259 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
2261 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
2262 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
2265 s
->dsp
.vc1_v_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2267 s
->dsp
.vc1_h_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2269 pat
= vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
, first_block
, s
->dest
[dst_idx
] + off
, (i
&4)?s
->uvlinesize
:s
->linesize
, (i
&4) && (s
->flags
& CODEC_FLAG_GRAY
), filter
, left_cbp
, top_cbp
);
2270 block_cbp
|= pat
<< (i
<< 2);
2271 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
2279 for(i
= 0; i
< 6; i
++) {
2280 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2281 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2283 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_SKIP
;
2284 s
->current_picture
.qscale_table
[mb_pos
] = 0;
2285 vc1_pred_mv(s
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
2292 if (!skipped
/* unskipped MB */)
2294 int intra_count
= 0, coded_inter
= 0;
2295 int is_intra
[6], is_coded
[6];
2297 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2300 val
= ((cbp
>> (5 - i
)) & 1);
2301 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2308 GET_MVDATA(dmv_x
, dmv_y
);
2310 vc1_pred_mv(s
, i
, dmv_x
, dmv_y
, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
2311 if(!s
->mb_intra
) vc1_mc_4mv_luma(v
, i
);
2312 intra_count
+= s
->mb_intra
;
2313 is_intra
[i
] = s
->mb_intra
;
2314 is_coded
[i
] = mb_has_coeffs
;
2317 is_intra
[i
] = (intra_count
>= 3);
2320 if(i
== 4) vc1_mc_4mv_chroma(v
);
2321 v
->mb_type
[0][s
->block_index
[i
]] = is_intra
[i
];
2322 if(!coded_inter
) coded_inter
= !is_intra
[i
] & is_coded
[i
];
2324 // if there are no coded blocks then don't do anything more
2325 if(!intra_count
&& !coded_inter
) return 0;
2328 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2329 /* test if block is intra and has pred */
2334 if(((!s
->first_slice_line
|| (i
==2 || i
==3)) && v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]])
2335 || ((s
->mb_x
|| (i
==1 || i
==3)) && v
->mb_type
[0][s
->block_index
[i
] - 1])) {
2340 if(intrapred
)s
->ac_pred
= get_bits1(gb
);
2341 else s
->ac_pred
= 0;
2343 if (!v
->ttmbf
&& coded_inter
)
2344 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
2348 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
2349 s
->mb_intra
= is_intra
[i
];
2351 /* check if prediction blocks A and C are available */
2352 v
->a_avail
= v
->c_avail
= 0;
2353 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
2354 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
2355 if(i
== 1 || i
== 3 || s
->mb_x
)
2356 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
2358 vc1_decode_intra_block(v
, s
->block
[i
], i
, is_coded
[i
], mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
2359 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
2360 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2361 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
2362 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, (i
&4)?s
->uvlinesize
:s
->linesize
);
2363 if(v
->pq
>= 9 && v
->overlap
) {
2365 s
->dsp
.vc1_h_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2367 s
->dsp
.vc1_v_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2369 if(v
->s
.loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
2370 int left_cbp
, top_cbp
;
2372 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
2373 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
2375 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
2376 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
2379 s
->dsp
.vc1_v_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2381 s
->dsp
.vc1_h_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2383 block_cbp
|= 0xF << (i
<< 2);
2384 } else if(is_coded
[i
]) {
2385 int left_cbp
= 0, top_cbp
= 0, filter
= 0;
2386 if(v
->s
.loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
2389 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
2390 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
2392 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
2393 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
2396 s
->dsp
.vc1_v_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2398 s
->dsp
.vc1_h_loop_filter8(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, v
->pq
);
2400 pat
= vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
, first_block
, s
->dest
[dst_idx
] + off
, (i
&4)?s
->uvlinesize
:s
->linesize
, (i
&4) && (s
->flags
& CODEC_FLAG_GRAY
), filter
, left_cbp
, top_cbp
);
2401 block_cbp
|= pat
<< (i
<< 2);
2402 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
2411 s
->current_picture
.qscale_table
[mb_pos
] = 0;
2412 for (i
=0; i
<6; i
++) {
2413 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2414 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2418 vc1_pred_mv(s
, i
, 0, 0, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
2419 vc1_mc_4mv_luma(v
, i
);
2421 vc1_mc_4mv_chroma(v
);
2422 s
->current_picture
.qscale_table
[mb_pos
] = 0;
2426 v
->cbp
[s
->mb_x
] = block_cbp
;
2428 /* Should never happen */
2432 /** Decode one B-frame MB (in Main profile)
2434 static void vc1_decode_b_mb(VC1Context
*v
)
2436 MpegEncContext
*s
= &v
->s
;
2437 GetBitContext
*gb
= &s
->gb
;
2439 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2440 int cbp
= 0; /* cbp decoding stuff */
2441 int mqdiff
, mquant
; /* MB quantization */
2442 int ttmb
= v
->ttfrm
; /* MB Transform type */
2443 int mb_has_coeffs
= 0; /* last_flag */
2444 int index
, index1
; /* LUT indexes */
2445 int val
, sign
; /* temp values */
2446 int first_block
= 1;
2448 int skipped
, direct
;
2449 int dmv_x
[2], dmv_y
[2];
2450 int bmvtype
= BMV_TYPE_BACKWARD
;
2452 mquant
= v
->pq
; /* Loosy initialization */
2456 direct
= get_bits1(gb
);
2458 direct
= v
->direct_mb_plane
[mb_pos
];
2460 skipped
= get_bits1(gb
);
2462 skipped
= v
->s
.mbskip_table
[mb_pos
];
2464 dmv_x
[0] = dmv_x
[1] = dmv_y
[0] = dmv_y
[1] = 0;
2465 for(i
= 0; i
< 6; i
++) {
2466 v
->mb_type
[0][s
->block_index
[i
]] = 0;
2467 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2469 s
->current_picture
.qscale_table
[mb_pos
] = 0;
2473 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
2474 dmv_x
[1] = dmv_x
[0];
2475 dmv_y
[1] = dmv_y
[0];
2477 if(skipped
|| !s
->mb_intra
) {
2478 bmvtype
= decode012(gb
);
2481 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_BACKWARD
: BMV_TYPE_FORWARD
;
2484 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_FORWARD
: BMV_TYPE_BACKWARD
;
2487 bmvtype
= BMV_TYPE_INTERPOLATED
;
2488 dmv_x
[0] = dmv_y
[0] = 0;
2492 for(i
= 0; i
< 6; i
++)
2493 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
2496 if(direct
) bmvtype
= BMV_TYPE_INTERPOLATED
;
2497 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2498 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2502 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2505 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2507 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
2508 dmv_x
[0] = dmv_y
[0] = dmv_x
[1] = dmv_y
[1] = 0;
2509 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2510 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2512 if(!mb_has_coeffs
&& !s
->mb_intra
) {
2513 /* no coded blocks - effectively skipped */
2514 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2515 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2518 if(s
->mb_intra
&& !mb_has_coeffs
) {
2520 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2521 s
->ac_pred
= get_bits1(gb
);
2523 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2525 if(bmvtype
== BMV_TYPE_INTERPOLATED
) {
2526 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
2527 if(!mb_has_coeffs
) {
2528 /* interpolated skipped block */
2529 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2530 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2534 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2536 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
2539 s
->ac_pred
= get_bits1(gb
);
2540 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
2542 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2543 if(!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
2544 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
2550 s
->dc_val
[0][s
->block_index
[i
]] = 0;
2552 val
= ((cbp
>> (5 - i
)) & 1);
2553 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
2554 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
2556 /* check if prediction blocks A and C are available */
2557 v
->a_avail
= v
->c_avail
= 0;
2558 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
2559 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
2560 if(i
== 1 || i
== 3 || s
->mb_x
)
2561 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
2563 vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
2564 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
2565 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
2566 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
2567 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
2569 vc1_decode_p_block(v
, s
->block
[i
], i
, mquant
, ttmb
, first_block
, s
->dest
[dst_idx
] + off
, (i
&4)?s
->uvlinesize
:s
->linesize
, (i
&4) && (s
->flags
& CODEC_FLAG_GRAY
), 0, 0, 0);
2570 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
2576 /** Decode blocks of I-frame
2578 static void vc1_decode_i_blocks(VC1Context
*v
)
2581 MpegEncContext
*s
= &v
->s
;
2586 /* select codingmode used for VLC tables selection */
2587 switch(v
->y_ac_table_index
){
2589 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2592 v
->codingset
= CS_HIGH_MOT_INTRA
;
2595 v
->codingset
= CS_MID_RATE_INTRA
;
2599 switch(v
->c_ac_table_index
){
2601 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2604 v
->codingset2
= CS_HIGH_MOT_INTER
;
2607 v
->codingset2
= CS_MID_RATE_INTER
;
2611 /* Set DC scale - y and c use the same */
2612 s
->y_dc_scale
= s
->y_dc_scale_table
[v
->pq
];
2613 s
->c_dc_scale
= s
->c_dc_scale_table
[v
->pq
];
2616 s
->mb_x
= s
->mb_y
= 0;
2618 s
->first_slice_line
= 1;
2619 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
2621 ff_init_block_index(s
);
2622 for(; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2623 ff_update_block_index(s
);
2624 s
->dsp
.clear_blocks(s
->block
[0]);
2625 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_width
;
2626 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
2627 s
->current_picture
.qscale_table
[mb_pos
] = v
->pq
;
2628 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
2629 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
2631 // do actual MB decoding and displaying
2632 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
2633 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
2635 for(k
= 0; k
< 6; k
++) {
2636 val
= ((cbp
>> (5 - k
)) & 1);
2639 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
2643 cbp
|= val
<< (5 - k
);
2645 vc1_decode_i_block(v
, s
->block
[k
], k
, val
, (k
<4)? v
->codingset
: v
->codingset2
);
2647 s
->dsp
.vc1_inv_trans_8x8(s
->block
[k
]);
2648 if(v
->pq
>= 9 && v
->overlap
) {
2649 for(j
= 0; j
< 64; j
++) s
->block
[k
][j
] += 128;
2653 vc1_put_block(v
, s
->block
);
2654 if(v
->pq
>= 9 && v
->overlap
) {
2656 s
->dsp
.vc1_h_overlap(s
->dest
[0], s
->linesize
);
2657 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
2658 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
2659 s
->dsp
.vc1_h_overlap(s
->dest
[1], s
->uvlinesize
);
2660 s
->dsp
.vc1_h_overlap(s
->dest
[2], s
->uvlinesize
);
2663 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8, s
->linesize
);
2664 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
2665 if(!s
->first_slice_line
) {
2666 s
->dsp
.vc1_v_overlap(s
->dest
[0], s
->linesize
);
2667 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8, s
->linesize
);
2668 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
2669 s
->dsp
.vc1_v_overlap(s
->dest
[1], s
->uvlinesize
);
2670 s
->dsp
.vc1_v_overlap(s
->dest
[2], s
->uvlinesize
);
2673 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
2674 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
2676 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, v
->pq
);
2678 if(get_bits_count(&s
->gb
) > v
->bits
) {
2679 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
2680 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n", get_bits_count(&s
->gb
), v
->bits
);
2684 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2685 s
->first_slice_line
= 0;
2687 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
2690 /** Decode blocks of I-frame for advanced profile
2692 static void vc1_decode_i_blocks_adv(VC1Context
*v
)
2695 MpegEncContext
*s
= &v
->s
;
2702 GetBitContext
*gb
= &s
->gb
;
2704 /* select codingmode used for VLC tables selection */
2705 switch(v
->y_ac_table_index
){
2707 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2710 v
->codingset
= CS_HIGH_MOT_INTRA
;
2713 v
->codingset
= CS_MID_RATE_INTRA
;
2717 switch(v
->c_ac_table_index
){
2719 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2722 v
->codingset2
= CS_HIGH_MOT_INTER
;
2725 v
->codingset2
= CS_MID_RATE_INTER
;
2730 s
->mb_x
= s
->mb_y
= 0;
2732 s
->first_slice_line
= 1;
2733 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
2735 ff_init_block_index(s
);
2736 for(;s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2737 ff_update_block_index(s
);
2738 s
->dsp
.clear_blocks(s
->block
[0]);
2739 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2740 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
2741 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
2742 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
2744 // do actual MB decoding and displaying
2745 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
2746 if(v
->acpred_is_raw
)
2747 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
2749 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
];
2751 if(v
->condover
== CONDOVER_SELECT
) {
2752 if(v
->overflg_is_raw
)
2753 overlap
= get_bits1(&v
->s
.gb
);
2755 overlap
= v
->over_flags_plane
[mb_pos
];
2757 overlap
= (v
->condover
== CONDOVER_ALL
);
2761 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
2762 /* Set DC scale - y and c use the same */
2763 s
->y_dc_scale
= s
->y_dc_scale_table
[mquant
];
2764 s
->c_dc_scale
= s
->c_dc_scale_table
[mquant
];
2766 for(k
= 0; k
< 6; k
++) {
2767 val
= ((cbp
>> (5 - k
)) & 1);
2770 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
2774 cbp
|= val
<< (5 - k
);
2776 v
->a_avail
= !s
->first_slice_line
|| (k
==2 || k
==3);
2777 v
->c_avail
= !!s
->mb_x
|| (k
==1 || k
==3);
2779 vc1_decode_i_block_adv(v
, s
->block
[k
], k
, val
, (k
<4)? v
->codingset
: v
->codingset2
, mquant
);
2781 s
->dsp
.vc1_inv_trans_8x8(s
->block
[k
]);
2782 for(j
= 0; j
< 64; j
++) s
->block
[k
][j
] += 128;
2785 vc1_put_block(v
, s
->block
);
2788 s
->dsp
.vc1_h_overlap(s
->dest
[0], s
->linesize
);
2789 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
2790 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
2791 s
->dsp
.vc1_h_overlap(s
->dest
[1], s
->uvlinesize
);
2792 s
->dsp
.vc1_h_overlap(s
->dest
[2], s
->uvlinesize
);
2795 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8, s
->linesize
);
2796 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
2797 if(!s
->first_slice_line
) {
2798 s
->dsp
.vc1_v_overlap(s
->dest
[0], s
->linesize
);
2799 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8, s
->linesize
);
2800 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
2801 s
->dsp
.vc1_v_overlap(s
->dest
[1], s
->uvlinesize
);
2802 s
->dsp
.vc1_v_overlap(s
->dest
[2], s
->uvlinesize
);
2805 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
2806 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
2808 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, v
->pq
);
2810 if(get_bits_count(&s
->gb
) > v
->bits
) {
2811 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
2812 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n", get_bits_count(&s
->gb
), v
->bits
);
2816 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2817 s
->first_slice_line
= 0;
2819 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
2822 static void vc1_decode_p_blocks(VC1Context
*v
)
2824 MpegEncContext
*s
= &v
->s
;
2826 /* select codingmode used for VLC tables selection */
2827 switch(v
->c_ac_table_index
){
2829 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2832 v
->codingset
= CS_HIGH_MOT_INTRA
;
2835 v
->codingset
= CS_MID_RATE_INTRA
;
2839 switch(v
->c_ac_table_index
){
2841 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2844 v
->codingset2
= CS_HIGH_MOT_INTER
;
2847 v
->codingset2
= CS_MID_RATE_INTER
;
2851 s
->first_slice_line
= 1;
2852 memset(v
->cbp_base
, 0, sizeof(v
->cbp_base
[0])*2*s
->mb_stride
);
2853 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
2855 ff_init_block_index(s
);
2856 for(; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2857 ff_update_block_index(s
);
2860 if(get_bits_count(&s
->gb
) > v
->bits
|| get_bits_count(&s
->gb
) < 0) {
2861 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
2862 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s
->gb
), v
->bits
,s
->mb_x
,s
->mb_y
);
2866 memmove(v
->cbp_base
, v
->cbp
, sizeof(v
->cbp_base
[0])*s
->mb_stride
);
2867 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2868 s
->first_slice_line
= 0;
2870 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
2873 static void vc1_decode_b_blocks(VC1Context
*v
)
2875 MpegEncContext
*s
= &v
->s
;
2877 /* select codingmode used for VLC tables selection */
2878 switch(v
->c_ac_table_index
){
2880 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
2883 v
->codingset
= CS_HIGH_MOT_INTRA
;
2886 v
->codingset
= CS_MID_RATE_INTRA
;
2890 switch(v
->c_ac_table_index
){
2892 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
2895 v
->codingset2
= CS_HIGH_MOT_INTER
;
2898 v
->codingset2
= CS_MID_RATE_INTER
;
2902 s
->first_slice_line
= 1;
2903 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
2905 ff_init_block_index(s
);
2906 for(; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
2907 ff_update_block_index(s
);
2910 if(get_bits_count(&s
->gb
) > v
->bits
|| get_bits_count(&s
->gb
) < 0) {
2911 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
2912 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s
->gb
), v
->bits
,s
->mb_x
,s
->mb_y
);
2915 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, v
->pq
);
2917 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2918 s
->first_slice_line
= 0;
2920 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
2923 static void vc1_decode_skip_blocks(VC1Context
*v
)
2925 MpegEncContext
*s
= &v
->s
;
2927 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
2928 s
->first_slice_line
= 1;
2929 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
2931 ff_init_block_index(s
);
2932 ff_update_block_index(s
);
2933 memcpy(s
->dest
[0], s
->last_picture
.data
[0] + s
->mb_y
* 16 * s
->linesize
, s
->linesize
* 16);
2934 memcpy(s
->dest
[1], s
->last_picture
.data
[1] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
2935 memcpy(s
->dest
[2], s
->last_picture
.data
[2] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
2936 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
2937 s
->first_slice_line
= 0;
2939 s
->pict_type
= FF_P_TYPE
;
2942 static void vc1_decode_blocks(VC1Context
*v
)
2945 v
->s
.esc3_level_length
= 0;
2947 ff_intrax8_decode_picture(&v
->x8
, 2*v
->pq
+v
->halfpq
, v
->pq
*(!v
->pquantizer
) );
2950 switch(v
->s
.pict_type
) {
2952 if(v
->profile
== PROFILE_ADVANCED
)
2953 vc1_decode_i_blocks_adv(v
);
2955 vc1_decode_i_blocks(v
);
2958 if(v
->p_frame_skipped
)
2959 vc1_decode_skip_blocks(v
);
2961 vc1_decode_p_blocks(v
);
2965 if(v
->profile
== PROFILE_ADVANCED
)
2966 vc1_decode_i_blocks_adv(v
);
2968 vc1_decode_i_blocks(v
);
2970 vc1_decode_b_blocks(v
);
2976 /** Initialize a VC1/WMV3 decoder
2977 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2978 * @todo TODO: Decypher remaining bits in extra_data
2980 static av_cold
int vc1_decode_init(AVCodecContext
*avctx
)
2982 VC1Context
*v
= avctx
->priv_data
;
2983 MpegEncContext
*s
= &v
->s
;
2986 if (!avctx
->extradata_size
|| !avctx
->extradata
) return -1;
2987 if (!(avctx
->flags
& CODEC_FLAG_GRAY
))
2988 avctx
->pix_fmt
= avctx
->get_format(avctx
, avctx
->codec
->pix_fmts
);
2990 avctx
->pix_fmt
= PIX_FMT_GRAY8
;
2991 avctx
->hwaccel
= ff_find_hwaccel(avctx
->codec
->id
, avctx
->pix_fmt
);
2993 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
2994 v
->s
.flags
|= CODEC_FLAG_EMU_EDGE
;
2996 if(avctx
->idct_algo
==FF_IDCT_AUTO
){
2997 avctx
->idct_algo
=FF_IDCT_WMV2
;
3000 if(ff_h263_decode_init(avctx
) < 0)
3002 if (vc1_init_common(v
) < 0) return -1;
3004 avctx
->coded_width
= avctx
->width
;
3005 avctx
->coded_height
= avctx
->height
;
3006 if (avctx
->codec_id
== CODEC_ID_WMV3
)
3010 // looks like WMV3 has a sequence header stored in the extradata
3011 // advanced sequence header may be before the first frame
3012 // the last byte of the extradata is a version number, 1 for the
3013 // samples we can decode
3015 init_get_bits(&gb
, avctx
->extradata
, avctx
->extradata_size
*8);
3017 if (vc1_decode_sequence_header(avctx
, v
, &gb
) < 0)
3020 count
= avctx
->extradata_size
*8 - get_bits_count(&gb
);
3023 av_log(avctx
, AV_LOG_INFO
, "Extra data: %i bits left, value: %X\n",
3024 count
, get_bits(&gb
, count
));
3028 av_log(avctx
, AV_LOG_INFO
, "Read %i bits in overflow\n", -count
);
3030 } else { // VC1/WVC1
3031 const uint8_t *start
= avctx
->extradata
;
3032 uint8_t *end
= avctx
->extradata
+ avctx
->extradata_size
;
3033 const uint8_t *next
;
3034 int size
, buf2_size
;
3035 uint8_t *buf2
= NULL
;
3036 int seq_initialized
= 0, ep_initialized
= 0;
3038 if(avctx
->extradata_size
< 16) {
3039 av_log(avctx
, AV_LOG_ERROR
, "Extradata size too small: %i\n", avctx
->extradata_size
);
3043 buf2
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
3044 if(start
[0]) start
++; // in WVC1 extradata first byte is its size
3046 for(; next
< end
; start
= next
){
3047 next
= find_next_marker(start
+ 4, end
);
3048 size
= next
- start
- 4;
3049 if(size
<= 0) continue;
3050 buf2_size
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
3051 init_get_bits(&gb
, buf2
, buf2_size
* 8);
3052 switch(AV_RB32(start
)){
3053 case VC1_CODE_SEQHDR
:
3054 if(vc1_decode_sequence_header(avctx
, v
, &gb
) < 0){
3058 seq_initialized
= 1;
3060 case VC1_CODE_ENTRYPOINT
:
3061 if(vc1_decode_entry_point(avctx
, v
, &gb
) < 0){
3070 if(!seq_initialized
|| !ep_initialized
){
3071 av_log(avctx
, AV_LOG_ERROR
, "Incomplete extradata\n");
3075 avctx
->has_b_frames
= !!(avctx
->max_b_frames
);
3076 s
->low_delay
= !avctx
->has_b_frames
;
3078 s
->mb_width
= (avctx
->coded_width
+15)>>4;
3079 s
->mb_height
= (avctx
->coded_height
+15)>>4;
3081 /* Allocate mb bitplanes */
3082 v
->mv_type_mb_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
3083 v
->direct_mb_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
3084 v
->acpred_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
3085 v
->over_flags_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
3087 v
->cbp_base
= av_malloc(sizeof(v
->cbp_base
[0]) * 2 * s
->mb_stride
);
3088 v
->cbp
= v
->cbp_base
+ s
->mb_stride
;
3090 /* allocate block type info in that way so it could be used with s->block_index[] */
3091 v
->mb_type_base
= av_malloc(s
->b8_stride
* (s
->mb_height
* 2 + 1) + s
->mb_stride
* (s
->mb_height
+ 1) * 2);
3092 v
->mb_type
[0] = v
->mb_type_base
+ s
->b8_stride
+ 1;
3093 v
->mb_type
[1] = v
->mb_type_base
+ s
->b8_stride
* (s
->mb_height
* 2 + 1) + s
->mb_stride
+ 1;
3094 v
->mb_type
[2] = v
->mb_type
[1] + s
->mb_stride
* (s
->mb_height
+ 1);
3096 /* Init coded blocks info */
3097 if (v
->profile
== PROFILE_ADVANCED
)
3099 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3101 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3105 ff_intrax8_common_init(&v
->x8
,s
);
3110 /** Decode a VC1/WMV3 frame
3111 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3113 static int vc1_decode_frame(AVCodecContext
*avctx
,
3114 void *data
, int *data_size
,
3117 const uint8_t *buf
= avpkt
->data
;
3118 int buf_size
= avpkt
->size
;
3119 VC1Context
*v
= avctx
->priv_data
;
3120 MpegEncContext
*s
= &v
->s
;
3121 AVFrame
*pict
= data
;
3122 uint8_t *buf2
= NULL
;
3123 const uint8_t *buf_start
= buf
;
3125 /* no supplementary picture */
3126 if (buf_size
== 0) {
3127 /* special case for last picture */
3128 if (s
->low_delay
==0 && s
->next_picture_ptr
) {
3129 *pict
= *(AVFrame
*)s
->next_picture_ptr
;
3130 s
->next_picture_ptr
= NULL
;
3132 *data_size
= sizeof(AVFrame
);
3138 /* We need to set current_picture_ptr before reading the header,
3139 * otherwise we cannot store anything in there. */
3140 if(s
->current_picture_ptr
==NULL
|| s
->current_picture_ptr
->data
[0]){
3141 int i
= ff_find_unused_picture(s
, 0);
3142 s
->current_picture_ptr
= &s
->picture
[i
];
3145 if (s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
){
3146 if (v
->profile
< PROFILE_ADVANCED
)
3147 avctx
->pix_fmt
= PIX_FMT_VDPAU_WMV3
;
3149 avctx
->pix_fmt
= PIX_FMT_VDPAU_VC1
;
3152 //for advanced profile we may need to parse and unescape data
3153 if (avctx
->codec_id
== CODEC_ID_VC1
) {
3155 buf2
= av_mallocz(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
3157 if(IS_MARKER(AV_RB32(buf
))){ /* frame starts with marker and needs to be parsed */
3158 const uint8_t *start
, *end
, *next
;
3162 for(start
= buf
, end
= buf
+ buf_size
; next
< end
; start
= next
){
3163 next
= find_next_marker(start
+ 4, end
);
3164 size
= next
- start
- 4;
3165 if(size
<= 0) continue;
3166 switch(AV_RB32(start
)){
3167 case VC1_CODE_FRAME
:
3168 if (avctx
->hwaccel
||
3169 s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
)
3171 buf_size2
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
3173 case VC1_CODE_ENTRYPOINT
: /* it should be before frame data */
3174 buf_size2
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
3175 init_get_bits(&s
->gb
, buf2
, buf_size2
*8);
3176 vc1_decode_entry_point(avctx
, v
, &s
->gb
);
3178 case VC1_CODE_SLICE
:
3179 av_log(avctx
, AV_LOG_ERROR
, "Sliced decoding is not implemented (yet)\n");
3184 }else if(v
->interlace
&& ((buf
[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
3185 const uint8_t *divider
;
3187 divider
= find_next_marker(buf
, buf
+ buf_size
);
3188 if((divider
== (buf
+ buf_size
)) || AV_RB32(divider
) != VC1_CODE_FIELD
){
3189 av_log(avctx
, AV_LOG_ERROR
, "Error in WVC1 interlaced frame\n");
3194 buf_size2
= vc1_unescape_buffer(buf
, divider
- buf
, buf2
);
3196 av_free(buf2
);return -1;
3198 buf_size2
= vc1_unescape_buffer(buf
, buf_size
, buf2
);
3200 init_get_bits(&s
->gb
, buf2
, buf_size2
*8);
3202 init_get_bits(&s
->gb
, buf
, buf_size
*8);
3203 // do parse frame header
3204 if(v
->profile
< PROFILE_ADVANCED
) {
3205 if(vc1_parse_frame_header(v
, &s
->gb
) == -1) {
3210 if(vc1_parse_frame_header_adv(v
, &s
->gb
) == -1) {
3216 if(s
->pict_type
!= FF_I_TYPE
&& !v
->res_rtm_flag
){
3222 s
->current_picture
.pict_type
= s
->pict_type
;
3223 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
3225 /* skip B-frames if we don't have reference frames */
3226 if(s
->last_picture_ptr
==NULL
&& (s
->pict_type
==FF_B_TYPE
|| s
->dropable
)){
3228 return -1;//buf_size;
3230 /* skip b frames if we are in a hurry */
3231 if(avctx
->hurry_up
&& s
->pict_type
==FF_B_TYPE
) return -1;//buf_size;
3232 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s
->pict_type
==FF_B_TYPE
)
3233 || (avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s
->pict_type
!=FF_I_TYPE
)
3234 || avctx
->skip_frame
>= AVDISCARD_ALL
) {
3238 /* skip everything if we are in a hurry>=5 */
3239 if(avctx
->hurry_up
>=5) {
3241 return -1;//buf_size;
3244 if(s
->next_p_frame_damaged
){
3245 if(s
->pict_type
==FF_B_TYPE
)
3248 s
->next_p_frame_damaged
=0;
3251 if(MPV_frame_start(s
, avctx
) < 0) {
3256 s
->me
.qpel_put
= s
->dsp
.put_qpel_pixels_tab
;
3257 s
->me
.qpel_avg
= s
->dsp
.avg_qpel_pixels_tab
;
3259 if ((CONFIG_VC1_VDPAU_DECODER
|| CONFIG_WMV3_VDPAU_DECODER
)
3260 &&s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
)
3261 ff_vdpau_vc1_decode_picture(s
, buf_start
, (buf
+ buf_size
) - buf_start
);
3262 else if (avctx
->hwaccel
) {
3263 if (avctx
->hwaccel
->start_frame(avctx
, buf
, buf_size
) < 0)
3265 if (avctx
->hwaccel
->decode_slice(avctx
, buf_start
, (buf
+ buf_size
) - buf_start
) < 0)
3267 if (avctx
->hwaccel
->end_frame(avctx
) < 0)
3270 ff_er_frame_start(s
);
3272 v
->bits
= buf_size
* 8;
3273 vc1_decode_blocks(v
);
3274 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3275 // if(get_bits_count(&s->gb) > buf_size * 8)
3282 assert(s
->current_picture
.pict_type
== s
->current_picture_ptr
->pict_type
);
3283 assert(s
->current_picture
.pict_type
== s
->pict_type
);
3284 if (s
->pict_type
== FF_B_TYPE
|| s
->low_delay
) {
3285 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
3286 } else if (s
->last_picture_ptr
!= NULL
) {
3287 *pict
= *(AVFrame
*)s
->last_picture_ptr
;
3290 if(s
->last_picture_ptr
|| s
->low_delay
){
3291 *data_size
= sizeof(AVFrame
);
3292 ff_print_debug_info(s
, pict
);
3300 /** Close a VC1/WMV3 decoder
3301 * @warning Initial try at using MpegEncContext stuff
3303 static av_cold
int vc1_decode_end(AVCodecContext
*avctx
)
3305 VC1Context
*v
= avctx
->priv_data
;
3307 av_freep(&v
->hrd_rate
);
3308 av_freep(&v
->hrd_buffer
);
3309 MPV_common_end(&v
->s
);
3310 av_freep(&v
->mv_type_mb_plane
);
3311 av_freep(&v
->direct_mb_plane
);
3312 av_freep(&v
->acpred_plane
);
3313 av_freep(&v
->over_flags_plane
);
3314 av_freep(&v
->mb_type_base
);
3315 av_freep(&v
->cbp_base
);
3316 ff_intrax8_common_end(&v
->x8
);
3321 AVCodec vc1_decoder
= {
3330 CODEC_CAP_DR1
| CODEC_CAP_DELAY
,
3332 .long_name
= NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3333 .pix_fmts
= ff_hwaccel_pixfmt_list_420
3336 AVCodec wmv3_decoder
= {
3345 CODEC_CAP_DR1
| CODEC_CAP_DELAY
,
3347 .long_name
= NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3348 .pix_fmts
= ff_hwaccel_pixfmt_list_420
3351 #if CONFIG_WMV3_VDPAU_DECODER
3352 AVCodec wmv3_vdpau_decoder
= {
3361 CODEC_CAP_DR1
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
3363 .long_name
= NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3364 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_VDPAU_WMV3
, PIX_FMT_NONE
}
3368 #if CONFIG_VC1_VDPAU_DECODER
3369 AVCodec vc1_vdpau_decoder
= {
3378 CODEC_CAP_DR1
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
3380 .long_name
= NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3381 .pix_fmts
= (enum PixelFormat
[]){PIX_FMT_VDPAU_VC1
, PIX_FMT_NONE
}