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