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/vc1.c
25 * VC-1 and WMV3 decoder
30 #include "mpegvideo.h"
33 #include "vc1acdata.h"
34 #include "msmpeg4data.h"
36 #include "simple_idct.h"
38 #include "vdpau_internal.h"
43 #define MB_INTRA_VLC_BITS 9
46 static const uint16_t table_mb_intra
[64][2];
50 * Init VC-1 specific tables and VC1Context members
51 * @param v The VC1Context to initialize
54 static int vc1_init_common(VC1Context
*v
)
59 v
->hrd_rate
= v
->hrd_buffer
= NULL
;
65 init_vlc(&ff_vc1_bfraction_vlc
, VC1_BFRACTION_VLC_BITS
, 23,
66 ff_vc1_bfraction_bits
, 1, 1,
67 ff_vc1_bfraction_codes
, 1, 1, 1);
68 init_vlc(&ff_vc1_norm2_vlc
, VC1_NORM2_VLC_BITS
, 4,
69 ff_vc1_norm2_bits
, 1, 1,
70 ff_vc1_norm2_codes
, 1, 1, 1);
71 init_vlc(&ff_vc1_norm6_vlc
, VC1_NORM6_VLC_BITS
, 64,
72 ff_vc1_norm6_bits
, 1, 1,
73 ff_vc1_norm6_codes
, 2, 2, 1);
74 init_vlc(&ff_vc1_imode_vlc
, VC1_IMODE_VLC_BITS
, 7,
75 ff_vc1_imode_bits
, 1, 1,
76 ff_vc1_imode_codes
, 1, 1, 1);
79 init_vlc(&ff_vc1_ttmb_vlc
[i
], VC1_TTMB_VLC_BITS
, 16,
80 ff_vc1_ttmb_bits
[i
], 1, 1,
81 ff_vc1_ttmb_codes
[i
], 2, 2, 1);
82 init_vlc(&ff_vc1_ttblk_vlc
[i
], VC1_TTBLK_VLC_BITS
, 8,
83 ff_vc1_ttblk_bits
[i
], 1, 1,
84 ff_vc1_ttblk_codes
[i
], 1, 1, 1);
85 init_vlc(&ff_vc1_subblkpat_vlc
[i
], VC1_SUBBLKPAT_VLC_BITS
, 15,
86 ff_vc1_subblkpat_bits
[i
], 1, 1,
87 ff_vc1_subblkpat_codes
[i
], 1, 1, 1);
91 init_vlc(&ff_vc1_4mv_block_pattern_vlc
[i
], VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 16,
92 ff_vc1_4mv_block_pattern_bits
[i
], 1, 1,
93 ff_vc1_4mv_block_pattern_codes
[i
], 1, 1, 1);
94 init_vlc(&ff_vc1_cbpcy_p_vlc
[i
], VC1_CBPCY_P_VLC_BITS
, 64,
95 ff_vc1_cbpcy_p_bits
[i
], 1, 1,
96 ff_vc1_cbpcy_p_codes
[i
], 2, 2, 1);
97 init_vlc(&ff_vc1_mv_diff_vlc
[i
], VC1_MV_DIFF_VLC_BITS
, 73,
98 ff_vc1_mv_diff_bits
[i
], 1, 1,
99 ff_vc1_mv_diff_codes
[i
], 2, 2, 1);
102 init_vlc(&ff_vc1_ac_coeff_table
[i
], AC_VLC_BITS
, vc1_ac_sizes
[i
],
103 &vc1_ac_tables
[i
][0][1], 8, 4,
104 &vc1_ac_tables
[i
][0][0], 8, 4, 1);
105 init_vlc(&ff_msmp4_mb_i_vlc
, MB_INTRA_VLC_BITS
, 64,
106 &ff_msmp4_mb_i_table
[0][1], 4, 2,
107 &ff_msmp4_mb_i_table
[0][0], 4, 2, 1);
112 v
->mvrange
= 0; /* 7.1.1.18, p80 */
117 /***********************************************************************/
119 * @defgroup vc1bitplane VC-1 Bitplane decoding
137 /** @} */ //imode defines
139 /** Decode rows by checking if they are skipped
140 * @param plane Buffer to store decoded bits
141 * @param[in] width Width of this buffer
142 * @param[in] height Height of this buffer
143 * @param[in] stride of this buffer
145 static void decode_rowskip(uint8_t* plane
, int width
, int height
, int stride
, GetBitContext
*gb
){
148 for (y
=0; y
<height
; y
++){
149 if (!get_bits1(gb
)) //rowskip
150 memset(plane
, 0, width
);
152 for (x
=0; x
<width
; x
++)
153 plane
[x
] = get_bits1(gb
);
158 /** Decode columns by checking if they are skipped
159 * @param plane Buffer to store decoded bits
160 * @param[in] width Width of this buffer
161 * @param[in] height Height of this buffer
162 * @param[in] stride of this buffer
163 * @todo FIXME: Optimize
165 static void decode_colskip(uint8_t* plane
, int width
, int height
, int stride
, GetBitContext
*gb
){
168 for (x
=0; x
<width
; x
++){
169 if (!get_bits1(gb
)) //colskip
170 for (y
=0; y
<height
; y
++)
173 for (y
=0; y
<height
; y
++)
174 plane
[y
*stride
] = get_bits1(gb
);
179 /** Decode a bitplane's bits
180 * @param data bitplane where to store the decode bits
181 * @param[out] raw_flag pointer to the flag indicating that this bitplane is not coded explicitly
182 * @param v VC-1 context for bit reading and logging
184 * @todo FIXME: Optimize
186 static int bitplane_decoding(uint8_t* data
, int *raw_flag
, VC1Context
*v
)
188 GetBitContext
*gb
= &v
->s
.gb
;
190 int imode
, x
, y
, code
, offset
;
191 uint8_t invert
, *planep
= data
;
192 int width
, height
, stride
;
194 width
= v
->s
.mb_width
;
195 height
= v
->s
.mb_height
;
196 stride
= v
->s
.mb_stride
;
197 invert
= get_bits1(gb
);
198 imode
= get_vlc2(gb
, ff_vc1_imode_vlc
.table
, VC1_IMODE_VLC_BITS
, 1);
204 //Data is actually read in the MB layer (same for all tests == "raw")
205 *raw_flag
= 1; //invert ignored
209 if ((height
* width
) & 1)
211 *planep
++ = get_bits1(gb
);
215 // decode bitplane as one long line
216 for (y
= offset
; y
< height
* width
; y
+= 2) {
217 code
= get_vlc2(gb
, ff_vc1_norm2_vlc
.table
, VC1_NORM2_VLC_BITS
, 1);
218 *planep
++ = code
& 1;
220 if(offset
== width
) {
222 planep
+= stride
- width
;
224 *planep
++ = code
>> 1;
226 if(offset
== width
) {
228 planep
+= stride
- width
;
234 if(!(height
% 3) && (width
% 3)) { // use 2x3 decoding
235 for(y
= 0; y
< height
; y
+= 3) {
236 for(x
= width
& 1; x
< width
; x
+= 2) {
237 code
= get_vlc2(gb
, ff_vc1_norm6_vlc
.table
, VC1_NORM6_VLC_BITS
, 2);
239 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "invalid NORM-6 VLC\n");
242 planep
[x
+ 0] = (code
>> 0) & 1;
243 planep
[x
+ 1] = (code
>> 1) & 1;
244 planep
[x
+ 0 + stride
] = (code
>> 2) & 1;
245 planep
[x
+ 1 + stride
] = (code
>> 3) & 1;
246 planep
[x
+ 0 + stride
* 2] = (code
>> 4) & 1;
247 planep
[x
+ 1 + stride
* 2] = (code
>> 5) & 1;
249 planep
+= stride
* 3;
251 if(width
& 1) decode_colskip(data
, 1, height
, stride
, &v
->s
.gb
);
253 planep
+= (height
& 1) * stride
;
254 for(y
= height
& 1; y
< height
; y
+= 2) {
255 for(x
= width
% 3; x
< width
; x
+= 3) {
256 code
= get_vlc2(gb
, ff_vc1_norm6_vlc
.table
, VC1_NORM6_VLC_BITS
, 2);
258 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "invalid NORM-6 VLC\n");
261 planep
[x
+ 0] = (code
>> 0) & 1;
262 planep
[x
+ 1] = (code
>> 1) & 1;
263 planep
[x
+ 2] = (code
>> 2) & 1;
264 planep
[x
+ 0 + stride
] = (code
>> 3) & 1;
265 planep
[x
+ 1 + stride
] = (code
>> 4) & 1;
266 planep
[x
+ 2 + stride
] = (code
>> 5) & 1;
268 planep
+= stride
* 2;
271 if(x
) decode_colskip(data
, x
, height
, stride
, &v
->s
.gb
);
272 if(height
& 1) decode_rowskip(data
+x
, width
- x
, 1, stride
, &v
->s
.gb
);
276 decode_rowskip(data
, width
, height
, stride
, &v
->s
.gb
);
279 decode_colskip(data
, width
, height
, stride
, &v
->s
.gb
);
284 /* Applying diff operator */
285 if (imode
== IMODE_DIFF2
|| imode
== IMODE_DIFF6
)
289 for (x
=1; x
<width
; x
++)
290 planep
[x
] ^= planep
[x
-1];
291 for (y
=1; y
<height
; y
++)
294 planep
[0] ^= planep
[-stride
];
295 for (x
=1; x
<width
; x
++)
297 if (planep
[x
-1] != planep
[x
-stride
]) planep
[x
] ^= invert
;
298 else planep
[x
] ^= planep
[x
-1];
305 for (x
=0; x
<stride
*height
; x
++) planep
[x
] = !planep
[x
]; //FIXME stride
307 return (imode
<<1) + invert
;
310 /** @} */ //Bitplane group
312 #define FILTSIGN(a) ((a) >= 0 ? 1 : -1)
314 * VC-1 in-loop deblocking filter for one line
315 * @param src source block type
316 * @param stride block stride
317 * @param pq block quantizer
318 * @return whether other 3 pairs should be filtered or not
321 static int av_always_inline
vc1_filter_line(uint8_t* src
, int stride
, int pq
){
322 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
324 int a0
= (2*(src
[-2*stride
] - src
[ 1*stride
]) - 5*(src
[-1*stride
] - src
[ 0*stride
]) + 4) >> 3;
325 int a0_sign
= a0
>> 31; /* Store sign */
326 a0
= (a0
^ a0_sign
) - a0_sign
; /* a0 = FFABS(a0); */
328 int a1
= FFABS((2*(src
[-4*stride
] - src
[-1*stride
]) - 5*(src
[-3*stride
] - src
[-2*stride
]) + 4) >> 3);
329 int a2
= FFABS((2*(src
[ 0*stride
] - src
[ 3*stride
]) - 5*(src
[ 1*stride
] - src
[ 2*stride
]) + 4) >> 3);
330 if(a1
< a0
|| a2
< a0
){
331 int clip
= src
[-1*stride
] - src
[ 0*stride
];
332 int clip_sign
= clip
>> 31;
333 clip
= ((clip
^ clip_sign
) - clip_sign
)>>1;
335 int a3
= FFMIN(a1
, a2
);
336 int d
= 5 * (a3
- a0
);
337 int d_sign
= (d
>> 31);
338 d
= ((d
^ d_sign
) - d_sign
) >> 3;
341 if( d_sign
^ clip_sign
)
345 d
= (d
^ d_sign
) - d_sign
; /* Restore sign */
346 src
[-1*stride
] = cm
[src
[-1*stride
] - d
];
347 src
[ 0*stride
] = cm
[src
[ 0*stride
] + d
];
357 * VC-1 in-loop deblocking filter
358 * @param src source block type
359 * @param step distance between horizontally adjacent elements
360 * @param stride distance between vertically adjacent elements
361 * @param len edge length to filter (4 or 8 pixels)
362 * @param pq block quantizer
365 static void vc1_loop_filter(uint8_t* src
, int step
, int stride
, int len
, int pq
)
370 for(i
= 0; i
< len
; i
+= 4){
371 filt3
= vc1_filter_line(src
+ 2*step
, stride
, pq
);
373 vc1_filter_line(src
+ 0*step
, stride
, pq
);
374 vc1_filter_line(src
+ 1*step
, stride
, pq
);
375 vc1_filter_line(src
+ 3*step
, stride
, pq
);
381 static void vc1_loop_filter_iblk(MpegEncContext
*s
, int pq
)
384 if(!s
->first_slice_line
)
385 vc1_loop_filter(s
->dest
[0], 1, s
->linesize
, 16, pq
);
386 vc1_loop_filter(s
->dest
[0] + 8*s
->linesize
, 1, s
->linesize
, 16, pq
);
387 for(i
= !s
->mb_x
*8; i
< 16; i
+= 8)
388 vc1_loop_filter(s
->dest
[0] + i
, s
->linesize
, 1, 16, pq
);
389 for(j
= 0; j
< 2; j
++){
390 if(!s
->first_slice_line
)
391 vc1_loop_filter(s
->dest
[j
+1], 1, s
->uvlinesize
, 8, pq
);
393 vc1_loop_filter(s
->dest
[j
+1], s
->uvlinesize
, 1, 8, pq
);
397 /***********************************************************************/
398 /** VOP Dquant decoding
399 * @param v VC-1 Context
401 static int vop_dquant_decoding(VC1Context
*v
)
403 GetBitContext
*gb
= &v
->s
.gb
;
409 pqdiff
= get_bits(gb
, 3);
410 if (pqdiff
== 7) v
->altpq
= get_bits(gb
, 5);
411 else v
->altpq
= v
->pq
+ pqdiff
+ 1;
415 v
->dquantfrm
= get_bits1(gb
);
418 v
->dqprofile
= get_bits(gb
, 2);
419 switch (v
->dqprofile
)
421 case DQPROFILE_SINGLE_EDGE
:
422 case DQPROFILE_DOUBLE_EDGES
:
423 v
->dqsbedge
= get_bits(gb
, 2);
425 case DQPROFILE_ALL_MBS
:
426 v
->dqbilevel
= get_bits1(gb
);
429 default: break; //Forbidden ?
431 if (v
->dqbilevel
|| v
->dqprofile
!= DQPROFILE_ALL_MBS
)
433 pqdiff
= get_bits(gb
, 3);
434 if (pqdiff
== 7) v
->altpq
= get_bits(gb
, 5);
435 else v
->altpq
= v
->pq
+ pqdiff
+ 1;
442 /** Put block onto picture
444 static void vc1_put_block(VC1Context
*v
, DCTELEM block
[6][64])
448 DSPContext
*dsp
= &v
->s
.dsp
;
452 for(k
= 0; k
< 6; k
++)
453 for(j
= 0; j
< 8; j
++)
454 for(i
= 0; i
< 8; i
++)
455 block
[k
][i
+ j
*8] = ((block
[k
][i
+ j
*8] - 128) << 1) + 128;
458 ys
= v
->s
.current_picture
.linesize
[0];
459 us
= v
->s
.current_picture
.linesize
[1];
460 vs
= v
->s
.current_picture
.linesize
[2];
463 dsp
->put_pixels_clamped(block
[0], Y
, ys
);
464 dsp
->put_pixels_clamped(block
[1], Y
+ 8, ys
);
466 dsp
->put_pixels_clamped(block
[2], Y
, ys
);
467 dsp
->put_pixels_clamped(block
[3], Y
+ 8, ys
);
469 if(!(v
->s
.flags
& CODEC_FLAG_GRAY
)) {
470 dsp
->put_pixels_clamped(block
[4], v
->s
.dest
[1], us
);
471 dsp
->put_pixels_clamped(block
[5], v
->s
.dest
[2], vs
);
475 /** Do motion compensation over 1 macroblock
476 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
478 static void vc1_mc_1mv(VC1Context
*v
, int dir
)
480 MpegEncContext
*s
= &v
->s
;
481 DSPContext
*dsp
= &v
->s
.dsp
;
482 uint8_t *srcY
, *srcU
, *srcV
;
483 int dxy
, uvdxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
485 if(!v
->s
.last_picture
.data
[0])return;
487 mx
= s
->mv
[dir
][0][0];
488 my
= s
->mv
[dir
][0][1];
490 // store motion vectors for further use in B frames
491 if(s
->pict_type
== FF_P_TYPE
) {
492 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = mx
;
493 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = my
;
495 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
496 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
498 uvmx
= uvmx
+ ((uvmx
<0)?(uvmx
&1):-(uvmx
&1));
499 uvmy
= uvmy
+ ((uvmy
<0)?(uvmy
&1):-(uvmy
&1));
502 srcY
= s
->last_picture
.data
[0];
503 srcU
= s
->last_picture
.data
[1];
504 srcV
= s
->last_picture
.data
[2];
506 srcY
= s
->next_picture
.data
[0];
507 srcU
= s
->next_picture
.data
[1];
508 srcV
= s
->next_picture
.data
[2];
511 src_x
= s
->mb_x
* 16 + (mx
>> 2);
512 src_y
= s
->mb_y
* 16 + (my
>> 2);
513 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
514 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
516 if(v
->profile
!= PROFILE_ADVANCED
){
517 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
518 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
519 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
520 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
522 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
523 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
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 srcY
+= src_y
* s
->linesize
+ src_x
;
529 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
530 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
532 /* for grayscale we should not try to read from unknown area */
533 if(s
->flags
& CODEC_FLAG_GRAY
) {
534 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
535 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
538 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
539 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 16 - s
->mspel
*3
540 || (unsigned)(src_y
- s
->mspel
) > s
->v_edge_pos
- (my
&3) - 16 - s
->mspel
*3){
541 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
543 srcY
-= s
->mspel
* (1 + s
->linesize
);
544 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 17+s
->mspel
*2, 17+s
->mspel
*2,
545 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
546 srcY
= s
->edge_emu_buffer
;
547 ff_emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8+1, 8+1,
548 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
549 ff_emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
550 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
553 /* if we deal with range reduction we need to scale source blocks */
559 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
560 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
563 src
= srcU
; src2
= srcV
;
564 for(j
= 0; j
< 9; j
++) {
565 for(i
= 0; i
< 9; i
++) {
566 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
567 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
569 src
+= s
->uvlinesize
;
570 src2
+= s
->uvlinesize
;
573 /* if we deal with intensity compensation we need to scale source blocks */
574 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
579 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
580 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = v
->luty
[src
[i
]];
583 src
= srcU
; src2
= srcV
;
584 for(j
= 0; j
< 9; j
++) {
585 for(i
= 0; i
< 9; i
++) {
586 src
[i
] = v
->lutuv
[src
[i
]];
587 src2
[i
] = v
->lutuv
[src2
[i
]];
589 src
+= s
->uvlinesize
;
590 src2
+= s
->uvlinesize
;
593 srcY
+= s
->mspel
* (1 + s
->linesize
);
597 dxy
= ((my
& 3) << 2) | (mx
& 3);
598 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] , srcY
, s
->linesize
, v
->rnd
);
599 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8, srcY
+ 8, s
->linesize
, v
->rnd
);
600 srcY
+= s
->linesize
* 8;
601 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
, srcY
, s
->linesize
, v
->rnd
);
602 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + 8 * s
->linesize
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
603 } else { // hpel mc - always used for luma
604 dxy
= (my
& 2) | ((mx
& 2) >> 1);
607 dsp
->put_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
609 dsp
->put_no_rnd_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
612 if(s
->flags
& CODEC_FLAG_GRAY
) return;
613 /* Chroma MC always uses qpel bilinear */
614 uvdxy
= ((uvmy
& 3) << 2) | (uvmx
& 3);
618 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
619 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
621 dsp
->put_no_rnd_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
622 dsp
->put_no_rnd_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
626 /** Do motion compensation for 4-MV macroblock - luminance block
628 static void vc1_mc_4mv_luma(VC1Context
*v
, int n
)
630 MpegEncContext
*s
= &v
->s
;
631 DSPContext
*dsp
= &v
->s
.dsp
;
633 int dxy
, mx
, my
, src_x
, src_y
;
636 if(!v
->s
.last_picture
.data
[0])return;
639 srcY
= s
->last_picture
.data
[0];
641 off
= s
->linesize
* 4 * (n
&2) + (n
&1) * 8;
643 src_x
= s
->mb_x
* 16 + (n
&1) * 8 + (mx
>> 2);
644 src_y
= s
->mb_y
* 16 + (n
&2) * 4 + (my
>> 2);
646 if(v
->profile
!= PROFILE_ADVANCED
){
647 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
648 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
650 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
651 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
654 srcY
+= src_y
* s
->linesize
+ src_x
;
656 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
657 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 8 - s
->mspel
*2
658 || (unsigned)(src_y
- s
->mspel
) > s
->v_edge_pos
- (my
&3) - 8 - s
->mspel
*2){
659 srcY
-= s
->mspel
* (1 + s
->linesize
);
660 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 9+s
->mspel
*2, 9+s
->mspel
*2,
661 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
662 srcY
= s
->edge_emu_buffer
;
663 /* if we deal with range reduction we need to scale source blocks */
669 for(j
= 0; j
< 9 + s
->mspel
*2; j
++) {
670 for(i
= 0; i
< 9 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
674 /* if we deal with intensity compensation we need to scale source blocks */
675 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
680 for(j
= 0; j
< 9 + s
->mspel
*2; j
++) {
681 for(i
= 0; i
< 9 + s
->mspel
*2; i
++) src
[i
] = v
->luty
[src
[i
]];
685 srcY
+= s
->mspel
* (1 + s
->linesize
);
689 dxy
= ((my
& 3) << 2) | (mx
& 3);
690 dsp
->put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, v
->rnd
);
691 } else { // hpel mc - always used for luma
692 dxy
= (my
& 2) | ((mx
& 2) >> 1);
694 dsp
->put_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
696 dsp
->put_no_rnd_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
700 static inline int median4(int a
, int b
, int c
, int d
)
703 if(c
< d
) return (FFMIN(b
, d
) + FFMAX(a
, c
)) / 2;
704 else return (FFMIN(b
, c
) + FFMAX(a
, d
)) / 2;
706 if(c
< d
) return (FFMIN(a
, d
) + FFMAX(b
, c
)) / 2;
707 else return (FFMIN(a
, c
) + FFMAX(b
, d
)) / 2;
712 /** Do motion compensation for 4-MV macroblock - both chroma blocks
714 static void vc1_mc_4mv_chroma(VC1Context
*v
)
716 MpegEncContext
*s
= &v
->s
;
717 DSPContext
*dsp
= &v
->s
.dsp
;
718 uint8_t *srcU
, *srcV
;
719 int uvdxy
, uvmx
, uvmy
, uvsrc_x
, uvsrc_y
;
720 int i
, idx
, tx
= 0, ty
= 0;
721 int mvx
[4], mvy
[4], intra
[4];
722 static const int count
[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
724 if(!v
->s
.last_picture
.data
[0])return;
725 if(s
->flags
& CODEC_FLAG_GRAY
) return;
727 for(i
= 0; i
< 4; i
++) {
728 mvx
[i
] = s
->mv
[0][i
][0];
729 mvy
[i
] = s
->mv
[0][i
][1];
730 intra
[i
] = v
->mb_type
[0][s
->block_index
[i
]];
733 /* calculate chroma MV vector from four luma MVs */
734 idx
= (intra
[3] << 3) | (intra
[2] << 2) | (intra
[1] << 1) | intra
[0];
735 if(!idx
) { // all blocks are inter
736 tx
= median4(mvx
[0], mvx
[1], mvx
[2], mvx
[3]);
737 ty
= median4(mvy
[0], mvy
[1], mvy
[2], mvy
[3]);
738 } else if(count
[idx
] == 1) { // 3 inter blocks
741 tx
= mid_pred(mvx
[1], mvx
[2], mvx
[3]);
742 ty
= mid_pred(mvy
[1], mvy
[2], mvy
[3]);
745 tx
= mid_pred(mvx
[0], mvx
[2], mvx
[3]);
746 ty
= mid_pred(mvy
[0], mvy
[2], mvy
[3]);
749 tx
= mid_pred(mvx
[0], mvx
[1], mvx
[3]);
750 ty
= mid_pred(mvy
[0], mvy
[1], mvy
[3]);
753 tx
= mid_pred(mvx
[0], mvx
[1], mvx
[2]);
754 ty
= mid_pred(mvy
[0], mvy
[1], mvy
[2]);
757 } else if(count
[idx
] == 2) {
759 for(i
=0; i
<3;i
++) if(!intra
[i
]) {t1
= i
; break;}
760 for(i
= t1
+1; i
<4; i
++)if(!intra
[i
]) {t2
= i
; break;}
761 tx
= (mvx
[t1
] + mvx
[t2
]) / 2;
762 ty
= (mvy
[t1
] + mvy
[t2
]) / 2;
764 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
765 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
766 return; //no need to do MC for inter blocks
769 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = tx
;
770 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = ty
;
771 uvmx
= (tx
+ ((tx
&3) == 3)) >> 1;
772 uvmy
= (ty
+ ((ty
&3) == 3)) >> 1;
774 uvmx
= uvmx
+ ((uvmx
<0)?(uvmx
&1):-(uvmx
&1));
775 uvmy
= uvmy
+ ((uvmy
<0)?(uvmy
&1):-(uvmy
&1));
778 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
779 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
781 if(v
->profile
!= PROFILE_ADVANCED
){
782 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
783 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
785 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
786 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
789 srcU
= s
->last_picture
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
790 srcV
= s
->last_picture
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
791 if(v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
792 || (unsigned)uvsrc_x
> (s
->h_edge_pos
>> 1) - 9
793 || (unsigned)uvsrc_y
> (s
->v_edge_pos
>> 1) - 9){
794 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcU
, s
->uvlinesize
, 8+1, 8+1,
795 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
796 ff_emulated_edge_mc(s
->edge_emu_buffer
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
797 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
798 srcU
= s
->edge_emu_buffer
;
799 srcV
= s
->edge_emu_buffer
+ 16;
801 /* if we deal with range reduction we need to scale source blocks */
806 src
= srcU
; src2
= srcV
;
807 for(j
= 0; j
< 9; j
++) {
808 for(i
= 0; i
< 9; i
++) {
809 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
810 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
812 src
+= s
->uvlinesize
;
813 src2
+= s
->uvlinesize
;
816 /* if we deal with intensity compensation we need to scale source blocks */
817 if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
821 src
= srcU
; src2
= srcV
;
822 for(j
= 0; j
< 9; j
++) {
823 for(i
= 0; i
< 9; i
++) {
824 src
[i
] = v
->lutuv
[src
[i
]];
825 src2
[i
] = v
->lutuv
[src2
[i
]];
827 src
+= s
->uvlinesize
;
828 src2
+= s
->uvlinesize
;
833 /* Chroma MC always uses qpel bilinear */
834 uvdxy
= ((uvmy
& 3) << 2) | (uvmx
& 3);
838 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
839 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
841 dsp
->put_no_rnd_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
842 dsp
->put_no_rnd_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
846 static int decode_sequence_header_adv(VC1Context
*v
, GetBitContext
*gb
);
849 * Decode Simple/Main Profiles sequence header
850 * @see Figure 7-8, p16-17
851 * @param avctx Codec context
852 * @param gb GetBit context initialized from Codec context extra_data
855 static int decode_sequence_header(AVCodecContext
*avctx
, GetBitContext
*gb
)
857 VC1Context
*v
= avctx
->priv_data
;
859 av_log(avctx
, AV_LOG_DEBUG
, "Header: %0X\n", show_bits(gb
, 32));
860 v
->profile
= get_bits(gb
, 2);
861 if (v
->profile
== PROFILE_COMPLEX
)
863 av_log(avctx
, AV_LOG_ERROR
, "WMV3 Complex Profile is not fully supported\n");
866 if (v
->profile
== PROFILE_ADVANCED
)
868 v
->zz_8x4
= ff_vc1_adv_progressive_8x4_zz
;
869 v
->zz_4x8
= ff_vc1_adv_progressive_4x8_zz
;
870 return decode_sequence_header_adv(v
, gb
);
874 v
->zz_8x4
= wmv2_scantableA
;
875 v
->zz_4x8
= wmv2_scantableB
;
876 v
->res_sm
= get_bits(gb
, 2); //reserved
879 av_log(avctx
, AV_LOG_ERROR
,
880 "Reserved RES_SM=%i is forbidden\n", v
->res_sm
);
886 v
->frmrtq_postproc
= get_bits(gb
, 3); //common
887 // (bitrate-32kbps)/64kbps
888 v
->bitrtq_postproc
= get_bits(gb
, 5); //common
889 v
->s
.loop_filter
= get_bits1(gb
); //common
890 if(v
->s
.loop_filter
== 1 && v
->profile
== PROFILE_SIMPLE
)
892 av_log(avctx
, AV_LOG_ERROR
,
893 "LOOPFILTER shell not be enabled in simple profile\n");
895 if(v
->s
.avctx
->skip_loop_filter
>= AVDISCARD_ALL
)
896 v
->s
.loop_filter
= 0;
898 v
->res_x8
= get_bits1(gb
); //reserved
899 v
->multires
= get_bits1(gb
);
900 v
->res_fasttx
= get_bits1(gb
);
903 v
->s
.dsp
.vc1_inv_trans_8x8
= ff_simple_idct
;
904 v
->s
.dsp
.vc1_inv_trans_8x4
= ff_simple_idct84_add
;
905 v
->s
.dsp
.vc1_inv_trans_4x8
= ff_simple_idct48_add
;
906 v
->s
.dsp
.vc1_inv_trans_4x4
= ff_simple_idct44_add
;
909 v
->fastuvmc
= get_bits1(gb
); //common
910 if (!v
->profile
&& !v
->fastuvmc
)
912 av_log(avctx
, AV_LOG_ERROR
,
913 "FASTUVMC unavailable in Simple Profile\n");
916 v
->extended_mv
= get_bits1(gb
); //common
917 if (!v
->profile
&& v
->extended_mv
)
919 av_log(avctx
, AV_LOG_ERROR
,
920 "Extended MVs unavailable in Simple Profile\n");
923 v
->dquant
= get_bits(gb
, 2); //common
924 v
->vstransform
= get_bits1(gb
); //common
926 v
->res_transtab
= get_bits1(gb
);
929 av_log(avctx
, AV_LOG_ERROR
,
930 "1 for reserved RES_TRANSTAB is forbidden\n");
934 v
->overlap
= get_bits1(gb
); //common
936 v
->s
.resync_marker
= get_bits1(gb
);
937 v
->rangered
= get_bits1(gb
);
938 if (v
->rangered
&& v
->profile
== PROFILE_SIMPLE
)
940 av_log(avctx
, AV_LOG_INFO
,
941 "RANGERED should be set to 0 in simple profile\n");
944 v
->s
.max_b_frames
= avctx
->max_b_frames
= get_bits(gb
, 3); //common
945 v
->quantizer_mode
= get_bits(gb
, 2); //common
947 v
->finterpflag
= get_bits1(gb
); //common
948 v
->res_rtm_flag
= get_bits1(gb
); //reserved
949 if (!v
->res_rtm_flag
)
951 // av_log(avctx, AV_LOG_ERROR,
952 // "0 for reserved RES_RTM_FLAG is forbidden\n");
953 av_log(avctx
, AV_LOG_ERROR
,
954 "Old WMV3 version detected, only I-frames will be decoded\n");
957 //TODO: figure out what they mean (always 0x402F)
958 if(!v
->res_fasttx
) skip_bits(gb
, 16);
959 av_log(avctx
, AV_LOG_DEBUG
,
960 "Profile %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
961 "LoopFilter=%i, MultiRes=%i, FastUVMC=%i, Extended MV=%i\n"
962 "Rangered=%i, VSTransform=%i, Overlap=%i, SyncMarker=%i\n"
963 "DQuant=%i, Quantizer mode=%i, Max B frames=%i\n",
964 v
->profile
, v
->frmrtq_postproc
, v
->bitrtq_postproc
,
965 v
->s
.loop_filter
, v
->multires
, v
->fastuvmc
, v
->extended_mv
,
966 v
->rangered
, v
->vstransform
, v
->overlap
, v
->s
.resync_marker
,
967 v
->dquant
, v
->quantizer_mode
, avctx
->max_b_frames
972 static int decode_sequence_header_adv(VC1Context
*v
, GetBitContext
*gb
)
975 v
->level
= get_bits(gb
, 3);
978 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Reserved LEVEL %i\n",v
->level
);
980 v
->chromaformat
= get_bits(gb
, 2);
981 if (v
->chromaformat
!= 1)
983 av_log(v
->s
.avctx
, AV_LOG_ERROR
,
984 "Only 4:2:0 chroma format supported\n");
989 v
->frmrtq_postproc
= get_bits(gb
, 3); //common
990 // (bitrate-32kbps)/64kbps
991 v
->bitrtq_postproc
= get_bits(gb
, 5); //common
992 v
->postprocflag
= get_bits1(gb
); //common
994 v
->s
.avctx
->coded_width
= (get_bits(gb
, 12) + 1) << 1;
995 v
->s
.avctx
->coded_height
= (get_bits(gb
, 12) + 1) << 1;
996 v
->s
.avctx
->width
= v
->s
.avctx
->coded_width
;
997 v
->s
.avctx
->height
= v
->s
.avctx
->coded_height
;
998 v
->broadcast
= get_bits1(gb
);
999 v
->interlace
= get_bits1(gb
);
1000 v
->tfcntrflag
= get_bits1(gb
);
1001 v
->finterpflag
= get_bits1(gb
);
1002 skip_bits1(gb
); // reserved
1004 v
->s
.h_edge_pos
= v
->s
.avctx
->coded_width
;
1005 v
->s
.v_edge_pos
= v
->s
.avctx
->coded_height
;
1007 av_log(v
->s
.avctx
, AV_LOG_DEBUG
,
1008 "Advanced Profile level %i:\nfrmrtq_postproc=%i, bitrtq_postproc=%i\n"
1009 "LoopFilter=%i, ChromaFormat=%i, Pulldown=%i, Interlace: %i\n"
1010 "TFCTRflag=%i, FINTERPflag=%i\n",
1011 v
->level
, v
->frmrtq_postproc
, v
->bitrtq_postproc
,
1012 v
->s
.loop_filter
, v
->chromaformat
, v
->broadcast
, v
->interlace
,
1013 v
->tfcntrflag
, v
->finterpflag
1016 v
->psf
= get_bits1(gb
);
1017 if(v
->psf
) { //PsF, 6.1.13
1018 av_log(v
->s
.avctx
, AV_LOG_ERROR
, "Progressive Segmented Frame mode: not supported (yet)\n");
1021 v
->s
.max_b_frames
= v
->s
.avctx
->max_b_frames
= 7;
1022 if(get_bits1(gb
)) { //Display Info - decoding is not affected by it
1024 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "Display extended info:\n");
1025 v
->s
.avctx
->coded_width
= w
= get_bits(gb
, 14) + 1;
1026 v
->s
.avctx
->coded_height
= h
= get_bits(gb
, 14) + 1;
1027 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "Display dimensions: %ix%i\n", w
, h
);
1029 ar
= get_bits(gb
, 4);
1031 v
->s
.avctx
->sample_aspect_ratio
= ff_vc1_pixel_aspect
[ar
];
1033 w
= get_bits(gb
, 8);
1034 h
= get_bits(gb
, 8);
1035 v
->s
.avctx
->sample_aspect_ratio
= (AVRational
){w
, h
};
1037 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "Aspect: %i:%i\n", v
->s
.avctx
->sample_aspect_ratio
.num
, v
->s
.avctx
->sample_aspect_ratio
.den
);
1039 if(get_bits1(gb
)){ //framerate stuff
1041 v
->s
.avctx
->time_base
.num
= 32;
1042 v
->s
.avctx
->time_base
.den
= get_bits(gb
, 16) + 1;
1045 nr
= get_bits(gb
, 8);
1046 dr
= get_bits(gb
, 4);
1047 if(nr
&& nr
< 8 && dr
&& dr
< 3){
1048 v
->s
.avctx
->time_base
.num
= ff_vc1_fps_dr
[dr
- 1];
1049 v
->s
.avctx
->time_base
.den
= ff_vc1_fps_nr
[nr
- 1] * 1000;
1055 v
->color_prim
= get_bits(gb
, 8);
1056 v
->transfer_char
= get_bits(gb
, 8);
1057 v
->matrix_coef
= get_bits(gb
, 8);
1061 v
->hrd_param_flag
= get_bits1(gb
);
1062 if(v
->hrd_param_flag
) {
1064 v
->hrd_num_leaky_buckets
= get_bits(gb
, 5);
1065 skip_bits(gb
, 4); //bitrate exponent
1066 skip_bits(gb
, 4); //buffer size exponent
1067 for(i
= 0; i
< v
->hrd_num_leaky_buckets
; i
++) {
1068 skip_bits(gb
, 16); //hrd_rate[n]
1069 skip_bits(gb
, 16); //hrd_buffer[n]
1075 static int decode_entry_point(AVCodecContext
*avctx
, GetBitContext
*gb
)
1077 VC1Context
*v
= avctx
->priv_data
;
1080 av_log(avctx
, AV_LOG_DEBUG
, "Entry point: %08X\n", show_bits_long(gb
, 32));
1081 v
->broken_link
= get_bits1(gb
);
1082 v
->closed_entry
= get_bits1(gb
);
1083 v
->panscanflag
= get_bits1(gb
);
1084 v
->refdist_flag
= get_bits1(gb
);
1085 v
->s
.loop_filter
= get_bits1(gb
);
1086 v
->fastuvmc
= get_bits1(gb
);
1087 v
->extended_mv
= get_bits1(gb
);
1088 v
->dquant
= get_bits(gb
, 2);
1089 v
->vstransform
= get_bits1(gb
);
1090 v
->overlap
= get_bits1(gb
);
1091 v
->quantizer_mode
= get_bits(gb
, 2);
1093 if(v
->hrd_param_flag
){
1094 for(i
= 0; i
< v
->hrd_num_leaky_buckets
; i
++) {
1095 skip_bits(gb
, 8); //hrd_full[n]
1100 avctx
->coded_width
= (get_bits(gb
, 12)+1)<<1;
1101 avctx
->coded_height
= (get_bits(gb
, 12)+1)<<1;
1104 v
->extended_dmv
= get_bits1(gb
);
1105 if((v
->range_mapy_flag
= get_bits1(gb
))) {
1106 av_log(avctx
, AV_LOG_ERROR
, "Luma scaling is not supported, expect wrong picture\n");
1107 v
->range_mapy
= get_bits(gb
, 3);
1109 if((v
->range_mapuv_flag
= get_bits1(gb
))) {
1110 av_log(avctx
, AV_LOG_ERROR
, "Chroma scaling is not supported, expect wrong picture\n");
1111 v
->range_mapuv
= get_bits(gb
, 3);
1114 av_log(avctx
, AV_LOG_DEBUG
, "Entry point info:\n"
1115 "BrokenLink=%i, ClosedEntry=%i, PanscanFlag=%i\n"
1116 "RefDist=%i, Postproc=%i, FastUVMC=%i, ExtMV=%i\n"
1117 "DQuant=%i, VSTransform=%i, Overlap=%i, Qmode=%i\n",
1118 v
->broken_link
, v
->closed_entry
, v
->panscanflag
, v
->refdist_flag
, v
->s
.loop_filter
,
1119 v
->fastuvmc
, v
->extended_mv
, v
->dquant
, v
->vstransform
, v
->overlap
, v
->quantizer_mode
);
1124 static int vc1_parse_frame_header(VC1Context
*v
, GetBitContext
* gb
)
1126 int pqindex
, lowquant
, status
;
1128 if(v
->finterpflag
) v
->interpfrm
= get_bits1(gb
);
1129 skip_bits(gb
, 2); //framecnt unused
1131 if (v
->rangered
) v
->rangeredfrm
= get_bits1(gb
);
1132 v
->s
.pict_type
= get_bits1(gb
);
1133 if (v
->s
.avctx
->max_b_frames
) {
1134 if (!v
->s
.pict_type
) {
1135 if (get_bits1(gb
)) v
->s
.pict_type
= FF_I_TYPE
;
1136 else v
->s
.pict_type
= FF_B_TYPE
;
1137 } else v
->s
.pict_type
= FF_P_TYPE
;
1138 } else v
->s
.pict_type
= v
->s
.pict_type
? FF_P_TYPE
: FF_I_TYPE
;
1141 if(v
->s
.pict_type
== FF_B_TYPE
) {
1142 v
->bfraction_lut_index
= get_vlc2(gb
, ff_vc1_bfraction_vlc
.table
, VC1_BFRACTION_VLC_BITS
, 1);
1143 v
->bfraction
= ff_vc1_bfraction_lut
[v
->bfraction_lut_index
];
1144 if(v
->bfraction
== 0) {
1145 v
->s
.pict_type
= FF_BI_TYPE
;
1148 if(v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
)
1149 skip_bits(gb
, 7); // skip buffer fullness
1152 if(v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
)
1154 if(v
->s
.pict_type
== FF_P_TYPE
)
1157 /* Quantizer stuff */
1158 pqindex
= get_bits(gb
, 5);
1159 if(!pqindex
) return -1;
1160 if (v
->quantizer_mode
== QUANT_FRAME_IMPLICIT
)
1161 v
->pq
= ff_vc1_pquant_table
[0][pqindex
];
1163 v
->pq
= ff_vc1_pquant_table
[1][pqindex
];
1166 if (v
->quantizer_mode
== QUANT_FRAME_IMPLICIT
)
1167 v
->pquantizer
= pqindex
< 9;
1168 if (v
->quantizer_mode
== QUANT_NON_UNIFORM
)
1170 v
->pqindex
= pqindex
;
1171 if (pqindex
< 9) v
->halfpq
= get_bits1(gb
);
1173 if (v
->quantizer_mode
== QUANT_FRAME_EXPLICIT
)
1174 v
->pquantizer
= get_bits1(gb
);
1176 if (v
->extended_mv
== 1) v
->mvrange
= get_unary(gb
, 0, 3);
1177 v
->k_x
= v
->mvrange
+ 9 + (v
->mvrange
>> 1); //k_x can be 9 10 12 13
1178 v
->k_y
= v
->mvrange
+ 8; //k_y can be 8 9 10 11
1179 v
->range_x
= 1 << (v
->k_x
- 1);
1180 v
->range_y
= 1 << (v
->k_y
- 1);
1181 if (v
->multires
&& v
->s
.pict_type
!= FF_B_TYPE
) v
->respic
= get_bits(gb
, 2);
1183 if(v
->res_x8
&& (v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
)){
1184 v
->x8_type
= get_bits1(gb
);
1185 }else v
->x8_type
= 0;
1186 //av_log(v->s.avctx, AV_LOG_INFO, "%c Frame: QP=[%i]%i (+%i/2) %i\n",
1187 // (v->s.pict_type == FF_P_TYPE) ? 'P' : ((v->s.pict_type == FF_I_TYPE) ? 'I' : 'B'), pqindex, v->pq, v->halfpq, v->rangeredfrm);
1189 if(v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_P_TYPE
) v
->use_ic
= 0;
1191 switch(v
->s
.pict_type
) {
1193 if (v
->pq
< 5) v
->tt_index
= 0;
1194 else if(v
->pq
< 13) v
->tt_index
= 1;
1195 else v
->tt_index
= 2;
1197 lowquant
= (v
->pq
> 12) ? 0 : 1;
1198 v
->mv_mode
= ff_vc1_mv_pmode_table
[lowquant
][get_unary(gb
, 1, 4)];
1199 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
1201 int scale
, shift
, i
;
1202 v
->mv_mode2
= ff_vc1_mv_pmode_table2
[lowquant
][get_unary(gb
, 1, 3)];
1203 v
->lumscale
= get_bits(gb
, 6);
1204 v
->lumshift
= get_bits(gb
, 6);
1206 /* fill lookup tables for intensity compensation */
1209 shift
= (255 - v
->lumshift
* 2) << 6;
1210 if(v
->lumshift
> 31)
1213 scale
= v
->lumscale
+ 32;
1214 if(v
->lumshift
> 31)
1215 shift
= (v
->lumshift
- 64) << 6;
1217 shift
= v
->lumshift
<< 6;
1219 for(i
= 0; i
< 256; i
++) {
1220 v
->luty
[i
] = av_clip_uint8((scale
* i
+ shift
+ 32) >> 6);
1221 v
->lutuv
[i
] = av_clip_uint8((scale
* (i
- 128) + 128*64 + 32) >> 6);
1224 if(v
->mv_mode
== MV_PMODE_1MV_HPEL
|| v
->mv_mode
== MV_PMODE_1MV_HPEL_BILIN
)
1225 v
->s
.quarter_sample
= 0;
1226 else if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
1227 if(v
->mv_mode2
== MV_PMODE_1MV_HPEL
|| v
->mv_mode2
== MV_PMODE_1MV_HPEL_BILIN
)
1228 v
->s
.quarter_sample
= 0;
1230 v
->s
.quarter_sample
= 1;
1232 v
->s
.quarter_sample
= 1;
1233 v
->s
.mspel
= !(v
->mv_mode
== MV_PMODE_1MV_HPEL_BILIN
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
&& v
->mv_mode2
== MV_PMODE_1MV_HPEL_BILIN
));
1235 if ((v
->mv_mode
== MV_PMODE_INTENSITY_COMP
&&
1236 v
->mv_mode2
== MV_PMODE_MIXED_MV
)
1237 || v
->mv_mode
== MV_PMODE_MIXED_MV
)
1239 status
= bitplane_decoding(v
->mv_type_mb_plane
, &v
->mv_type_is_raw
, v
);
1240 if (status
< 0) return -1;
1241 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB MV Type plane encoding: "
1242 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1244 v
->mv_type_is_raw
= 0;
1245 memset(v
->mv_type_mb_plane
, 0, v
->s
.mb_stride
* v
->s
.mb_height
);
1247 status
= bitplane_decoding(v
->s
.mbskip_table
, &v
->skip_is_raw
, v
);
1248 if (status
< 0) return -1;
1249 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Skip plane encoding: "
1250 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1252 /* Hopefully this is correct for P frames */
1253 v
->s
.mv_table_index
= get_bits(gb
, 2); //but using ff_vc1_ tables
1254 v
->cbpcy_vlc
= &ff_vc1_cbpcy_p_vlc
[get_bits(gb
, 2)];
1258 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "VOP DQuant info\n");
1259 vop_dquant_decoding(v
);
1262 v
->ttfrm
= 0; //FIXME Is that so ?
1265 v
->ttmbf
= get_bits1(gb
);
1268 v
->ttfrm
= ff_vc1_ttfrm_to_tt
[get_bits(gb
, 2)];
1276 if (v
->pq
< 5) v
->tt_index
= 0;
1277 else if(v
->pq
< 13) v
->tt_index
= 1;
1278 else v
->tt_index
= 2;
1280 lowquant
= (v
->pq
> 12) ? 0 : 1;
1281 v
->mv_mode
= get_bits1(gb
) ? MV_PMODE_1MV
: MV_PMODE_1MV_HPEL_BILIN
;
1282 v
->s
.quarter_sample
= (v
->mv_mode
== MV_PMODE_1MV
);
1283 v
->s
.mspel
= v
->s
.quarter_sample
;
1285 status
= bitplane_decoding(v
->direct_mb_plane
, &v
->dmb_is_raw
, v
);
1286 if (status
< 0) return -1;
1287 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Direct Type plane encoding: "
1288 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1289 status
= bitplane_decoding(v
->s
.mbskip_table
, &v
->skip_is_raw
, v
);
1290 if (status
< 0) return -1;
1291 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Skip plane encoding: "
1292 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1294 v
->s
.mv_table_index
= get_bits(gb
, 2);
1295 v
->cbpcy_vlc
= &ff_vc1_cbpcy_p_vlc
[get_bits(gb
, 2)];
1299 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "VOP DQuant info\n");
1300 vop_dquant_decoding(v
);
1306 v
->ttmbf
= get_bits1(gb
);
1309 v
->ttfrm
= ff_vc1_ttfrm_to_tt
[get_bits(gb
, 2)];
1321 v
->c_ac_table_index
= decode012(gb
);
1322 if (v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
)
1324 v
->y_ac_table_index
= decode012(gb
);
1327 v
->s
.dc_table_index
= get_bits1(gb
);
1330 if(v
->s
.pict_type
== FF_BI_TYPE
) {
1331 v
->s
.pict_type
= FF_B_TYPE
;
1337 static int vc1_parse_frame_header_adv(VC1Context
*v
, GetBitContext
* gb
)
1339 int pqindex
, lowquant
;
1342 v
->p_frame_skipped
= 0;
1345 v
->fcm
= decode012(gb
);
1346 if(v
->fcm
) return -1; // interlaced frames/fields are not implemented
1348 switch(get_unary(gb
, 0, 4)) {
1350 v
->s
.pict_type
= FF_P_TYPE
;
1353 v
->s
.pict_type
= FF_B_TYPE
;
1356 v
->s
.pict_type
= FF_I_TYPE
;
1359 v
->s
.pict_type
= FF_BI_TYPE
;
1362 v
->s
.pict_type
= FF_P_TYPE
; // skipped pic
1363 v
->p_frame_skipped
= 1;
1369 if(!v
->interlace
|| v
->psf
) {
1370 v
->rptfrm
= get_bits(gb
, 2);
1372 v
->tff
= get_bits1(gb
);
1373 v
->rptfrm
= get_bits1(gb
);
1376 if(v
->panscanflag
) {
1379 v
->rnd
= get_bits1(gb
);
1381 v
->uvsamp
= get_bits1(gb
);
1382 if(v
->finterpflag
) v
->interpfrm
= get_bits1(gb
);
1383 if(v
->s
.pict_type
== FF_B_TYPE
) {
1384 v
->bfraction_lut_index
= get_vlc2(gb
, ff_vc1_bfraction_vlc
.table
, VC1_BFRACTION_VLC_BITS
, 1);
1385 v
->bfraction
= ff_vc1_bfraction_lut
[v
->bfraction_lut_index
];
1386 if(v
->bfraction
== 0) {
1387 v
->s
.pict_type
= FF_BI_TYPE
; /* XXX: should not happen here */
1390 pqindex
= get_bits(gb
, 5);
1391 if(!pqindex
) return -1;
1392 v
->pqindex
= pqindex
;
1393 if (v
->quantizer_mode
== QUANT_FRAME_IMPLICIT
)
1394 v
->pq
= ff_vc1_pquant_table
[0][pqindex
];
1396 v
->pq
= ff_vc1_pquant_table
[1][pqindex
];
1399 if (v
->quantizer_mode
== QUANT_FRAME_IMPLICIT
)
1400 v
->pquantizer
= pqindex
< 9;
1401 if (v
->quantizer_mode
== QUANT_NON_UNIFORM
)
1403 v
->pqindex
= pqindex
;
1404 if (pqindex
< 9) v
->halfpq
= get_bits1(gb
);
1406 if (v
->quantizer_mode
== QUANT_FRAME_EXPLICIT
)
1407 v
->pquantizer
= get_bits1(gb
);
1409 v
->postproc
= get_bits(gb
, 2);
1411 if(v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_P_TYPE
) v
->use_ic
= 0;
1413 switch(v
->s
.pict_type
) {
1416 status
= bitplane_decoding(v
->acpred_plane
, &v
->acpred_is_raw
, v
);
1417 if (status
< 0) return -1;
1418 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "ACPRED plane encoding: "
1419 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1420 v
->condover
= CONDOVER_NONE
;
1421 if(v
->overlap
&& v
->pq
<= 8) {
1422 v
->condover
= decode012(gb
);
1423 if(v
->condover
== CONDOVER_SELECT
) {
1424 status
= bitplane_decoding(v
->over_flags_plane
, &v
->overflg_is_raw
, v
);
1425 if (status
< 0) return -1;
1426 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "CONDOVER plane encoding: "
1427 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1432 if (v
->extended_mv
) v
->mvrange
= get_unary(gb
, 0, 3);
1433 else v
->mvrange
= 0;
1434 v
->k_x
= v
->mvrange
+ 9 + (v
->mvrange
>> 1); //k_x can be 9 10 12 13
1435 v
->k_y
= v
->mvrange
+ 8; //k_y can be 8 9 10 11
1436 v
->range_x
= 1 << (v
->k_x
- 1);
1437 v
->range_y
= 1 << (v
->k_y
- 1);
1439 if (v
->pq
< 5) v
->tt_index
= 0;
1440 else if(v
->pq
< 13) v
->tt_index
= 1;
1441 else v
->tt_index
= 2;
1443 lowquant
= (v
->pq
> 12) ? 0 : 1;
1444 v
->mv_mode
= ff_vc1_mv_pmode_table
[lowquant
][get_unary(gb
, 1, 4)];
1445 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
1447 int scale
, shift
, i
;
1448 v
->mv_mode2
= ff_vc1_mv_pmode_table2
[lowquant
][get_unary(gb
, 1, 3)];
1449 v
->lumscale
= get_bits(gb
, 6);
1450 v
->lumshift
= get_bits(gb
, 6);
1451 /* fill lookup tables for intensity compensation */
1454 shift
= (255 - v
->lumshift
* 2) << 6;
1455 if(v
->lumshift
> 31)
1458 scale
= v
->lumscale
+ 32;
1459 if(v
->lumshift
> 31)
1460 shift
= (v
->lumshift
- 64) << 6;
1462 shift
= v
->lumshift
<< 6;
1464 for(i
= 0; i
< 256; i
++) {
1465 v
->luty
[i
] = av_clip_uint8((scale
* i
+ shift
+ 32) >> 6);
1466 v
->lutuv
[i
] = av_clip_uint8((scale
* (i
- 128) + 128*64 + 32) >> 6);
1470 if(v
->mv_mode
== MV_PMODE_1MV_HPEL
|| v
->mv_mode
== MV_PMODE_1MV_HPEL_BILIN
)
1471 v
->s
.quarter_sample
= 0;
1472 else if(v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
1473 if(v
->mv_mode2
== MV_PMODE_1MV_HPEL
|| v
->mv_mode2
== MV_PMODE_1MV_HPEL_BILIN
)
1474 v
->s
.quarter_sample
= 0;
1476 v
->s
.quarter_sample
= 1;
1478 v
->s
.quarter_sample
= 1;
1479 v
->s
.mspel
= !(v
->mv_mode
== MV_PMODE_1MV_HPEL_BILIN
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
&& v
->mv_mode2
== MV_PMODE_1MV_HPEL_BILIN
));
1481 if ((v
->mv_mode
== MV_PMODE_INTENSITY_COMP
&&
1482 v
->mv_mode2
== MV_PMODE_MIXED_MV
)
1483 || v
->mv_mode
== MV_PMODE_MIXED_MV
)
1485 status
= bitplane_decoding(v
->mv_type_mb_plane
, &v
->mv_type_is_raw
, v
);
1486 if (status
< 0) return -1;
1487 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB MV Type plane encoding: "
1488 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1490 v
->mv_type_is_raw
= 0;
1491 memset(v
->mv_type_mb_plane
, 0, v
->s
.mb_stride
* v
->s
.mb_height
);
1493 status
= bitplane_decoding(v
->s
.mbskip_table
, &v
->skip_is_raw
, v
);
1494 if (status
< 0) return -1;
1495 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Skip plane encoding: "
1496 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1498 /* Hopefully this is correct for P frames */
1499 v
->s
.mv_table_index
= get_bits(gb
, 2); //but using ff_vc1_ tables
1500 v
->cbpcy_vlc
= &ff_vc1_cbpcy_p_vlc
[get_bits(gb
, 2)];
1503 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "VOP DQuant info\n");
1504 vop_dquant_decoding(v
);
1507 v
->ttfrm
= 0; //FIXME Is that so ?
1510 v
->ttmbf
= get_bits1(gb
);
1513 v
->ttfrm
= ff_vc1_ttfrm_to_tt
[get_bits(gb
, 2)];
1521 if (v
->extended_mv
) v
->mvrange
= get_unary(gb
, 0, 3);
1522 else v
->mvrange
= 0;
1523 v
->k_x
= v
->mvrange
+ 9 + (v
->mvrange
>> 1); //k_x can be 9 10 12 13
1524 v
->k_y
= v
->mvrange
+ 8; //k_y can be 8 9 10 11
1525 v
->range_x
= 1 << (v
->k_x
- 1);
1526 v
->range_y
= 1 << (v
->k_y
- 1);
1528 if (v
->pq
< 5) v
->tt_index
= 0;
1529 else if(v
->pq
< 13) v
->tt_index
= 1;
1530 else v
->tt_index
= 2;
1532 lowquant
= (v
->pq
> 12) ? 0 : 1;
1533 v
->mv_mode
= get_bits1(gb
) ? MV_PMODE_1MV
: MV_PMODE_1MV_HPEL_BILIN
;
1534 v
->s
.quarter_sample
= (v
->mv_mode
== MV_PMODE_1MV
);
1535 v
->s
.mspel
= v
->s
.quarter_sample
;
1537 status
= bitplane_decoding(v
->direct_mb_plane
, &v
->dmb_is_raw
, v
);
1538 if (status
< 0) return -1;
1539 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Direct Type plane encoding: "
1540 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1541 status
= bitplane_decoding(v
->s
.mbskip_table
, &v
->skip_is_raw
, v
);
1542 if (status
< 0) return -1;
1543 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "MB Skip plane encoding: "
1544 "Imode: %i, Invert: %i\n", status
>>1, status
&1);
1546 v
->s
.mv_table_index
= get_bits(gb
, 2);
1547 v
->cbpcy_vlc
= &ff_vc1_cbpcy_p_vlc
[get_bits(gb
, 2)];
1551 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "VOP DQuant info\n");
1552 vop_dquant_decoding(v
);
1558 v
->ttmbf
= get_bits1(gb
);
1561 v
->ttfrm
= ff_vc1_ttfrm_to_tt
[get_bits(gb
, 2)];
1571 v
->c_ac_table_index
= decode012(gb
);
1572 if (v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
)
1574 v
->y_ac_table_index
= decode012(gb
);
1577 v
->s
.dc_table_index
= get_bits1(gb
);
1578 if ((v
->s
.pict_type
== FF_I_TYPE
|| v
->s
.pict_type
== FF_BI_TYPE
) && v
->dquant
) {
1579 av_log(v
->s
.avctx
, AV_LOG_DEBUG
, "VOP DQuant info\n");
1580 vop_dquant_decoding(v
);
1584 if(v
->s
.pict_type
== FF_BI_TYPE
) {
1585 v
->s
.pict_type
= FF_B_TYPE
;
1591 /***********************************************************************/
1593 * @defgroup vc1block VC-1 Block-level functions
1594 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1600 * @brief Get macroblock-level quantizer scale
1602 #define GET_MQUANT() \
1606 if (v->dqprofile == DQPROFILE_ALL_MBS) \
1610 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1614 mqdiff = get_bits(gb, 3); \
1615 if (mqdiff != 7) mquant = v->pq + mqdiff; \
1616 else mquant = get_bits(gb, 5); \
1619 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1620 edges = 1 << v->dqsbedge; \
1621 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1622 edges = (3 << v->dqsbedge) % 15; \
1623 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
1625 if((edges&1) && !s->mb_x) \
1626 mquant = v->altpq; \
1627 if((edges&2) && s->first_slice_line) \
1628 mquant = v->altpq; \
1629 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
1630 mquant = v->altpq; \
1631 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
1632 mquant = v->altpq; \
1636 * @def GET_MVDATA(_dmv_x, _dmv_y)
1637 * @brief Get MV differentials
1638 * @see MVDATA decoding from 8.3.5.2, p(1)20
1639 * @param _dmv_x Horizontal differential for decoded MV
1640 * @param _dmv_y Vertical differential for decoded MV
1642 #define GET_MVDATA(_dmv_x, _dmv_y) \
1643 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
1644 VC1_MV_DIFF_VLC_BITS, 2); \
1647 mb_has_coeffs = 1; \
1650 else mb_has_coeffs = 0; \
1652 if (!index) { _dmv_x = _dmv_y = 0; } \
1653 else if (index == 35) \
1655 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1656 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1658 else if (index == 36) \
1667 if (!s->quarter_sample && index1 == 5) val = 1; \
1669 if(size_table[index1] - val > 0) \
1670 val = get_bits(gb, size_table[index1] - val); \
1672 sign = 0 - (val&1); \
1673 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1676 if (!s->quarter_sample && index1 == 5) val = 1; \
1678 if(size_table[index1] - val > 0) \
1679 val = get_bits(gb, size_table[index1] - val); \
1681 sign = 0 - (val&1); \
1682 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1685 /** Predict and set motion vector
1687 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
)
1689 int xy
, wrap
, off
= 0;
1694 /* scale MV difference to be quad-pel */
1695 dmv_x
<<= 1 - s
->quarter_sample
;
1696 dmv_y
<<= 1 - s
->quarter_sample
;
1698 wrap
= s
->b8_stride
;
1699 xy
= s
->block_index
[n
];
1702 s
->mv
[0][n
][0] = s
->current_picture
.motion_val
[0][xy
][0] = 0;
1703 s
->mv
[0][n
][1] = s
->current_picture
.motion_val
[0][xy
][1] = 0;
1704 s
->current_picture
.motion_val
[1][xy
][0] = 0;
1705 s
->current_picture
.motion_val
[1][xy
][1] = 0;
1706 if(mv1
) { /* duplicate motion data for 1-MV block */
1707 s
->current_picture
.motion_val
[0][xy
+ 1][0] = 0;
1708 s
->current_picture
.motion_val
[0][xy
+ 1][1] = 0;
1709 s
->current_picture
.motion_val
[0][xy
+ wrap
][0] = 0;
1710 s
->current_picture
.motion_val
[0][xy
+ wrap
][1] = 0;
1711 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][0] = 0;
1712 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][1] = 0;
1713 s
->current_picture
.motion_val
[1][xy
+ 1][0] = 0;
1714 s
->current_picture
.motion_val
[1][xy
+ 1][1] = 0;
1715 s
->current_picture
.motion_val
[1][xy
+ wrap
][0] = 0;
1716 s
->current_picture
.motion_val
[1][xy
+ wrap
][1] = 0;
1717 s
->current_picture
.motion_val
[1][xy
+ wrap
+ 1][0] = 0;
1718 s
->current_picture
.motion_val
[1][xy
+ wrap
+ 1][1] = 0;
1723 C
= s
->current_picture
.motion_val
[0][xy
- 1];
1724 A
= s
->current_picture
.motion_val
[0][xy
- wrap
];
1726 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -1 : 2;
1728 //in 4-MV mode different blocks have different B predictor position
1731 off
= (s
->mb_x
> 0) ? -1 : 1;
1734 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -1 : 1;
1743 B
= s
->current_picture
.motion_val
[0][xy
- wrap
+ off
];
1745 if(!s
->first_slice_line
|| (n
==2 || n
==3)) { // predictor A is not out of bounds
1746 if(s
->mb_width
== 1) {
1750 px
= mid_pred(A
[0], B
[0], C
[0]);
1751 py
= mid_pred(A
[1], B
[1], C
[1]);
1753 } else if(s
->mb_x
|| (n
==1 || n
==3)) { // predictor C is not out of bounds
1759 /* Pullback MV as specified in 8.3.5.3.4 */
1762 qx
= (s
->mb_x
<< 6) + ((n
==1 || n
==3) ? 32 : 0);
1763 qy
= (s
->mb_y
<< 6) + ((n
==2 || n
==3) ? 32 : 0);
1764 X
= (s
->mb_width
<< 6) - 4;
1765 Y
= (s
->mb_height
<< 6) - 4;
1767 if(qx
+ px
< -60) px
= -60 - qx
;
1768 if(qy
+ py
< -60) py
= -60 - qy
;
1770 if(qx
+ px
< -28) px
= -28 - qx
;
1771 if(qy
+ py
< -28) py
= -28 - qy
;
1773 if(qx
+ px
> X
) px
= X
- qx
;
1774 if(qy
+ py
> Y
) py
= Y
- qy
;
1776 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1777 if((!s
->first_slice_line
|| (n
==2 || n
==3)) && (s
->mb_x
|| (n
==1 || n
==3))) {
1778 if(is_intra
[xy
- wrap
])
1779 sum
= FFABS(px
) + FFABS(py
);
1781 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
1783 if(get_bits1(&s
->gb
)) {
1791 if(is_intra
[xy
- 1])
1792 sum
= FFABS(px
) + FFABS(py
);
1794 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
1796 if(get_bits1(&s
->gb
)) {
1806 /* store MV using signed modulus of MV range defined in 4.11 */
1807 s
->mv
[0][n
][0] = s
->current_picture
.motion_val
[0][xy
][0] = ((px
+ dmv_x
+ r_x
) & ((r_x
<< 1) - 1)) - r_x
;
1808 s
->mv
[0][n
][1] = s
->current_picture
.motion_val
[0][xy
][1] = ((py
+ dmv_y
+ r_y
) & ((r_y
<< 1) - 1)) - r_y
;
1809 if(mv1
) { /* duplicate motion data for 1-MV block */
1810 s
->current_picture
.motion_val
[0][xy
+ 1][0] = s
->current_picture
.motion_val
[0][xy
][0];
1811 s
->current_picture
.motion_val
[0][xy
+ 1][1] = s
->current_picture
.motion_val
[0][xy
][1];
1812 s
->current_picture
.motion_val
[0][xy
+ wrap
][0] = s
->current_picture
.motion_val
[0][xy
][0];
1813 s
->current_picture
.motion_val
[0][xy
+ wrap
][1] = s
->current_picture
.motion_val
[0][xy
][1];
1814 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][0] = s
->current_picture
.motion_val
[0][xy
][0];
1815 s
->current_picture
.motion_val
[0][xy
+ wrap
+ 1][1] = s
->current_picture
.motion_val
[0][xy
][1];
1819 /** Motion compensation for direct or interpolated blocks in B-frames
1821 static void vc1_interp_mc(VC1Context
*v
)
1823 MpegEncContext
*s
= &v
->s
;
1824 DSPContext
*dsp
= &v
->s
.dsp
;
1825 uint8_t *srcY
, *srcU
, *srcV
;
1826 int dxy
, uvdxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
1828 if(!v
->s
.next_picture
.data
[0])return;
1830 mx
= s
->mv
[1][0][0];
1831 my
= s
->mv
[1][0][1];
1832 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
1833 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
1835 uvmx
= uvmx
+ ((uvmx
<0)?-(uvmx
&1):(uvmx
&1));
1836 uvmy
= uvmy
+ ((uvmy
<0)?-(uvmy
&1):(uvmy
&1));
1838 srcY
= s
->next_picture
.data
[0];
1839 srcU
= s
->next_picture
.data
[1];
1840 srcV
= s
->next_picture
.data
[2];
1842 src_x
= s
->mb_x
* 16 + (mx
>> 2);
1843 src_y
= s
->mb_y
* 16 + (my
>> 2);
1844 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
1845 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
1847 if(v
->profile
!= PROFILE_ADVANCED
){
1848 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
1849 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
1850 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
1851 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
1853 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
1854 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
1855 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
1856 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
1859 srcY
+= src_y
* s
->linesize
+ src_x
;
1860 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1861 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1863 /* for grayscale we should not try to read from unknown area */
1864 if(s
->flags
& CODEC_FLAG_GRAY
) {
1865 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
1866 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
1870 || (unsigned)src_x
> s
->h_edge_pos
- (mx
&3) - 16
1871 || (unsigned)src_y
> s
->v_edge_pos
- (my
&3) - 16){
1872 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
1874 srcY
-= s
->mspel
* (1 + s
->linesize
);
1875 ff_emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
, 17+s
->mspel
*2, 17+s
->mspel
*2,
1876 src_x
- s
->mspel
, src_y
- s
->mspel
, s
->h_edge_pos
, s
->v_edge_pos
);
1877 srcY
= s
->edge_emu_buffer
;
1878 ff_emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8+1, 8+1,
1879 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
1880 ff_emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8+1, 8+1,
1881 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, s
->v_edge_pos
>> 1);
1884 /* if we deal with range reduction we need to scale source blocks */
1885 if(v
->rangeredfrm
) {
1887 uint8_t *src
, *src2
;
1890 for(j
= 0; j
< 17 + s
->mspel
*2; j
++) {
1891 for(i
= 0; i
< 17 + s
->mspel
*2; i
++) src
[i
] = ((src
[i
] - 128) >> 1) + 128;
1894 src
= srcU
; src2
= srcV
;
1895 for(j
= 0; j
< 9; j
++) {
1896 for(i
= 0; i
< 9; i
++) {
1897 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
1898 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
1900 src
+= s
->uvlinesize
;
1901 src2
+= s
->uvlinesize
;
1904 srcY
+= s
->mspel
* (1 + s
->linesize
);
1909 dxy
= ((my
& 1) << 1) | (mx
& 1);
1911 dsp
->avg_pixels_tab
[0][dxy
](s
->dest
[0], srcY
, s
->linesize
, 16);
1913 if(s
->flags
& CODEC_FLAG_GRAY
) return;
1914 /* Chroma MC always uses qpel blilinear */
1915 uvdxy
= ((uvmy
& 3) << 2) | (uvmx
& 3);
1918 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[1], srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1919 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[2], srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1922 static av_always_inline
int scale_mv(int value
, int bfrac
, int inv
, int qs
)
1926 #if B_FRACTION_DEN==256
1930 return 2 * ((value
* n
+ 255) >> 9);
1931 return (value
* n
+ 128) >> 8;
1934 n
-= B_FRACTION_DEN
;
1936 return 2 * ((value
* n
+ B_FRACTION_DEN
- 1) / (2 * B_FRACTION_DEN
));
1937 return (value
* n
+ B_FRACTION_DEN
/2) / B_FRACTION_DEN
;
1941 /** Reconstruct motion vector for B-frame and do motion compensation
1943 static inline void vc1_b_mc(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2], int direct
, int mode
)
1946 v
->mv_mode2
= v
->mv_mode
;
1947 v
->mv_mode
= MV_PMODE_INTENSITY_COMP
;
1952 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
1955 if(mode
== BMV_TYPE_INTERPOLATED
) {
1958 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
1962 if(v
->use_ic
&& (mode
== BMV_TYPE_BACKWARD
)) v
->mv_mode
= v
->mv_mode2
;
1963 vc1_mc_1mv(v
, (mode
== BMV_TYPE_BACKWARD
));
1964 if(v
->use_ic
) v
->mv_mode
= v
->mv_mode2
;
1967 static inline void vc1_pred_b_mv(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2], int direct
, int mvtype
)
1969 MpegEncContext
*s
= &v
->s
;
1970 int xy
, wrap
, off
= 0;
1975 const uint8_t *is_intra
= v
->mb_type
[0];
1979 /* scale MV difference to be quad-pel */
1980 dmv_x
[0] <<= 1 - s
->quarter_sample
;
1981 dmv_y
[0] <<= 1 - s
->quarter_sample
;
1982 dmv_x
[1] <<= 1 - s
->quarter_sample
;
1983 dmv_y
[1] <<= 1 - s
->quarter_sample
;
1985 wrap
= s
->b8_stride
;
1986 xy
= s
->block_index
[0];
1989 s
->current_picture
.motion_val
[0][xy
][0] =
1990 s
->current_picture
.motion_val
[0][xy
][1] =
1991 s
->current_picture
.motion_val
[1][xy
][0] =
1992 s
->current_picture
.motion_val
[1][xy
][1] = 0;
1995 s
->mv
[0][0][0] = scale_mv(s
->next_picture
.motion_val
[1][xy
][0], v
->bfraction
, 0, s
->quarter_sample
);
1996 s
->mv
[0][0][1] = scale_mv(s
->next_picture
.motion_val
[1][xy
][1], v
->bfraction
, 0, s
->quarter_sample
);
1997 s
->mv
[1][0][0] = scale_mv(s
->next_picture
.motion_val
[1][xy
][0], v
->bfraction
, 1, s
->quarter_sample
);
1998 s
->mv
[1][0][1] = scale_mv(s
->next_picture
.motion_val
[1][xy
][1], v
->bfraction
, 1, s
->quarter_sample
);
2000 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2001 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));
2002 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));
2003 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));
2004 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));
2006 s
->current_picture
.motion_val
[0][xy
][0] = s
->mv
[0][0][0];
2007 s
->current_picture
.motion_val
[0][xy
][1] = s
->mv
[0][0][1];
2008 s
->current_picture
.motion_val
[1][xy
][0] = s
->mv
[1][0][0];
2009 s
->current_picture
.motion_val
[1][xy
][1] = s
->mv
[1][0][1];
2013 if((mvtype
== BMV_TYPE_FORWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
2014 C
= s
->current_picture
.motion_val
[0][xy
- 2];
2015 A
= s
->current_picture
.motion_val
[0][xy
- wrap
*2];
2016 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -2 : 2;
2017 B
= s
->current_picture
.motion_val
[0][xy
- wrap
*2 + off
];
2019 if(!s
->mb_x
) C
[0] = C
[1] = 0;
2020 if(!s
->first_slice_line
) { // predictor A is not out of bounds
2021 if(s
->mb_width
== 1) {
2025 px
= mid_pred(A
[0], B
[0], C
[0]);
2026 py
= mid_pred(A
[1], B
[1], C
[1]);
2028 } else if(s
->mb_x
) { // predictor C is not out of bounds
2034 /* Pullback MV as specified in 8.3.5.3.4 */
2037 if(v
->profile
< PROFILE_ADVANCED
) {
2038 qx
= (s
->mb_x
<< 5);
2039 qy
= (s
->mb_y
<< 5);
2040 X
= (s
->mb_width
<< 5) - 4;
2041 Y
= (s
->mb_height
<< 5) - 4;
2042 if(qx
+ px
< -28) px
= -28 - qx
;
2043 if(qy
+ py
< -28) py
= -28 - qy
;
2044 if(qx
+ px
> X
) px
= X
- qx
;
2045 if(qy
+ py
> Y
) py
= Y
- qy
;
2047 qx
= (s
->mb_x
<< 6);
2048 qy
= (s
->mb_y
<< 6);
2049 X
= (s
->mb_width
<< 6) - 4;
2050 Y
= (s
->mb_height
<< 6) - 4;
2051 if(qx
+ px
< -60) px
= -60 - qx
;
2052 if(qy
+ py
< -60) py
= -60 - qy
;
2053 if(qx
+ px
> X
) px
= X
- qx
;
2054 if(qy
+ py
> Y
) py
= Y
- qy
;
2057 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2058 if(0 && !s
->first_slice_line
&& s
->mb_x
) {
2059 if(is_intra
[xy
- wrap
])
2060 sum
= FFABS(px
) + FFABS(py
);
2062 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
2064 if(get_bits1(&s
->gb
)) {
2072 if(is_intra
[xy
- 2])
2073 sum
= FFABS(px
) + FFABS(py
);
2075 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
2077 if(get_bits1(&s
->gb
)) {
2087 /* store MV using signed modulus of MV range defined in 4.11 */
2088 s
->mv
[0][0][0] = ((px
+ dmv_x
[0] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
2089 s
->mv
[0][0][1] = ((py
+ dmv_y
[0] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
2091 if((mvtype
== BMV_TYPE_BACKWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
2092 C
= s
->current_picture
.motion_val
[1][xy
- 2];
2093 A
= s
->current_picture
.motion_val
[1][xy
- wrap
*2];
2094 off
= (s
->mb_x
== (s
->mb_width
- 1)) ? -2 : 2;
2095 B
= s
->current_picture
.motion_val
[1][xy
- wrap
*2 + off
];
2097 if(!s
->mb_x
) C
[0] = C
[1] = 0;
2098 if(!s
->first_slice_line
) { // predictor A is not out of bounds
2099 if(s
->mb_width
== 1) {
2103 px
= mid_pred(A
[0], B
[0], C
[0]);
2104 py
= mid_pred(A
[1], B
[1], C
[1]);
2106 } else if(s
->mb_x
) { // predictor C is not out of bounds
2112 /* Pullback MV as specified in 8.3.5.3.4 */
2115 if(v
->profile
< PROFILE_ADVANCED
) {
2116 qx
= (s
->mb_x
<< 5);
2117 qy
= (s
->mb_y
<< 5);
2118 X
= (s
->mb_width
<< 5) - 4;
2119 Y
= (s
->mb_height
<< 5) - 4;
2120 if(qx
+ px
< -28) px
= -28 - qx
;
2121 if(qy
+ py
< -28) py
= -28 - qy
;
2122 if(qx
+ px
> X
) px
= X
- qx
;
2123 if(qy
+ py
> Y
) py
= Y
- qy
;
2125 qx
= (s
->mb_x
<< 6);
2126 qy
= (s
->mb_y
<< 6);
2127 X
= (s
->mb_width
<< 6) - 4;
2128 Y
= (s
->mb_height
<< 6) - 4;
2129 if(qx
+ px
< -60) px
= -60 - qx
;
2130 if(qy
+ py
< -60) py
= -60 - qy
;
2131 if(qx
+ px
> X
) px
= X
- qx
;
2132 if(qy
+ py
> Y
) py
= Y
- qy
;
2135 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2136 if(0 && !s
->first_slice_line
&& s
->mb_x
) {
2137 if(is_intra
[xy
- wrap
])
2138 sum
= FFABS(px
) + FFABS(py
);
2140 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
2142 if(get_bits1(&s
->gb
)) {
2150 if(is_intra
[xy
- 2])
2151 sum
= FFABS(px
) + FFABS(py
);
2153 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
2155 if(get_bits1(&s
->gb
)) {
2165 /* store MV using signed modulus of MV range defined in 4.11 */
2167 s
->mv
[1][0][0] = ((px
+ dmv_x
[1] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
2168 s
->mv
[1][0][1] = ((py
+ dmv_y
[1] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
2170 s
->current_picture
.motion_val
[0][xy
][0] = s
->mv
[0][0][0];
2171 s
->current_picture
.motion_val
[0][xy
][1] = s
->mv
[0][0][1];
2172 s
->current_picture
.motion_val
[1][xy
][0] = s
->mv
[1][0][0];
2173 s
->current_picture
.motion_val
[1][xy
][1] = s
->mv
[1][0][1];
2176 /** Get predicted DC value for I-frames only
2177 * prediction dir: left=0, top=1
2178 * @param s MpegEncContext
2179 * @param overlap flag indicating that overlap filtering is used
2180 * @param pq integer part of picture quantizer
2181 * @param[in] n block index in the current MB
2182 * @param dc_val_ptr Pointer to DC predictor
2183 * @param dir_ptr Prediction direction for use in AC prediction
2185 static inline int vc1_i_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
2186 int16_t **dc_val_ptr
, int *dir_ptr
)
2188 int a
, b
, c
, wrap
, pred
, scale
;
2190 static const uint16_t dcpred
[32] = {
2191 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2192 114, 102, 93, 85, 79, 73, 68, 64,
2193 60, 57, 54, 51, 49, 47, 45, 43,
2194 41, 39, 38, 37, 35, 34, 33
2197 /* find prediction - wmv3_dc_scale always used here in fact */
2198 if (n
< 4) scale
= s
->y_dc_scale
;
2199 else scale
= s
->c_dc_scale
;
2201 wrap
= s
->block_wrap
[n
];
2202 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
2208 b
= dc_val
[ - 1 - wrap
];
2209 a
= dc_val
[ - wrap
];
2211 if (pq
< 9 || !overlap
)
2213 /* Set outer values */
2214 if (s
->first_slice_line
&& (n
!=2 && n
!=3)) b
=a
=dcpred
[scale
];
2215 if (s
->mb_x
== 0 && (n
!=1 && n
!=3)) b
=c
=dcpred
[scale
];
2219 /* Set outer values */
2220 if (s
->first_slice_line
&& (n
!=2 && n
!=3)) b
=a
=0;
2221 if (s
->mb_x
== 0 && (n
!=1 && n
!=3)) b
=c
=0;
2224 if (abs(a
- b
) <= abs(b
- c
)) {
2232 /* update predictor */
2233 *dc_val_ptr
= &dc_val
[0];
2238 /** Get predicted DC value
2239 * prediction dir: left=0, top=1
2240 * @param s MpegEncContext
2241 * @param overlap flag indicating that overlap filtering is used
2242 * @param pq integer part of picture quantizer
2243 * @param[in] n block index in the current MB
2244 * @param a_avail flag indicating top block availability
2245 * @param c_avail flag indicating left block availability
2246 * @param dc_val_ptr Pointer to DC predictor
2247 * @param dir_ptr Prediction direction for use in AC prediction
2249 static inline int vc1_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
2250 int a_avail
, int c_avail
,
2251 int16_t **dc_val_ptr
, int *dir_ptr
)
2253 int a
, b
, c
, wrap
, pred
, scale
;
2255 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2258 /* find prediction - wmv3_dc_scale always used here in fact */
2259 if (n
< 4) scale
= s
->y_dc_scale
;
2260 else scale
= s
->c_dc_scale
;
2262 wrap
= s
->block_wrap
[n
];
2263 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
2269 b
= dc_val
[ - 1 - wrap
];
2270 a
= dc_val
[ - wrap
];
2271 /* scale predictors if needed */
2272 q1
= s
->current_picture
.qscale_table
[mb_pos
];
2273 if(c_avail
&& (n
!= 1 && n
!=3)) {
2274 q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
2276 c
= (c
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2278 if(a_avail
&& (n
!= 2 && n
!=3)) {
2279 q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
2281 a
= (a
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2283 if(a_avail
&& c_avail
&& (n
!=3)) {
2286 if(n
!= 2) off
-= s
->mb_stride
;
2287 q2
= s
->current_picture
.qscale_table
[off
];
2289 b
= (b
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2292 if(a_avail
&& c_avail
) {
2293 if(abs(a
- b
) <= abs(b
- c
)) {
2300 } else if(a_avail
) {
2303 } else if(c_avail
) {
2311 /* update predictor */
2312 *dc_val_ptr
= &dc_val
[0];
2316 /** @} */ // Block group
2319 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
2320 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2324 static inline int vc1_coded_block_pred(MpegEncContext
* s
, int n
, uint8_t **coded_block_ptr
)
2326 int xy
, wrap
, pred
, a
, b
, c
;
2328 xy
= s
->block_index
[n
];
2329 wrap
= s
->b8_stride
;
2334 a
= s
->coded_block
[xy
- 1 ];
2335 b
= s
->coded_block
[xy
- 1 - wrap
];
2336 c
= s
->coded_block
[xy
- wrap
];
2345 *coded_block_ptr
= &s
->coded_block
[xy
];
2351 * Decode one AC coefficient
2352 * @param v The VC1 context
2353 * @param last Last coefficient
2354 * @param skip How much zero coefficients to skip
2355 * @param value Decoded AC coefficient value
2356 * @param codingset set of VLC to decode data
2359 static void vc1_decode_ac_coeff(VC1Context
*v
, int *last
, int *skip
, int *value
, int codingset
)
2361 GetBitContext
*gb
= &v
->s
.gb
;
2362 int index
, escape
, run
= 0, level
= 0, lst
= 0;
2364 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
2365 if (index
!= vc1_ac_sizes
[codingset
] - 1) {
2366 run
= vc1_index_decode_table
[codingset
][index
][0];
2367 level
= vc1_index_decode_table
[codingset
][index
][1];
2368 lst
= index
>= vc1_last_decode_table
[codingset
];
2372 escape
= decode210(gb
);
2374 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
2375 run
= vc1_index_decode_table
[codingset
][index
][0];
2376 level
= vc1_index_decode_table
[codingset
][index
][1];
2377 lst
= index
>= vc1_last_decode_table
[codingset
];
2380 level
+= vc1_last_delta_level_table
[codingset
][run
];
2382 level
+= vc1_delta_level_table
[codingset
][run
];
2385 run
+= vc1_last_delta_run_table
[codingset
][level
] + 1;
2387 run
+= vc1_delta_run_table
[codingset
][level
] + 1;
2393 lst
= get_bits1(gb
);
2394 if(v
->s
.esc3_level_length
== 0) {
2395 if(v
->pq
< 8 || v
->dquantfrm
) { // table 59
2396 v
->s
.esc3_level_length
= get_bits(gb
, 3);
2397 if(!v
->s
.esc3_level_length
)
2398 v
->s
.esc3_level_length
= get_bits(gb
, 2) + 8;
2400 v
->s
.esc3_level_length
= get_unary(gb
, 1, 6) + 2;
2402 v
->s
.esc3_run_length
= 3 + get_bits(gb
, 2);
2404 run
= get_bits(gb
, v
->s
.esc3_run_length
);
2405 sign
= get_bits1(gb
);
2406 level
= get_bits(gb
, v
->s
.esc3_level_length
);
2417 /** Decode intra block in intra frames - should be faster than decode_intra_block
2418 * @param v VC1Context
2419 * @param block block to decode
2420 * @param[in] n subblock index
2421 * @param coded are AC coeffs present or not
2422 * @param codingset set of VLC to decode data
2424 static int vc1_decode_i_block(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int codingset
)
2426 GetBitContext
*gb
= &v
->s
.gb
;
2427 MpegEncContext
*s
= &v
->s
;
2428 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
2431 int16_t *ac_val
, *ac_val2
;
2434 /* Get DC differential */
2436 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2438 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2441 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
2446 if (dcdiff
== 119 /* ESC index value */)
2448 /* TODO: Optimize */
2449 if (v
->pq
== 1) dcdiff
= get_bits(gb
, 10);
2450 else if (v
->pq
== 2) dcdiff
= get_bits(gb
, 9);
2451 else dcdiff
= get_bits(gb
, 8);
2456 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
2457 else if (v
->pq
== 2)
2458 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
2465 dcdiff
+= vc1_i_pred_dc(&v
->s
, v
->overlap
, v
->pq
, n
, &dc_val
, &dc_pred_dir
);
2468 /* Store the quantized DC coeff, used for prediction */
2470 block
[0] = dcdiff
* s
->y_dc_scale
;
2472 block
[0] = dcdiff
* s
->c_dc_scale
;
2485 int last
= 0, skip
, value
;
2486 const int8_t *zz_table
;
2490 scale
= v
->pq
* 2 + v
->halfpq
;
2494 zz_table
= wmv1_scantable
[2];
2496 zz_table
= wmv1_scantable
[3];
2498 zz_table
= wmv1_scantable
[1];
2500 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2502 if(dc_pred_dir
) //left
2505 ac_val
-= 16 * s
->block_wrap
[n
];
2508 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
2512 block
[zz_table
[i
++]] = value
;
2515 /* apply AC prediction if needed */
2517 if(dc_pred_dir
) { //left
2518 for(k
= 1; k
< 8; k
++)
2519 block
[k
<< 3] += ac_val
[k
];
2521 for(k
= 1; k
< 8; k
++)
2522 block
[k
] += ac_val
[k
+ 8];
2525 /* save AC coeffs for further prediction */
2526 for(k
= 1; k
< 8; k
++) {
2527 ac_val2
[k
] = block
[k
<< 3];
2528 ac_val2
[k
+ 8] = block
[k
];
2531 /* scale AC coeffs */
2532 for(k
= 1; k
< 64; k
++)
2536 block
[k
] += (block
[k
] < 0) ? -v
->pq
: v
->pq
;
2539 if(s
->ac_pred
) i
= 63;
2545 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2548 scale
= v
->pq
* 2 + v
->halfpq
;
2549 memset(ac_val2
, 0, 16 * 2);
2550 if(dc_pred_dir
) {//left
2553 memcpy(ac_val2
, ac_val
, 8 * 2);
2555 ac_val
-= 16 * s
->block_wrap
[n
];
2557 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
2560 /* apply AC prediction if needed */
2562 if(dc_pred_dir
) { //left
2563 for(k
= 1; k
< 8; k
++) {
2564 block
[k
<< 3] = ac_val
[k
] * scale
;
2565 if(!v
->pquantizer
&& block
[k
<< 3])
2566 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -v
->pq
: v
->pq
;
2569 for(k
= 1; k
< 8; k
++) {
2570 block
[k
] = ac_val
[k
+ 8] * scale
;
2571 if(!v
->pquantizer
&& block
[k
])
2572 block
[k
] += (block
[k
] < 0) ? -v
->pq
: v
->pq
;
2578 s
->block_last_index
[n
] = i
;
2583 /** Decode intra block in intra frames - should be faster than decode_intra_block
2584 * @param v VC1Context
2585 * @param block block to decode
2586 * @param[in] n subblock number
2587 * @param coded are AC coeffs present or not
2588 * @param codingset set of VLC to decode data
2589 * @param mquant quantizer value for this macroblock
2591 static int vc1_decode_i_block_adv(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int codingset
, int mquant
)
2593 GetBitContext
*gb
= &v
->s
.gb
;
2594 MpegEncContext
*s
= &v
->s
;
2595 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
2598 int16_t *ac_val
, *ac_val2
;
2600 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
2601 int use_pred
= s
->ac_pred
;
2604 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2606 /* Get DC differential */
2608 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2610 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2613 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
2618 if (dcdiff
== 119 /* ESC index value */)
2620 /* TODO: Optimize */
2621 if (mquant
== 1) dcdiff
= get_bits(gb
, 10);
2622 else if (mquant
== 2) dcdiff
= get_bits(gb
, 9);
2623 else dcdiff
= get_bits(gb
, 8);
2628 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
2629 else if (mquant
== 2)
2630 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
2637 dcdiff
+= vc1_pred_dc(&v
->s
, v
->overlap
, mquant
, n
, v
->a_avail
, v
->c_avail
, &dc_val
, &dc_pred_dir
);
2640 /* Store the quantized DC coeff, used for prediction */
2642 block
[0] = dcdiff
* s
->y_dc_scale
;
2644 block
[0] = dcdiff
* s
->c_dc_scale
;
2653 /* check if AC is needed at all */
2654 if(!a_avail
&& !c_avail
) use_pred
= 0;
2655 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2658 scale
= mquant
* 2 + ((mquant
== v
->pq
) ? v
->halfpq
: 0);
2660 if(dc_pred_dir
) //left
2663 ac_val
-= 16 * s
->block_wrap
[n
];
2665 q1
= s
->current_picture
.qscale_table
[mb_pos
];
2666 if(dc_pred_dir
&& c_avail
&& mb_pos
) q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
2667 if(!dc_pred_dir
&& a_avail
&& mb_pos
>= s
->mb_stride
) q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
2668 if(dc_pred_dir
&& n
==1) q2
= q1
;
2669 if(!dc_pred_dir
&& n
==2) q2
= q1
;
2673 int last
= 0, skip
, value
;
2674 const int8_t *zz_table
;
2679 zz_table
= wmv1_scantable
[2];
2681 zz_table
= wmv1_scantable
[3];
2683 zz_table
= wmv1_scantable
[1];
2686 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
2690 block
[zz_table
[i
++]] = value
;
2693 /* apply AC prediction if needed */
2695 /* scale predictors if needed*/
2697 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2698 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2700 if(dc_pred_dir
) { //left
2701 for(k
= 1; k
< 8; k
++)
2702 block
[k
<< 3] += (ac_val
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2704 for(k
= 1; k
< 8; k
++)
2705 block
[k
] += (ac_val
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2708 if(dc_pred_dir
) { //left
2709 for(k
= 1; k
< 8; k
++)
2710 block
[k
<< 3] += ac_val
[k
];
2712 for(k
= 1; k
< 8; k
++)
2713 block
[k
] += ac_val
[k
+ 8];
2717 /* save AC coeffs for further prediction */
2718 for(k
= 1; k
< 8; k
++) {
2719 ac_val2
[k
] = block
[k
<< 3];
2720 ac_val2
[k
+ 8] = block
[k
];
2723 /* scale AC coeffs */
2724 for(k
= 1; k
< 64; k
++)
2728 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
2731 if(use_pred
) i
= 63;
2732 } else { // no AC coeffs
2735 memset(ac_val2
, 0, 16 * 2);
2736 if(dc_pred_dir
) {//left
2738 memcpy(ac_val2
, ac_val
, 8 * 2);
2740 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2741 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2742 for(k
= 1; k
< 8; k
++)
2743 ac_val2
[k
] = (ac_val2
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2748 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
2750 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2751 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2752 for(k
= 1; k
< 8; k
++)
2753 ac_val2
[k
+ 8] = (ac_val2
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2758 /* apply AC prediction if needed */
2760 if(dc_pred_dir
) { //left
2761 for(k
= 1; k
< 8; k
++) {
2762 block
[k
<< 3] = ac_val2
[k
] * scale
;
2763 if(!v
->pquantizer
&& block
[k
<< 3])
2764 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -mquant
: mquant
;
2767 for(k
= 1; k
< 8; k
++) {
2768 block
[k
] = ac_val2
[k
+ 8] * scale
;
2769 if(!v
->pquantizer
&& block
[k
])
2770 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
2776 s
->block_last_index
[n
] = i
;
2781 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2782 * @param v VC1Context
2783 * @param block block to decode
2784 * @param[in] n subblock index
2785 * @param coded are AC coeffs present or not
2786 * @param mquant block quantizer
2787 * @param codingset set of VLC to decode data
2789 static int vc1_decode_intra_block(VC1Context
*v
, DCTELEM block
[64], int n
, int coded
, int mquant
, int codingset
)
2791 GetBitContext
*gb
= &v
->s
.gb
;
2792 MpegEncContext
*s
= &v
->s
;
2793 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
2796 int16_t *ac_val
, *ac_val2
;
2798 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2799 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
2800 int use_pred
= s
->ac_pred
;
2804 /* XXX: Guard against dumb values of mquant */
2805 mquant
= (mquant
< 1) ? 0 : ( (mquant
>31) ? 31 : mquant
);
2807 /* Set DC scale - y and c use the same */
2808 s
->y_dc_scale
= s
->y_dc_scale_table
[mquant
];
2809 s
->c_dc_scale
= s
->c_dc_scale_table
[mquant
];
2811 /* Get DC differential */
2813 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2815 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2818 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
2823 if (dcdiff
== 119 /* ESC index value */)
2825 /* TODO: Optimize */
2826 if (mquant
== 1) dcdiff
= get_bits(gb
, 10);
2827 else if (mquant
== 2) dcdiff
= get_bits(gb
, 9);
2828 else dcdiff
= get_bits(gb
, 8);
2833 dcdiff
= (dcdiff
<<2) + get_bits(gb
, 2) - 3;
2834 else if (mquant
== 2)
2835 dcdiff
= (dcdiff
<<1) + get_bits1(gb
) - 1;
2842 dcdiff
+= vc1_pred_dc(&v
->s
, v
->overlap
, mquant
, n
, a_avail
, c_avail
, &dc_val
, &dc_pred_dir
);
2845 /* Store the quantized DC coeff, used for prediction */
2848 block
[0] = dcdiff
* s
->y_dc_scale
;
2850 block
[0] = dcdiff
* s
->c_dc_scale
;
2859 /* check if AC is needed at all and adjust direction if needed */
2860 if(!a_avail
) dc_pred_dir
= 1;
2861 if(!c_avail
) dc_pred_dir
= 0;
2862 if(!a_avail
&& !c_avail
) use_pred
= 0;
2863 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2866 scale
= mquant
* 2 + v
->halfpq
;
2868 if(dc_pred_dir
) //left
2871 ac_val
-= 16 * s
->block_wrap
[n
];
2873 q1
= s
->current_picture
.qscale_table
[mb_pos
];
2874 if(dc_pred_dir
&& c_avail
&& mb_pos
) q2
= s
->current_picture
.qscale_table
[mb_pos
- 1];
2875 if(!dc_pred_dir
&& a_avail
&& mb_pos
>= s
->mb_stride
) q2
= s
->current_picture
.qscale_table
[mb_pos
- s
->mb_stride
];
2876 if(dc_pred_dir
&& n
==1) q2
= q1
;
2877 if(!dc_pred_dir
&& n
==2) q2
= q1
;
2881 int last
= 0, skip
, value
;
2882 const int8_t *zz_table
;
2885 zz_table
= wmv1_scantable
[0];
2888 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
2892 block
[zz_table
[i
++]] = value
;
2895 /* apply AC prediction if needed */
2897 /* scale predictors if needed*/
2899 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2900 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2902 if(dc_pred_dir
) { //left
2903 for(k
= 1; k
< 8; k
++)
2904 block
[k
<< 3] += (ac_val
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2906 for(k
= 1; k
< 8; k
++)
2907 block
[k
] += (ac_val
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2910 if(dc_pred_dir
) { //left
2911 for(k
= 1; k
< 8; k
++)
2912 block
[k
<< 3] += ac_val
[k
];
2914 for(k
= 1; k
< 8; k
++)
2915 block
[k
] += ac_val
[k
+ 8];
2919 /* save AC coeffs for further prediction */
2920 for(k
= 1; k
< 8; k
++) {
2921 ac_val2
[k
] = block
[k
<< 3];
2922 ac_val2
[k
+ 8] = block
[k
];
2925 /* scale AC coeffs */
2926 for(k
= 1; k
< 64; k
++)
2930 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
2933 if(use_pred
) i
= 63;
2934 } else { // no AC coeffs
2937 memset(ac_val2
, 0, 16 * 2);
2938 if(dc_pred_dir
) {//left
2940 memcpy(ac_val2
, ac_val
, 8 * 2);
2942 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2943 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2944 for(k
= 1; k
< 8; k
++)
2945 ac_val2
[k
] = (ac_val2
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2950 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
2952 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2953 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2954 for(k
= 1; k
< 8; k
++)
2955 ac_val2
[k
+ 8] = (ac_val2
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2960 /* apply AC prediction if needed */
2962 if(dc_pred_dir
) { //left
2963 for(k
= 1; k
< 8; k
++) {
2964 block
[k
<< 3] = ac_val2
[k
] * scale
;
2965 if(!v
->pquantizer
&& block
[k
<< 3])
2966 block
[k
<< 3] += (block
[k
<< 3] < 0) ? -mquant
: mquant
;
2969 for(k
= 1; k
< 8; k
++) {
2970 block
[k
] = ac_val2
[k
+ 8] * scale
;
2971 if(!v
->pquantizer
&& block
[k
])
2972 block
[k
] += (block
[k
] < 0) ? -mquant
: mquant
;
2978 s
->block_last_index
[n
] = i
;
2985 static int vc1_decode_p_block(VC1Context
*v
, DCTELEM block
[64], int n
, int mquant
, int ttmb
, int first_block
,
2986 uint8_t *dst
, int linesize
, int skip_block
, int apply_filter
, int cbp_top
, int cbp_left
)
2988 MpegEncContext
*s
= &v
->s
;
2989 GetBitContext
*gb
= &s
->gb
;
2992 int scale
, off
, idx
, last
, skip
, value
;
2993 int ttblk
= ttmb
& 7;
2997 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)];
2999 if(ttblk
== TT_4X4
) {
3000 subblkpat
= ~(get_vlc2(gb
, ff_vc1_subblkpat_vlc
[v
->tt_index
].table
, VC1_SUBBLKPAT_VLC_BITS
, 1) + 1);
3002 if((ttblk
!= TT_8X8
&& ttblk
!= TT_4X4
) && (v
->ttmbf
|| (ttmb
!= -1 && (ttmb
& 8) && !first_block
))) {
3003 subblkpat
= decode012(gb
);
3004 if(subblkpat
) subblkpat
^= 3; //swap decoded pattern bits
3005 if(ttblk
== TT_8X4_TOP
|| ttblk
== TT_8X4_BOTTOM
) ttblk
= TT_8X4
;
3006 if(ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
) ttblk
= TT_4X8
;
3008 scale
= 2 * mquant
+ ((v
->pq
== mquant
) ? v
->halfpq
: 0);
3010 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3011 if(ttblk
== TT_8X4_TOP
|| ttblk
== TT_8X4_BOTTOM
) {
3012 subblkpat
= 2 - (ttblk
== TT_8X4_TOP
);
3015 if(ttblk
== TT_4X8_RIGHT
|| ttblk
== TT_4X8_LEFT
) {
3016 subblkpat
= 2 - (ttblk
== TT_4X8_LEFT
);
3025 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
3029 idx
= wmv1_scantable
[0][i
++];
3030 block
[idx
] = value
* scale
;
3032 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
3035 s
->dsp
.vc1_inv_trans_8x8(block
);
3036 s
->dsp
.add_pixels_clamped(block
, dst
, linesize
);
3037 if(apply_filter
&& cbp_top
& 0xC)
3038 vc1_loop_filter(dst
, 1, linesize
, 8, mquant
);
3039 if(apply_filter
&& cbp_left
& 0xA)
3040 vc1_loop_filter(dst
, linesize
, 1, 8, mquant
);
3044 pat
= ~subblkpat
& 0xF;
3045 for(j
= 0; j
< 4; j
++) {
3046 last
= subblkpat
& (1 << (3 - j
));
3048 off
= (j
& 1) * 4 + (j
& 2) * 16;
3050 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
3054 idx
= ff_vc1_simple_progressive_4x4_zz
[i
++];
3055 block
[idx
+ off
] = value
* scale
;
3057 block
[idx
+ off
] += (block
[idx
+ off
] < 0) ? -mquant
: mquant
;
3059 if(!(subblkpat
& (1 << (3 - j
))) && !skip_block
){
3060 s
->dsp
.vc1_inv_trans_4x4(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, block
+ off
);
3061 if(apply_filter
&& (j
&2 ? pat
& (1<<(j
-2)) : (cbp_top
& (1 << (j
+ 2)))))
3062 vc1_loop_filter(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, 1, linesize
, 4, mquant
);
3063 if(apply_filter
&& (j
&1 ? pat
& (1<<(j
-1)) : (cbp_left
& (1 << (j
+ 1)))))
3064 vc1_loop_filter(dst
+ (j
&1)*4 + (j
&2)*2*linesize
, linesize
, 1, 4, mquant
);
3069 pat
= ~((subblkpat
& 2)*6 + (subblkpat
& 1)*3) & 0xF;
3070 for(j
= 0; j
< 2; j
++) {
3071 last
= subblkpat
& (1 << (1 - j
));
3075 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
3079 idx
= v
->zz_8x4
[i
++]+off
;
3080 block
[idx
] = value
* scale
;
3082 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
3084 if(!(subblkpat
& (1 << (1 - j
))) && !skip_block
){
3085 s
->dsp
.vc1_inv_trans_8x4(dst
+ j
*4*linesize
, linesize
, block
+ off
);
3086 if(apply_filter
&& j
? pat
& 0x3 : (cbp_top
& 0xC))
3087 vc1_loop_filter(dst
+ j
*4*linesize
, 1, linesize
, 8, mquant
);
3088 if(apply_filter
&& cbp_left
& (2 << j
))
3089 vc1_loop_filter(dst
+ j
*4*linesize
, linesize
, 1, 4, mquant
);
3094 pat
= ~(subblkpat
*5) & 0xF;
3095 for(j
= 0; j
< 2; j
++) {
3096 last
= subblkpat
& (1 << (1 - j
));
3100 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, v
->codingset2
);
3104 idx
= v
->zz_4x8
[i
++]+off
;
3105 block
[idx
] = value
* scale
;
3107 block
[idx
] += (block
[idx
] < 0) ? -mquant
: mquant
;
3109 if(!(subblkpat
& (1 << (1 - j
))) && !skip_block
){
3110 s
->dsp
.vc1_inv_trans_4x8(dst
+ j
*4, linesize
, block
+ off
);
3111 if(apply_filter
&& cbp_top
& (2 << j
))
3112 vc1_loop_filter(dst
+ j
*4, 1, linesize
, 4, mquant
);
3113 if(apply_filter
&& j
? pat
& 0x5 : (cbp_left
& 0xA))
3114 vc1_loop_filter(dst
+ j
*4, linesize
, 1, 8, mquant
);
3122 /** @} */ // Macroblock group
3124 static const int size_table
[6] = { 0, 2, 3, 4, 5, 8 };
3125 static const int offset_table
[6] = { 0, 1, 3, 7, 15, 31 };
3127 /** Decode one P-frame MB (in Simple/Main profile)
3129 static int vc1_decode_p_mb(VC1Context
*v
)
3131 MpegEncContext
*s
= &v
->s
;
3132 GetBitContext
*gb
= &s
->gb
;
3134 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
3135 int cbp
; /* cbp decoding stuff */
3136 int mqdiff
, mquant
; /* MB quantization */
3137 int ttmb
= v
->ttfrm
; /* MB Transform type */
3139 int mb_has_coeffs
= 1; /* last_flag */
3140 int dmv_x
, dmv_y
; /* Differential MV components */
3141 int index
, index1
; /* LUT indexes */
3142 int val
, sign
; /* temp values */
3143 int first_block
= 1;
3145 int skipped
, fourmv
;
3146 int block_cbp
= 0, pat
;
3147 int apply_loop_filter
;
3149 mquant
= v
->pq
; /* Loosy initialization */
3151 if (v
->mv_type_is_raw
)
3152 fourmv
= get_bits1(gb
);
3154 fourmv
= v
->mv_type_mb_plane
[mb_pos
];
3156 skipped
= get_bits1(gb
);
3158 skipped
= v
->s
.mbskip_table
[mb_pos
];
3160 s
->dsp
.clear_blocks(s
->block
[0]);
3162 apply_loop_filter
= s
->loop_filter
&& !(s
->avctx
->skip_loop_filter
>= AVDISCARD_NONKEY
);
3163 if (!fourmv
) /* 1MV mode */
3167 GET_MVDATA(dmv_x
, dmv_y
);
3170 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
3171 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
3173 s
->current_picture
.mb_type
[mb_pos
] = s
->mb_intra
? MB_TYPE_INTRA
: MB_TYPE_16x16
;
3174 vc1_pred_mv(s
, 0, dmv_x
, dmv_y
, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
3176 /* FIXME Set DC val for inter block ? */
3177 if (s
->mb_intra
&& !mb_has_coeffs
)
3180 s
->ac_pred
= get_bits1(gb
);
3183 else if (mb_has_coeffs
)
3185 if (s
->mb_intra
) s
->ac_pred
= get_bits1(gb
);
3186 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
3194 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3196 if (!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
3197 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
,
3198 VC1_TTMB_VLC_BITS
, 2);
3199 if(!s
->mb_intra
) vc1_mc_1mv(v
, 0);
3203 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3205 val
= ((cbp
>> (5 - i
)) & 1);
3206 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
3207 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
3209 /* check if prediction blocks A and C are available */
3210 v
->a_avail
= v
->c_avail
= 0;
3211 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
3212 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
3213 if(i
== 1 || i
== 3 || s
->mb_x
)
3214 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
3216 vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
3217 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
3218 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
3219 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
3220 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3221 if(v
->pq
>= 9 && v
->overlap
) {
3223 s
->dsp
.vc1_h_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3225 s
->dsp
.vc1_v_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3227 if(apply_loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
3228 int left_cbp
, top_cbp
;
3230 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
3231 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
3233 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
3234 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
3237 vc1_loop_filter(s
->dest
[dst_idx
] + off
, 1, i
& 4 ? s
->uvlinesize
: s
->linesize
, 8, mquant
);
3239 vc1_loop_filter(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, 1, 8, mquant
);
3241 block_cbp
|= 0xF << (i
<< 2);
3243 int left_cbp
= 0, top_cbp
= 0, filter
= 0;
3244 if(apply_loop_filter
&& s
->mb_x
&& s
->mb_x
!= (s
->mb_width
- 1) && s
->mb_y
&& s
->mb_y
!= (s
->mb_height
- 1)){
3247 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
3248 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
3250 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
3251 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
3254 vc1_loop_filter(s
->dest
[dst_idx
] + off
, 1, i
& 4 ? s
->uvlinesize
: s
->linesize
, 8, mquant
);
3256 vc1_loop_filter(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, 1, 8, mquant
);
3258 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
);
3259 block_cbp
|= pat
<< (i
<< 2);
3260 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
3268 for(i
= 0; i
< 6; i
++) {
3269 v
->mb_type
[0][s
->block_index
[i
]] = 0;
3270 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3272 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_SKIP
;
3273 s
->current_picture
.qscale_table
[mb_pos
] = 0;
3274 vc1_pred_mv(s
, 0, 0, 0, 1, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
3281 if (!skipped
/* unskipped MB */)
3283 int intra_count
= 0, coded_inter
= 0;
3284 int is_intra
[6], is_coded
[6];
3286 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
3289 val
= ((cbp
>> (5 - i
)) & 1);
3290 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3297 GET_MVDATA(dmv_x
, dmv_y
);
3299 vc1_pred_mv(s
, i
, dmv_x
, dmv_y
, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
3300 if(!s
->mb_intra
) vc1_mc_4mv_luma(v
, i
);
3301 intra_count
+= s
->mb_intra
;
3302 is_intra
[i
] = s
->mb_intra
;
3303 is_coded
[i
] = mb_has_coeffs
;
3306 is_intra
[i
] = (intra_count
>= 3);
3309 if(i
== 4) vc1_mc_4mv_chroma(v
);
3310 v
->mb_type
[0][s
->block_index
[i
]] = is_intra
[i
];
3311 if(!coded_inter
) coded_inter
= !is_intra
[i
] & is_coded
[i
];
3313 // if there are no coded blocks then don't do anything more
3314 if(!intra_count
&& !coded_inter
) return 0;
3317 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3318 /* test if block is intra and has pred */
3323 if(((!s
->first_slice_line
|| (i
==2 || i
==3)) && v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]])
3324 || ((s
->mb_x
|| (i
==1 || i
==3)) && v
->mb_type
[0][s
->block_index
[i
] - 1])) {
3329 if(intrapred
)s
->ac_pred
= get_bits1(gb
);
3330 else s
->ac_pred
= 0;
3332 if (!v
->ttmbf
&& coded_inter
)
3333 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
3337 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
3338 s
->mb_intra
= is_intra
[i
];
3340 /* check if prediction blocks A and C are available */
3341 v
->a_avail
= v
->c_avail
= 0;
3342 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
3343 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
3344 if(i
== 1 || i
== 3 || s
->mb_x
)
3345 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
3347 vc1_decode_intra_block(v
, s
->block
[i
], i
, is_coded
[i
], mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
3348 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
3349 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
3350 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
3351 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, (i
&4)?s
->uvlinesize
:s
->linesize
);
3352 if(v
->pq
>= 9 && v
->overlap
) {
3354 s
->dsp
.vc1_h_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3356 s
->dsp
.vc1_v_overlap(s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3358 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)){
3359 int left_cbp
, top_cbp
;
3361 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
3362 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
3364 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
3365 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
3368 vc1_loop_filter(s
->dest
[dst_idx
] + off
, 1, i
& 4 ? s
->uvlinesize
: s
->linesize
, 8, mquant
);
3370 vc1_loop_filter(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, 1, 8, mquant
);
3372 block_cbp
|= 0xF << (i
<< 2);
3373 } else if(is_coded
[i
]) {
3374 int left_cbp
= 0, top_cbp
= 0, filter
= 0;
3375 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)){
3378 left_cbp
= v
->cbp
[s
->mb_x
- 1] >> (i
* 4);
3379 top_cbp
= v
->cbp
[s
->mb_x
- s
->mb_stride
] >> (i
* 4);
3381 left_cbp
= (i
& 1) ? (cbp
>> ((i
-1)*4)) : (v
->cbp
[s
->mb_x
- 1] >> ((i
+1)*4));
3382 top_cbp
= (i
& 2) ? (cbp
>> ((i
-2)*4)) : (v
->cbp
[s
->mb_x
- s
->mb_stride
] >> ((i
+2)*4));
3385 vc1_loop_filter(s
->dest
[dst_idx
] + off
, 1, i
& 4 ? s
->uvlinesize
: s
->linesize
, 8, mquant
);
3387 vc1_loop_filter(s
->dest
[dst_idx
] + off
, i
& 4 ? s
->uvlinesize
: s
->linesize
, 1, 8, mquant
);
3389 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
);
3390 block_cbp
|= pat
<< (i
<< 2);
3391 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
3400 s
->current_picture
.qscale_table
[mb_pos
] = 0;
3401 for (i
=0; i
<6; i
++) {
3402 v
->mb_type
[0][s
->block_index
[i
]] = 0;
3403 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3407 vc1_pred_mv(s
, i
, 0, 0, 0, v
->range_x
, v
->range_y
, v
->mb_type
[0]);
3408 vc1_mc_4mv_luma(v
, i
);
3410 vc1_mc_4mv_chroma(v
);
3411 s
->current_picture
.qscale_table
[mb_pos
] = 0;
3415 v
->cbp
[s
->mb_x
] = block_cbp
;
3417 /* Should never happen */
3421 /** Decode one B-frame MB (in Main profile)
3423 static void vc1_decode_b_mb(VC1Context
*v
)
3425 MpegEncContext
*s
= &v
->s
;
3426 GetBitContext
*gb
= &s
->gb
;
3428 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
3429 int cbp
= 0; /* cbp decoding stuff */
3430 int mqdiff
, mquant
; /* MB quantization */
3431 int ttmb
= v
->ttfrm
; /* MB Transform type */
3432 int mb_has_coeffs
= 0; /* last_flag */
3433 int index
, index1
; /* LUT indexes */
3434 int val
, sign
; /* temp values */
3435 int first_block
= 1;
3437 int skipped
, direct
;
3438 int dmv_x
[2], dmv_y
[2];
3439 int bmvtype
= BMV_TYPE_BACKWARD
;
3441 mquant
= v
->pq
; /* Loosy initialization */
3445 direct
= get_bits1(gb
);
3447 direct
= v
->direct_mb_plane
[mb_pos
];
3449 skipped
= get_bits1(gb
);
3451 skipped
= v
->s
.mbskip_table
[mb_pos
];
3453 s
->dsp
.clear_blocks(s
->block
[0]);
3454 dmv_x
[0] = dmv_x
[1] = dmv_y
[0] = dmv_y
[1] = 0;
3455 for(i
= 0; i
< 6; i
++) {
3456 v
->mb_type
[0][s
->block_index
[i
]] = 0;
3457 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3459 s
->current_picture
.qscale_table
[mb_pos
] = 0;
3463 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
3464 dmv_x
[1] = dmv_x
[0];
3465 dmv_y
[1] = dmv_y
[0];
3467 if(skipped
|| !s
->mb_intra
) {
3468 bmvtype
= decode012(gb
);
3471 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_BACKWARD
: BMV_TYPE_FORWARD
;
3474 bmvtype
= (v
->bfraction
>= (B_FRACTION_DEN
/2)) ? BMV_TYPE_FORWARD
: BMV_TYPE_BACKWARD
;
3477 bmvtype
= BMV_TYPE_INTERPOLATED
;
3478 dmv_x
[0] = dmv_y
[0] = 0;
3482 for(i
= 0; i
< 6; i
++)
3483 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
3486 if(direct
) bmvtype
= BMV_TYPE_INTERPOLATED
;
3487 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3488 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3492 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
3496 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3498 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
3499 dmv_x
[0] = dmv_y
[0] = dmv_x
[1] = dmv_y
[1] = 0;
3500 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3501 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3503 if(!mb_has_coeffs
&& !s
->mb_intra
) {
3504 /* no coded blocks - effectively skipped */
3505 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3506 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3509 if(s
->mb_intra
&& !mb_has_coeffs
) {
3511 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3512 s
->ac_pred
= get_bits1(gb
);
3514 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3516 if(bmvtype
== BMV_TYPE_INTERPOLATED
) {
3517 GET_MVDATA(dmv_x
[0], dmv_y
[0]);
3518 if(!mb_has_coeffs
) {
3519 /* interpolated skipped block */
3520 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3521 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3525 vc1_pred_b_mv(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3527 vc1_b_mc(v
, dmv_x
, dmv_y
, direct
, bmvtype
);
3530 s
->ac_pred
= get_bits1(gb
);
3531 cbp
= get_vlc2(&v
->s
.gb
, v
->cbpcy_vlc
->table
, VC1_CBPCY_P_VLC_BITS
, 2);
3533 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3534 if(!v
->ttmbf
&& !s
->mb_intra
&& mb_has_coeffs
)
3535 ttmb
= get_vlc2(gb
, ff_vc1_ttmb_vlc
[v
->tt_index
].table
, VC1_TTMB_VLC_BITS
, 2);
3541 s
->dc_val
[0][s
->block_index
[i
]] = 0;
3543 val
= ((cbp
>> (5 - i
)) & 1);
3544 off
= (i
& 4) ? 0 : ((i
& 1) * 8 + (i
& 2) * 4 * s
->linesize
);
3545 v
->mb_type
[0][s
->block_index
[i
]] = s
->mb_intra
;
3547 /* check if prediction blocks A and C are available */
3548 v
->a_avail
= v
->c_avail
= 0;
3549 if(i
== 2 || i
== 3 || !s
->first_slice_line
)
3550 v
->a_avail
= v
->mb_type
[0][s
->block_index
[i
] - s
->block_wrap
[i
]];
3551 if(i
== 1 || i
== 3 || s
->mb_x
)
3552 v
->c_avail
= v
->mb_type
[0][s
->block_index
[i
] - 1];
3554 vc1_decode_intra_block(v
, s
->block
[i
], i
, val
, mquant
, (i
&4)?v
->codingset2
:v
->codingset
);
3555 if((i
>3) && (s
->flags
& CODEC_FLAG_GRAY
)) continue;
3556 s
->dsp
.vc1_inv_trans_8x8(s
->block
[i
]);
3557 if(v
->rangeredfrm
) for(j
= 0; j
< 64; j
++) s
->block
[i
][j
] <<= 1;
3558 s
->dsp
.put_signed_pixels_clamped(s
->block
[i
], s
->dest
[dst_idx
] + off
, s
->linesize
>> ((i
& 4) >> 2));
3560 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);
3561 if(!v
->ttmbf
&& ttmb
< 8) ttmb
= -1;
3567 /** Decode blocks of I-frame
3569 static void vc1_decode_i_blocks(VC1Context
*v
)
3572 MpegEncContext
*s
= &v
->s
;
3577 /* select codingmode used for VLC tables selection */
3578 switch(v
->y_ac_table_index
){
3580 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
3583 v
->codingset
= CS_HIGH_MOT_INTRA
;
3586 v
->codingset
= CS_MID_RATE_INTRA
;
3590 switch(v
->c_ac_table_index
){
3592 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
3595 v
->codingset2
= CS_HIGH_MOT_INTER
;
3598 v
->codingset2
= CS_MID_RATE_INTER
;
3602 /* Set DC scale - y and c use the same */
3603 s
->y_dc_scale
= s
->y_dc_scale_table
[v
->pq
];
3604 s
->c_dc_scale
= s
->c_dc_scale_table
[v
->pq
];
3607 s
->mb_x
= s
->mb_y
= 0;
3609 s
->first_slice_line
= 1;
3610 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
3611 for(s
->mb_x
= 0; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
3612 ff_init_block_index(s
);
3613 ff_update_block_index(s
);
3614 s
->dsp
.clear_blocks(s
->block
[0]);
3615 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_width
;
3616 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
3617 s
->current_picture
.qscale_table
[mb_pos
] = v
->pq
;
3618 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
3619 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
3621 // do actual MB decoding and displaying
3622 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
3623 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
3625 for(k
= 0; k
< 6; k
++) {
3626 val
= ((cbp
>> (5 - k
)) & 1);
3629 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
3633 cbp
|= val
<< (5 - k
);
3635 vc1_decode_i_block(v
, s
->block
[k
], k
, val
, (k
<4)? v
->codingset
: v
->codingset2
);
3637 s
->dsp
.vc1_inv_trans_8x8(s
->block
[k
]);
3638 if(v
->pq
>= 9 && v
->overlap
) {
3639 for(j
= 0; j
< 64; j
++) s
->block
[k
][j
] += 128;
3643 vc1_put_block(v
, s
->block
);
3644 if(v
->pq
>= 9 && v
->overlap
) {
3646 s
->dsp
.vc1_h_overlap(s
->dest
[0], s
->linesize
);
3647 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
3648 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
3649 s
->dsp
.vc1_h_overlap(s
->dest
[1], s
->uvlinesize
);
3650 s
->dsp
.vc1_h_overlap(s
->dest
[2], s
->uvlinesize
);
3653 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8, s
->linesize
);
3654 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
3655 if(!s
->first_slice_line
) {
3656 s
->dsp
.vc1_v_overlap(s
->dest
[0], s
->linesize
);
3657 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8, s
->linesize
);
3658 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
3659 s
->dsp
.vc1_v_overlap(s
->dest
[1], s
->uvlinesize
);
3660 s
->dsp
.vc1_v_overlap(s
->dest
[2], s
->uvlinesize
);
3663 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
3664 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
3666 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, s
->current_picture
.qscale_table
[mb_pos
]);
3668 if(get_bits_count(&s
->gb
) > v
->bits
) {
3669 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
3670 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n", get_bits_count(&s
->gb
), v
->bits
);
3674 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3675 s
->first_slice_line
= 0;
3677 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
3680 /** Decode blocks of I-frame for advanced profile
3682 static void vc1_decode_i_blocks_adv(VC1Context
*v
)
3685 MpegEncContext
*s
= &v
->s
;
3692 GetBitContext
*gb
= &s
->gb
;
3694 /* select codingmode used for VLC tables selection */
3695 switch(v
->y_ac_table_index
){
3697 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
3700 v
->codingset
= CS_HIGH_MOT_INTRA
;
3703 v
->codingset
= CS_MID_RATE_INTRA
;
3707 switch(v
->c_ac_table_index
){
3709 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
3712 v
->codingset2
= CS_HIGH_MOT_INTER
;
3715 v
->codingset2
= CS_MID_RATE_INTER
;
3720 s
->mb_x
= s
->mb_y
= 0;
3722 s
->first_slice_line
= 1;
3723 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
3724 for(s
->mb_x
= 0; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
3725 ff_init_block_index(s
);
3726 ff_update_block_index(s
);
3727 s
->dsp
.clear_blocks(s
->block
[0]);
3728 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
3729 s
->current_picture
.mb_type
[mb_pos
] = MB_TYPE_INTRA
;
3730 s
->current_picture
.motion_val
[1][s
->block_index
[0]][0] = 0;
3731 s
->current_picture
.motion_val
[1][s
->block_index
[0]][1] = 0;
3733 // do actual MB decoding and displaying
3734 cbp
= get_vlc2(&v
->s
.gb
, ff_msmp4_mb_i_vlc
.table
, MB_INTRA_VLC_BITS
, 2);
3735 if(v
->acpred_is_raw
)
3736 v
->s
.ac_pred
= get_bits1(&v
->s
.gb
);
3738 v
->s
.ac_pred
= v
->acpred_plane
[mb_pos
];
3740 if(v
->condover
== CONDOVER_SELECT
) {
3741 if(v
->overflg_is_raw
)
3742 overlap
= get_bits1(&v
->s
.gb
);
3744 overlap
= v
->over_flags_plane
[mb_pos
];
3746 overlap
= (v
->condover
== CONDOVER_ALL
);
3750 s
->current_picture
.qscale_table
[mb_pos
] = mquant
;
3751 /* Set DC scale - y and c use the same */
3752 s
->y_dc_scale
= s
->y_dc_scale_table
[mquant
];
3753 s
->c_dc_scale
= s
->c_dc_scale_table
[mquant
];
3755 for(k
= 0; k
< 6; k
++) {
3756 val
= ((cbp
>> (5 - k
)) & 1);
3759 int pred
= vc1_coded_block_pred(&v
->s
, k
, &coded_val
);
3763 cbp
|= val
<< (5 - k
);
3765 v
->a_avail
= !s
->first_slice_line
|| (k
==2 || k
==3);
3766 v
->c_avail
= !!s
->mb_x
|| (k
==1 || k
==3);
3768 vc1_decode_i_block_adv(v
, s
->block
[k
], k
, val
, (k
<4)? v
->codingset
: v
->codingset2
, mquant
);
3770 s
->dsp
.vc1_inv_trans_8x8(s
->block
[k
]);
3771 for(j
= 0; j
< 64; j
++) s
->block
[k
][j
] += 128;
3774 vc1_put_block(v
, s
->block
);
3777 s
->dsp
.vc1_h_overlap(s
->dest
[0], s
->linesize
);
3778 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
3779 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
3780 s
->dsp
.vc1_h_overlap(s
->dest
[1], s
->uvlinesize
);
3781 s
->dsp
.vc1_h_overlap(s
->dest
[2], s
->uvlinesize
);
3784 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8, s
->linesize
);
3785 s
->dsp
.vc1_h_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
3786 if(!s
->first_slice_line
) {
3787 s
->dsp
.vc1_v_overlap(s
->dest
[0], s
->linesize
);
3788 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8, s
->linesize
);
3789 if(!(s
->flags
& CODEC_FLAG_GRAY
)) {
3790 s
->dsp
.vc1_v_overlap(s
->dest
[1], s
->uvlinesize
);
3791 s
->dsp
.vc1_v_overlap(s
->dest
[2], s
->uvlinesize
);
3794 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
);
3795 s
->dsp
.vc1_v_overlap(s
->dest
[0] + 8 * s
->linesize
+ 8, s
->linesize
);
3797 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, s
->current_picture
.qscale_table
[mb_pos
]);
3799 if(get_bits_count(&s
->gb
) > v
->bits
) {
3800 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
3801 av_log(s
->avctx
, AV_LOG_ERROR
, "Bits overconsumption: %i > %i\n", get_bits_count(&s
->gb
), v
->bits
);
3805 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3806 s
->first_slice_line
= 0;
3808 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
3811 static void vc1_decode_p_blocks(VC1Context
*v
)
3813 MpegEncContext
*s
= &v
->s
;
3815 /* select codingmode used for VLC tables selection */
3816 switch(v
->c_ac_table_index
){
3818 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
3821 v
->codingset
= CS_HIGH_MOT_INTRA
;
3824 v
->codingset
= CS_MID_RATE_INTRA
;
3828 switch(v
->c_ac_table_index
){
3830 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
3833 v
->codingset2
= CS_HIGH_MOT_INTER
;
3836 v
->codingset2
= CS_MID_RATE_INTER
;
3840 s
->first_slice_line
= 1;
3841 memset(v
->cbp_base
, 0, sizeof(v
->cbp_base
[0])*2*s
->mb_stride
);
3842 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
3843 for(s
->mb_x
= 0; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
3844 ff_init_block_index(s
);
3845 ff_update_block_index(s
);
3846 s
->dsp
.clear_blocks(s
->block
[0]);
3849 if(get_bits_count(&s
->gb
) > v
->bits
|| get_bits_count(&s
->gb
) < 0) {
3850 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
3851 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
);
3855 memmove(v
->cbp_base
, v
->cbp
, sizeof(v
->cbp_base
[0])*s
->mb_stride
);
3856 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3857 s
->first_slice_line
= 0;
3859 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
3862 static void vc1_decode_b_blocks(VC1Context
*v
)
3864 MpegEncContext
*s
= &v
->s
;
3866 /* select codingmode used for VLC tables selection */
3867 switch(v
->c_ac_table_index
){
3869 v
->codingset
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTRA
: CS_LOW_MOT_INTRA
;
3872 v
->codingset
= CS_HIGH_MOT_INTRA
;
3875 v
->codingset
= CS_MID_RATE_INTRA
;
3879 switch(v
->c_ac_table_index
){
3881 v
->codingset2
= (v
->pqindex
<= 8) ? CS_HIGH_RATE_INTER
: CS_LOW_MOT_INTER
;
3884 v
->codingset2
= CS_HIGH_MOT_INTER
;
3887 v
->codingset2
= CS_MID_RATE_INTER
;
3891 s
->first_slice_line
= 1;
3892 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
3893 for(s
->mb_x
= 0; s
->mb_x
< s
->mb_width
; s
->mb_x
++) {
3894 ff_init_block_index(s
);
3895 ff_update_block_index(s
);
3896 s
->dsp
.clear_blocks(s
->block
[0]);
3899 if(get_bits_count(&s
->gb
) > v
->bits
|| get_bits_count(&s
->gb
) < 0) {
3900 ff_er_add_slice(s
, 0, 0, s
->mb_x
, s
->mb_y
, (AC_END
|DC_END
|MV_END
));
3901 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
);
3904 if(v
->s
.loop_filter
) vc1_loop_filter_iblk(s
, s
->current_picture
.qscale_table
[s
->mb_x
+ s
->mb_y
*s
->mb_stride
]);
3906 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3907 s
->first_slice_line
= 0;
3909 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
3912 static void vc1_decode_skip_blocks(VC1Context
*v
)
3914 MpegEncContext
*s
= &v
->s
;
3916 ff_er_add_slice(s
, 0, 0, s
->mb_width
- 1, s
->mb_height
- 1, (AC_END
|DC_END
|MV_END
));
3917 s
->first_slice_line
= 1;
3918 for(s
->mb_y
= 0; s
->mb_y
< s
->mb_height
; s
->mb_y
++) {
3920 ff_init_block_index(s
);
3921 ff_update_block_index(s
);
3922 memcpy(s
->dest
[0], s
->last_picture
.data
[0] + s
->mb_y
* 16 * s
->linesize
, s
->linesize
* 16);
3923 memcpy(s
->dest
[1], s
->last_picture
.data
[1] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
3924 memcpy(s
->dest
[2], s
->last_picture
.data
[2] + s
->mb_y
* 8 * s
->uvlinesize
, s
->uvlinesize
* 8);
3925 ff_draw_horiz_band(s
, s
->mb_y
* 16, 16);
3926 s
->first_slice_line
= 0;
3928 s
->pict_type
= FF_P_TYPE
;
3931 static void vc1_decode_blocks(VC1Context
*v
)
3934 v
->s
.esc3_level_length
= 0;
3936 ff_intrax8_decode_picture(&v
->x8
, 2*v
->pq
+v
->halfpq
, v
->pq
*(!v
->pquantizer
) );
3939 switch(v
->s
.pict_type
) {
3941 if(v
->profile
== PROFILE_ADVANCED
)
3942 vc1_decode_i_blocks_adv(v
);
3944 vc1_decode_i_blocks(v
);
3947 if(v
->p_frame_skipped
)
3948 vc1_decode_skip_blocks(v
);
3950 vc1_decode_p_blocks(v
);
3954 if(v
->profile
== PROFILE_ADVANCED
)
3955 vc1_decode_i_blocks_adv(v
);
3957 vc1_decode_i_blocks(v
);
3959 vc1_decode_b_blocks(v
);
3965 /** Find VC-1 marker in buffer
3966 * @return position where next marker starts or end of buffer if no marker found
3968 static av_always_inline
const uint8_t* find_next_marker(const uint8_t *src
, const uint8_t *end
)
3970 uint32_t mrk
= 0xFFFFFFFF;
3972 if(end
-src
< 4) return end
;
3974 mrk
= (mrk
<< 8) | *src
++;
3981 static av_always_inline
int vc1_unescape_buffer(const uint8_t *src
, int size
, uint8_t *dst
)
3986 for(dsize
= 0; dsize
< size
; dsize
++) *dst
++ = *src
++;
3989 for(i
= 0; i
< size
; i
++, src
++) {
3990 if(src
[0] == 3 && i
>= 2 && !src
[-1] && !src
[-2] && i
< size
-1 && src
[1] < 4) {
3991 dst
[dsize
++] = src
[1];
3995 dst
[dsize
++] = *src
;
4000 /** Initialize a VC1/WMV3 decoder
4001 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4002 * @todo TODO: Decypher remaining bits in extra_data
4004 static av_cold
int vc1_decode_init(AVCodecContext
*avctx
)
4006 VC1Context
*v
= avctx
->priv_data
;
4007 MpegEncContext
*s
= &v
->s
;
4010 if (!avctx
->extradata_size
|| !avctx
->extradata
) return -1;
4011 if (!(avctx
->flags
& CODEC_FLAG_GRAY
))
4012 avctx
->pix_fmt
= PIX_FMT_YUV420P
;
4014 avctx
->pix_fmt
= PIX_FMT_GRAY8
;
4016 avctx
->flags
|= CODEC_FLAG_EMU_EDGE
;
4017 v
->s
.flags
|= CODEC_FLAG_EMU_EDGE
;
4019 if(avctx
->idct_algo
==FF_IDCT_AUTO
){
4020 avctx
->idct_algo
=FF_IDCT_WMV2
;
4023 if(ff_h263_decode_init(avctx
) < 0)
4025 if (vc1_init_common(v
) < 0) return -1;
4027 avctx
->coded_width
= avctx
->width
;
4028 avctx
->coded_height
= avctx
->height
;
4029 if (avctx
->codec_id
== CODEC_ID_WMV3
)
4033 // looks like WMV3 has a sequence header stored in the extradata
4034 // advanced sequence header may be before the first frame
4035 // the last byte of the extradata is a version number, 1 for the
4036 // samples we can decode
4038 init_get_bits(&gb
, avctx
->extradata
, avctx
->extradata_size
*8);
4040 if (decode_sequence_header(avctx
, &gb
) < 0)
4043 count
= avctx
->extradata_size
*8 - get_bits_count(&gb
);
4046 av_log(avctx
, AV_LOG_INFO
, "Extra data: %i bits left, value: %X\n",
4047 count
, get_bits(&gb
, count
));
4051 av_log(avctx
, AV_LOG_INFO
, "Read %i bits in overflow\n", -count
);
4053 } else { // VC1/WVC1
4054 const uint8_t *start
= avctx
->extradata
;
4055 uint8_t *end
= avctx
->extradata
+ avctx
->extradata_size
;
4056 const uint8_t *next
;
4057 int size
, buf2_size
;
4058 uint8_t *buf2
= NULL
;
4059 int seq_initialized
= 0, ep_initialized
= 0;
4061 if(avctx
->extradata_size
< 16) {
4062 av_log(avctx
, AV_LOG_ERROR
, "Extradata size too small: %i\n", avctx
->extradata_size
);
4066 buf2
= av_mallocz(avctx
->extradata_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
4067 if(start
[0]) start
++; // in WVC1 extradata first byte is its size
4069 for(; next
< end
; start
= next
){
4070 next
= find_next_marker(start
+ 4, end
);
4071 size
= next
- start
- 4;
4072 if(size
<= 0) continue;
4073 buf2_size
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
4074 init_get_bits(&gb
, buf2
, buf2_size
* 8);
4075 switch(AV_RB32(start
)){
4076 case VC1_CODE_SEQHDR
:
4077 if(decode_sequence_header(avctx
, &gb
) < 0){
4081 seq_initialized
= 1;
4083 case VC1_CODE_ENTRYPOINT
:
4084 if(decode_entry_point(avctx
, &gb
) < 0){
4093 if(!seq_initialized
|| !ep_initialized
){
4094 av_log(avctx
, AV_LOG_ERROR
, "Incomplete extradata\n");
4098 avctx
->has_b_frames
= !!(avctx
->max_b_frames
);
4099 s
->low_delay
= !avctx
->has_b_frames
;
4101 s
->mb_width
= (avctx
->coded_width
+15)>>4;
4102 s
->mb_height
= (avctx
->coded_height
+15)>>4;
4104 /* Allocate mb bitplanes */
4105 v
->mv_type_mb_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
4106 v
->direct_mb_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
4107 v
->acpred_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
4108 v
->over_flags_plane
= av_malloc(s
->mb_stride
* s
->mb_height
);
4110 v
->cbp_base
= av_malloc(sizeof(v
->cbp_base
[0]) * 2 * s
->mb_stride
);
4111 v
->cbp
= v
->cbp_base
+ s
->mb_stride
;
4113 /* allocate block type info in that way so it could be used with s->block_index[] */
4114 v
->mb_type_base
= av_malloc(s
->b8_stride
* (s
->mb_height
* 2 + 1) + s
->mb_stride
* (s
->mb_height
+ 1) * 2);
4115 v
->mb_type
[0] = v
->mb_type_base
+ s
->b8_stride
+ 1;
4116 v
->mb_type
[1] = v
->mb_type_base
+ s
->b8_stride
* (s
->mb_height
* 2 + 1) + s
->mb_stride
+ 1;
4117 v
->mb_type
[2] = v
->mb_type
[1] + s
->mb_stride
* (s
->mb_height
+ 1);
4119 /* Init coded blocks info */
4120 if (v
->profile
== PROFILE_ADVANCED
)
4122 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
4124 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
4128 ff_intrax8_common_init(&v
->x8
,s
);
4133 /** Decode a VC1/WMV3 frame
4134 * @todo TODO: Handle VC-1 IDUs (Transport level?)
4136 static int vc1_decode_frame(AVCodecContext
*avctx
,
4137 void *data
, int *data_size
,
4138 const uint8_t *buf
, int buf_size
)
4140 VC1Context
*v
= avctx
->priv_data
;
4141 MpegEncContext
*s
= &v
->s
;
4142 AVFrame
*pict
= data
;
4143 uint8_t *buf2
= NULL
;
4144 const uint8_t *buf_vdpau
= buf
;
4146 /* no supplementary picture */
4147 if (buf_size
== 0) {
4148 /* special case for last picture */
4149 if (s
->low_delay
==0 && s
->next_picture_ptr
) {
4150 *pict
= *(AVFrame
*)s
->next_picture_ptr
;
4151 s
->next_picture_ptr
= NULL
;
4153 *data_size
= sizeof(AVFrame
);
4159 /* We need to set current_picture_ptr before reading the header,
4160 * otherwise we cannot store anything in there. */
4161 if(s
->current_picture_ptr
==NULL
|| s
->current_picture_ptr
->data
[0]){
4162 int i
= ff_find_unused_picture(s
, 0);
4163 s
->current_picture_ptr
= &s
->picture
[i
];
4166 if (s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
){
4167 if (v
->profile
< PROFILE_ADVANCED
)
4168 avctx
->pix_fmt
= PIX_FMT_VDPAU_WMV3
;
4170 avctx
->pix_fmt
= PIX_FMT_VDPAU_VC1
;
4173 //for advanced profile we may need to parse and unescape data
4174 if (avctx
->codec_id
== CODEC_ID_VC1
) {
4176 buf2
= av_mallocz(buf_size
+ FF_INPUT_BUFFER_PADDING_SIZE
);
4178 if(IS_MARKER(AV_RB32(buf
))){ /* frame starts with marker and needs to be parsed */
4179 const uint8_t *start
, *end
, *next
;
4183 for(start
= buf
, end
= buf
+ buf_size
; next
< end
; start
= next
){
4184 next
= find_next_marker(start
+ 4, end
);
4185 size
= next
- start
- 4;
4186 if(size
<= 0) continue;
4187 switch(AV_RB32(start
)){
4188 case VC1_CODE_FRAME
:
4189 if (s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
)
4191 buf_size2
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
4193 case VC1_CODE_ENTRYPOINT
: /* it should be before frame data */
4194 buf_size2
= vc1_unescape_buffer(start
+ 4, size
, buf2
);
4195 init_get_bits(&s
->gb
, buf2
, buf_size2
*8);
4196 decode_entry_point(avctx
, &s
->gb
);
4198 case VC1_CODE_SLICE
:
4199 av_log(avctx
, AV_LOG_ERROR
, "Sliced decoding is not implemented (yet)\n");
4204 }else if(v
->interlace
&& ((buf
[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
4205 const uint8_t *divider
;
4207 divider
= find_next_marker(buf
, buf
+ buf_size
);
4208 if((divider
== (buf
+ buf_size
)) || AV_RB32(divider
) != VC1_CODE_FIELD
){
4209 av_log(avctx
, AV_LOG_ERROR
, "Error in WVC1 interlaced frame\n");
4214 buf_size2
= vc1_unescape_buffer(buf
, divider
- buf
, buf2
);
4216 av_free(buf2
);return -1;
4218 buf_size2
= vc1_unescape_buffer(buf
, buf_size
, buf2
);
4220 init_get_bits(&s
->gb
, buf2
, buf_size2
*8);
4222 init_get_bits(&s
->gb
, buf
, buf_size
*8);
4223 // do parse frame header
4224 if(v
->profile
< PROFILE_ADVANCED
) {
4225 if(vc1_parse_frame_header(v
, &s
->gb
) == -1) {
4230 if(vc1_parse_frame_header_adv(v
, &s
->gb
) == -1) {
4236 if(s
->pict_type
!= FF_I_TYPE
&& !v
->res_rtm_flag
){
4242 s
->current_picture
.pict_type
= s
->pict_type
;
4243 s
->current_picture
.key_frame
= s
->pict_type
== FF_I_TYPE
;
4245 /* skip B-frames if we don't have reference frames */
4246 if(s
->last_picture_ptr
==NULL
&& (s
->pict_type
==FF_B_TYPE
|| s
->dropable
)){
4248 return -1;//buf_size;
4250 /* skip b frames if we are in a hurry */
4251 if(avctx
->hurry_up
&& s
->pict_type
==FF_B_TYPE
) return -1;//buf_size;
4252 if( (avctx
->skip_frame
>= AVDISCARD_NONREF
&& s
->pict_type
==FF_B_TYPE
)
4253 || (avctx
->skip_frame
>= AVDISCARD_NONKEY
&& s
->pict_type
!=FF_I_TYPE
)
4254 || avctx
->skip_frame
>= AVDISCARD_ALL
) {
4258 /* skip everything if we are in a hurry>=5 */
4259 if(avctx
->hurry_up
>=5) {
4261 return -1;//buf_size;
4264 if(s
->next_p_frame_damaged
){
4265 if(s
->pict_type
==FF_B_TYPE
)
4268 s
->next_p_frame_damaged
=0;
4271 if(MPV_frame_start(s
, avctx
) < 0) {
4276 s
->me
.qpel_put
= s
->dsp
.put_qpel_pixels_tab
;
4277 s
->me
.qpel_avg
= s
->dsp
.avg_qpel_pixels_tab
;
4279 if ((CONFIG_VC1_VDPAU_DECODER
|| CONFIG_WMV3_VDPAU_DECODER
)
4280 &&s
->avctx
->codec
->capabilities
&CODEC_CAP_HWACCEL_VDPAU
)
4281 ff_vdpau_vc1_decode_picture(s
, buf_vdpau
, (buf
+ buf_size
) - buf_vdpau
);
4283 ff_er_frame_start(s
);
4285 v
->bits
= buf_size
* 8;
4286 vc1_decode_blocks(v
);
4287 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
4288 // if(get_bits_count(&s->gb) > buf_size * 8)
4295 assert(s
->current_picture
.pict_type
== s
->current_picture_ptr
->pict_type
);
4296 assert(s
->current_picture
.pict_type
== s
->pict_type
);
4297 if (s
->pict_type
== FF_B_TYPE
|| s
->low_delay
) {
4298 *pict
= *(AVFrame
*)s
->current_picture_ptr
;
4299 } else if (s
->last_picture_ptr
!= NULL
) {
4300 *pict
= *(AVFrame
*)s
->last_picture_ptr
;
4303 if(s
->last_picture_ptr
|| s
->low_delay
){
4304 *data_size
= sizeof(AVFrame
);
4305 ff_print_debug_info(s
, pict
);
4308 /* Return the Picture timestamp as the frame number */
4309 /* we subtract 1 because it is added on utils.c */
4310 avctx
->frame_number
= s
->picture_number
- 1;
4317 /** Close a VC1/WMV3 decoder
4318 * @warning Initial try at using MpegEncContext stuff
4320 static av_cold
int vc1_decode_end(AVCodecContext
*avctx
)
4322 VC1Context
*v
= avctx
->priv_data
;
4324 av_freep(&v
->hrd_rate
);
4325 av_freep(&v
->hrd_buffer
);
4326 MPV_common_end(&v
->s
);
4327 av_freep(&v
->mv_type_mb_plane
);
4328 av_freep(&v
->direct_mb_plane
);
4329 av_freep(&v
->acpred_plane
);
4330 av_freep(&v
->over_flags_plane
);
4331 av_freep(&v
->mb_type_base
);
4332 av_freep(&v
->cbp_base
);
4333 ff_intrax8_common_end(&v
->x8
);
4338 AVCodec vc1_decoder
= {
4349 .long_name
= NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
4352 AVCodec wmv3_decoder
= {
4363 .long_name
= NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
4366 #if CONFIG_WMV3_VDPAU_DECODER
4367 AVCodec wmv3_vdpau_decoder
= {
4376 CODEC_CAP_DR1
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
4378 .long_name
= NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
4382 #if CONFIG_VC1_VDPAU_DECODER
4383 AVCodec vc1_vdpau_decoder
= {
4392 CODEC_CAP_DR1
| CODEC_CAP_DELAY
| CODEC_CAP_HWACCEL_VDPAU
,
4394 .long_name
= NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),