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