2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of Libav.
9 * Libav is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * Libav is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with Libav; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
32 #include "mpegvideo.h"
36 #include "vc1acdata.h"
37 #include "msmpeg4data.h"
39 #include "simple_idct.h"
41 #include "vdpau_internal.h"
46 #define MB_INTRA_VLC_BITS 9
51 static const uint16_t vlc_offs
[] = {
52 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
53 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8342,
54 9304, 9988, 10630, 11234, 12174, 13006, 13560, 14232, 14786, 15432, 16350, 17522,
55 20372, 21818, 22330, 22394, 23166, 23678, 23742, 24820, 25332, 25396, 26460, 26980,
56 27048, 27592, 27600, 27608, 27616, 27624, 28224, 28258, 28290, 28802, 28834, 28866,
57 29378, 29412, 29444, 29960, 29994, 30026, 30538, 30572, 30604, 31120, 31154, 31186,
58 31714, 31746, 31778, 32306, 32340, 32372
61 // offset tables for interlaced picture MVDATA decoding
62 static const int offset_table1
[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
63 static const int offset_table2
[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
66 * Init VC-1 specific tables and VC1Context members
67 * @param v The VC1Context to initialize
70 static int vc1_init_common(VC1Context
*v
)
74 static VLC_TYPE vlc_table
[32372][2];
76 v
->hrd_rate
= v
->hrd_buffer
= NULL
;
80 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc
, VC1_BFRACTION_VLC_BITS
, 23,
81 ff_vc1_bfraction_bits
, 1, 1,
82 ff_vc1_bfraction_codes
, 1, 1, 1 << VC1_BFRACTION_VLC_BITS
);
83 INIT_VLC_STATIC(&ff_vc1_norm2_vlc
, VC1_NORM2_VLC_BITS
, 4,
84 ff_vc1_norm2_bits
, 1, 1,
85 ff_vc1_norm2_codes
, 1, 1, 1 << VC1_NORM2_VLC_BITS
);
86 INIT_VLC_STATIC(&ff_vc1_norm6_vlc
, VC1_NORM6_VLC_BITS
, 64,
87 ff_vc1_norm6_bits
, 1, 1,
88 ff_vc1_norm6_codes
, 2, 2, 556);
89 INIT_VLC_STATIC(&ff_vc1_imode_vlc
, VC1_IMODE_VLC_BITS
, 7,
90 ff_vc1_imode_bits
, 1, 1,
91 ff_vc1_imode_codes
, 1, 1, 1 << VC1_IMODE_VLC_BITS
);
92 for (i
= 0; i
< 3; i
++) {
93 ff_vc1_ttmb_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 0]];
94 ff_vc1_ttmb_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 1] - vlc_offs
[i
* 3 + 0];
95 init_vlc(&ff_vc1_ttmb_vlc
[i
], VC1_TTMB_VLC_BITS
, 16,
96 ff_vc1_ttmb_bits
[i
], 1, 1,
97 ff_vc1_ttmb_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
98 ff_vc1_ttblk_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 1]];
99 ff_vc1_ttblk_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 2] - vlc_offs
[i
* 3 + 1];
100 init_vlc(&ff_vc1_ttblk_vlc
[i
], VC1_TTBLK_VLC_BITS
, 8,
101 ff_vc1_ttblk_bits
[i
], 1, 1,
102 ff_vc1_ttblk_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
103 ff_vc1_subblkpat_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 2]];
104 ff_vc1_subblkpat_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 3] - vlc_offs
[i
* 3 + 2];
105 init_vlc(&ff_vc1_subblkpat_vlc
[i
], VC1_SUBBLKPAT_VLC_BITS
, 15,
106 ff_vc1_subblkpat_bits
[i
], 1, 1,
107 ff_vc1_subblkpat_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
109 for (i
= 0; i
< 4; i
++) {
110 ff_vc1_4mv_block_pattern_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 9]];
111 ff_vc1_4mv_block_pattern_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 10] - vlc_offs
[i
* 3 + 9];
112 init_vlc(&ff_vc1_4mv_block_pattern_vlc
[i
], VC1_4MV_BLOCK_PATTERN_VLC_BITS
, 16,
113 ff_vc1_4mv_block_pattern_bits
[i
], 1, 1,
114 ff_vc1_4mv_block_pattern_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
115 ff_vc1_cbpcy_p_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 10]];
116 ff_vc1_cbpcy_p_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 11] - vlc_offs
[i
* 3 + 10];
117 init_vlc(&ff_vc1_cbpcy_p_vlc
[i
], VC1_CBPCY_P_VLC_BITS
, 64,
118 ff_vc1_cbpcy_p_bits
[i
], 1, 1,
119 ff_vc1_cbpcy_p_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
120 ff_vc1_mv_diff_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 11]];
121 ff_vc1_mv_diff_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 12] - vlc_offs
[i
* 3 + 11];
122 init_vlc(&ff_vc1_mv_diff_vlc
[i
], VC1_MV_DIFF_VLC_BITS
, 73,
123 ff_vc1_mv_diff_bits
[i
], 1, 1,
124 ff_vc1_mv_diff_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
126 for (i
= 0; i
< 8; i
++) {
127 ff_vc1_ac_coeff_table
[i
].table
= &vlc_table
[vlc_offs
[i
* 2 + 21]];
128 ff_vc1_ac_coeff_table
[i
].table_allocated
= vlc_offs
[i
* 2 + 22] - vlc_offs
[i
* 2 + 21];
129 init_vlc(&ff_vc1_ac_coeff_table
[i
], AC_VLC_BITS
, vc1_ac_sizes
[i
],
130 &vc1_ac_tables
[i
][0][1], 8, 4,
131 &vc1_ac_tables
[i
][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC
);
132 /* initialize interlaced MVDATA tables (2-Ref) */
133 ff_vc1_2ref_mvdata_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 2 + 22]];
134 ff_vc1_2ref_mvdata_vlc
[i
].table_allocated
= vlc_offs
[i
* 2 + 23] - vlc_offs
[i
* 2 + 22];
135 init_vlc(&ff_vc1_2ref_mvdata_vlc
[i
], VC1_2REF_MVDATA_VLC_BITS
, 126,
136 ff_vc1_2ref_mvdata_bits
[i
], 1, 1,
137 ff_vc1_2ref_mvdata_codes
[i
], 4, 4, INIT_VLC_USE_NEW_STATIC
);
139 for (i
= 0; i
< 4; i
++) {
140 /* initialize 4MV MBMODE VLC tables for interlaced frame P picture */
141 ff_vc1_intfr_4mv_mbmode_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 37]];
142 ff_vc1_intfr_4mv_mbmode_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 38] - vlc_offs
[i
* 3 + 37];
143 init_vlc(&ff_vc1_intfr_4mv_mbmode_vlc
[i
], VC1_INTFR_4MV_MBMODE_VLC_BITS
, 15,
144 ff_vc1_intfr_4mv_mbmode_bits
[i
], 1, 1,
145 ff_vc1_intfr_4mv_mbmode_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
146 /* initialize NON-4MV MBMODE VLC tables for the same */
147 ff_vc1_intfr_non4mv_mbmode_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 38]];
148 ff_vc1_intfr_non4mv_mbmode_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 39] - vlc_offs
[i
* 3 + 38];
149 init_vlc(&ff_vc1_intfr_non4mv_mbmode_vlc
[i
], VC1_INTFR_NON4MV_MBMODE_VLC_BITS
, 9,
150 ff_vc1_intfr_non4mv_mbmode_bits
[i
], 1, 1,
151 ff_vc1_intfr_non4mv_mbmode_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
152 /* initialize interlaced MVDATA tables (1-Ref) */
153 ff_vc1_1ref_mvdata_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 39]];
154 ff_vc1_1ref_mvdata_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 40] - vlc_offs
[i
* 3 + 39];
155 init_vlc(&ff_vc1_1ref_mvdata_vlc
[i
], VC1_1REF_MVDATA_VLC_BITS
, 72,
156 ff_vc1_1ref_mvdata_bits
[i
], 1, 1,
157 ff_vc1_1ref_mvdata_codes
[i
], 4, 4, INIT_VLC_USE_NEW_STATIC
);
159 for (i
= 0; i
< 4; i
++) {
160 /* Initialize 2MV Block pattern VLC tables */
161 ff_vc1_2mv_block_pattern_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
+ 49]];
162 ff_vc1_2mv_block_pattern_vlc
[i
].table_allocated
= vlc_offs
[i
+ 50] - vlc_offs
[i
+ 49];
163 init_vlc(&ff_vc1_2mv_block_pattern_vlc
[i
], VC1_2MV_BLOCK_PATTERN_VLC_BITS
, 4,
164 ff_vc1_2mv_block_pattern_bits
[i
], 1, 1,
165 ff_vc1_2mv_block_pattern_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
167 for (i
= 0; i
< 8; i
++) {
168 /* Initialize interlaced CBPCY VLC tables (Table 124 - Table 131) */
169 ff_vc1_icbpcy_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 53]];
170 ff_vc1_icbpcy_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 54] - vlc_offs
[i
* 3 + 53];
171 init_vlc(&ff_vc1_icbpcy_vlc
[i
], VC1_ICBPCY_VLC_BITS
, 63,
172 ff_vc1_icbpcy_p_bits
[i
], 1, 1,
173 ff_vc1_icbpcy_p_codes
[i
], 2, 2, INIT_VLC_USE_NEW_STATIC
);
174 /* Initialize interlaced field picture MBMODE VLC tables */
175 ff_vc1_if_mmv_mbmode_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 54]];
176 ff_vc1_if_mmv_mbmode_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 55] - vlc_offs
[i
* 3 + 54];
177 init_vlc(&ff_vc1_if_mmv_mbmode_vlc
[i
], VC1_IF_MMV_MBMODE_VLC_BITS
, 8,
178 ff_vc1_if_mmv_mbmode_bits
[i
], 1, 1,
179 ff_vc1_if_mmv_mbmode_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
180 ff_vc1_if_1mv_mbmode_vlc
[i
].table
= &vlc_table
[vlc_offs
[i
* 3 + 55]];
181 ff_vc1_if_1mv_mbmode_vlc
[i
].table_allocated
= vlc_offs
[i
* 3 + 56] - vlc_offs
[i
* 3 + 55];
182 init_vlc(&ff_vc1_if_1mv_mbmode_vlc
[i
], VC1_IF_1MV_MBMODE_VLC_BITS
, 6,
183 ff_vc1_if_1mv_mbmode_bits
[i
], 1, 1,
184 ff_vc1_if_1mv_mbmode_codes
[i
], 1, 1, INIT_VLC_USE_NEW_STATIC
);
191 v
->mvrange
= 0; /* 7.1.1.18, p80 */
196 /***********************************************************************/
198 * @name VC-1 Bitplane decoding
216 /** @} */ //imode defines
219 /** @} */ //Bitplane group
221 static void vc1_put_signed_blocks_clamped(VC1Context
*v
)
223 MpegEncContext
*s
= &v
->s
;
224 int topleft_mb_pos
, top_mb_pos
;
225 int stride_y
, fieldtx
;
228 /* The put pixels loop is always one MB row behind the decoding loop,
229 * because we can only put pixels when overlap filtering is done, and
230 * for filtering of the bottom edge of a MB, we need the next MB row
232 * Within the row, the put pixels loop is also one MB col behind the
233 * decoding loop. The reason for this is again, because for filtering
234 * of the right MB edge, we need the next MB present. */
235 if (!s
->first_slice_line
) {
237 topleft_mb_pos
= (s
->mb_y
- 1) * s
->mb_stride
+ s
->mb_x
- 1;
238 fieldtx
= v
->fieldtx_plane
[topleft_mb_pos
];
239 stride_y
= s
->linesize
<< fieldtx
;
240 v_dist
= (16 - fieldtx
) >> (fieldtx
== 0);
241 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][0],
242 s
->dest
[0] - 16 * s
->linesize
- 16,
244 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][1],
245 s
->dest
[0] - 16 * s
->linesize
- 8,
247 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][2],
248 s
->dest
[0] - v_dist
* s
->linesize
- 16,
250 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][3],
251 s
->dest
[0] - v_dist
* s
->linesize
- 8,
253 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][4],
254 s
->dest
[1] - 8 * s
->uvlinesize
- 8,
256 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->topleft_blk_idx
][5],
257 s
->dest
[2] - 8 * s
->uvlinesize
- 8,
260 if (s
->mb_x
== s
->mb_width
- 1) {
261 top_mb_pos
= (s
->mb_y
- 1) * s
->mb_stride
+ s
->mb_x
;
262 fieldtx
= v
->fieldtx_plane
[top_mb_pos
];
263 stride_y
= s
->linesize
<< fieldtx
;
264 v_dist
= fieldtx ?
15 : 8;
265 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][0],
266 s
->dest
[0] - 16 * s
->linesize
,
268 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][1],
269 s
->dest
[0] - 16 * s
->linesize
+ 8,
271 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][2],
272 s
->dest
[0] - v_dist
* s
->linesize
,
274 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][3],
275 s
->dest
[0] - v_dist
* s
->linesize
+ 8,
277 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][4],
278 s
->dest
[1] - 8 * s
->uvlinesize
,
280 s
->dsp
.put_signed_pixels_clamped(v
->block
[v
->top_blk_idx
][5],
281 s
->dest
[2] - 8 * s
->uvlinesize
,
286 #define inc_blk_idx(idx) do { \
288 if (idx >= v->n_allocated_blks) \
292 inc_blk_idx(v
->topleft_blk_idx
);
293 inc_blk_idx(v
->top_blk_idx
);
294 inc_blk_idx(v
->left_blk_idx
);
295 inc_blk_idx(v
->cur_blk_idx
);
298 static void vc1_loop_filter_iblk(VC1Context
*v
, int pq
)
300 MpegEncContext
*s
= &v
->s
;
302 if (!s
->first_slice_line
) {
303 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0], s
->linesize
, pq
);
305 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
, s
->linesize
, pq
);
306 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
+ 8, s
->linesize
, pq
);
307 for (j
= 0; j
< 2; j
++) {
308 v
->vc1dsp
.vc1_v_loop_filter8(s
->dest
[j
+ 1], s
->uvlinesize
, pq
);
310 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[j
+ 1] - 8 * s
->uvlinesize
, s
->uvlinesize
, pq
);
313 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0] + 8 * s
->linesize
, s
->linesize
, pq
);
315 if (s
->mb_y
== s
->end_mb_y
- 1) {
317 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0], s
->linesize
, pq
);
318 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[1], s
->uvlinesize
, pq
);
319 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[2], s
->uvlinesize
, pq
);
321 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] + 8, s
->linesize
, pq
);
325 static void vc1_loop_filter_iblk_delayed(VC1Context
*v
, int pq
)
327 MpegEncContext
*s
= &v
->s
;
330 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
331 * means it runs two rows/cols behind the decoding loop. */
332 if (!s
->first_slice_line
) {
334 if (s
->mb_y
>= s
->start_mb_y
+ 2) {
335 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0] - 16 * s
->linesize
- 16, s
->linesize
, pq
);
338 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 32 * s
->linesize
- 16, s
->linesize
, pq
);
339 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 32 * s
->linesize
- 8, s
->linesize
, pq
);
340 for (j
= 0; j
< 2; j
++) {
341 v
->vc1dsp
.vc1_v_loop_filter8(s
->dest
[j
+ 1] - 8 * s
->uvlinesize
- 8, s
->uvlinesize
, pq
);
343 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[j
+ 1] - 16 * s
->uvlinesize
- 8, s
->uvlinesize
, pq
);
347 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0] - 8 * s
->linesize
- 16, s
->linesize
, pq
);
350 if (s
->mb_x
== s
->mb_width
- 1) {
351 if (s
->mb_y
>= s
->start_mb_y
+ 2) {
352 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0] - 16 * s
->linesize
, s
->linesize
, pq
);
355 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 32 * s
->linesize
, s
->linesize
, pq
);
356 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 32 * s
->linesize
+ 8, s
->linesize
, pq
);
357 for (j
= 0; j
< 2; j
++) {
358 v
->vc1dsp
.vc1_v_loop_filter8(s
->dest
[j
+ 1] - 8 * s
->uvlinesize
, s
->uvlinesize
, pq
);
360 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[j
+ 1] - 16 * s
->uvlinesize
, s
->uvlinesize
, pq
);
364 v
->vc1dsp
.vc1_v_loop_filter16(s
->dest
[0] - 8 * s
->linesize
, s
->linesize
, pq
);
367 if (s
->mb_y
== s
->end_mb_y
) {
370 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
- 16, s
->linesize
, pq
);
371 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
- 8, s
->linesize
, pq
);
373 for (j
= 0; j
< 2; j
++) {
374 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[j
+ 1] - 8 * s
->uvlinesize
- 8, s
->uvlinesize
, pq
);
379 if (s
->mb_x
== s
->mb_width
- 1) {
381 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
, s
->linesize
, pq
);
382 v
->vc1dsp
.vc1_h_loop_filter16(s
->dest
[0] - 16 * s
->linesize
+ 8, s
->linesize
, pq
);
384 for (j
= 0; j
< 2; j
++) {
385 v
->vc1dsp
.vc1_h_loop_filter8(s
->dest
[j
+ 1] - 8 * s
->uvlinesize
, s
->uvlinesize
, pq
);
393 static void vc1_smooth_overlap_filter_iblk(VC1Context
*v
)
395 MpegEncContext
*s
= &v
->s
;
398 if (v
->condover
== CONDOVER_NONE
)
401 mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
403 /* Within a MB, the horizontal overlap always runs before the vertical.
404 * To accomplish that, we run the H on left and internal borders of the
405 * currently decoded MB. Then, we wait for the next overlap iteration
406 * to do H overlap on the right edge of this MB, before moving over and
407 * running the V overlap. Therefore, the V overlap makes us trail by one
408 * MB col and the H overlap filter makes us trail by one MB row. This
409 * is reflected in the time at which we run the put_pixels loop. */
410 if (v
->condover
== CONDOVER_ALL
|| v
->pq
>= 9 || v
->over_flags_plane
[mb_pos
]) {
411 if (s
->mb_x
&& (v
->condover
== CONDOVER_ALL
|| v
->pq
>= 9 ||
412 v
->over_flags_plane
[mb_pos
- 1])) {
413 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->left_blk_idx
][1],
414 v
->block
[v
->cur_blk_idx
][0]);
415 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->left_blk_idx
][3],
416 v
->block
[v
->cur_blk_idx
][2]);
417 if (!(s
->flags
& CODEC_FLAG_GRAY
)) {
418 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->left_blk_idx
][4],
419 v
->block
[v
->cur_blk_idx
][4]);
420 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->left_blk_idx
][5],
421 v
->block
[v
->cur_blk_idx
][5]);
424 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->cur_blk_idx
][0],
425 v
->block
[v
->cur_blk_idx
][1]);
426 v
->vc1dsp
.vc1_h_s_overlap(v
->block
[v
->cur_blk_idx
][2],
427 v
->block
[v
->cur_blk_idx
][3]);
429 if (s
->mb_x
== s
->mb_width
- 1) {
430 if (!s
->first_slice_line
&& (v
->condover
== CONDOVER_ALL
|| v
->pq
>= 9 ||
431 v
->over_flags_plane
[mb_pos
- s
->mb_stride
])) {
432 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->top_blk_idx
][2],
433 v
->block
[v
->cur_blk_idx
][0]);
434 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->top_blk_idx
][3],
435 v
->block
[v
->cur_blk_idx
][1]);
436 if (!(s
->flags
& CODEC_FLAG_GRAY
)) {
437 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->top_blk_idx
][4],
438 v
->block
[v
->cur_blk_idx
][4]);
439 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->top_blk_idx
][5],
440 v
->block
[v
->cur_blk_idx
][5]);
443 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->cur_blk_idx
][0],
444 v
->block
[v
->cur_blk_idx
][2]);
445 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->cur_blk_idx
][1],
446 v
->block
[v
->cur_blk_idx
][3]);
449 if (s
->mb_x
&& (v
->condover
== CONDOVER_ALL
|| v
->over_flags_plane
[mb_pos
- 1])) {
450 if (!s
->first_slice_line
&& (v
->condover
== CONDOVER_ALL
|| v
->pq
>= 9 ||
451 v
->over_flags_plane
[mb_pos
- s
->mb_stride
- 1])) {
452 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->topleft_blk_idx
][2],
453 v
->block
[v
->left_blk_idx
][0]);
454 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->topleft_blk_idx
][3],
455 v
->block
[v
->left_blk_idx
][1]);
456 if (!(s
->flags
& CODEC_FLAG_GRAY
)) {
457 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->topleft_blk_idx
][4],
458 v
->block
[v
->left_blk_idx
][4]);
459 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->topleft_blk_idx
][5],
460 v
->block
[v
->left_blk_idx
][5]);
463 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->left_blk_idx
][0],
464 v
->block
[v
->left_blk_idx
][2]);
465 v
->vc1dsp
.vc1_v_s_overlap(v
->block
[v
->left_blk_idx
][1],
466 v
->block
[v
->left_blk_idx
][3]);
470 /** Do motion compensation over 1 macroblock
471 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
473 static void vc1_mc_1mv(VC1Context
*v
, int dir
)
475 MpegEncContext
*s
= &v
->s
;
476 DSPContext
*dsp
= &v
->s
.dsp
;
477 uint8_t *srcY
, *srcU
, *srcV
;
478 int dxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
480 int v_edge_pos
= s
->v_edge_pos
>> v
->field_mode
;
481 if (!v
->field_mode
&& !v
->s
.last_picture
.f
.data
[0])
484 mx
= s
->mv
[dir
][0][0];
485 my
= s
->mv
[dir
][0][1];
487 // store motion vectors for further use in B frames
488 if (s
->pict_type
== AV_PICTURE_TYPE_P
) {
489 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0] = mx
;
490 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1] = my
;
493 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
494 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
495 v
->luma_mv
[s
->mb_x
][0] = uvmx
;
496 v
->luma_mv
[s
->mb_x
][1] = uvmy
;
499 v
->cur_field_type
!= v
->ref_field_type
[dir
]) {
500 my
= my
- 2 + 4 * v
->cur_field_type
;
501 uvmy
= uvmy
- 2 + 4 * v
->cur_field_type
;
504 // fastuvmc shall be ignored for interlaced frame picture
505 if (v
->fastuvmc
&& (v
->fcm
!= ILACE_FRAME
)) {
506 uvmx
= uvmx
+ ((uvmx
< 0) ?
(uvmx
& 1) : -(uvmx
& 1));
507 uvmy
= uvmy
+ ((uvmy
< 0) ?
(uvmy
& 1) : -(uvmy
& 1));
509 if (v
->field_mode
) { // interlaced field picture
511 if ((v
->cur_field_type
!= v
->ref_field_type
[dir
]) && v
->cur_field_type
) {
512 srcY
= s
->current_picture
.f
.data
[0];
513 srcU
= s
->current_picture
.f
.data
[1];
514 srcV
= s
->current_picture
.f
.data
[2];
516 srcY
= s
->last_picture
.f
.data
[0];
517 srcU
= s
->last_picture
.f
.data
[1];
518 srcV
= s
->last_picture
.f
.data
[2];
521 srcY
= s
->next_picture
.f
.data
[0];
522 srcU
= s
->next_picture
.f
.data
[1];
523 srcV
= s
->next_picture
.f
.data
[2];
527 srcY
= s
->last_picture
.f
.data
[0];
528 srcU
= s
->last_picture
.f
.data
[1];
529 srcV
= s
->last_picture
.f
.data
[2];
531 srcY
= s
->next_picture
.f
.data
[0];
532 srcU
= s
->next_picture
.f
.data
[1];
533 srcV
= s
->next_picture
.f
.data
[2];
537 src_x
= s
->mb_x
* 16 + (mx
>> 2);
538 src_y
= s
->mb_y
* 16 + (my
>> 2);
539 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
540 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
542 if (v
->profile
!= PROFILE_ADVANCED
) {
543 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
544 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
545 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
546 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
548 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
549 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
550 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
551 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
554 srcY
+= src_y
* s
->linesize
+ src_x
;
555 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
556 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
558 if (v
->field_mode
&& v
->ref_field_type
[dir
]) {
559 srcY
+= s
->current_picture_ptr
->f
.linesize
[0];
560 srcU
+= s
->current_picture_ptr
->f
.linesize
[1];
561 srcV
+= s
->current_picture_ptr
->f
.linesize
[2];
564 /* for grayscale we should not try to read from unknown area */
565 if (s
->flags
& CODEC_FLAG_GRAY
) {
566 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
567 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
570 if (v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
571 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
&3) - 16 - s
->mspel
* 3
572 || (unsigned)(src_y
- s
->mspel
) > v_edge_pos
- (my
&3) - 16 - s
->mspel
* 3) {
573 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
575 srcY
-= s
->mspel
* (1 + s
->linesize
);
576 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
,
577 17 + s
->mspel
* 2, 17 + s
->mspel
* 2,
578 src_x
- s
->mspel
, src_y
- s
->mspel
,
579 s
->h_edge_pos
, v_edge_pos
);
580 srcY
= s
->edge_emu_buffer
;
581 s
->dsp
.emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8 + 1, 8 + 1,
582 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
583 s
->dsp
.emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8 + 1, 8 + 1,
584 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
587 /* if we deal with range reduction we need to scale source blocks */
588 if (v
->rangeredfrm
) {
593 for (j
= 0; j
< 17 + s
->mspel
* 2; j
++) {
594 for (i
= 0; i
< 17 + s
->mspel
* 2; i
++)
595 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
600 for (j
= 0; j
< 9; j
++) {
601 for (i
= 0; i
< 9; i
++) {
602 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
603 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
605 src
+= s
->uvlinesize
;
606 src2
+= s
->uvlinesize
;
609 /* if we deal with intensity compensation we need to scale source blocks */
610 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
615 for (j
= 0; j
< 17 + s
->mspel
* 2; j
++) {
616 for (i
= 0; i
< 17 + s
->mspel
* 2; i
++)
617 src
[i
] = v
->luty
[src
[i
]];
622 for (j
= 0; j
< 9; j
++) {
623 for (i
= 0; i
< 9; i
++) {
624 src
[i
] = v
->lutuv
[src
[i
]];
625 src2
[i
] = v
->lutuv
[src2
[i
]];
627 src
+= s
->uvlinesize
;
628 src2
+= s
->uvlinesize
;
631 srcY
+= s
->mspel
* (1 + s
->linesize
);
634 if (v
->field_mode
&& v
->cur_field_type
) {
635 off
= s
->current_picture_ptr
->f
.linesize
[0];
636 off_uv
= s
->current_picture_ptr
->f
.linesize
[1];
642 dxy
= ((my
& 3) << 2) | (mx
& 3);
643 v
->vc1dsp
.put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, v
->rnd
);
644 v
->vc1dsp
.put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
645 srcY
+= s
->linesize
* 8;
646 v
->vc1dsp
.put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8 * s
->linesize
, srcY
, s
->linesize
, v
->rnd
);
647 v
->vc1dsp
.put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8 * s
->linesize
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
648 } else { // hpel mc - always used for luma
649 dxy
= (my
& 2) | ((mx
& 2) >> 1);
651 dsp
->put_pixels_tab
[0][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 16);
653 dsp
->put_no_rnd_pixels_tab
[0][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 16);
656 if (s
->flags
& CODEC_FLAG_GRAY
) return;
657 /* Chroma MC always uses qpel bilinear */
658 uvmx
= (uvmx
& 3) << 1;
659 uvmy
= (uvmy
& 3) << 1;
661 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1] + off_uv
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
662 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2] + off_uv
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
664 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1] + off_uv
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
665 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2] + off_uv
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
669 static inline int median4(int a
, int b
, int c
, int d
)
672 if (c
< d
) return (FFMIN(b
, d
) + FFMAX(a
, c
)) / 2;
673 else return (FFMIN(b
, c
) + FFMAX(a
, d
)) / 2;
675 if (c
< d
) return (FFMIN(a
, d
) + FFMAX(b
, c
)) / 2;
676 else return (FFMIN(a
, c
) + FFMAX(b
, d
)) / 2;
680 /** Do motion compensation for 4-MV macroblock - luminance block
682 static void vc1_mc_4mv_luma(VC1Context
*v
, int n
, int dir
)
684 MpegEncContext
*s
= &v
->s
;
685 DSPContext
*dsp
= &v
->s
.dsp
;
687 int dxy
, mx
, my
, src_x
, src_y
;
689 int fieldmv
= (v
->fcm
== ILACE_FRAME
) ? v
->blk_mv_type
[s
->block_index
[n
]] : 0;
690 int v_edge_pos
= s
->v_edge_pos
>> v
->field_mode
;
692 if (!v
->field_mode
&& !v
->s
.last_picture
.f
.data
[0])
695 mx
= s
->mv
[dir
][n
][0];
696 my
= s
->mv
[dir
][n
][1];
700 if ((v
->cur_field_type
!= v
->ref_field_type
[dir
]) && v
->cur_field_type
)
701 srcY
= s
->current_picture
.f
.data
[0];
703 srcY
= s
->last_picture
.f
.data
[0];
705 srcY
= s
->last_picture
.f
.data
[0];
707 srcY
= s
->next_picture
.f
.data
[0];
710 if (v
->cur_field_type
!= v
->ref_field_type
[dir
])
711 my
= my
- 2 + 4 * v
->cur_field_type
;
714 if (s
->pict_type
== AV_PICTURE_TYPE_P
&& n
== 3 && v
->field_mode
) {
715 int same_count
= 0, opp_count
= 0, k
;
716 int chosen_mv
[2][4][2], f
;
718 for (k
= 0; k
< 4; k
++) {
719 f
= v
->mv_f
[0][s
->block_index
[k
] + v
->blocks_off
];
720 chosen_mv
[f
][f ? opp_count
: same_count
][0] = s
->mv
[0][k
][0];
721 chosen_mv
[f
][f ? opp_count
: same_count
][1] = s
->mv
[0][k
][1];
725 f
= opp_count
> same_count
;
726 switch (f ? opp_count
: same_count
) {
728 tx
= median4(chosen_mv
[f
][0][0], chosen_mv
[f
][1][0],
729 chosen_mv
[f
][2][0], chosen_mv
[f
][3][0]);
730 ty
= median4(chosen_mv
[f
][0][1], chosen_mv
[f
][1][1],
731 chosen_mv
[f
][2][1], chosen_mv
[f
][3][1]);
734 tx
= mid_pred(chosen_mv
[f
][0][0], chosen_mv
[f
][1][0], chosen_mv
[f
][2][0]);
735 ty
= mid_pred(chosen_mv
[f
][0][1], chosen_mv
[f
][1][1], chosen_mv
[f
][2][1]);
738 tx
= (chosen_mv
[f
][0][0] + chosen_mv
[f
][1][0]) / 2;
739 ty
= (chosen_mv
[f
][0][1] + chosen_mv
[f
][1][1]) / 2;
742 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0] = tx
;
743 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1] = ty
;
744 for (k
= 0; k
< 4; k
++)
745 v
->mv_f
[1][s
->block_index
[k
] + v
->blocks_off
] = f
;
748 if (v
->fcm
== ILACE_FRAME
) { // not sure if needed for other types of picture
750 int width
= s
->avctx
->coded_width
;
751 int height
= s
->avctx
->coded_height
>> 1;
752 qx
= (s
->mb_x
* 16) + (mx
>> 2);
753 qy
= (s
->mb_y
* 8) + (my
>> 3);
758 mx
-= 4 * (qx
- width
);
761 else if (qy
> height
+ 1)
762 my
-= 8 * (qy
- height
- 1);
765 if ((v
->fcm
== ILACE_FRAME
) && fieldmv
)
766 off
= ((n
> 1) ? s
->linesize
: 0) + (n
& 1) * 8;
768 off
= s
->linesize
* 4 * (n
& 2) + (n
& 1) * 8;
769 if (v
->field_mode
&& v
->cur_field_type
)
770 off
+= s
->current_picture_ptr
->f
.linesize
[0];
772 src_x
= s
->mb_x
* 16 + (n
& 1) * 8 + (mx
>> 2);
774 src_y
= s
->mb_y
* 16 + (n
& 2) * 4 + (my
>> 2);
776 src_y
= s
->mb_y
* 16 + ((n
> 1) ?
1 : 0) + (my
>> 2);
778 if (v
->profile
!= PROFILE_ADVANCED
) {
779 src_x
= av_clip(src_x
, -16, s
->mb_width
* 16);
780 src_y
= av_clip(src_y
, -16, s
->mb_height
* 16);
782 src_x
= av_clip(src_x
, -17, s
->avctx
->coded_width
);
783 if (v
->fcm
== ILACE_FRAME
) {
785 src_y
= av_clip(src_y
, -17, s
->avctx
->coded_height
+ 1);
787 src_y
= av_clip(src_y
, -18, s
->avctx
->coded_height
);
789 src_y
= av_clip(src_y
, -18, s
->avctx
->coded_height
+ 1);
793 srcY
+= src_y
* s
->linesize
+ src_x
;
794 if (v
->field_mode
&& v
->ref_field_type
[dir
])
795 srcY
+= s
->current_picture_ptr
->f
.linesize
[0];
797 if (fieldmv
&& !(src_y
& 1))
799 if (fieldmv
&& (src_y
& 1) && src_y
< 4)
801 if (v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
802 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
& 3) - 8 - s
->mspel
* 2
803 || (unsigned)(src_y
- (s
->mspel
<< fieldmv
)) > v_edge_pos
- (my
& 3) - ((8 + s
->mspel
* 2) << fieldmv
)) {
804 srcY
-= s
->mspel
* (1 + (s
->linesize
<< fieldmv
));
805 /* check emulate edge stride and offset */
806 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
,
807 9 + s
->mspel
* 2, (9 + s
->mspel
* 2) << fieldmv
,
808 src_x
- s
->mspel
, src_y
- (s
->mspel
<< fieldmv
),
809 s
->h_edge_pos
, v_edge_pos
);
810 srcY
= s
->edge_emu_buffer
;
811 /* if we deal with range reduction we need to scale source blocks */
812 if (v
->rangeredfrm
) {
817 for (j
= 0; j
< 9 + s
->mspel
* 2; j
++) {
818 for (i
= 0; i
< 9 + s
->mspel
* 2; i
++)
819 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
820 src
+= s
->linesize
<< fieldmv
;
823 /* if we deal with intensity compensation we need to scale source blocks */
824 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
829 for (j
= 0; j
< 9 + s
->mspel
* 2; j
++) {
830 for (i
= 0; i
< 9 + s
->mspel
* 2; i
++)
831 src
[i
] = v
->luty
[src
[i
]];
832 src
+= s
->linesize
<< fieldmv
;
835 srcY
+= s
->mspel
* (1 + (s
->linesize
<< fieldmv
));
839 dxy
= ((my
& 3) << 2) | (mx
& 3);
840 v
->vc1dsp
.put_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
<< fieldmv
, v
->rnd
);
841 } else { // hpel mc - always used for luma
842 dxy
= (my
& 2) | ((mx
& 2) >> 1);
844 dsp
->put_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
846 dsp
->put_no_rnd_pixels_tab
[1][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 8);
850 static av_always_inline
int get_chroma_mv(int *mvx
, int *mvy
, int *a
, int flag
, int *tx
, int *ty
)
853 static const int count
[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
855 idx
= ((a
[3] != flag
) << 3)
856 | ((a
[2] != flag
) << 2)
857 | ((a
[1] != flag
) << 1)
860 *tx
= median4(mvx
[0], mvx
[1], mvx
[2], mvx
[3]);
861 *ty
= median4(mvy
[0], mvy
[1], mvy
[2], mvy
[3]);
863 } else if (count
[idx
] == 1) {
866 *tx
= mid_pred(mvx
[1], mvx
[2], mvx
[3]);
867 *ty
= mid_pred(mvy
[1], mvy
[2], mvy
[3]);
870 *tx
= mid_pred(mvx
[0], mvx
[2], mvx
[3]);
871 *ty
= mid_pred(mvy
[0], mvy
[2], mvy
[3]);
874 *tx
= mid_pred(mvx
[0], mvx
[1], mvx
[3]);
875 *ty
= mid_pred(mvy
[0], mvy
[1], mvy
[3]);
878 *tx
= mid_pred(mvx
[0], mvx
[1], mvx
[2]);
879 *ty
= mid_pred(mvy
[0], mvy
[1], mvy
[2]);
882 } else if (count
[idx
] == 2) {
884 for (i
= 0; i
< 3; i
++)
889 for (i
= t1
+ 1; i
< 4; i
++)
894 *tx
= (mvx
[t1
] + mvx
[t2
]) / 2;
895 *ty
= (mvy
[t1
] + mvy
[t2
]) / 2;
903 /** Do motion compensation for 4-MV macroblock - both chroma blocks
905 static void vc1_mc_4mv_chroma(VC1Context
*v
, int dir
)
907 MpegEncContext
*s
= &v
->s
;
908 DSPContext
*dsp
= &v
->s
.dsp
;
909 uint8_t *srcU
, *srcV
;
910 int uvmx
, uvmy
, uvsrc_x
, uvsrc_y
;
911 int k
, tx
= 0, ty
= 0;
912 int mvx
[4], mvy
[4], intra
[4], mv_f
[4];
914 int chroma_ref_type
= v
->cur_field_type
, off
= 0;
915 int v_edge_pos
= s
->v_edge_pos
>> v
->field_mode
;
917 if (!v
->field_mode
&& !v
->s
.last_picture
.f
.data
[0])
919 if (s
->flags
& CODEC_FLAG_GRAY
)
922 for (k
= 0; k
< 4; k
++) {
923 mvx
[k
] = s
->mv
[dir
][k
][0];
924 mvy
[k
] = s
->mv
[dir
][k
][1];
925 intra
[k
] = v
->mb_type
[0][s
->block_index
[k
]];
927 mv_f
[k
] = v
->mv_f
[dir
][s
->block_index
[k
] + v
->blocks_off
];
930 /* calculate chroma MV vector from four luma MVs */
931 if (!v
->field_mode
|| (v
->field_mode
&& !v
->numref
)) {
932 valid_count
= get_chroma_mv(mvx
, mvy
, intra
, 0, &tx
, &ty
);
934 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0] = 0;
935 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1] = 0;
936 v
->luma_mv
[s
->mb_x
][0] = v
->luma_mv
[s
->mb_x
][1] = 0;
937 return; //no need to do MC for intra blocks
941 if (mv_f
[0] + mv_f
[1] + mv_f
[2] + mv_f
[3] > 2)
943 valid_count
= get_chroma_mv(mvx
, mvy
, mv_f
, dominant
, &tx
, &ty
);
945 chroma_ref_type
= !v
->cur_field_type
;
947 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0] = tx
;
948 s
->current_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1] = ty
;
949 uvmx
= (tx
+ ((tx
& 3) == 3)) >> 1;
950 uvmy
= (ty
+ ((ty
& 3) == 3)) >> 1;
952 v
->luma_mv
[s
->mb_x
][0] = uvmx
;
953 v
->luma_mv
[s
->mb_x
][1] = uvmy
;
956 uvmx
= uvmx
+ ((uvmx
< 0) ?
(uvmx
& 1) : -(uvmx
& 1));
957 uvmy
= uvmy
+ ((uvmy
< 0) ?
(uvmy
& 1) : -(uvmy
& 1));
959 // Field conversion bias
960 if (v
->cur_field_type
!= chroma_ref_type
)
961 uvmy
+= 2 - 4 * chroma_ref_type
;
963 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
964 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
966 if (v
->profile
!= PROFILE_ADVANCED
) {
967 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
968 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
970 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
971 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
976 if ((v
->cur_field_type
!= chroma_ref_type
) && v
->cur_field_type
) {
977 srcU
= s
->current_picture
.f
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
978 srcV
= s
->current_picture
.f
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
980 srcU
= s
->last_picture
.f
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
981 srcV
= s
->last_picture
.f
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
984 srcU
= s
->last_picture
.f
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
985 srcV
= s
->last_picture
.f
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
988 srcU
= s
->next_picture
.f
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
989 srcV
= s
->next_picture
.f
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
993 if (chroma_ref_type
) {
994 srcU
+= s
->current_picture_ptr
->f
.linesize
[1];
995 srcV
+= s
->current_picture_ptr
->f
.linesize
[2];
997 off
= v
->cur_field_type ? s
->current_picture_ptr
->f
.linesize
[1] : 0;
1000 if (v
->rangeredfrm
|| (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
1001 || (unsigned)uvsrc_x
> (s
->h_edge_pos
>> 1) - 9
1002 || (unsigned)uvsrc_y
> (v_edge_pos
>> 1) - 9) {
1003 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
, srcU
, s
->uvlinesize
,
1004 8 + 1, 8 + 1, uvsrc_x
, uvsrc_y
,
1005 s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
1006 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
+ 16, srcV
, s
->uvlinesize
,
1007 8 + 1, 8 + 1, uvsrc_x
, uvsrc_y
,
1008 s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
1009 srcU
= s
->edge_emu_buffer
;
1010 srcV
= s
->edge_emu_buffer
+ 16;
1012 /* if we deal with range reduction we need to scale source blocks */
1013 if (v
->rangeredfrm
) {
1015 uint8_t *src
, *src2
;
1019 for (j
= 0; j
< 9; j
++) {
1020 for (i
= 0; i
< 9; i
++) {
1021 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
1022 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
1024 src
+= s
->uvlinesize
;
1025 src2
+= s
->uvlinesize
;
1028 /* if we deal with intensity compensation we need to scale source blocks */
1029 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
1031 uint8_t *src
, *src2
;
1035 for (j
= 0; j
< 9; j
++) {
1036 for (i
= 0; i
< 9; i
++) {
1037 src
[i
] = v
->lutuv
[src
[i
]];
1038 src2
[i
] = v
->lutuv
[src2
[i
]];
1040 src
+= s
->uvlinesize
;
1041 src2
+= s
->uvlinesize
;
1046 /* Chroma MC always uses qpel bilinear */
1047 uvmx
= (uvmx
& 3) << 1;
1048 uvmy
= (uvmy
& 3) << 1;
1050 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[1] + off
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1051 dsp
->put_h264_chroma_pixels_tab
[0](s
->dest
[2] + off
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1053 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1] + off
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1054 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2] + off
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
1058 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
1060 static void vc1_mc_4mv_chroma4(VC1Context
*v
)
1062 MpegEncContext
*s
= &v
->s
;
1063 DSPContext
*dsp
= &v
->s
.dsp
;
1064 uint8_t *srcU
, *srcV
;
1065 int uvsrc_x
, uvsrc_y
;
1066 int uvmx_field
[4], uvmy_field
[4];
1068 int fieldmv
= v
->blk_mv_type
[s
->block_index
[0]];
1069 static const int s_rndtblfield
[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
1070 int v_dist
= fieldmv ?
1 : 4; // vertical offset for lower sub-blocks
1071 int v_edge_pos
= s
->v_edge_pos
>> 1;
1073 if (!v
->s
.last_picture
.f
.data
[0])
1075 if (s
->flags
& CODEC_FLAG_GRAY
)
1078 for (i
= 0; i
< 4; i
++) {
1079 tx
= s
->mv
[0][i
][0];
1080 uvmx_field
[i
] = (tx
+ ((tx
& 3) == 3)) >> 1;
1081 ty
= s
->mv
[0][i
][1];
1083 uvmy_field
[i
] = (ty
>> 4) * 8 + s_rndtblfield
[ty
& 0xF];
1085 uvmy_field
[i
] = (ty
+ ((ty
& 3) == 3)) >> 1;
1088 for (i
= 0; i
< 4; i
++) {
1089 off
= (i
& 1) * 4 + ((i
& 2) ? v_dist
* s
->uvlinesize
: 0);
1090 uvsrc_x
= s
->mb_x
* 8 + (i
& 1) * 4 + (uvmx_field
[i
] >> 2);
1091 uvsrc_y
= s
->mb_y
* 8 + ((i
& 2) ? v_dist
: 0) + (uvmy_field
[i
] >> 2);
1092 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1093 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
1094 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
1095 srcU
= s
->last_picture
.f
.data
[1] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1096 srcV
= s
->last_picture
.f
.data
[2] + uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1097 uvmx_field
[i
] = (uvmx_field
[i
] & 3) << 1;
1098 uvmy_field
[i
] = (uvmy_field
[i
] & 3) << 1;
1100 if (fieldmv
&& !(uvsrc_y
& 1))
1102 if (fieldmv
&& (uvsrc_y
& 1) && uvsrc_y
< 2)
1104 if ((v
->mv_mode
== MV_PMODE_INTENSITY_COMP
)
1105 || (unsigned)uvsrc_x
> (s
->h_edge_pos
>> 1) - 5
1106 || (unsigned)uvsrc_y
> v_edge_pos
- (5 << fieldmv
)) {
1107 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
, srcU
, s
->uvlinesize
,
1108 5, (5 << fieldmv
), uvsrc_x
, uvsrc_y
,
1109 s
->h_edge_pos
>> 1, v_edge_pos
);
1110 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
+ 16, srcV
, s
->uvlinesize
,
1111 5, (5 << fieldmv
), uvsrc_x
, uvsrc_y
,
1112 s
->h_edge_pos
>> 1, v_edge_pos
);
1113 srcU
= s
->edge_emu_buffer
;
1114 srcV
= s
->edge_emu_buffer
+ 16;
1116 /* if we deal with intensity compensation we need to scale source blocks */
1117 if (v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) {
1119 uint8_t *src
, *src2
;
1123 for (j
= 0; j
< 5; j
++) {
1124 for (i
= 0; i
< 5; i
++) {
1125 src
[i
] = v
->lutuv
[src
[i
]];
1126 src2
[i
] = v
->lutuv
[src2
[i
]];
1128 src
+= s
->uvlinesize
<< 1;
1129 src2
+= s
->uvlinesize
<< 1;
1134 dsp
->put_h264_chroma_pixels_tab
[1](s
->dest
[1] + off
, srcU
, s
->uvlinesize
<< fieldmv
, 4, uvmx_field
[i
], uvmy_field
[i
]);
1135 dsp
->put_h264_chroma_pixels_tab
[1](s
->dest
[2] + off
, srcV
, s
->uvlinesize
<< fieldmv
, 4, uvmx_field
[i
], uvmy_field
[i
]);
1137 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[1](s
->dest
[1] + off
, srcU
, s
->uvlinesize
<< fieldmv
, 4, uvmx_field
[i
], uvmy_field
[i
]);
1138 v
->vc1dsp
.put_no_rnd_vc1_chroma_pixels_tab
[1](s
->dest
[2] + off
, srcV
, s
->uvlinesize
<< fieldmv
, 4, uvmx_field
[i
], uvmy_field
[i
]);
1143 /***********************************************************************/
1145 * @name VC-1 Block-level functions
1146 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1152 * @brief Get macroblock-level quantizer scale
1154 #define GET_MQUANT() \
1155 if (v->dquantfrm) { \
1157 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1158 if (v->dqbilevel) { \
1159 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1161 mqdiff = get_bits(gb, 3); \
1163 mquant = v->pq + mqdiff; \
1165 mquant = get_bits(gb, 5); \
1168 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1169 edges = 1 << v->dqsbedge; \
1170 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1171 edges = (3 << v->dqsbedge) % 15; \
1172 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1174 if ((edges&1) && !s->mb_x) \
1175 mquant = v->altpq; \
1176 if ((edges&2) && s->first_slice_line) \
1177 mquant = v->altpq; \
1178 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1179 mquant = v->altpq; \
1180 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1181 mquant = v->altpq; \
1185 * @def GET_MVDATA(_dmv_x, _dmv_y)
1186 * @brief Get MV differentials
1187 * @see MVDATA decoding from 8.3.5.2, p(1)20
1188 * @param _dmv_x Horizontal differential for decoded MV
1189 * @param _dmv_y Vertical differential for decoded MV
1191 #define GET_MVDATA(_dmv_x, _dmv_y) \
1192 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1193 VC1_MV_DIFF_VLC_BITS, 2); \
1195 mb_has_coeffs = 1; \
1198 mb_has_coeffs = 0; \
1201 _dmv_x = _dmv_y = 0; \
1202 } else if (index == 35) { \
1203 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1204 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1205 } else if (index == 36) { \
1210 index1 = index % 6; \
1211 if (!s->quarter_sample && index1 == 5) val = 1; \
1213 if (size_table[index1] - val > 0) \
1214 val = get_bits(gb, size_table[index1] - val); \
1216 sign = 0 - (val&1); \
1217 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1219 index1 = index / 6; \
1220 if (!s->quarter_sample && index1 == 5) val = 1; \
1222 if (size_table[index1] - val > 0) \
1223 val = get_bits(gb, size_table[index1] - val); \
1225 sign = 0 - (val & 1); \
1226 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1229 static av_always_inline
void get_mvdata_interlaced(VC1Context
*v
, int *dmv_x
,
1230 int *dmv_y
, int *pred_flag
)
1233 int extend_x
= 0, extend_y
= 0;
1234 GetBitContext
*gb
= &v
->s
.gb
;
1237 const int* offs_tab
;
1240 bits
= VC1_2REF_MVDATA_VLC_BITS
;
1243 bits
= VC1_1REF_MVDATA_VLC_BITS
;
1246 switch (v
->dmvrange
) {
1254 extend_x
= extend_y
= 1;
1257 index
= get_vlc2(gb
, v
->imv_vlc
->table
, bits
, 3);
1259 *dmv_x
= get_bits(gb
, v
->k_x
);
1260 *dmv_y
= get_bits(gb
, v
->k_y
);
1262 *pred_flag
= *dmv_y
& 1;
1263 *dmv_y
= (*dmv_y
+ *pred_flag
) >> 1;
1268 offs_tab
= offset_table2
;
1270 offs_tab
= offset_table1
;
1271 index1
= (index
+ 1) % 9;
1273 val
= get_bits(gb
, index1
+ extend_x
);
1274 sign
= 0 -(val
& 1);
1275 *dmv_x
= (sign
^ ((val
>> 1) + offs_tab
[index1
])) - sign
;
1279 offs_tab
= offset_table2
;
1281 offs_tab
= offset_table1
;
1282 index1
= (index
+ 1) / 9;
1283 if (index1
> v
->numref
) {
1284 val
= get_bits(gb
, (index1
+ (extend_y
<< v
->numref
)) >> v
->numref
);
1285 sign
= 0 - (val
& 1);
1286 *dmv_y
= (sign
^ ((val
>> 1) + offs_tab
[index1
>> v
->numref
])) - sign
;
1290 *pred_flag
= index1
& 1;
1294 static av_always_inline
int scaleforsame_x(VC1Context
*v
, int n
/* MV */, int dir
)
1296 int scaledvalue
, refdist
;
1297 int scalesame1
, scalesame2
;
1298 int scalezone1_x
, zone1offset_x
;
1299 int table_index
= dir
^ v
->second_field
;
1301 if (v
->s
.pict_type
!= AV_PICTURE_TYPE_B
)
1302 refdist
= v
->refdist
;
1304 refdist
= dir ? v
->brfd
: v
->frfd
;
1307 scalesame1
= vc1_field_mvpred_scales
[table_index
][1][refdist
];
1308 scalesame2
= vc1_field_mvpred_scales
[table_index
][2][refdist
];
1309 scalezone1_x
= vc1_field_mvpred_scales
[table_index
][3][refdist
];
1310 zone1offset_x
= vc1_field_mvpred_scales
[table_index
][5][refdist
];
1315 if (FFABS(n
) < scalezone1_x
)
1316 scaledvalue
= (n
* scalesame1
) >> 8;
1319 scaledvalue
= ((n
* scalesame2
) >> 8) - zone1offset_x
;
1321 scaledvalue
= ((n
* scalesame2
) >> 8) + zone1offset_x
;
1324 return av_clip(scaledvalue
, -v
->range_x
, v
->range_x
- 1);
1327 static av_always_inline
int scaleforsame_y(VC1Context
*v
, int i
, int n
/* MV */, int dir
)
1329 int scaledvalue
, refdist
;
1330 int scalesame1
, scalesame2
;
1331 int scalezone1_y
, zone1offset_y
;
1332 int table_index
= dir
^ v
->second_field
;
1334 if (v
->s
.pict_type
!= AV_PICTURE_TYPE_B
)
1335 refdist
= v
->refdist
;
1337 refdist
= dir ? v
->brfd
: v
->frfd
;
1340 scalesame1
= vc1_field_mvpred_scales
[table_index
][1][refdist
];
1341 scalesame2
= vc1_field_mvpred_scales
[table_index
][2][refdist
];
1342 scalezone1_y
= vc1_field_mvpred_scales
[table_index
][4][refdist
];
1343 zone1offset_y
= vc1_field_mvpred_scales
[table_index
][6][refdist
];
1348 if (FFABS(n
) < scalezone1_y
)
1349 scaledvalue
= (n
* scalesame1
) >> 8;
1352 scaledvalue
= ((n
* scalesame2
) >> 8) - zone1offset_y
;
1354 scaledvalue
= ((n
* scalesame2
) >> 8) + zone1offset_y
;
1358 if (v
->cur_field_type
&& !v
->ref_field_type
[dir
])
1359 return av_clip(scaledvalue
, -v
->range_y
/ 2 + 1, v
->range_y
/ 2);
1361 return av_clip(scaledvalue
, -v
->range_y
/ 2, v
->range_y
/ 2 - 1);
1364 static av_always_inline
int scaleforopp_x(VC1Context
*v
, int n
/* MV */)
1366 int scalezone1_x
, zone1offset_x
;
1367 int scaleopp1
, scaleopp2
, brfd
;
1370 brfd
= FFMIN(v
->brfd
, 3);
1371 scalezone1_x
= vc1_b_field_mvpred_scales
[3][brfd
];
1372 zone1offset_x
= vc1_b_field_mvpred_scales
[5][brfd
];
1373 scaleopp1
= vc1_b_field_mvpred_scales
[1][brfd
];
1374 scaleopp2
= vc1_b_field_mvpred_scales
[2][brfd
];
1379 if (FFABS(n
) < scalezone1_x
)
1380 scaledvalue
= (n
* scaleopp1
) >> 8;
1383 scaledvalue
= ((n
* scaleopp2
) >> 8) - zone1offset_x
;
1385 scaledvalue
= ((n
* scaleopp2
) >> 8) + zone1offset_x
;
1388 return av_clip(scaledvalue
, -v
->range_x
, v
->range_x
- 1);
1391 static av_always_inline
int scaleforopp_y(VC1Context
*v
, int n
/* MV */, int dir
)
1393 int scalezone1_y
, zone1offset_y
;
1394 int scaleopp1
, scaleopp2
, brfd
;
1397 brfd
= FFMIN(v
->brfd
, 3);
1398 scalezone1_y
= vc1_b_field_mvpred_scales
[4][brfd
];
1399 zone1offset_y
= vc1_b_field_mvpred_scales
[6][brfd
];
1400 scaleopp1
= vc1_b_field_mvpred_scales
[1][brfd
];
1401 scaleopp2
= vc1_b_field_mvpred_scales
[2][brfd
];
1406 if (FFABS(n
) < scalezone1_y
)
1407 scaledvalue
= (n
* scaleopp1
) >> 8;
1410 scaledvalue
= ((n
* scaleopp2
) >> 8) - zone1offset_y
;
1412 scaledvalue
= ((n
* scaleopp2
) >> 8) + zone1offset_y
;
1415 if (v
->cur_field_type
&& !v
->ref_field_type
[dir
]) {
1416 return av_clip(scaledvalue
, -v
->range_y
/ 2 + 1, v
->range_y
/ 2);
1418 return av_clip(scaledvalue
, -v
->range_y
/ 2, v
->range_y
/ 2 - 1);
1422 static av_always_inline
int scaleforsame(VC1Context
*v
, int i
, int n
/* MV */,
1425 int brfd
, scalesame
;
1426 int hpel
= 1 - v
->s
.quarter_sample
;
1429 if (v
->s
.pict_type
!= AV_PICTURE_TYPE_B
|| v
->second_field
|| !dir
) {
1431 n
= scaleforsame_y(v
, i
, n
, dir
) << hpel
;
1433 n
= scaleforsame_x(v
, n
, dir
) << hpel
;
1436 brfd
= FFMIN(v
->brfd
, 3);
1437 scalesame
= vc1_b_field_mvpred_scales
[0][brfd
];
1439 n
= (n
* scalesame
>> 8) << hpel
;
1443 static av_always_inline
int scaleforopp(VC1Context
*v
, int n
/* MV */,
1446 int refdist
, scaleopp
;
1447 int hpel
= 1 - v
->s
.quarter_sample
;
1450 if (v
->s
.pict_type
== AV_PICTURE_TYPE_B
&& !v
->second_field
&& dir
== 1) {
1452 n
= scaleforopp_y(v
, n
, dir
) << hpel
;
1454 n
= scaleforopp_x(v
, n
) << hpel
;
1457 if (v
->s
.pict_type
!= AV_PICTURE_TYPE_B
)
1458 refdist
= FFMIN(v
->refdist
, 3);
1460 refdist
= dir ? v
->brfd
: v
->frfd
;
1461 scaleopp
= vc1_field_mvpred_scales
[dir
^ v
->second_field
][0][refdist
];
1463 n
= (n
* scaleopp
>> 8) << hpel
;
1467 /** Predict and set motion vector
1469 static inline void vc1_pred_mv(VC1Context
*v
, int n
, int dmv_x
, int dmv_y
,
1470 int mv1
, int r_x
, int r_y
, uint8_t* is_intra
,
1471 int pred_flag
, int dir
)
1473 MpegEncContext
*s
= &v
->s
;
1474 int xy
, wrap
, off
= 0;
1478 int mixedmv_pic
, num_samefield
= 0, num_oppfield
= 0;
1479 int opposit
, a_f
, b_f
, c_f
;
1480 int16_t field_predA
[2];
1481 int16_t field_predB
[2];
1482 int16_t field_predC
[2];
1483 int a_valid
, b_valid
, c_valid
;
1484 int hybridmv_thresh
, y_bias
= 0;
1486 if (v
->mv_mode
== MV_PMODE_MIXED_MV
||
1487 ((v
->mv_mode
== MV_PMODE_INTENSITY_COMP
) && (v
->mv_mode2
== MV_PMODE_MIXED_MV
)))
1491 /* scale MV difference to be quad-pel */
1492 dmv_x
<<= 1 - s
->quarter_sample
;
1493 dmv_y
<<= 1 - s
->quarter_sample
;
1495 wrap
= s
->b8_stride
;
1496 xy
= s
->block_index
[n
];
1499 s
->mv
[0][n
][0] = s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][0] = 0;
1500 s
->mv
[0][n
][1] = s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][1] = 0;
1501 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][0] = 0;
1502 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][1] = 0;
1503 if (mv1
) { /* duplicate motion data for 1-MV block */
1504 s
->current_picture
.f
.motion_val
[0][xy
+ 1 + v
->blocks_off
][0] = 0;
1505 s
->current_picture
.f
.motion_val
[0][xy
+ 1 + v
->blocks_off
][1] = 0;
1506 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ v
->blocks_off
][0] = 0;
1507 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ v
->blocks_off
][1] = 0;
1508 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1 + v
->blocks_off
][0] = 0;
1509 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1 + v
->blocks_off
][1] = 0;
1510 v
->luma_mv
[s
->mb_x
][0] = v
->luma_mv
[s
->mb_x
][1] = 0;
1511 s
->current_picture
.f
.motion_val
[1][xy
+ 1 + v
->blocks_off
][0] = 0;
1512 s
->current_picture
.f
.motion_val
[1][xy
+ 1 + v
->blocks_off
][1] = 0;
1513 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
][0] = 0;
1514 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
+ v
->blocks_off
][1] = 0;
1515 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
+ 1 + v
->blocks_off
][0] = 0;
1516 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
+ 1 + v
->blocks_off
][1] = 0;
1521 C
= s
->current_picture
.f
.motion_val
[dir
][xy
- 1 + v
->blocks_off
];
1522 A
= s
->current_picture
.f
.motion_val
[dir
][xy
- wrap
+ v
->blocks_off
];
1524 if (v
->field_mode
&& mixedmv_pic
)
1525 off
= (s
->mb_x
== (s
->mb_width
- 1)) ?
-2 : 2;
1527 off
= (s
->mb_x
== (s
->mb_width
- 1)) ?
-1 : 2;
1529 //in 4-MV mode different blocks have different B predictor position
1532 off
= (s
->mb_x
> 0) ?
-1 : 1;
1535 off
= (s
->mb_x
== (s
->mb_width
- 1)) ?
-1 : 1;
1544 B
= s
->current_picture
.f
.motion_val
[dir
][xy
- wrap
+ off
+ v
->blocks_off
];
1546 a_valid
= !s
->first_slice_line
|| (n
== 2 || n
== 3);
1547 b_valid
= a_valid
&& (s
->mb_width
> 1);
1548 c_valid
= s
->mb_x
|| (n
== 1 || n
== 3);
1549 if (v
->field_mode
) {
1550 a_valid
= a_valid
&& !is_intra
[xy
- wrap
];
1551 b_valid
= b_valid
&& !is_intra
[xy
- wrap
+ off
];
1552 c_valid
= c_valid
&& !is_intra
[xy
- 1];
1556 a_f
= v
->mv_f
[dir
][xy
- wrap
+ v
->blocks_off
];
1557 num_oppfield
+= a_f
;
1558 num_samefield
+= 1 - a_f
;
1559 field_predA
[0] = A
[0];
1560 field_predA
[1] = A
[1];
1562 field_predA
[0] = field_predA
[1] = 0;
1566 b_f
= v
->mv_f
[dir
][xy
- wrap
+ off
+ v
->blocks_off
];
1567 num_oppfield
+= b_f
;
1568 num_samefield
+= 1 - b_f
;
1569 field_predB
[0] = B
[0];
1570 field_predB
[1] = B
[1];
1572 field_predB
[0] = field_predB
[1] = 0;
1576 c_f
= v
->mv_f
[dir
][xy
- 1 + v
->blocks_off
];
1577 num_oppfield
+= c_f
;
1578 num_samefield
+= 1 - c_f
;
1579 field_predC
[0] = C
[0];
1580 field_predC
[1] = C
[1];
1582 field_predC
[0] = field_predC
[1] = 0;
1586 if (v
->field_mode
) {
1587 if (num_samefield
<= num_oppfield
)
1588 opposit
= 1 - pred_flag
;
1590 opposit
= pred_flag
;
1594 if (a_valid
&& !a_f
) {
1595 field_predA
[0] = scaleforopp(v
, field_predA
[0], 0, dir
);
1596 field_predA
[1] = scaleforopp(v
, field_predA
[1], 1, dir
);
1598 if (b_valid
&& !b_f
) {
1599 field_predB
[0] = scaleforopp(v
, field_predB
[0], 0, dir
);
1600 field_predB
[1] = scaleforopp(v
, field_predB
[1], 1, dir
);
1602 if (c_valid
&& !c_f
) {
1603 field_predC
[0] = scaleforopp(v
, field_predC
[0], 0, dir
);
1604 field_predC
[1] = scaleforopp(v
, field_predC
[1], 1, dir
);
1606 v
->mv_f
[dir
][xy
+ v
->blocks_off
] = 1;
1607 v
->ref_field_type
[dir
] = !v
->cur_field_type
;
1609 if (a_valid
&& a_f
) {
1610 field_predA
[0] = scaleforsame(v
, n
, field_predA
[0], 0, dir
);
1611 field_predA
[1] = scaleforsame(v
, n
, field_predA
[1], 1, dir
);
1613 if (b_valid
&& b_f
) {
1614 field_predB
[0] = scaleforsame(v
, n
, field_predB
[0], 0, dir
);
1615 field_predB
[1] = scaleforsame(v
, n
, field_predB
[1], 1, dir
);
1617 if (c_valid
&& c_f
) {
1618 field_predC
[0] = scaleforsame(v
, n
, field_predC
[0], 0, dir
);
1619 field_predC
[1] = scaleforsame(v
, n
, field_predC
[1], 1, dir
);
1621 v
->mv_f
[dir
][xy
+ v
->blocks_off
] = 0;
1622 v
->ref_field_type
[dir
] = v
->cur_field_type
;
1626 px
= field_predA
[0];
1627 py
= field_predA
[1];
1628 } else if (c_valid
) {
1629 px
= field_predC
[0];
1630 py
= field_predC
[1];
1631 } else if (b_valid
) {
1632 px
= field_predB
[0];
1633 py
= field_predB
[1];
1639 if (num_samefield
+ num_oppfield
> 1) {
1640 px
= mid_pred(field_predA
[0], field_predB
[0], field_predC
[0]);
1641 py
= mid_pred(field_predA
[1], field_predB
[1], field_predC
[1]);
1644 /* Pullback MV as specified in 8.3.5.3.4 */
1645 if (!v
->field_mode
) {
1647 qx
= (s
->mb_x
<< 6) + ((n
== 1 || n
== 3) ?
32 : 0);
1648 qy
= (s
->mb_y
<< 6) + ((n
== 2 || n
== 3) ?
32 : 0);
1649 X
= (s
->mb_width
<< 6) - 4;
1650 Y
= (s
->mb_height
<< 6) - 4;
1652 if (qx
+ px
< -60) px
= -60 - qx
;
1653 if (qy
+ py
< -60) py
= -60 - qy
;
1655 if (qx
+ px
< -28) px
= -28 - qx
;
1656 if (qy
+ py
< -28) py
= -28 - qy
;
1658 if (qx
+ px
> X
) px
= X
- qx
;
1659 if (qy
+ py
> Y
) py
= Y
- qy
;
1662 if (!v
->field_mode
|| s
->pict_type
!= AV_PICTURE_TYPE_B
) {
1663 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1664 hybridmv_thresh
= 32;
1665 if (a_valid
&& c_valid
) {
1666 if (is_intra
[xy
- wrap
])
1667 sum
= FFABS(px
) + FFABS(py
);
1669 sum
= FFABS(px
- field_predA
[0]) + FFABS(py
- field_predA
[1]);
1670 if (sum
> hybridmv_thresh
) {
1671 if (get_bits1(&s
->gb
)) { // read HYBRIDPRED bit
1672 px
= field_predA
[0];
1673 py
= field_predA
[1];
1675 px
= field_predC
[0];
1676 py
= field_predC
[1];
1679 if (is_intra
[xy
- 1])
1680 sum
= FFABS(px
) + FFABS(py
);
1682 sum
= FFABS(px
- field_predC
[0]) + FFABS(py
- field_predC
[1]);
1683 if (sum
> hybridmv_thresh
) {
1684 if (get_bits1(&s
->gb
)) {
1685 px
= field_predA
[0];
1686 py
= field_predA
[1];
1688 px
= field_predC
[0];
1689 py
= field_predC
[1];
1696 if (v
->field_mode
&& !s
->quarter_sample
) {
1700 if (v
->field_mode
&& v
->numref
)
1702 if (v
->field_mode
&& v
->cur_field_type
&& v
->ref_field_type
[dir
] == 0)
1704 /* store MV using signed modulus of MV range defined in 4.11 */
1705 s
->mv
[dir
][n
][0] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][0] = ((px
+ dmv_x
+ r_x
) & ((r_x
<< 1) - 1)) - r_x
;
1706 s
->mv
[dir
][n
][1] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][1] = ((py
+ dmv_y
+ r_y
- y_bias
) & ((r_y
<< 1) - 1)) - r_y
+ y_bias
;
1707 if (mv1
) { /* duplicate motion data for 1-MV block */
1708 s
->current_picture
.f
.motion_val
[dir
][xy
+ 1 + v
->blocks_off
][0] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][0];
1709 s
->current_picture
.f
.motion_val
[dir
][xy
+ 1 + v
->blocks_off
][1] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][1];
1710 s
->current_picture
.f
.motion_val
[dir
][xy
+ wrap
+ v
->blocks_off
][0] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][0];
1711 s
->current_picture
.f
.motion_val
[dir
][xy
+ wrap
+ v
->blocks_off
][1] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][1];
1712 s
->current_picture
.f
.motion_val
[dir
][xy
+ wrap
+ 1 + v
->blocks_off
][0] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][0];
1713 s
->current_picture
.f
.motion_val
[dir
][xy
+ wrap
+ 1 + v
->blocks_off
][1] = s
->current_picture
.f
.motion_val
[dir
][xy
+ v
->blocks_off
][1];
1714 v
->mv_f
[dir
][xy
+ 1 + v
->blocks_off
] = v
->mv_f
[dir
][xy
+ v
->blocks_off
];
1715 v
->mv_f
[dir
][xy
+ wrap
+ v
->blocks_off
] = v
->mv_f
[dir
][xy
+ wrap
+ 1 + v
->blocks_off
] = v
->mv_f
[dir
][xy
+ v
->blocks_off
];
1719 /** Predict and set motion vector for interlaced frame picture MBs
1721 static inline void vc1_pred_mv_intfr(VC1Context
*v
, int n
, int dmv_x
, int dmv_y
,
1722 int mvn
, int r_x
, int r_y
, uint8_t* is_intra
)
1724 MpegEncContext
*s
= &v
->s
;
1725 int xy
, wrap
, off
= 0;
1726 int A
[2], B
[2], C
[2];
1728 int a_valid
= 0, b_valid
= 0, c_valid
= 0;
1729 int field_a
, field_b
, field_c
; // 0: same, 1: opposit
1730 int total_valid
, num_samefield
, num_oppfield
;
1731 int pos_c
, pos_b
, n_adj
;
1733 wrap
= s
->b8_stride
;
1734 xy
= s
->block_index
[n
];
1737 s
->mv
[0][n
][0] = s
->current_picture
.f
.motion_val
[0][xy
][0] = 0;
1738 s
->mv
[0][n
][1] = s
->current_picture
.f
.motion_val
[0][xy
][1] = 0;
1739 s
->current_picture
.f
.motion_val
[1][xy
][0] = 0;
1740 s
->current_picture
.f
.motion_val
[1][xy
][1] = 0;
1741 if (mvn
== 1) { /* duplicate motion data for 1-MV block */
1742 s
->current_picture
.f
.motion_val
[0][xy
+ 1][0] = 0;
1743 s
->current_picture
.f
.motion_val
[0][xy
+ 1][1] = 0;
1744 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
][0] = 0;
1745 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
][1] = 0;
1746 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1][0] = 0;
1747 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1][1] = 0;
1748 v
->luma_mv
[s
->mb_x
][0] = v
->luma_mv
[s
->mb_x
][1] = 0;
1749 s
->current_picture
.f
.motion_val
[1][xy
+ 1][0] = 0;
1750 s
->current_picture
.f
.motion_val
[1][xy
+ 1][1] = 0;
1751 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
][0] = 0;
1752 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
][1] = 0;
1753 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
+ 1][0] = 0;
1754 s
->current_picture
.f
.motion_val
[1][xy
+ wrap
+ 1][1] = 0;
1759 off
= ((n
== 0) || (n
== 1)) ?
1 : -1;
1761 if (s
->mb_x
|| (n
== 1) || (n
== 3)) {
1762 if ((v
->blk_mv_type
[xy
]) // current block (MB) has a field MV
1763 || (!v
->blk_mv_type
[xy
] && !v
->blk_mv_type
[xy
- 1])) { // or both have frame MV
1764 A
[0] = s
->current_picture
.f
.motion_val
[0][xy
- 1][0];
1765 A
[1] = s
->current_picture
.f
.motion_val
[0][xy
- 1][1];
1767 } else { // current block has frame mv and cand. has field MV (so average)
1768 A
[0] = (s
->current_picture
.f
.motion_val
[0][xy
- 1][0]
1769 + s
->current_picture
.f
.motion_val
[0][xy
- 1 + off
* wrap
][0] + 1) >> 1;
1770 A
[1] = (s
->current_picture
.f
.motion_val
[0][xy
- 1][1]
1771 + s
->current_picture
.f
.motion_val
[0][xy
- 1 + off
* wrap
][1] + 1) >> 1;
1774 if (!(n
& 1) && v
->is_intra
[s
->mb_x
- 1]) {
1780 /* Predict B and C */
1781 B
[0] = B
[1] = C
[0] = C
[1] = 0;
1782 if (n
== 0 || n
== 1 || v
->blk_mv_type
[xy
]) {
1783 if (!s
->first_slice_line
) {
1784 if (!v
->is_intra
[s
->mb_x
- s
->mb_stride
]) {
1787 pos_b
= s
->block_index
[n_adj
] - 2 * wrap
;
1788 if (v
->blk_mv_type
[pos_b
] && v
->blk_mv_type
[xy
]) {
1789 n_adj
= (n
& 2) | (n
& 1);
1791 B
[0] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
][0];
1792 B
[1] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
][1];
1793 if (v
->blk_mv_type
[pos_b
] && !v
->blk_mv_type
[xy
]) {
1794 B
[0] = (B
[0] + s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
^ 2] - 2 * wrap
][0] + 1) >> 1;
1795 B
[1] = (B
[1] + s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
^ 2] - 2 * wrap
][1] + 1) >> 1;
1798 if (s
->mb_width
> 1) {
1799 if (!v
->is_intra
[s
->mb_x
- s
->mb_stride
+ 1]) {
1802 pos_c
= s
->block_index
[2] - 2 * wrap
+ 2;
1803 if (v
->blk_mv_type
[pos_c
] && v
->blk_mv_type
[xy
]) {
1806 C
[0] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
+ 2][0];
1807 C
[1] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
+ 2][1];
1808 if (v
->blk_mv_type
[pos_c
] && !v
->blk_mv_type
[xy
]) {
1809 C
[0] = (1 + C
[0] + (s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
^ 2] - 2 * wrap
+ 2][0])) >> 1;
1810 C
[1] = (1 + C
[1] + (s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
^ 2] - 2 * wrap
+ 2][1])) >> 1;
1812 if (s
->mb_x
== s
->mb_width
- 1) {
1813 if (!v
->is_intra
[s
->mb_x
- s
->mb_stride
- 1]) {
1816 pos_c
= s
->block_index
[3] - 2 * wrap
- 2;
1817 if (v
->blk_mv_type
[pos_c
] && v
->blk_mv_type
[xy
]) {
1820 C
[0] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
- 2][0];
1821 C
[1] = s
->current_picture
.f
.motion_val
[0][s
->block_index
[n_adj
] - 2 * wrap
- 2][1];
1822 if (v
->blk_mv_type
[pos_c
] && !v
->blk_mv_type
[xy
]) {
1823 C
[0] = (1 + C
[0] + s
->current_picture
.f
.motion_val
[0][s
->block_index
[1] - 2 * wrap
- 2][0]) >> 1;
1824 C
[1] = (1 + C
[1] + s
->current_picture
.f
.motion_val
[0][s
->block_index
[1] - 2 * wrap
- 2][1]) >> 1;
1833 pos_b
= s
->block_index
[1];
1835 B
[0] = s
->current_picture
.f
.motion_val
[0][pos_b
][0];
1836 B
[1] = s
->current_picture
.f
.motion_val
[0][pos_b
][1];
1837 pos_c
= s
->block_index
[0];
1839 C
[0] = s
->current_picture
.f
.motion_val
[0][pos_c
][0];
1840 C
[1] = s
->current_picture
.f
.motion_val
[0][pos_c
][1];
1843 total_valid
= a_valid
+ b_valid
+ c_valid
;
1844 // check if predictor A is out of bounds
1845 if (!s
->mb_x
&& !(n
== 1 || n
== 3)) {
1848 // check if predictor B is out of bounds
1849 if ((s
->first_slice_line
&& v
->blk_mv_type
[xy
]) || (s
->first_slice_line
&& !(n
& 2))) {
1850 B
[0] = B
[1] = C
[0] = C
[1] = 0;
1852 if (!v
->blk_mv_type
[xy
]) {
1853 if (s
->mb_width
== 1) {
1857 if (total_valid
>= 2) {
1858 px
= mid_pred(A
[0], B
[0], C
[0]);
1859 py
= mid_pred(A
[1], B
[1], C
[1]);
1860 } else if (total_valid
) {
1861 if (a_valid
) { px
= A
[0]; py
= A
[1]; }
1862 if (b_valid
) { px
= B
[0]; py
= B
[1]; }
1863 if (c_valid
) { px
= C
[0]; py
= C
[1]; }
1869 field_a
= (A
[1] & 4) ?
1 : 0;
1873 field_b
= (B
[1] & 4) ?
1 : 0;
1877 field_c
= (C
[1] & 4) ?
1 : 0;
1881 num_oppfield
= field_a
+ field_b
+ field_c
;
1882 num_samefield
= total_valid
- num_oppfield
;
1883 if (total_valid
== 3) {
1884 if ((num_samefield
== 3) || (num_oppfield
== 3)) {
1885 px
= mid_pred(A
[0], B
[0], C
[0]);
1886 py
= mid_pred(A
[1], B
[1], C
[1]);
1887 } else if (num_samefield
>= num_oppfield
) {
1888 /* take one MV from same field set depending on priority
1889 the check for B may not be necessary */
1890 px
= !field_a ? A
[0] : B
[0];
1891 py
= !field_a ? A
[1] : B
[1];
1893 px
= field_a ? A
[0] : B
[0];
1894 py
= field_a ? A
[1] : B
[1];
1896 } else if (total_valid
== 2) {
1897 if (num_samefield
>= num_oppfield
) {
1898 if (!field_a
&& a_valid
) {
1901 } else if (!field_b
&& b_valid
) {
1904 } else if (c_valid
) {
1909 if (field_a
&& a_valid
) {
1912 } else if (field_b
&& b_valid
) {
1915 } else if (c_valid
) {
1920 } else if (total_valid
== 1) {
1921 px
= (a_valid
) ? A
[0] : ((b_valid
) ? B
[0] : C
[0]);
1922 py
= (a_valid
) ? A
[1] : ((b_valid
) ? B
[1] : C
[1]);
1927 /* store MV using signed modulus of MV range defined in 4.11 */
1928 s
->mv
[0][n
][0] = s
->current_picture
.f
.motion_val
[0][xy
][0] = ((px
+ dmv_x
+ r_x
) & ((r_x
<< 1) - 1)) - r_x
;
1929 s
->mv
[0][n
][1] = s
->current_picture
.f
.motion_val
[0][xy
][1] = ((py
+ dmv_y
+ r_y
) & ((r_y
<< 1) - 1)) - r_y
;
1930 if (mvn
== 1) { /* duplicate motion data for 1-MV block */
1931 s
->current_picture
.f
.motion_val
[0][xy
+ 1 ][0] = s
->current_picture
.f
.motion_val
[0][xy
][0];
1932 s
->current_picture
.f
.motion_val
[0][xy
+ 1 ][1] = s
->current_picture
.f
.motion_val
[0][xy
][1];
1933 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
][0] = s
->current_picture
.f
.motion_val
[0][xy
][0];
1934 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
][1] = s
->current_picture
.f
.motion_val
[0][xy
][1];
1935 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1][0] = s
->current_picture
.f
.motion_val
[0][xy
][0];
1936 s
->current_picture
.f
.motion_val
[0][xy
+ wrap
+ 1][1] = s
->current_picture
.f
.motion_val
[0][xy
][1];
1937 } else if (mvn
== 2) { /* duplicate motion data for 2-Field MV block */
1938 s
->current_picture
.f
.motion_val
[0][xy
+ 1][0] = s
->current_picture
.f
.motion_val
[0][xy
][0];
1939 s
->current_picture
.f
.motion_val
[0][xy
+ 1][1] = s
->current_picture
.f
.motion_val
[0][xy
][1];
1940 s
->mv
[0][n
+ 1][0] = s
->mv
[0][n
][0];
1941 s
->mv
[0][n
+ 1][1] = s
->mv
[0][n
][1];
1945 /** Motion compensation for direct or interpolated blocks in B-frames
1947 static void vc1_interp_mc(VC1Context
*v
)
1949 MpegEncContext
*s
= &v
->s
;
1950 DSPContext
*dsp
= &v
->s
.dsp
;
1951 uint8_t *srcY
, *srcU
, *srcV
;
1952 int dxy
, mx
, my
, uvmx
, uvmy
, src_x
, src_y
, uvsrc_x
, uvsrc_y
;
1954 int v_edge_pos
= s
->v_edge_pos
>> v
->field_mode
;
1956 if (!v
->field_mode
&& !v
->s
.next_picture
.f
.data
[0])
1959 mx
= s
->mv
[1][0][0];
1960 my
= s
->mv
[1][0][1];
1961 uvmx
= (mx
+ ((mx
& 3) == 3)) >> 1;
1962 uvmy
= (my
+ ((my
& 3) == 3)) >> 1;
1963 if (v
->field_mode
) {
1964 if (v
->cur_field_type
!= v
->ref_field_type
[1])
1965 my
= my
- 2 + 4 * v
->cur_field_type
;
1966 uvmy
= uvmy
- 2 + 4 * v
->cur_field_type
;
1969 uvmx
= uvmx
+ ((uvmx
< 0) ?
-(uvmx
& 1) : (uvmx
& 1));
1970 uvmy
= uvmy
+ ((uvmy
< 0) ?
-(uvmy
& 1) : (uvmy
& 1));
1972 srcY
= s
->next_picture
.f
.data
[0];
1973 srcU
= s
->next_picture
.f
.data
[1];
1974 srcV
= s
->next_picture
.f
.data
[2];
1976 src_x
= s
->mb_x
* 16 + (mx
>> 2);
1977 src_y
= s
->mb_y
* 16 + (my
>> 2);
1978 uvsrc_x
= s
->mb_x
* 8 + (uvmx
>> 2);
1979 uvsrc_y
= s
->mb_y
* 8 + (uvmy
>> 2);
1981 if (v
->profile
!= PROFILE_ADVANCED
) {
1982 src_x
= av_clip( src_x
, -16, s
->mb_width
* 16);
1983 src_y
= av_clip( src_y
, -16, s
->mb_height
* 16);
1984 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->mb_width
* 8);
1985 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->mb_height
* 8);
1987 src_x
= av_clip( src_x
, -17, s
->avctx
->coded_width
);
1988 src_y
= av_clip( src_y
, -18, s
->avctx
->coded_height
+ 1);
1989 uvsrc_x
= av_clip(uvsrc_x
, -8, s
->avctx
->coded_width
>> 1);
1990 uvsrc_y
= av_clip(uvsrc_y
, -8, s
->avctx
->coded_height
>> 1);
1993 srcY
+= src_y
* s
->linesize
+ src_x
;
1994 srcU
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1995 srcV
+= uvsrc_y
* s
->uvlinesize
+ uvsrc_x
;
1997 if (v
->field_mode
&& v
->ref_field_type
[1]) {
1998 srcY
+= s
->current_picture_ptr
->f
.linesize
[0];
1999 srcU
+= s
->current_picture_ptr
->f
.linesize
[1];
2000 srcV
+= s
->current_picture_ptr
->f
.linesize
[2];
2003 /* for grayscale we should not try to read from unknown area */
2004 if (s
->flags
& CODEC_FLAG_GRAY
) {
2005 srcU
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
2006 srcV
= s
->edge_emu_buffer
+ 18 * s
->linesize
;
2010 || (unsigned)(src_x
- s
->mspel
) > s
->h_edge_pos
- (mx
& 3) - 16 - s
->mspel
* 3
2011 || (unsigned)(src_y
- s
->mspel
) > v_edge_pos
- (my
& 3) - 16 - s
->mspel
* 3) {
2012 uint8_t *uvbuf
= s
->edge_emu_buffer
+ 19 * s
->linesize
;
2014 srcY
-= s
->mspel
* (1 + s
->linesize
);
2015 s
->dsp
.emulated_edge_mc(s
->edge_emu_buffer
, srcY
, s
->linesize
,
2016 17 + s
->mspel
* 2, 17 + s
->mspel
* 2,
2017 src_x
- s
->mspel
, src_y
- s
->mspel
,
2018 s
->h_edge_pos
, v_edge_pos
);
2019 srcY
= s
->edge_emu_buffer
;
2020 s
->dsp
.emulated_edge_mc(uvbuf
, srcU
, s
->uvlinesize
, 8 + 1, 8 + 1,
2021 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
2022 s
->dsp
.emulated_edge_mc(uvbuf
+ 16, srcV
, s
->uvlinesize
, 8 + 1, 8 + 1,
2023 uvsrc_x
, uvsrc_y
, s
->h_edge_pos
>> 1, v_edge_pos
>> 1);
2026 /* if we deal with range reduction we need to scale source blocks */
2027 if (v
->rangeredfrm
) {
2029 uint8_t *src
, *src2
;
2032 for (j
= 0; j
< 17 + s
->mspel
* 2; j
++) {
2033 for (i
= 0; i
< 17 + s
->mspel
* 2; i
++)
2034 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
2039 for (j
= 0; j
< 9; j
++) {
2040 for (i
= 0; i
< 9; i
++) {
2041 src
[i
] = ((src
[i
] - 128) >> 1) + 128;
2042 src2
[i
] = ((src2
[i
] - 128) >> 1) + 128;
2044 src
+= s
->uvlinesize
;
2045 src2
+= s
->uvlinesize
;
2048 srcY
+= s
->mspel
* (1 + s
->linesize
);
2051 if (v
->field_mode
&& v
->cur_field_type
) {
2052 off
= s
->current_picture_ptr
->f
.linesize
[0];
2053 off_uv
= s
->current_picture_ptr
->f
.linesize
[1];
2060 dxy
= ((my
& 3) << 2) | (mx
& 3);
2061 v
->vc1dsp
.avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, v
->rnd
);
2062 v
->vc1dsp
.avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
2063 srcY
+= s
->linesize
* 8;
2064 v
->vc1dsp
.avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8 * s
->linesize
, srcY
, s
->linesize
, v
->rnd
);
2065 v
->vc1dsp
.avg_vc1_mspel_pixels_tab
[dxy
](s
->dest
[0] + off
+ 8 * s
->linesize
+ 8, srcY
+ 8, s
->linesize
, v
->rnd
);
2067 dxy
= (my
& 2) | ((mx
& 2) >> 1);
2070 dsp
->avg_pixels_tab
[0][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 16);
2072 dsp
->avg_no_rnd_pixels_tab
[0][dxy
](s
->dest
[0] + off
, srcY
, s
->linesize
, 16);
2075 if (s
->flags
& CODEC_FLAG_GRAY
) return;
2076 /* Chroma MC always uses qpel blilinear */
2077 uvmx
= (uvmx
& 3) << 1;
2078 uvmy
= (uvmy
& 3) << 1;
2080 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[1] + off_uv
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
2081 dsp
->avg_h264_chroma_pixels_tab
[0](s
->dest
[2] + off_uv
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
2083 v
->vc1dsp
.avg_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[1] + off_uv
, srcU
, s
->uvlinesize
, 8, uvmx
, uvmy
);
2084 v
->vc1dsp
.avg_no_rnd_vc1_chroma_pixels_tab
[0](s
->dest
[2] + off_uv
, srcV
, s
->uvlinesize
, 8, uvmx
, uvmy
);
2088 static av_always_inline
int scale_mv(int value
, int bfrac
, int inv
, int qs
)
2092 #if B_FRACTION_DEN==256
2096 return 2 * ((value
* n
+ 255) >> 9);
2097 return (value
* n
+ 128) >> 8;
2100 n
-= B_FRACTION_DEN
;
2102 return 2 * ((value
* n
+ B_FRACTION_DEN
- 1) / (2 * B_FRACTION_DEN
));
2103 return (value
* n
+ B_FRACTION_DEN
/2) / B_FRACTION_DEN
;
2107 static av_always_inline
int scale_mv_intfi(int value
, int bfrac
, int inv
,
2108 int qs
, int qs_last
)
2116 return (value
* n
+ 255) >> 9;
2118 return (value
* n
+ 128) >> 8;
2121 /** Reconstruct motion vector for B-frame and do motion compensation
2123 static inline void vc1_b_mc(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2],
2124 int direct
, int mode
)
2127 v
->mv_mode2
= v
->mv_mode
;
2128 v
->mv_mode
= MV_PMODE_INTENSITY_COMP
;
2134 v
->mv_mode
= v
->mv_mode2
;
2137 if (mode
== BMV_TYPE_INTERPOLATED
) {
2141 v
->mv_mode
= v
->mv_mode2
;
2145 if (v
->use_ic
&& (mode
== BMV_TYPE_BACKWARD
))
2146 v
->mv_mode
= v
->mv_mode2
;
2147 vc1_mc_1mv(v
, (mode
== BMV_TYPE_BACKWARD
));
2149 v
->mv_mode
= v
->mv_mode2
;
2152 static inline void vc1_pred_b_mv(VC1Context
*v
, int dmv_x
[2], int dmv_y
[2],
2153 int direct
, int mvtype
)
2155 MpegEncContext
*s
= &v
->s
;
2156 int xy
, wrap
, off
= 0;
2161 const uint8_t *is_intra
= v
->mb_type
[0];
2165 /* scale MV difference to be quad-pel */
2166 dmv_x
[0] <<= 1 - s
->quarter_sample
;
2167 dmv_y
[0] <<= 1 - s
->quarter_sample
;
2168 dmv_x
[1] <<= 1 - s
->quarter_sample
;
2169 dmv_y
[1] <<= 1 - s
->quarter_sample
;
2171 wrap
= s
->b8_stride
;
2172 xy
= s
->block_index
[0];
2175 s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][0] =
2176 s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][1] =
2177 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][0] =
2178 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][1] = 0;
2181 if (!v
->field_mode
) {
2182 s
->mv
[0][0][0] = scale_mv(s
->next_picture
.f
.motion_val
[1][xy
][0], v
->bfraction
, 0, s
->quarter_sample
);
2183 s
->mv
[0][0][1] = scale_mv(s
->next_picture
.f
.motion_val
[1][xy
][1], v
->bfraction
, 0, s
->quarter_sample
);
2184 s
->mv
[1][0][0] = scale_mv(s
->next_picture
.f
.motion_val
[1][xy
][0], v
->bfraction
, 1, s
->quarter_sample
);
2185 s
->mv
[1][0][1] = scale_mv(s
->next_picture
.f
.motion_val
[1][xy
][1], v
->bfraction
, 1, s
->quarter_sample
);
2187 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2188 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));
2189 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));
2190 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));
2191 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));
2194 s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][0] = s
->mv
[0][0][0];
2195 s
->current_picture
.f
.motion_val
[0][xy
+ v
->blocks_off
][1] = s
->mv
[0][0][1];
2196 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][0] = s
->mv
[1][0][0];
2197 s
->current_picture
.f
.motion_val
[1][xy
+ v
->blocks_off
][1] = s
->mv
[1][0][1];
2201 if ((mvtype
== BMV_TYPE_FORWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
2202 C
= s
->current_picture
.f
.motion_val
[0][xy
- 2];
2203 A
= s
->current_picture
.f
.motion_val
[0][xy
- wrap
* 2];
2204 off
= (s
->mb_x
== (s
->mb_width
- 1)) ?
-2 : 2;
2205 B
= s
->current_picture
.f
.motion_val
[0][xy
- wrap
* 2 + off
];
2207 if (!s
->mb_x
) C
[0] = C
[1] = 0;
2208 if (!s
->first_slice_line
) { // predictor A is not out of bounds
2209 if (s
->mb_width
== 1) {
2213 px
= mid_pred(A
[0], B
[0], C
[0]);
2214 py
= mid_pred(A
[1], B
[1], C
[1]);
2216 } else if (s
->mb_x
) { // predictor C is not out of bounds
2222 /* Pullback MV as specified in 8.3.5.3.4 */
2225 if (v
->profile
< PROFILE_ADVANCED
) {
2226 qx
= (s
->mb_x
<< 5);
2227 qy
= (s
->mb_y
<< 5);
2228 X
= (s
->mb_width
<< 5) - 4;
2229 Y
= (s
->mb_height
<< 5) - 4;
2230 if (qx
+ px
< -28) px
= -28 - qx
;
2231 if (qy
+ py
< -28) py
= -28 - qy
;
2232 if (qx
+ px
> X
) px
= X
- qx
;
2233 if (qy
+ py
> Y
) py
= Y
- qy
;
2235 qx
= (s
->mb_x
<< 6);
2236 qy
= (s
->mb_y
<< 6);
2237 X
= (s
->mb_width
<< 6) - 4;
2238 Y
= (s
->mb_height
<< 6) - 4;
2239 if (qx
+ px
< -60) px
= -60 - qx
;
2240 if (qy
+ py
< -60) py
= -60 - qy
;
2241 if (qx
+ px
> X
) px
= X
- qx
;
2242 if (qy
+ py
> Y
) py
= Y
- qy
;
2245 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2246 if (0 && !s
->first_slice_line
&& s
->mb_x
) {
2247 if (is_intra
[xy
- wrap
])
2248 sum
= FFABS(px
) + FFABS(py
);
2250 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
2252 if (get_bits1(&s
->gb
)) {
2260 if (is_intra
[xy
- 2])
2261 sum
= FFABS(px
) + FFABS(py
);
2263 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
2265 if (get_bits1(&s
->gb
)) {
2275 /* store MV using signed modulus of MV range defined in 4.11 */
2276 s
->mv
[0][0][0] = ((px
+ dmv_x
[0] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
2277 s
->mv
[0][0][1] = ((py
+ dmv_y
[0] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
2279 if ((mvtype
== BMV_TYPE_BACKWARD
) || (mvtype
== BMV_TYPE_INTERPOLATED
)) {
2280 C
= s
->current_picture
.f
.motion_val
[1][xy
- 2];
2281 A
= s
->current_picture
.f
.motion_val
[1][xy
- wrap
* 2];
2282 off
= (s
->mb_x
== (s
->mb_width
- 1)) ?
-2 : 2;
2283 B
= s
->current_picture
.f
.motion_val
[1][xy
- wrap
* 2 + off
];
2287 if (!s
->first_slice_line
) { // predictor A is not out of bounds
2288 if (s
->mb_width
== 1) {
2292 px
= mid_pred(A
[0], B
[0], C
[0]);
2293 py
= mid_pred(A
[1], B
[1], C
[1]);
2295 } else if (s
->mb_x
) { // predictor C is not out of bounds
2301 /* Pullback MV as specified in 8.3.5.3.4 */
2304 if (v
->profile
< PROFILE_ADVANCED
) {
2305 qx
= (s
->mb_x
<< 5);
2306 qy
= (s
->mb_y
<< 5);
2307 X
= (s
->mb_width
<< 5) - 4;
2308 Y
= (s
->mb_height
<< 5) - 4;
2309 if (qx
+ px
< -28) px
= -28 - qx
;
2310 if (qy
+ py
< -28) py
= -28 - qy
;
2311 if (qx
+ px
> X
) px
= X
- qx
;
2312 if (qy
+ py
> Y
) py
= Y
- qy
;
2314 qx
= (s
->mb_x
<< 6);
2315 qy
= (s
->mb_y
<< 6);
2316 X
= (s
->mb_width
<< 6) - 4;
2317 Y
= (s
->mb_height
<< 6) - 4;
2318 if (qx
+ px
< -60) px
= -60 - qx
;
2319 if (qy
+ py
< -60) py
= -60 - qy
;
2320 if (qx
+ px
> X
) px
= X
- qx
;
2321 if (qy
+ py
> Y
) py
= Y
- qy
;
2324 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2325 if (0 && !s
->first_slice_line
&& s
->mb_x
) {
2326 if (is_intra
[xy
- wrap
])
2327 sum
= FFABS(px
) + FFABS(py
);
2329 sum
= FFABS(px
- A
[0]) + FFABS(py
- A
[1]);
2331 if (get_bits1(&s
->gb
)) {
2339 if (is_intra
[xy
- 2])
2340 sum
= FFABS(px
) + FFABS(py
);
2342 sum
= FFABS(px
- C
[0]) + FFABS(py
- C
[1]);
2344 if (get_bits1(&s
->gb
)) {
2354 /* store MV using signed modulus of MV range defined in 4.11 */
2356 s
->mv
[1][0][0] = ((px
+ dmv_x
[1] + r_x
) & ((r_x
<< 1) - 1)) - r_x
;
2357 s
->mv
[1][0][1] = ((py
+ dmv_y
[1] + r_y
) & ((r_y
<< 1) - 1)) - r_y
;
2359 s
->current_picture
.f
.motion_val
[0][xy
][0] = s
->mv
[0][0][0];
2360 s
->current_picture
.f
.motion_val
[0][xy
][1] = s
->mv
[0][0][1];
2361 s
->current_picture
.f
.motion_val
[1][xy
][0] = s
->mv
[1][0][0];
2362 s
->current_picture
.f
.motion_val
[1][xy
][1] = s
->mv
[1][0][1];
2365 static inline void vc1_pred_b_mv_intfi(VC1Context
*v
, int n
, int *dmv_x
, int *dmv_y
, int mv1
, int *pred_flag
)
2367 int dir
= (v
->bmvtype
== BMV_TYPE_BACKWARD
) ?
1 : 0;
2368 MpegEncContext
*s
= &v
->s
;
2369 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2371 if (v
->bmvtype
== BMV_TYPE_DIRECT
) {
2372 int total_opp
, k
, f
;
2373 if (s
->next_picture
.f
.mb_type
[mb_pos
+ v
->mb_off
] != MB_TYPE_INTRA
) {
2374 s
->mv
[0][0][0] = scale_mv_intfi(s
->next_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0],
2375 v
->bfraction
, 0, s
->quarter_sample
, v
->qs_last
);
2376 s
->mv
[0][0][1] = scale_mv_intfi(s
->next_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1],
2377 v
->bfraction
, 0, s
->quarter_sample
, v
->qs_last
);
2378 s
->mv
[1][0][0] = scale_mv_intfi(s
->next_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][0],
2379 v
->bfraction
, 1, s
->quarter_sample
, v
->qs_last
);
2380 s
->mv
[1][0][1] = scale_mv_intfi(s
->next_picture
.f
.motion_val
[1][s
->block_index
[0] + v
->blocks_off
][1],
2381 v
->bfraction
, 1, s
->quarter_sample
, v
->qs_last
);
2383 total_opp
= v
->mv_f_next
[0][s
->block_index
[0] + v
->blocks_off
]
2384 + v
->mv_f_next
[0][s
->block_index
[1] + v
->blocks_off
]
2385 + v
->mv_f_next
[0][s
->block_index
[2] + v
->blocks_off
]
2386 + v
->mv_f_next
[0][s
->block_index
[3] + v
->blocks_off
];
2387 f
= (total_opp
> 2) ?
1 : 0;
2389 s
->mv
[0][0][0] = s
->mv
[0][0][1] = 0;
2390 s
->mv
[1][0][0] = s
->mv
[1][0][1] = 0;
2393 v
->ref_field_type
[0] = v
->ref_field_type
[1] = v
->cur_field_type
^ f
;
2394 for (k
= 0; k
< 4; k
++) {
2395 s
->current_picture
.f
.motion_val
[0][s
->block_index
[k
] + v
->blocks_off
][0] = s
->mv
[0][0][0];
2396 s
->current_picture
.f
.motion_val
[0][s
->block_index
[k
] + v
->blocks_off
][1] = s
->mv
[0][0][1];
2397 s
->current_picture
.f
.motion_val
[1][s
->block_index
[k
] + v
->blocks_off
][0] = s
->mv
[1][0][0];
2398 s
->current_picture
.f
.motion_val
[1][s
->block_index
[k
] + v
->blocks_off
][1] = s
->mv
[1][0][1];
2399 v
->mv_f
[0][s
->block_index
[k
] + v
->blocks_off
] = f
;
2400 v
->mv_f
[1][s
->block_index
[k
] + v
->blocks_off
] = f
;
2404 if (v
->bmvtype
== BMV_TYPE_INTERPOLATED
) {
2405 vc1_pred_mv(v
, 0, dmv_x
[0], dmv_y
[0], 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
[0], 0);
2406 vc1_pred_mv(v
, 0, dmv_x
[1], dmv_y
[1], 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
[1], 1);
2409 if (dir
) { // backward
2410 vc1_pred_mv(v
, n
, dmv_x
[1], dmv_y
[1], mv1
, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
[1], 1);
2411 if (n
== 3 || mv1
) {
2412 vc1_pred_mv(v
, 0, dmv_x
[0], dmv_y
[0], 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 0);
2415 vc1_pred_mv(v
, n
, dmv_x
[0], dmv_y
[0], mv1
, v
->range_x
, v
->range_y
, v
->mb_type
[0], pred_flag
[0], 0);
2416 if (n
== 3 || mv1
) {
2417 vc1_pred_mv(v
, 0, dmv_x
[1], dmv_y
[1], 1, v
->range_x
, v
->range_y
, v
->mb_type
[0], 0, 1);
2422 /** Get predicted DC value for I-frames only
2423 * prediction dir: left=0, top=1
2424 * @param s MpegEncContext
2425 * @param overlap flag indicating that overlap filtering is used
2426 * @param pq integer part of picture quantizer
2427 * @param[in] n block index in the current MB
2428 * @param dc_val_ptr Pointer to DC predictor
2429 * @param dir_ptr Prediction direction for use in AC prediction
2431 static inline int vc1_i_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
2432 int16_t **dc_val_ptr
, int *dir_ptr
)
2434 int a
, b
, c
, wrap
, pred
, scale
;
2436 static const uint16_t dcpred
[32] = {
2437 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2438 114, 102, 93, 85, 79, 73, 68, 64,
2439 60, 57, 54, 51, 49, 47, 45, 43,
2440 41, 39, 38, 37, 35, 34, 33
2443 /* find prediction - wmv3_dc_scale always used here in fact */
2444 if (n
< 4) scale
= s
->y_dc_scale
;
2445 else scale
= s
->c_dc_scale
;
2447 wrap
= s
->block_wrap
[n
];
2448 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
2454 b
= dc_val
[ - 1 - wrap
];
2455 a
= dc_val
[ - wrap
];
2457 if (pq
< 9 || !overlap
) {
2458 /* Set outer values */
2459 if (s
->first_slice_line
&& (n
!= 2 && n
!= 3))
2460 b
= a
= dcpred
[scale
];
2461 if (s
->mb_x
== 0 && (n
!= 1 && n
!= 3))
2462 b
= c
= dcpred
[scale
];
2464 /* Set outer values */
2465 if (s
->first_slice_line
&& (n
!= 2 && n
!= 3))
2467 if (s
->mb_x
== 0 && (n
!= 1 && n
!= 3))
2471 if (abs(a
- b
) <= abs(b
- c
)) {
2473 *dir_ptr
= 1; // left
2476 *dir_ptr
= 0; // top
2479 /* update predictor */
2480 *dc_val_ptr
= &dc_val
[0];
2485 /** Get predicted DC value
2486 * prediction dir: left=0, top=1
2487 * @param s MpegEncContext
2488 * @param overlap flag indicating that overlap filtering is used
2489 * @param pq integer part of picture quantizer
2490 * @param[in] n block index in the current MB
2491 * @param a_avail flag indicating top block availability
2492 * @param c_avail flag indicating left block availability
2493 * @param dc_val_ptr Pointer to DC predictor
2494 * @param dir_ptr Prediction direction for use in AC prediction
2496 static inline int vc1_pred_dc(MpegEncContext
*s
, int overlap
, int pq
, int n
,
2497 int a_avail
, int c_avail
,
2498 int16_t **dc_val_ptr
, int *dir_ptr
)
2500 int a
, b
, c
, wrap
, pred
;
2502 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2505 wrap
= s
->block_wrap
[n
];
2506 dc_val
= s
->dc_val
[0] + s
->block_index
[n
];
2512 b
= dc_val
[ - 1 - wrap
];
2513 a
= dc_val
[ - wrap
];
2514 /* scale predictors if needed */
2515 q1
= s
->current_picture
.f
.qscale_table
[mb_pos
];
2516 if (c_avail
&& (n
!= 1 && n
!= 3)) {
2517 q2
= s
->current_picture
.f
.qscale_table
[mb_pos
- 1];
2519 c
= (c
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2521 if (a_avail
&& (n
!= 2 && n
!= 3)) {
2522 q2
= s
->current_picture
.f
.qscale_table
[mb_pos
- s
->mb_stride
];
2524 a
= (a
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2526 if (a_avail
&& c_avail
&& (n
!= 3)) {
2531 off
-= s
->mb_stride
;
2532 q2
= s
->current_picture
.f
.qscale_table
[off
];
2534 b
= (b
* s
->y_dc_scale_table
[q2
] * ff_vc1_dqscale
[s
->y_dc_scale_table
[q1
] - 1] + 0x20000) >> 18;
2537 if (a_avail
&& c_avail
) {
2538 if (abs(a
- b
) <= abs(b
- c
)) {
2540 *dir_ptr
= 1; // left
2543 *dir_ptr
= 0; // top
2545 } else if (a_avail
) {
2547 *dir_ptr
= 0; // top
2548 } else if (c_avail
) {
2550 *dir_ptr
= 1; // left
2553 *dir_ptr
= 1; // left
2556 /* update predictor */
2557 *dc_val_ptr
= &dc_val
[0];
2561 /** @} */ // Block group
2564 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2565 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2569 static inline int vc1_coded_block_pred(MpegEncContext
* s
, int n
,
2570 uint8_t **coded_block_ptr
)
2572 int xy
, wrap
, pred
, a
, b
, c
;
2574 xy
= s
->block_index
[n
];
2575 wrap
= s
->b8_stride
;
2580 a
= s
->coded_block
[xy
- 1 ];
2581 b
= s
->coded_block
[xy
- 1 - wrap
];
2582 c
= s
->coded_block
[xy
- wrap
];
2591 *coded_block_ptr
= &s
->coded_block
[xy
];
2597 * Decode one AC coefficient
2598 * @param v The VC1 context
2599 * @param last Last coefficient
2600 * @param skip How much zero coefficients to skip
2601 * @param value Decoded AC coefficient value
2602 * @param codingset set of VLC to decode data
2605 static void vc1_decode_ac_coeff(VC1Context
*v
, int *last
, int *skip
,
2606 int *value
, int codingset
)
2608 GetBitContext
*gb
= &v
->s
.gb
;
2609 int index
, escape
, run
= 0, level
= 0, lst
= 0;
2611 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
2612 if (index
!= vc1_ac_sizes
[codingset
] - 1) {
2613 run
= vc1_index_decode_table
[codingset
][index
][0];
2614 level
= vc1_index_decode_table
[codingset
][index
][1];
2615 lst
= index
>= vc1_last_decode_table
[codingset
] || get_bits_left(gb
) < 0;
2619 escape
= decode210(gb
);
2621 index
= get_vlc2(gb
, ff_vc1_ac_coeff_table
[codingset
].table
, AC_VLC_BITS
, 3);
2622 run
= vc1_index_decode_table
[codingset
][index
][0];
2623 level
= vc1_index_decode_table
[codingset
][index
][1];
2624 lst
= index
>= vc1_last_decode_table
[codingset
];
2627 level
+= vc1_last_delta_level_table
[codingset
][run
];
2629 level
+= vc1_delta_level_table
[codingset
][run
];
2632 run
+= vc1_last_delta_run_table
[codingset
][level
] + 1;
2634 run
+= vc1_delta_run_table
[codingset
][level
] + 1;
2640 lst
= get_bits1(gb
);
2641 if (v
->s
.esc3_level_length
== 0) {
2642 if (v
->pq
< 8 || v
->dquantfrm
) { // table 59
2643 v
->s
.esc3_level_length
= get_bits(gb
, 3);
2644 if (!v
->s
.esc3_level_length
)
2645 v
->s
.esc3_level_length
= get_bits(gb
, 2) + 8;
2646 } else { // table 60
2647 v
->s
.esc3_level_length
= get_unary(gb
, 1, 6) + 2;
2649 v
->s
.esc3_run_length
= 3 + get_bits(gb
, 2);
2651 run
= get_bits(gb
, v
->s
.esc3_run_length
);
2652 sign
= get_bits1(gb
);
2653 level
= get_bits(gb
, v
->s
.esc3_level_length
);
2664 /** Decode intra block in intra frames - should be faster than decode_intra_block
2665 * @param v VC1Context
2666 * @param block block to decode
2667 * @param[in] n subblock index
2668 * @param coded are AC coeffs present or not
2669 * @param codingset set of VLC to decode data
2671 static int vc1_decode_i_block(VC1Context
*v
, DCTELEM block
[64], int n
,
2672 int coded
, int codingset
)
2674 GetBitContext
*gb
= &v
->s
.gb
;
2675 MpegEncContext
*s
= &v
->s
;
2676 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
2679 int16_t *ac_val
, *ac_val2
;
2682 /* Get DC differential */
2684 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2686 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2689 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
2693 if (dcdiff
== 119 /* ESC index value */) {
2694 /* TODO: Optimize */
2695 if (v
->pq
== 1) dcdiff
= get_bits(gb
, 10);
2696 else if (v
->pq
== 2) dcdiff
= get_bits(gb
, 9);
2697 else dcdiff
= get_bits(gb
, 8);
2700 dcdiff
= (dcdiff
<< 2) + get_bits(gb
, 2) - 3;
2701 else if (v
->pq
== 2)
2702 dcdiff
= (dcdiff
<< 1) + get_bits1(gb
) - 1;
2709 dcdiff
+= vc1_i_pred_dc(&v
->s
, v
->overlap
, v
->pq
, n
, &dc_val
, &dc_pred_dir
);
2712 /* Store the quantized DC coeff, used for prediction */
2714 block
[0] = dcdiff
* s
->y_dc_scale
;
2716 block
[0] = dcdiff
* s
->c_dc_scale
;
2727 int last
= 0, skip
, value
;
2728 const uint8_t *zz_table
;
2732 scale
= v
->pq
* 2 + v
->halfpq
;
2736 zz_table
= v
->zz_8x8
[2];
2738 zz_table
= v
->zz_8x8
[3];
2740 zz_table
= v
->zz_8x8
[1];
2742 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2744 if (dc_pred_dir
) // left
2747 ac_val
-= 16 * s
->block_wrap
[n
];
2750 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
2754 block
[zz_table
[i
++]] = value
;
2757 /* apply AC prediction if needed */
2759 if (dc_pred_dir
) { // left
2760 for (k
= 1; k
< 8; k
++)
2761 block
[k
<< v
->left_blk_sh
] += ac_val
[k
];
2763 for (k
= 1; k
< 8; k
++)
2764 block
[k
<< v
->top_blk_sh
] += ac_val
[k
+ 8];
2767 /* save AC coeffs for further prediction */
2768 for (k
= 1; k
< 8; k
++) {
2769 ac_val2
[k
] = block
[k
<< v
->left_blk_sh
];
2770 ac_val2
[k
+ 8] = block
[k
<< v
->top_blk_sh
];
2773 /* scale AC coeffs */
2774 for (k
= 1; k
< 64; k
++)
2778 block
[k
] += (block
[k
] < 0) ?
-v
->pq
: v
->pq
;
2781 if (s
->ac_pred
) i
= 63;
2787 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2791 scale
= v
->pq
* 2 + v
->halfpq
;
2792 memset(ac_val2
, 0, 16 * 2);
2793 if (dc_pred_dir
) { // left
2796 memcpy(ac_val2
, ac_val
, 8 * 2);
2798 ac_val
-= 16 * s
->block_wrap
[n
];
2800 memcpy(ac_val2
+ 8, ac_val
+ 8, 8 * 2);
2803 /* apply AC prediction if needed */
2805 if (dc_pred_dir
) { //left
2806 for (k
= 1; k
< 8; k
++) {
2807 block
[k
<< v
->left_blk_sh
] = ac_val
[k
] * scale
;
2808 if (!v
->pquantizer
&& block
[k
<< v
->left_blk_sh
])
2809 block
[k
<< v
->left_blk_sh
] += (block
[k
<< v
->left_blk_sh
] < 0) ?
-v
->pq
: v
->pq
;
2812 for (k
= 1; k
< 8; k
++) {
2813 block
[k
<< v
->top_blk_sh
] = ac_val
[k
+ 8] * scale
;
2814 if (!v
->pquantizer
&& block
[k
<< v
->top_blk_sh
])
2815 block
[k
<< v
->top_blk_sh
] += (block
[k
<< v
->top_blk_sh
] < 0) ?
-v
->pq
: v
->pq
;
2821 s
->block_last_index
[n
] = i
;
2826 /** Decode intra block in intra frames - should be faster than decode_intra_block
2827 * @param v VC1Context
2828 * @param block block to decode
2829 * @param[in] n subblock number
2830 * @param coded are AC coeffs present or not
2831 * @param codingset set of VLC to decode data
2832 * @param mquant quantizer value for this macroblock
2834 static int vc1_decode_i_block_adv(VC1Context
*v
, DCTELEM block
[64], int n
,
2835 int coded
, int codingset
, int mquant
)
2837 GetBitContext
*gb
= &v
->s
.gb
;
2838 MpegEncContext
*s
= &v
->s
;
2839 int dc_pred_dir
= 0; /* Direction of the DC prediction used */
2842 int16_t *ac_val
, *ac_val2
;
2844 int a_avail
= v
->a_avail
, c_avail
= v
->c_avail
;
2845 int use_pred
= s
->ac_pred
;
2848 int mb_pos
= s
->mb_x
+ s
->mb_y
* s
->mb_stride
;
2850 /* Get DC differential */
2852 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_luma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2854 dcdiff
= get_vlc2(&s
->gb
, ff_msmp4_dc_chroma_vlc
[s
->dc_table_index
].table
, DC_VLC_BITS
, 3);
2857 av_log(s
->avctx
, AV_LOG_ERROR
, "Illegal DC VLC\n");
2861 if (dcdiff
== 119 /* ESC index value */) {
2862 /* TODO: Optimize */
2863 if (mquant
== 1) dcdiff
= get_bits(gb
, 10);
2864 else if (mquant
== 2) dcdiff
= get_bits(gb
, 9);
2865 else dcdiff
= get_bits(gb
, 8);
2868 dcdiff
= (dcdiff
<< 2) + get_bits(gb
, 2) - 3;
2869 else if (mquant
== 2)
2870 dcdiff
= (dcdiff
<< 1) + get_bits1(gb
) - 1;
2877 dcdiff
+= vc1_pred_dc(&v
->s
, v
->overlap
, mquant
, n
, v
->a_avail
, v
->c_avail
, &dc_val
, &dc_pred_dir
);
2880 /* Store the quantized DC coeff, used for prediction */
2882 block
[0] = dcdiff
* s
->y_dc_scale
;
2884 block
[0] = dcdiff
* s
->c_dc_scale
;
2890 /* check if AC is needed at all */
2891 if (!a_avail
&& !c_avail
)
2893 ac_val
= s
->ac_val
[0][0] + s
->block_index
[n
] * 16;
2896 scale
= mquant
* 2 + ((mquant
== v
->pq
) ? v
->halfpq
: 0);
2898 if (dc_pred_dir
) // left
2901 ac_val
-= 16 * s
->block_wrap
[n
];
2903 q1
= s
->current_picture
.f
.qscale_table
[mb_pos
];
2904 if ( dc_pred_dir
&& c_avail
&& mb_pos
)
2905 q2
= s
->current_picture
.f
.qscale_table
[mb_pos
- 1];
2906 if (!dc_pred_dir
&& a_avail
&& mb_pos
>= s
->mb_stride
)
2907 q2
= s
->current_picture
.f
.qscale_table
[mb_pos
- s
->mb_stride
];
2908 if ( dc_pred_dir
&& n
== 1)
2910 if (!dc_pred_dir
&& n
== 2)
2916 int last
= 0, skip
, value
;
2917 const uint8_t *zz_table
;
2921 if (!use_pred
&& v
->fcm
== ILACE_FRAME
) {
2922 zz_table
= v
->zzi_8x8
;
2924 if (!dc_pred_dir
) // top
2925 zz_table
= v
->zz_8x8
[2];
2927 zz_table
= v
->zz_8x8
[3];
2930 if (v
->fcm
!= ILACE_FRAME
)
2931 zz_table
= v
->zz_8x8
[1];
2933 zz_table
= v
->zzi_8x8
;
2937 vc1_decode_ac_coeff(v
, &last
, &skip
, &value
, codingset
);
2941 block
[zz_table
[i
++]] = value
;
2944 /* apply AC prediction if needed */
2946 /* scale predictors if needed*/
2947 if (q2
&& q1
!= q2
) {
2948 q1
= q1
* 2 + ((q1
== v
->pq
) ? v
->halfpq
: 0) - 1;
2949 q2
= q2
* 2 + ((q2
== v
->pq
) ? v
->halfpq
: 0) - 1;
2951 if (dc_pred_dir
) { // left
2952 for (k
= 1; k
< 8; k
++)
2953 block
[k
<< v
->left_blk_sh
] += (ac_val
[k
] * q2
* ff_vc1_dqscale
[q1
- 1] + 0x20000) >> 18;
2955 for (k
= 1; k
< 8; k
++)
2956 block
[k
<< v
->top_blk_sh
] += (ac_val
[k
+ 8] * q2
* ff_vc1_dqscale
[q1
- 1