FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
vc1dec.c
Go to the documentation of this file.
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 FFmpeg.
8  *
9  * FFmpeg 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  * FFmpeg 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 FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 /**
25  * @file
26  * VC-1 and WMV3 decoder
27  */
28 
29 #include "internal.h"
30 #include "avcodec.h"
31 #include "error_resilience.h"
32 #include "mpegvideo.h"
33 #include "h263.h"
34 #include "h264chroma.h"
35 #include "vc1.h"
36 #include "vc1data.h"
37 #include "vc1acdata.h"
38 #include "msmpeg4data.h"
39 #include "unary.h"
40 #include "mathops.h"
41 #include "vdpau_internal.h"
42 #include "libavutil/avassert.h"
43 
44 #undef NDEBUG
45 #include <assert.h>
46 
47 #define MB_INTRA_VLC_BITS 9
48 #define DC_VLC_BITS 9
49 
50 
51 // offset tables for interlaced picture MVDATA decoding
52 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
53 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
54 
55 /***********************************************************************/
56 /**
57  * @name VC-1 Bitplane decoding
58  * @see 8.7, p56
59  * @{
60  */
61 
62 /**
63  * Imode types
64  * @{
65  */
66 enum Imode {
74 };
75 /** @} */ //imode defines
76 
78 {
79  MpegEncContext *s = &v->s;
81  if (v->field_mode && !(v->second_field ^ v->tff)) {
82  s->dest[0] += s->current_picture_ptr->f.linesize[0];
83  s->dest[1] += s->current_picture_ptr->f.linesize[1];
84  s->dest[2] += s->current_picture_ptr->f.linesize[2];
85  }
86 }
87 
88 /** @} */ //Bitplane group
89 
91 {
92  MpegEncContext *s = &v->s;
93  int topleft_mb_pos, top_mb_pos;
94  int stride_y, fieldtx = 0;
95  int v_dist;
96 
97  /* The put pixels loop is always one MB row behind the decoding loop,
98  * because we can only put pixels when overlap filtering is done, and
99  * for filtering of the bottom edge of a MB, we need the next MB row
100  * present as well.
101  * Within the row, the put pixels loop is also one MB col behind the
102  * decoding loop. The reason for this is again, because for filtering
103  * of the right MB edge, we need the next MB present. */
104  if (!s->first_slice_line) {
105  if (s->mb_x) {
106  topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
107  if (v->fcm == ILACE_FRAME)
108  fieldtx = v->fieldtx_plane[topleft_mb_pos];
109  stride_y = s->linesize << fieldtx;
110  v_dist = (16 - fieldtx) >> (fieldtx == 0);
112  s->dest[0] - 16 * s->linesize - 16,
113  stride_y);
115  s->dest[0] - 16 * s->linesize - 8,
116  stride_y);
118  s->dest[0] - v_dist * s->linesize - 16,
119  stride_y);
121  s->dest[0] - v_dist * s->linesize - 8,
122  stride_y);
124  s->dest[1] - 8 * s->uvlinesize - 8,
125  s->uvlinesize);
127  s->dest[2] - 8 * s->uvlinesize - 8,
128  s->uvlinesize);
129  }
130  if (s->mb_x == s->mb_width - 1) {
131  top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
132  if (v->fcm == ILACE_FRAME)
133  fieldtx = v->fieldtx_plane[top_mb_pos];
134  stride_y = s->linesize << fieldtx;
135  v_dist = fieldtx ? 15 : 8;
137  s->dest[0] - 16 * s->linesize,
138  stride_y);
140  s->dest[0] - 16 * s->linesize + 8,
141  stride_y);
143  s->dest[0] - v_dist * s->linesize,
144  stride_y);
146  s->dest[0] - v_dist * s->linesize + 8,
147  stride_y);
149  s->dest[1] - 8 * s->uvlinesize,
150  s->uvlinesize);
152  s->dest[2] - 8 * s->uvlinesize,
153  s->uvlinesize);
154  }
155  }
156 
157 #define inc_blk_idx(idx) do { \
158  idx++; \
159  if (idx >= v->n_allocated_blks) \
160  idx = 0; \
161  } while (0)
162 
167 }
168 
169 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
170 {
171  MpegEncContext *s = &v->s;
172  int j;
173  if (!s->first_slice_line) {
174  v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
175  if (s->mb_x)
176  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
177  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
178  for (j = 0; j < 2; j++) {
179  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
180  if (s->mb_x)
181  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
182  }
183  }
184  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
185 
186  if (s->mb_y == s->end_mb_y - 1) {
187  if (s->mb_x) {
188  v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
189  v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
190  v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
191  }
192  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
193  }
194 }
195 
197 {
198  MpegEncContext *s = &v->s;
199  int j;
200 
201  /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
202  * means it runs two rows/cols behind the decoding loop. */
203  if (!s->first_slice_line) {
204  if (s->mb_x) {
205  if (s->mb_y >= s->start_mb_y + 2) {
206  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
207 
208  if (s->mb_x >= 2)
209  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
210  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
211  for (j = 0; j < 2; j++) {
212  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
213  if (s->mb_x >= 2) {
214  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
215  }
216  }
217  }
218  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
219  }
220 
221  if (s->mb_x == s->mb_width - 1) {
222  if (s->mb_y >= s->start_mb_y + 2) {
223  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
224 
225  if (s->mb_x)
226  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
227  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
228  for (j = 0; j < 2; j++) {
229  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
230  if (s->mb_x >= 2) {
231  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
232  }
233  }
234  }
235  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
236  }
237 
238  if (s->mb_y == s->end_mb_y) {
239  if (s->mb_x) {
240  if (s->mb_x >= 2)
241  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
242  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
243  if (s->mb_x >= 2) {
244  for (j = 0; j < 2; j++) {
245  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
246  }
247  }
248  }
249 
250  if (s->mb_x == s->mb_width - 1) {
251  if (s->mb_x)
252  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
253  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
254  if (s->mb_x) {
255  for (j = 0; j < 2; j++) {
256  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
257  }
258  }
259  }
260  }
261  }
262 }
263 
265 {
266  MpegEncContext *s = &v->s;
267  int mb_pos;
268 
269  if (v->condover == CONDOVER_NONE)
270  return;
271 
272  mb_pos = s->mb_x + s->mb_y * s->mb_stride;
273 
274  /* Within a MB, the horizontal overlap always runs before the vertical.
275  * To accomplish that, we run the H on left and internal borders of the
276  * currently decoded MB. Then, we wait for the next overlap iteration
277  * to do H overlap on the right edge of this MB, before moving over and
278  * running the V overlap. Therefore, the V overlap makes us trail by one
279  * MB col and the H overlap filter makes us trail by one MB row. This
280  * is reflected in the time at which we run the put_pixels loop. */
281  if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
282  if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
283  v->over_flags_plane[mb_pos - 1])) {
285  v->block[v->cur_blk_idx][0]);
287  v->block[v->cur_blk_idx][2]);
288  if (!(s->flags & CODEC_FLAG_GRAY)) {
290  v->block[v->cur_blk_idx][4]);
292  v->block[v->cur_blk_idx][5]);
293  }
294  }
296  v->block[v->cur_blk_idx][1]);
298  v->block[v->cur_blk_idx][3]);
299 
300  if (s->mb_x == s->mb_width - 1) {
301  if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
302  v->over_flags_plane[mb_pos - s->mb_stride])) {
304  v->block[v->cur_blk_idx][0]);
306  v->block[v->cur_blk_idx][1]);
307  if (!(s->flags & CODEC_FLAG_GRAY)) {
309  v->block[v->cur_blk_idx][4]);
311  v->block[v->cur_blk_idx][5]);
312  }
313  }
315  v->block[v->cur_blk_idx][2]);
317  v->block[v->cur_blk_idx][3]);
318  }
319  }
320  if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
321  if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
322  v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
324  v->block[v->left_blk_idx][0]);
326  v->block[v->left_blk_idx][1]);
327  if (!(s->flags & CODEC_FLAG_GRAY)) {
329  v->block[v->left_blk_idx][4]);
331  v->block[v->left_blk_idx][5]);
332  }
333  }
335  v->block[v->left_blk_idx][2]);
337  v->block[v->left_blk_idx][3]);
338  }
339 }
340 
341 /** Do motion compensation over 1 macroblock
342  * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
343  */
344 static void vc1_mc_1mv(VC1Context *v, int dir)
345 {
346  MpegEncContext *s = &v->s;
347  H264ChromaContext *h264chroma = &v->h264chroma;
348  uint8_t *srcY, *srcU, *srcV;
349  int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
350  int v_edge_pos = s->v_edge_pos >> v->field_mode;
351  int i;
352  uint8_t (*luty)[256], (*lutuv)[256];
353  int use_ic;
354 
355  if ((!v->field_mode ||
356  (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
357  !v->s.last_picture.f.data[0])
358  return;
359 
360  mx = s->mv[dir][0][0];
361  my = s->mv[dir][0][1];
362 
363  // store motion vectors for further use in B frames
364  if (s->pict_type == AV_PICTURE_TYPE_P) {
365  for (i = 0; i < 4; i++) {
366  s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
367  s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
368  }
369  }
370 
371  uvmx = (mx + ((mx & 3) == 3)) >> 1;
372  uvmy = (my + ((my & 3) == 3)) >> 1;
373  v->luma_mv[s->mb_x][0] = uvmx;
374  v->luma_mv[s->mb_x][1] = uvmy;
375 
376  if (v->field_mode &&
377  v->cur_field_type != v->ref_field_type[dir]) {
378  my = my - 2 + 4 * v->cur_field_type;
379  uvmy = uvmy - 2 + 4 * v->cur_field_type;
380  }
381 
382  // fastuvmc shall be ignored for interlaced frame picture
383  if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
384  uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
385  uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
386  }
387  if (!dir) {
388  if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
389  srcY = s->current_picture.f.data[0];
390  srcU = s->current_picture.f.data[1];
391  srcV = s->current_picture.f.data[2];
392  luty = v->curr_luty;
393  lutuv = v->curr_lutuv;
394  use_ic = v->curr_use_ic;
395  } else {
396  srcY = s->last_picture.f.data[0];
397  srcU = s->last_picture.f.data[1];
398  srcV = s->last_picture.f.data[2];
399  luty = v->last_luty;
400  lutuv = v->last_lutuv;
401  use_ic = v->last_use_ic;
402  }
403  } else {
404  srcY = s->next_picture.f.data[0];
405  srcU = s->next_picture.f.data[1];
406  srcV = s->next_picture.f.data[2];
407  luty = v->next_luty;
408  lutuv = v->next_lutuv;
409  use_ic = v->next_use_ic;
410  }
411 
412  if(!srcY)
413  return;
414 
415  src_x = s->mb_x * 16 + (mx >> 2);
416  src_y = s->mb_y * 16 + (my >> 2);
417  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
418  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
419 
420  if (v->profile != PROFILE_ADVANCED) {
421  src_x = av_clip( src_x, -16, s->mb_width * 16);
422  src_y = av_clip( src_y, -16, s->mb_height * 16);
423  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
424  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
425  } else {
426  src_x = av_clip( src_x, -17, s->avctx->coded_width);
427  src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
428  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
429  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
430  }
431 
432  srcY += src_y * s->linesize + src_x;
433  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
434  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
435 
436  if (v->field_mode && v->ref_field_type[dir]) {
437  srcY += s->current_picture_ptr->f.linesize[0];
438  srcU += s->current_picture_ptr->f.linesize[1];
439  srcV += s->current_picture_ptr->f.linesize[2];
440  }
441 
442  /* for grayscale we should not try to read from unknown area */
443  if (s->flags & CODEC_FLAG_GRAY) {
444  srcU = s->edge_emu_buffer + 18 * s->linesize;
445  srcV = s->edge_emu_buffer + 18 * s->linesize;
446  }
447 
448  if (v->rangeredfrm || use_ic
449  || s->h_edge_pos < 22 || v_edge_pos < 22
450  || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
451  || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
452  uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
453 
454  srcY -= s->mspel * (1 + s->linesize);
456  17 + s->mspel * 2, 17 + s->mspel * 2,
457  src_x - s->mspel, src_y - s->mspel,
458  s->h_edge_pos, v_edge_pos);
459  srcY = s->edge_emu_buffer;
460  s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
461  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
462  s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
463  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
464  srcU = uvbuf;
465  srcV = uvbuf + 16;
466  /* if we deal with range reduction we need to scale source blocks */
467  if (v->rangeredfrm) {
468  int i, j;
469  uint8_t *src, *src2;
470 
471  src = srcY;
472  for (j = 0; j < 17 + s->mspel * 2; j++) {
473  for (i = 0; i < 17 + s->mspel * 2; i++)
474  src[i] = ((src[i] - 128) >> 1) + 128;
475  src += s->linesize;
476  }
477  src = srcU;
478  src2 = srcV;
479  for (j = 0; j < 9; j++) {
480  for (i = 0; i < 9; i++) {
481  src[i] = ((src[i] - 128) >> 1) + 128;
482  src2[i] = ((src2[i] - 128) >> 1) + 128;
483  }
484  src += s->uvlinesize;
485  src2 += s->uvlinesize;
486  }
487  }
488  /* if we deal with intensity compensation we need to scale source blocks */
489  if (use_ic) {
490  int i, j;
491  uint8_t *src, *src2;
492 
493  src = srcY;
494  for (j = 0; j < 17 + s->mspel * 2; j++) {
495  int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
496  for (i = 0; i < 17 + s->mspel * 2; i++)
497  src[i] = luty[f][src[i]];
498  src += s->linesize;
499  }
500  src = srcU;
501  src2 = srcV;
502  for (j = 0; j < 9; j++) {
503  int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
504  for (i = 0; i < 9; i++) {
505  src[i] = lutuv[f][src[i]];
506  src2[i] = lutuv[f][src2[i]];
507  }
508  src += s->uvlinesize;
509  src2 += s->uvlinesize;
510  }
511  }
512  srcY += s->mspel * (1 + s->linesize);
513  }
514 
515  if (s->mspel) {
516  dxy = ((my & 3) << 2) | (mx & 3);
517  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
518  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
519  srcY += s->linesize * 8;
520  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
521  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
522  } else { // hpel mc - always used for luma
523  dxy = (my & 2) | ((mx & 2) >> 1);
524  if (!v->rnd)
525  s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
526  else
527  s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
528  }
529 
530  if (s->flags & CODEC_FLAG_GRAY) return;
531  /* Chroma MC always uses qpel bilinear */
532  uvmx = (uvmx & 3) << 1;
533  uvmy = (uvmy & 3) << 1;
534  if (!v->rnd) {
535  h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
536  h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
537  } else {
538  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
539  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
540  }
541 }
542 
543 static inline int median4(int a, int b, int c, int d)
544 {
545  if (a < b) {
546  if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
547  else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
548  } else {
549  if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
550  else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
551  }
552 }
553 
554 /** Do motion compensation for 4-MV macroblock - luminance block
555  */
556 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
557 {
558  MpegEncContext *s = &v->s;
559  uint8_t *srcY;
560  int dxy, mx, my, src_x, src_y;
561  int off;
562  int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
563  int v_edge_pos = s->v_edge_pos >> v->field_mode;
564  uint8_t (*luty)[256];
565  int use_ic;
566 
567  if ((!v->field_mode ||
568  (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
569  !v->s.last_picture.f.data[0])
570  return;
571 
572  mx = s->mv[dir][n][0];
573  my = s->mv[dir][n][1];
574 
575  if (!dir) {
576  if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
577  srcY = s->current_picture.f.data[0];
578  luty = v->curr_luty;
579  use_ic = v->curr_use_ic;
580  } else {
581  srcY = s->last_picture.f.data[0];
582  luty = v->last_luty;
583  use_ic = v->last_use_ic;
584  }
585  } else {
586  srcY = s->next_picture.f.data[0];
587  luty = v->next_luty;
588  use_ic = v->next_use_ic;
589  }
590 
591  if(!srcY)
592  return;
593 
594  if (v->field_mode) {
595  if (v->cur_field_type != v->ref_field_type[dir])
596  my = my - 2 + 4 * v->cur_field_type;
597  }
598 
599  if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
600  int same_count = 0, opp_count = 0, k;
601  int chosen_mv[2][4][2], f;
602  int tx, ty;
603  for (k = 0; k < 4; k++) {
604  f = v->mv_f[0][s->block_index[k] + v->blocks_off];
605  chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
606  chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
607  opp_count += f;
608  same_count += 1 - f;
609  }
610  f = opp_count > same_count;
611  switch (f ? opp_count : same_count) {
612  case 4:
613  tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
614  chosen_mv[f][2][0], chosen_mv[f][3][0]);
615  ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
616  chosen_mv[f][2][1], chosen_mv[f][3][1]);
617  break;
618  case 3:
619  tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
620  ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
621  break;
622  case 2:
623  tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
624  ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
625  break;
626  default:
627  av_assert0(0);
628  }
629  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
630  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
631  for (k = 0; k < 4; k++)
632  v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
633  }
634 
635  if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
636  int qx, qy;
637  int width = s->avctx->coded_width;
638  int height = s->avctx->coded_height >> 1;
639  if (s->pict_type == AV_PICTURE_TYPE_P) {
640  s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
641  s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
642  }
643  qx = (s->mb_x * 16) + (mx >> 2);
644  qy = (s->mb_y * 8) + (my >> 3);
645 
646  if (qx < -17)
647  mx -= 4 * (qx + 17);
648  else if (qx > width)
649  mx -= 4 * (qx - width);
650  if (qy < -18)
651  my -= 8 * (qy + 18);
652  else if (qy > height + 1)
653  my -= 8 * (qy - height - 1);
654  }
655 
656  if ((v->fcm == ILACE_FRAME) && fieldmv)
657  off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
658  else
659  off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
660 
661  src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
662  if (!fieldmv)
663  src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
664  else
665  src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
666 
667  if (v->profile != PROFILE_ADVANCED) {
668  src_x = av_clip(src_x, -16, s->mb_width * 16);
669  src_y = av_clip(src_y, -16, s->mb_height * 16);
670  } else {
671  src_x = av_clip(src_x, -17, s->avctx->coded_width);
672  if (v->fcm == ILACE_FRAME) {
673  if (src_y & 1)
674  src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
675  else
676  src_y = av_clip(src_y, -18, s->avctx->coded_height);
677  } else {
678  src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
679  }
680  }
681 
682  srcY += src_y * s->linesize + src_x;
683  if (v->field_mode && v->ref_field_type[dir])
684  srcY += s->current_picture_ptr->f.linesize[0];
685 
686  if (fieldmv && !(src_y & 1))
687  v_edge_pos--;
688  if (fieldmv && (src_y & 1) && src_y < 4)
689  src_y--;
690  if (v->rangeredfrm || use_ic
691  || s->h_edge_pos < 13 || v_edge_pos < 23
692  || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
693  || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
694  srcY -= s->mspel * (1 + (s->linesize << fieldmv));
695  /* check emulate edge stride and offset */
697  9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
698  src_x - s->mspel, src_y - (s->mspel << fieldmv),
699  s->h_edge_pos, v_edge_pos);
700  srcY = s->edge_emu_buffer;
701  /* if we deal with range reduction we need to scale source blocks */
702  if (v->rangeredfrm) {
703  int i, j;
704  uint8_t *src;
705 
706  src = srcY;
707  for (j = 0; j < 9 + s->mspel * 2; j++) {
708  for (i = 0; i < 9 + s->mspel * 2; i++)
709  src[i] = ((src[i] - 128) >> 1) + 128;
710  src += s->linesize << fieldmv;
711  }
712  }
713  /* if we deal with intensity compensation we need to scale source blocks */
714  if (use_ic) {
715  int i, j;
716  uint8_t *src;
717 
718  src = srcY;
719  for (j = 0; j < 9 + s->mspel * 2; j++) {
720  int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
721  for (i = 0; i < 9 + s->mspel * 2; i++)
722  src[i] = luty[f][src[i]];
723  src += s->linesize << fieldmv;
724  }
725  }
726  srcY += s->mspel * (1 + (s->linesize << fieldmv));
727  }
728 
729  if (s->mspel) {
730  dxy = ((my & 3) << 2) | (mx & 3);
731  if (avg)
732  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
733  else
734  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
735  } else { // hpel mc - always used for luma
736  dxy = (my & 2) | ((mx & 2) >> 1);
737  if (!v->rnd)
738  s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
739  else
740  s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
741  }
742 }
743 
744 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
745 {
746  int idx, i;
747  static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
748 
749  idx = ((a[3] != flag) << 3)
750  | ((a[2] != flag) << 2)
751  | ((a[1] != flag) << 1)
752  | (a[0] != flag);
753  if (!idx) {
754  *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
755  *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
756  return 4;
757  } else if (count[idx] == 1) {
758  switch (idx) {
759  case 0x1:
760  *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
761  *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
762  return 3;
763  case 0x2:
764  *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
765  *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
766  return 3;
767  case 0x4:
768  *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
769  *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
770  return 3;
771  case 0x8:
772  *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
773  *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
774  return 3;
775  }
776  } else if (count[idx] == 2) {
777  int t1 = 0, t2 = 0;
778  for (i = 0; i < 3; i++)
779  if (!a[i]) {
780  t1 = i;
781  break;
782  }
783  for (i = t1 + 1; i < 4; i++)
784  if (!a[i]) {
785  t2 = i;
786  break;
787  }
788  *tx = (mvx[t1] + mvx[t2]) / 2;
789  *ty = (mvy[t1] + mvy[t2]) / 2;
790  return 2;
791  } else {
792  return 0;
793  }
794  return -1;
795 }
796 
797 /** Do motion compensation for 4-MV macroblock - both chroma blocks
798  */
799 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
800 {
801  MpegEncContext *s = &v->s;
802  H264ChromaContext *h264chroma = &v->h264chroma;
803  uint8_t *srcU, *srcV;
804  int uvmx, uvmy, uvsrc_x, uvsrc_y;
805  int k, tx = 0, ty = 0;
806  int mvx[4], mvy[4], intra[4], mv_f[4];
807  int valid_count;
808  int chroma_ref_type = v->cur_field_type;
809  int v_edge_pos = s->v_edge_pos >> v->field_mode;
810  uint8_t (*lutuv)[256];
811  int use_ic;
812 
813  if (!v->field_mode && !v->s.last_picture.f.data[0])
814  return;
815  if (s->flags & CODEC_FLAG_GRAY)
816  return;
817 
818  for (k = 0; k < 4; k++) {
819  mvx[k] = s->mv[dir][k][0];
820  mvy[k] = s->mv[dir][k][1];
821  intra[k] = v->mb_type[0][s->block_index[k]];
822  if (v->field_mode)
823  mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
824  }
825 
826  /* calculate chroma MV vector from four luma MVs */
827  if (!v->field_mode || (v->field_mode && !v->numref)) {
828  valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
829  chroma_ref_type = v->reffield;
830  if (!valid_count) {
831  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
832  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
833  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
834  return; //no need to do MC for intra blocks
835  }
836  } else {
837  int dominant = 0;
838  if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
839  dominant = 1;
840  valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
841  if (dominant)
842  chroma_ref_type = !v->cur_field_type;
843  }
844  if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
845  return;
846  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
847  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
848  uvmx = (tx + ((tx & 3) == 3)) >> 1;
849  uvmy = (ty + ((ty & 3) == 3)) >> 1;
850 
851  v->luma_mv[s->mb_x][0] = uvmx;
852  v->luma_mv[s->mb_x][1] = uvmy;
853 
854  if (v->fastuvmc) {
855  uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
856  uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
857  }
858  // Field conversion bias
859  if (v->cur_field_type != chroma_ref_type)
860  uvmy += 2 - 4 * chroma_ref_type;
861 
862  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
863  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
864 
865  if (v->profile != PROFILE_ADVANCED) {
866  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
867  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
868  } else {
869  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
870  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
871  }
872 
873  if (!dir) {
874  if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
875  srcU = s->current_picture.f.data[1];
876  srcV = s->current_picture.f.data[2];
877  lutuv = v->curr_lutuv;
878  use_ic = v->curr_use_ic;
879  } else {
880  srcU = s->last_picture.f.data[1];
881  srcV = s->last_picture.f.data[2];
882  lutuv = v->last_lutuv;
883  use_ic = v->last_use_ic;
884  }
885  } else {
886  srcU = s->next_picture.f.data[1];
887  srcV = s->next_picture.f.data[2];
888  lutuv = v->next_lutuv;
889  use_ic = v->next_use_ic;
890  }
891 
892  if(!srcU)
893  return;
894 
895  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
896  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
897 
898  if (v->field_mode) {
899  if (chroma_ref_type) {
900  srcU += s->current_picture_ptr->f.linesize[1];
901  srcV += s->current_picture_ptr->f.linesize[2];
902  }
903  }
904 
905  if (v->rangeredfrm || use_ic
906  || s->h_edge_pos < 18 || v_edge_pos < 18
907  || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
908  || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
910  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
911  s->h_edge_pos >> 1, v_edge_pos >> 1);
912  s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
913  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
914  s->h_edge_pos >> 1, v_edge_pos >> 1);
915  srcU = s->edge_emu_buffer;
916  srcV = s->edge_emu_buffer + 16;
917 
918  /* if we deal with range reduction we need to scale source blocks */
919  if (v->rangeredfrm) {
920  int i, j;
921  uint8_t *src, *src2;
922 
923  src = srcU;
924  src2 = srcV;
925  for (j = 0; j < 9; j++) {
926  for (i = 0; i < 9; i++) {
927  src[i] = ((src[i] - 128) >> 1) + 128;
928  src2[i] = ((src2[i] - 128) >> 1) + 128;
929  }
930  src += s->uvlinesize;
931  src2 += s->uvlinesize;
932  }
933  }
934  /* if we deal with intensity compensation we need to scale source blocks */
935  if (use_ic) {
936  int i, j;
937  uint8_t *src, *src2;
938 
939  src = srcU;
940  src2 = srcV;
941  for (j = 0; j < 9; j++) {
942  int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
943  for (i = 0; i < 9; i++) {
944  src[i] = lutuv[f][src[i]];
945  src2[i] = lutuv[f][src2[i]];
946  }
947  src += s->uvlinesize;
948  src2 += s->uvlinesize;
949  }
950  }
951  }
952 
953  /* Chroma MC always uses qpel bilinear */
954  uvmx = (uvmx & 3) << 1;
955  uvmy = (uvmy & 3) << 1;
956  if (!v->rnd) {
957  h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
958  h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
959  } else {
960  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
961  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
962  }
963 }
964 
965 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
966  */
967 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
968 {
969  MpegEncContext *s = &v->s;
970  H264ChromaContext *h264chroma = &v->h264chroma;
971  uint8_t *srcU, *srcV;
972  int uvsrc_x, uvsrc_y;
973  int uvmx_field[4], uvmy_field[4];
974  int i, off, tx, ty;
975  int fieldmv = v->blk_mv_type[s->block_index[0]];
976  static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
977  int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
978  int v_edge_pos = s->v_edge_pos >> 1;
979  int use_ic;
980  uint8_t (*lutuv)[256];
981 
982  if (s->flags & CODEC_FLAG_GRAY)
983  return;
984 
985  for (i = 0; i < 4; i++) {
986  int d = i < 2 ? dir: dir2;
987  tx = s->mv[d][i][0];
988  uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
989  ty = s->mv[d][i][1];
990  if (fieldmv)
991  uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
992  else
993  uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
994  }
995 
996  for (i = 0; i < 4; i++) {
997  off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
998  uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
999  uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1000  // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1001  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1002  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1003  if (i < 2 ? dir : dir2) {
1004  srcU = s->next_picture.f.data[1];
1005  srcV = s->next_picture.f.data[2];
1006  lutuv = v->next_lutuv;
1007  use_ic = v->next_use_ic;
1008  } else {
1009  srcU = s->last_picture.f.data[1];
1010  srcV = s->last_picture.f.data[2];
1011  lutuv = v->last_lutuv;
1012  use_ic = v->last_use_ic;
1013  }
1014  if (!srcU)
1015  return;
1016  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1017  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1018  uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1019  uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1020 
1021  if (fieldmv && !(uvsrc_y & 1))
1022  v_edge_pos = (s->v_edge_pos >> 1) - 1;
1023 
1024  if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1025  uvsrc_y--;
1026  if (use_ic
1027  || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1028  || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1029  || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1031  5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1032  s->h_edge_pos >> 1, v_edge_pos);
1033  s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1034  5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1035  s->h_edge_pos >> 1, v_edge_pos);
1036  srcU = s->edge_emu_buffer;
1037  srcV = s->edge_emu_buffer + 16;
1038 
1039  /* if we deal with intensity compensation we need to scale source blocks */
1040  if (use_ic) {
1041  int i, j;
1042  uint8_t *src, *src2;
1043 
1044  src = srcU;
1045  src2 = srcV;
1046  for (j = 0; j < 5; j++) {
1047  int f = (uvsrc_y + (j << fieldmv)) & 1;
1048  for (i = 0; i < 5; i++) {
1049  src[i] = lutuv[f][src[i]];
1050  src2[i] = lutuv[f][src2[i]];
1051  }
1052  src += s->uvlinesize << fieldmv;
1053  src2 += s->uvlinesize << fieldmv;
1054  }
1055  }
1056  }
1057  if (avg) {
1058  if (!v->rnd) {
1059  h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1060  h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1061  } else {
1062  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1063  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1064  }
1065  } else {
1066  if (!v->rnd) {
1067  h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1068  h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1069  } else {
1070  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]);
1071  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]);
1072  }
1073  }
1074  }
1075 }
1076 
1077 /***********************************************************************/
1078 /**
1079  * @name VC-1 Block-level functions
1080  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1081  * @{
1082  */
1083 
1084 /**
1085  * @def GET_MQUANT
1086  * @brief Get macroblock-level quantizer scale
1087  */
1088 #define GET_MQUANT() \
1089  if (v->dquantfrm) { \
1090  int edges = 0; \
1091  if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1092  if (v->dqbilevel) { \
1093  mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1094  } else { \
1095  mqdiff = get_bits(gb, 3); \
1096  if (mqdiff != 7) \
1097  mquant = v->pq + mqdiff; \
1098  else \
1099  mquant = get_bits(gb, 5); \
1100  } \
1101  } \
1102  if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1103  edges = 1 << v->dqsbedge; \
1104  else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1105  edges = (3 << v->dqsbedge) % 15; \
1106  else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1107  edges = 15; \
1108  if ((edges&1) && !s->mb_x) \
1109  mquant = v->altpq; \
1110  if ((edges&2) && s->first_slice_line) \
1111  mquant = v->altpq; \
1112  if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1113  mquant = v->altpq; \
1114  if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1115  mquant = v->altpq; \
1116  if (!mquant || mquant > 31) { \
1117  av_log(v->s.avctx, AV_LOG_ERROR, \
1118  "Overriding invalid mquant %d\n", mquant); \
1119  mquant = 1; \
1120  } \
1121  }
1122 
1123 /**
1124  * @def GET_MVDATA(_dmv_x, _dmv_y)
1125  * @brief Get MV differentials
1126  * @see MVDATA decoding from 8.3.5.2, p(1)20
1127  * @param _dmv_x Horizontal differential for decoded MV
1128  * @param _dmv_y Vertical differential for decoded MV
1129  */
1130 #define GET_MVDATA(_dmv_x, _dmv_y) \
1131  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1132  VC1_MV_DIFF_VLC_BITS, 2); \
1133  if (index > 36) { \
1134  mb_has_coeffs = 1; \
1135  index -= 37; \
1136  } else \
1137  mb_has_coeffs = 0; \
1138  s->mb_intra = 0; \
1139  if (!index) { \
1140  _dmv_x = _dmv_y = 0; \
1141  } else if (index == 35) { \
1142  _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1143  _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1144  } else if (index == 36) { \
1145  _dmv_x = 0; \
1146  _dmv_y = 0; \
1147  s->mb_intra = 1; \
1148  } else { \
1149  index1 = index % 6; \
1150  if (!s->quarter_sample && index1 == 5) val = 1; \
1151  else val = 0; \
1152  if (size_table[index1] - val > 0) \
1153  val = get_bits(gb, size_table[index1] - val); \
1154  else val = 0; \
1155  sign = 0 - (val&1); \
1156  _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1157  \
1158  index1 = index / 6; \
1159  if (!s->quarter_sample && index1 == 5) val = 1; \
1160  else val = 0; \
1161  if (size_table[index1] - val > 0) \
1162  val = get_bits(gb, size_table[index1] - val); \
1163  else val = 0; \
1164  sign = 0 - (val & 1); \
1165  _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1166  }
1167 
1169  int *dmv_y, int *pred_flag)
1170 {
1171  int index, index1;
1172  int extend_x = 0, extend_y = 0;
1173  GetBitContext *gb = &v->s.gb;
1174  int bits, esc;
1175  int val, sign;
1176  const int* offs_tab;
1177 
1178  if (v->numref) {
1179  bits = VC1_2REF_MVDATA_VLC_BITS;
1180  esc = 125;
1181  } else {
1182  bits = VC1_1REF_MVDATA_VLC_BITS;
1183  esc = 71;
1184  }
1185  switch (v->dmvrange) {
1186  case 1:
1187  extend_x = 1;
1188  break;
1189  case 2:
1190  extend_y = 1;
1191  break;
1192  case 3:
1193  extend_x = extend_y = 1;
1194  break;
1195  }
1196  index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1197  if (index == esc) {
1198  *dmv_x = get_bits(gb, v->k_x);
1199  *dmv_y = get_bits(gb, v->k_y);
1200  if (v->numref) {
1201  if (pred_flag) {
1202  *pred_flag = *dmv_y & 1;
1203  *dmv_y = (*dmv_y + *pred_flag) >> 1;
1204  } else {
1205  *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1206  }
1207  }
1208  }
1209  else {
1210  av_assert0(index < esc);
1211  if (extend_x)
1212  offs_tab = offset_table2;
1213  else
1214  offs_tab = offset_table1;
1215  index1 = (index + 1) % 9;
1216  if (index1 != 0) {
1217  val = get_bits(gb, index1 + extend_x);
1218  sign = 0 -(val & 1);
1219  *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1220  } else
1221  *dmv_x = 0;
1222  if (extend_y)
1223  offs_tab = offset_table2;
1224  else
1225  offs_tab = offset_table1;
1226  index1 = (index + 1) / 9;
1227  if (index1 > v->numref) {
1228  val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1229  sign = 0 - (val & 1);
1230  *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1231  } else
1232  *dmv_y = 0;
1233  if (v->numref && pred_flag)
1234  *pred_flag = index1 & 1;
1235  }
1236 }
1237 
1238 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1239 {
1240  int scaledvalue, refdist;
1241  int scalesame1, scalesame2;
1242  int scalezone1_x, zone1offset_x;
1243  int table_index = dir ^ v->second_field;
1244 
1245  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1246  refdist = v->refdist;
1247  else
1248  refdist = dir ? v->brfd : v->frfd;
1249  if (refdist > 3)
1250  refdist = 3;
1251  scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1252  scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1253  scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1254  zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1255 
1256  if (FFABS(n) > 255)
1257  scaledvalue = n;
1258  else {
1259  if (FFABS(n) < scalezone1_x)
1260  scaledvalue = (n * scalesame1) >> 8;
1261  else {
1262  if (n < 0)
1263  scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1264  else
1265  scaledvalue = ((n * scalesame2) >> 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 scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1272 {
1273  int scaledvalue, refdist;
1274  int scalesame1, scalesame2;
1275  int scalezone1_y, zone1offset_y;
1276  int table_index = dir ^ v->second_field;
1277 
1278  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1279  refdist = v->refdist;
1280  else
1281  refdist = dir ? v->brfd : v->frfd;
1282  if (refdist > 3)
1283  refdist = 3;
1284  scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1285  scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1286  scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1287  zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1288 
1289  if (FFABS(n) > 63)
1290  scaledvalue = n;
1291  else {
1292  if (FFABS(n) < scalezone1_y)
1293  scaledvalue = (n * scalesame1) >> 8;
1294  else {
1295  if (n < 0)
1296  scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1297  else
1298  scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1299  }
1300  }
1301 
1302  if (v->cur_field_type && !v->ref_field_type[dir])
1303  return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1304  else
1305  return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1306 }
1307 
1308 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1309 {
1310  int scalezone1_x, zone1offset_x;
1311  int scaleopp1, scaleopp2, brfd;
1312  int scaledvalue;
1313 
1314  brfd = FFMIN(v->brfd, 3);
1315  scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1316  zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1317  scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1318  scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1319 
1320  if (FFABS(n) > 255)
1321  scaledvalue = n;
1322  else {
1323  if (FFABS(n) < scalezone1_x)
1324  scaledvalue = (n * scaleopp1) >> 8;
1325  else {
1326  if (n < 0)
1327  scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1328  else
1329  scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1330  }
1331  }
1332  return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1333 }
1334 
1335 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1336 {
1337  int scalezone1_y, zone1offset_y;
1338  int scaleopp1, scaleopp2, brfd;
1339  int scaledvalue;
1340 
1341  brfd = FFMIN(v->brfd, 3);
1342  scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1343  zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1344  scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1345  scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1346 
1347  if (FFABS(n) > 63)
1348  scaledvalue = n;
1349  else {
1350  if (FFABS(n) < scalezone1_y)
1351  scaledvalue = (n * scaleopp1) >> 8;
1352  else {
1353  if (n < 0)
1354  scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1355  else
1356  scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1357  }
1358  }
1359  if (v->cur_field_type && !v->ref_field_type[dir]) {
1360  return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1361  } else {
1362  return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1363  }
1364 }
1365 
1366 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1367  int dim, int dir)
1368 {
1369  int brfd, scalesame;
1370  int hpel = 1 - v->s.quarter_sample;
1371 
1372  n >>= hpel;
1373  if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1374  if (dim)
1375  n = scaleforsame_y(v, i, n, dir) << hpel;
1376  else
1377  n = scaleforsame_x(v, n, dir) << hpel;
1378  return n;
1379  }
1380  brfd = FFMIN(v->brfd, 3);
1381  scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1382 
1383  n = (n * scalesame >> 8) << hpel;
1384  return n;
1385 }
1386 
1387 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1388  int dim, int dir)
1389 {
1390  int refdist, scaleopp;
1391  int hpel = 1 - v->s.quarter_sample;
1392 
1393  n >>= hpel;
1394  if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1395  if (dim)
1396  n = scaleforopp_y(v, n, dir) << hpel;
1397  else
1398  n = scaleforopp_x(v, n) << hpel;
1399  return n;
1400  }
1401  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1402  refdist = FFMIN(v->refdist, 3);
1403  else
1404  refdist = dir ? v->brfd : v->frfd;
1405  scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1406 
1407  n = (n * scaleopp >> 8) << hpel;
1408  return n;
1409 }
1410 
1411 /** Predict and set motion vector
1412  */
1413 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1414  int mv1, int r_x, int r_y, uint8_t* is_intra,
1415  int pred_flag, int dir)
1416 {
1417  MpegEncContext *s = &v->s;
1418  int xy, wrap, off = 0;
1419  int16_t *A, *B, *C;
1420  int px, py;
1421  int sum;
1422  int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1423  int opposite, a_f, b_f, c_f;
1424  int16_t field_predA[2];
1425  int16_t field_predB[2];
1426  int16_t field_predC[2];
1427  int a_valid, b_valid, c_valid;
1428  int hybridmv_thresh, y_bias = 0;
1429 
1430  if (v->mv_mode == MV_PMODE_MIXED_MV ||
1432  mixedmv_pic = 1;
1433  else
1434  mixedmv_pic = 0;
1435  /* scale MV difference to be quad-pel */
1436  dmv_x <<= 1 - s->quarter_sample;
1437  dmv_y <<= 1 - s->quarter_sample;
1438 
1439  wrap = s->b8_stride;
1440  xy = s->block_index[n];
1441 
1442  if (s->mb_intra) {
1443  s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1444  s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1445  s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1446  s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1447  if (mv1) { /* duplicate motion data for 1-MV block */
1448  s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1449  s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1450  s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1451  s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1452  s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1453  s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1454  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1455  s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1456  s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1457  s->current_picture.motion_val[1][xy + wrap][0] = 0;
1458  s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1459  s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1460  s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1461  }
1462  return;
1463  }
1464 
1465  C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1466  A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1467  if (mv1) {
1468  if (v->field_mode && mixedmv_pic)
1469  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1470  else
1471  off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1472  } else {
1473  //in 4-MV mode different blocks have different B predictor position
1474  switch (n) {
1475  case 0:
1476  off = (s->mb_x > 0) ? -1 : 1;
1477  break;
1478  case 1:
1479  off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1480  break;
1481  case 2:
1482  off = 1;
1483  break;
1484  case 3:
1485  off = -1;
1486  }
1487  }
1488  B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1489 
1490  a_valid = !s->first_slice_line || (n == 2 || n == 3);
1491  b_valid = a_valid && (s->mb_width > 1);
1492  c_valid = s->mb_x || (n == 1 || n == 3);
1493  if (v->field_mode) {
1494  a_valid = a_valid && !is_intra[xy - wrap];
1495  b_valid = b_valid && !is_intra[xy - wrap + off];
1496  c_valid = c_valid && !is_intra[xy - 1];
1497  }
1498 
1499  if (a_valid) {
1500  a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1501  num_oppfield += a_f;
1502  num_samefield += 1 - a_f;
1503  field_predA[0] = A[0];
1504  field_predA[1] = A[1];
1505  } else {
1506  field_predA[0] = field_predA[1] = 0;
1507  a_f = 0;
1508  }
1509  if (b_valid) {
1510  b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1511  num_oppfield += b_f;
1512  num_samefield += 1 - b_f;
1513  field_predB[0] = B[0];
1514  field_predB[1] = B[1];
1515  } else {
1516  field_predB[0] = field_predB[1] = 0;
1517  b_f = 0;
1518  }
1519  if (c_valid) {
1520  c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1521  num_oppfield += c_f;
1522  num_samefield += 1 - c_f;
1523  field_predC[0] = C[0];
1524  field_predC[1] = C[1];
1525  } else {
1526  field_predC[0] = field_predC[1] = 0;
1527  c_f = 0;
1528  }
1529 
1530  if (v->field_mode) {
1531  if (!v->numref)
1532  // REFFIELD determines if the last field or the second-last field is
1533  // to be used as reference
1534  opposite = 1 - v->reffield;
1535  else {
1536  if (num_samefield <= num_oppfield)
1537  opposite = 1 - pred_flag;
1538  else
1539  opposite = pred_flag;
1540  }
1541  } else
1542  opposite = 0;
1543  if (opposite) {
1544  if (a_valid && !a_f) {
1545  field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1546  field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1547  }
1548  if (b_valid && !b_f) {
1549  field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1550  field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1551  }
1552  if (c_valid && !c_f) {
1553  field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1554  field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1555  }
1556  v->mv_f[dir][xy + v->blocks_off] = 1;
1557  v->ref_field_type[dir] = !v->cur_field_type;
1558  } else {
1559  if (a_valid && a_f) {
1560  field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1561  field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1562  }
1563  if (b_valid && b_f) {
1564  field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1565  field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1566  }
1567  if (c_valid && c_f) {
1568  field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1569  field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1570  }
1571  v->mv_f[dir][xy + v->blocks_off] = 0;
1572  v->ref_field_type[dir] = v->cur_field_type;
1573  }
1574 
1575  if (a_valid) {
1576  px = field_predA[0];
1577  py = field_predA[1];
1578  } else if (c_valid) {
1579  px = field_predC[0];
1580  py = field_predC[1];
1581  } else if (b_valid) {
1582  px = field_predB[0];
1583  py = field_predB[1];
1584  } else {
1585  px = 0;
1586  py = 0;
1587  }
1588 
1589  if (num_samefield + num_oppfield > 1) {
1590  px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1591  py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1592  }
1593 
1594  /* Pullback MV as specified in 8.3.5.3.4 */
1595  if (!v->field_mode) {
1596  int qx, qy, X, Y;
1597  qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1598  qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1599  X = (s->mb_width << 6) - 4;
1600  Y = (s->mb_height << 6) - 4;
1601  if (mv1) {
1602  if (qx + px < -60) px = -60 - qx;
1603  if (qy + py < -60) py = -60 - qy;
1604  } else {
1605  if (qx + px < -28) px = -28 - qx;
1606  if (qy + py < -28) py = -28 - qy;
1607  }
1608  if (qx + px > X) px = X - qx;
1609  if (qy + py > Y) py = Y - qy;
1610  }
1611 
1612  if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1613  /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1614  hybridmv_thresh = 32;
1615  if (a_valid && c_valid) {
1616  if (is_intra[xy - wrap])
1617  sum = FFABS(px) + FFABS(py);
1618  else
1619  sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1620  if (sum > hybridmv_thresh) {
1621  if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1622  px = field_predA[0];
1623  py = field_predA[1];
1624  } else {
1625  px = field_predC[0];
1626  py = field_predC[1];
1627  }
1628  } else {
1629  if (is_intra[xy - 1])
1630  sum = FFABS(px) + FFABS(py);
1631  else
1632  sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1633  if (sum > hybridmv_thresh) {
1634  if (get_bits1(&s->gb)) {
1635  px = field_predA[0];
1636  py = field_predA[1];
1637  } else {
1638  px = field_predC[0];
1639  py = field_predC[1];
1640  }
1641  }
1642  }
1643  }
1644  }
1645 
1646  if (v->field_mode && v->numref)
1647  r_y >>= 1;
1648  if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1649  y_bias = 1;
1650  /* store MV using signed modulus of MV range defined in 4.11 */
1651  s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1652  s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1653  if (mv1) { /* duplicate motion data for 1-MV block */
1654  s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1655  s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1656  s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1657  s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1658  s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1659  s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1660  v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1661  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];
1662  }
1663 }
1664 
1665 /** Predict and set motion vector for interlaced frame picture MBs
1666  */
1667 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1668  int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1669 {
1670  MpegEncContext *s = &v->s;
1671  int xy, wrap, off = 0;
1672  int A[2], B[2], C[2];
1673  int px = 0, py = 0;
1674  int a_valid = 0, b_valid = 0, c_valid = 0;
1675  int field_a, field_b, field_c; // 0: same, 1: opposit
1676  int total_valid, num_samefield, num_oppfield;
1677  int pos_c, pos_b, n_adj;
1678 
1679  wrap = s->b8_stride;
1680  xy = s->block_index[n];
1681 
1682  if (s->mb_intra) {
1683  s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1684  s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1685  s->current_picture.motion_val[1][xy][0] = 0;
1686  s->current_picture.motion_val[1][xy][1] = 0;
1687  if (mvn == 1) { /* duplicate motion data for 1-MV block */
1688  s->current_picture.motion_val[0][xy + 1][0] = 0;
1689  s->current_picture.motion_val[0][xy + 1][1] = 0;
1690  s->current_picture.motion_val[0][xy + wrap][0] = 0;
1691  s->current_picture.motion_val[0][xy + wrap][1] = 0;
1692  s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1693  s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1694  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1695  s->current_picture.motion_val[1][xy + 1][0] = 0;
1696  s->current_picture.motion_val[1][xy + 1][1] = 0;
1697  s->current_picture.motion_val[1][xy + wrap][0] = 0;
1698  s->current_picture.motion_val[1][xy + wrap][1] = 0;
1699  s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1700  s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1701  }
1702  return;
1703  }
1704 
1705  off = ((n == 0) || (n == 1)) ? 1 : -1;
1706  /* predict A */
1707  if (s->mb_x || (n == 1) || (n == 3)) {
1708  if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1709  || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1710  A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1711  A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1712  a_valid = 1;
1713  } else { // current block has frame mv and cand. has field MV (so average)
1714  A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1715  + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1716  A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1717  + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1718  a_valid = 1;
1719  }
1720  if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1721  a_valid = 0;
1722  A[0] = A[1] = 0;
1723  }
1724  } else
1725  A[0] = A[1] = 0;
1726  /* Predict B and C */
1727  B[0] = B[1] = C[0] = C[1] = 0;
1728  if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1729  if (!s->first_slice_line) {
1730  if (!v->is_intra[s->mb_x - s->mb_stride]) {
1731  b_valid = 1;
1732  n_adj = n | 2;
1733  pos_b = s->block_index[n_adj] - 2 * wrap;
1734  if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1735  n_adj = (n & 2) | (n & 1);
1736  }
1737  B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1738  B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1739  if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1740  B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1741  B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1742  }
1743  }
1744  if (s->mb_width > 1) {
1745  if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1746  c_valid = 1;
1747  n_adj = 2;
1748  pos_c = s->block_index[2] - 2 * wrap + 2;
1749  if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1750  n_adj = n & 2;
1751  }
1752  C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1753  C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1754  if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1755  C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1756  C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1757  }
1758  if (s->mb_x == s->mb_width - 1) {
1759  if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1760  c_valid = 1;
1761  n_adj = 3;
1762  pos_c = s->block_index[3] - 2 * wrap - 2;
1763  if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1764  n_adj = n | 1;
1765  }
1766  C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1767  C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1768  if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1769  C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1770  C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1771  }
1772  } else
1773  c_valid = 0;
1774  }
1775  }
1776  }
1777  }
1778  } else {
1779  pos_b = s->block_index[1];
1780  b_valid = 1;
1781  B[0] = s->current_picture.motion_val[dir][pos_b][0];
1782  B[1] = s->current_picture.motion_val[dir][pos_b][1];
1783  pos_c = s->block_index[0];
1784  c_valid = 1;
1785  C[0] = s->current_picture.motion_val[dir][pos_c][0];
1786  C[1] = s->current_picture.motion_val[dir][pos_c][1];
1787  }
1788 
1789  total_valid = a_valid + b_valid + c_valid;
1790  // check if predictor A is out of bounds
1791  if (!s->mb_x && !(n == 1 || n == 3)) {
1792  A[0] = A[1] = 0;
1793  }
1794  // check if predictor B is out of bounds
1795  if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1796  B[0] = B[1] = C[0] = C[1] = 0;
1797  }
1798  if (!v->blk_mv_type[xy]) {
1799  if (s->mb_width == 1) {
1800  px = B[0];
1801  py = B[1];
1802  } else {
1803  if (total_valid >= 2) {
1804  px = mid_pred(A[0], B[0], C[0]);
1805  py = mid_pred(A[1], B[1], C[1]);
1806  } else if (total_valid) {
1807  if (a_valid) { px = A[0]; py = A[1]; }
1808  else if (b_valid) { px = B[0]; py = B[1]; }
1809  else if (c_valid) { px = C[0]; py = C[1]; }
1810  else av_assert2(0);
1811  }
1812  }
1813  } else {
1814  if (a_valid)
1815  field_a = (A[1] & 4) ? 1 : 0;
1816  else
1817  field_a = 0;
1818  if (b_valid)
1819  field_b = (B[1] & 4) ? 1 : 0;
1820  else
1821  field_b = 0;
1822  if (c_valid)
1823  field_c = (C[1] & 4) ? 1 : 0;
1824  else
1825  field_c = 0;
1826 
1827  num_oppfield = field_a + field_b + field_c;
1828  num_samefield = total_valid - num_oppfield;
1829  if (total_valid == 3) {
1830  if ((num_samefield == 3) || (num_oppfield == 3)) {
1831  px = mid_pred(A[0], B[0], C[0]);
1832  py = mid_pred(A[1], B[1], C[1]);
1833  } else if (num_samefield >= num_oppfield) {
1834  /* take one MV from same field set depending on priority
1835  the check for B may not be necessary */
1836  px = !field_a ? A[0] : B[0];
1837  py = !field_a ? A[1] : B[1];
1838  } else {
1839  px = field_a ? A[0] : B[0];
1840  py = field_a ? A[1] : B[1];
1841  }
1842  } else if (total_valid == 2) {
1843  if (num_samefield >= num_oppfield) {
1844  if (!field_a && a_valid) {
1845  px = A[0];
1846  py = A[1];
1847  } else if (!field_b && b_valid) {
1848  px = B[0];
1849  py = B[1];
1850  } else /*if (c_valid)*/ {
1851  av_assert1(c_valid);
1852  px = C[0];
1853  py = C[1];
1854  } /*else px = py = 0;*/
1855  } else {
1856  if (field_a && a_valid) {
1857  px = A[0];
1858  py = A[1];
1859  } else /*if (field_b && b_valid)*/ {
1860  av_assert1(field_b && b_valid);
1861  px = B[0];
1862  py = B[1];
1863  } /*else if (c_valid) {
1864  px = C[0];
1865  py = C[1];
1866  }*/
1867  }
1868  } else if (total_valid == 1) {
1869  px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1870  py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1871  }
1872  }
1873 
1874  /* store MV using signed modulus of MV range defined in 4.11 */
1875  s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1876  s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1877  if (mvn == 1) { /* duplicate motion data for 1-MV block */
1878  s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1879  s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1880  s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1881  s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1882  s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1883  s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1884  } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1885  s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1886  s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1887  s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1888  s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1889  }
1890 }
1891 
1892 /** Motion compensation for direct or interpolated blocks in B-frames
1893  */
1895 {
1896  MpegEncContext *s = &v->s;
1897  H264ChromaContext *h264chroma = &v->h264chroma;
1898  uint8_t *srcY, *srcU, *srcV;
1899  int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1900  int off, off_uv;
1901  int v_edge_pos = s->v_edge_pos >> v->field_mode;
1902  int use_ic = v->next_use_ic;
1903 
1904  if (!v->field_mode && !v->s.next_picture.f.data[0])
1905  return;
1906 
1907  mx = s->mv[1][0][0];
1908  my = s->mv[1][0][1];
1909  uvmx = (mx + ((mx & 3) == 3)) >> 1;
1910  uvmy = (my + ((my & 3) == 3)) >> 1;
1911  if (v->field_mode) {
1912  if (v->cur_field_type != v->ref_field_type[1])
1913  my = my - 2 + 4 * v->cur_field_type;
1914  uvmy = uvmy - 2 + 4 * v->cur_field_type;
1915  }
1916  if (v->fastuvmc) {
1917  uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1918  uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1919  }
1920  srcY = s->next_picture.f.data[0];
1921  srcU = s->next_picture.f.data[1];
1922  srcV = s->next_picture.f.data[2];
1923 
1924  src_x = s->mb_x * 16 + (mx >> 2);
1925  src_y = s->mb_y * 16 + (my >> 2);
1926  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1927  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1928 
1929  if (v->profile != PROFILE_ADVANCED) {
1930  src_x = av_clip( src_x, -16, s->mb_width * 16);
1931  src_y = av_clip( src_y, -16, s->mb_height * 16);
1932  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1933  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1934  } else {
1935  src_x = av_clip( src_x, -17, s->avctx->coded_width);
1936  src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1937  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1938  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1939  }
1940 
1941  srcY += src_y * s->linesize + src_x;
1942  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1943  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1944 
1945  if (v->field_mode && v->ref_field_type[1]) {
1946  srcY += s->current_picture_ptr->f.linesize[0];
1947  srcU += s->current_picture_ptr->f.linesize[1];
1948  srcV += s->current_picture_ptr->f.linesize[2];
1949  }
1950 
1951  /* for grayscale we should not try to read from unknown area */
1952  if (s->flags & CODEC_FLAG_GRAY) {
1953  srcU = s->edge_emu_buffer + 18 * s->linesize;
1954  srcV = s->edge_emu_buffer + 18 * s->linesize;
1955  }
1956 
1957  if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1958  || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1959  || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1960  uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1961 
1962  srcY -= s->mspel * (1 + s->linesize);
1964  17 + s->mspel * 2, 17 + s->mspel * 2,
1965  src_x - s->mspel, src_y - s->mspel,
1966  s->h_edge_pos, v_edge_pos);
1967  srcY = s->edge_emu_buffer;
1968  s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1969  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1970  s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1971  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1972  srcU = uvbuf;
1973  srcV = uvbuf + 16;
1974  /* if we deal with range reduction we need to scale source blocks */
1975  if (v->rangeredfrm) {
1976  int i, j;
1977  uint8_t *src, *src2;
1978 
1979  src = srcY;
1980  for (j = 0; j < 17 + s->mspel * 2; j++) {
1981  for (i = 0; i < 17 + s->mspel * 2; i++)
1982  src[i] = ((src[i] - 128) >> 1) + 128;
1983  src += s->linesize;
1984  }
1985  src = srcU;
1986  src2 = srcV;
1987  for (j = 0; j < 9; j++) {
1988  for (i = 0; i < 9; i++) {
1989  src[i] = ((src[i] - 128) >> 1) + 128;
1990  src2[i] = ((src2[i] - 128) >> 1) + 128;
1991  }
1992  src += s->uvlinesize;
1993  src2 += s->uvlinesize;
1994  }
1995  }
1996 
1997  if (use_ic) {
1998  uint8_t (*luty )[256] = v->next_luty;
1999  uint8_t (*lutuv)[256] = v->next_lutuv;
2000  int i, j;
2001  uint8_t *src, *src2;
2002 
2003  src = srcY;
2004  for (j = 0; j < 17 + s->mspel * 2; j++) {
2005  int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2006  for (i = 0; i < 17 + s->mspel * 2; i++)
2007  src[i] = luty[f][src[i]];
2008  src += s->linesize;
2009  }
2010  src = srcU;
2011  src2 = srcV;
2012  for (j = 0; j < 9; j++) {
2013  int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2014  for (i = 0; i < 9; i++) {
2015  src[i] = lutuv[f][src[i]];
2016  src2[i] = lutuv[f][src2[i]];
2017  }
2018  src += s->uvlinesize;
2019  src2 += s->uvlinesize;
2020  }
2021  }
2022  srcY += s->mspel * (1 + s->linesize);
2023  }
2024 
2025  off = 0;
2026  off_uv = 0;
2027 
2028  if (s->mspel) {
2029  dxy = ((my & 3) << 2) | (mx & 3);
2030  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2031  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2032  srcY += s->linesize * 8;
2033  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2034  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2035  } else { // hpel mc
2036  dxy = (my & 2) | ((mx & 2) >> 1);
2037 
2038  if (!v->rnd)
2039  s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2040  else
2041  s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2042  }
2043 
2044  if (s->flags & CODEC_FLAG_GRAY) return;
2045  /* Chroma MC always uses qpel blilinear */
2046  uvmx = (uvmx & 3) << 1;
2047  uvmy = (uvmy & 3) << 1;
2048  if (!v->rnd) {
2049  h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2050  h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2051  } else {
2052  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2053  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2054  }
2055 }
2056 
2057 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2058 {
2059  int n = bfrac;
2060 
2061 #if B_FRACTION_DEN==256
2062  if (inv)
2063  n -= 256;
2064  if (!qs)
2065  return 2 * ((value * n + 255) >> 9);
2066  return (value * n + 128) >> 8;
2067 #else
2068  if (inv)
2069  n -= B_FRACTION_DEN;
2070  if (!qs)
2071  return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2072  return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2073 #endif
2074 }
2075 
2076 /** Reconstruct motion vector for B-frame and do motion compensation
2077  */
2078 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2079  int direct, int mode)
2080 {
2081  if (direct) {
2082  vc1_mc_1mv(v, 0);
2083  vc1_interp_mc(v);
2084  return;
2085  }
2086  if (mode == BMV_TYPE_INTERPOLATED) {
2087  vc1_mc_1mv(v, 0);
2088  vc1_interp_mc(v);
2089  return;
2090  }
2091 
2092  vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2093 }
2094 
2095 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2096  int direct, int mvtype)
2097 {
2098  MpegEncContext *s = &v->s;
2099  int xy, wrap, off = 0;
2100  int16_t *A, *B, *C;
2101  int px, py;
2102  int sum;
2103  int r_x, r_y;
2104  const uint8_t *is_intra = v->mb_type[0];
2105 
2106  r_x = v->range_x;
2107  r_y = v->range_y;
2108  /* scale MV difference to be quad-pel */
2109  dmv_x[0] <<= 1 - s->quarter_sample;
2110  dmv_y[0] <<= 1 - s->quarter_sample;
2111  dmv_x[1] <<= 1 - s->quarter_sample;
2112  dmv_y[1] <<= 1 - s->quarter_sample;
2113 
2114  wrap = s->b8_stride;
2115  xy = s->block_index[0];
2116 
2117  if (s->mb_intra) {
2118  s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2119  s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2120  s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2121  s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2122  return;
2123  }
2124  if (!v->field_mode) {
2125  s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2126  s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2127  s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2128  s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2129 
2130  /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2131  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));
2132  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));
2133  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));
2134  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));
2135  }
2136  if (direct) {
2137  s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2138  s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2139  s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2140  s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2141  return;
2142  }
2143 
2144  if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2145  C = s->current_picture.motion_val[0][xy - 2];
2146  A = s->current_picture.motion_val[0][xy - wrap * 2];
2147  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2148  B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2149 
2150  if (!s->mb_x) C[0] = C[1] = 0;
2151  if (!s->first_slice_line) { // predictor A is not out of bounds
2152  if (s->mb_width == 1) {
2153  px = A[0];
2154  py = A[1];
2155  } else {
2156  px = mid_pred(A[0], B[0], C[0]);
2157  py = mid_pred(A[1], B[1], C[1]);
2158  }
2159  } else if (s->mb_x) { // predictor C is not out of bounds
2160  px = C[0];
2161  py = C[1];
2162  } else {
2163  px = py = 0;
2164  }
2165  /* Pullback MV as specified in 8.3.5.3.4 */
2166  {
2167  int qx, qy, X, Y;
2168  if (v->profile < PROFILE_ADVANCED) {
2169  qx = (s->mb_x << 5);
2170  qy = (s->mb_y << 5);
2171  X = (s->mb_width << 5) - 4;
2172  Y = (s->mb_height << 5) - 4;
2173  if (qx + px < -28) px = -28 - qx;
2174  if (qy + py < -28) py = -28 - qy;
2175  if (qx + px > X) px = X - qx;
2176  if (qy + py > Y) py = Y - qy;
2177  } else {
2178  qx = (s->mb_x << 6);
2179  qy = (s->mb_y << 6);
2180  X = (s->mb_width << 6) - 4;
2181  Y = (s->mb_height << 6) - 4;
2182  if (qx + px < -60) px = -60 - qx;
2183  if (qy + py < -60) py = -60 - qy;
2184  if (qx + px > X) px = X - qx;
2185  if (qy + py > Y) py = Y - qy;
2186  }
2187  }
2188  /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2189  if (0 && !s->first_slice_line && s->mb_x) {
2190  if (is_intra[xy - wrap])
2191  sum = FFABS(px) + FFABS(py);
2192  else
2193  sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2194  if (sum > 32) {
2195  if (get_bits1(&s->gb)) {
2196  px = A[0];
2197  py = A[1];
2198  } else {
2199  px = C[0];
2200  py = C[1];
2201  }
2202  } else {
2203  if (is_intra[xy - 2])
2204  sum = FFABS(px) + FFABS(py);
2205  else
2206  sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2207  if (sum > 32) {
2208  if (get_bits1(&s->gb)) {
2209  px = A[0];
2210  py = A[1];
2211  } else {
2212  px = C[0];
2213  py = C[1];
2214  }
2215  }
2216  }
2217  }
2218  /* store MV using signed modulus of MV range defined in 4.11 */
2219  s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2220  s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2221  }
2222  if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2223  C = s->current_picture.motion_val[1][xy - 2];
2224  A = s->current_picture.motion_val[1][xy - wrap * 2];
2225  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2226  B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2227 
2228  if (!s->mb_x)
2229  C[0] = C[1] = 0;
2230  if (!s->first_slice_line) { // predictor A is not out of bounds
2231  if (s->mb_width == 1) {
2232  px = A[0];
2233  py = A[1];
2234  } else {
2235  px = mid_pred(A[0], B[0], C[0]);
2236  py = mid_pred(A[1], B[1], C[1]);
2237  }
2238  } else if (s->mb_x) { // predictor C is not out of bounds
2239  px = C[0];
2240  py = C[1];
2241  } else {
2242  px = py = 0;
2243  }
2244  /* Pullback MV as specified in 8.3.5.3.4 */
2245  {
2246  int qx, qy, X, Y;
2247  if (v->profile < PROFILE_ADVANCED) {
2248  qx = (s->mb_x << 5);
2249  qy = (s->mb_y << 5);
2250  X = (s->mb_width << 5) - 4;
2251  Y = (s->mb_height << 5) - 4;
2252  if (qx + px < -28) px = -28 - qx;
2253  if (qy + py < -28) py = -28 - qy;
2254  if (qx + px > X) px = X - qx;
2255  if (qy + py > Y) py = Y - qy;
2256  } else {
2257  qx = (s->mb_x << 6);
2258  qy = (s->mb_y << 6);
2259  X = (s->mb_width << 6) - 4;
2260  Y = (s->mb_height << 6) - 4;
2261  if (qx + px < -60) px = -60 - qx;
2262  if (qy + py < -60) py = -60 - qy;
2263  if (qx + px > X) px = X - qx;
2264  if (qy + py > Y) py = Y - qy;
2265  }
2266  }
2267  /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2268  if (0 && !s->first_slice_line && s->mb_x) {
2269  if (is_intra[xy - wrap])
2270  sum = FFABS(px) + FFABS(py);
2271  else
2272  sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2273  if (sum > 32) {
2274  if (get_bits1(&s->gb)) {
2275  px = A[0];
2276  py = A[1];
2277  } else {
2278  px = C[0];
2279  py = C[1];
2280  }
2281  } else {
2282  if (is_intra[xy - 2])
2283  sum = FFABS(px) + FFABS(py);
2284  else
2285  sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2286  if (sum > 32) {
2287  if (get_bits1(&s->gb)) {
2288  px = A[0];
2289  py = A[1];
2290  } else {
2291  px = C[0];
2292  py = C[1];
2293  }
2294  }
2295  }
2296  }
2297  /* store MV using signed modulus of MV range defined in 4.11 */
2298 
2299  s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2300  s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2301  }
2302  s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2303  s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2304  s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2305  s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2306 }
2307 
2308 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2309 {
2310  int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2311  MpegEncContext *s = &v->s;
2312  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2313 
2314  if (v->bmvtype == BMV_TYPE_DIRECT) {
2315  int total_opp, k, f;
2316  if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2317  s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2318  v->bfraction, 0, s->quarter_sample);
2319  s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2320  v->bfraction, 0, s->quarter_sample);
2321  s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2322  v->bfraction, 1, s->quarter_sample);
2323  s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2324  v->bfraction, 1, s->quarter_sample);
2325 
2326  total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2327  + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2328  + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2329  + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2330  f = (total_opp > 2) ? 1 : 0;
2331  } else {
2332  s->mv[0][0][0] = s->mv[0][0][1] = 0;
2333  s->mv[1][0][0] = s->mv[1][0][1] = 0;
2334  f = 0;
2335  }
2336  v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2337  for (k = 0; k < 4; k++) {
2338  s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2339  s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2340  s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2341  s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2342  v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2343  v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2344  }
2345  return;
2346  }
2347  if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2348  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);
2349  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);
2350  return;
2351  }
2352  if (dir) { // backward
2353  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);
2354  if (n == 3 || mv1) {
2355  vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2356  }
2357  } else { // forward
2358  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);
2359  if (n == 3 || mv1) {
2360  vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2361  }
2362  }
2363 }
2364 
2365 /** Get predicted DC value for I-frames only
2366  * prediction dir: left=0, top=1
2367  * @param s MpegEncContext
2368  * @param overlap flag indicating that overlap filtering is used
2369  * @param pq integer part of picture quantizer
2370  * @param[in] n block index in the current MB
2371  * @param dc_val_ptr Pointer to DC predictor
2372  * @param dir_ptr Prediction direction for use in AC prediction
2373  */
2374 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2375  int16_t **dc_val_ptr, int *dir_ptr)
2376 {
2377  int a, b, c, wrap, pred, scale;
2378  int16_t *dc_val;
2379  static const uint16_t dcpred[32] = {
2380  -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2381  114, 102, 93, 85, 79, 73, 68, 64,
2382  60, 57, 54, 51, 49, 47, 45, 43,
2383  41, 39, 38, 37, 35, 34, 33
2384  };
2385 
2386  /* find prediction - wmv3_dc_scale always used here in fact */
2387  if (n < 4) scale = s->y_dc_scale;
2388  else scale = s->c_dc_scale;
2389 
2390  wrap = s->block_wrap[n];
2391  dc_val = s->dc_val[0] + s->block_index[n];
2392 
2393  /* B A
2394  * C X
2395  */
2396  c = dc_val[ - 1];
2397  b = dc_val[ - 1 - wrap];
2398  a = dc_val[ - wrap];
2399 
2400  if (pq < 9 || !overlap) {
2401  /* Set outer values */
2402  if (s->first_slice_line && (n != 2 && n != 3))
2403  b = a = dcpred[scale];
2404  if (s->mb_x == 0 && (n != 1 && n != 3))
2405  b = c = dcpred[scale];
2406  } else {
2407  /* Set outer values */
2408  if (s->first_slice_line && (n != 2 && n != 3))
2409  b = a = 0;
2410  if (s->mb_x == 0 && (n != 1 && n != 3))
2411  b = c = 0;
2412  }
2413 
2414  if (abs(a - b) <= abs(b - c)) {
2415  pred = c;
2416  *dir_ptr = 1; // left
2417  } else {
2418  pred = a;
2419  *dir_ptr = 0; // top
2420  }
2421 
2422  /* update predictor */
2423  *dc_val_ptr = &dc_val[0];
2424  return pred;
2425 }
2426 
2427 
2428 /** Get predicted DC value
2429  * prediction dir: left=0, top=1
2430  * @param s MpegEncContext
2431  * @param overlap flag indicating that overlap filtering is used
2432  * @param pq integer part of picture quantizer
2433  * @param[in] n block index in the current MB
2434  * @param a_avail flag indicating top block availability
2435  * @param c_avail flag indicating left block availability
2436  * @param dc_val_ptr Pointer to DC predictor
2437  * @param dir_ptr Prediction direction for use in AC prediction
2438  */
2439 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2440  int a_avail, int c_avail,
2441  int16_t **dc_val_ptr, int *dir_ptr)
2442 {
2443  int a, b, c, wrap, pred;
2444  int16_t *dc_val;
2445  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2446  int q1, q2 = 0;
2447  int dqscale_index;
2448 
2449  wrap = s->block_wrap[n];
2450  dc_val = s->dc_val[0] + s->block_index[n];
2451 
2452  /* B A
2453  * C X
2454  */
2455  c = dc_val[ - 1];
2456  b = dc_val[ - 1 - wrap];
2457  a = dc_val[ - wrap];
2458  /* scale predictors if needed */
2459  q1 = s->current_picture.qscale_table[mb_pos];
2460  dqscale_index = s->y_dc_scale_table[q1] - 1;
2461  if (dqscale_index < 0)
2462  return 0;
2463  if (c_avail && (n != 1 && n != 3)) {
2464  q2 = s->current_picture.qscale_table[mb_pos - 1];
2465  if (q2 && q2 != q1)
2466  c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2467  }
2468  if (a_avail && (n != 2 && n != 3)) {
2469  q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2470  if (q2 && q2 != q1)
2471  a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2472  }
2473  if (a_avail && c_avail && (n != 3)) {
2474  int off = mb_pos;
2475  if (n != 1)
2476  off--;
2477  if (n != 2)
2478  off -= s->mb_stride;
2479  q2 = s->current_picture.qscale_table[off];
2480  if (q2 && q2 != q1)
2481  b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2482  }
2483 
2484  if (a_avail && c_avail) {
2485  if (abs(a - b) <= abs(b - c)) {
2486  pred = c;
2487  *dir_ptr = 1; // left
2488  } else {
2489  pred = a;
2490  *dir_ptr = 0; // top
2491  }
2492  } else if (a_avail) {
2493  pred = a;
2494  *dir_ptr = 0; // top
2495  } else if (c_avail) {
2496  pred = c;
2497  *dir_ptr = 1; // left
2498  } else {
2499  pred = 0;
2500  *dir_ptr = 1; // left
2501  }
2502 
2503  /* update predictor */
2504  *dc_val_ptr = &dc_val[0];
2505  return pred;
2506 }
2507 
2508 /** @} */ // Block group
2509 
2510 /**
2511  * @name VC1 Macroblock-level functions in Simple/Main Profiles
2512  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2513  * @{
2514  */
2515 
2516 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2517  uint8_t **coded_block_ptr)
2518 {
2519  int xy, wrap, pred, a, b, c;
2520 
2521  xy = s->block_index[n];
2522  wrap = s->b8_stride;
2523 
2524  /* B C
2525  * A X
2526  */
2527  a = s->coded_block[xy - 1 ];
2528  b = s->coded_block[xy - 1 - wrap];
2529  c = s->coded_block[xy - wrap];
2530 
2531  if (b == c) {
2532  pred = a;
2533  } else {
2534  pred = c;
2535  }
2536 
2537  /* store value */
2538  *coded_block_ptr = &s->coded_block[xy];
2539 
2540  return pred;
2541 }
2542 
2543 /**
2544  * Decode one AC coefficient
2545  * @param v The VC1 context
2546  * @param last Last coefficient
2547  * @param skip How much zero coefficients to skip
2548  * @param value Decoded AC coefficient value
2549  * @param codingset set of VLC to decode data
2550  * @see 8.1.3.4
2551  */
2552 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2553  int *value, int codingset)
2554 {
2555  GetBitContext *gb = &v->s.gb;
2556  int index, escape, run = 0, level = 0, lst = 0;
2557 
2558  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2559  if (index != ff_vc1_ac_sizes[codingset] - 1) {
2560  run = vc1_index_decode_table[codingset][index][0];
2561  level = vc1_index_decode_table[codingset][index][1];
2562  lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2563  if (get_bits1(gb))
2564  level = -level;
2565  } else {
2566  escape = decode210(gb);
2567  if (escape != 2) {
2568  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2569  run = vc1_index_decode_table[codingset][index][0];
2570  level = vc1_index_decode_table[codingset][index][1];
2571  lst = index >= vc1_last_decode_table[codingset];
2572  if (escape == 0) {
2573  if (lst)
2574  level += vc1_last_delta_level_table[codingset][run];
2575  else
2576  level += vc1_delta_level_table[codingset][run];
2577  } else {
2578  if (lst)
2579  run += vc1_last_delta_run_table[codingset][level] + 1;
2580  else
2581  run += vc1_delta_run_table[codingset][level] + 1;
2582  }
2583  if (get_bits1(gb))
2584  level = -level;
2585  } else {
2586  int sign;
2587  lst = get_bits1(gb);
2588  if (v->s.esc3_level_length == 0) {
2589  if (v->pq < 8 || v->dquantfrm) { // table 59
2590  v->s.esc3_level_length = get_bits(gb, 3);
2591  if (!v->s.esc3_level_length)
2592  v->s.esc3_level_length = get_bits(gb, 2) + 8;
2593  } else { // table 60
2594  v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2595  }
2596  v->s.esc3_run_length = 3 + get_bits(gb, 2);
2597  }
2598  run = get_bits(gb, v->s.esc3_run_length);
2599  sign = get_bits1(gb);
2600  level = get_bits(gb, v->s.esc3_level_length);
2601  if (sign)
2602  level = -level;
2603  }
2604  }
2605 
2606  *last = lst;
2607  *skip = run;
2608  *value = level;
2609 }
2610 
2611 /** Decode intra block in intra frames - should be faster than decode_intra_block
2612  * @param v VC1Context
2613  * @param block block to decode
2614  * @param[in] n subblock index
2615  * @param coded are AC coeffs present or not
2616  * @param codingset set of VLC to decode data
2617  */
2618 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2619  int coded, int codingset)
2620 {
2621  GetBitContext *gb = &v->s.gb;
2622  MpegEncContext *s = &v->s;
2623  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2624  int i;
2625  int16_t *dc_val;
2626  int16_t *ac_val, *ac_val2;
2627  int dcdiff;
2628 
2629  /* Get DC differential */
2630  if (n < 4) {
2632  } else {
2634  }
2635  if (dcdiff < 0) {
2636  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2637  return -1;
2638  }
2639  if (dcdiff) {
2640  if (dcdiff == 119 /* ESC index value */) {
2641  /* TODO: Optimize */
2642  if (v->pq == 1) dcdiff = get_bits(gb, 10);
2643  else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2644  else dcdiff = get_bits(gb, 8);
2645  } else {
2646  if (v->pq == 1)
2647  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2648  else if (v->pq == 2)
2649  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2650  }
2651  if (get_bits1(gb))
2652  dcdiff = -dcdiff;
2653  }
2654 
2655  /* Prediction */
2656  dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2657  *dc_val = dcdiff;
2658 
2659  /* Store the quantized DC coeff, used for prediction */
2660  if (n < 4) {
2661  block[0] = dcdiff * s->y_dc_scale;
2662  } else {
2663  block[0] = dcdiff * s->c_dc_scale;
2664  }
2665  /* Skip ? */
2666  if (!coded) {
2667  goto not_coded;
2668  }
2669 
2670  // AC Decoding
2671  i = 1;
2672 
2673  {
2674  int last = 0, skip, value;
2675  const uint8_t *zz_table;
2676  int scale;
2677  int k;
2678 
2679  scale = v->pq * 2 + v->halfpq;
2680 
2681  if (v->s.ac_pred) {
2682  if (!dc_pred_dir)
2683  zz_table = v->zz_8x8[2];
2684  else
2685  zz_table = v->zz_8x8[3];
2686  } else
2687  zz_table = v->zz_8x8[1];
2688 
2689  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2690  ac_val2 = ac_val;
2691  if (dc_pred_dir) // left
2692  ac_val -= 16;
2693  else // top
2694  ac_val -= 16 * s->block_wrap[n];
2695 
2696  while (!last) {
2697  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2698  i += skip;
2699  if (i > 63)
2700  break;
2701  block[zz_table[i++]] = value;
2702  }
2703 
2704  /* apply AC prediction if needed */
2705  if (s->ac_pred) {
2706  if (dc_pred_dir) { // left
2707  for (k = 1; k < 8; k++)
2708  block[k << v->left_blk_sh] += ac_val[k];
2709  } else { // top
2710  for (k = 1; k < 8; k++)
2711  block[k << v->top_blk_sh] += ac_val[k + 8];
2712  }
2713  }
2714  /* save AC coeffs for further prediction */
2715  for (k = 1; k < 8; k++) {
2716  ac_val2[k] = block[k << v->left_blk_sh];
2717  ac_val2[k + 8] = block[k << v->top_blk_sh];
2718  }
2719 
2720  /* scale AC coeffs */
2721  for (k = 1; k < 64; k++)
2722  if (block[k]) {
2723  block[k] *= scale;
2724  if (!v->pquantizer)
2725  block[k] += (block[k] < 0) ? -v->pq : v->pq;
2726  }
2727 
2728  if (s->ac_pred) i = 63;
2729  }
2730 
2731 not_coded:
2732  if (!coded) {
2733  int k, scale;
2734  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2735  ac_val2 = ac_val;
2736 
2737  i = 0;
2738  scale = v->pq * 2 + v->halfpq;
2739  memset(ac_val2, 0, 16 * 2);
2740  if (dc_pred_dir) { // left
2741  ac_val -= 16;
2742  if (s->ac_pred)
2743  memcpy(ac_val2, ac_val, 8 * 2);
2744  } else { // top
2745  ac_val -= 16 * s->block_wrap[n];
2746  if (s->ac_pred)
2747  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2748  }
2749 
2750  /* apply AC prediction if needed */
2751  if (s->ac_pred) {
2752  if (dc_pred_dir) { //left
2753  for (k = 1; k < 8; k++) {
2754  block[k << v->left_blk_sh] = ac_val[k] * scale;
2755  if (!v->pquantizer && block[k << v->left_blk_sh])
2756  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2757  }
2758  } else { // top
2759  for (k = 1; k < 8; k++) {
2760  block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2761  if (!v->pquantizer && block[k << v->top_blk_sh])
2762  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2763  }
2764  }
2765  i = 63;
2766  }
2767  }
2768  s->block_last_index[n] = i;
2769 
2770  return 0;
2771 }
2772 
2773 /** Decode intra block in intra frames - should be faster than decode_intra_block
2774  * @param v VC1Context
2775  * @param block block to decode
2776  * @param[in] n subblock number
2777  * @param coded are AC coeffs present or not
2778  * @param codingset set of VLC to decode data
2779  * @param mquant quantizer value for this macroblock
2780  */
2781 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2782  int coded, int codingset, int mquant)
2783 {
2784  GetBitContext *gb = &v->s.gb;
2785  MpegEncContext *s = &v->s;
2786  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2787  int i;
2788  int16_t *dc_val = NULL;
2789  int16_t *ac_val, *ac_val2;
2790  int dcdiff;
2791  int a_avail = v->a_avail, c_avail = v->c_avail;
2792  int use_pred = s->ac_pred;
2793  int scale;
2794  int q1, q2 = 0;
2795  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2796 
2797  /* Get DC differential */
2798  if (n < 4) {
2800  } else {
2802  }
2803  if (dcdiff < 0) {
2804  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2805  return -1;
2806  }
2807  if (dcdiff) {
2808  if (dcdiff == 119 /* ESC index value */) {
2809  /* TODO: Optimize */
2810  if (mquant == 1) dcdiff = get_bits(gb, 10);
2811  else if (mquant == 2) dcdiff = get_bits(gb, 9);
2812  else dcdiff = get_bits(gb, 8);
2813  } else {
2814  if (mquant == 1)
2815  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2816  else if (mquant == 2)
2817  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2818  }
2819  if (get_bits1(gb))
2820  dcdiff = -dcdiff;
2821  }
2822 
2823  /* Prediction */
2824  dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2825  *dc_val = dcdiff;
2826 
2827  /* Store the quantized DC coeff, used for prediction */
2828  if (n < 4) {
2829  block[0] = dcdiff * s->y_dc_scale;
2830  } else {
2831  block[0] = dcdiff * s->c_dc_scale;
2832  }
2833 
2834  //AC Decoding
2835  i = 1;
2836 
2837  /* check if AC is needed at all */
2838  if (!a_avail && !c_avail)
2839  use_pred = 0;
2840  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2841  ac_val2 = ac_val;
2842 
2843  scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2844 
2845  if (dc_pred_dir) // left
2846  ac_val -= 16;
2847  else // top
2848  ac_val -= 16 * s->block_wrap[n];
2849 
2850  q1 = s->current_picture.qscale_table[mb_pos];
2851  if ( dc_pred_dir && c_avail && mb_pos)
2852  q2 = s->current_picture.qscale_table[mb_pos - 1];
2853  if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2854  q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2855  if ( dc_pred_dir && n == 1)
2856  q2 = q1;
2857  if (!dc_pred_dir && n == 2)
2858  q2 = q1;
2859  if (n == 3)
2860  q2 = q1;
2861 
2862  if (coded) {
2863  int last = 0, skip, value;
2864  const uint8_t *zz_table;
2865  int k;
2866 
2867  if (v->s.ac_pred) {
2868  if (!use_pred && v->fcm == ILACE_FRAME) {
2869  zz_table = v->zzi_8x8;
2870  } else {
2871  if (!dc_pred_dir) // top
2872  zz_table = v->zz_8x8[2];
2873  else // left
2874  zz_table = v->zz_8x8[3];
2875  }
2876  } else {
2877  if (v->fcm != ILACE_FRAME)
2878  zz_table = v->zz_8x8[1];
2879  else
2880  zz_table = v->zzi_8x8;
2881  }
2882 
2883  while (!last) {
2884  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2885  i += skip;
2886  if (i > 63)
2887  break;
2888  block[zz_table[i++]] = value;
2889  }
2890 
2891  /* apply AC prediction if needed */
2892  if (use_pred) {
2893  /* scale predictors if needed*/
2894  if (q2 && q1 != q2) {
2895  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2896  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2897 
2898  if (q1 < 1)
2899  return AVERROR_INVALIDDATA;
2900  if (dc_pred_dir) { // left
2901  for (k = 1; k < 8; k++)
2902  block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2903  } else { // top
2904  for (k = 1; k < 8; k++)
2905  block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2906  }
2907  } else {
2908  if (dc_pred_dir) { //left
2909  for (k = 1; k < 8; k++)
2910  block[k << v->left_blk_sh] += ac_val[k];
2911  } else { //top
2912  for (k = 1; k < 8; k++)
2913  block[k << v->top_blk_sh] += ac_val[k + 8];
2914  }
2915  }
2916  }
2917  /* save AC coeffs for further prediction */
2918  for (k = 1; k < 8; k++) {
2919  ac_val2[k ] = block[k << v->left_blk_sh];
2920  ac_val2[k + 8] = block[k << v->top_blk_sh];
2921  }
2922 
2923  /* scale AC coeffs */
2924  for (k = 1; k < 64; k++)
2925  if (block[k]) {
2926  block[k] *= scale;
2927  if (!v->pquantizer)
2928  block[k] += (block[k] < 0) ? -mquant : mquant;
2929  }
2930 
2931  if (use_pred) i = 63;
2932  } else { // no AC coeffs
2933  int k;
2934 
2935  memset(ac_val2, 0, 16 * 2);
2936  if (dc_pred_dir) { // left
2937  if (use_pred) {
2938  memcpy(ac_val2, ac_val, 8 * 2);
2939  if (q2 && q1 != q2) {
2940  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2941  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2942  if (q1 < 1)
2943  return AVERROR_INVALIDDATA;
2944  for (k = 1; k < 8; k++)
2945  ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2946  }
2947  }
2948  } else { // top
2949  if (use_pred) {
2950  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2951  if (q2 && q1 != q2) {
2952  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2953  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2954  if (q1 < 1)
2955  return AVERROR_INVALIDDATA;
2956  for (k = 1; k < 8; k++)
2957  ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2958  }
2959  }
2960  }
2961 
2962  /* apply AC prediction if needed */
2963  if (use_pred) {
2964  if (dc_pred_dir) { // left
2965  for (k = 1; k < 8; k++) {
2966  block[k << v->left_blk_sh] = ac_val2[k] * scale;
2967  if (!v->pquantizer && block[k << v->left_blk_sh])
2968  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2969  }
2970  } else { // top
2971  for (k = 1; k < 8; k++) {
2972  block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2973  if (!v->pquantizer && block[k << v->top_blk_sh])
2974  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2975  }
2976  }
2977  i = 63;
2978  }
2979  }
2980  s->block_last_index[n] = i;
2981 
2982  return 0;
2983 }
2984 
2985 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2986  * @param v VC1Context
2987  * @param block block to decode
2988  * @param[in] n subblock index
2989  * @param coded are AC coeffs present or not
2990  * @param mquant block quantizer
2991  * @param codingset set of VLC to decode data
2992  */
2993 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2994  int coded, int mquant, int codingset)
2995 {
2996  GetBitContext *gb = &v->s.gb;
2997  MpegEncContext *s = &v->s;
2998  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2999  int i;
3000  int16_t *dc_val = NULL;
3001  int16_t *ac_val, *ac_val2;
3002  int dcdiff;
3003  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3004  int a_avail = v->a_avail, c_avail = v->c_avail;
3005  int use_pred = s->ac_pred;
3006  int scale;
3007  int q1, q2 = 0;
3008 
3009  s->dsp.clear_block(block);
3010 
3011  /* XXX: Guard against dumb values of mquant */
3012  mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3013 
3014  /* Set DC scale - y and c use the same */
3015  s->y_dc_scale = s->y_dc_scale_table[mquant];
3016  s->c_dc_scale = s->c_dc_scale_table[mquant];
3017 
3018  /* Get DC differential */
3019  if (n < 4) {
3021  } else {
3023  }
3024  if (dcdiff < 0) {
3025  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3026  return -1;
3027  }
3028  if (dcdiff) {
3029  if (dcdiff == 119 /* ESC index value */) {
3030  /* TODO: Optimize */
3031  if (mquant == 1) dcdiff = get_bits(gb, 10);
3032  else if (mquant == 2) dcdiff = get_bits(gb, 9);
3033  else dcdiff = get_bits(gb, 8);
3034  } else {
3035  if (mquant == 1)
3036  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3037  else if (mquant == 2)
3038  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3039  }
3040  if (get_bits1(gb))
3041  dcdiff = -dcdiff;
3042  }
3043 
3044  /* Prediction */
3045  dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3046  *dc_val = dcdiff;
3047 
3048  /* Store the quantized DC coeff, used for prediction */
3049 
3050  if (n < 4) {
3051  block[0] = dcdiff * s->y_dc_scale;
3052  } else {
3053  block[0] = dcdiff * s->c_dc_scale;
3054  }
3055 
3056  //AC Decoding
3057  i = 1;
3058 
3059  /* check if AC is needed at all and adjust direction if needed */
3060  if (!a_avail) dc_pred_dir = 1;
3061  if (!c_avail) dc_pred_dir = 0;
3062  if (!a_avail && !c_avail) use_pred = 0;
3063  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3064  ac_val2 = ac_val;
3065 
3066  scale = mquant * 2 + v->halfpq;
3067 
3068  if (dc_pred_dir) //left
3069  ac_val -= 16;
3070  else //top
3071  ac_val -= 16 * s->block_wrap[n];
3072 
3073  q1 = s->current_picture.qscale_table[mb_pos];
3074  if (dc_pred_dir && c_avail && mb_pos)
3075  q2 = s->current_picture.qscale_table[mb_pos - 1];
3076  if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3077  q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3078  if ( dc_pred_dir && n == 1)
3079  q2 = q1;
3080  if (!dc_pred_dir && n == 2)
3081  q2 = q1;
3082  if (n == 3) q2 = q1;
3083 
3084  if (coded) {
3085  int last = 0, skip, value;
3086  int k;
3087 
3088  while (!last) {
3089  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3090  i += skip;
3091  if (i > 63)
3092  break;
3093  if (v->fcm == PROGRESSIVE)
3094  block[v->zz_8x8[0][i++]] = value;
3095  else {
3096  if (use_pred && (v->fcm == ILACE_FRAME)) {
3097  if (!dc_pred_dir) // top
3098  block[v->zz_8x8[2][i++]] = value;
3099  else // left
3100  block[v->zz_8x8[3][i++]] = value;
3101  } else {
3102  block[v->zzi_8x8[i++]] = value;
3103  }
3104  }
3105  }
3106 
3107  /* apply AC prediction if needed */
3108  if (use_pred) {
3109  /* scale predictors if needed*/
3110  if (q2 && q1 != q2) {
3111  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3112  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3113 
3114  if (q1 < 1)
3115  return AVERROR_INVALIDDATA;
3116  if (dc_pred_dir) { // left
3117  for (k = 1; k < 8; k++)
3118  block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3119  } else { //top
3120  for (k = 1; k < 8; k++)
3121  block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3122  }
3123  } else {
3124  if (dc_pred_dir) { // left
3125  for (k = 1; k < 8; k++)
3126  block[k << v->left_blk_sh] += ac_val[k];
3127  } else { // top
3128  for (k = 1; k < 8; k++)
3129  block[k << v->top_blk_sh] += ac_val[k + 8];
3130  }
3131  }
3132  }
3133  /* save AC coeffs for further prediction */
3134  for (k = 1; k < 8; k++) {
3135  ac_val2[k ] = block[k << v->left_blk_sh];
3136  ac_val2[k + 8] = block[k << v->top_blk_sh];
3137  }
3138 
3139  /* scale AC coeffs */
3140  for (k = 1; k < 64; k++)
3141  if (block[k]) {
3142  block[k] *= scale;
3143  if (!v->pquantizer)
3144  block[k] += (block[k] < 0) ? -mquant : mquant;
3145  }
3146 
3147  if (use_pred) i = 63;
3148  } else { // no AC coeffs
3149  int k;
3150 
3151  memset(ac_val2, 0, 16 * 2);
3152  if (dc_pred_dir) { // left
3153  if (use_pred) {
3154  memcpy(ac_val2, ac_val, 8 * 2);
3155  if (q2 && q1 != q2) {
3156  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3157  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3158  if (q1 < 1)
3159  return AVERROR_INVALIDDATA;
3160  for (k = 1; k < 8; k++)
3161  ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3162  }
3163  }
3164  } else { // top
3165  if (use_pred) {
3166  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3167  if (q2 && q1 != q2) {
3168  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3169  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3170  if (q1 < 1)
3171  return AVERROR_INVALIDDATA;
3172  for (k = 1; k < 8; k++)
3173  ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3174  }
3175  }
3176  }
3177 
3178  /* apply AC prediction if needed */
3179  if (use_pred) {
3180  if (dc_pred_dir) { // left
3181  for (k = 1; k < 8; k++) {
3182  block[k << v->left_blk_sh] = ac_val2[k] * scale;
3183  if (!v->pquantizer && block[k << v->left_blk_sh])
3184  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3185  }
3186  } else { // top
3187  for (k = 1; k < 8; k++) {
3188  block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3189  if (!v->pquantizer && block[k << v->top_blk_sh])
3190  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3191  }
3192  }
3193  i = 63;
3194  }
3195  }
3196  s->block_last_index[n] = i;
3197 
3198  return 0;
3199 }
3200 
3201 /** Decode P block
3202  */
3203 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3204  int mquant, int ttmb, int first_block,
3205  uint8_t *dst, int linesize, int skip_block,
3206  int *ttmb_out)
3207 {
3208  MpegEncContext *s = &v->s;
3209  GetBitContext *gb = &s->gb;
3210  int i, j;
3211  int subblkpat = 0;
3212  int scale, off, idx, last, skip, value;
3213  int ttblk = ttmb & 7;
3214  int pat = 0;
3215 
3216  s->dsp.clear_block(block);
3217 
3218  if (ttmb == -1) {
3220  }
3221  if (ttblk == TT_4X4) {
3222  subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3223  }
3224  if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3225  && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3226  || (!v->res_rtm_flag && !first_block))) {
3227  subblkpat = decode012(gb);
3228  if (subblkpat)
3229  subblkpat ^= 3; // swap decoded pattern bits
3230  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3231  ttblk = TT_8X4;
3232  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3233  ttblk = TT_4X8;
3234  }
3235  scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3236 
3237  // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3238  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3239  subblkpat = 2 - (ttblk == TT_8X4_TOP);
3240  ttblk = TT_8X4;
3241  }
3242  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3243  subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3244  ttblk = TT_4X8;
3245  }
3246  switch (ttblk) {
3247  case TT_8X8:
3248  pat = 0xF;
3249  i = 0;
3250  last = 0;
3251  while (!last) {
3252  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3253  i += skip;
3254  if (i > 63)
3255  break;
3256  if (!v->fcm)
3257  idx = v->zz_8x8[0][i++];
3258  else
3259  idx = v->zzi_8x8[i++];
3260  block[idx] = value * scale;
3261  if (!v->pquantizer)
3262  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3263  }
3264  if (!skip_block) {
3265  if (i == 1)
3266  v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3267  else {
3268  v->vc1dsp.vc1_inv_trans_8x8(block);
3269  s->dsp.add_pixels_clamped(block, dst, linesize);
3270  }
3271  }
3272  break;
3273  case TT_4X4:
3274  pat = ~subblkpat & 0xF;
3275  for (j = 0; j < 4; j++) {
3276  last = subblkpat & (1 << (3 - j));
3277  i = 0;
3278  off = (j & 1) * 4 + (j & 2) * 16;
3279  while (!last) {
3280  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3281  i += skip;
3282  if (i > 15)
3283  break;
3284  if (!v->fcm)
3286  else
3287  idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3288  block[idx + off] = value * scale;
3289  if (!v->pquantizer)
3290  block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3291  }
3292  if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3293  if (i == 1)
3294  v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3295  else
3296  v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3297  }
3298  }
3299  break;
3300  case TT_8X4:
3301  pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3302  for (j = 0; j < 2; j++) {
3303  last = subblkpat & (1 << (1 - j));
3304  i = 0;
3305  off = j * 32;
3306  while (!last) {
3307  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3308  i += skip;
3309  if (i > 31)
3310  break;
3311  if (!v->fcm)
3312  idx = v->zz_8x4[i++] + off;
3313  else
3314  idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3315  block[idx] = value * scale;
3316  if (!v->pquantizer)
3317  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3318  }
3319  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3320  if (i == 1)
3321  v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3322  else
3323  v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3324  }
3325  }
3326  break;
3327  case TT_4X8:
3328  pat = ~(subblkpat * 5) & 0xF;
3329  for (j = 0; j < 2; j++) {
3330  last = subblkpat & (1 << (1 - j));
3331  i = 0;
3332  off = j * 4;
3333  while (!last) {
3334  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3335  i += skip;
3336  if (i > 31)
3337  break;
3338  if (!v->fcm)
3339  idx = v->zz_4x8[i++] + off;
3340  else
3341  idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3342  block[idx] = value * scale;
3343  if (!v->pquantizer)
3344  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3345  }
3346  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3347  if (i == 1)
3348  v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3349  else
3350  v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3351  }
3352  }
3353  break;
3354  }
3355  if (ttmb_out)
3356  *ttmb_out |= ttblk << (n * 4);
3357  return pat;
3358 }
3359 
3360 /** @} */ // Macroblock group
3361 
3362 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3363 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3364 
3366 {
3367  MpegEncContext *s = &v->s;
3368  int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3369  block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3370  mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3371  block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3372  int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3373  uint8_t *dst;
3374 
3375  if (block_num > 3) {
3376  dst = s->dest[block_num - 3];
3377  } else {
3378  dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3379  }
3380  if (s->mb_y != s->end_mb_y || block_num < 2) {
3381  int16_t (*mv)[2];
3382  int mv_stride;
3383 
3384  if (block_num > 3) {
3385  bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3386  bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3387  mv = &v->luma_mv[s->mb_x - s->mb_stride];
3388  mv_stride = s->mb_stride;
3389  } else {
3390  bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3391  : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3392  bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3393  : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3394  mv_stride = s->b8_stride;
3395  mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3396  }
3397 
3398  if (bottom_is_intra & 1 || block_is_intra & 1 ||
3399  mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3400  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3401  } else {
3402  idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3403  if (idx == 3) {
3404  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3405  } else if (idx) {
3406  if (idx == 1)
3407  v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3408  else
3409  v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3410  }
3411  }
3412  }
3413 
3414  dst -= 4 * linesize;
3415  ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3416  if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3417  idx = (block_cbp | (block_cbp >> 2)) & 3;
3418  if (idx == 3) {
3419  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3420  } else if (idx) {
3421  if (idx == 1)
3422  v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3423  else
3424  v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3425  }
3426  }
3427 }
3428 
3430 {
3431  MpegEncContext *s = &v->s;
3432  int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3433  block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3434  mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3435  block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3436  int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3437  uint8_t *dst;
3438 
3439  if (block_num > 3) {
3440  dst = s->dest[block_num - 3] - 8 * linesize;
3441  } else {
3442  dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3443  }
3444 
3445  if (s->mb_x != s->mb_width || !(block_num & 5)) {
3446  int16_t (*mv)[2];
3447 
3448  if (block_num > 3) {
3449  right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3450  right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3451  mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3452  } else {
3453  right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3454  : (mb_cbp >> ((block_num + 1) * 4));
3455  right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3456  : (mb_is_intra >> ((block_num + 1) * 4));
3457  mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3458  }
3459  if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3460  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3461  } else {
3462  idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3463  if (idx == 5) {
3464  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3465  } else if (idx) {
3466  if (idx == 1)
3467  v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3468  else
3469  v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3470  }
3471  }
3472  }
3473 
3474  dst -= 4;
3475  ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3476  if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3477  idx = (block_cbp | (block_cbp >> 1)) & 5;
3478  if (idx == 5) {
3479  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3480  } else if (idx) {
3481  if (idx == 1)
3482  v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3483  else
3484  v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3485  }
3486  }
3487 }
3488 
3490 {
3491  MpegEncContext *s = &v->s;
3492  int i;
3493 
3494  for (i = 0; i < 6; i++) {
3496  }
3497 
3498  /* V always precedes H, therefore we run H one MB before V;
3499  * at the end of a row, we catch up to complete the row */
3500  if (s->mb_x) {
3501  for (i = 0; i < 6; i++) {
3503  }
3504  if (s->mb_x == s->mb_width - 1) {
3505  s->mb_x++;
3507  for (i = 0; i < 6; i++) {
3509  }
3510  }
3511  }
3512 }
3513 
3514 /** Decode one P-frame MB
3515  */
3517 {
3518  MpegEncContext *s = &v->s;
3519  GetBitContext *gb = &s->gb;
3520  int i, j;
3521  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3522  int cbp; /* cbp decoding stuff */
3523  int mqdiff, mquant; /* MB quantization */
3524  int ttmb = v->ttfrm; /* MB Transform type */
3525 
3526  int mb_has_coeffs = 1; /* last_flag */
3527  int dmv_x, dmv_y; /* Differential MV components */
3528  int index, index1; /* LUT indexes */
3529  int val, sign; /* temp values */
3530  int first_block = 1;
3531  int dst_idx, off;
3532  int skipped, fourmv;
3533  int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3534 
3535  mquant = v->pq; /* lossy initialization */
3536 
3537  if (v->mv_type_is_raw)
3538  fourmv = get_bits1(gb);
3539  else
3540  fourmv = v->mv_type_mb_plane[mb_pos];
3541  if (v->skip_is_raw)
3542  skipped = get_bits1(gb);
3543  else
3544  skipped = v->s.mbskip_table[mb_pos];
3545 
3546  if (!fourmv) { /* 1MV mode */
3547  if (!skipped) {
3548  GET_MVDATA(dmv_x, dmv_y);
3549 
3550  if (s->mb_intra) {
3551  s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3552  s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3553  }
3555  vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3556 
3557  /* FIXME Set DC val for inter block ? */
3558  if (s->mb_intra && !mb_has_coeffs) {
3559  GET_MQUANT();
3560  s->ac_pred = get_bits1(gb);
3561  cbp = 0;
3562  } else if (mb_has_coeffs) {
3563  if (s->mb_intra)
3564  s->ac_pred = get_bits1(gb);
3565  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3566  GET_MQUANT();
3567  } else {
3568  mquant = v->pq;
3569  cbp = 0;
3570  }
3571  s->current_picture.qscale_table[mb_pos] = mquant;
3572 
3573  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3574  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3575  VC1_TTMB_VLC_BITS, 2);
3576  if (!s->mb_intra) vc1_mc_1mv(v, 0);
3577  dst_idx = 0;
3578  for (i = 0; i < 6; i++) {
3579  s->dc_val[0][s->block_index[i]] = 0;
3580  dst_idx += i >> 2;
3581  val = ((cbp >> (5 - i)) & 1);
3582  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3583  v->mb_type[0][s->block_index[i]] = s->mb_intra;
3584  if (s->mb_intra) {
3585  /* check if prediction blocks A and C are available */
3586  v->a_avail = v->c_avail = 0;
3587  if (i == 2 || i == 3 || !s->first_slice_line)
3588  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3589  if (i == 1 || i == 3 || s->mb_x)
3590  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3591 
3592  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3593  (i & 4) ? v->codingset2 : v->codingset);
3594  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3595  continue;
3596  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3597  if (v->rangeredfrm)
3598  for (j = 0; j < 64; j++)
3599  s->block[i][j] <<= 1;
3600  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3601  if (v->pq >= 9 && v->overlap) {
3602  if (v->c_avail)
3603  v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3604  if (v->a_avail)
3605  v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3606  }
3607  block_cbp |= 0xF << (i << 2);
3608  block_intra |= 1 << i;
3609  } else if (val) {
3610  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3611  s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3612  (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3613  block_cbp |= pat << (i << 2);
3614  if (!v->ttmbf && ttmb < 8)
3615  ttmb = -1;
3616  first_block = 0;
3617  }
3618  }
3619  } else { // skipped
3620  s->mb_intra = 0;
3621  for (i = 0; i < 6; i++) {
3622  v->mb_type[0][s->block_index[i]] = 0;
3623  s->dc_val[0][s->block_index[i]] = 0;
3624  }
3625  s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3626  s->current_picture.qscale_table[mb_pos] = 0;
3627  vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3628  vc1_mc_1mv(v, 0);
3629  }
3630  } else { // 4MV mode
3631  if (!skipped /* unskipped MB */) {
3632  int intra_count = 0, coded_inter = 0;
3633  int is_intra[6], is_coded[6];
3634  /* Get CBPCY */
3635  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3636  for (i = 0; i < 6; i++) {
3637  val = ((cbp >> (5 - i)) & 1);
3638  s->dc_val[0][s->block_index[i]] = 0;
3639  s->mb_intra = 0;
3640  if (i < 4) {
3641  dmv_x = dmv_y = 0;
3642  s->mb_intra = 0;
3643  mb_has_coeffs = 0;
3644  if (val) {
3645  GET_MVDATA(dmv_x, dmv_y);
3646  }
3647  vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3648  if (!s->mb_intra)
3649  vc1_mc_4mv_luma(v, i, 0, 0);
3650  intra_count += s->mb_intra;
3651  is_intra[i] = s->mb_intra;
3652  is_coded[i] = mb_has_coeffs;
3653  }
3654  if (i & 4) {
3655  is_intra[i] = (intra_count >= 3);
3656  is_coded[i] = val;
3657  }
3658  if (i == 4)
3659  vc1_mc_4mv_chroma(v, 0);
3660  v->mb_type[0][s->block_index[i]] = is_intra[i];
3661  if (!coded_inter)
3662  coded_inter = !is_intra[i] & is_coded[i];
3663  }
3664  // if there are no coded blocks then don't do anything more
3665  dst_idx = 0;
3666  if (!intra_count && !coded_inter)
3667  goto end;
3668  GET_MQUANT();
3669  s->current_picture.qscale_table[mb_pos] = mquant;
3670  /* test if block is intra and has pred */
3671  {
3672  int intrapred = 0;
3673  for (i = 0; i < 6; i++)
3674  if (is_intra[i]) {
3675  if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3676  || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3677  intrapred = 1;
3678  break;
3679  }
3680  }
3681  if (intrapred)
3682  s->ac_pred = get_bits1(gb);
3683  else
3684  s->ac_pred = 0;
3685  }
3686  if (!v->ttmbf && coded_inter)
3688  for (i = 0; i < 6; i++) {
3689  dst_idx += i >> 2;
3690  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3691  s->mb_intra = is_intra[i];
3692  if (is_intra[i]) {
3693  /* check if prediction blocks A and C are available */
3694  v->a_avail = v->c_avail = 0;
3695  if (i == 2 || i == 3 || !s->first_slice_line)
3696  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3697  if (i == 1 || i == 3 || s->mb_x)
3698  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3699 
3700  vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3701  (i & 4) ? v->codingset2 : v->codingset);
3702  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3703  continue;
3704  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3705  if (v->rangeredfrm)
3706  for (j = 0; j < 64; j++)
3707  s->block[i][j] <<= 1;
3708  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3709  (i & 4) ? s->uvlinesize : s->linesize);
3710  if (v->pq >= 9 && v->overlap) {
3711  if (v->c_avail)
3712  v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3713  if (v->a_avail)
3714  v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3715  }
3716  block_cbp |= 0xF << (i << 2);
3717  block_intra |= 1 << i;
3718  } else if (is_coded[i]) {
3719  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3720  first_block, s->dest[dst_idx] + off,
3721  (i & 4) ? s->uvlinesize : s->linesize,
3722  (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3723  &block_tt);
3724  block_cbp |= pat << (i << 2);
3725  if (!v->ttmbf && ttmb < 8)
3726  ttmb = -1;
3727  first_block = 0;
3728  }
3729  }
3730  } else { // skipped MB
3731  s->mb_intra = 0;
3732  s->current_picture.qscale_table[mb_pos] = 0;
3733  for (i = 0; i < 6; i++) {
3734  v->mb_type[0][s->block_index[i]] = 0;
3735  s->dc_val[0][s->block_index[i]] = 0;
3736  }
3737  for (i = 0; i < 4; i++) {
3738  vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3739  vc1_mc_4mv_luma(v, i, 0, 0);
3740  }
3741  vc1_mc_4mv_chroma(v, 0);
3742  s->current_picture.qscale_table[mb_pos] = 0;
3743  }
3744  }
3745 end:
3746  v->cbp[s->mb_x] = block_cbp;
3747  v->ttblk[s->mb_x] = block_tt;
3748  v->is_intra[s->mb_x] = block_intra;
3749 
3750  return 0;
3751 }
3752 
3753 /* Decode one macroblock in an interlaced frame p picture */
3754 
3756 {
3757  MpegEncContext *s = &v->s;
3758  GetBitContext *gb = &s->gb;
3759  int i;
3760  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3761  int cbp = 0; /* cbp decoding stuff */
3762  int mqdiff, mquant; /* MB quantization */
3763  int ttmb = v->ttfrm; /* MB Transform type */
3764 
3765  int mb_has_coeffs = 1; /* last_flag */
3766  int dmv_x, dmv_y; /* Differential MV components */
3767  int val; /* temp value */
3768  int first_block = 1;
3769  int dst_idx, off;
3770  int skipped, fourmv = 0, twomv = 0;
3771  int block_cbp = 0, pat, block_tt = 0;
3772  int idx_mbmode = 0, mvbp;
3773  int stride_y, fieldtx;
3774 
3775  mquant = v->pq; /* Lossy initialization */
3776 
3777  if (v->skip_is_raw)
3778  skipped = get_bits1(gb);
3779  else
3780  skipped = v->s.mbskip_table[mb_pos];
3781  if (!skipped) {
3782  if (v->fourmvswitch)
3783  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3784  else
3785  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3786  switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3787  /* store the motion vector type in a flag (useful later) */
3788  case MV_PMODE_INTFR_4MV:
3789  fourmv = 1;
3790  v->blk_mv_type[s->block_index[0]] = 0;
3791  v->blk_mv_type[s->block_index[1]] = 0;
3792  v->blk_mv_type[s->block_index[2]] = 0;
3793  v->blk_mv_type[s->block_index[3]] = 0;
3794  break;
3796  fourmv = 1;
3797  v->blk_mv_type[s->block_index[0]] = 1;
3798  v->blk_mv_type[s->block_index[1]] = 1;
3799  v->blk_mv_type[s->block_index[2]] = 1;
3800  v->blk_mv_type[s->block_index[3]] = 1;
3801  break;
3803  twomv = 1;
3804  v->blk_mv_type[s->block_index[0]] = 1;
3805  v->blk_mv_type[s->block_index[1]] = 1;
3806  v->blk_mv_type[s->block_index[2]] = 1;
3807  v->blk_mv_type[s->block_index[3]] = 1;
3808  break;
3809  case MV_PMODE_INTFR_1MV:
3810  v->blk_mv_type[s->block_index[0]] = 0;
3811  v->blk_mv_type[s->block_index[1]] = 0;
3812  v->blk_mv_type[s->block_index[2]] = 0;
3813  v->blk_mv_type[s->block_index[3]] = 0;
3814  break;
3815  }
3816  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3817  for (i = 0; i < 4; i++) {
3818  s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3819  s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3820  }
3821  s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3822  s->mb_intra = v->is_intra[s->mb_x] = 1;
3823  for (i = 0; i < 6; i++)
3824  v->mb_type[0][s->block_index[i]] = 1;
3825  fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3826  mb_has_coeffs = get_bits1(gb);
3827  if (mb_has_coeffs)
3828  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3829  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3830  GET_MQUANT();
3831  s->current_picture.qscale_table[mb_pos] = mquant;
3832  /* Set DC scale - y and c use the same (not sure if necessary here) */
3833  s->y_dc_scale = s->y_dc_scale_table[mquant];
3834  s->c_dc_scale = s->c_dc_scale_table[mquant];
3835  dst_idx = 0;
3836  for (i = 0; i < 6; i++) {
3837  s->dc_val[0][s->block_index[i]] = 0;
3838  dst_idx += i >> 2;
3839  val = ((cbp >> (5 - i)) & 1);
3840  v->mb_type[0][s->block_index[i]] = s->mb_intra;
3841  v->a_avail = v->c_avail = 0;
3842  if (i == 2 || i == 3 || !s->first_slice_line)
3843  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3844  if (i == 1 || i == 3 || s->mb_x)
3845  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3846 
3847  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3848  (i & 4) ? v->codingset2 : v->codingset);
3849  if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3850  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3851  if (i < 4) {
3852  stride_y = s->linesize << fieldtx;
3853  off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3854  } else {
3855  stride_y = s->uvlinesize;
3856  off = 0;
3857  }
3858  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3859  //TODO: loop filter
3860  }
3861 
3862  } else { // inter MB
3863  mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3864  if (mb_has_coeffs)
3865  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3866  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3868  } else {
3869  if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3870  || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3872  }
3873  }
3874  s->mb_intra = v->is_intra[s->mb_x] = 0;
3875  for (i = 0; i < 6; i++)
3876  v->mb_type[0][s->block_index[i]] = 0;
3877  fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3878  /* for all motion vector read MVDATA and motion compensate each block */
3879  dst_idx = 0;
3880  if (fourmv) {
3881  mvbp = v->fourmvbp;
3882  for (i = 0; i < 6; i++) {
3883  if (i < 4) {
3884  dmv_x = dmv_y = 0;
3885  val = ((mvbp >> (3 - i)) & 1);
3886  if (val) {
3887  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3888  }
3889  vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3890  vc1_mc_4mv_luma(v, i, 0, 0);
3891  } else if (i == 4) {
3892  vc1_mc_4mv_chroma4(v, 0, 0, 0);
3893  }
3894  }
3895  } else if (twomv) {
3896  mvbp = v->twomvbp;
3897  dmv_x = dmv_y = 0;
3898  if (mvbp & 2) {
3899  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3900  }
3901  vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3902  vc1_mc_4mv_luma(v, 0, 0, 0);
3903  vc1_mc_4mv_luma(v, 1, 0, 0);
3904  dmv_x = dmv_y = 0;
3905  if (mvbp & 1) {
3906  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3907  }
3908  vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3909  vc1_mc_4mv_luma(v, 2, 0, 0);
3910  vc1_mc_4mv_luma(v, 3, 0, 0);
3911  vc1_mc_4mv_chroma4(v, 0, 0, 0);
3912  } else {
3913  mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3914  dmv_x = dmv_y = 0;
3915  if (mvbp) {
3916  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3917  }
3918  vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3919  vc1_mc_1mv(v, 0);
3920  }
3921  if (cbp)
3922  GET_MQUANT(); // p. 227
3923  s->current_picture.qscale_table[mb_pos] = mquant;
3924  if (!v->ttmbf && cbp)
3926  for (i = 0; i < 6; i++) {
3927  s->dc_val[0][s->block_index[i]] = 0;
3928  dst_idx += i >> 2;
3929  val = ((cbp >> (5 - i)) & 1);
3930  if (!fieldtx)
3931  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3932  else
3933  off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3934  if (val) {
3935  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3936  first_block, s->dest[dst_idx] + off,
3937  (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3938  (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3939  block_cbp |= pat << (i << 2);
3940  if (!v->ttmbf && ttmb < 8)
3941  ttmb = -1;
3942  first_block = 0;
3943  }
3944  }
3945  }
3946  } else { // skipped
3947  s->mb_intra = v->is_intra[s->mb_x] = 0;
3948  for (i = 0; i < 6; i++) {
3949  v->mb_type[0][s->block_index[i]] = 0;
3950  s->dc_val[0][s->block_index[i]] = 0;
3951  }
3952  s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3953  s->current_picture.qscale_table[mb_pos] = 0;
3954  v->blk_mv_type[s->block_index[0]] = 0;
3955  v->blk_mv_type[s->block_index[1]] = 0;
3956  v->blk_mv_type[s->block_index[2]] = 0;
3957  v->blk_mv_type[s->block_index[3]] = 0;
3958  vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3959  vc1_mc_1mv(v, 0);
3960  }
3961  if (s->mb_x == s->mb_width - 1)
3962  memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3963  return 0;
3964 }
3965 
3967 {
3968  MpegEncContext *s = &v->s;
3969  GetBitContext *gb = &s->gb;
3970  int i;
3971  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3972  int cbp = 0; /* cbp decoding stuff */
3973  int mqdiff, mquant; /* MB quantization */
3974  int ttmb = v->ttfrm; /* MB Transform type */
3975 
3976  int mb_has_coeffs = 1; /* last_flag */
3977  int dmv_x, dmv_y; /* Differential MV components */
3978  int val; /* temp values */
3979  int first_block = 1;
3980  int dst_idx, off;
3981  int pred_flag = 0;
3982  int block_cbp = 0, pat, block_tt = 0;
3983  int idx_mbmode = 0;
3984 
3985  mquant = v->pq; /* Lossy initialization */
3986 
3987  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3988  if (idx_mbmode <= 1) { // intra MB
3989  s->mb_intra = v->is_intra[s->mb_x] = 1;
3990  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3991  s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3992  s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3993  GET_MQUANT();
3994  s->current_picture.qscale_table[mb_pos] = mquant;
3995  /* Set DC scale - y and c use the same (not sure if necessary here) */
3996  s->y_dc_scale = s->y_dc_scale_table[mquant];
3997  s->c_dc_scale = s->c_dc_scale_table[mquant];
3998  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3999  mb_has_coeffs = idx_mbmode & 1;
4000  if (mb_has_coeffs)
4001  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4002  dst_idx = 0;
4003  for (i = 0; i < 6; i++) {
4004  s->dc_val[0][s->block_index[i]] = 0;
4005  v->mb_type[0][s->block_index[i]] = 1;
4006  dst_idx += i >> 2;
4007  val = ((cbp >> (5 - i)) & 1);
4008  v->a_avail = v->c_avail = 0;
4009  if (i == 2 || i == 3 || !s->first_slice_line)
4010  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4011  if (i == 1 || i == 3 || s->mb_x)
4012  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4013 
4014  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4015  (i & 4) ? v->codingset2 : v->codingset);
4016  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4017  continue;
4018  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4019  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4020  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4021  // TODO: loop filter
4022  }
4023  } else {
4024  s->mb_intra = v->is_intra[s->mb_x] = 0;
4025  s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4026  for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4027  if (idx_mbmode <= 5) { // 1-MV
4028  dmv_x = dmv_y = pred_flag = 0;
4029  if (idx_mbmode & 1) {
4030  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4031  }
4032  vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4033  vc1_mc_1mv(v, 0);
4034  mb_has_coeffs = !(idx_mbmode & 2);
4035  } else { // 4-MV
4037  for (i = 0; i < 6; i++) {
4038  if (i < 4) {
4039  dmv_x = dmv_y = pred_flag = 0;
4040  val = ((v->fourmvbp >> (3 - i)) & 1);
4041  if (val) {
4042  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4043  }
4044  vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4045  vc1_mc_4mv_luma(v, i, 0, 0);
4046  } else if (i == 4)
4047  vc1_mc_4mv_chroma(v, 0);
4048  }
4049  mb_has_coeffs = idx_mbmode & 1;
4050  }
4051  if (mb_has_coeffs)
4052  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4053  if (cbp) {
4054  GET_MQUANT();
4055  }
4056  s->current_picture.qscale_table[mb_pos] = mquant;
4057  if (!v->ttmbf && cbp) {
4059  }
4060  dst_idx = 0;
4061  for (i = 0; i < 6; i++) {
4062  s->dc_val[0][s->block_index[i]] = 0;
4063  dst_idx += i >> 2;
4064  val = ((cbp >> (5 - i)) & 1);
4065  off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4066  if (val) {
4067  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4068  first_block, s->dest[dst_idx] + off,
4069  (i & 4) ? s->uvlinesize : s->linesize,
4070  (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4071  &block_tt);
4072  block_cbp |= pat << (i << 2);
4073  if (!v->ttmbf && ttmb < 8) ttmb = -1;
4074  first_block = 0;
4075  }
4076  }
4077  }
4078  if (s->mb_x == s->mb_width - 1)
4079  memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4080  return 0;
4081 }
4082 
4083 /** Decode one B-frame MB (in Main profile)
4084  */
4086 {
4087  MpegEncContext *s = &v->s;
4088  GetBitContext *gb = &s->gb;
4089  int i, j;
4090  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4091  int cbp = 0; /* cbp decoding stuff */
4092  int mqdiff, mquant; /* MB quantization */
4093  int ttmb = v->ttfrm; /* MB Transform type */
4094  int mb_has_coeffs = 0; /* last_flag */
4095  int index, index1; /* LUT indexes */
4096  int val, sign; /* temp values */
4097  int first_block = 1;
4098  int dst_idx, off;
4099  int skipped, direct;
4100  int dmv_x[2], dmv_y[2];
4101  int bmvtype = BMV_TYPE_BACKWARD;
4102 
4103  mquant = v->pq; /* lossy initialization */
4104  s->mb_intra = 0;
4105 
4106  if (v->dmb_is_raw)
4107  direct = get_bits1(gb);
4108  else
4109  direct = v->direct_mb_plane[mb_pos];
4110  if (v->skip_is_raw)
4111  skipped = get_bits1(gb);
4112  else
4113  skipped = v->s.mbskip_table[mb_pos];
4114 
4115  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4116  for (i = 0; i < 6; i++) {
4117  v->mb_type[0][s->block_index[i]] = 0;
4118  s->dc_val[0][s->block_index[i]] = 0;
4119  }
4120  s->current_picture.qscale_table[mb_pos] = 0;
4121 
4122  if (!direct) {
4123  if (!skipped) {
4124  GET_MVDATA(dmv_x[0], dmv_y[0]);
4125  dmv_x[1] = dmv_x[0];
4126  dmv_y[1] = dmv_y[0];
4127  }
4128  if (skipped || !s->mb_intra) {
4129  bmvtype = decode012(gb);
4130  switch (bmvtype) {
4131  case 0:
4132  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4133  break;
4134  case 1:
4135  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4136  break;
4137  case 2:
4138  bmvtype = BMV_TYPE_INTERPOLATED;
4139  dmv_x[0] = dmv_y[0] = 0;
4140  }
4141  }
4142  }
4143  for (i = 0; i < 6; i++)
4144  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4145 
4146  if (skipped) {
4147  if (direct)
4148  bmvtype = BMV_TYPE_INTERPOLATED;
4149  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4150  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4151  return;
4152  }
4153  if (direct) {
4154  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4155  GET_MQUANT();
4156  s->mb_intra = 0;
4157  s->current_picture.qscale_table[mb_pos] = mquant;
4158  if (!v->ttmbf)
4160  dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4161  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4162  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4163  } else {
4164  if (!mb_has_coeffs && !s->mb_intra) {
4165  /* no coded blocks - effectively skipped */
4166  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4167  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4168  return;
4169  }
4170  if (s->mb_intra && !mb_has_coeffs) {
4171  GET_MQUANT();
4172  s->current_picture.qscale_table[mb_pos] = mquant;
4173  s->ac_pred = get_bits1(gb);
4174  cbp = 0;
4175  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4176  } else {
4177  if (bmvtype == BMV_TYPE_INTERPOLATED) {
4178  GET_MVDATA(dmv_x[0], dmv_y[0]);
4179  if (!mb_has_coeffs) {
4180  /* interpolated skipped block */
4181  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4182  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4183  return;
4184  }
4185  }
4186  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4187  if (!s->mb_intra) {
4188  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4189  }
4190  if (s->mb_intra)
4191  s->ac_pred = get_bits1(gb);
4192  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4193  GET_MQUANT();
4194  s->current_picture.qscale_table[mb_pos] = mquant;
4195  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4197  }
4198  }
4199  dst_idx = 0;
4200  for (i = 0; i < 6; i++) {
4201  s->dc_val[0][s->block_index[i]] = 0;
4202  dst_idx += i >> 2;
4203  val = ((cbp >> (5 - i)) & 1);
4204  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4205  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4206  if (s->mb_intra) {
4207  /* check if prediction blocks A and C are available */
4208  v->a_avail = v->c_avail = 0;
4209  if (i == 2 || i == 3 || !s->first_slice_line)
4210  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4211  if (i == 1 || i == 3 || s->mb_x)
4212  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4213 
4214  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4215  (i & 4) ? v->codingset2 : v->codingset);
4216  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4217  continue;
4218  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4219  if (v->rangeredfrm)
4220  for (j = 0; j < 64; j++)
4221  s->block[i][j] <<= 1;
4222  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4223  } else if (val) {
4224  vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4225  first_block, s->dest[dst_idx] + off,
4226  (i & 4) ? s->uvlinesize : s->linesize,
4227  (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4228  if (!v->ttmbf && ttmb < 8)
4229  ttmb = -1;
4230  first_block = 0;
4231  }
4232  }
4233 }
4234 
4235 /** Decode one B-frame MB (in interlaced field B picture)
4236  */
4238 {
4239  MpegEncContext *s = &v->s;
4240  GetBitContext *gb = &s->gb;
4241  int i, j;
4242  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4243  int cbp = 0; /* cbp decoding stuff */
4244  int mqdiff, mquant; /* MB quantization */
4245  int ttmb = v->ttfrm; /* MB Transform type */
4246  int mb_has_coeffs = 0; /* last_flag */
4247  int val; /* temp value */
4248  int first_block = 1;
4249  int dst_idx, off;
4250  int fwd;
4251  int dmv_x[2], dmv_y[2], pred_flag[2];
4252  int bmvtype = BMV_TYPE_BACKWARD;
4253  int idx_mbmode;
4254 
4255  mquant = v->pq; /* Lossy initialization */
4256  s->mb_intra = 0;
4257 
4258  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4259  if (idx_mbmode <= 1) { // intra MB
4260  s->mb_intra = v->is_intra[s->mb_x] = 1;
4261  s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4262  s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4263  s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4264  GET_MQUANT();
4265  s->current_picture.qscale_table[mb_pos] = mquant;
4266  /* Set DC scale - y and c use the same (not sure if necessary here) */
4267  s->y_dc_scale = s->y_dc_scale_table[mquant];
4268  s->c_dc_scale = s->c_dc_scale_table[mquant];
4269  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4270  mb_has_coeffs = idx_mbmode & 1;
4271  if (mb_has_coeffs)
4272  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4273  dst_idx = 0;
4274  for (i = 0; i < 6; i++) {
4275  s->dc_val[0][s->block_index[i]] = 0;
4276  dst_idx += i >> 2;
4277  val = ((cbp >> (5 - i)) & 1);
4278  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4279  v->a_avail = v->c_avail = 0;
4280  if (i == 2 || i == 3 || !s->first_slice_line)
4281  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4282  if (i == 1 || i == 3 || s->mb_x)
4283  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4284 
4285  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4286  (i & 4) ? v->codingset2 : v->codingset);
4287  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4288  continue;
4289  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4290  if (v->rangeredfrm)
4291  for (j = 0; j < 64; j++)
4292  s->block[i][j] <<= 1;
4293  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4294  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4295  // TODO: yet to perform loop filter
4296  }
4297  } else {
4298  s->mb_intra = v->is_intra[s->mb_x] = 0;
4299  s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4300  for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4301  if (v->fmb_is_raw)
4302  fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4303  else
4304  fwd = v->forward_mb_plane[mb_pos];
4305  if (idx_mbmode <= 5) { // 1-MV
4306  int interpmvp = 0;
4307  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4308  pred_flag[0] = pred_flag[1] = 0;
4309  if (fwd)
4310  bmvtype = BMV_TYPE_FORWARD;
4311  else {
4312  bmvtype = decode012(gb);
4313  switch (bmvtype) {
4314  case 0:
4315  bmvtype = BMV_TYPE_BACKWARD;
4316  break;
4317  case 1:
4318  bmvtype = BMV_TYPE_DIRECT;
4319  break;
4320  case 2:
4321  bmvtype = BMV_TYPE_INTERPOLATED;
4322  interpmvp = get_bits1(gb);
4323  }
4324  }
4325  v->bmvtype = bmvtype;
4326  if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4327  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4328  }
4329  if (interpmvp) {
4330  get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4331  }
4332  if (bmvtype == BMV_TYPE_DIRECT) {
4333  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4334  dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4335  }
4336  vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4337  vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4338  mb_has_coeffs = !(idx_mbmode & 2);
4339  } else { // 4-MV
4340  if (fwd)
4341  bmvtype = BMV_TYPE_FORWARD;
4342  v->bmvtype = bmvtype;
4344  for (i = 0; i < 6; i++) {
4345  if (i < 4) {
4346  dmv_