42dc83f120859a2ce1934bc0cfdc9856e2f2ee75
[libav.git] / libavcodec / vc1dec.c
1 /*
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
6 *
7 * This file is part of Libav.
8 *
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.
13 *
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.
18 *
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
22 */
23
24 /**
25 * @file
26 * VC-1 and WMV3 decoder
27 */
28
29 #include "internal.h"
30 #include "avcodec.h"
31 #include "error_resilience.h"
32 #include "mpegvideo.h"
33 #include "h263.h"
34 #include "h264chroma.h"
35 #include "vc1.h"
36 #include "vc1data.h"
37 #include "vc1acdata.h"
38 #include "msmpeg4data.h"
39 #include "unary.h"
40 #include "mathops.h"
41 #include "vdpau_internal.h"
42
43 #undef NDEBUG
44 #include <assert.h>
45
46 #define MB_INTRA_VLC_BITS 9
47 #define DC_VLC_BITS 9
48
49
50 // offset tables for interlaced picture MVDATA decoding
51 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
52 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
53
54 /***********************************************************************/
55 /**
56 * @name VC-1 Bitplane decoding
57 * @see 8.7, p56
58 * @{
59 */
60
61 /**
62 * Imode types
63 * @{
64 */
65 enum Imode {
66 IMODE_RAW,
67 IMODE_NORM2,
68 IMODE_DIFF2,
69 IMODE_NORM6,
70 IMODE_DIFF6,
71 IMODE_ROWSKIP,
72 IMODE_COLSKIP
73 };
74 /** @} */ //imode defines
75
76 static void init_block_index(VC1Context *v)
77 {
78 MpegEncContext *s = &v->s;
79 ff_init_block_index(s);
80 if (v->field_mode && !(v->second_field ^ v->tff)) {
81 s->dest[0] += s->current_picture_ptr->f.linesize[0];
82 s->dest[1] += s->current_picture_ptr->f.linesize[1];
83 s->dest[2] += s->current_picture_ptr->f.linesize[2];
84 }
85 }
86
87 /** @} */ //Bitplane group
88
89 static void vc1_put_signed_blocks_clamped(VC1Context *v)
90 {
91 MpegEncContext *s = &v->s;
92 int topleft_mb_pos, top_mb_pos;
93 int stride_y, fieldtx = 0;
94 int v_dist;
95
96 /* The put pixels loop is always one MB row behind the decoding loop,
97 * because we can only put pixels when overlap filtering is done, and
98 * for filtering of the bottom edge of a MB, we need the next MB row
99 * present as well.
100 * Within the row, the put pixels loop is also one MB col behind the
101 * decoding loop. The reason for this is again, because for filtering
102 * of the right MB edge, we need the next MB present. */
103 if (!s->first_slice_line) {
104 if (s->mb_x) {
105 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
106 if (v->fcm == ILACE_FRAME)
107 fieldtx = v->fieldtx_plane[topleft_mb_pos];
108 stride_y = s->linesize << fieldtx;
109 v_dist = (16 - fieldtx) >> (fieldtx == 0);
110 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
111 s->dest[0] - 16 * s->linesize - 16,
112 stride_y);
113 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
114 s->dest[0] - 16 * s->linesize - 8,
115 stride_y);
116 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
117 s->dest[0] - v_dist * s->linesize - 16,
118 stride_y);
119 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
120 s->dest[0] - v_dist * s->linesize - 8,
121 stride_y);
122 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
123 s->dest[1] - 8 * s->uvlinesize - 8,
124 s->uvlinesize);
125 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
126 s->dest[2] - 8 * s->uvlinesize - 8,
127 s->uvlinesize);
128 }
129 if (s->mb_x == s->mb_width - 1) {
130 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
131 if (v->fcm == ILACE_FRAME)
132 fieldtx = v->fieldtx_plane[top_mb_pos];
133 stride_y = s->linesize << fieldtx;
134 v_dist = fieldtx ? 15 : 8;
135 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
136 s->dest[0] - 16 * s->linesize,
137 stride_y);
138 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
139 s->dest[0] - 16 * s->linesize + 8,
140 stride_y);
141 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
142 s->dest[0] - v_dist * s->linesize,
143 stride_y);
144 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
145 s->dest[0] - v_dist * s->linesize + 8,
146 stride_y);
147 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
148 s->dest[1] - 8 * s->uvlinesize,
149 s->uvlinesize);
150 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
151 s->dest[2] - 8 * s->uvlinesize,
152 s->uvlinesize);
153 }
154 }
155
156 #define inc_blk_idx(idx) do { \
157 idx++; \
158 if (idx >= v->n_allocated_blks) \
159 idx = 0; \
160 } while (0)
161
162 inc_blk_idx(v->topleft_blk_idx);
163 inc_blk_idx(v->top_blk_idx);
164 inc_blk_idx(v->left_blk_idx);
165 inc_blk_idx(v->cur_blk_idx);
166 }
167
168 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
169 {
170 MpegEncContext *s = &v->s;
171 int j;
172 if (!s->first_slice_line) {
173 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
174 if (s->mb_x)
175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
176 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
177 for (j = 0; j < 2; j++) {
178 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
179 if (s->mb_x)
180 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
181 }
182 }
183 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
184
185 if (s->mb_y == s->end_mb_y - 1) {
186 if (s->mb_x) {
187 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
188 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
189 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
190 }
191 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
192 }
193 }
194
195 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
196 {
197 MpegEncContext *s = &v->s;
198 int j;
199
200 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
201 * means it runs two rows/cols behind the decoding loop. */
202 if (!s->first_slice_line) {
203 if (s->mb_x) {
204 if (s->mb_y >= s->start_mb_y + 2) {
205 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
206
207 if (s->mb_x >= 2)
208 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
209 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
210 for (j = 0; j < 2; j++) {
211 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
212 if (s->mb_x >= 2) {
213 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
214 }
215 }
216 }
217 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
218 }
219
220 if (s->mb_x == s->mb_width - 1) {
221 if (s->mb_y >= s->start_mb_y + 2) {
222 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
223
224 if (s->mb_x)
225 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
226 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
227 for (j = 0; j < 2; j++) {
228 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
229 if (s->mb_x >= 2) {
230 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
231 }
232 }
233 }
234 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
235 }
236
237 if (s->mb_y == s->end_mb_y) {
238 if (s->mb_x) {
239 if (s->mb_x >= 2)
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
242 if (s->mb_x >= 2) {
243 for (j = 0; j < 2; j++) {
244 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
245 }
246 }
247 }
248
249 if (s->mb_x == s->mb_width - 1) {
250 if (s->mb_x)
251 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
252 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
253 if (s->mb_x) {
254 for (j = 0; j < 2; j++) {
255 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
256 }
257 }
258 }
259 }
260 }
261 }
262
263 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
264 {
265 MpegEncContext *s = &v->s;
266 int mb_pos;
267
268 if (v->condover == CONDOVER_NONE)
269 return;
270
271 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
272
273 /* Within a MB, the horizontal overlap always runs before the vertical.
274 * To accomplish that, we run the H on left and internal borders of the
275 * currently decoded MB. Then, we wait for the next overlap iteration
276 * to do H overlap on the right edge of this MB, before moving over and
277 * running the V overlap. Therefore, the V overlap makes us trail by one
278 * MB col and the H overlap filter makes us trail by one MB row. This
279 * is reflected in the time at which we run the put_pixels loop. */
280 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
281 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
282 v->over_flags_plane[mb_pos - 1])) {
283 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
284 v->block[v->cur_blk_idx][0]);
285 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
286 v->block[v->cur_blk_idx][2]);
287 if (!(s->flags & CODEC_FLAG_GRAY)) {
288 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
289 v->block[v->cur_blk_idx][4]);
290 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
291 v->block[v->cur_blk_idx][5]);
292 }
293 }
294 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
295 v->block[v->cur_blk_idx][1]);
296 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
297 v->block[v->cur_blk_idx][3]);
298
299 if (s->mb_x == s->mb_width - 1) {
300 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
301 v->over_flags_plane[mb_pos - s->mb_stride])) {
302 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
303 v->block[v->cur_blk_idx][0]);
304 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
305 v->block[v->cur_blk_idx][1]);
306 if (!(s->flags & CODEC_FLAG_GRAY)) {
307 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
308 v->block[v->cur_blk_idx][4]);
309 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
310 v->block[v->cur_blk_idx][5]);
311 }
312 }
313 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
314 v->block[v->cur_blk_idx][2]);
315 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
316 v->block[v->cur_blk_idx][3]);
317 }
318 }
319 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
320 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
321 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
322 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
323 v->block[v->left_blk_idx][0]);
324 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
325 v->block[v->left_blk_idx][1]);
326 if (!(s->flags & CODEC_FLAG_GRAY)) {
327 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
328 v->block[v->left_blk_idx][4]);
329 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
330 v->block[v->left_blk_idx][5]);
331 }
332 }
333 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
334 v->block[v->left_blk_idx][2]);
335 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
336 v->block[v->left_blk_idx][3]);
337 }
338 }
339
340 /** Do motion compensation over 1 macroblock
341 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
342 */
343 static void vc1_mc_1mv(VC1Context *v, int dir)
344 {
345 MpegEncContext *s = &v->s;
346 H264ChromaContext *h264chroma = &v->h264chroma;
347 uint8_t *srcY, *srcU, *srcV;
348 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
349 int v_edge_pos = s->v_edge_pos >> v->field_mode;
350 int i;
351 uint8_t (*luty)[256], (*lutuv)[256];
352 int use_ic;
353
354 if ((!v->field_mode ||
355 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
356 !v->s.last_picture.f.data[0])
357 return;
358
359 mx = s->mv[dir][0][0];
360 my = s->mv[dir][0][1];
361
362 // store motion vectors for further use in B frames
363 if (s->pict_type == AV_PICTURE_TYPE_P) {
364 for (i = 0; i < 4; i++) {
365 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
366 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
367 }
368 }
369
370 uvmx = (mx + ((mx & 3) == 3)) >> 1;
371 uvmy = (my + ((my & 3) == 3)) >> 1;
372 v->luma_mv[s->mb_x][0] = uvmx;
373 v->luma_mv[s->mb_x][1] = uvmy;
374
375 if (v->field_mode &&
376 v->cur_field_type != v->ref_field_type[dir]) {
377 my = my - 2 + 4 * v->cur_field_type;
378 uvmy = uvmy - 2 + 4 * v->cur_field_type;
379 }
380
381 // fastuvmc shall be ignored for interlaced frame picture
382 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
383 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
384 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
385 }
386 if (!dir) {
387 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
388 srcY = s->current_picture.f.data[0];
389 srcU = s->current_picture.f.data[1];
390 srcV = s->current_picture.f.data[2];
391 luty = v->curr_luty;
392 lutuv = v->curr_lutuv;
393 use_ic = v->curr_use_ic;
394 } else {
395 srcY = s->last_picture.f.data[0];
396 srcU = s->last_picture.f.data[1];
397 srcV = s->last_picture.f.data[2];
398 luty = v->last_luty;
399 lutuv = v->last_lutuv;
400 use_ic = v->last_use_ic;
401 }
402 } else {
403 srcY = s->next_picture.f.data[0];
404 srcU = s->next_picture.f.data[1];
405 srcV = s->next_picture.f.data[2];
406 luty = v->next_luty;
407 lutuv = v->next_lutuv;
408 use_ic = v->next_use_ic;
409 }
410
411 src_x = s->mb_x * 16 + (mx >> 2);
412 src_y = s->mb_y * 16 + (my >> 2);
413 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
414 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
415
416 if (v->profile != PROFILE_ADVANCED) {
417 src_x = av_clip( src_x, -16, s->mb_width * 16);
418 src_y = av_clip( src_y, -16, s->mb_height * 16);
419 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
420 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
421 } else {
422 src_x = av_clip( src_x, -17, s->avctx->coded_width);
423 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
424 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
425 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
426 }
427
428 srcY += src_y * s->linesize + src_x;
429 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
430 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
431
432 if (v->field_mode && v->ref_field_type[dir]) {
433 srcY += s->current_picture_ptr->f.linesize[0];
434 srcU += s->current_picture_ptr->f.linesize[1];
435 srcV += s->current_picture_ptr->f.linesize[2];
436 }
437
438 /* for grayscale we should not try to read from unknown area */
439 if (s->flags & CODEC_FLAG_GRAY) {
440 srcU = s->edge_emu_buffer + 18 * s->linesize;
441 srcV = s->edge_emu_buffer + 18 * s->linesize;
442 }
443
444 if (v->rangeredfrm || use_ic
445 || s->h_edge_pos < 22 || v_edge_pos < 22
446 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
447 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
448 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
449
450 srcY -= s->mspel * (1 + s->linesize);
451 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
452 17 + s->mspel * 2, 17 + s->mspel * 2,
453 src_x - s->mspel, src_y - s->mspel,
454 s->h_edge_pos, v_edge_pos);
455 srcY = s->edge_emu_buffer;
456 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
457 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
458 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
459 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
460 srcU = uvbuf;
461 srcV = uvbuf + 16;
462 /* if we deal with range reduction we need to scale source blocks */
463 if (v->rangeredfrm) {
464 int i, j;
465 uint8_t *src, *src2;
466
467 src = srcY;
468 for (j = 0; j < 17 + s->mspel * 2; j++) {
469 for (i = 0; i < 17 + s->mspel * 2; i++)
470 src[i] = ((src[i] - 128) >> 1) + 128;
471 src += s->linesize;
472 }
473 src = srcU;
474 src2 = srcV;
475 for (j = 0; j < 9; j++) {
476 for (i = 0; i < 9; i++) {
477 src[i] = ((src[i] - 128) >> 1) + 128;
478 src2[i] = ((src2[i] - 128) >> 1) + 128;
479 }
480 src += s->uvlinesize;
481 src2 += s->uvlinesize;
482 }
483 }
484 /* if we deal with intensity compensation we need to scale source blocks */
485 if (use_ic) {
486 int i, j;
487 uint8_t *src, *src2;
488
489 src = srcY;
490 for (j = 0; j < 17 + s->mspel * 2; j++) {
491 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
492 for (i = 0; i < 17 + s->mspel * 2; i++)
493 src[i] = luty[f][src[i]];
494 src += s->linesize;
495 }
496 src = srcU;
497 src2 = srcV;
498 for (j = 0; j < 9; j++) {
499 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
500 for (i = 0; i < 9; i++) {
501 src[i] = lutuv[f][src[i]];
502 src2[i] = lutuv[f][src2[i]];
503 }
504 src += s->uvlinesize;
505 src2 += s->uvlinesize;
506 }
507 }
508 srcY += s->mspel * (1 + s->linesize);
509 }
510
511 if (s->mspel) {
512 dxy = ((my & 3) << 2) | (mx & 3);
513 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
514 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
515 srcY += s->linesize * 8;
516 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
517 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
518 } else { // hpel mc - always used for luma
519 dxy = (my & 2) | ((mx & 2) >> 1);
520 if (!v->rnd)
521 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
522 else
523 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
524 }
525
526 if (s->flags & CODEC_FLAG_GRAY) return;
527 /* Chroma MC always uses qpel bilinear */
528 uvmx = (uvmx & 3) << 1;
529 uvmy = (uvmy & 3) << 1;
530 if (!v->rnd) {
531 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
532 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
533 } else {
534 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
535 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
536 }
537 }
538
539 static inline int median4(int a, int b, int c, int d)
540 {
541 if (a < b) {
542 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
543 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
544 } else {
545 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
546 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
547 }
548 }
549
550 /** Do motion compensation for 4-MV macroblock - luminance block
551 */
552 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
553 {
554 MpegEncContext *s = &v->s;
555 uint8_t *srcY;
556 int dxy, mx, my, src_x, src_y;
557 int off;
558 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
559 int v_edge_pos = s->v_edge_pos >> v->field_mode;
560 uint8_t (*luty)[256];
561 int use_ic;
562
563 if ((!v->field_mode ||
564 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
565 !v->s.last_picture.f.data[0])
566 return;
567
568 mx = s->mv[dir][n][0];
569 my = s->mv[dir][n][1];
570
571 if (!dir) {
572 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
573 srcY = s->current_picture.f.data[0];
574 luty = v->curr_luty;
575 use_ic = v->curr_use_ic;
576 } else {
577 srcY = s->last_picture.f.data[0];
578 luty = v->last_luty;
579 use_ic = v->last_use_ic;
580 }
581 } else {
582 srcY = s->next_picture.f.data[0];
583 luty = v->next_luty;
584 use_ic = v->next_use_ic;
585 }
586
587 if (v->field_mode) {
588 if (v->cur_field_type != v->ref_field_type[dir])
589 my = my - 2 + 4 * v->cur_field_type;
590 }
591
592 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
593 int same_count = 0, opp_count = 0, k;
594 int chosen_mv[2][4][2], f;
595 int tx, ty;
596 for (k = 0; k < 4; k++) {
597 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
598 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
599 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
600 opp_count += f;
601 same_count += 1 - f;
602 }
603 f = opp_count > same_count;
604 switch (f ? opp_count : same_count) {
605 case 4:
606 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
607 chosen_mv[f][2][0], chosen_mv[f][3][0]);
608 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
609 chosen_mv[f][2][1], chosen_mv[f][3][1]);
610 break;
611 case 3:
612 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
613 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
614 break;
615 case 2:
616 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
617 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
618 break;
619 }
620 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
621 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
622 for (k = 0; k < 4; k++)
623 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
624 }
625
626 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
627 int qx, qy;
628 int width = s->avctx->coded_width;
629 int height = s->avctx->coded_height >> 1;
630 if (s->pict_type == AV_PICTURE_TYPE_P) {
631 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
632 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
633 }
634 qx = (s->mb_x * 16) + (mx >> 2);
635 qy = (s->mb_y * 8) + (my >> 3);
636
637 if (qx < -17)
638 mx -= 4 * (qx + 17);
639 else if (qx > width)
640 mx -= 4 * (qx - width);
641 if (qy < -18)
642 my -= 8 * (qy + 18);
643 else if (qy > height + 1)
644 my -= 8 * (qy - height - 1);
645 }
646
647 if ((v->fcm == ILACE_FRAME) && fieldmv)
648 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
649 else
650 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
651
652 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
653 if (!fieldmv)
654 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
655 else
656 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
657
658 if (v->profile != PROFILE_ADVANCED) {
659 src_x = av_clip(src_x, -16, s->mb_width * 16);
660 src_y = av_clip(src_y, -16, s->mb_height * 16);
661 } else {
662 src_x = av_clip(src_x, -17, s->avctx->coded_width);
663 if (v->fcm == ILACE_FRAME) {
664 if (src_y & 1)
665 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
666 else
667 src_y = av_clip(src_y, -18, s->avctx->coded_height);
668 } else {
669 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
670 }
671 }
672
673 srcY += src_y * s->linesize + src_x;
674 if (v->field_mode && v->ref_field_type[dir])
675 srcY += s->current_picture_ptr->f.linesize[0];
676
677 if (fieldmv && !(src_y & 1))
678 v_edge_pos--;
679 if (fieldmv && (src_y & 1) && src_y < 4)
680 src_y--;
681 if (v->rangeredfrm || use_ic
682 || s->h_edge_pos < 13 || v_edge_pos < 23
683 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
684 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
685 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
686 /* check emulate edge stride and offset */
687 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
688 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
689 src_x - s->mspel, src_y - (s->mspel << fieldmv),
690 s->h_edge_pos, v_edge_pos);
691 srcY = s->edge_emu_buffer;
692 /* if we deal with range reduction we need to scale source blocks */
693 if (v->rangeredfrm) {
694 int i, j;
695 uint8_t *src;
696
697 src = srcY;
698 for (j = 0; j < 9 + s->mspel * 2; j++) {
699 for (i = 0; i < 9 + s->mspel * 2; i++)
700 src[i] = ((src[i] - 128) >> 1) + 128;
701 src += s->linesize << fieldmv;
702 }
703 }
704 /* if we deal with intensity compensation we need to scale source blocks */
705 if (use_ic) {
706 int i, j;
707 uint8_t *src;
708
709 src = srcY;
710 for (j = 0; j < 9 + s->mspel * 2; j++) {
711 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
712 for (i = 0; i < 9 + s->mspel * 2; i++)
713 src[i] = luty[f][src[i]];
714 src += s->linesize << fieldmv;
715 }
716 }
717 srcY += s->mspel * (1 + (s->linesize << fieldmv));
718 }
719
720 if (s->mspel) {
721 dxy = ((my & 3) << 2) | (mx & 3);
722 if (avg)
723 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
724 else
725 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
726 } else { // hpel mc - always used for luma
727 dxy = (my & 2) | ((mx & 2) >> 1);
728 if (!v->rnd)
729 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
730 else
731 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
732 }
733 }
734
735 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
736 {
737 int idx, i;
738 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
739
740 idx = ((a[3] != flag) << 3)
741 | ((a[2] != flag) << 2)
742 | ((a[1] != flag) << 1)
743 | (a[0] != flag);
744 if (!idx) {
745 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
746 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
747 return 4;
748 } else if (count[idx] == 1) {
749 switch (idx) {
750 case 0x1:
751 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
752 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
753 return 3;
754 case 0x2:
755 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
756 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
757 return 3;
758 case 0x4:
759 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
760 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
761 return 3;
762 case 0x8:
763 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
764 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
765 return 3;
766 }
767 } else if (count[idx] == 2) {
768 int t1 = 0, t2 = 0;
769 for (i = 0; i < 3; i++)
770 if (!a[i]) {
771 t1 = i;
772 break;
773 }
774 for (i = t1 + 1; i < 4; i++)
775 if (!a[i]) {
776 t2 = i;
777 break;
778 }
779 *tx = (mvx[t1] + mvx[t2]) / 2;
780 *ty = (mvy[t1] + mvy[t2]) / 2;
781 return 2;
782 } else {
783 return 0;
784 }
785 return -1;
786 }
787
788 /** Do motion compensation for 4-MV macroblock - both chroma blocks
789 */
790 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
791 {
792 MpegEncContext *s = &v->s;
793 H264ChromaContext *h264chroma = &v->h264chroma;
794 uint8_t *srcU, *srcV;
795 int uvmx, uvmy, uvsrc_x, uvsrc_y;
796 int k, tx = 0, ty = 0;
797 int mvx[4], mvy[4], intra[4], mv_f[4];
798 int valid_count;
799 int chroma_ref_type = v->cur_field_type;
800 int v_edge_pos = s->v_edge_pos >> v->field_mode;
801 uint8_t (*lutuv)[256];
802 int use_ic;
803
804 if (!v->field_mode && !v->s.last_picture.f.data[0])
805 return;
806 if (s->flags & CODEC_FLAG_GRAY)
807 return;
808
809 for (k = 0; k < 4; k++) {
810 mvx[k] = s->mv[dir][k][0];
811 mvy[k] = s->mv[dir][k][1];
812 intra[k] = v->mb_type[0][s->block_index[k]];
813 if (v->field_mode)
814 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
815 }
816
817 /* calculate chroma MV vector from four luma MVs */
818 if (!v->field_mode || (v->field_mode && !v->numref)) {
819 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
820 chroma_ref_type = v->reffield;
821 if (!valid_count) {
822 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
823 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
824 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
825 return; //no need to do MC for intra blocks
826 }
827 } else {
828 int dominant = 0;
829 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
830 dominant = 1;
831 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
832 if (dominant)
833 chroma_ref_type = !v->cur_field_type;
834 }
835 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
836 return;
837 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
838 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
839 uvmx = (tx + ((tx & 3) == 3)) >> 1;
840 uvmy = (ty + ((ty & 3) == 3)) >> 1;
841
842 v->luma_mv[s->mb_x][0] = uvmx;
843 v->luma_mv[s->mb_x][1] = uvmy;
844
845 if (v->fastuvmc) {
846 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
847 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
848 }
849 // Field conversion bias
850 if (v->cur_field_type != chroma_ref_type)
851 uvmy += 2 - 4 * chroma_ref_type;
852
853 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
854 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
855
856 if (v->profile != PROFILE_ADVANCED) {
857 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
858 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
859 } else {
860 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
861 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
862 }
863
864 if (!dir) {
865 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
866 srcU = s->current_picture.f.data[1];
867 srcV = s->current_picture.f.data[2];
868 lutuv = v->curr_lutuv;
869 use_ic = v->curr_use_ic;
870 } else {
871 srcU = s->last_picture.f.data[1];
872 srcV = s->last_picture.f.data[2];
873 lutuv = v->last_lutuv;
874 use_ic = v->last_use_ic;
875 }
876 } else {
877 srcU = s->next_picture.f.data[1];
878 srcV = s->next_picture.f.data[2];
879 lutuv = v->next_lutuv;
880 use_ic = v->next_use_ic;
881 }
882
883 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
884 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
885
886 if (v->field_mode) {
887 if (chroma_ref_type) {
888 srcU += s->current_picture_ptr->f.linesize[1];
889 srcV += s->current_picture_ptr->f.linesize[2];
890 }
891 }
892
893 if (v->rangeredfrm || use_ic
894 || s->h_edge_pos < 18 || v_edge_pos < 18
895 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
896 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
897 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
898 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
899 s->h_edge_pos >> 1, v_edge_pos >> 1);
900 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
901 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
902 s->h_edge_pos >> 1, v_edge_pos >> 1);
903 srcU = s->edge_emu_buffer;
904 srcV = s->edge_emu_buffer + 16;
905
906 /* if we deal with range reduction we need to scale source blocks */
907 if (v->rangeredfrm) {
908 int i, j;
909 uint8_t *src, *src2;
910
911 src = srcU;
912 src2 = srcV;
913 for (j = 0; j < 9; j++) {
914 for (i = 0; i < 9; i++) {
915 src[i] = ((src[i] - 128) >> 1) + 128;
916 src2[i] = ((src2[i] - 128) >> 1) + 128;
917 }
918 src += s->uvlinesize;
919 src2 += s->uvlinesize;
920 }
921 }
922 /* if we deal with intensity compensation we need to scale source blocks */
923 if (use_ic) {
924 int i, j;
925 uint8_t *src, *src2;
926
927 src = srcU;
928 src2 = srcV;
929 for (j = 0; j < 9; j++) {
930 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
931 for (i = 0; i < 9; i++) {
932 src[i] = lutuv[f][src[i]];
933 src2[i] = lutuv[f][src2[i]];
934 }
935 src += s->uvlinesize;
936 src2 += s->uvlinesize;
937 }
938 }
939 }
940
941 /* Chroma MC always uses qpel bilinear */
942 uvmx = (uvmx & 3) << 1;
943 uvmy = (uvmy & 3) << 1;
944 if (!v->rnd) {
945 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
946 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
947 } else {
948 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
949 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
950 }
951 }
952
953 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
954 */
955 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
956 {
957 MpegEncContext *s = &v->s;
958 H264ChromaContext *h264chroma = &v->h264chroma;
959 uint8_t *srcU, *srcV;
960 int uvsrc_x, uvsrc_y;
961 int uvmx_field[4], uvmy_field[4];
962 int i, off, tx, ty;
963 int fieldmv = v->blk_mv_type[s->block_index[0]];
964 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
965 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
966 int v_edge_pos = s->v_edge_pos >> 1;
967 int use_ic;
968 uint8_t (*lutuv)[256];
969
970 if (s->flags & CODEC_FLAG_GRAY)
971 return;
972
973 for (i = 0; i < 4; i++) {
974 int d = i < 2 ? dir: dir2;
975 tx = s->mv[d][i][0];
976 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
977 ty = s->mv[d][i][1];
978 if (fieldmv)
979 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
980 else
981 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
982 }
983
984 for (i = 0; i < 4; i++) {
985 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
986 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
987 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
988 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
989 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
990 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
991 if (i < 2 ? dir : dir2) {
992 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
993 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
994 lutuv = v->next_lutuv;
995 use_ic = v->next_use_ic;
996 } else {
997 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
998 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
999 lutuv = v->last_lutuv;
1000 use_ic = v->last_use_ic;
1001 }
1002 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1003 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1004
1005 if (fieldmv && !(uvsrc_y & 1))
1006 v_edge_pos--;
1007 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1008 uvsrc_y--;
1009 if (use_ic
1010 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1011 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1012 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1013 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1014 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1015 s->h_edge_pos >> 1, v_edge_pos);
1016 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1017 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1018 s->h_edge_pos >> 1, v_edge_pos);
1019 srcU = s->edge_emu_buffer;
1020 srcV = s->edge_emu_buffer + 16;
1021
1022 /* if we deal with intensity compensation we need to scale source blocks */
1023 if (use_ic) {
1024 int i, j;
1025 uint8_t *src, *src2;
1026
1027 src = srcU;
1028 src2 = srcV;
1029 for (j = 0; j < 5; j++) {
1030 int f = (uvsrc_y + (j << fieldmv)) & 1;
1031 for (i = 0; i < 5; i++) {
1032 src[i] = lutuv[f][src[i]];
1033 src2[i] = lutuv[f][src2[i]];
1034 }
1035 src += s->uvlinesize << fieldmv;
1036 src2 += s->uvlinesize << fieldmv;
1037 }
1038 }
1039 }
1040 if (avg) {
1041 if (!v->rnd) {
1042 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1043 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1044 } else {
1045 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1046 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1047 }
1048 } else {
1049 if (!v->rnd) {
1050 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1051 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1052 } else {
1053 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]);
1054 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]);
1055 }
1056 }
1057 }
1058 }
1059
1060 /***********************************************************************/
1061 /**
1062 * @name VC-1 Block-level functions
1063 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1064 * @{
1065 */
1066
1067 /**
1068 * @def GET_MQUANT
1069 * @brief Get macroblock-level quantizer scale
1070 */
1071 #define GET_MQUANT() \
1072 if (v->dquantfrm) { \
1073 int edges = 0; \
1074 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1075 if (v->dqbilevel) { \
1076 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1077 } else { \
1078 mqdiff = get_bits(gb, 3); \
1079 if (mqdiff != 7) \
1080 mquant = v->pq + mqdiff; \
1081 else \
1082 mquant = get_bits(gb, 5); \
1083 } \
1084 } \
1085 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1086 edges = 1 << v->dqsbedge; \
1087 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1088 edges = (3 << v->dqsbedge) % 15; \
1089 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1090 edges = 15; \
1091 if ((edges&1) && !s->mb_x) \
1092 mquant = v->altpq; \
1093 if ((edges&2) && s->first_slice_line) \
1094 mquant = v->altpq; \
1095 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1096 mquant = v->altpq; \
1097 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1098 mquant = v->altpq; \
1099 if (!mquant || mquant > 31) { \
1100 av_log(v->s.avctx, AV_LOG_ERROR, \
1101 "Overriding invalid mquant %d\n", mquant); \
1102 mquant = 1; \
1103 } \
1104 }
1105
1106 /**
1107 * @def GET_MVDATA(_dmv_x, _dmv_y)
1108 * @brief Get MV differentials
1109 * @see MVDATA decoding from 8.3.5.2, p(1)20
1110 * @param _dmv_x Horizontal differential for decoded MV
1111 * @param _dmv_y Vertical differential for decoded MV
1112 */
1113 #define GET_MVDATA(_dmv_x, _dmv_y) \
1114 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1115 VC1_MV_DIFF_VLC_BITS, 2); \
1116 if (index > 36) { \
1117 mb_has_coeffs = 1; \
1118 index -= 37; \
1119 } else \
1120 mb_has_coeffs = 0; \
1121 s->mb_intra = 0; \
1122 if (!index) { \
1123 _dmv_x = _dmv_y = 0; \
1124 } else if (index == 35) { \
1125 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1126 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1127 } else if (index == 36) { \
1128 _dmv_x = 0; \
1129 _dmv_y = 0; \
1130 s->mb_intra = 1; \
1131 } else { \
1132 index1 = index % 6; \
1133 if (!s->quarter_sample && index1 == 5) val = 1; \
1134 else val = 0; \
1135 if (size_table[index1] - val > 0) \
1136 val = get_bits(gb, size_table[index1] - val); \
1137 else val = 0; \
1138 sign = 0 - (val&1); \
1139 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1140 \
1141 index1 = index / 6; \
1142 if (!s->quarter_sample && index1 == 5) val = 1; \
1143 else val = 0; \
1144 if (size_table[index1] - val > 0) \
1145 val = get_bits(gb, size_table[index1] - val); \
1146 else val = 0; \
1147 sign = 0 - (val & 1); \
1148 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1149 }
1150
1151 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1152 int *dmv_y, int *pred_flag)
1153 {
1154 int index, index1;
1155 int extend_x = 0, extend_y = 0;
1156 GetBitContext *gb = &v->s.gb;
1157 int bits, esc;
1158 int val, sign;
1159 const int* offs_tab;
1160
1161 if (v->numref) {
1162 bits = VC1_2REF_MVDATA_VLC_BITS;
1163 esc = 125;
1164 } else {
1165 bits = VC1_1REF_MVDATA_VLC_BITS;
1166 esc = 71;
1167 }
1168 switch (v->dmvrange) {
1169 case 1:
1170 extend_x = 1;
1171 break;
1172 case 2:
1173 extend_y = 1;
1174 break;
1175 case 3:
1176 extend_x = extend_y = 1;
1177 break;
1178 }
1179 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1180 if (index == esc) {
1181 *dmv_x = get_bits(gb, v->k_x);
1182 *dmv_y = get_bits(gb, v->k_y);
1183 if (v->numref) {
1184 if (pred_flag) {
1185 *pred_flag = *dmv_y & 1;
1186 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1187 } else {
1188 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1189 }
1190 }
1191 }
1192 else {
1193 if (extend_x)
1194 offs_tab = offset_table2;
1195 else
1196 offs_tab = offset_table1;
1197 index1 = (index + 1) % 9;
1198 if (index1 != 0) {
1199 val = get_bits(gb, index1 + extend_x);
1200 sign = 0 -(val & 1);
1201 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1202 } else
1203 *dmv_x = 0;
1204 if (extend_y)
1205 offs_tab = offset_table2;
1206 else
1207 offs_tab = offset_table1;
1208 index1 = (index + 1) / 9;
1209 if (index1 > v->numref) {
1210 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1211 sign = 0 - (val & 1);
1212 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1213 } else
1214 *dmv_y = 0;
1215 if (v->numref && pred_flag)
1216 *pred_flag = index1 & 1;
1217 }
1218 }
1219
1220 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1221 {
1222 int scaledvalue, refdist;
1223 int scalesame1, scalesame2;
1224 int scalezone1_x, zone1offset_x;
1225 int table_index = dir ^ v->second_field;
1226
1227 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1228 refdist = v->refdist;
1229 else
1230 refdist = dir ? v->brfd : v->frfd;
1231 if (refdist > 3)
1232 refdist = 3;
1233 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1234 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1235 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1236 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1237
1238 if (FFABS(n) > 255)
1239 scaledvalue = n;
1240 else {
1241 if (FFABS(n) < scalezone1_x)
1242 scaledvalue = (n * scalesame1) >> 8;
1243 else {
1244 if (n < 0)
1245 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1246 else
1247 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1248 }
1249 }
1250 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1251 }
1252
1253 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1254 {
1255 int scaledvalue, refdist;
1256 int scalesame1, scalesame2;
1257 int scalezone1_y, zone1offset_y;
1258 int table_index = dir ^ v->second_field;
1259
1260 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1261 refdist = v->refdist;
1262 else
1263 refdist = dir ? v->brfd : v->frfd;
1264 if (refdist > 3)
1265 refdist = 3;
1266 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1267 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1268 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1269 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1270
1271 if (FFABS(n) > 63)
1272 scaledvalue = n;
1273 else {
1274 if (FFABS(n) < scalezone1_y)
1275 scaledvalue = (n * scalesame1) >> 8;
1276 else {
1277 if (n < 0)
1278 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1279 else
1280 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1281 }
1282 }
1283
1284 if (v->cur_field_type && !v->ref_field_type[dir])
1285 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1286 else
1287 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1288 }
1289
1290 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1291 {
1292 int scalezone1_x, zone1offset_x;
1293 int scaleopp1, scaleopp2, brfd;
1294 int scaledvalue;
1295
1296 brfd = FFMIN(v->brfd, 3);
1297 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1298 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1299 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1300 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1301
1302 if (FFABS(n) > 255)
1303 scaledvalue = n;
1304 else {
1305 if (FFABS(n) < scalezone1_x)
1306 scaledvalue = (n * scaleopp1) >> 8;
1307 else {
1308 if (n < 0)
1309 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1310 else
1311 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1312 }
1313 }
1314 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1315 }
1316
1317 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1318 {
1319 int scalezone1_y, zone1offset_y;
1320 int scaleopp1, scaleopp2, brfd;
1321 int scaledvalue;
1322
1323 brfd = FFMIN(v->brfd, 3);
1324 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1325 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1326 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1327 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1328
1329 if (FFABS(n) > 63)
1330 scaledvalue = n;
1331 else {
1332 if (FFABS(n) < scalezone1_y)
1333 scaledvalue = (n * scaleopp1) >> 8;
1334 else {
1335 if (n < 0)
1336 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1337 else
1338 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1339 }
1340 }
1341 if (v->cur_field_type && !v->ref_field_type[dir]) {
1342 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1343 } else {
1344 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1345 }
1346 }
1347
1348 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1349 int dim, int dir)
1350 {
1351 int brfd, scalesame;
1352 int hpel = 1 - v->s.quarter_sample;
1353
1354 n >>= hpel;
1355 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1356 if (dim)
1357 n = scaleforsame_y(v, i, n, dir) << hpel;
1358 else
1359 n = scaleforsame_x(v, n, dir) << hpel;
1360 return n;
1361 }
1362 brfd = FFMIN(v->brfd, 3);
1363 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1364
1365 n = (n * scalesame >> 8) << hpel;
1366 return n;
1367 }
1368
1369 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1370 int dim, int dir)
1371 {
1372 int refdist, scaleopp;
1373 int hpel = 1 - v->s.quarter_sample;
1374
1375 n >>= hpel;
1376 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1377 if (dim)
1378 n = scaleforopp_y(v, n, dir) << hpel;
1379 else
1380 n = scaleforopp_x(v, n) << hpel;
1381 return n;
1382 }
1383 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1384 refdist = FFMIN(v->refdist, 3);
1385 else
1386 refdist = dir ? v->brfd : v->frfd;
1387 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1388
1389 n = (n * scaleopp >> 8) << hpel;
1390 return n;
1391 }
1392
1393 /** Predict and set motion vector
1394 */
1395 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1396 int mv1, int r_x, int r_y, uint8_t* is_intra,
1397 int pred_flag, int dir)
1398 {
1399 MpegEncContext *s = &v->s;
1400 int xy, wrap, off = 0;
1401 int16_t *A, *B, *C;
1402 int px, py;
1403 int sum;
1404 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1405 int opposite, a_f, b_f, c_f;
1406 int16_t field_predA[2];
1407 int16_t field_predB[2];
1408 int16_t field_predC[2];
1409 int a_valid, b_valid, c_valid;
1410 int hybridmv_thresh, y_bias = 0;
1411
1412 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1413 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1414 mixedmv_pic = 1;
1415 else
1416 mixedmv_pic = 0;
1417 /* scale MV difference to be quad-pel */
1418 dmv_x <<= 1 - s->quarter_sample;
1419 dmv_y <<= 1 - s->quarter_sample;
1420
1421 wrap = s->b8_stride;
1422 xy = s->block_index[n];
1423
1424 if (s->mb_intra) {
1425 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1426 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1427 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1428 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1429 if (mv1) { /* duplicate motion data for 1-MV block */
1430 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1431 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1432 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1433 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1434 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1435 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1436 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1437 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1438 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1439 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1440 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1441 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1442 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1443 }
1444 return;
1445 }
1446
1447 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1448 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1449 if (mv1) {
1450 if (v->field_mode && mixedmv_pic)
1451 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1452 else
1453 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1454 } else {
1455 //in 4-MV mode different blocks have different B predictor position
1456 switch (n) {
1457 case 0:
1458 off = (s->mb_x > 0) ? -1 : 1;
1459 break;
1460 case 1:
1461 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1462 break;
1463 case 2:
1464 off = 1;
1465 break;
1466 case 3:
1467 off = -1;
1468 }
1469 }
1470 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1471
1472 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1473 b_valid = a_valid && (s->mb_width > 1);
1474 c_valid = s->mb_x || (n == 1 || n == 3);
1475 if (v->field_mode) {
1476 a_valid = a_valid && !is_intra[xy - wrap];
1477 b_valid = b_valid && !is_intra[xy - wrap + off];
1478 c_valid = c_valid && !is_intra[xy - 1];
1479 }
1480
1481 if (a_valid) {
1482 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1483 num_oppfield += a_f;
1484 num_samefield += 1 - a_f;
1485 field_predA[0] = A[0];
1486 field_predA[1] = A[1];
1487 } else {
1488 field_predA[0] = field_predA[1] = 0;
1489 a_f = 0;
1490 }
1491 if (b_valid) {
1492 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1493 num_oppfield += b_f;
1494 num_samefield += 1 - b_f;
1495 field_predB[0] = B[0];
1496 field_predB[1] = B[1];
1497 } else {
1498 field_predB[0] = field_predB[1] = 0;
1499 b_f = 0;
1500 }
1501 if (c_valid) {
1502 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1503 num_oppfield += c_f;
1504 num_samefield += 1 - c_f;
1505 field_predC[0] = C[0];
1506 field_predC[1] = C[1];
1507 } else {
1508 field_predC[0] = field_predC[1] = 0;
1509 c_f = 0;
1510 }
1511
1512 if (v->field_mode) {
1513 if (!v->numref)
1514 // REFFIELD determines if the last field or the second-last field is
1515 // to be used as reference
1516 opposite = 1 - v->reffield;
1517 else {
1518 if (num_samefield <= num_oppfield)
1519 opposite = 1 - pred_flag;
1520 else
1521 opposite = pred_flag;
1522 }
1523 } else
1524 opposite = 0;
1525 if (opposite) {
1526 if (a_valid && !a_f) {
1527 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1528 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1529 }
1530 if (b_valid && !b_f) {
1531 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1532 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1533 }
1534 if (c_valid && !c_f) {
1535 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1536 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1537 }
1538 v->mv_f[dir][xy + v->blocks_off] = 1;
1539 v->ref_field_type[dir] = !v->cur_field_type;
1540 } else {
1541 if (a_valid && a_f) {
1542 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1543 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1544 }
1545 if (b_valid && b_f) {
1546 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1547 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1548 }
1549 if (c_valid && c_f) {
1550 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1551 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1552 }
1553 v->mv_f[dir][xy + v->blocks_off] = 0;
1554 v->ref_field_type[dir] = v->cur_field_type;
1555 }
1556
1557 if (a_valid) {
1558 px = field_predA[0];
1559 py = field_predA[1];
1560 } else if (c_valid) {
1561 px = field_predC[0];
1562 py = field_predC[1];
1563 } else if (b_valid) {
1564 px = field_predB[0];
1565 py = field_predB[1];
1566 } else {
1567 px = 0;
1568 py = 0;
1569 }
1570
1571 if (num_samefield + num_oppfield > 1) {
1572 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1573 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1574 }
1575
1576 /* Pullback MV as specified in 8.3.5.3.4 */
1577 if (!v->field_mode) {
1578 int qx, qy, X, Y;
1579 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1580 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1581 X = (s->mb_width << 6) - 4;
1582 Y = (s->mb_height << 6) - 4;
1583 if (mv1) {
1584 if (qx + px < -60) px = -60 - qx;
1585 if (qy + py < -60) py = -60 - qy;
1586 } else {
1587 if (qx + px < -28) px = -28 - qx;
1588 if (qy + py < -28) py = -28 - qy;
1589 }
1590 if (qx + px > X) px = X - qx;
1591 if (qy + py > Y) py = Y - qy;
1592 }
1593
1594 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1595 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1596 hybridmv_thresh = 32;
1597 if (a_valid && c_valid) {
1598 if (is_intra[xy - wrap])
1599 sum = FFABS(px) + FFABS(py);
1600 else
1601 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1602 if (sum > hybridmv_thresh) {
1603 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1604 px = field_predA[0];
1605 py = field_predA[1];
1606 } else {
1607 px = field_predC[0];
1608 py = field_predC[1];
1609 }
1610 } else {
1611 if (is_intra[xy - 1])
1612 sum = FFABS(px) + FFABS(py);
1613 else
1614 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1615 if (sum > hybridmv_thresh) {
1616 if (get_bits1(&s->gb)) {
1617 px = field_predA[0];
1618 py = field_predA[1];
1619 } else {
1620 px = field_predC[0];
1621 py = field_predC[1];
1622 }
1623 }
1624 }
1625 }
1626 }
1627
1628 if (v->field_mode && v->numref)
1629 r_y >>= 1;
1630 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1631 y_bias = 1;
1632 /* store MV using signed modulus of MV range defined in 4.11 */
1633 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1634 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1635 if (mv1) { /* duplicate motion data for 1-MV block */
1636 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1637 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1638 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1639 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1640 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1641 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1642 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1643 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];
1644 }
1645 }
1646
1647 /** Predict and set motion vector for interlaced frame picture MBs
1648 */
1649 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1650 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1651 {
1652 MpegEncContext *s = &v->s;
1653 int xy, wrap, off = 0;
1654 int A[2], B[2], C[2];
1655 int px, py;
1656 int a_valid = 0, b_valid = 0, c_valid = 0;
1657 int field_a, field_b, field_c; // 0: same, 1: opposit
1658 int total_valid, num_samefield, num_oppfield;
1659 int pos_c, pos_b, n_adj;
1660
1661 wrap = s->b8_stride;
1662 xy = s->block_index[n];
1663
1664 if (s->mb_intra) {
1665 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1666 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1667 s->current_picture.motion_val[1][xy][0] = 0;
1668 s->current_picture.motion_val[1][xy][1] = 0;
1669 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1670 s->current_picture.motion_val[0][xy + 1][0] = 0;
1671 s->current_picture.motion_val[0][xy + 1][1] = 0;
1672 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1673 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1674 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1675 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1676 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1677 s->current_picture.motion_val[1][xy + 1][0] = 0;
1678 s->current_picture.motion_val[1][xy + 1][1] = 0;
1679 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1680 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1681 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1682 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1683 }
1684 return;
1685 }
1686
1687 off = ((n == 0) || (n == 1)) ? 1 : -1;
1688 /* predict A */
1689 if (s->mb_x || (n == 1) || (n == 3)) {
1690 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1691 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1692 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1693 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1694 a_valid = 1;
1695 } else { // current block has frame mv and cand. has field MV (so average)
1696 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1697 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1698 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1699 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1700 a_valid = 1;
1701 }
1702 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1703 a_valid = 0;
1704 A[0] = A[1] = 0;
1705 }
1706 } else
1707 A[0] = A[1] = 0;
1708 /* Predict B and C */
1709 B[0] = B[1] = C[0] = C[1] = 0;
1710 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1711 if (!s->first_slice_line) {
1712 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1713 b_valid = 1;
1714 n_adj = n | 2;
1715 pos_b = s->block_index[n_adj] - 2 * wrap;
1716 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1717 n_adj = (n & 2) | (n & 1);
1718 }
1719 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1720 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1721 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1722 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1723 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1724 }
1725 }
1726 if (s->mb_width > 1) {
1727 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1728 c_valid = 1;
1729 n_adj = 2;
1730 pos_c = s->block_index[2] - 2 * wrap + 2;
1731 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1732 n_adj = n & 2;
1733 }
1734 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1735 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1736 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1737 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1738 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1739 }
1740 if (s->mb_x == s->mb_width - 1) {
1741 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1742 c_valid = 1;
1743 n_adj = 3;
1744 pos_c = s->block_index[3] - 2 * wrap - 2;
1745 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1746 n_adj = n | 1;
1747 }
1748 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1749 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1750 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1751 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1752 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1753 }
1754 } else
1755 c_valid = 0;
1756 }
1757 }
1758 }
1759 }
1760 } else {
1761 pos_b = s->block_index[1];
1762 b_valid = 1;
1763 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1764 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1765 pos_c = s->block_index[0];
1766 c_valid = 1;
1767 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1768 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1769 }
1770
1771 total_valid = a_valid + b_valid + c_valid;
1772 // check if predictor A is out of bounds
1773 if (!s->mb_x && !(n == 1 || n == 3)) {
1774 A[0] = A[1] = 0;
1775 }
1776 // check if predictor B is out of bounds
1777 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1778 B[0] = B[1] = C[0] = C[1] = 0;
1779 }
1780 if (!v->blk_mv_type[xy]) {
1781 if (s->mb_width == 1) {
1782 px = B[0];
1783 py = B[1];
1784 } else {
1785 if (total_valid >= 2) {
1786 px = mid_pred(A[0], B[0], C[0]);
1787 py = mid_pred(A[1], B[1], C[1]);
1788 } else if (total_valid) {
1789 if (a_valid) { px = A[0]; py = A[1]; }
1790 if (b_valid) { px = B[0]; py = B[1]; }
1791 if (c_valid) { px = C[0]; py = C[1]; }
1792 } else
1793 px = py = 0;
1794 }
1795 } else {
1796 if (a_valid)
1797 field_a = (A[1] & 4) ? 1 : 0;
1798 else
1799 field_a = 0;
1800 if (b_valid)
1801 field_b = (B[1] & 4) ? 1 : 0;
1802 else
1803 field_b = 0;
1804 if (c_valid)
1805 field_c = (C[1] & 4) ? 1 : 0;
1806 else
1807 field_c = 0;
1808
1809 num_oppfield = field_a + field_b + field_c;
1810 num_samefield = total_valid - num_oppfield;
1811 if (total_valid == 3) {
1812 if ((num_samefield == 3) || (num_oppfield == 3)) {
1813 px = mid_pred(A[0], B[0], C[0]);
1814 py = mid_pred(A[1], B[1], C[1]);
1815 } else if (num_samefield >= num_oppfield) {
1816 /* take one MV from same field set depending on priority
1817 the check for B may not be necessary */
1818 px = !field_a ? A[0] : B[0];
1819 py = !field_a ? A[1] : B[1];
1820 } else {
1821 px = field_a ? A[0] : B[0];
1822 py = field_a ? A[1] : B[1];
1823 }
1824 } else if (total_valid == 2) {
1825 if (num_samefield >= num_oppfield) {
1826 if (!field_a && a_valid) {
1827 px = A[0];
1828 py = A[1];
1829 } else if (!field_b && b_valid) {
1830 px = B[0];
1831 py = B[1];
1832 } else if (c_valid) {
1833 px = C[0];
1834 py = C[1];
1835 } else px = py = 0;
1836 } else {
1837 if (field_a && a_valid) {
1838 px = A[0];
1839 py = A[1];
1840 } else if (field_b && b_valid) {
1841 px = B[0];
1842 py = B[1];
1843 } else if (c_valid) {
1844 px = C[0];
1845 py = C[1];
1846 }
1847 }
1848 } else if (total_valid == 1) {
1849 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1850 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1851 } else
1852 px = py = 0;
1853 }
1854
1855 /* store MV using signed modulus of MV range defined in 4.11 */
1856 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1857 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1858 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1859 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1860 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1861 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1862 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1863 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1864 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1865 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1866 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1867 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1868 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1869 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1870 }
1871 }
1872
1873 /** Motion compensation for direct or interpolated blocks in B-frames
1874 */
1875 static void vc1_interp_mc(VC1Context *v)
1876 {
1877 MpegEncContext *s = &v->s;
1878 H264ChromaContext *h264chroma = &v->h264chroma;
1879 uint8_t *srcY, *srcU, *srcV;
1880 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1881 int off, off_uv;
1882 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1883 int use_ic = v->next_use_ic;
1884
1885 if (!v->field_mode && !v->s.next_picture.f.data[0])
1886 return;
1887
1888 mx = s->mv[1][0][0];
1889 my = s->mv[1][0][1];
1890 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1891 uvmy = (my + ((my & 3) == 3)) >> 1;
1892 if (v->field_mode) {
1893 if (v->cur_field_type != v->ref_field_type[1])
1894 my = my - 2 + 4 * v->cur_field_type;
1895 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1896 }
1897 if (v->fastuvmc) {
1898 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1899 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1900 }
1901 srcY = s->next_picture.f.data[0];
1902 srcU = s->next_picture.f.data[1];
1903 srcV = s->next_picture.f.data[2];
1904
1905 src_x = s->mb_x * 16 + (mx >> 2);
1906 src_y = s->mb_y * 16 + (my >> 2);
1907 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1908 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1909
1910 if (v->profile != PROFILE_ADVANCED) {
1911 src_x = av_clip( src_x, -16, s->mb_width * 16);
1912 src_y = av_clip( src_y, -16, s->mb_height * 16);
1913 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1914 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1915 } else {
1916 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1917 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1918 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1919 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1920 }
1921
1922 srcY += src_y * s->linesize + src_x;
1923 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1924 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1925
1926 if (v->field_mode && v->ref_field_type[1]) {
1927 srcY += s->current_picture_ptr->f.linesize[0];
1928 srcU += s->current_picture_ptr->f.linesize[1];
1929 srcV += s->current_picture_ptr->f.linesize[2];
1930 }
1931
1932 /* for grayscale we should not try to read from unknown area */
1933 if (s->flags & CODEC_FLAG_GRAY) {
1934 srcU = s->edge_emu_buffer + 18 * s->linesize;
1935 srcV = s->edge_emu_buffer + 18 * s->linesize;
1936 }
1937
1938 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1939 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1940 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1941 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1942
1943 srcY -= s->mspel * (1 + s->linesize);
1944 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1945 17 + s->mspel * 2, 17 + s->mspel * 2,
1946 src_x - s->mspel, src_y - s->mspel,
1947 s->h_edge_pos, v_edge_pos);
1948 srcY = s->edge_emu_buffer;
1949 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1950 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1951 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1952 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1953 srcU = uvbuf;
1954 srcV = uvbuf + 16;
1955 /* if we deal with range reduction we need to scale source blocks */
1956 if (v->rangeredfrm) {
1957 int i, j;
1958 uint8_t *src, *src2;
1959
1960 src = srcY;
1961 for (j = 0; j < 17 + s->mspel * 2; j++) {
1962 for (i = 0; i < 17 + s->mspel * 2; i++)
1963 src[i] = ((src[i] - 128) >> 1) + 128;
1964 src += s->linesize;
1965 }
1966 src = srcU;
1967 src2 = srcV;
1968 for (j = 0; j < 9; j++) {
1969 for (i = 0; i < 9; i++) {
1970 src[i] = ((src[i] - 128) >> 1) + 128;
1971 src2[i] = ((src2[i] - 128) >> 1) + 128;
1972 }
1973 src += s->uvlinesize;
1974 src2 += s->uvlinesize;
1975 }
1976 }
1977
1978 if (use_ic) {
1979 uint8_t (*luty )[256] = v->next_luty;
1980 uint8_t (*lutuv)[256] = v->next_lutuv;
1981 int i, j;
1982 uint8_t *src, *src2;
1983
1984 src = srcY;
1985 for (j = 0; j < 17 + s->mspel * 2; j++) {
1986 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
1987 for (i = 0; i < 17 + s->mspel * 2; i++)
1988 src[i] = luty[f][src[i]];
1989 src += s->linesize;
1990 }
1991 src = srcU;
1992 src2 = srcV;
1993 for (j = 0; j < 9; j++) {
1994 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
1995 for (i = 0; i < 9; i++) {
1996 src[i] = lutuv[f][src[i]];
1997 src2[i] = lutuv[f][src2[i]];
1998 }
1999 src += s->uvlinesize;
2000 src2 += s->uvlinesize;
2001 }
2002 }
2003 srcY += s->mspel * (1 + s->linesize);
2004 }
2005
2006 off = 0;
2007 off_uv = 0;
2008
2009 if (s->mspel) {
2010 dxy = ((my & 3) << 2) | (mx & 3);
2011 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2012 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2013 srcY += s->linesize * 8;
2014 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2015 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2016 } else { // hpel mc
2017 dxy = (my & 2) | ((mx & 2) >> 1);
2018
2019 if (!v->rnd)
2020 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2021 else
2022 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2023 }
2024
2025 if (s->flags & CODEC_FLAG_GRAY) return;
2026 /* Chroma MC always uses qpel blilinear */
2027 uvmx = (uvmx & 3) << 1;
2028 uvmy = (uvmy & 3) << 1;
2029 if (!v->rnd) {
2030 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2031 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2032 } else {
2033 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2034 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2035 }
2036 }
2037
2038 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2039 {
2040 int n = bfrac;
2041
2042 #if B_FRACTION_DEN==256
2043 if (inv)
2044 n -= 256;
2045 if (!qs)
2046 return 2 * ((value * n + 255) >> 9);
2047 return (value * n + 128) >> 8;
2048 #else
2049 if (inv)
2050 n -= B_FRACTION_DEN;
2051 if (!qs)
2052 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2053 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2054 #endif
2055 }
2056
2057 /** Reconstruct motion vector for B-frame and do motion compensation
2058 */
2059 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2060 int direct, int mode)
2061 {
2062 if (direct) {
2063 vc1_mc_1mv(v, 0);
2064 vc1_interp_mc(v);
2065 return;
2066 }
2067 if (mode == BMV_TYPE_INTERPOLATED) {
2068 vc1_mc_1mv(v, 0);
2069 vc1_interp_mc(v);
2070 return;
2071 }
2072
2073 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2074 }
2075
2076 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2077 int direct, int mvtype)
2078 {
2079 MpegEncContext *s = &v->s;
2080 int xy, wrap, off = 0;
2081 int16_t *A, *B, *C;
2082 int px, py;
2083 int sum;
2084 int r_x, r_y;
2085 const uint8_t *is_intra = v->mb_type[0];
2086
2087 r_x = v->range_x;
2088 r_y = v->range_y;
2089 /* scale MV difference to be quad-pel */
2090 dmv_x[0] <<= 1 - s->quarter_sample;
2091 dmv_y[0] <<= 1 - s->quarter_sample;
2092 dmv_x[1] <<= 1 - s->quarter_sample;
2093 dmv_y[1] <<= 1 - s->quarter_sample;
2094
2095 wrap = s->b8_stride;
2096 xy = s->block_index[0];
2097
2098 if (s->mb_intra) {
2099 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2100 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2101 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2102 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2103 return;
2104 }
2105 if (!v->field_mode) {
2106 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2107 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2108 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2109 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2110
2111 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2112 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));
2113 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));
2114 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));
2115 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));
2116 }
2117 if (direct) {
2118 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2119 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2120 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2121 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2122 return;
2123 }
2124
2125 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2126 C = s->current_picture.motion_val[0][xy - 2];
2127 A = s->current_picture.motion_val[0][xy - wrap * 2];
2128 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2129 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2130
2131 if (!s->mb_x) C[0] = C[1] = 0;
2132 if (!s->first_slice_line) { // predictor A is not out of bounds
2133 if (s->mb_width == 1) {
2134 px = A[0];
2135 py = A[1];
2136 } else {
2137 px = mid_pred(A[0], B[0], C[0]);
2138 py = mid_pred(A[1], B[1], C[1]);
2139 }
2140 } else if (s->mb_x) { // predictor C is not out of bounds
2141 px = C[0];
2142 py = C[1];
2143 } else {
2144 px = py = 0;
2145 }
2146 /* Pullback MV as specified in 8.3.5.3.4 */
2147 {
2148 int qx, qy, X, Y;
2149 if (v->profile < PROFILE_ADVANCED) {
2150 qx = (s->mb_x << 5);
2151 qy = (s->mb_y << 5);
2152 X = (s->mb_width << 5) - 4;
2153 Y = (s->mb_height << 5) - 4;
2154 if (qx + px < -28) px = -28 - qx;
2155 if (qy + py < -28) py = -28 - qy;
2156 if (qx + px > X) px = X - qx;
2157 if (qy + py > Y) py = Y - qy;
2158 } else {
2159 qx = (s->mb_x << 6);
2160 qy = (s->mb_y << 6);
2161 X = (s->mb_width << 6) - 4;
2162 Y = (s->mb_height << 6) - 4;
2163 if (qx + px < -60) px = -60 - qx;
2164 if (qy + py < -60) py = -60 - qy;
2165 if (qx + px > X) px = X - qx;
2166 if (qy + py > Y) py = Y - qy;
2167 }
2168 }
2169 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2170 if (0 && !s->first_slice_line && s->mb_x) {
2171 if (is_intra[xy - wrap])
2172 sum = FFABS(px) + FFABS(py);
2173 else
2174 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2175 if (sum > 32) {
2176 if (get_bits1(&s->gb)) {
2177 px = A[0];
2178 py = A[1];
2179 } else {
2180 px = C[0];
2181 py = C[1];
2182 }
2183 } else {
2184 if (is_intra[xy - 2])
2185 sum = FFABS(px) + FFABS(py);
2186 else
2187 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2188 if (sum > 32) {
2189 if (get_bits1(&s->gb)) {
2190 px = A[0];
2191 py = A[1];
2192 } else {
2193 px = C[0];
2194 py = C[1];
2195 }
2196 }
2197 }
2198 }
2199 /* store MV using signed modulus of MV range defined in 4.11 */
2200 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2201 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2202 }
2203 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2204 C = s->current_picture.motion_val[1][xy - 2];
2205 A = s->current_picture.motion_val[1][xy - wrap * 2];
2206 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2207 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2208
2209 if (!s->mb_x)
2210 C[0] = C[1] = 0;
2211 if (!s->first_slice_line) { // predictor A is not out of bounds
2212 if (s->mb_width == 1) {
2213 px = A[0];
2214 py = A[1];
2215 } else {
2216 px = mid_pred(A[0], B[0], C[0]);
2217 py = mid_pred(A[1], B[1], C[1]);
2218 }
2219 } else if (s->mb_x) { // predictor C is not out of bounds
2220 px = C[0];
2221 py = C[1];
2222 } else {
2223 px = py = 0;
2224 }
2225 /* Pullback MV as specified in 8.3.5.3.4 */
2226 {
2227 int qx, qy, X, Y;
2228 if (v->profile < PROFILE_ADVANCED) {
2229 qx = (s->mb_x << 5);
2230 qy = (s->mb_y << 5);
2231 X = (s->mb_width << 5) - 4;
2232 Y = (s->mb_height << 5) - 4;
2233 if (qx + px < -28) px = -28 - qx;
2234 if (qy + py < -28) py = -28 - qy;
2235 if (qx + px > X) px = X - qx;
2236 if (qy + py > Y) py = Y - qy;
2237 } else {
2238 qx = (s->mb_x << 6);
2239 qy = (s->mb_y << 6);
2240 X = (s->mb_width << 6) - 4;
2241 Y = (s->mb_height << 6) - 4;
2242 if (qx + px < -60) px = -60 - qx;
2243 if (qy + py < -60) py = -60 - qy;
2244 if (qx + px > X) px = X - qx;
2245 if (qy + py > Y) py = Y - qy;
2246 }
2247 }
2248 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2249 if (0 && !s->first_slice_line && s->mb_x) {
2250 if (is_intra[xy - wrap])
2251 sum = FFABS(px) + FFABS(py);
2252 else
2253 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2254 if (sum > 32) {
2255 if (get_bits1(&s->gb)) {
2256 px = A[0];
2257 py = A[1];
2258 } else {
2259 px = C[0];
2260 py = C[1];
2261 }
2262 } else {
2263 if (is_intra[xy - 2])
2264 sum = FFABS(px) + FFABS(py);
2265 else
2266 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2267 if (sum > 32) {
2268 if (get_bits1(&s->gb)) {
2269 px = A[0];
2270 py = A[1];
2271 } else {
2272 px = C[0];
2273 py = C[1];
2274 }
2275 }
2276 }
2277 }
2278 /* store MV using signed modulus of MV range defined in 4.11 */
2279
2280 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2281 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2282 }
2283 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2284 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2285 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2286 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2287 }
2288
2289 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2290 {
2291 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2292 MpegEncContext *s = &v->s;
2293 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2294
2295 if (v->bmvtype == BMV_TYPE_DIRECT) {
2296 int total_opp, k, f;
2297 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2298 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2299 v->bfraction, 0, s->quarter_sample);
2300 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2301 v->bfraction, 0, s->quarter_sample);
2302 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2303 v->bfraction, 1, s->quarter_sample);
2304 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2305 v->bfraction, 1, s->quarter_sample);
2306
2307 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2308 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2309 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2310 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2311 f = (total_opp > 2) ? 1 : 0;
2312 } else {
2313 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2314 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2315 f = 0;
2316 }
2317 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2318 for (k = 0; k < 4; k++) {
2319 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2320 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2321 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2322 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2323 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2324 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2325 }
2326 return;
2327 }
2328 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2329 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);
2330 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);
2331 return;
2332 }
2333 if (dir) { // backward
2334 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);
2335 if (n == 3 || mv1) {
2336 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2337 }
2338 } else { // forward
2339 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);
2340 if (n == 3 || mv1) {
2341 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2342 }
2343 }
2344 }
2345
2346 /** Get predicted DC value for I-frames only
2347 * prediction dir: left=0, top=1
2348 * @param s MpegEncContext
2349 * @param overlap flag indicating that overlap filtering is used
2350 * @param pq integer part of picture quantizer
2351 * @param[in] n block index in the current MB
2352 * @param dc_val_ptr Pointer to DC predictor
2353 * @param dir_ptr Prediction direction for use in AC prediction
2354 */
2355 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2356 int16_t **dc_val_ptr, int *dir_ptr)
2357 {
2358 int a, b, c, wrap, pred, scale;
2359 int16_t *dc_val;
2360 static const uint16_t dcpred[32] = {
2361 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2362 114, 102, 93, 85, 79, 73, 68, 64,
2363 60, 57, 54, 51, 49, 47, 45, 43,
2364 41, 39, 38, 37, 35, 34, 33
2365 };
2366
2367 /* find prediction - wmv3_dc_scale always used here in fact */
2368 if (n < 4) scale = s->y_dc_scale;
2369 else scale = s->c_dc_scale;
2370
2371 wrap = s->block_wrap[n];
2372 dc_val = s->dc_val[0] + s->block_index[n];
2373
2374 /* B A
2375 * C X
2376 */
2377 c = dc_val[ - 1];
2378 b = dc_val[ - 1 - wrap];
2379 a = dc_val[ - wrap];
2380
2381 if (pq < 9 || !overlap) {
2382 /* Set outer values */
2383 if (s->first_slice_line && (n != 2 && n != 3))
2384 b = a = dcpred[scale];
2385 if (s->mb_x == 0 && (n != 1 && n != 3))
2386 b = c = dcpred[scale];
2387 } else {
2388 /* Set outer values */
2389 if (s->first_slice_line && (n != 2 && n != 3))
2390 b = a = 0;
2391 if (s->mb_x == 0 && (n != 1 && n != 3))
2392 b = c = 0;
2393 }
2394
2395 if (abs(a - b) <= abs(b - c)) {
2396 pred = c;
2397 *dir_ptr = 1; // left
2398 } else {
2399 pred = a;
2400 *dir_ptr = 0; // top
2401 }
2402
2403 /* update predictor */
2404 *dc_val_ptr = &dc_val[0];
2405 return pred;
2406 }
2407
2408
2409 /** Get predicted DC value
2410 * prediction dir: left=0, top=1
2411 * @param s MpegEncContext
2412 * @param overlap flag indicating that overlap filtering is used
2413 * @param pq integer part of picture quantizer
2414 * @param[in] n block index in the current MB
2415 * @param a_avail flag indicating top block availability
2416 * @param c_avail flag indicating left block availability
2417 * @param dc_val_ptr Pointer to DC predictor
2418 * @param dir_ptr Prediction direction for use in AC prediction
2419 */
2420 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2421 int a_avail, int c_avail,
2422 int16_t **dc_val_ptr, int *dir_ptr)
2423 {
2424 int a, b, c, wrap, pred;
2425 int16_t *dc_val;
2426 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2427 int q1, q2 = 0;
2428 int dqscale_index;
2429
2430 wrap = s->block_wrap[n];
2431 dc_val = s->dc_val[0] + s->block_index[n];
2432
2433 /* B A
2434 * C X
2435 */
2436 c = dc_val[ - 1];
2437 b = dc_val[ - 1 - wrap];
2438 a = dc_val[ - wrap];
2439 /* scale predictors if needed */
2440 q1 = s->current_picture.qscale_table[mb_pos];
2441 dqscale_index = s->y_dc_scale_table[q1] - 1;
2442 if (dqscale_index < 0)
2443 return 0;
2444 if (c_avail && (n != 1 && n != 3)) {
2445 q2 = s->current_picture.qscale_table[mb_pos - 1];
2446 if (q2 && q2 != q1)
2447 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2448 }
2449 if (a_avail && (n != 2 && n != 3)) {
2450 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2451 if (q2 && q2 != q1)
2452 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2453 }
2454 if (a_avail && c_avail && (n != 3)) {
2455 int off = mb_pos;
2456 if (n != 1)
2457 off--;
2458 if (n != 2)
2459 off -= s->mb_stride;
2460 q2 = s->current_picture.qscale_table[off];
2461 if (q2 && q2 != q1)
2462 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2463 }
2464
2465 if (a_avail && c_avail) {
2466 if (abs(a - b) <= abs(b - c)) {
2467 pred = c;
2468 *dir_ptr = 1; // left
2469 } else {
2470 pred = a;
2471 *dir_ptr = 0; // top
2472 }
2473 } else if (a_avail) {
2474 pred = a;
2475 *dir_ptr = 0; // top
2476 } else if (c_avail) {
2477 pred = c;
2478 *dir_ptr = 1; // left
2479 } else {
2480 pred = 0;
2481 *dir_ptr = 1; // left
2482 }
2483
2484 /* update predictor */
2485 *dc_val_ptr = &dc_val[0];
2486 return pred;
2487 }
2488
2489 /** @} */ // Block group
2490
2491 /**
2492 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2493 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2494 * @{
2495 */
2496
2497 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2498 uint8_t **coded_block_ptr)
2499 {
2500 int xy, wrap, pred, a, b, c;
2501
2502 xy = s->block_index[n];
2503 wrap = s->b8_stride;
2504
2505 /* B C
2506 * A X
2507 */
2508 a = s->coded_block[xy - 1 ];
2509 b = s->coded_block[xy - 1 - wrap];
2510 c = s->coded_block[xy - wrap];
2511
2512 if (b == c) {
2513 pred = a;
2514 } else {
2515 pred = c;
2516 }
2517
2518 /* store value */
2519 *coded_block_ptr = &s->coded_block[xy];
2520
2521 return pred;
2522 }
2523
2524 /**
2525 * Decode one AC coefficient
2526 * @param v The VC1 context
2527 * @param last Last coefficient
2528 * @param skip How much zero coefficients to skip
2529 * @param value Decoded AC coefficient value
2530 * @param codingset set of VLC to decode data
2531 * @see 8.1.3.4
2532 */
2533 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2534 int *value, int codingset)
2535 {
2536 GetBitContext *gb = &v->s.gb;
2537 int index, escape, run = 0, level = 0, lst = 0;
2538
2539 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2540 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2541 run = vc1_index_decode_table[codingset][index][0];
2542 level = vc1_index_decode_table[codingset][index][1];
2543 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2544 if (get_bits1(gb))
2545 level = -level;
2546 } else {
2547 escape = decode210(gb);
2548 if (escape != 2) {
2549 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2550 run = vc1_index_decode_table[codingset][index][0];
2551 level = vc1_index_decode_table[codingset][index][1];
2552 lst = index >= vc1_last_decode_table[codingset];
2553 if (escape == 0) {
2554 if (lst)
2555 level += vc1_last_delta_level_table[codingset][run];
2556 else
2557 level += vc1_delta_level_table[codingset][run];
2558 } else {
2559 if (lst)
2560 run += vc1_last_delta_run_table[codingset][level] + 1;
2561 else
2562 run += vc1_delta_run_table[codingset][level] + 1;
2563 }
2564 if (get_bits1(gb))
2565 level = -level;
2566 } else {
2567 int sign;
2568 lst = get_bits1(gb);
2569 if (v->s.esc3_level_length == 0) {
2570 if (v->pq < 8 || v->dquantfrm) { // table 59
2571 v->s.esc3_level_length = get_bits(gb, 3);
2572 if (!v->s.esc3_level_length)
2573 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2574 } else { // table 60
2575 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2576 }
2577 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2578 }
2579 run = get_bits(gb, v->s.esc3_run_length);
2580 sign = get_bits1(gb);
2581 level = get_bits(gb, v->s.esc3_level_length);
2582 if (sign)
2583 level = -level;
2584 }
2585 }
2586
2587 *last = lst;
2588 *skip = run;
2589 *value = level;
2590 }
2591
2592 /** Decode intra block in intra frames - should be faster than decode_intra_block
2593 * @param v VC1Context
2594 * @param block block to decode
2595 * @param[in] n subblock index
2596 * @param coded are AC coeffs present or not
2597 * @param codingset set of VLC to decode data
2598 */
2599 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2600 int coded, int codingset)
2601 {
2602 GetBitContext *gb = &v->s.gb;
2603 MpegEncContext *s = &v->s;
2604 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2605 int i;
2606 int16_t *dc_val;
2607 int16_t *ac_val, *ac_val2;
2608 int dcdiff;
2609
2610 /* Get DC differential */
2611 if (n < 4) {
2612 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2613 } else {
2614 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2615 }
2616 if (dcdiff < 0) {
2617 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2618 return -1;
2619 }
2620 if (dcdiff) {
2621 if (dcdiff == 119 /* ESC index value */) {
2622 /* TODO: Optimize */
2623 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2624 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2625 else dcdiff = get_bits(gb, 8);
2626 } else {
2627 if (v->pq == 1)
2628 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2629 else if (v->pq == 2)
2630 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2631 }
2632 if (get_bits1(gb))
2633 dcdiff = -dcdiff;
2634 }
2635
2636 /* Prediction */
2637 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2638 *dc_val = dcdiff;
2639
2640 /* Store the quantized DC coeff, used for prediction */
2641 if (n < 4) {
2642 block[0] = dcdiff * s->y_dc_scale;
2643 } else {
2644 block[0] = dcdiff * s->c_dc_scale;
2645 }
2646 /* Skip ? */
2647 if (!coded) {
2648 goto not_coded;
2649 }
2650
2651 // AC Decoding
2652 i = 1;
2653
2654 {
2655 int last = 0, skip, value;
2656 const uint8_t *zz_table;
2657 int scale;
2658 int k;
2659
2660 scale = v->pq * 2 + v->halfpq;
2661
2662 if (v->s.ac_pred) {
2663 if (!dc_pred_dir)
2664 zz_table = v->zz_8x8[2];
2665 else
2666 zz_table = v->zz_8x8[3];
2667 } else
2668 zz_table = v->zz_8x8[1];
2669
2670 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2671 ac_val2 = ac_val;
2672 if (dc_pred_dir) // left
2673 ac_val -= 16;
2674 else // top
2675 ac_val -= 16 * s->block_wrap[n];
2676
2677 while (!last) {
2678 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2679 i += skip;
2680 if (i > 63)
2681 break;
2682 block[zz_table[i++]] = value;
2683 }
2684
2685 /* apply AC prediction if needed */
2686 if (s->ac_pred) {
2687 if (dc_pred_dir) { // left
2688 for (k = 1; k < 8; k++)
2689 block[k << v->left_blk_sh] += ac_val[k];
2690 } else { // top
2691 for (k = 1; k < 8; k++)
2692 block[k << v->top_blk_sh] += ac_val[k + 8];
2693 }
2694 }
2695 /* save AC coeffs for further prediction */
2696 for (k = 1; k < 8; k++) {
2697 ac_val2[k] = block[k << v->left_blk_sh];
2698 ac_val2[k + 8] = block[k << v->top_blk_sh];
2699 }
2700
2701 /* scale AC coeffs */
2702 for (k = 1; k < 64; k++)
2703 if (block[k]) {
2704 block[k] *= scale;
2705 if (!v->pquantizer)
2706 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2707 }
2708
2709 if (s->ac_pred) i = 63;
2710 }
2711
2712 not_coded:
2713 if (!coded) {
2714 int k, scale;
2715 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2716 ac_val2 = ac_val;
2717
2718 i = 0;
2719 scale = v->pq * 2 + v->halfpq;
2720 memset(ac_val2, 0, 16 * 2);
2721 if (dc_pred_dir) { // left
2722 ac_val -= 16;
2723 if (s->ac_pred)
2724 memcpy(ac_val2, ac_val, 8 * 2);
2725 } else { // top
2726 ac_val -= 16 * s->block_wrap[n];
2727 if (s->ac_pred)
2728 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2729 }
2730
2731 /* apply AC prediction if needed */
2732 if (s->ac_pred) {
2733 if (dc_pred_dir) { //left
2734 for (k = 1; k < 8; k++) {
2735 block[k << v->left_blk_sh] = ac_val[k] * scale;
2736 if (!v->pquantizer && block[k << v->left_blk_sh])
2737 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2738 }
2739 } else { // top
2740 for (k = 1; k < 8; k++) {
2741 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2742 if (!v->pquantizer && block[k << v->top_blk_sh])
2743 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2744 }
2745 }
2746 i = 63;
2747 }
2748 }
2749 s->block_last_index[n] = i;
2750
2751 return 0;
2752 }
2753
2754 /** Decode intra block in intra frames - should be faster than decode_intra_block
2755 * @param v VC1Context
2756 * @param block block to decode
2757 * @param[in] n subblock number
2758 * @param coded are AC coeffs present or not
2759 * @param codingset set of VLC to decode data
2760 * @param mquant quantizer value for this macroblock
2761 */
2762 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2763 int coded, int codingset, int mquant)
2764 {
2765 GetBitContext *gb = &v->s.gb;
2766 MpegEncContext *s = &v->s;
2767 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2768 int i;
2769 int16_t *dc_val;
2770 int16_t *ac_val, *ac_val2;
2771 int dcdiff;
2772 int a_avail = v->a_avail, c_avail = v->c_avail;
2773 int use_pred = s->ac_pred;
2774 int scale;
2775 int q1, q2 = 0;
2776 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2777
2778 /* Get DC differential */
2779 if (n < 4) {
2780 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2781 } else {
2782 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2783 }
2784 if (dcdiff < 0) {
2785 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2786 return -1;
2787 }
2788 if (dcdiff) {
2789 if (dcdiff == 119 /* ESC index value */) {
2790 /* TODO: Optimize */
2791 if (mquant == 1) dcdiff = get_bits(gb, 10);
2792 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2793 else dcdiff = get_bits(gb, 8);
2794 } else {
2795 if (mquant == 1)
2796 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2797 else if (mquant == 2)
2798 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2799 }
2800 if (get_bits1(gb))
2801 dcdiff = -dcdiff;
2802 }
2803
2804 /* Prediction */
2805 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2806 *dc_val = dcdiff;
2807
2808 /* Store the quantized DC coeff, used for prediction */
2809 if (n < 4) {
2810 block[0] = dcdiff * s->y_dc_scale;
2811 } else {
2812 block[0] = dcdiff * s->c_dc_scale;
2813 }
2814
2815 //AC Decoding
2816 i = 1;
2817
2818 /* check if AC is needed at all */
2819 if (!a_avail && !c_avail)
2820 use_pred = 0;
2821 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2822 ac_val2 = ac_val;
2823
2824 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2825
2826 if (dc_pred_dir) // left
2827 ac_val -= 16;
2828 else // top
2829 ac_val -= 16 * s->block_wrap[n];
2830
2831 q1 = s->current_picture.qscale_table[mb_pos];
2832 if ( dc_pred_dir && c_avail && mb_pos)
2833 q2 = s->current_picture.qscale_table[mb_pos - 1];
2834 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2835 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2836 if ( dc_pred_dir && n == 1)
2837 q2 = q1;
2838 if (!dc_pred_dir && n == 2)
2839 q2 = q1;
2840 if (n == 3)
2841 q2 = q1;
2842
2843 if (coded) {
2844 int last = 0, skip, value;
2845 const uint8_t *zz_table;
2846 int k;
2847
2848 if (v->s.ac_pred) {
2849 if (!use_pred && v->fcm == ILACE_FRAME) {
2850 zz_table = v->zzi_8x8;
2851 } else {
2852 if (!dc_pred_dir) // top
2853 zz_table = v->zz_8x8[2];
2854 else // left
2855 zz_table = v->zz_8x8[3];
2856 }
2857 } else {
2858 if (v->fcm != ILACE_FRAME)
2859 zz_table = v->zz_8x8[1];
2860 else
2861 zz_table = v->zzi_8x8;
2862 }
2863
2864 while (!last) {
2865 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2866 i += skip;
2867 if (i > 63)
2868 break;
2869 block[zz_table[i++]] = value;
2870 }
2871
2872 /* apply AC prediction if needed */
2873 if (use_pred) {
2874 /* scale predictors if needed*/
2875 if (q2 && q1 != q2) {
2876 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2877 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2878
2879 if (q1 < 1)
2880 return AVERROR_INVALIDDATA;
2881 if (dc_pred_dir) { // left
2882 for (k = 1; k < 8; k++)
2883 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2884 } else { // top
2885 for (k = 1; k < 8; k++)
2886 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2887 }
2888 } else {
2889 if (dc_pred_dir) { //left
2890 for (k = 1; k < 8; k++)
2891 block[k << v->left_blk_sh] += ac_val[k];
2892 } else { //top
2893 for (k = 1; k < 8; k++)
2894 block[k << v->top_blk_sh] += ac_val[k + 8];
2895 }
2896 }
2897 }
2898 /* save AC coeffs for further prediction */
2899 for (k = 1; k < 8; k++) {
2900 ac_val2[k ] = block[k << v->left_blk_sh];
2901 ac_val2[k + 8] = block[k << v->top_blk_sh];
2902 }
2903
2904 /* scale AC coeffs */
2905 for (k = 1; k < 64; k++)
2906 if (block[k]) {
2907 block[k] *= scale;
2908 if (!v->pquantizer)
2909 block[k] += (block[k] < 0) ? -mquant : mquant;
2910 }
2911
2912 if (use_pred) i = 63;
2913 } else { // no AC coeffs
2914 int k;
2915
2916 memset(ac_val2, 0, 16 * 2);
2917 if (dc_pred_dir) { // left
2918 if (use_pred) {
2919 memcpy(ac_val2, ac_val, 8 * 2);
2920 if (q2 && q1 != q2) {
2921 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2922 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2923 if (q1 < 1)
2924 return AVERROR_INVALIDDATA;
2925 for (k = 1; k < 8; k++)
2926 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2927 }
2928 }
2929 } else { // top
2930 if (use_pred) {
2931 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2932 if (q2 && q1 != q2) {
2933 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2934 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2935 if (q1 < 1)
2936 return AVERROR_INVALIDDATA;
2937 for (k = 1; k < 8; k++)
2938 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2939 }
2940 }
2941 }
2942
2943 /* apply AC prediction if needed */
2944 if (use_pred) {
2945 if (dc_pred_dir) { // left
2946 for (k = 1; k < 8; k++) {
2947 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2948 if (!v->pquantizer && block[k << v->left_blk_sh])
2949 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2950 }
2951 } else { // top
2952 for (k = 1; k < 8; k++) {
2953 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2954 if (!v->pquantizer && block[k << v->top_blk_sh])
2955 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2956 }
2957 }
2958 i = 63;
2959 }
2960 }
2961 s->block_last_index[n] = i;
2962
2963 return 0;
2964 }
2965
2966 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2967 * @param v VC1Context
2968 * @param block block to decode
2969 * @param[in] n subblock index
2970 * @param coded are AC coeffs present or not
2971 * @param mquant block quantizer
2972 * @param codingset set of VLC to decode data
2973 */
2974 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2975 int coded, int mquant, int codingset)
2976 {
2977 GetBitContext *gb = &v->s.gb;
2978 MpegEncContext *s = &v->s;
2979 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2980 int i;
2981 int16_t *dc_val;
2982 int16_t *ac_val, *ac_val2;
2983 int dcdiff;
2984 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2985 int a_avail = v->a_avail, c_avail = v->c_avail;
2986 int use_pred = s->ac_pred;
2987 int scale;
2988 int q1, q2 = 0;
2989
2990 s->dsp.clear_block(block);
2991
2992 /* XXX: Guard against dumb values of mquant */
2993 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2994
2995 /* Set DC scale - y and c use the same */
2996 s->y_dc_scale = s->y_dc_scale_table[mquant];
2997 s->c_dc_scale = s->c_dc_scale_table[mquant];
2998
2999 /* Get DC differential */
3000 if (n < 4) {
3001 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3002 } else {
3003 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3004 }
3005 if (dcdiff < 0) {
3006 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3007 return -1;
3008 }
3009 if (dcdiff) {
3010 if (dcdiff == 119 /* ESC index value */) {
3011 /* TODO: Optimize */
3012 if (mquant == 1) dcdiff = get_bits(gb, 10);
3013 else if (mquant ==