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