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