FFmpeg
 All Data Structures 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 "dsputil.h"
31 #include "avcodec.h"
32 #include "mpegvideo.h"
33 #include "h263.h"
34 #include "vc1.h"
35 #include "vc1data.h"
36 #include "vc1acdata.h"
37 #include "msmpeg4data.h"
38 #include "unary.h"
39 #include "mathops.h"
40 #include "vdpau_internal.h"
41 #include "libavutil/avassert.h"
42 
43 #undef NDEBUG
44 #include <assert.h>
45 
46 #define MB_INTRA_VLC_BITS 9
47 #define DC_VLC_BITS 9
48 
49 
50 // offset tables for interlaced picture MVDATA decoding
51 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
52 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
53 
54 /***********************************************************************/
55 /**
56  * @name VC-1 Bitplane decoding
57  * @see 8.7, p56
58  * @{
59  */
60 
61 /**
62  * Imode types
63  * @{
64  */
65 enum Imode {
73 };
74 /** @} */ //imode defines
75 
76 
77 /** @} */ //Bitplane group
78 
80 {
81  MpegEncContext *s = &v->s;
82  int topleft_mb_pos, top_mb_pos;
83  int stride_y, fieldtx;
84  int v_dist;
85 
86  /* The put pixels loop is always one MB row behind the decoding loop,
87  * because we can only put pixels when overlap filtering is done, and
88  * for filtering of the bottom edge of a MB, we need the next MB row
89  * present as well.
90  * Within the row, the put pixels loop is also one MB col behind the
91  * decoding loop. The reason for this is again, because for filtering
92  * of the right MB edge, we need the next MB present. */
93  if (!s->first_slice_line) {
94  if (s->mb_x) {
95  topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
96  fieldtx = v->fieldtx_plane[topleft_mb_pos];
97  stride_y = s->linesize << fieldtx;
98  v_dist = (16 - fieldtx) >> (fieldtx == 0);
100  s->dest[0] - 16 * s->linesize - 16,
101  stride_y);
103  s->dest[0] - 16 * s->linesize - 8,
104  stride_y);
106  s->dest[0] - v_dist * s->linesize - 16,
107  stride_y);
109  s->dest[0] - v_dist * s->linesize - 8,
110  stride_y);
112  s->dest[1] - 8 * s->uvlinesize - 8,
113  s->uvlinesize);
115  s->dest[2] - 8 * s->uvlinesize - 8,
116  s->uvlinesize);
117  }
118  if (s->mb_x == s->mb_width - 1) {
119  top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
120  fieldtx = v->fieldtx_plane[top_mb_pos];
121  stride_y = s->linesize << fieldtx;
122  v_dist = fieldtx ? 15 : 8;
124  s->dest[0] - 16 * s->linesize,
125  stride_y);
127  s->dest[0] - 16 * s->linesize + 8,
128  stride_y);
130  s->dest[0] - v_dist * s->linesize,
131  stride_y);
133  s->dest[0] - v_dist * s->linesize + 8,
134  stride_y);
136  s->dest[1] - 8 * s->uvlinesize,
137  s->uvlinesize);
139  s->dest[2] - 8 * s->uvlinesize,
140  s->uvlinesize);
141  }
142  }
143 
144 #define inc_blk_idx(idx) do { \
145  idx++; \
146  if (idx >= v->n_allocated_blks) \
147  idx = 0; \
148  } while (0)
149 
154 }
155 
156 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
157 {
158  MpegEncContext *s = &v->s;
159  int j;
160  if (!s->first_slice_line) {
161  v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
162  if (s->mb_x)
163  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
164  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
165  for (j = 0; j < 2; j++) {
166  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
167  if (s->mb_x)
168  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
169  }
170  }
171  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
172 
173  if (s->mb_y == s->end_mb_y - 1) {
174  if (s->mb_x) {
175  v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
176  v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
177  v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
178  }
179  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
180  }
181 }
182 
184 {
185  MpegEncContext *s = &v->s;
186  int j;
187 
188  /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
189  * means it runs two rows/cols behind the decoding loop. */
190  if (!s->first_slice_line) {
191  if (s->mb_x) {
192  if (s->mb_y >= s->start_mb_y + 2) {
193  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
194 
195  if (s->mb_x >= 2)
196  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
197  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
198  for (j = 0; j < 2; j++) {
199  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
200  if (s->mb_x >= 2) {
201  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
202  }
203  }
204  }
205  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
206  }
207 
208  if (s->mb_x == s->mb_width - 1) {
209  if (s->mb_y >= s->start_mb_y + 2) {
210  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
211 
212  if (s->mb_x)
213  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
214  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
215  for (j = 0; j < 2; j++) {
216  v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
217  if (s->mb_x >= 2) {
218  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
219  }
220  }
221  }
222  v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
223  }
224 
225  if (s->mb_y == s->end_mb_y) {
226  if (s->mb_x) {
227  if (s->mb_x >= 2)
228  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
229  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
230  if (s->mb_x >= 2) {
231  for (j = 0; j < 2; j++) {
232  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
233  }
234  }
235  }
236 
237  if (s->mb_x == s->mb_width - 1) {
238  if (s->mb_x)
239  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
240  v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
241  if (s->mb_x) {
242  for (j = 0; j < 2; j++) {
243  v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
244  }
245  }
246  }
247  }
248  }
249 }
250 
252 {
253  MpegEncContext *s = &v->s;
254  int mb_pos;
255 
256  if (v->condover == CONDOVER_NONE)
257  return;
258 
259  mb_pos = s->mb_x + s->mb_y * s->mb_stride;
260 
261  /* Within a MB, the horizontal overlap always runs before the vertical.
262  * To accomplish that, we run the H on left and internal borders of the
263  * currently decoded MB. Then, we wait for the next overlap iteration
264  * to do H overlap on the right edge of this MB, before moving over and
265  * running the V overlap. Therefore, the V overlap makes us trail by one
266  * MB col and the H overlap filter makes us trail by one MB row. This
267  * is reflected in the time at which we run the put_pixels loop. */
268  if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
269  if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
270  v->over_flags_plane[mb_pos - 1])) {
272  v->block[v->cur_blk_idx][0]);
274  v->block[v->cur_blk_idx][2]);
275  if (!(s->flags & CODEC_FLAG_GRAY)) {
277  v->block[v->cur_blk_idx][4]);
279  v->block[v->cur_blk_idx][5]);
280  }
281  }
283  v->block[v->cur_blk_idx][1]);
285  v->block[v->cur_blk_idx][3]);
286 
287  if (s->mb_x == s->mb_width - 1) {
288  if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
289  v->over_flags_plane[mb_pos - s->mb_stride])) {
291  v->block[v->cur_blk_idx][0]);
293  v->block[v->cur_blk_idx][1]);
294  if (!(s->flags & CODEC_FLAG_GRAY)) {
296  v->block[v->cur_blk_idx][4]);
298  v->block[v->cur_blk_idx][5]);
299  }
300  }
302  v->block[v->cur_blk_idx][2]);
304  v->block[v->cur_blk_idx][3]);
305  }
306  }
307  if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
308  if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
309  v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
311  v->block[v->left_blk_idx][0]);
313  v->block[v->left_blk_idx][1]);
314  if (!(s->flags & CODEC_FLAG_GRAY)) {
316  v->block[v->left_blk_idx][4]);
318  v->block[v->left_blk_idx][5]);
319  }
320  }
322  v->block[v->left_blk_idx][2]);
324  v->block[v->left_blk_idx][3]);
325  }
326 }
327 
328 /** Do motion compensation over 1 macroblock
329  * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
330  */
331 static void vc1_mc_1mv(VC1Context *v, int dir)
332 {
333  MpegEncContext *s = &v->s;
334  DSPContext *dsp = &v->s.dsp;
335  uint8_t *srcY, *srcU, *srcV;
336  int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
337  int off, off_uv;
338  int v_edge_pos = s->v_edge_pos >> v->field_mode;
339 
340  if ((!v->field_mode ||
341  (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
342  !v->s.last_picture.f.data[0])
343  return;
344 
345  mx = s->mv[dir][0][0];
346  my = s->mv[dir][0][1];
347 
348  // store motion vectors for further use in B frames
349  if (s->pict_type == AV_PICTURE_TYPE_P) {
350  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
351  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
352  }
353 
354  uvmx = (mx + ((mx & 3) == 3)) >> 1;
355  uvmy = (my + ((my & 3) == 3)) >> 1;
356  v->luma_mv[s->mb_x][0] = uvmx;
357  v->luma_mv[s->mb_x][1] = uvmy;
358 
359  if (v->field_mode &&
360  v->cur_field_type != v->ref_field_type[dir]) {
361  my = my - 2 + 4 * v->cur_field_type;
362  uvmy = uvmy - 2 + 4 * v->cur_field_type;
363  }
364 
365  // fastuvmc shall be ignored for interlaced frame picture
366  if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
367  uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
368  uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
369  }
370  if (v->field_mode) { // interlaced field picture
371  if (!dir) {
372  if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
373  srcY = s->current_picture.f.data[0];
374  srcU = s->current_picture.f.data[1];
375  srcV = s->current_picture.f.data[2];
376  } else {
377  srcY = s->last_picture.f.data[0];
378  srcU = s->last_picture.f.data[1];
379  srcV = s->last_picture.f.data[2];
380  }
381  } else {
382  srcY = s->next_picture.f.data[0];
383  srcU = s->next_picture.f.data[1];
384  srcV = s->next_picture.f.data[2];
385  }
386  } else {
387  if (!dir) {
388  srcY = s->last_picture.f.data[0];
389  srcU = s->last_picture.f.data[1];
390  srcV = s->last_picture.f.data[2];
391  } else {
392  srcY = s->next_picture.f.data[0];
393  srcU = s->next_picture.f.data[1];
394  srcV = s->next_picture.f.data[2];
395  }
396  }
397 
398  if(!srcY)
399  return;
400 
401  src_x = s->mb_x * 16 + (mx >> 2);
402  src_y = s->mb_y * 16 + (my >> 2);
403  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
404  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
405 
406  if (v->profile != PROFILE_ADVANCED) {
407  src_x = av_clip( src_x, -16, s->mb_width * 16);
408  src_y = av_clip( src_y, -16, s->mb_height * 16);
409  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
410  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
411  } else {
412  src_x = av_clip( src_x, -17, s->avctx->coded_width);
413  src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
414  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
415  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
416  }
417 
418  srcY += src_y * s->linesize + src_x;
419  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
420  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
421 
422  if (v->field_mode && v->ref_field_type[dir]) {
423  srcY += s->current_picture_ptr->f.linesize[0];
424  srcU += s->current_picture_ptr->f.linesize[1];
425  srcV += s->current_picture_ptr->f.linesize[2];
426  }
427 
428  /* for grayscale we should not try to read from unknown area */
429  if (s->flags & CODEC_FLAG_GRAY) {
430  srcU = s->edge_emu_buffer + 18 * s->linesize;
431  srcV = s->edge_emu_buffer + 18 * s->linesize;
432  }
433 
435  || s->h_edge_pos < 22 || v_edge_pos < 22
436  || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
437  || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
438  uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
439 
440  srcY -= s->mspel * (1 + s->linesize);
442  17 + s->mspel * 2, 17 + s->mspel * 2,
443  src_x - s->mspel, src_y - s->mspel,
444  s->h_edge_pos, v_edge_pos);
445  srcY = s->edge_emu_buffer;
446  s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
447  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
448  s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
449  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
450  srcU = uvbuf;
451  srcV = uvbuf + 16;
452  /* if we deal with range reduction we need to scale source blocks */
453  if (v->rangeredfrm) {
454  int i, j;
455  uint8_t *src, *src2;
456 
457  src = srcY;
458  for (j = 0; j < 17 + s->mspel * 2; j++) {
459  for (i = 0; i < 17 + s->mspel * 2; i++)
460  src[i] = ((src[i] - 128) >> 1) + 128;
461  src += s->linesize;
462  }
463  src = srcU;
464  src2 = srcV;
465  for (j = 0; j < 9; j++) {
466  for (i = 0; i < 9; i++) {
467  src[i] = ((src[i] - 128) >> 1) + 128;
468  src2[i] = ((src2[i] - 128) >> 1) + 128;
469  }
470  src += s->uvlinesize;
471  src2 += s->uvlinesize;
472  }
473  }
474  /* if we deal with intensity compensation we need to scale source blocks */
475  if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
476  int i, j;
477  uint8_t *src, *src2;
478 
479  src = srcY;
480  for (j = 0; j < 17 + s->mspel * 2; j++) {
481  for (i = 0; i < 17 + s->mspel * 2; i++)
482  src[i] = v->luty[src[i]];
483  src += s->linesize;
484  }
485  src = srcU;
486  src2 = srcV;
487  for (j = 0; j < 9; j++) {
488  for (i = 0; i < 9; i++) {
489  src[i] = v->lutuv[src[i]];
490  src2[i] = v->lutuv[src2[i]];
491  }
492  src += s->uvlinesize;
493  src2 += s->uvlinesize;
494  }
495  }
496  srcY += s->mspel * (1 + s->linesize);
497  }
498 
499  if (v->field_mode && v->second_field) {
500  off = s->current_picture_ptr->f.linesize[0];
501  off_uv = s->current_picture_ptr->f.linesize[1];
502  } else {
503  off = 0;
504  off_uv = 0;
505  }
506  if (s->mspel) {
507  dxy = ((my & 3) << 2) | (mx & 3);
508  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
509  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
510  srcY += s->linesize * 8;
511  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
512  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
513  } else { // hpel mc - always used for luma
514  dxy = (my & 2) | ((mx & 2) >> 1);
515  if (!v->rnd)
516  dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
517  else
518  dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
519  }
520 
521  if (s->flags & CODEC_FLAG_GRAY) return;
522  /* Chroma MC always uses qpel bilinear */
523  uvmx = (uvmx & 3) << 1;
524  uvmy = (uvmy & 3) << 1;
525  if (!v->rnd) {
526  dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
527  dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
528  } else {
529  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
530  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
531  }
532 }
533 
534 static inline int median4(int a, int b, int c, int d)
535 {
536  if (a < b) {
537  if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
538  else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
539  } else {
540  if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
541  else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
542  }
543 }
544 
545 /** Do motion compensation for 4-MV macroblock - luminance block
546  */
547 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
548 {
549  MpegEncContext *s = &v->s;
550  DSPContext *dsp = &v->s.dsp;
551  uint8_t *srcY;
552  int dxy, mx, my, src_x, src_y;
553  int off;
554  int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
555  int v_edge_pos = s->v_edge_pos >> v->field_mode;
556 
557  if ((!v->field_mode ||
558  (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
559  !v->s.last_picture.f.data[0])
560  return;
561 
562  mx = s->mv[dir][n][0];
563  my = s->mv[dir][n][1];
564 
565  if (!dir) {
566  if (v->field_mode) {
567  if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
568  srcY = s->current_picture.f.data[0];
569  else
570  srcY = s->last_picture.f.data[0];
571  } else
572  srcY = s->last_picture.f.data[0];
573  } else
574  srcY = s->next_picture.f.data[0];
575 
576  if(!srcY)
577  return;
578 
579  if (v->field_mode) {
580  if (v->cur_field_type != v->ref_field_type[dir])
581  my = my - 2 + 4 * v->cur_field_type;
582  }
583 
584  if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
585  int same_count = 0, opp_count = 0, k;
586  int chosen_mv[2][4][2], f;
587  int tx, ty;
588  for (k = 0; k < 4; k++) {
589  f = v->mv_f[0][s->block_index[k] + v->blocks_off];
590  chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
591  chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
592  opp_count += f;
593  same_count += 1 - f;
594  }
595  f = opp_count > same_count;
596  switch (f ? opp_count : same_count) {
597  case 4:
598  tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
599  chosen_mv[f][2][0], chosen_mv[f][3][0]);
600  ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
601  chosen_mv[f][2][1], chosen_mv[f][3][1]);
602  break;
603  case 3:
604  tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
605  ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
606  break;
607  case 2:
608  tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
609  ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
610  break;
611  default:
612  av_assert2(0);
613  }
614  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
615  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
616  for (k = 0; k < 4; k++)
617  v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
618  }
619 
620  if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
621  int qx, qy;
622  int width = s->avctx->coded_width;
623  int height = s->avctx->coded_height >> 1;
624  qx = (s->mb_x * 16) + (mx >> 2);
625  qy = (s->mb_y * 8) + (my >> 3);
626 
627  if (qx < -17)
628  mx -= 4 * (qx + 17);
629  else if (qx > width)
630  mx -= 4 * (qx - width);
631  if (qy < -18)
632  my -= 8 * (qy + 18);
633  else if (qy > height + 1)
634  my -= 8 * (qy - height - 1);
635  }
636 
637  if ((v->fcm == ILACE_FRAME) && fieldmv)
638  off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
639  else
640  off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
641  if (v->field_mode && v->second_field)
642  off += s->current_picture_ptr->f.linesize[0];
643 
644  src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
645  if (!fieldmv)
646  src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
647  else
648  src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
649 
650  if (v->profile != PROFILE_ADVANCED) {
651  src_x = av_clip(src_x, -16, s->mb_width * 16);
652  src_y = av_clip(src_y, -16, s->mb_height * 16);
653  } else {
654  src_x = av_clip(src_x, -17, s->avctx->coded_width);
655  if (v->fcm == ILACE_FRAME) {
656  if (src_y & 1)
657  src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
658  else
659  src_y = av_clip(src_y, -18, s->avctx->coded_height);
660  } else {
661  src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
662  }
663  }
664 
665  srcY += src_y * s->linesize + src_x;
666  if (v->field_mode && v->ref_field_type[dir])
667  srcY += s->current_picture_ptr->f.linesize[0];
668 
669  if (fieldmv && !(src_y & 1))
670  v_edge_pos--;
671  if (fieldmv && (src_y & 1) && src_y < 4)
672  src_y--;
674  || s->h_edge_pos < 13 || v_edge_pos < 23
675  || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
676  || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
677  srcY -= s->mspel * (1 + (s->linesize << fieldmv));
678  /* check emulate edge stride and offset */
680  9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
681  src_x - s->mspel, src_y - (s->mspel << fieldmv),
682  s->h_edge_pos, v_edge_pos);
683  srcY = s->edge_emu_buffer;
684  /* if we deal with range reduction we need to scale source blocks */
685  if (v->rangeredfrm) {
686  int i, j;
687  uint8_t *src;
688 
689  src = srcY;
690  for (j = 0; j < 9 + s->mspel * 2; j++) {
691  for (i = 0; i < 9 + s->mspel * 2; i++)
692  src[i] = ((src[i] - 128) >> 1) + 128;
693  src += s->linesize << fieldmv;
694  }
695  }
696  /* if we deal with intensity compensation we need to scale source blocks */
697  if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
698  int i, j;
699  uint8_t *src;
700 
701  src = srcY;
702  for (j = 0; j < 9 + s->mspel * 2; j++) {
703  for (i = 0; i < 9 + s->mspel * 2; i++)
704  src[i] = v->luty[src[i]];
705  src += s->linesize << fieldmv;
706  }
707  }
708  srcY += s->mspel * (1 + (s->linesize << fieldmv));
709  }
710 
711  if (s->mspel) {
712  dxy = ((my & 3) << 2) | (mx & 3);
713  v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
714  } else { // hpel mc - always used for luma
715  dxy = (my & 2) | ((mx & 2) >> 1);
716  if (!v->rnd)
717  dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
718  else
719  dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
720  }
721 }
722 
723 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
724 {
725  int idx, i;
726  static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
727 
728  idx = ((a[3] != flag) << 3)
729  | ((a[2] != flag) << 2)
730  | ((a[1] != flag) << 1)
731  | (a[0] != flag);
732  if (!idx) {
733  *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
734  *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
735  return 4;
736  } else if (count[idx] == 1) {
737  switch (idx) {
738  case 0x1:
739  *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
740  *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
741  return 3;
742  case 0x2:
743  *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
744  *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
745  return 3;
746  case 0x4:
747  *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
748  *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
749  return 3;
750  case 0x8:
751  *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
752  *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
753  return 3;
754  }
755  } else if (count[idx] == 2) {
756  int t1 = 0, t2 = 0;
757  for (i = 0; i < 3; i++)
758  if (!a[i]) {
759  t1 = i;
760  break;
761  }
762  for (i = t1 + 1; i < 4; i++)
763  if (!a[i]) {
764  t2 = i;
765  break;
766  }
767  *tx = (mvx[t1] + mvx[t2]) / 2;
768  *ty = (mvy[t1] + mvy[t2]) / 2;
769  return 2;
770  } else {
771  return 0;
772  }
773  return -1;
774 }
775 
776 /** Do motion compensation for 4-MV macroblock - both chroma blocks
777  */
778 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
779 {
780  MpegEncContext *s = &v->s;
781  DSPContext *dsp = &v->s.dsp;
782  uint8_t *srcU, *srcV;
783  int uvmx, uvmy, uvsrc_x, uvsrc_y;
784  int k, tx = 0, ty = 0;
785  int mvx[4], mvy[4], intra[4], mv_f[4];
786  int valid_count;
787  int chroma_ref_type = v->cur_field_type, off = 0;
788  int v_edge_pos = s->v_edge_pos >> v->field_mode;
789 
790  if (!v->field_mode && !v->s.last_picture.f.data[0])
791  return;
792  if (s->flags & CODEC_FLAG_GRAY)
793  return;
794 
795  for (k = 0; k < 4; k++) {
796  mvx[k] = s->mv[dir][k][0];
797  mvy[k] = s->mv[dir][k][1];
798  intra[k] = v->mb_type[0][s->block_index[k]];
799  if (v->field_mode)
800  mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
801  }
802 
803  /* calculate chroma MV vector from four luma MVs */
804  if (!v->field_mode || (v->field_mode && !v->numref)) {
805  valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
806  chroma_ref_type = v->reffield;
807  if (!valid_count) {
808  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
809  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
810  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
811  return; //no need to do MC for intra blocks
812  }
813  } else {
814  int dominant = 0;
815  if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
816  dominant = 1;
817  valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
818  if (dominant)
819  chroma_ref_type = !v->cur_field_type;
820  }
821  if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
822  return;
823  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
824  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
825  uvmx = (tx + ((tx & 3) == 3)) >> 1;
826  uvmy = (ty + ((ty & 3) == 3)) >> 1;
827 
828  v->luma_mv[s->mb_x][0] = uvmx;
829  v->luma_mv[s->mb_x][1] = uvmy;
830 
831  if (v->fastuvmc) {
832  uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
833  uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
834  }
835  // Field conversion bias
836  if (v->cur_field_type != chroma_ref_type)
837  uvmy += 2 - 4 * chroma_ref_type;
838 
839  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
840  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
841 
842  if (v->profile != PROFILE_ADVANCED) {
843  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
844  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
845  } else {
846  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
847  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
848  }
849 
850  if (!dir) {
851  if (v->field_mode) {
852  if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
853  srcU = s->current_picture.f.data[1];
854  srcV = s->current_picture.f.data[2];
855  } else {
856  srcU = s->last_picture.f.data[1];
857  srcV = s->last_picture.f.data[2];
858  }
859  } else {
860  srcU = s->last_picture.f.data[1];
861  srcV = s->last_picture.f.data[2];
862  }
863  } else {
864  srcU = s->next_picture.f.data[1];
865  srcV = s->next_picture.f.data[2];
866  }
867 
868  if(!srcU)
869  return;
870 
871  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
872  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
873 
874  if (v->field_mode) {
875  if (chroma_ref_type) {
876  srcU += s->current_picture_ptr->f.linesize[1];
877  srcV += s->current_picture_ptr->f.linesize[2];
878  }
879  off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
880  }
881 
883  || s->h_edge_pos < 18 || v_edge_pos < 18
884  || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
885  || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
887  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
888  s->h_edge_pos >> 1, v_edge_pos >> 1);
889  s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
890  8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
891  s->h_edge_pos >> 1, v_edge_pos >> 1);
892  srcU = s->edge_emu_buffer;
893  srcV = s->edge_emu_buffer + 16;
894 
895  /* if we deal with range reduction we need to scale source blocks */
896  if (v->rangeredfrm) {
897  int i, j;
898  uint8_t *src, *src2;
899 
900  src = srcU;
901  src2 = srcV;
902  for (j = 0; j < 9; j++) {
903  for (i = 0; i < 9; i++) {
904  src[i] = ((src[i] - 128) >> 1) + 128;
905  src2[i] = ((src2[i] - 128) >> 1) + 128;
906  }
907  src += s->uvlinesize;
908  src2 += s->uvlinesize;
909  }
910  }
911  /* if we deal with intensity compensation we need to scale source blocks */
912  if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
913  int i, j;
914  uint8_t *src, *src2;
915 
916  src = srcU;
917  src2 = srcV;
918  for (j = 0; j < 9; j++) {
919  for (i = 0; i < 9; i++) {
920  src[i] = v->lutuv[src[i]];
921  src2[i] = v->lutuv[src2[i]];
922  }
923  src += s->uvlinesize;
924  src2 += s->uvlinesize;
925  }
926  }
927  }
928 
929  /* Chroma MC always uses qpel bilinear */
930  uvmx = (uvmx & 3) << 1;
931  uvmy = (uvmy & 3) << 1;
932  if (!v->rnd) {
933  dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
934  dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
935  } else {
936  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
937  v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
938  }
939 }
940 
941 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
942  */
944 {
945  MpegEncContext *s = &v->s;
946  DSPContext *dsp = &v->s.dsp;
947  uint8_t *srcU, *srcV;
948  int uvsrc_x, uvsrc_y;
949  int uvmx_field[4], uvmy_field[4];
950  int i, off, tx, ty;
951  int fieldmv = v->blk_mv_type[s->block_index[0]];
952  static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
953  int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
954  int v_edge_pos = s->v_edge_pos >> 1;
955 
956  if (!v->s.last_picture.f.data[0])
957  return;
958  if (s->flags & CODEC_FLAG_GRAY)
959  return;
960 
961  for (i = 0; i < 4; i++) {
962  tx = s->mv[0][i][0];
963  uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
964  ty = s->mv[0][i][1];
965  if (fieldmv)
966  uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
967  else
968  uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
969  }
970 
971  for (i = 0; i < 4; i++) {
972  off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
973  uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
974  uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
975  // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
976  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
977  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
978  srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
979  srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
980  uvmx_field[i] = (uvmx_field[i] & 3) << 1;
981  uvmy_field[i] = (uvmy_field[i] & 3) << 1;
982 
983  if (fieldmv && !(uvsrc_y & 1))
984  v_edge_pos = (s->v_edge_pos >> 1) - 1;
985 
986  if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
987  uvsrc_y--;
988  if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
989  || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
990  || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
991  || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
993  5, (5 << fieldmv), uvsrc_x, uvsrc_y,
994  s->h_edge_pos >> 1, v_edge_pos);
995  s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
996  5, (5 << fieldmv), uvsrc_x, uvsrc_y,
997  s->h_edge_pos >> 1, v_edge_pos);
998  srcU = s->edge_emu_buffer;
999  srcV = s->edge_emu_buffer + 16;
1000 
1001  /* if we deal with intensity compensation we need to scale source blocks */
1002  if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1003  int i, j;
1004  uint8_t *src, *src2;
1005 
1006  src = srcU;
1007  src2 = srcV;
1008  for (j = 0; j < 5; j++) {
1009  for (i = 0; i < 5; i++) {
1010  src[i] = v->lutuv[src[i]];
1011  src2[i] = v->lutuv[src2[i]];
1012  }
1013  src += s->uvlinesize << 1;
1014  src2 += s->uvlinesize << 1;
1015  }
1016  }
1017  }
1018  if (!v->rnd) {
1019  dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1020  dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1021  } else {
1022  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]);
1023  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]);
1024  }
1025  }
1026 }
1027 
1028 /***********************************************************************/
1029 /**
1030  * @name VC-1 Block-level functions
1031  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1032  * @{
1033  */
1034 
1035 /**
1036  * @def GET_MQUANT
1037  * @brief Get macroblock-level quantizer scale
1038  */
1039 #define GET_MQUANT() \
1040  if (v->dquantfrm) { \
1041  int edges = 0; \
1042  if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1043  if (v->dqbilevel) { \
1044  mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1045  } else { \
1046  mqdiff = get_bits(gb, 3); \
1047  if (mqdiff != 7) \
1048  mquant = v->pq + mqdiff; \
1049  else \
1050  mquant = get_bits(gb, 5); \
1051  } \
1052  } \
1053  if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1054  edges = 1 << v->dqsbedge; \
1055  else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1056  edges = (3 << v->dqsbedge) % 15; \
1057  else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1058  edges = 15; \
1059  if ((edges&1) && !s->mb_x) \
1060  mquant = v->altpq; \
1061  if ((edges&2) && s->first_slice_line) \
1062  mquant = v->altpq; \
1063  if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1064  mquant = v->altpq; \
1065  if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1066  mquant = v->altpq; \
1067  if (!mquant || mquant > 31) { \
1068  av_log(v->s.avctx, AV_LOG_ERROR, \
1069  "Overriding invalid mquant %d\n", mquant); \
1070  mquant = 1; \
1071  } \
1072  }
1073 
1074 /**
1075  * @def GET_MVDATA(_dmv_x, _dmv_y)
1076  * @brief Get MV differentials
1077  * @see MVDATA decoding from 8.3.5.2, p(1)20
1078  * @param _dmv_x Horizontal differential for decoded MV
1079  * @param _dmv_y Vertical differential for decoded MV
1080  */
1081 #define GET_MVDATA(_dmv_x, _dmv_y) \
1082  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1083  VC1_MV_DIFF_VLC_BITS, 2); \
1084  if (index > 36) { \
1085  mb_has_coeffs = 1; \
1086  index -= 37; \
1087  } else \
1088  mb_has_coeffs = 0; \
1089  s->mb_intra = 0; \
1090  if (!index) { \
1091  _dmv_x = _dmv_y = 0; \
1092  } else if (index == 35) { \
1093  _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1094  _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1095  } else if (index == 36) { \
1096  _dmv_x = 0; \
1097  _dmv_y = 0; \
1098  s->mb_intra = 1; \
1099  } else { \
1100  index1 = index % 6; \
1101  if (!s->quarter_sample && index1 == 5) val = 1; \
1102  else val = 0; \
1103  if (size_table[index1] - val > 0) \
1104  val = get_bits(gb, size_table[index1] - val); \
1105  else val = 0; \
1106  sign = 0 - (val&1); \
1107  _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1108  \
1109  index1 = index / 6; \
1110  if (!s->quarter_sample && index1 == 5) val = 1; \
1111  else val = 0; \
1112  if (size_table[index1] - val > 0) \
1113  val = get_bits(gb, size_table[index1] - val); \
1114  else val = 0; \
1115  sign = 0 - (val & 1); \
1116  _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1117  }
1118 
1120  int *dmv_y, int *pred_flag)
1121 {
1122  int index, index1;
1123  int extend_x = 0, extend_y = 0;
1124  GetBitContext *gb = &v->s.gb;
1125  int bits, esc;
1126  int val, sign;
1127  const int* offs_tab;
1128 
1129  if (v->numref) {
1130  bits = VC1_2REF_MVDATA_VLC_BITS;
1131  esc = 125;
1132  } else {
1133  bits = VC1_1REF_MVDATA_VLC_BITS;
1134  esc = 71;
1135  }
1136  switch (v->dmvrange) {
1137  case 1:
1138  extend_x = 1;
1139  break;
1140  case 2:
1141  extend_y = 1;
1142  break;
1143  case 3:
1144  extend_x = extend_y = 1;
1145  break;
1146  }
1147  index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1148  if (index == esc) {
1149  *dmv_x = get_bits(gb, v->k_x);
1150  *dmv_y = get_bits(gb, v->k_y);
1151  if (v->numref) {
1152  if (pred_flag) {
1153  *pred_flag = *dmv_y & 1;
1154  *dmv_y = (*dmv_y + *pred_flag) >> 1;
1155  } else {
1156  *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1157  }
1158  }
1159  }
1160  else {
1161  av_assert0(index < esc);
1162  if (extend_x)
1163  offs_tab = offset_table2;
1164  else
1165  offs_tab = offset_table1;
1166  index1 = (index + 1) % 9;
1167  if (index1 != 0) {
1168  val = get_bits(gb, index1 + extend_x);
1169  sign = 0 -(val & 1);
1170  *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1171  } else
1172  *dmv_x = 0;
1173  if (extend_y)
1174  offs_tab = offset_table2;
1175  else
1176  offs_tab = offset_table1;
1177  index1 = (index + 1) / 9;
1178  if (index1 > v->numref) {
1179  val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1180  sign = 0 - (val & 1);
1181  *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1182  } else
1183  *dmv_y = 0;
1184  if (v->numref && pred_flag)
1185  *pred_flag = index1 & 1;
1186  }
1187 }
1188 
1189 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1190 {
1191  int scaledvalue, refdist;
1192  int scalesame1, scalesame2;
1193  int scalezone1_x, zone1offset_x;
1194  int table_index = dir ^ v->second_field;
1195 
1196  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1197  refdist = v->refdist;
1198  else
1199  refdist = dir ? v->brfd : v->frfd;
1200  if (refdist > 3)
1201  refdist = 3;
1202  scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1203  scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1204  scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1205  zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1206 
1207  if (FFABS(n) > 255)
1208  scaledvalue = n;
1209  else {
1210  if (FFABS(n) < scalezone1_x)
1211  scaledvalue = (n * scalesame1) >> 8;
1212  else {
1213  if (n < 0)
1214  scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1215  else
1216  scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1217  }
1218  }
1219  return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1220 }
1221 
1222 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1223 {
1224  int scaledvalue, refdist;
1225  int scalesame1, scalesame2;
1226  int scalezone1_y, zone1offset_y;
1227  int table_index = dir ^ v->second_field;
1228 
1229  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1230  refdist = v->refdist;
1231  else
1232  refdist = dir ? v->brfd : v->frfd;
1233  if (refdist > 3)
1234  refdist = 3;
1235  scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1236  scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1237  scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1238  zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1239 
1240  if (FFABS(n) > 63)
1241  scaledvalue = n;
1242  else {
1243  if (FFABS(n) < scalezone1_y)
1244  scaledvalue = (n * scalesame1) >> 8;
1245  else {
1246  if (n < 0)
1247  scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1248  else
1249  scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1250  }
1251  }
1252 
1253  if (v->cur_field_type && !v->ref_field_type[dir])
1254  return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1255  else
1256  return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1257 }
1258 
1259 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1260 {
1261  int scalezone1_x, zone1offset_x;
1262  int scaleopp1, scaleopp2, brfd;
1263  int scaledvalue;
1264 
1265  brfd = FFMIN(v->brfd, 3);
1266  scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1267  zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1268  scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1269  scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1270 
1271  if (FFABS(n) > 255)
1272  scaledvalue = n;
1273  else {
1274  if (FFABS(n) < scalezone1_x)
1275  scaledvalue = (n * scaleopp1) >> 8;
1276  else {
1277  if (n < 0)
1278  scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1279  else
1280  scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1281  }
1282  }
1283  return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1284 }
1285 
1286 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1287 {
1288  int scalezone1_y, zone1offset_y;
1289  int scaleopp1, scaleopp2, brfd;
1290  int scaledvalue;
1291 
1292  brfd = FFMIN(v->brfd, 3);
1293  scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1294  zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1295  scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1296  scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1297 
1298  if (FFABS(n) > 63)
1299  scaledvalue = n;
1300  else {
1301  if (FFABS(n) < scalezone1_y)
1302  scaledvalue = (n * scaleopp1) >> 8;
1303  else {
1304  if (n < 0)
1305  scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1306  else
1307  scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1308  }
1309  }
1310  if (v->cur_field_type && !v->ref_field_type[dir]) {
1311  return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1312  } else {
1313  return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1314  }
1315 }
1316 
1317 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1318  int dim, int dir)
1319 {
1320  int brfd, scalesame;
1321  int hpel = 1 - v->s.quarter_sample;
1322 
1323  n >>= hpel;
1324  if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1325  if (dim)
1326  n = scaleforsame_y(v, i, n, dir) << hpel;
1327  else
1328  n = scaleforsame_x(v, n, dir) << hpel;
1329  return n;
1330  }
1331  brfd = FFMIN(v->brfd, 3);
1332  scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1333 
1334  n = (n * scalesame >> 8) << hpel;
1335  return n;
1336 }
1337 
1338 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1339  int dim, int dir)
1340 {
1341  int refdist, scaleopp;
1342  int hpel = 1 - v->s.quarter_sample;
1343 
1344  n >>= hpel;
1345  if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1346  if (dim)
1347  n = scaleforopp_y(v, n, dir) << hpel;
1348  else
1349  n = scaleforopp_x(v, n) << hpel;
1350  return n;
1351  }
1352  if (v->s.pict_type != AV_PICTURE_TYPE_B)
1353  refdist = FFMIN(v->refdist, 3);
1354  else
1355  refdist = dir ? v->brfd : v->frfd;
1356  scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1357 
1358  n = (n * scaleopp >> 8) << hpel;
1359  return n;
1360 }
1361 
1362 /** Predict and set motion vector
1363  */
1364 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1365  int mv1, int r_x, int r_y, uint8_t* is_intra,
1366  int pred_flag, int dir)
1367 {
1368  MpegEncContext *s = &v->s;
1369  int xy, wrap, off = 0;
1370  int16_t *A, *B, *C;
1371  int px, py;
1372  int sum;
1373  int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1374  int opposite, a_f, b_f, c_f;
1375  int16_t field_predA[2];
1376  int16_t field_predB[2];
1377  int16_t field_predC[2];
1378  int a_valid, b_valid, c_valid;
1379  int hybridmv_thresh, y_bias = 0;
1380 
1381  if (v->mv_mode == MV_PMODE_MIXED_MV ||
1383  mixedmv_pic = 1;
1384  else
1385  mixedmv_pic = 0;
1386  /* scale MV difference to be quad-pel */
1387  dmv_x <<= 1 - s->quarter_sample;
1388  dmv_y <<= 1 - s->quarter_sample;
1389 
1390  wrap = s->b8_stride;
1391  xy = s->block_index[n];
1392 
1393  if (s->mb_intra) {
1394  s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1395  s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1396  s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1397  s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1398  if (mv1) { /* duplicate motion data for 1-MV block */
1399  s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1400  s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1401  s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1402  s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1403  s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1404  s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1405  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1406  s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1407  s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1408  s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1409  s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1410  s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1411  s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1412  }
1413  return;
1414  }
1415 
1416  C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1417  A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1418  if (mv1) {
1419  if (v->field_mode && mixedmv_pic)
1420  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1421  else
1422  off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1423  } else {
1424  //in 4-MV mode different blocks have different B predictor position
1425  switch (n) {
1426  case 0:
1427  off = (s->mb_x > 0) ? -1 : 1;
1428  break;
1429  case 1:
1430  off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1431  break;
1432  case 2:
1433  off = 1;
1434  break;
1435  case 3:
1436  off = -1;
1437  }
1438  }
1439  B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1440 
1441  a_valid = !s->first_slice_line || (n == 2 || n == 3);
1442  b_valid = a_valid && (s->mb_width > 1);
1443  c_valid = s->mb_x || (n == 1 || n == 3);
1444  if (v->field_mode) {
1445  a_valid = a_valid && !is_intra[xy - wrap];
1446  b_valid = b_valid && !is_intra[xy - wrap + off];
1447  c_valid = c_valid && !is_intra[xy - 1];
1448  }
1449 
1450  if (a_valid) {
1451  a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1452  num_oppfield += a_f;
1453  num_samefield += 1 - a_f;
1454  field_predA[0] = A[0];
1455  field_predA[1] = A[1];
1456  } else {
1457  field_predA[0] = field_predA[1] = 0;
1458  a_f = 0;
1459  }
1460  if (b_valid) {
1461  b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1462  num_oppfield += b_f;
1463  num_samefield += 1 - b_f;
1464  field_predB[0] = B[0];
1465  field_predB[1] = B[1];
1466  } else {
1467  field_predB[0] = field_predB[1] = 0;
1468  b_f = 0;
1469  }
1470  if (c_valid) {
1471  c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1472  num_oppfield += c_f;
1473  num_samefield += 1 - c_f;
1474  field_predC[0] = C[0];
1475  field_predC[1] = C[1];
1476  } else {
1477  field_predC[0] = field_predC[1] = 0;
1478  c_f = 0;
1479  }
1480 
1481  if (v->field_mode) {
1482  if (!v->numref)
1483  // REFFIELD determines if the last field or the second-last field is
1484  // to be used as reference
1485  opposite = 1 - v->reffield;
1486  else {
1487  if (num_samefield <= num_oppfield)
1488  opposite = 1 - pred_flag;
1489  else
1490  opposite = pred_flag;
1491  }
1492  } else
1493  opposite = 0;
1494  if (opposite) {
1495  if (a_valid && !a_f) {
1496  field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1497  field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1498  }
1499  if (b_valid && !b_f) {
1500  field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1501  field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1502  }
1503  if (c_valid && !c_f) {
1504  field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1505  field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1506  }
1507  v->mv_f[dir][xy + v->blocks_off] = 1;
1508  v->ref_field_type[dir] = !v->cur_field_type;
1509  } else {
1510  if (a_valid && a_f) {
1511  field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1512  field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1513  }
1514  if (b_valid && b_f) {
1515  field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1516  field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1517  }
1518  if (c_valid && c_f) {
1519  field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1520  field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1521  }
1522  v->mv_f[dir][xy + v->blocks_off] = 0;
1523  v->ref_field_type[dir] = v->cur_field_type;
1524  }
1525 
1526  if (a_valid) {
1527  px = field_predA[0];
1528  py = field_predA[1];
1529  } else if (c_valid) {
1530  px = field_predC[0];
1531  py = field_predC[1];
1532  } else if (b_valid) {
1533  px = field_predB[0];
1534  py = field_predB[1];
1535  } else {
1536  px = 0;
1537  py = 0;
1538  }
1539 
1540  if (num_samefield + num_oppfield > 1) {
1541  px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1542  py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1543  }
1544 
1545  /* Pullback MV as specified in 8.3.5.3.4 */
1546  if (!v->field_mode) {
1547  int qx, qy, X, Y;
1548  qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1549  qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1550  X = (s->mb_width << 6) - 4;
1551  Y = (s->mb_height << 6) - 4;
1552  if (mv1) {
1553  if (qx + px < -60) px = -60 - qx;
1554  if (qy + py < -60) py = -60 - qy;
1555  } else {
1556  if (qx + px < -28) px = -28 - qx;
1557  if (qy + py < -28) py = -28 - qy;
1558  }
1559  if (qx + px > X) px = X - qx;
1560  if (qy + py > Y) py = Y - qy;
1561  }
1562 
1563  if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1564  /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1565  hybridmv_thresh = 32;
1566  if (a_valid && c_valid) {
1567  if (is_intra[xy - wrap])
1568  sum = FFABS(px) + FFABS(py);
1569  else
1570  sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1571  if (sum > hybridmv_thresh) {
1572  if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1573  px = field_predA[0];
1574  py = field_predA[1];
1575  } else {
1576  px = field_predC[0];
1577  py = field_predC[1];
1578  }
1579  } else {
1580  if (is_intra[xy - 1])
1581  sum = FFABS(px) + FFABS(py);
1582  else
1583  sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1584  if (sum > hybridmv_thresh) {
1585  if (get_bits1(&s->gb)) {
1586  px = field_predA[0];
1587  py = field_predA[1];
1588  } else {
1589  px = field_predC[0];
1590  py = field_predC[1];
1591  }
1592  }
1593  }
1594  }
1595  }
1596 
1597  if (v->field_mode && v->numref)
1598  r_y >>= 1;
1599  if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1600  y_bias = 1;
1601  /* store MV using signed modulus of MV range defined in 4.11 */
1602  s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1603  s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1604  if (mv1) { /* duplicate motion data for 1-MV block */
1605  s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1606  s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1607  s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1608  s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1609  s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1610  s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1611  v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1612  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];
1613  }
1614 }
1615 
1616 /** Predict and set motion vector for interlaced frame picture MBs
1617  */
1618 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1619  int mvn, int r_x, int r_y, uint8_t* is_intra)
1620 {
1621  MpegEncContext *s = &v->s;
1622  int xy, wrap, off = 0;
1623  int A[2], B[2], C[2];
1624  int px, py;
1625  int a_valid = 0, b_valid = 0, c_valid = 0;
1626  int field_a, field_b, field_c; // 0: same, 1: opposit
1627  int total_valid, num_samefield, num_oppfield;
1628  int pos_c, pos_b, n_adj;
1629 
1630  wrap = s->b8_stride;
1631  xy = s->block_index[n];
1632 
1633  if (s->mb_intra) {
1634  s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1635  s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1636  s->current_picture.f.motion_val[1][xy][0] = 0;
1637  s->current_picture.f.motion_val[1][xy][1] = 0;
1638  if (mvn == 1) { /* duplicate motion data for 1-MV block */
1639  s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1640  s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1641  s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1642  s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1643  s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1644  s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1645  v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1646  s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1647  s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1648  s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1649  s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1650  s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1651  s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1652  }
1653  return;
1654  }
1655 
1656  off = ((n == 0) || (n == 1)) ? 1 : -1;
1657  /* predict A */
1658  if (s->mb_x || (n == 1) || (n == 3)) {
1659  if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1660  || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1661  A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1662  A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1663  a_valid = 1;
1664  } else { // current block has frame mv and cand. has field MV (so average)
1665  A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1666  + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1667  A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1668  + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1669  a_valid = 1;
1670  }
1671  if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1672  a_valid = 0;
1673  A[0] = A[1] = 0;
1674  }
1675  } else
1676  A[0] = A[1] = 0;
1677  /* Predict B and C */
1678  B[0] = B[1] = C[0] = C[1] = 0;
1679  if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1680  if (!s->first_slice_line) {
1681  if (!v->is_intra[s->mb_x - s->mb_stride]) {
1682  b_valid = 1;
1683  n_adj = n | 2;
1684  pos_b = s->block_index[n_adj] - 2 * wrap;
1685  if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1686  n_adj = (n & 2) | (n & 1);
1687  }
1688  B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1689  B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1690  if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1691  B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1692  B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1693  }
1694  }
1695  if (s->mb_width > 1) {
1696  if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1697  c_valid = 1;
1698  n_adj = 2;
1699  pos_c = s->block_index[2] - 2 * wrap + 2;
1700  if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1701  n_adj = n & 2;
1702  }
1703  C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1704  C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1705  if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1706  C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1707  C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1708  }
1709  if (s->mb_x == s->mb_width - 1) {
1710  if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1711  c_valid = 1;
1712  n_adj = 3;
1713  pos_c = s->block_index[3] - 2 * wrap - 2;
1714  if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1715  n_adj = n | 1;
1716  }
1717  C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1718  C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1719  if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1720  C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1721  C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1722  }
1723  } else
1724  c_valid = 0;
1725  }
1726  }
1727  }
1728  }
1729  } else {
1730  pos_b = s->block_index[1];
1731  b_valid = 1;
1732  B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1733  B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1734  pos_c = s->block_index[0];
1735  c_valid = 1;
1736  C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1737  C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1738  }
1739 
1740  total_valid = a_valid + b_valid + c_valid;
1741  // check if predictor A is out of bounds
1742  if (!s->mb_x && !(n == 1 || n == 3)) {
1743  A[0] = A[1] = 0;
1744  }
1745  // check if predictor B is out of bounds
1746  if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1747  B[0] = B[1] = C[0] = C[1] = 0;
1748  }
1749  if (!v->blk_mv_type[xy]) {
1750  if (s->mb_width == 1) {
1751  px = B[0];
1752  py = B[1];
1753  } else {
1754  if (total_valid >= 2) {
1755  px = mid_pred(A[0], B[0], C[0]);
1756  py = mid_pred(A[1], B[1], C[1]);
1757  } else if (total_valid) {
1758  if (a_valid) { px = A[0]; py = A[1]; }
1759  if (b_valid) { px = B[0]; py = B[1]; }
1760  if (c_valid) { px = C[0]; py = C[1]; }
1761  } else
1762  px = py = 0;
1763  }
1764  } else {
1765  if (a_valid)
1766  field_a = (A[1] & 4) ? 1 : 0;
1767  else
1768  field_a = 0;
1769  if (b_valid)
1770  field_b = (B[1] & 4) ? 1 : 0;
1771  else
1772  field_b = 0;
1773  if (c_valid)
1774  field_c = (C[1] & 4) ? 1 : 0;
1775  else
1776  field_c = 0;
1777 
1778  num_oppfield = field_a + field_b + field_c;
1779  num_samefield = total_valid - num_oppfield;
1780  if (total_valid == 3) {
1781  if ((num_samefield == 3) || (num_oppfield == 3)) {
1782  px = mid_pred(A[0], B[0], C[0]);
1783  py = mid_pred(A[1], B[1], C[1]);
1784  } else if (num_samefield >= num_oppfield) {
1785  /* take one MV from same field set depending on priority
1786  the check for B may not be necessary */
1787  px = !field_a ? A[0] : B[0];
1788  py = !field_a ? A[1] : B[1];
1789  } else {
1790  px = field_a ? A[0] : B[0];
1791  py = field_a ? A[1] : B[1];
1792  }
1793  } else if (total_valid == 2) {
1794  if (num_samefield >= num_oppfield) {
1795  if (!field_a && a_valid) {
1796  px = A[0];
1797  py = A[1];
1798  } else if (!field_b && b_valid) {
1799  px = B[0];
1800  py = B[1];
1801  } else if (c_valid) {
1802  px = C[0];
1803  py = C[1];
1804  } else px = py = 0;
1805  } else {
1806  if (field_a && a_valid) {
1807  px = A[0];
1808  py = A[1];
1809  } else if (field_b && b_valid) {
1810  px = B[0];
1811  py = B[1];
1812  } else if (c_valid) {
1813  px = C[0];
1814  py = C[1];
1815  } else px = py = 0;
1816  }
1817  } else if (total_valid == 1) {
1818  px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1819  py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1820  } else
1821  px = py = 0;
1822  }
1823 
1824  /* store MV using signed modulus of MV range defined in 4.11 */
1825  s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1826  s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1827  if (mvn == 1) { /* duplicate motion data for 1-MV block */
1828  s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1829  s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1830  s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1831  s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1832  s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1833  s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1834  } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1835  s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1836  s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1837  s->mv[0][n + 1][0] = s->mv[0][n][0];
1838  s->mv[0][n + 1][1] = s->mv[0][n][1];
1839  }
1840 }
1841 
1842 /** Motion compensation for direct or interpolated blocks in B-frames
1843  */
1844 static void vc1_interp_mc(VC1Context *v)
1845 {
1846  MpegEncContext *s = &v->s;
1847  DSPContext *dsp = &v->s.dsp;
1848  uint8_t *srcY, *srcU, *srcV;
1849  int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1850  int off, off_uv;
1851  int v_edge_pos = s->v_edge_pos >> v->field_mode;
1852 
1853  if (!v->field_mode && !v->s.next_picture.f.data[0])
1854  return;
1855 
1856  mx = s->mv[1][0][0];
1857  my = s->mv[1][0][1];
1858  uvmx = (mx + ((mx & 3) == 3)) >> 1;
1859  uvmy = (my + ((my & 3) == 3)) >> 1;
1860  if (v->field_mode) {
1861  if (v->cur_field_type != v->ref_field_type[1])
1862  my = my - 2 + 4 * v->cur_field_type;
1863  uvmy = uvmy - 2 + 4 * v->cur_field_type;
1864  }
1865  if (v->fastuvmc) {
1866  uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1867  uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1868  }
1869  srcY = s->next_picture.f.data[0];
1870  srcU = s->next_picture.f.data[1];
1871  srcV = s->next_picture.f.data[2];
1872 
1873  src_x = s->mb_x * 16 + (mx >> 2);
1874  src_y = s->mb_y * 16 + (my >> 2);
1875  uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1876  uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1877 
1878  if (v->profile != PROFILE_ADVANCED) {
1879  src_x = av_clip( src_x, -16, s->mb_width * 16);
1880  src_y = av_clip( src_y, -16, s->mb_height * 16);
1881  uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1882  uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1883  } else {
1884  src_x = av_clip( src_x, -17, s->avctx->coded_width);
1885  src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1886  uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1887  uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1888  }
1889 
1890  srcY += src_y * s->linesize + src_x;
1891  srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1892  srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1893 
1894  if (v->field_mode && v->ref_field_type[1]) {
1895  srcY += s->current_picture_ptr->f.linesize[0];
1896  srcU += s->current_picture_ptr->f.linesize[1];
1897  srcV += s->current_picture_ptr->f.linesize[2];
1898  }
1899 
1900  /* for grayscale we should not try to read from unknown area */
1901  if (s->flags & CODEC_FLAG_GRAY) {
1902  srcU = s->edge_emu_buffer + 18 * s->linesize;
1903  srcV = s->edge_emu_buffer + 18 * s->linesize;
1904  }
1905 
1906  if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1907  || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1908  || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1909  uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1910 
1911  srcY -= s->mspel * (1 + s->linesize);
1913  17 + s->mspel * 2, 17 + s->mspel * 2,
1914  src_x - s->mspel, src_y - s->mspel,
1915  s->h_edge_pos, v_edge_pos);
1916  srcY = s->edge_emu_buffer;
1917  s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1918  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1919  s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1920  uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1921  srcU = uvbuf;
1922  srcV = uvbuf + 16;
1923  /* if we deal with range reduction we need to scale source blocks */
1924  if (v->rangeredfrm) {
1925  int i, j;
1926  uint8_t *src, *src2;
1927 
1928  src = srcY;
1929  for (j = 0; j < 17 + s->mspel * 2; j++) {
1930  for (i = 0; i < 17 + s->mspel * 2; i++)
1931  src[i] = ((src[i] - 128) >> 1) + 128;
1932  src += s->linesize;
1933  }
1934  src = srcU;
1935  src2 = srcV;
1936  for (j = 0; j < 9; j++) {
1937  for (i = 0; i < 9; i++) {
1938  src[i] = ((src[i] - 128) >> 1) + 128;
1939  src2[i] = ((src2[i] - 128) >> 1) + 128;
1940  }
1941  src += s->uvlinesize;
1942  src2 += s->uvlinesize;
1943  }
1944  }
1945  srcY += s->mspel * (1 + s->linesize);
1946  }
1947 
1948  if (v->field_mode && v->second_field) {
1949  off = s->current_picture_ptr->f.linesize[0];
1950  off_uv = s->current_picture_ptr->f.linesize[1];
1951  } else {
1952  off = 0;
1953  off_uv = 0;
1954  }
1955 
1956  if (s->mspel) {
1957  dxy = ((my & 3) << 2) | (mx & 3);
1958  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1959  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1960  srcY += s->linesize * 8;
1961  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1962  v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1963  } else { // hpel mc
1964  dxy = (my & 2) | ((mx & 2) >> 1);
1965 
1966  if (!v->rnd)
1967  dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1968  else
1969  dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1970  }
1971 
1972  if (s->flags & CODEC_FLAG_GRAY) return;
1973  /* Chroma MC always uses qpel blilinear */
1974  uvmx = (uvmx & 3) << 1;
1975  uvmy = (uvmy & 3) << 1;
1976  if (!v->rnd) {
1977  dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1978  dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1979  } else {
1980  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1981  v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1982  }
1983 }
1984 
1985 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1986 {
1987  int n = bfrac;
1988 
1989 #if B_FRACTION_DEN==256
1990  if (inv)
1991  n -= 256;
1992  if (!qs)
1993  return 2 * ((value * n + 255) >> 9);
1994  return (value * n + 128) >> 8;
1995 #else
1996  if (inv)
1997  n -= B_FRACTION_DEN;
1998  if (!qs)
1999  return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2000  return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2001 #endif
2002 }
2003 
2004 /** Reconstruct motion vector for B-frame and do motion compensation
2005  */
2006 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2007  int direct, int mode)
2008 {
2009  if (v->use_ic) {
2010  v->mv_mode2 = v->mv_mode;
2012  }
2013  if (direct) {
2014  vc1_mc_1mv(v, 0);
2015  vc1_interp_mc(v);
2016  if (v->use_ic)
2017  v->mv_mode = v->mv_mode2;
2018  return;
2019  }
2020  if (mode == BMV_TYPE_INTERPOLATED) {
2021  vc1_mc_1mv(v, 0);
2022  vc1_interp_mc(v);
2023  if (v->use_ic)
2024  v->mv_mode = v->mv_mode2;
2025  return;
2026  }
2027 
2028  if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2029  v->mv_mode = v->mv_mode2;
2030  vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2031  if (v->use_ic)
2032  v->mv_mode = v->mv_mode2;
2033 }
2034 
2035 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2036  int direct, int mvtype)
2037 {
2038  MpegEncContext *s = &v->s;
2039  int xy, wrap, off = 0;
2040  int16_t *A, *B, *C;
2041  int px, py;
2042  int sum;
2043  int r_x, r_y;
2044  const uint8_t *is_intra = v->mb_type[0];
2045 
2046  r_x = v->range_x;
2047  r_y = v->range_y;
2048  /* scale MV difference to be quad-pel */
2049  dmv_x[0] <<= 1 - s->quarter_sample;
2050  dmv_y[0] <<= 1 - s->quarter_sample;
2051  dmv_x[1] <<= 1 - s->quarter_sample;
2052  dmv_y[1] <<= 1 - s->quarter_sample;
2053 
2054  wrap = s->b8_stride;
2055  xy = s->block_index[0];
2056 
2057  if (s->mb_intra) {
2058  s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2059  s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2060  s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2061  s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2062  return;
2063  }
2064  if (!v->field_mode) {
2065  s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2066  s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2067  s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2068  s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2069 
2070  /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2071  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));
2072  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));
2073  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));
2074  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));
2075  }
2076  if (direct) {
2077  s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2078  s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2079  s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2080  s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2081  return;
2082  }
2083 
2084  if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2085  C = s->current_picture.f.motion_val[0][xy - 2];
2086  A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2087  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2088  B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2089 
2090  if (!s->mb_x) C[0] = C[1] = 0;
2091  if (!s->first_slice_line) { // predictor A is not out of bounds
2092  if (s->mb_width == 1) {
2093  px = A[0];
2094  py = A[1];
2095  } else {
2096  px = mid_pred(A[0], B[0], C[0]);
2097  py = mid_pred(A[1], B[1], C[1]);
2098  }
2099  } else if (s->mb_x) { // predictor C is not out of bounds
2100  px = C[0];
2101  py = C[1];
2102  } else {
2103  px = py = 0;
2104  }
2105  /* Pullback MV as specified in 8.3.5.3.4 */
2106  {
2107  int qx, qy, X, Y;
2108  if (v->profile < PROFILE_ADVANCED) {
2109  qx = (s->mb_x << 5);
2110  qy = (s->mb_y << 5);
2111  X = (s->mb_width << 5) - 4;
2112  Y = (s->mb_height << 5) - 4;
2113  if (qx + px < -28) px = -28 - qx;
2114  if (qy + py < -28) py = -28 - qy;
2115  if (qx + px > X) px = X - qx;
2116  if (qy + py > Y) py = Y - qy;
2117  } else {
2118  qx = (s->mb_x << 6);
2119  qy = (s->mb_y << 6);
2120  X = (s->mb_width << 6) - 4;
2121  Y = (s->mb_height << 6) - 4;
2122  if (qx + px < -60) px = -60 - qx;
2123  if (qy + py < -60) py = -60 - qy;
2124  if (qx + px > X) px = X - qx;
2125  if (qy + py > Y) py = Y - qy;
2126  }
2127  }
2128  /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2129  if (0 && !s->first_slice_line && s->mb_x) {
2130  if (is_intra[xy - wrap])
2131  sum = FFABS(px) + FFABS(py);
2132  else
2133  sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2134  if (sum > 32) {
2135  if (get_bits1(&s->gb)) {
2136  px = A[0];
2137  py = A[1];
2138  } else {
2139  px = C[0];
2140  py = C[1];
2141  }
2142  } else {
2143  if (is_intra[xy - 2])
2144  sum = FFABS(px) + FFABS(py);
2145  else
2146  sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2147  if (sum > 32) {
2148  if (get_bits1(&s->gb)) {
2149  px = A[0];
2150  py = A[1];
2151  } else {
2152  px = C[0];
2153  py = C[1];
2154  }
2155  }
2156  }
2157  }
2158  /* store MV using signed modulus of MV range defined in 4.11 */
2159  s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2160  s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2161  }
2162  if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2163  C = s->current_picture.f.motion_val[1][xy - 2];
2164  A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2165  off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2166  B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2167 
2168  if (!s->mb_x)
2169  C[0] = C[1] = 0;
2170  if (!s->first_slice_line) { // predictor A is not out of bounds
2171  if (s->mb_width == 1) {
2172  px = A[0];
2173  py = A[1];
2174  } else {
2175  px = mid_pred(A[0], B[0], C[0]);
2176  py = mid_pred(A[1], B[1], C[1]);
2177  }
2178  } else if (s->mb_x) { // predictor C is not out of bounds
2179  px = C[0];
2180  py = C[1];
2181  } else {
2182  px = py = 0;
2183  }
2184  /* Pullback MV as specified in 8.3.5.3.4 */
2185  {
2186  int qx, qy, X, Y;
2187  if (v->profile < PROFILE_ADVANCED) {
2188  qx = (s->mb_x << 5);
2189  qy = (s->mb_y << 5);
2190  X = (s->mb_width << 5) - 4;
2191  Y = (s->mb_height << 5) - 4;
2192  if (qx + px < -28) px = -28 - qx;
2193  if (qy + py < -28) py = -28 - qy;
2194  if (qx + px > X) px = X - qx;
2195  if (qy + py > Y) py = Y - qy;
2196  } else {
2197  qx = (s->mb_x << 6);
2198  qy = (s->mb_y << 6);
2199  X = (s->mb_width << 6) - 4;
2200  Y = (s->mb_height << 6) - 4;
2201  if (qx + px < -60) px = -60 - qx;
2202  if (qy + py < -60) py = -60 - qy;
2203  if (qx + px > X) px = X - qx;
2204  if (qy + py > Y) py = Y - qy;
2205  }
2206  }
2207  /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2208  if (0 && !s->first_slice_line && s->mb_x) {
2209  if (is_intra[xy - wrap])
2210  sum = FFABS(px) + FFABS(py);
2211  else
2212  sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2213  if (sum > 32) {
2214  if (get_bits1(&s->gb)) {
2215  px = A[0];
2216  py = A[1];
2217  } else {
2218  px = C[0];
2219  py = C[1];
2220  }
2221  } else {
2222  if (is_intra[xy - 2])
2223  sum = FFABS(px) + FFABS(py);
2224  else
2225  sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2226  if (sum > 32) {
2227  if (get_bits1(&s->gb)) {
2228  px = A[0];
2229  py = A[1];
2230  } else {
2231  px = C[0];
2232  py = C[1];
2233  }
2234  }
2235  }
2236  }
2237  /* store MV using signed modulus of MV range defined in 4.11 */
2238 
2239  s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2240  s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2241  }
2242  s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2243  s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2244  s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2245  s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2246 }
2247 
2248 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2249 {
2250  int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2251  MpegEncContext *s = &v->s;
2252  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2253 
2254  if (v->bmvtype == BMV_TYPE_DIRECT) {
2255  int total_opp, k, f;
2256  if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2257  s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2258  v->bfraction, 0, s->quarter_sample);
2259  s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2260  v->bfraction, 0, s->quarter_sample);
2261  s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2262  v->bfraction, 1, s->quarter_sample);
2263  s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2264  v->bfraction, 1, s->quarter_sample);
2265 
2266  total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2267  + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2268  + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2269  + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2270  f = (total_opp > 2) ? 1 : 0;
2271  } else {
2272  s->mv[0][0][0] = s->mv[0][0][1] = 0;
2273  s->mv[1][0][0] = s->mv[1][0][1] = 0;
2274  f = 0;
2275  }
2276  v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2277  for (k = 0; k < 4; k++) {
2278  s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2279  s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2280  s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2281  s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2282  v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2283  v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2284  }
2285  return;
2286  }
2287  if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2288  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);
2289  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);
2290  return;
2291  }
2292  if (dir) { // backward
2293  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);
2294  if (n == 3 || mv1) {
2295  vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2296  }
2297  } else { // forward
2298  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);
2299  if (n == 3 || mv1) {
2300  vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2301  }
2302  }
2303 }
2304 
2305 /** Get predicted DC value for I-frames only
2306  * prediction dir: left=0, top=1
2307  * @param s MpegEncContext
2308  * @param overlap flag indicating that overlap filtering is used
2309  * @param pq integer part of picture quantizer
2310  * @param[in] n block index in the current MB
2311  * @param dc_val_ptr Pointer to DC predictor
2312  * @param dir_ptr Prediction direction for use in AC prediction
2313  */
2314 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2315  int16_t **dc_val_ptr, int *dir_ptr)
2316 {
2317  int a, b, c, wrap, pred, scale;
2318  int16_t *dc_val;
2319  static const uint16_t dcpred[32] = {
2320  -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2321  114, 102, 93, 85, 79, 73, 68, 64,
2322  60, 57, 54, 51, 49, 47, 45, 43,
2323  41, 39, 38, 37, 35, 34, 33
2324  };
2325 
2326  /* find prediction - wmv3_dc_scale always used here in fact */
2327  if (n < 4) scale = s->y_dc_scale;
2328  else scale = s->c_dc_scale;
2329 
2330  wrap = s->block_wrap[n];
2331  dc_val = s->dc_val[0] + s->block_index[n];
2332 
2333  /* B A
2334  * C X
2335  */
2336  c = dc_val[ - 1];
2337  b = dc_val[ - 1 - wrap];
2338  a = dc_val[ - wrap];
2339 
2340  if (pq < 9 || !overlap) {
2341  /* Set outer values */
2342  if (s->first_slice_line && (n != 2 && n != 3))
2343  b = a = dcpred[scale];
2344  if (s->mb_x == 0 && (n != 1 && n != 3))
2345  b = c = dcpred[scale];
2346  } else {
2347  /* Set outer values */
2348  if (s->first_slice_line && (n != 2 && n != 3))
2349  b = a = 0;
2350  if (s->mb_x == 0 && (n != 1 && n != 3))
2351  b = c = 0;
2352  }
2353 
2354  if (abs(a - b) <= abs(b - c)) {
2355  pred = c;
2356  *dir_ptr = 1; // left
2357  } else {
2358  pred = a;
2359  *dir_ptr = 0; // top
2360  }
2361 
2362  /* update predictor */
2363  *dc_val_ptr = &dc_val[0];
2364  return pred;
2365 }
2366 
2367 
2368 /** Get predicted DC value
2369  * prediction dir: left=0, top=1
2370  * @param s MpegEncContext
2371  * @param overlap flag indicating that overlap filtering is used
2372  * @param pq integer part of picture quantizer
2373  * @param[in] n block index in the current MB
2374  * @param a_avail flag indicating top block availability
2375  * @param c_avail flag indicating left block availability
2376  * @param dc_val_ptr Pointer to DC predictor
2377  * @param dir_ptr Prediction direction for use in AC prediction
2378  */
2379 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2380  int a_avail, int c_avail,
2381  int16_t **dc_val_ptr, int *dir_ptr)
2382 {
2383  int a, b, c, wrap, pred;
2384  int16_t *dc_val;
2385  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2386  int q1, q2 = 0;
2387  int dqscale_index;
2388 
2389  wrap = s->block_wrap[n];
2390  dc_val = s->dc_val[0] + s->block_index[n];
2391 
2392  /* B A
2393  * C X
2394  */
2395  c = dc_val[ - 1];
2396  b = dc_val[ - 1 - wrap];
2397  a = dc_val[ - wrap];
2398  /* scale predictors if needed */
2399  q1 = s->current_picture.f.qscale_table[mb_pos];
2400  dqscale_index = s->y_dc_scale_table[q1] - 1;
2401  if (dqscale_index < 0)
2402  return 0;
2403  if (c_avail && (n != 1 && n != 3)) {
2404  q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2405  if (q2 && q2 != q1)
2406  c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2407  }
2408  if (a_avail && (n != 2 && n != 3)) {
2409  q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2410  if (q2 && q2 != q1)
2411  a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2412  }
2413  if (a_avail && c_avail && (n != 3)) {
2414  int off = mb_pos;
2415  if (n != 1)
2416  off--;
2417  if (n != 2)
2418  off -= s->mb_stride;
2419  q2 = s->current_picture.f.qscale_table[off];
2420  if (q2 && q2 != q1)
2421  b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2422  }
2423 
2424  if (a_avail && c_avail) {
2425  if (abs(a - b) <= abs(b - c)) {
2426  pred = c;
2427  *dir_ptr = 1; // left
2428  } else {
2429  pred = a;
2430  *dir_ptr = 0; // top
2431  }
2432  } else if (a_avail) {
2433  pred = a;
2434  *dir_ptr = 0; // top
2435  } else if (c_avail) {
2436  pred = c;
2437  *dir_ptr = 1; // left
2438  } else {
2439  pred = 0;
2440  *dir_ptr = 1; // left
2441  }
2442 
2443  /* update predictor */
2444  *dc_val_ptr = &dc_val[0];
2445  return pred;
2446 }
2447 
2448 /** @} */ // Block group
2449 
2450 /**
2451  * @name VC1 Macroblock-level functions in Simple/Main Profiles
2452  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2453  * @{
2454  */
2455 
2456 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2457  uint8_t **coded_block_ptr)
2458 {
2459  int xy, wrap, pred, a, b, c;
2460 
2461  xy = s->block_index[n];
2462  wrap = s->b8_stride;
2463 
2464  /* B C
2465  * A X
2466  */
2467  a = s->coded_block[xy - 1 ];
2468  b = s->coded_block[xy - 1 - wrap];
2469  c = s->coded_block[xy - wrap];
2470 
2471  if (b == c) {
2472  pred = a;
2473  } else {
2474  pred = c;
2475  }
2476 
2477  /* store value */
2478  *coded_block_ptr = &s->coded_block[xy];
2479 
2480  return pred;
2481 }
2482 
2483 /**
2484  * Decode one AC coefficient
2485  * @param v The VC1 context
2486  * @param last Last coefficient
2487  * @param skip How much zero coefficients to skip
2488  * @param value Decoded AC coefficient value
2489  * @param codingset set of VLC to decode data
2490  * @see 8.1.3.4
2491  */
2492 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2493  int *value, int codingset)
2494 {
2495  GetBitContext *gb = &v->s.gb;
2496  int index, escape, run = 0, level = 0, lst = 0;
2497 
2498  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2499  if (index != ff_vc1_ac_sizes[codingset] - 1) {
2500  run = vc1_index_decode_table[codingset][index][0];
2501  level = vc1_index_decode_table[codingset][index][1];
2502  lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2503  if (get_bits1(gb))
2504  level = -level;
2505  } else {
2506  escape = decode210(gb);
2507  if (escape != 2) {
2508  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2509  run = vc1_index_decode_table[codingset][index][0];
2510  level = vc1_index_decode_table[codingset][index][1];
2511  lst = index >= vc1_last_decode_table[codingset];
2512  if (escape == 0) {
2513  if (lst)
2514  level += vc1_last_delta_level_table[codingset][run];
2515  else
2516  level += vc1_delta_level_table[codingset][run];
2517  } else {
2518  if (lst)
2519  run += vc1_last_delta_run_table[codingset][level] + 1;
2520  else
2521  run += vc1_delta_run_table[codingset][level] + 1;
2522  }
2523  if (get_bits1(gb))
2524  level = -level;
2525  } else {
2526  int sign;
2527  lst = get_bits1(gb);
2528  if (v->s.esc3_level_length == 0) {
2529  if (v->pq < 8 || v->dquantfrm) { // table 59
2530  v->s.esc3_level_length = get_bits(gb, 3);
2531  if (!v->s.esc3_level_length)
2532  v->s.esc3_level_length = get_bits(gb, 2) + 8;
2533  } else { // table 60
2534  v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2535  }
2536  v->s.esc3_run_length = 3 + get_bits(gb, 2);
2537  }
2538  run = get_bits(gb, v->s.esc3_run_length);
2539  sign = get_bits1(gb);
2540  level = get_bits(gb, v->s.esc3_level_length);
2541  if (sign)
2542  level = -level;
2543  }
2544  }
2545 
2546  *last = lst;
2547  *skip = run;
2548  *value = level;
2549 }
2550 
2551 /** Decode intra block in intra frames - should be faster than decode_intra_block
2552  * @param v VC1Context
2553  * @param block block to decode
2554  * @param[in] n subblock index
2555  * @param coded are AC coeffs present or not
2556  * @param codingset set of VLC to decode data
2557  */
2558 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2559  int coded, int codingset)
2560 {
2561  GetBitContext *gb = &v->s.gb;
2562  MpegEncContext *s = &v->s;
2563  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2564  int i;
2565  int16_t *dc_val;
2566  int16_t *ac_val, *ac_val2;
2567  int dcdiff;
2568 
2569  /* Get DC differential */
2570  if (n < 4) {
2572  } else {
2574  }
2575  if (dcdiff < 0) {
2576  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2577  return -1;
2578  }
2579  if (dcdiff) {
2580  if (dcdiff == 119 /* ESC index value */) {
2581  /* TODO: Optimize */
2582  if (v->pq == 1) dcdiff = get_bits(gb, 10);
2583  else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2584  else dcdiff = get_bits(gb, 8);
2585  } else {
2586  if (v->pq == 1)
2587  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2588  else if (v->pq == 2)
2589  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2590  }
2591  if (get_bits1(gb))
2592  dcdiff = -dcdiff;
2593  }
2594 
2595  /* Prediction */
2596  dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2597  *dc_val = dcdiff;
2598 
2599  /* Store the quantized DC coeff, used for prediction */
2600  if (n < 4) {
2601  block[0] = dcdiff * s->y_dc_scale;
2602  } else {
2603  block[0] = dcdiff * s->c_dc_scale;
2604  }
2605  /* Skip ? */
2606  if (!coded) {
2607  goto not_coded;
2608  }
2609 
2610  // AC Decoding
2611  i = 1;
2612 
2613  {
2614  int last = 0, skip, value;
2615  const uint8_t *zz_table;
2616  int scale;
2617  int k;
2618 
2619  scale = v->pq * 2 + v->halfpq;
2620 
2621  if (v->s.ac_pred) {
2622  if (!dc_pred_dir)
2623  zz_table = v->zz_8x8[2];
2624  else
2625  zz_table = v->zz_8x8[3];
2626  } else
2627  zz_table = v->zz_8x8[1];
2628 
2629  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2630  ac_val2 = ac_val;
2631  if (dc_pred_dir) // left
2632  ac_val -= 16;
2633  else // top
2634  ac_val -= 16 * s->block_wrap[n];
2635 
2636  while (!last) {
2637  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2638  i += skip;
2639  if (i > 63)
2640  break;
2641  block[zz_table[i++]] = value;
2642  }
2643 
2644  /* apply AC prediction if needed */
2645  if (s->ac_pred) {
2646  if (dc_pred_dir) { // left
2647  for (k = 1; k < 8; k++)
2648  block[k << v->left_blk_sh] += ac_val[k];
2649  } else { // top
2650  for (k = 1; k < 8; k++)
2651  block[k << v->top_blk_sh] += ac_val[k + 8];
2652  }
2653  }
2654  /* save AC coeffs for further prediction */
2655  for (k = 1; k < 8; k++) {
2656  ac_val2[k] = block[k << v->left_blk_sh];
2657  ac_val2[k + 8] = block[k << v->top_blk_sh];
2658  }
2659 
2660  /* scale AC coeffs */
2661  for (k = 1; k < 64; k++)
2662  if (block[k]) {
2663  block[k] *= scale;
2664  if (!v->pquantizer)
2665  block[k] += (block[k] < 0) ? -v->pq : v->pq;
2666  }
2667 
2668  if (s->ac_pred) i = 63;
2669  }
2670 
2671 not_coded:
2672  if (!coded) {
2673  int k, scale;
2674  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2675  ac_val2 = ac_val;
2676 
2677  i = 0;
2678  scale = v->pq * 2 + v->halfpq;
2679  memset(ac_val2, 0, 16 * 2);
2680  if (dc_pred_dir) { // left
2681  ac_val -= 16;
2682  if (s->ac_pred)
2683  memcpy(ac_val2, ac_val, 8 * 2);
2684  } else { // top
2685  ac_val -= 16 * s->block_wrap[n];
2686  if (s->ac_pred)
2687  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2688  }
2689 
2690  /* apply AC prediction if needed */
2691  if (s->ac_pred) {
2692  if (dc_pred_dir) { //left
2693  for (k = 1; k < 8; k++) {
2694  block[k << v->left_blk_sh] = ac_val[k] * scale;
2695  if (!v->pquantizer && block[k << v->left_blk_sh])
2696  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2697  }
2698  } else { // top
2699  for (k = 1; k < 8; k++) {
2700  block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2701  if (!v->pquantizer && block[k << v->top_blk_sh])
2702  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2703  }
2704  }
2705  i = 63;
2706  }
2707  }
2708  s->block_last_index[n] = i;
2709 
2710  return 0;
2711 }
2712 
2713 /** Decode intra block in intra frames - should be faster than decode_intra_block
2714  * @param v VC1Context
2715  * @param block block to decode
2716  * @param[in] n subblock number
2717  * @param coded are AC coeffs present or not
2718  * @param codingset set of VLC to decode data
2719  * @param mquant quantizer value for this macroblock
2720  */
2722  int coded, int codingset, int mquant)
2723 {
2724  GetBitContext *gb = &v->s.gb;
2725  MpegEncContext *s = &v->s;
2726  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2727  int i;
2728  int16_t *dc_val;
2729  int16_t *ac_val, *ac_val2;
2730  int dcdiff;
2731  int a_avail = v->a_avail, c_avail = v->c_avail;
2732  int use_pred = s->ac_pred;
2733  int scale;
2734  int q1, q2 = 0;
2735  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2736 
2737  /* Get DC differential */
2738  if (n < 4) {
2740  } else {
2742  }
2743  if (dcdiff < 0) {
2744  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2745  return -1;
2746  }
2747  if (dcdiff) {
2748  if (dcdiff == 119 /* ESC index value */) {
2749  /* TODO: Optimize */
2750  if (mquant == 1) dcdiff = get_bits(gb, 10);
2751  else if (mquant == 2) dcdiff = get_bits(gb, 9);
2752  else dcdiff = get_bits(gb, 8);
2753  } else {
2754  if (mquant == 1)
2755  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2756  else if (mquant == 2)
2757  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2758  }
2759  if (get_bits1(gb))
2760  dcdiff = -dcdiff;
2761  }
2762 
2763  /* Prediction */
2764  dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2765  *dc_val = dcdiff;
2766 
2767  /* Store the quantized DC coeff, used for prediction */
2768  if (n < 4) {
2769  block[0] = dcdiff * s->y_dc_scale;
2770  } else {
2771  block[0] = dcdiff * s->c_dc_scale;
2772  }
2773 
2774  //AC Decoding
2775  i = 1;
2776 
2777  /* check if AC is needed at all */
2778  if (!a_avail && !c_avail)
2779  use_pred = 0;
2780  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2781  ac_val2 = ac_val;
2782 
2783  scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2784 
2785  if (dc_pred_dir) // left
2786  ac_val -= 16;
2787  else // top
2788  ac_val -= 16 * s->block_wrap[n];
2789 
2790  q1 = s->current_picture.f.qscale_table[mb_pos];
2791  if ( dc_pred_dir && c_avail && mb_pos)
2792  q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2793  if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2794  q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2795  if ( dc_pred_dir && n == 1)
2796  q2 = q1;
2797  if (!dc_pred_dir && n == 2)
2798  q2 = q1;
2799  if (n == 3)
2800  q2 = q1;
2801 
2802  if (coded) {
2803  int last = 0, skip, value;
2804  const uint8_t *zz_table;
2805  int k;
2806 
2807  if (v->s.ac_pred) {
2808  if (!use_pred && v->fcm == ILACE_FRAME) {
2809  zz_table = v->zzi_8x8;
2810  } else {
2811  if (!dc_pred_dir) // top
2812  zz_table = v->zz_8x8[2];
2813  else // left
2814  zz_table = v->zz_8x8[3];
2815  }
2816  } else {
2817  if (v->fcm != ILACE_FRAME)
2818  zz_table = v->zz_8x8[1];
2819  else
2820  zz_table = v->zzi_8x8;
2821  }
2822 
2823  while (!last) {
2824  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2825  i += skip;
2826  if (i > 63)
2827  break;
2828  block[zz_table[i++]] = value;
2829  }
2830 
2831  /* apply AC prediction if needed */
2832  if (use_pred) {
2833  /* scale predictors if needed*/
2834  if (q2 && q1 != q2) {
2835  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2836  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2837 
2838  if (q1 < 1)
2839  return AVERROR_INVALIDDATA;
2840  if (dc_pred_dir) { // left
2841  for (k = 1; k < 8; k++)
2842  block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2843  } else { // top
2844  for (k = 1; k < 8; k++)
2845  block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2846  }
2847  } else {
2848  if (dc_pred_dir) { //left
2849  for (k = 1; k < 8; k++)
2850  block[k << v->left_blk_sh] += ac_val[k];
2851  } else { //top
2852  for (k = 1; k < 8; k++)
2853  block[k << v->top_blk_sh] += ac_val[k + 8];
2854  }
2855  }
2856  }
2857  /* save AC coeffs for further prediction */
2858  for (k = 1; k < 8; k++) {
2859  ac_val2[k ] = block[k << v->left_blk_sh];
2860  ac_val2[k + 8] = block[k << v->top_blk_sh];
2861  }
2862 
2863  /* scale AC coeffs */
2864  for (k = 1; k < 64; k++)
2865  if (block[k]) {
2866  block[k] *= scale;
2867  if (!v->pquantizer)
2868  block[k] += (block[k] < 0) ? -mquant : mquant;
2869  }
2870 
2871  if (use_pred) i = 63;
2872  } else { // no AC coeffs
2873  int k;
2874 
2875  memset(ac_val2, 0, 16 * 2);
2876  if (dc_pred_dir) { // left
2877  if (use_pred) {
2878  memcpy(ac_val2, ac_val, 8 * 2);
2879  if (q2 && q1 != q2) {
2880  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2881  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2882  if (q1 < 1)
2883  return AVERROR_INVALIDDATA;
2884  for (k = 1; k < 8; k++)
2885  ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2886  }
2887  }
2888  } else { // top
2889  if (use_pred) {
2890  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2891  if (q2 && q1 != q2) {
2892  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2893  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2894  if (q1 < 1)
2895  return AVERROR_INVALIDDATA;
2896  for (k = 1; k < 8; k++)
2897  ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2898  }
2899  }
2900  }
2901 
2902  /* apply AC prediction if needed */
2903  if (use_pred) {
2904  if (dc_pred_dir) { // left
2905  for (k = 1; k < 8; k++) {
2906  block[k << v->left_blk_sh] = ac_val2[k] * scale;
2907  if (!v->pquantizer && block[k << v->left_blk_sh])
2908  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2909  }
2910  } else { // top
2911  for (k = 1; k < 8; k++) {
2912  block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2913  if (!v->pquantizer && block[k << v->top_blk_sh])
2914  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2915  }
2916  }
2917  i = 63;
2918  }
2919  }
2920  s->block_last_index[n] = i;
2921 
2922  return 0;
2923 }
2924 
2925 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2926  * @param v VC1Context
2927  * @param block block to decode
2928  * @param[in] n subblock index
2929  * @param coded are AC coeffs present or not
2930  * @param mquant block quantizer
2931  * @param codingset set of VLC to decode data
2932  */
2934  int coded, int mquant, int codingset)
2935 {
2936  GetBitContext *gb = &v->s.gb;
2937  MpegEncContext *s = &v->s;
2938  int dc_pred_dir = 0; /* Direction of the DC prediction used */
2939  int i;
2940  int16_t *dc_val;
2941  int16_t *ac_val, *ac_val2;
2942  int dcdiff;
2943  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2944  int a_avail = v->a_avail, c_avail = v->c_avail;
2945  int use_pred = s->ac_pred;
2946  int scale;
2947  int q1, q2 = 0;
2948 
2949  s->dsp.clear_block(block);
2950 
2951  /* XXX: Guard against dumb values of mquant */
2952  mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2953 
2954  /* Set DC scale - y and c use the same */
2955  s->y_dc_scale = s->y_dc_scale_table[mquant];
2956  s->c_dc_scale = s->c_dc_scale_table[mquant];
2957 
2958  /* Get DC differential */
2959  if (n < 4) {
2961  } else {
2963  }
2964  if (dcdiff < 0) {
2965  av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2966  return -1;
2967  }
2968  if (dcdiff) {
2969  if (dcdiff == 119 /* ESC index value */) {
2970  /* TODO: Optimize */
2971  if (mquant == 1) dcdiff = get_bits(gb, 10);
2972  else if (mquant == 2) dcdiff = get_bits(gb, 9);
2973  else dcdiff = get_bits(gb, 8);
2974  } else {
2975  if (mquant == 1)
2976  dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2977  else if (mquant == 2)
2978  dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2979  }
2980  if (get_bits1(gb))
2981  dcdiff = -dcdiff;
2982  }
2983 
2984  /* Prediction */
2985  dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2986  *dc_val = dcdiff;
2987 
2988  /* Store the quantized DC coeff, used for prediction */
2989 
2990  if (n < 4) {
2991  block[0] = dcdiff * s->y_dc_scale;
2992  } else {
2993  block[0] = dcdiff * s->c_dc_scale;
2994  }
2995 
2996  //AC Decoding
2997  i = 1;
2998 
2999  /* check if AC is needed at all and adjust direction if needed */
3000  if (!a_avail) dc_pred_dir = 1;
3001  if (!c_avail) dc_pred_dir = 0;
3002  if (!a_avail && !c_avail) use_pred = 0;
3003  ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3004  ac_val2 = ac_val;
3005 
3006  scale = mquant * 2 + v->halfpq;
3007 
3008  if (dc_pred_dir) //left
3009  ac_val -= 16;
3010  else //top
3011  ac_val -= 16 * s->block_wrap[n];
3012 
3013  q1 = s->current_picture.f.qscale_table[mb_pos];
3014  if (dc_pred_dir && c_avail && mb_pos)
3015  q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3016  if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3017  q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3018  if ( dc_pred_dir && n == 1)
3019  q2 = q1;
3020  if (!dc_pred_dir && n == 2)
3021  q2 = q1;
3022  if (n == 3) q2 = q1;
3023 
3024  if (coded) {
3025  int last = 0, skip, value;
3026  int k;
3027 
3028  while (!last) {
3029  vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3030  i += skip;
3031  if (i > 63)
3032  break;
3033  if (v->fcm == PROGRESSIVE)
3034  block[v->zz_8x8[0][i++]] = value;
3035  else {
3036  if (use_pred && (v->fcm == ILACE_FRAME)) {
3037  if (!dc_pred_dir) // top
3038  block[v->zz_8x8[2][i++]] = value;
3039  else // left
3040  block[v->zz_8x8[3][i++]] = value;
3041  } else {
3042  block[v->zzi_8x8[i++]] = value;
3043  }
3044  }
3045  }
3046 
3047  /* apply AC prediction if needed */
3048  if (use_pred) {
3049  /* scale predictors if needed*/
3050  if (q2 && q1 != q2) {
3051  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3052  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3053 
3054  if (q1 < 1)
3055  return AVERROR_INVALIDDATA;
3056  if (dc_pred_dir) { // left
3057  for (k = 1; k < 8; k++)
3058  block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3059  } else { //top
3060  for (k = 1; k < 8; k++)
3061  block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3062  }
3063  } else {
3064  if (dc_pred_dir) { // left
3065  for (k = 1; k < 8; k++)
3066  block[k << v->left_blk_sh] += ac_val[k];
3067  } else { // top
3068  for (k = 1; k < 8; k++)
3069  block[k << v->top_blk_sh] += ac_val[k + 8];
3070  }
3071  }
3072  }
3073  /* save AC coeffs for further prediction */
3074  for (k = 1; k < 8; k++) {
3075  ac_val2[k ] = block[k << v->left_blk_sh];
3076  ac_val2[k + 8] = block[k << v->top_blk_sh];
3077  }
3078 
3079  /* scale AC coeffs */
3080  for (k = 1; k < 64; k++)
3081  if (block[k]) {
3082  block[k] *= scale;
3083  if (!v->pquantizer)
3084  block[k] += (block[k] < 0) ? -mquant : mquant;
3085  }
3086 
3087  if (use_pred) i = 63;
3088  } else { // no AC coeffs
3089  int k;
3090 
3091  memset(ac_val2, 0, 16 * 2);
3092  if (dc_pred_dir) { // left
3093  if (use_pred) {
3094  memcpy(ac_val2, ac_val, 8 * 2);
3095  if (q2 && q1 != q2) {
3096  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3097  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3098  if (q1 < 1)
3099  return AVERROR_INVALIDDATA;
3100  for (k = 1; k < 8; k++)
3101  ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3102  }
3103  }
3104  } else { // top
3105  if (use_pred) {
3106  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3107  if (q2 && q1 != q2) {
3108  q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3109  q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3110  if (q1 < 1)
3111  return AVERROR_INVALIDDATA;
3112  for (k = 1; k < 8; k++)
3113  ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3114  }
3115  }
3116  }
3117 
3118  /* apply AC prediction if needed */
3119  if (use_pred) {
3120  if (dc_pred_dir) { // left
3121  for (k = 1; k < 8; k++) {
3122  block[k << v->left_blk_sh] = ac_val2[k] * scale;
3123  if (!v->pquantizer && block[k << v->left_blk_sh])
3124  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3125  }
3126  } else { // top
3127  for (k = 1; k < 8; k++) {
3128  block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3129  if (!v->pquantizer && block[k << v->top_blk_sh])
3130  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3131  }
3132  }
3133  i = 63;
3134  }
3135  }
3136  s->block_last_index[n] = i;
3137 
3138  return 0;
3139 }
3140 
3141 /** Decode P block
3142  */
3143 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3144  int mquant, int ttmb, int first_block,
3145  uint8_t *dst, int linesize, int skip_block,
3146  int *ttmb_out)
3147 {
3148  MpegEncContext *s = &v->s;
3149  GetBitContext *gb = &s->gb;
3150  int i, j;
3151  int subblkpat = 0;
3152  int scale, off, idx, last, skip, value;
3153  int ttblk = ttmb & 7;
3154  int pat = 0;
3155 
3156  s->dsp.clear_block(block);
3157 
3158  if (ttmb == -1) {
3160  }
3161  if (ttblk == TT_4X4) {
3162  subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3163  }
3164  if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3165  && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3166  || (!v->res_rtm_flag && !first_block))) {
3167  subblkpat = decode012(gb);
3168  if (subblkpat)
3169  subblkpat ^= 3; // swap decoded pattern bits
3170  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3171  ttblk = TT_8X4;
3172  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3173  ttblk = TT_4X8;
3174  }
3175  scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3176 
3177  // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3178  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3179  subblkpat = 2 - (ttblk == TT_8X4_TOP);
3180  ttblk = TT_8X4;
3181  }
3182  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3183  subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3184  ttblk = TT_4X8;
3185  }
3186  switch (ttblk) {
3187  case TT_8X8:
3188  pat = 0xF;
3189  i = 0;
3190  last = 0;
3191  while (!last) {
3192  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3193  i += skip;
3194  if (i > 63)
3195  break;
3196  if (!v->fcm)
3197  idx = v->zz_8x8[0][i++];
3198  else
3199  idx = v->zzi_8x8[i++];
3200  block[idx] = value * scale;
3201  if (!v->pquantizer)
3202  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3203  }
3204  if (!skip_block) {
3205  if (i == 1)
3206  v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3207  else {
3208  v->vc1dsp.vc1_inv_trans_8x8(block);
3209  s->dsp.add_pixels_clamped(block, dst, linesize);
3210  }
3211  }
3212  break;
3213  case TT_4X4:
3214  pat = ~subblkpat & 0xF;
3215  for (j = 0; j < 4; j++) {
3216  last = subblkpat & (1 << (3 - j));
3217  i = 0;
3218  off = (j & 1) * 4 + (j & 2) * 16;
3219  while (!last) {
3220  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3221  i += skip;
3222  if (i > 15)
3223  break;
3224  if (!v->fcm)
3226  else
3227  idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3228  block[idx + off] = value * scale;
3229  if (!v->pquantizer)
3230  block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3231  }
3232  if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3233  if (i == 1)
3234  v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3235  else
3236  v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3237  }
3238  }
3239  break;
3240  case TT_8X4:
3241  pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3242  for (j = 0; j < 2; j++) {
3243  last = subblkpat & (1 << (1 - j));
3244  i = 0;
3245  off = j * 32;
3246  while (!last) {
3247  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3248  i += skip;
3249  if (i > 31)
3250  break;
3251  if (!v->fcm)
3252  idx = v->zz_8x4[i++] + off;
3253  else
3254  idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3255  block[idx] = value * scale;
3256  if (!v->pquantizer)
3257  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3258  }
3259  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3260  if (i == 1)
3261  v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3262  else
3263  v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3264  }
3265  }
3266  break;
3267  case TT_4X8:
3268  pat = ~(subblkpat * 5) & 0xF;
3269  for (j = 0; j < 2; j++) {
3270  last = subblkpat & (1 << (1 - j));
3271  i = 0;
3272  off = j * 4;
3273  while (!last) {
3274  vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3275  i += skip;
3276  if (i > 31)
3277  break;
3278  if (!v->fcm)
3279  idx = v->zz_4x8[i++] + off;
3280  else
3281  idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3282  block[idx] = value * scale;
3283  if (!v->pquantizer)
3284  block[idx] += (block[idx] < 0) ? -mquant : mquant;
3285  }
3286  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3287  if (i == 1)
3288  v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3289  else
3290  v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3291  }
3292  }
3293  break;
3294  }
3295  if (ttmb_out)
3296  *ttmb_out |= ttblk << (n * 4);
3297  return pat;
3298 }
3299 
3300 /** @} */ // Macroblock group
3301 
3302 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3303 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3304 
3306 {
3307  MpegEncContext *s = &v->s;
3308  int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3309  block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3310  mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3311  block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3312  int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3313  uint8_t *dst;
3314 
3315  if (block_num > 3) {
3316  dst = s->dest[block_num - 3];
3317  } else {
3318  dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3319  }
3320  if (s->mb_y != s->end_mb_y || block_num < 2) {
3321  int16_t (*mv)[2];
3322  int mv_stride;
3323 
3324  if (block_num > 3) {
3325  bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3326  bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3327  mv = &v->luma_mv[s->mb_x - s->mb_stride];
3328  mv_stride = s->mb_stride;
3329  } else {
3330  bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3331  : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3332  bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3333  : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3334  mv_stride = s->b8_stride;
3335  mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3336  }
3337 
3338  if (bottom_is_intra & 1 || block_is_intra & 1 ||
3339  mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3340  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3341  } else {
3342  idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3343  if (idx == 3) {
3344  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3345  } else if (idx) {
3346  if (idx == 1)
3347  v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3348  else
3349  v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3350  }
3351  }
3352  }
3353 
3354  dst -= 4 * linesize;
3355  ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3356  if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3357  idx = (block_cbp | (block_cbp >> 2)) & 3;
3358  if (idx == 3) {
3359  v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3360  } else if (idx) {
3361  if (idx == 1)
3362  v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3363  else
3364  v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3365  }
3366  }
3367 }
3368 
3370 {
3371  MpegEncContext *s = &v->s;
3372  int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3373  block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3374  mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3375  block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3376  int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3377  uint8_t *dst;
3378 
3379  if (block_num > 3) {
3380  dst = s->dest[block_num - 3] - 8 * linesize;
3381  } else {
3382  dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3383  }
3384 
3385  if (s->mb_x != s->mb_width || !(block_num & 5)) {
3386  int16_t (*mv)[2];
3387 
3388  if (block_num > 3) {
3389  right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3390  right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3391  mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3392  } else {
3393  right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3394  : (mb_cbp >> ((block_num + 1) * 4));
3395  right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3396  : (mb_is_intra >> ((block_num + 1) * 4));
3397  mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3398  }
3399  if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3400  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3401  } else {
3402  idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3403  if (idx == 5) {
3404  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3405  } else if (idx) {
3406  if (idx == 1)
3407  v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3408  else
3409  v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3410  }
3411  }
3412  }
3413 
3414  dst -= 4;
3415  ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3416  if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3417  idx = (block_cbp | (block_cbp >> 1)) & 5;
3418  if (idx == 5) {
3419  v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3420  } else if (idx) {
3421  if (idx == 1)
3422  v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3423  else
3424  v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3425  }
3426  }
3427 }
3428 
3430 {
3431  MpegEncContext *s = &v->s;
3432  int i;
3433 
3434  for (i = 0; i < 6; i++) {
3436  }
3437 
3438  /* V always precedes H, therefore we run H one MB before V;
3439  * at the end of a row, we catch up to complete the row */
3440  if (s->mb_x) {
3441  for (i = 0; i < 6; i++) {
3443  }
3444  if (s->mb_x == s->mb_width - 1) {
3445  s->mb_x++;
3447  for (i = 0; i < 6; i++) {
3449  }
3450  }
3451  }
3452 }
3453 
3454 /** Decode one P-frame MB
3455  */
3457 {
3458  MpegEncContext *s = &v->s;
3459  GetBitContext *gb = &s->gb;
3460  int i, j;
3461  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3462  int cbp; /* cbp decoding stuff */
3463  int mqdiff, mquant; /* MB quantization */
3464  int ttmb = v->ttfrm; /* MB Transform type */
3465 
3466  int mb_has_coeffs = 1; /* last_flag */
3467  int dmv_x, dmv_y; /* Differential MV components */
3468  int index, index1; /* LUT indexes */
3469  int val, sign; /* temp values */
3470  int first_block = 1;
3471  int dst_idx, off;
3472  int skipped, fourmv;
3473  int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3474 
3475  mquant = v->pq; /* lossy initialization */
3476 
3477  if (v->mv_type_is_raw)
3478  fourmv = get_bits1(gb);
3479  else
3480  fourmv = v->mv_type_mb_plane[mb_pos];
3481  if (v->skip_is_raw)
3482  skipped = get_bits1(gb);
3483  else
3484  skipped = v->s.mbskip_table[mb_pos];
3485 
3486  if (!fourmv) { /* 1MV mode */
3487  if (!skipped) {
3488  GET_MVDATA(dmv_x, dmv_y);
3489 
3490  if (s->mb_intra) {
3491  s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3492  s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3493  }
3495  vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3496 
3497  /* FIXME Set DC val for inter block ? */
3498  if (s->mb_intra && !mb_has_coeffs) {
3499  GET_MQUANT();
3500  s->ac_pred = get_bits1(gb);
3501  cbp = 0;
3502  } else if (mb_has_coeffs) {
3503  if (s->mb_intra)
3504  s->ac_pred = get_bits1(gb);
3505  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3506  GET_MQUANT();
3507  } else {
3508  mquant = v->pq;
3509  cbp = 0;
3510  }
3511  s->current_picture.f.qscale_table[mb_pos] = mquant;
3512 
3513  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3514  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3515  VC1_TTMB_VLC_BITS, 2);
3516  if (!s->mb_intra) vc1_mc_1mv(v, 0);
3517  dst_idx = 0;
3518  for (i = 0; i < 6; i++) {
3519  s->dc_val[0][s->block_index[i]] = 0;
3520  dst_idx += i >> 2;
3521  val = ((cbp >> (5 - i)) & 1);
3522  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3523  v->mb_type[0][s->block_index[i]] = s->mb_intra;
3524  if (s->mb_intra) {
3525  /* check if prediction blocks A and C are available */
3526  v->a_avail = v->c_avail = 0;
3527  if (i == 2 || i == 3 || !s->first_slice_line)
3528  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3529  if (i == 1 || i == 3 || s->mb_x)
3530  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3531 
3532  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3533  (i & 4) ? v->codingset2 : v->codingset);
3534  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3535  continue;
3536  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3537  if (v->rangeredfrm)
3538  for (j = 0; j < 64; j++)
3539  s->block[i][j] <<= 1;
3540  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3541  if (v->pq >= 9 && v->overlap) {
3542  if (v->c_avail)
3543  v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3544  if (v->a_avail)
3545  v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3546  }
3547  block_cbp |= 0xF << (i << 2);
3548  block_intra |= 1 << i;
3549  } else if (val) {
3550  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3551  s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3552  (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3553  block_cbp |= pat << (i << 2);
3554  if (!v->ttmbf && ttmb < 8)
3555  ttmb = -1;
3556  first_block = 0;
3557  }
3558  }
3559  } else { // skipped
3560  s->mb_intra = 0;
3561  for (i = 0; i < 6; i++) {
3562  v->mb_type[0][s->block_index[i]] = 0;
3563  s->dc_val[0][s->block_index[i]] = 0;
3564  }
3565  s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3566  s->current_picture.f.qscale_table[mb_pos] = 0;
3567  vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3568  vc1_mc_1mv(v, 0);
3569  }
3570  } else { // 4MV mode
3571  if (!skipped /* unskipped MB */) {
3572  int intra_count = 0, coded_inter = 0;
3573  int is_intra[6], is_coded[6];
3574  /* Get CBPCY */
3575  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3576  for (i = 0; i < 6; i++) {
3577  val = ((cbp >> (5 - i)) & 1);
3578  s->dc_val[0][s->block_index[i]] = 0;
3579  s->mb_intra = 0;
3580  if (i < 4) {
3581  dmv_x = dmv_y = 0;
3582  s->mb_intra = 0;
3583  mb_has_coeffs = 0;
3584  if (val) {
3585  GET_MVDATA(dmv_x, dmv_y);
3586  }
3587  vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3588  if (!s->mb_intra)
3589  vc1_mc_4mv_luma(v, i, 0);
3590  intra_count += s->mb_intra;
3591  is_intra[i] = s->mb_intra;
3592  is_coded[i] = mb_has_coeffs;
3593  }
3594  if (i & 4) {
3595  is_intra[i] = (intra_count >= 3);
3596  is_coded[i] = val;
3597  }
3598  if (i == 4)
3599  vc1_mc_4mv_chroma(v, 0);
3600  v->mb_type[0][s->block_index[i]] = is_intra[i];
3601  if (!coded_inter)
3602  coded_inter = !is_intra[i] & is_coded[i];
3603  }
3604  // if there are no coded blocks then don't do anything more
3605  dst_idx = 0;
3606  if (!intra_count && !coded_inter)
3607  goto end;
3608  GET_MQUANT();
3609  s->current_picture.f.qscale_table[mb_pos] = mquant;
3610  /* test if block is intra and has pred */
3611  {
3612  int intrapred = 0;
3613  for (i = 0; i < 6; i++)
3614  if (is_intra[i]) {
3615  if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3616  || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3617  intrapred = 1;
3618  break;
3619  }
3620  }
3621  if (intrapred)
3622  s->ac_pred = get_bits1(gb);
3623  else
3624  s->ac_pred = 0;
3625  }
3626  if (!v->ttmbf && coded_inter)
3628  for (i = 0; i < 6; i++) {
3629  dst_idx += i >> 2;
3630  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3631  s->mb_intra = is_intra[i];
3632  if (is_intra[i]) {
3633  /* check if prediction blocks A and C are available */
3634  v->a_avail = v->c_avail = 0;
3635  if (i == 2 || i == 3 || !s->first_slice_line)
3636  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3637  if (i == 1 || i == 3 || s->mb_x)
3638  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3639 
3640  vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3641  (i & 4) ? v->codingset2 : v->codingset);
3642  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3643  continue;
3644  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3645  if (v->rangeredfrm)
3646  for (j = 0; j < 64; j++)
3647  s->block[i][j] <<= 1;
3648  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3649  (i & 4) ? s->uvlinesize : s->linesize);
3650  if (v->pq >= 9 && v->overlap) {
3651  if (v->c_avail)
3652  v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3653  if (v->a_avail)
3654  v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3655  }
3656  block_cbp |= 0xF << (i << 2);
3657  block_intra |= 1 << i;
3658  } else if (is_coded[i]) {
3659  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3660  first_block, s->dest[dst_idx] + off,
3661  (i & 4) ? s->uvlinesize : s->linesize,
3662  (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3663  &block_tt);
3664  block_cbp |= pat << (i << 2);
3665  if (!v->ttmbf && ttmb < 8)
3666  ttmb = -1;
3667  first_block = 0;
3668  }
3669  }
3670  } else { // skipped MB
3671  s->mb_intra = 0;
3672  s->current_picture.f.qscale_table[mb_pos] = 0;
3673  for (i = 0; i < 6; i++) {
3674  v->mb_type[0][s->block_index[i]] = 0;
3675  s->dc_val[0][s->block_index[i]] = 0;
3676  }
3677  for (i = 0; i < 4; i++) {
3678  vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3679  vc1_mc_4mv_luma(v, i, 0);
3680  }
3681  vc1_mc_4mv_chroma(v, 0);
3682  s->current_picture.f.qscale_table[mb_pos] = 0;
3683  }
3684  }
3685 end:
3686  v->cbp[s->mb_x] = block_cbp;
3687  v->ttblk[s->mb_x] = block_tt;
3688  v->is_intra[s->mb_x] = block_intra;
3689 
3690  return 0;
3691 }
3692 
3693 /* Decode one macroblock in an interlaced frame p picture */
3694 
3696 {
3697  MpegEncContext *s = &v->s;
3698  GetBitContext *gb = &s->gb;
3699  int i;
3700  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3701  int cbp = 0; /* cbp decoding stuff */
3702  int mqdiff, mquant; /* MB quantization */
3703  int ttmb = v->ttfrm; /* MB Transform type */
3704 
3705  int mb_has_coeffs = 1; /* last_flag */
3706  int dmv_x, dmv_y; /* Differential MV components */
3707  int val; /* temp value */
3708  int first_block = 1;
3709  int dst_idx, off;
3710  int skipped, fourmv = 0, twomv = 0;
3711  int block_cbp = 0, pat, block_tt = 0;
3712  int idx_mbmode = 0, mvbp;
3713  int stride_y, fieldtx;
3714 
3715  mquant = v->pq; /* Lossy initialization */
3716 
3717  if (v->skip_is_raw)
3718  skipped = get_bits1(gb);
3719  else
3720  skipped = v->s.mbskip_table[mb_pos];
3721  if (!skipped) {
3722  if (v->fourmvswitch)
3723  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3724  else
3725  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3726  switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3727  /* store the motion vector type in a flag (useful later) */
3728  case MV_PMODE_INTFR_4MV:
3729  fourmv = 1;
3730  v->blk_mv_type[s->block_index[0]] = 0;
3731  v->blk_mv_type[s->block_index[1]] = 0;
3732  v->blk_mv_type[s->block_index[2]] = 0;
3733  v->blk_mv_type[s->block_index[3]] = 0;
3734  break;
3736  fourmv = 1;
3737  v->blk_mv_type[s->block_index[0]] = 1;
3738  v->blk_mv_type[s->block_index[1]] = 1;
3739  v->blk_mv_type[s->block_index[2]] = 1;
3740  v->blk_mv_type[s->block_index[3]] = 1;
3741  break;
3743  twomv = 1;
3744  v->blk_mv_type[s->block_index[0]] = 1;
3745  v->blk_mv_type[s->block_index[1]] = 1;
3746  v->blk_mv_type[s->block_index[2]] = 1;
3747  v->blk_mv_type[s->block_index[3]] = 1;
3748  break;
3749  case MV_PMODE_INTFR_1MV:
3750  v->blk_mv_type[s->block_index[0]] = 0;
3751  v->blk_mv_type[s->block_index[1]] = 0;
3752  v->blk_mv_type[s->block_index[2]] = 0;
3753  v->blk_mv_type[s->block_index[3]] = 0;
3754  break;
3755  }
3756  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3757  s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3758  s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3759  s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3760  s->mb_intra = v->is_intra[s->mb_x] = 1;
3761  for (i = 0; i < 6; i++)
3762  v->mb_type[0][s->block_index[i]] = 1;
3763  fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3764  mb_has_coeffs = get_bits1(gb);
3765  if (mb_has_coeffs)
3766  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3767  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3768  GET_MQUANT();
3769  s->current_picture.f.qscale_table[mb_pos] = mquant;
3770  /* Set DC scale - y and c use the same (not sure if necessary here) */
3771  s->y_dc_scale = s->y_dc_scale_table[mquant];
3772  s->c_dc_scale = s->c_dc_scale_table[mquant];
3773  dst_idx = 0;
3774  for (i = 0; i < 6; i++) {
3775  s->dc_val[0][s->block_index[i]] = 0;
3776  dst_idx += i >> 2;
3777  val = ((cbp >> (5 - i)) & 1);
3778  v->mb_type[0][s->block_index[i]] = s->mb_intra;
3779  v->a_avail = v->c_avail = 0;
3780  if (i == 2 || i == 3 || !s->first_slice_line)
3781  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3782  if (i == 1 || i == 3 || s->mb_x)
3783  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3784 
3785  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3786  (i & 4) ? v->codingset2 : v->codingset);
3787  if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3788  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3789  if (i < 4) {
3790  stride_y = s->linesize << fieldtx;
3791  off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3792  } else {
3793  stride_y = s->uvlinesize;
3794  off = 0;
3795  }
3796  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3797  //TODO: loop filter
3798  }
3799 
3800  } else { // inter MB
3801  mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3802  if (mb_has_coeffs)
3803  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3804  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3806  } else {
3807  if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3808  || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3810  }
3811  }
3812  s->mb_intra = v->is_intra[s->mb_x] = 0;
3813  for (i = 0; i < 6; i++)
3814  v->mb_type[0][s->block_index[i]] = 0;
3815  fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3816  /* for all motion vector read MVDATA and motion compensate each block */
3817  dst_idx = 0;
3818  if (fourmv) {
3819  mvbp = v->fourmvbp;
3820  for (i = 0; i < 6; i++) {
3821  if (i < 4) {
3822  dmv_x = dmv_y = 0;
3823  val = ((mvbp >> (3 - i)) & 1);
3824  if (val) {
3825  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3826  }
3827  vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3828  vc1_mc_4mv_luma(v, i, 0);
3829  } else if (i == 4) {
3830  vc1_mc_4mv_chroma4(v);
3831  }
3832  }
3833  } else if (twomv) {
3834  mvbp = v->twomvbp;
3835  dmv_x = dmv_y = 0;
3836  if (mvbp & 2) {
3837  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3838  }
3839  vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3840  vc1_mc_4mv_luma(v, 0, 0);
3841  vc1_mc_4mv_luma(v, 1, 0);
3842  dmv_x = dmv_y = 0;
3843  if (mvbp & 1) {
3844  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3845  }
3846  vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3847  vc1_mc_4mv_luma(v, 2, 0);
3848  vc1_mc_4mv_luma(v, 3, 0);
3849  vc1_mc_4mv_chroma4(v);
3850  } else {
3851  mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3852  dmv_x = dmv_y = 0;
3853  if (mvbp) {
3854  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3855  }
3856  vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3857  vc1_mc_1mv(v, 0);
3858  }
3859  if (cbp)
3860  GET_MQUANT(); // p. 227
3861  s->current_picture.f.qscale_table[mb_pos] = mquant;
3862  if (!v->ttmbf && cbp)
3864  for (i = 0; i < 6; i++) {
3865  s->dc_val[0][s->block_index[i]] = 0;
3866  dst_idx += i >> 2;
3867  val = ((cbp >> (5 - i)) & 1);
3868  if (!fieldtx)
3869  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3870  else
3871  off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3872  if (val) {
3873  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3874  first_block, s->dest[dst_idx] + off,
3875  (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3876  (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3877  block_cbp |= pat << (i << 2);
3878  if (!v->ttmbf && ttmb < 8)
3879  ttmb = -1;
3880  first_block = 0;
3881  }
3882  }
3883  }
3884  } else { // skipped
3885  s->mb_intra = v->is_intra[s->mb_x] = 0;
3886  for (i = 0; i < 6; i++) {
3887  v->mb_type[0][s->block_index[i]] = 0;
3888  s->dc_val[0][s->block_index[i]] = 0;
3889  }
3890  s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3891  s->current_picture.f.qscale_table[mb_pos] = 0;
3892  v->blk_mv_type[s->block_index[0]] = 0;
3893  v->blk_mv_type[s->block_index[1]] = 0;
3894  v->blk_mv_type[s->block_index[2]] = 0;
3895  v->blk_mv_type[s->block_index[3]] = 0;
3896  vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3897  vc1_mc_1mv(v, 0);
3898  }
3899  if (s->mb_x == s->mb_width - 1)
3900  memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3901  return 0;
3902 }
3903 
3905 {
3906  MpegEncContext *s = &v->s;
3907  GetBitContext *gb = &s->gb;
3908  int i;
3909  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3910  int cbp = 0; /* cbp decoding stuff */
3911  int mqdiff, mquant; /* MB quantization */
3912  int ttmb = v->ttfrm; /* MB Transform type */
3913 
3914  int mb_has_coeffs = 1; /* last_flag */
3915  int dmv_x, dmv_y; /* Differential MV components */
3916  int val; /* temp values */
3917  int first_block = 1;
3918  int dst_idx, off;
3919  int pred_flag = 0;
3920  int block_cbp = 0, pat, block_tt = 0;
3921  int idx_mbmode = 0;
3922 
3923  mquant = v->pq; /* Lossy initialization */
3924 
3925  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3926  if (idx_mbmode <= 1) { // intra MB
3927  s->mb_intra = v->is_intra[s->mb_x] = 1;
3928  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3929  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3930  s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3931  GET_MQUANT();
3932  s->current_picture.f.qscale_table[mb_pos] = mquant;
3933  /* Set DC scale - y and c use the same (not sure if necessary here) */
3934  s->y_dc_scale = s->y_dc_scale_table[mquant];
3935  s->c_dc_scale = s->c_dc_scale_table[mquant];
3936  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3937  mb_has_coeffs = idx_mbmode & 1;
3938  if (mb_has_coeffs)
3939  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3940  dst_idx = 0;
3941  for (i = 0; i < 6; i++) {
3942  s->dc_val[0][s->block_index[i]] = 0;
3943  v->mb_type[0][s->block_index[i]] = 1;
3944  dst_idx += i >> 2;
3945  val = ((cbp >> (5 - i)) & 1);
3946  v->a_avail = v->c_avail = 0;
3947  if (i == 2 || i == 3 || !s->first_slice_line)
3948  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3949  if (i == 1 || i == 3 || s->mb_x)
3950  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3951 
3952  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3953  (i & 4) ? v->codingset2 : v->codingset);
3954  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3955  continue;
3956  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3957  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3958  off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3959  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3960  // TODO: loop filter
3961  }
3962  } else {
3963  s->mb_intra = v->is_intra[s->mb_x] = 0;
3964  s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3965  for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3966  if (idx_mbmode <= 5) { // 1-MV
3967  dmv_x = dmv_y = pred_flag = 0;
3968  if (idx_mbmode & 1) {
3969  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3970  }
3971  vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3972  vc1_mc_1mv(v, 0);
3973  mb_has_coeffs = !(idx_mbmode & 2);
3974  } else { // 4-MV
3976  for (i = 0; i < 6; i++) {
3977  if (i < 4) {
3978  dmv_x = dmv_y = pred_flag = 0;
3979  val = ((v->fourmvbp >> (3 - i)) & 1);
3980  if (val) {
3981  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3982  }
3983  vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3984  vc1_mc_4mv_luma(v, i, 0);
3985  } else if (i == 4)
3986  vc1_mc_4mv_chroma(v, 0);
3987  }
3988  mb_has_coeffs = idx_mbmode & 1;
3989  }
3990  if (mb_has_coeffs)
3991  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3992  if (cbp) {
3993  GET_MQUANT();
3994  }
3995  s->current_picture.f.qscale_table[mb_pos] = mquant;
3996  if (!v->ttmbf && cbp) {
3998  }
3999  dst_idx = 0;
4000  for (i = 0; i < 6; i++) {
4001  s->dc_val[0][s->block_index[i]] = 0;
4002  dst_idx += i >> 2;
4003  val = ((cbp >> (5 - i)) & 1);
4004  off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4005  if (v->second_field)
4006  off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4007  if (val) {
4008  pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4009  first_block, s->dest[dst_idx] + off,
4010  (i & 4) ? s->uvlinesize : s->linesize,
4011  (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4012  &block_tt);
4013  block_cbp |= pat << (i << 2);
4014  if (!v->ttmbf && ttmb < 8) ttmb = -1;
4015  first_block = 0;
4016  }
4017  }
4018  }
4019  if (s->mb_x == s->mb_width - 1)
4020  memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4021  return 0;
4022 }
4023 
4024 /** Decode one B-frame MB (in Main profile)
4025  */
4027 {
4028  MpegEncContext *s = &v->s;
4029  GetBitContext *gb = &s->gb;
4030  int i, j;
4031  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4032  int cbp = 0; /* cbp decoding stuff */
4033  int mqdiff, mquant; /* MB quantization */
4034  int ttmb = v->ttfrm; /* MB Transform type */
4035  int mb_has_coeffs = 0; /* last_flag */
4036  int index, index1; /* LUT indexes */
4037  int val, sign; /* temp values */
4038  int first_block = 1;
4039  int dst_idx, off;
4040  int skipped, direct;
4041  int dmv_x[2], dmv_y[2];
4042  int bmvtype = BMV_TYPE_BACKWARD;
4043 
4044  mquant = v->pq; /* lossy initialization */
4045  s->mb_intra = 0;
4046 
4047  if (v->dmb_is_raw)
4048  direct = get_bits1(gb);
4049  else
4050  direct = v->direct_mb_plane[mb_pos];
4051  if (v->skip_is_raw)
4052  skipped = get_bits1(gb);
4053  else
4054  skipped = v->s.mbskip_table[mb_pos];
4055 
4056  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4057  for (i = 0; i < 6; i++) {
4058  v->mb_type[0][s->block_index[i]] = 0;
4059  s->dc_val[0][s->block_index[i]] = 0;
4060  }
4061  s->current_picture.f.qscale_table[mb_pos] = 0;
4062 
4063  if (!direct) {
4064  if (!skipped) {
4065  GET_MVDATA(dmv_x[0], dmv_y[0]);
4066  dmv_x[1] = dmv_x[0];
4067  dmv_y[1] = dmv_y[0];
4068  }
4069  if (skipped || !s->mb_intra) {
4070  bmvtype = decode012(gb);
4071  switch (bmvtype) {
4072  case 0:
4073  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4074  break;
4075  case 1:
4076  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4077  break;
4078  case 2:
4079  bmvtype = BMV_TYPE_INTERPOLATED;
4080  dmv_x[0] = dmv_y[0] = 0;
4081  }
4082  }
4083  }
4084  for (i = 0; i < 6; i++)
4085  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4086 
4087  if (skipped) {
4088  if (direct)
4089  bmvtype = BMV_TYPE_INTERPOLATED;
4090  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4091  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4092  return;
4093  }
4094  if (direct) {
4095  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4096  GET_MQUANT();
4097  s->mb_intra = 0;
4098  s->current_picture.f.qscale_table[mb_pos] = mquant;
4099  if (!v->ttmbf)
4101  dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4102  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4103  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4104  } else {
4105  if (!mb_has_coeffs && !s->mb_intra) {
4106  /* no coded blocks - effectively skipped */
4107  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4108  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4109  return;
4110  }
4111  if (s->mb_intra && !mb_has_coeffs) {
4112  GET_MQUANT();
4113  s->current_picture.f.qscale_table[mb_pos] = mquant;
4114  s->ac_pred = get_bits1(gb);
4115  cbp = 0;
4116  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4117  } else {
4118  if (bmvtype == BMV_TYPE_INTERPOLATED) {
4119  GET_MVDATA(dmv_x[0], dmv_y[0]);
4120  if (!mb_has_coeffs) {
4121  /* interpolated skipped block */
4122  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4123  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4124  return;
4125  }
4126  }
4127  vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4128  if (!s->mb_intra) {
4129  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4130  }
4131  if (s->mb_intra)
4132  s->ac_pred = get_bits1(gb);
4133  cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4134  GET_MQUANT();
4135  s->current_picture.f.qscale_table[mb_pos] = mquant;
4136  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4138  }
4139  }
4140  dst_idx = 0;
4141  for (i = 0; i < 6; i++) {
4142  s->dc_val[0][s->block_index[i]] = 0;
4143  dst_idx += i >> 2;
4144  val = ((cbp >> (5 - i)) & 1);
4145  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4146  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4147  if (s->mb_intra) {
4148  /* check if prediction blocks A and C are available */
4149  v->a_avail = v->c_avail = 0;
4150  if (i == 2 || i == 3 || !s->first_slice_line)
4151  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4152  if (i == 1 || i == 3 || s->mb_x)
4153  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4154 
4155  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4156  (i & 4) ? v->codingset2 : v->codingset);
4157  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4158  continue;
4159  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4160  if (v->rangeredfrm)
4161  for (j = 0; j < 64; j++)
4162  s->block[i][j] <<= 1;
4163  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4164  } else if (val) {
4165  vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4166  first_block, s->dest[dst_idx] + off,
4167  (i & 4) ? s->uvlinesize : s->linesize,
4168  (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4169  if (!v->ttmbf && ttmb < 8)
4170  ttmb = -1;
4171  first_block = 0;
4172  }
4173  }
4174 }
4175 
4176 /** Decode one B-frame MB (in interlaced field B picture)
4177  */
4179 {
4180  MpegEncContext *s = &v->s;
4181  GetBitContext *gb = &s->gb;
4182  int i, j;
4183  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4184  int cbp = 0; /* cbp decoding stuff */
4185  int mqdiff, mquant; /* MB quantization */
4186  int ttmb = v->ttfrm; /* MB Transform type */
4187  int mb_has_coeffs = 0; /* last_flag */
4188  int val; /* temp value */
4189  int first_block = 1;
4190  int dst_idx, off;
4191  int fwd;
4192  int dmv_x[2], dmv_y[2], pred_flag[2];
4193  int bmvtype = BMV_TYPE_BACKWARD;
4194  int idx_mbmode, interpmvp;
4195 
4196  mquant = v->pq; /* Lossy initialization */
4197  s->mb_intra = 0;
4198 
4199  idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4200  if (idx_mbmode <= 1) { // intra MB
4201  s->mb_intra = v->is_intra[s->mb_x] = 1;
4202  s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4203  s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4204  s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4205  GET_MQUANT();
4206  s->current_picture.f.qscale_table[mb_pos] = mquant;
4207  /* Set DC scale - y and c use the same (not sure if necessary here) */
4208  s->y_dc_scale = s->y_dc_scale_table[mquant];
4209  s->c_dc_scale = s->c_dc_scale_table[mquant];
4210  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4211  mb_has_coeffs = idx_mbmode & 1;
4212  if (mb_has_coeffs)
4213  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4214  dst_idx = 0;
4215  for (i = 0; i < 6; i++) {
4216  s->dc_val[0][s->block_index[i]] = 0;
4217  dst_idx += i >> 2;
4218  val = ((cbp >> (5 - i)) & 1);
4219  v->mb_type[0][s->block_index[i]] = s->mb_intra;
4220  v->a_avail = v->c_avail = 0;
4221  if (i == 2 || i == 3 || !s->first_slice_line)
4222  v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4223  if (i == 1 || i == 3 || s->mb_x)
4224  v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4225 
4226  vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4227  (i & 4) ? v->codingset2 : v->codingset);
4228  if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4229  continue;
4230  v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4231  if (v->rangeredfrm)
4232  for (j = 0; j < 64; j++)
4233  s->block[i][j] <<= 1;
4234  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4235  off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4236  s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4237  // TODO: yet to perform loop filter
4238  }
4239  } else {
4240  s->mb_intra = v->is_intra[s->mb_x] = 0;
4241  s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4242  for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4243  if (v->fmb_is_raw)
4244  fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4245  else
4246  fwd = v->forward_mb_plane[mb_pos];
4247  if (idx_mbmode <= 5) { // 1-MV
4248  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4249  pred_flag[0] = pred_flag[1] = 0;
4250  if (fwd)
4251  bmvtype = BMV_TYPE_FORWARD;
4252  else {
4253  bmvtype = decode012(gb);
4254  switch (bmvtype) {
4255  case 0:
4256  bmvtype = BMV_TYPE_BACKWARD;
4257  break;
4258  case 1:
4259  bmvtype = BMV_TYPE_DIRECT;
4260  break;
4261  case 2:
4262  bmvtype = BMV_TYPE_INTERPOLATED;
4263  interpmvp = get_bits1(gb);
4264  }
4265  }
4266  v->bmvtype = bmvtype;
4267  if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4268  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4269  }
4270  if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4271  get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4272  }
4273  if (bmvtype == BMV_TYPE_DIRECT) {
4274  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4275  dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4276  }
4277  vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4278  vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4279  mb_has_coeffs = !(idx_mbmode & 2);
4280  } else { // 4-MV
4281  if (fwd)
4282  bmvtype = BMV_TYPE_FORWARD;
4283  v->bmvtype = bmvtype;
4285  for (i = 0; i < 6; i++) {
4286  if (i < 4) {
4287  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4288  dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4289  val = ((v->fourmvbp >> (3 - i)) & 1);
4290  if (val) {
4291  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4292  &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4293  &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4294  }
4295  vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4296  vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4297  } else if (i == 4)
4298  vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4299  }
4300  mb_has_coeffs = idx_mbmode & 1;
4301  }
4302  if (mb_has_coeffs)
4303  cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4304  if (cbp) {
4305  GET_MQUANT();
4306  }
4307  s->current_picture.f.qscale_table[mb_pos] = mquant;
4308  if (!v->ttmbf && cbp) {
4310  }
4311  dst_idx = 0;
4312  for (i = 0; i < 6; i++) {
4313  s->dc_val[0][s->block_index[i]] = 0;
4314  dst_idx += i >> 2;
4315  val = ((cbp >> (5 - i)) & 1);
4316  off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4317  if (v->second_field)
4318  off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4319  if (val) {
4320  vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4321  first_block, s->dest[dst_idx] + off,
4322  (i & 4) ? s->uvlinesize : s->linesize,
4323  (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4324  if (!v->ttmbf && ttmb < 8)
4325  ttmb = -1;
4326  first_block = 0;
4327  }
4328  }
4329  }
4330 }
4331 
4332 /** Decode blocks of I-frame
4333  */
4335 {
4336  int k, j;
4337  MpegEncContext *s = &v->s;
4338  int cbp, val;
4339  uint8_t *coded_val;
4340  int mb_pos;
4341 
4342  /* select codingmode used for VLC tables selection */
4343  switch (v->y_ac_table_index) {
4344  case 0:
4346  break;
4347  case 1:
4349  break;
4350  case 2:
4352  break;
4353  }
4354 
4355  switch (v->c_ac_table_index) {
4356  case 0:
4358  break;
4359  case 1:
4361  break;
4362  case 2:
4364  break;
4365  }
4366 
4367  /* Set DC scale - y and c use the same */
4368  s->y_dc_scale = s->y_dc_scale_table[v->pq];
4369  s->c_dc_scale = s->c_dc_scale_table[v->pq];
4370 
4371  //do frame decode
4372  s->mb_x = s->mb_y = 0;
4373  s->mb_intra = 1;
4374  s->first_slice_line = 1;
4375  for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4376  s->mb_x = 0;
4378  for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4379  uint8_t *dst[6];
4381  dst[0] = s->dest[0];
4382  dst[1] = dst[0] + 8;
4383  dst[2] = s->dest[0] + s->linesize * 8;
4384  dst[3] = dst[2] + 8;
4385  dst[4] = s->dest[1];
4386  dst[5] = s->dest[2];
4387  s->dsp.clear_blocks(s->block[0]);
4388  mb_pos = s->mb_x + s->mb_y * s->mb_width;
4389  s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4390  s->current_picture.f.qscale_table[mb_pos] = v->pq;
4391  s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4392  s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4393 
4394  // do actual MB decoding and displaying
4396  v->s.ac_pred = get_bits1(&v->s.gb);
4397 
4398  for (k = 0; k < 6; k++) {
4399  val = ((cbp >> (5 - k)) & 1);
4400 
4401  if (k < 4) {
4402  int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4403  val = val ^ pred;
4404  *coded_val = val;
4405  }
4406  cbp |= val << (5 - k);
4407 
4408  vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4409 
4410  if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4411  continue;
4412  v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4413  if (v->pq >= 9 && v->overlap) {
4414  if (v->rangeredfrm)
4415  for (j = 0; j < 64; j++)
4416  s->block[k][j] <<= 1;
4417  s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4418  } else {
4419  if (v->rangeredfrm)
4420  for (j = 0; j < 64; j++)
4421  s->block[k][j] = (s->block[k][j] - 64) << 1;
4422  s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4423  }
4424  }
4425 
4426  if (v->pq >= 9 && v->overlap) {
4427  if (s->mb_x) {
4428  v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4429  v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4430  if (!(s->flags & CODEC_FLAG_GRAY)) {
4431  v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4432  v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4433  }
4434  }
4435  v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4436  v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4437  if (!s->first_slice_line) {
4438  v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4439  v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4440  if (!(s->flags & CODEC_FLAG_GRAY)) {
4441  v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4442  v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4443  }
4444  }
4445  v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4446  v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4447  }
4448  if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4449 
4450  if (get_bits_count(&s->gb) > v->bits) {
4451  ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4452  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4453  get_bits_count(&s->gb), v->bits);
4454  return;
4455  }
4456  }
4457  if (!v->s.loop_filter)
4458  ff_draw_horiz_band(s, s->mb_y * 16, 16);
4459  else if (s->mb_y)
4460  ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4461 
4462  s->first_slice_line = 0;
4463  }
4464  if (v->s.loop_filter)
4465  ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4466 
4467  /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4468  * profile, these only differ are when decoding MSS2 rectangles. */
4469  ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4470 }
4471 
4472 /** Decode blocks of I-frame for advanced profile
4473  */
4475 {
4476  int k;
4477  MpegEncContext *s = &v->s;
4478  int cbp, val;
4479  uint8_t *coded_val;
4480  int mb_pos;
4481  int mquant = v->pq;
4482  int mqdiff;
4483  GetBitContext *gb = &s->gb;
4484 
4485  /* select codingmode used for VLC tables selection */
4486  switch (v->y_ac_table_index) {
4487  case 0:
4489  break;
4490  case 1:
4492  break;
4493  case 2:
4495  break;
4496  }
4497 
4498  switch (v->c_ac_table_index) {
4499  case 0:
4501  break;
4502  case 1:
4504  break;
4505  case 2:
4507  break;
4508  }
4509 
4510  // do frame decode
4511  s->mb_x = s->mb_y = 0;
4512  s->mb_intra = 1;
4513  s->first_slice_line = 1;
4514  s->mb_y = s->start_mb_y;
4515  if (s->start_mb_y) {
4516  s->mb_x = 0;
4518  memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4519  (1 + s->b8_stride) * sizeof(*s->coded_block));
4520  }
4521  for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4522  s->mb_x = 0;
4524  for (;s->mb_x < s->mb_width; s->mb_x++) {
4525  DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4527  s->dsp.clear_blocks(block[0]);
4528  mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4529  s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4530  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4531  s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4532 
4533  // do actual MB decoding and displaying
4534  if (v->fieldtx_is_raw)
4535  v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4537  if ( v->acpred_is_raw)
4538  v->s.ac_pred = get_bits1(&v->s.gb);
4539  else
4540  v->s.ac_pred = v->acpred_plane[mb_pos];
4541 
4542  if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4543  v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4544 
4545  GET_MQUANT();
4546 
4547  s->current_picture.f.qscale_table[mb_pos] = mquant;
4548  /* Set DC scale - y and c use the same */
4549  s->y_dc_scale = s->y_dc_scale_table[mquant];
4550  s->c_dc_scale = s->c_dc_scale_table[mquant];
4551 
4552  for (k = 0; k < 6; k++) {
4553  val = ((cbp >> (5 - k)) & 1);
4554 
4555  if (k < 4) {
4556  int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4557  val = val ^ pred;
4558  *coded_val = val;
4559  }
4560  cbp |= val << (5 - k);
4561 
4562  v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4563  v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4564 
4565  vc1_decode_i_block_adv(v, block[k], k, val,
4566  (k < 4) ? v->codingset : v->codingset2, mquant);
4567 
4568  if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4569  continue;
4571  }
4572 
4576 
4577  if (get_bits_count(&s->gb) > v->bits) {
4578  // TODO: may need modification to handle slice coding
4579  ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4580  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4581  get_bits_count(&s->gb), v->bits);
4582  return;
4583  }
4584  }
4585  if (!v->s.loop_filter)
4586  ff_draw_horiz_band(s, s->mb_y * 16, 16);
4587  else if (s->mb_y)
4588  ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4589  s->first_slice_line = 0;
4590  }
4591 
4592  /* raw bottom MB row */
4593  s->mb_x = 0;
4595  for (;s->mb_x < s->mb_width; s->mb_x++) {
4598  if (v->s.loop_filter)
4600  }
4601  if (v->s.loop_filter)
4602  ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4603  ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4604  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4605 }
4606 
4608 {
4609  MpegEncContext *s = &v->s;
4610  int apply_loop_filter;
4611 
4612  /* select codingmode used for VLC tables selection */
4613  switch (v->c_ac_table_index) {
4614  case 0:
4616  break;
4617  case 1:
4619  break;
4620  case 2:
4622  break;
4623  }
4624 
4625  switch (v->c_ac_table_index) {
4626  case 0:
4628  break;
4629  case 1:
4631  break;
4632  case 2:
4634  break;
4635  }
4636 
4637  apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4638  s->first_slice_line = 1;
4639  memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4640  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4641  s->mb_x = 0;
4643  for (; s->mb_x < s->mb_width; s->mb_x++) {
4645 
4646  if (v->fcm == ILACE_FIELD)
4648  else if (v->fcm == ILACE_FRAME)
4650  else vc1_decode_p_mb(v);
4651  if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4653  if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4654  // TODO: may need modification to handle slice coding
4655  ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4656  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4657  get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4658  return;
4659  }
4660  }
4661  memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4662  memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4663  memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4664  memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4665  if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4666  s->first_slice_line = 0;
4667  }
4668  if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4669  s->mb_x = 0;
4671  for (; s->mb_x < s->mb_width; s->mb_x++) {
4674  }
4675  }
4676  if (s->end_mb_y >= s->start_mb_y)
4677  ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4678  ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4679  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4680 }
4681 
4683 {
4684  MpegEncContext *s = &v->s;
4685 
4686  /* select codingmode used for VLC tables selection */
4687  switch (v->c_ac_table_index) {
4688  case 0:
4690  break;
4691  case 1:
4693  break;
4694  case 2:
4696  break;
4697  }
4698 
4699  switch (v->c_ac_table_index) {
4700  case 0:
4702  break;
4703  case 1:
4705  break;
4706  case 2:
4708  break;
4709  }
4710 
4711  s->first_slice_line = 1;
4712  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4713  s->mb_x = 0;
4715  for (; s->mb_x < s->mb_width; s->mb_x++) {
4717 
4718  if (v->fcm == ILACE_FIELD)
4720  else
4721  vc1_decode_b_mb(v);
4722  if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4723  // TODO: may need modification to handle slice coding
4724  ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4725  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4726  get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4727  return;
4728  }
4729  if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4730  }
4731  if (!v->s.loop_filter)
4732  ff_draw_horiz_band(s, s->mb_y * 16, 16);
4733  else if (s->mb_y)
4734  ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4735  s->first_slice_line = 0;
4736  }
4737  if (v->s.loop_filter)
4738  ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4739  ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4740  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4741 }
4742 
4744 {
4745  MpegEncContext *s = &v->s;
4746 
4747  ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4748  s->first_slice_line = 1;
4749  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4750  s->mb_x = 0;
4753  if (s->last_picture.f.data[0]) {
4754  memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4755  memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4756  memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4757  }
4758  ff_draw_horiz_band(s, s->mb_y * 16, 16);
4759  s->first_slice_line = 0;
4760  }
4762 }
4763 
4765 {
4766 
4767  v->s.esc3_level_length = 0;
4768  if (v->x8_type) {
4769  ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4770  } else {
4771  v->cur_blk_idx = 0;
4772  v->left_blk_idx = -1;
4773  v->topleft_blk_idx = 1;
4774  v->top_blk_idx = 2;
4775  switch (v->s.pict_type) {
4776  case AV_PICTURE_TYPE_I:
4777  if (v->profile == PROFILE_ADVANCED)
4779  else
4781  break;
4782  case AV_PICTURE_TYPE_P:
4783  if (v->p_frame_skipped)
4785  else
4787  break;
4788  case AV_PICTURE_TYPE_B:
4789  if (v->bi_type) {
4790  if (v->profile == PROFILE_ADVANCED)
4792  else
4794  } else
4796  break;
4797  }
4798  }
4799 }
4800 
4801 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4802 
4803 typedef struct {
4804  /**
4805  * Transform coefficients for both sprites in 16.16 fixed point format,
4806  * in the order they appear in the bitstream:
4807  * x scale
4808  * rotation 1 (unused)
4809  * x offset
4810  * rotation 2 (unused)
4811  * y scale
4812  * y offset
4813  * alpha
4814  */
4815  int coefs[2][7];
4816 
4817  int effect_type, effect_flag;
4818  int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4819  int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4820 } SpriteData;
4821 
4822 static inline int get_fp_val(GetBitContext* gb)
4823 {
4824  return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4825 }
4826 
4827 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4828 {
4829  c[1] = c[3] = 0;
4830 
4831  switch (get_bits(gb, 2)) {
4832  case 0:
4833  c[0] = 1 << 16;
4834  c[2] = get_fp_val(gb);
4835  c[4] = 1 << 16;
4836  break;
4837  case 1:
4838  c[0] = c[4] = get_fp_val(gb);
4839  c[2] = get_fp_val(gb);
4840  break;
4841  case 2:
4842  c[0] = get_fp_val(gb);
4843  c[2] = get_fp_val(gb);
4844  c[4] = get_fp_val(gb);
4845  break;
4846  case 3:
4847  c[0] = get_fp_val(gb);
4848  c[1] = get_fp_val(gb);
4849  c[2] = get_fp_val(gb);
4850  c[3] = get_fp_val(gb);
4851  c[4] = get_fp_val(gb);
4852  break;
4853  }
4854  c[5] = get_fp_val(gb);
4855  if (get_bits1(gb))
4856  c[6] = get_fp_val(gb);
4857  else
4858  c[6] = 1 << 16;
4859 }
4860 
4861 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4862 {
4863  AVCodecContext *avctx = v->s.avctx;
4864  int sprite, i;
4865 
4866  for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4867  vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4868  if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4869  av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4870  av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4871  for (i = 0; i < 7; i++)
4872  av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4873  sd->coefs[sprite][i] / (1<<16),
4874  (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4875  av_log(avctx, AV_LOG_DEBUG, "\n");
4876  }
4877 
4878  skip_bits(gb, 2);
4879  if (sd->effect_type = get_bits_long(gb, 30)) {
4880  switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4881  case 7:
4882  vc1_sprite_parse_transform(gb, sd->effect_params1);
4883  break;
4884  case 14:
4885  vc1_sprite_parse_transform(gb, sd->effect_params1);
4886  vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4887  break;
4888  default:
4889  for (i = 0; i < sd->effect_pcount1; i++)
4890  sd->effect_params1[i] = get_fp_val(gb);
4891  }
4892  if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4893  // effect 13 is simple alpha blending and matches the opacity above
4894  av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4895  for (i = 0; i < sd->effect_pcount1; i++)
4896  av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4897  sd->effect_params1[i] / (1 << 16),
4898  (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4899  av_log(avctx, AV_LOG_DEBUG, "\n");
4900  }
4901 
4902  sd->effect_pcount2 = get_bits(gb, 16);
4903  if (sd->effect_pcount2 > 10) {
4904  av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4905  return;
4906  } else if (sd->effect_pcount2) {
4907  i = -1;
4908  av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4909  while (++i < sd->effect_pcount2) {
4910  sd->effect_params2[i] = get_fp_val(gb);
4911  av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4912  sd->effect_params2[i] / (1 << 16),
4913  (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4914  }
4915  av_log(avctx, AV_LOG_DEBUG, "\n");
4916  }
4917  }
4918  if (sd->effect_flag = get_bits1(gb))
4919  av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4920 
4921  if (get_bits_count(gb) >= gb->size_in_bits +
4922  (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4923  av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4924  if (get_bits_count(gb) < gb->size_in_bits - 8)
4925  av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4926 }
4927 
4928 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4929 {
4930  int i, plane, row, sprite;
4931  int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4932  uint8_t* src_h[2][2];
4933  int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4934  int ysub[2];
4935  MpegEncContext *s = &v->s;
4936 
4937  for (i = 0; i < 2; i++) {
4938  xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4939  xadv[i] = sd->coefs[i][0];
4940  if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4941  xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4942 
4943  yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4944  yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4945  }
4946  alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4947 
4948  for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4949  int width = v->output_width>>!!plane;
4950 
4951  for (row = 0; row < v->output_height>>!!plane; row++) {
4952  uint8_t *dst = v->sprite_output_frame.data[plane] +
4953  v->sprite_output_frame.linesize[plane] * row;
4954 
4955  for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4956  uint8_t *iplane = s->current_picture.f.data[plane];
4957  int iline = s->current_picture.f.linesize[plane];
4958  int ycoord = yoff[sprite] + yadv[sprite] * row;
4959  int yline = ycoord >> 16;
4960  int next_line;
4961  ysub[sprite] = ycoord & 0xFFFF;
4962  if (sprite) {
4963  iplane = s->last_picture.f.data[plane];
4964  iline = s->last_picture.f.linesize[plane];
4965  }
4966  next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4967  if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4968  src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4969  if (ysub[sprite])
4970  src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4971  } else {
4972  if (sr_cache[sprite][0] != yline) {
4973  if (sr_cache[sprite][1] == yline) {
4974  FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4975  FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4976  } else {
4977  v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4978  sr_cache[sprite][0] = yline;
4979  }
4980  }
4981  if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4982  v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4983  iplane + next_line, xoff[sprite],
4984  xadv[sprite], width);
4985  sr_cache[sprite][1] = yline + 1;
4986  }
4987  src_h[sprite][0] = v->sr_rows[sprite][0];
4988  src_h[sprite][1] = v->sr_rows[sprite][1];
4989  }
4990  }
4991 
4992  if (!v->two_sprites) {
4993  if (ysub[0]) {
4994  v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4995  } else {
4996  memcpy(dst, src_h[0][0], width);
4997  }
4998  } else {
4999  if (ysub[0] && ysub[1]) {
5000  v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5001  src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5002  } else if (ysub[0]) {
5003  v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5004  src_h[1][0], alpha, width);
5005  } else if (ysub[1]) {
5006  v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5007  src_h[0][0], (1<<16)-1-alpha, width);
5008  } else {
5009  v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5010  }
5011  }
5012  }
5013 
5014  if (!plane) {
5015  for (i = 0; i < 2; i++) {
5016  xoff[i] >>= 1;
5017  yoff[i] >>= 1;
5018  }
5019  }
5020 
5021  }
5022 }
5023 
5024 
5025 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5026 {
5027  MpegEncContext *s = &v->s;
5028  AVCodecContext *avctx = s->avctx;
5029  SpriteData sd;
5030 
5031  vc1_parse_sprites(v, gb, &sd);
5032 
5033  if (!s->current_picture.f.data[0]) {
5034  av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5035  return -1;
5036  }
5037 
5038  if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5039  av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5040  v->two_sprites = 0;
5041  }
5042 
5043  if (v->sprite_output_frame.data[0])
5044  avctx->release_buffer(avctx, &v->sprite_output_frame);
5045 
5048  if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
5049  av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5050  return -1;
5051  }
5052 
5053  vc1_draw_sprites(v, &sd);
5054 
5055  return 0;
5056 }
5057 
5058 static void vc1_sprite_flush(AVCodecContext *avctx)
5059 {
5060  VC1Context *v = avctx->priv_data;
5061  MpegEncContext *s = &v->s;
5062  AVFrame *f = &s->current_picture.f;
5063  int plane, i;
5064 
5065  /* Windows Media Image codecs have a convergence interval of two keyframes.
5066  Since we can't enforce it, clear to black the missing sprite. This is
5067  wrong but it looks better than doing nothing. */
5068 
5069  if (f->data[0])
5070  for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5071  for (i = 0; i < v->sprite_height>>!!plane; i++)
5072  memset(f->data[plane] + i * f->linesize[plane],
5073  plane ? 128 : 0, f->linesize[plane]);
5074 }
5075 
5076 #endif
5077 
5079 {
5080  MpegEncContext *s = &v->s;
5081  int i;
5082 
5083  /* Allocate mb bitplanes */
5088  v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5090 
5091  v->n_allocated_blks = s->mb_width + 2;
5092  v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5093  v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5094  v->cbp = v->cbp_base + s->mb_stride;
5095  v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5096  v->ttblk = v->ttblk_base + s->mb_stride;
5097  v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5098  v->is_intra = v->is_intra_base + s->mb_stride;
5099  v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5100  v->luma_mv = v->luma_mv_base + s->mb_stride;
5101 
5102  /* allocate block type info in that way so it could be used with s->block_index[] */
5103  v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5104  v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5105  v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5106  v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5107 
5108  /* allocate memory to store block level MV info */
5109  v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5110  v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5111  v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5112  v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5113  v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5114  v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5115  v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5116  v->mv_f_last[1] = v->mv_f_last[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5117  v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5118  v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5119  v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5120 
5121  /* Init coded blocks info */
5122  if (v->profile == PROFILE_ADVANCED) {
5123 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5124 // return -1;
5125 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5126 // return -1;
5127  }
5128 
5129  ff_intrax8_common_init(&v->x8,s);
5130 
5132  for (i = 0; i < 4; i++)
5133  if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5134  }
5135 
5136  if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5137  !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5138  !v->mb_type_base)
5139  return -1;
5140 
5141  return 0;
5142 }
5143 
5145 {
5146  int i;
5147  for (i = 0; i < 64; i++) {
5148 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5149  v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5150  v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5151  v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5152  v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5154  }
5155  v->left_blk_sh = 0;
5156  v->top_blk_sh = 3;
5157 }
5158 
5159 /** Initialize a VC1/WMV3 decoder
5160  * @todo TODO: Handle VC-1 IDUs (Transport level?)
5161  * @todo TODO: Decypher remaining bits in extra_data
5162  */
5164 {
5165  VC1Context *v = avctx->priv_data;
5166  MpegEncContext *s = &v->s;
5167  GetBitContext gb;
5168 
5169  /* save the container output size for WMImage */
5170  v->output_width = avctx->width;
5171  v->output_height = avctx->height;
5172 
5173  if (!avctx->extradata_size || !avctx->extradata)
5174  return -1;
5175  if (!(avctx->flags & CODEC_FLAG_GRAY))
5176  avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5177  else
5178  avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5179  avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5180  v->s.avctx = avctx;
5181  avctx->flags |= CODEC_FLAG_EMU_EDGE;
5182  v->s.flags |= CODEC_FLAG_EMU_EDGE;
5183 
5184  if (avctx->idct_algo == FF_IDCT_AUTO) {
5185  avctx->idct_algo = FF_IDCT_WMV2;
5186  }
5187 
5188  if (ff_vc1_init_common(v) < 0)
5189  return -1;
5190  // ensure static VLC tables are initialized
5191  if (ff_msmpeg4_decode_init(avctx) < 0)
5192  return -1;
5194  return -1;
5195  // Hack to ensure the above functions will be called
5196  // again once we know all necessary settings.
5197  // That this is necessary might indicate a bug.
5198  ff_vc1_decode_end(avctx);
5199  ff_vc1dsp_init(&v->vc1dsp);
5200 
5201  if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5202  int count = 0;
5203 
5204  // looks like WMV3 has a sequence header stored in the extradata
5205  // advanced sequence header may be before the first frame
5206  // the last byte of the extradata is a version number, 1 for the
5207  // samples we can decode
5208 
5209  init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5210 
5211  if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5212  return -1;
5213 
5214  count = avctx->extradata_size*8 - get_bits_count(&gb);
5215  if (count > 0) {
5216  av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5217  count, get_bits(&gb, count));
5218  } else if (count < 0) {
5219  av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5220  }
5221  } else { // VC1/WVC1/WVP2
5222  const uint8_t *start = avctx->extradata;
5223  uint8_t *end = avctx->extradata + avctx->extradata_size;
5224  const uint8_t *next;
5225  int size, buf2_size;
5226  uint8_t *buf2 = NULL;
5227  int seq_initialized = 0, ep_initialized = 0;
5228 
5229  if (avctx->extradata_size < 16) {
5230  av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5231  return -1;
5232  }
5233 
5235  start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5236  next = start;
5237  for (; next < end; start = next) {
5238  next = find_next_marker(start + 4, end);
5239  size = next - start - 4;
5240  if (size <= 0)
5241  continue;
5242  buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5243  init_get_bits(&gb, buf2, buf2_size * 8);
5244  switch (AV_RB32(start)) {
5245  case VC1_CODE_SEQHDR:
5246  if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5247  av_free(buf2);
5248  return -1;
5249  }
5250  seq_initialized = 1;
5251  break;
5252  case VC1_CODE_ENTRYPOINT:
5253  if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5254  av_free(buf2);
5255  return -1;
5256  }
5257  ep_initialized = 1;
5258  break;
5259  }
5260  }
5261  av_free(buf2);
5262  if (!seq_initialized || !ep_initialized) {
5263  av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5264  return -1;
5265  }
5266  v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5267  }
5268 
5269  avctx->profile = v->profile;
5270  if (v->profile == PROFILE_ADVANCED)
5271  avctx->level = v->level;
5272 
5273  avctx->has_b_frames = !!avctx->max_b_frames;
5274 
5275  s->mb_width = (avctx->coded_width + 15) >> 4;
5276  s->mb_height = (avctx->coded_height + 15) >> 4;
5277 
5278  if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5280  } else {
5281  memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5282  v->left_blk_sh = 3;
5283  v->top_blk_sh = 0;
5284  }
5285 
5286  if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5287  v->sprite_width = avctx->coded_width;
5288  v->sprite_height = avctx->coded_height;
5289 
5290  avctx->coded_width = avctx->width = v->output_width;
5291  avctx->coded_height = avctx->height = v->output_height;
5292 
5293  // prevent 16.16 overflows
5294  if (v->sprite_width > 1 << 14 ||
5295  v->sprite_height > 1 << 14 ||
5296  v->output_width > 1 << 14 ||
5297  v->output_height > 1 << 14) return -1;
5298  }
5299  return 0;
5300 }
5301 
5302 /** Close a VC1/WMV3 decoder
5303  * @warning Initial try at using MpegEncContext stuff
5304  */
5306 {
5307  VC1Context *v = avctx->priv_data;
5308  int i;
5309 
5310  if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5311  && v->sprite_output_frame.data[0])
5312  avctx->release_buffer(avctx, &v->sprite_output_frame);
5313  for (i = 0; i < 4; i++)
5314  av_freep(&v->sr_rows[i >> 1][i & 1]);
5315  av_freep(&v->hrd_rate);
5316  av_freep(&v->hrd_buffer);
5317  ff_MPV_common_end(&v->s);
5321  av_freep(&v->fieldtx_plane);
5322  av_freep(&v->acpred_plane);
5324  av_freep(&v->mb_type_base);
5326  av_freep(&v->mv_f_base);
5327  av_freep(&v->mv_f_last_base);
5328  av_freep(&v->mv_f_next_base);
5329  av_freep(&v->block);
5330  av_freep(&v->cbp_base);
5331  av_freep(&v->ttblk_base);
5332  av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5333  av_freep(&v->luma_mv_base);
5335  return 0;
5336 }
5337 
5338 
5339 /** Decode a VC1/WMV3 frame
5340  * @todo TODO: Handle VC-1 IDUs (Transport level?)
5341  */
5342 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5343  int *got_frame, AVPacket *avpkt)
5344 {
5345  const uint8_t *buf = avpkt->data;
5346  int buf_size = avpkt->size, n_slices = 0, i;
5347  VC1Context *v = avctx->priv_data;
5348  MpegEncContext *s = &v->s;
5349  AVFrame *pict = data;
5350  uint8_t *buf2 = NULL;
5351  const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5352  int mb_height, n_slices1=-1;
5353  struct {
5354  uint8_t *buf;
5355  GetBitContext gb;
5356  int mby_start;
5357  } *slices = NULL, *tmp;
5358 
5359  v->second_field = 0;
5360 
5361  if(s->flags & CODEC_FLAG_LOW_DELAY)
5362  s->low_delay = 1;
5363 
5364  /* no supplementary picture */
5365  if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5366  /* special case for last picture */
5367  if (s->low_delay == 0 && s->next_picture_ptr) {
5368  *pict = s->next_picture_ptr->f;
5369  s->next_picture_ptr = NULL;
5370 
5371  *got_frame = 1;
5372  }
5373 
5374  return buf_size;
5375  }
5376 
5378  if (v->profile < PROFILE_ADVANCED)
5379  avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5380  else
5381  avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5382  }
5383 
5384  //for advanced profile we may need to parse and unescape data
5385  if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5386  int buf_size2 = 0;
5387  buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5388 
5389  if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5390  const uint8_t *start, *end, *next;
5391  int size;
5392 
5393  next = buf;
5394  for (start = buf, end = buf + buf_size; next < end; start = next) {
5395  next = find_next_marker(start + 4, end);
5396  size = next - start - 4;
5397  if (size <= 0) continue;
5398  switch (AV_RB32(start)) {
5399  case VC1_CODE_FRAME:
5400  if (avctx->hwaccel ||
5402  buf_start = start;
5403  buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5404  break;
5405  case VC1_CODE_FIELD: {
5406  int buf_size3;
5407  if (avctx->hwaccel ||
5409  buf_start_second_field = start;
5410  tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5411  if (!tmp)
5412  goto err;
5413  slices = tmp;
5414  slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5415  if (!slices[n_slices].buf)
5416  goto err;
5417  buf_size3 = vc1_unescape_buffer(start + 4, size,
5418  slices[n_slices].buf);
5419  init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5420  buf_size3 << 3);
5421  /* assuming that the field marker is at the exact middle,
5422  hope it's correct */
5423  slices[n_slices].mby_start = s->mb_height >> 1;
5424  n_slices1 = n_slices - 1; // index of the last slice of the first field
5425  n_slices++;
5426  break;
5427  }
5428  case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5429  buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5430  init_get_bits(&s->gb, buf2, buf_size2 * 8);
5431  ff_vc1_decode_entry_point(avctx, v, &s->gb);
5432  break;
5433  case VC1_CODE_SLICE: {
5434  int buf_size3;
5435  tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5436  if (!tmp)
5437  goto err;
5438  slices = tmp;
5439  slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5440  if (!slices[n_slices].buf)
5441  goto err;
5442  buf_size3 = vc1_unescape_buffer(start + 4, size,
5443  slices[n_slices].buf);
5444  init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5445  buf_size3 << 3);
5446  slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5447  n_slices++;
5448  break;
5449  }
5450  }
5451  }
5452  } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5453  const uint8_t *divider;
5454  int buf_size3;
5455 
5456  divider = find_next_marker(buf, buf + buf_size);
5457  if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5458  av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5459  goto err;
5460  } else { // found field marker, unescape second field
5461  if (avctx->hwaccel ||
5463  buf_start_second_field = divider;
5464  tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5465  if (!tmp)
5466  goto err;
5467  slices = tmp;
5468  slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5469  if (!slices[n_slices].buf)
5470  goto err;
5471  buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5472  init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5473  buf_size3 << 3);
5474  slices[n_slices].mby_start = s->mb_height >> 1;
5475  n_slices1 = n_slices - 1;
5476  n_slices++;
5477  }
5478  buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5479  } else {
5480  buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5481  }
5482  init_get_bits(&s->gb, buf2, buf_size2*8);
5483  } else
5484  init_get_bits(&s->gb, buf, buf_size*8);
5485 
5486  if (v->res_sprite) {
5487  v->new_sprite = !get_bits1(&s->gb);
5488  v->two_sprites = get_bits1(&s->gb);
5489  /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5490  we're using the sprite compositor. These are intentionally kept separate
5491  so you can get the raw sprites by using the wmv3 decoder for WMVP or
5492  the vc1 one for WVP2 */
5493  if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5494  if (v->new_sprite) {
5495  // switch AVCodecContext parameters to those of the sprites
5496  avctx->width = avctx->coded_width = v->sprite_width;
5497  avctx->height = avctx->coded_height = v->sprite_height;
5498  } else {
5499  goto image;
5500  }
5501  }
5502  }
5503 
5504  if (s->context_initialized &&
5505  (s->width != avctx->coded_width ||
5506  s->height != avctx->coded_height)) {
5507  ff_vc1_decode_end(avctx);
5508  }
5509 
5510  if (!s->context_initialized) {
5512  goto err;
5513 
5514  s->low_delay = !avctx->has_b_frames || v->res_sprite;
5515 
5516  if (v->profile == PROFILE_ADVANCED) {
5517  if(avctx->coded_width<=1 || avctx->coded_height<=1)
5518  goto err;
5519  s->h_edge_pos = avctx->coded_width;
5520  s->v_edge_pos = avctx->coded_height;
5521  }
5522  }
5523 
5524  /* We need to set current_picture_ptr before reading the header,
5525  * otherwise we cannot store anything in there. */
5526  if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5527  int i = ff_find_unused_picture(s, 0);
5528  if (i < 0)
5529  goto err;
5530  s->current_picture_ptr = &s->picture[i];
5531  }
5532 
5533  // do parse frame header
5534  v->pic_header_flag = 0;
5535  v->first_pic_header_flag = 1;
5536  if (v->profile < PROFILE_ADVANCED) {
5537  if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5538  goto err;
5539  }
5540  } else {
5541  if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5542  goto err;
5543  }
5544  }
5545  v->first_pic_header_flag = 0;
5546 
5547  if (avctx->debug & FF_DEBUG_PICT_INFO)
5548  av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5549 
5550  if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5551  && s->pict_type != AV_PICTURE_TYPE_I) {
5552  av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5553  goto err;
5554  }
5555 
5556  if ((s->mb_height >> v->field_mode) == 0) {
5557  av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5558  goto err;
5559  }
5560 
5561  // process pulldown flags
5563  // Pulldown flags are only valid when 'broadcast' has been set.
5564  // So ticks_per_frame will be 2
5565  if (v->rff) {
5566  // repeat field
5568  } else if (v->rptfrm) {
5569  // repeat frames
5570  s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5571  }
5572 
5573  // for skipping the frame
5576 
5577  /* skip B-frames if we don't have reference frames */
5578  if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5579  goto err;
5580  }
5581  if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5582  (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5583  avctx->skip_frame >= AVDISCARD_ALL) {
5584  goto end;
5585  }
5586 
5587  if (s->next_p_frame_damaged) {
5588  if (s->pict_type == AV_PICTURE_TYPE_B)
5589  goto end;
5590  else
5591  s->next_p_frame_damaged = 0;
5592  }
5593 
5594  if (ff_MPV_frame_start(s, avctx) < 0) {
5595  goto err;
5596  }
5597 
5600 
5603 
5604  if ((CONFIG_VC1_VDPAU_DECODER)
5606  ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5607  else if (avctx->hwaccel) {
5608  if (v->field_mode && buf_start_second_field) {
5609  // decode first field
5611  if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5612  goto err;
5613  if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5614  goto err;
5615  if (avctx->hwaccel->end_frame(avctx) < 0)
5616  goto err;
5617 
5618  // decode second field
5619  s->gb = slices[n_slices1 + 1].gb;
5621  v->second_field = 1;
5622  v->pic_header_flag = 0;
5623  if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5624  av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5625  goto err;
5626  }
5628 
5629  if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5630  goto err;
5631  if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5632  goto err;
5633  if (avctx->hwaccel->end_frame(avctx) < 0)
5634  goto err;
5635  } else {
5637  if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5638  goto err;
5639  if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5640  goto err;
5641  if (avctx->hwaccel->end_frame(avctx) < 0)
5642  goto err;
5643  }
5644  } else {
5645  if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5646  goto err; // This codepath is still incomplete thus it is disabled
5647 
5648  ff_er_frame_start(s);
5649 
5650  v->bits = buf_size * 8;
5651  v->end_mb_x = s->mb_width;
5652  if (v->field_mode) {
5653  uint8_t *tmp[2];
5654  s->current_picture.f.linesize[0] <<= 1;
5655  s->current_picture.f.linesize[1] <<= 1;
5656  s->current_picture.f.linesize[2] <<= 1;
5657  s->linesize <<= 1;
5658  s->uvlinesize <<= 1;
5659  tmp[0] = v->mv_f_last[0];
5660  tmp[1] = v->mv_f_last[1];
5661  v->mv_f_last[0] = v->mv_f_next[0];
5662  v->mv_f_last[1] = v->mv_f_next[1];
5663  v->mv_f_next[0] = v->mv_f[0];
5664  v->mv_f_next[1] = v->mv_f[1];
5665  v->mv_f[0] = tmp[0];
5666  v->mv_f[1] = tmp[1];
5667  }
5668  mb_height = s->mb_height >> v->field_mode;
5669  for (i = 0; i <= n_slices; i++) {
5670  if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5671  if (v->field_mode <= 0) {
5672  av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5673  "picture boundary (%d >= %d)\n", i,
5674  slices[i - 1].mby_start, mb_height);
5675  continue;
5676  }
5677  v->second_field = 1;
5678  v->blocks_off = s->mb_width * s->mb_height << 1;
5679  v->mb_off = s->mb_stride * s->mb_height >> 1;
5680  } else {
5681  v->second_field = 0;
5682  v->blocks_off = 0;
5683  v->mb_off = 0;
5684  }
5685  if (i) {
5686  v->pic_header_flag = 0;
5687  if (v->field_mode && i == n_slices1 + 2) {
5688  if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5689  av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5690  continue;
5691  }
5692  } else if (get_bits1(&s->gb)) {
5693  v->pic_header_flag = 1;
5694  if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5695  av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5696  continue;
5697  }
5698  }
5699  }
5700  s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5701  if (!v->field_mode || v->second_field)
5702  s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5703  else {
5704  if (i >= n_slices) {
5705  av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5706  continue;
5707  }
5708  s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5709  }
5710  if (s->end_mb_y <= s->start_mb_y) {
5711  av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5712  continue;
5713  }
5715  if (i != n_slices)
5716  s->gb = slices[i].gb;
5717  }
5718  if (v->field_mode) {
5719  v->second_field = 0;
5720  if (s->pict_type == AV_PICTURE_TYPE_B) {
5721  memcpy(v->mv_f_base, v->mv_f_next_base,
5722  2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5723  }
5724  s->current_picture.f.linesize[0] >>= 1;
5725  s->current_picture.f.linesize[1] >>= 1;
5726  s->current_picture.f.linesize[2] >>= 1;
5727  s->linesize >>= 1;
5728  s->uvlinesize >>= 1;
5729  }
5730  av_dlog(s->avctx, "Consumed %i/%i bits\n",
5731  get_bits_count(&s->gb), s->gb.size_in_bits);
5732 // if (get_bits_count(&s->gb) > buf_size * 8)
5733 // return -1;
5735  goto err;
5736  if(!v->field_mode)
5737  ff_er_frame_end(s);
5738  }
5739 
5740  ff_MPV_frame_end(s);
5741 
5742  if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5743 image:
5744  avctx->width = avctx->coded_width = v->output_width;
5745  avctx->height = avctx->coded_height = v->output_height;
5746  if (avctx->skip_frame >= AVDISCARD_NONREF)
5747  goto end;
5748 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5749  if (vc1_decode_sprites(v, &s->gb))
5750  goto err;
5751 #endif
5752  *pict = v->sprite_output_frame;
5753  *got_frame = 1;
5754  } else {
5755  if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5756  *pict = s->current_picture_ptr->f;
5757  } else if (s->last_picture_ptr != NULL) {
5758  *pict = s->last_picture_ptr->f;
5759  }
5760  if (s->last_picture_ptr || s->low_delay) {
5761  *got_frame = 1;
5762  ff_print_debug_info(s, pict);
5763  }
5764  }
5765 
5766 end:
5767  av_free(buf2);
5768  for (i = 0; i < n_slices; i++)
5769  av_free(slices[i].buf);
5770  av_free(slices);
5771  return buf_size;
5772 
5773 err:
5774  av_free(buf2);
5775  for (i = 0; i < n_slices; i++)
5776  av_free(slices[i].buf);
5777  av_free(slices);
5778  return -1;
5779 }
5780 
5781 
5782 static const AVProfile profiles[] = {
5783  { FF_PROFILE_VC1_SIMPLE, "Simple" },
5784  { FF_PROFILE_VC1_MAIN, "Main" },
5785  { FF_PROFILE_VC1_COMPLEX, "Complex" },
5786  { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5787  { FF_PROFILE_UNKNOWN },
5788 };
5789 
5791  .name = "vc1",
5792  .type = AVMEDIA_TYPE_VIDEO,
5793  .id = AV_CODEC_ID_VC1,
5794  .priv_data_size = sizeof(VC1Context),
5795  .init = vc1_decode_init,
5798  .flush = ff_mpeg_flush,
5799  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5800  .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5801  .pix_fmts = ff_hwaccel_pixfmt_list_420,
5802  .profiles = NULL_IF_CONFIG_SMALL(profiles)
5803 };
5804 
5805 #if CONFIG_WMV3_DECODER
5806 AVCodec ff_wmv3_decoder = {
5807  .name = "wmv3",
5808  .type = AVMEDIA_TYPE_VIDEO,
5809  .id = AV_CODEC_ID_WMV3,
5810  .priv_data_size = sizeof(VC1Context),
5811  .init = vc1_decode_init,
5814  .flush = ff_mpeg_flush,
5815  .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5816  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5817  .pix_fmts = ff_hwaccel_pixfmt_list_420,
5818  .profiles = NULL_IF_CONFIG_SMALL(profiles)
5819 };
5820 #endif
5821 
5822 #if CONFIG_WMV3_VDPAU_DECODER
5823 AVCodec ff_wmv3_vdpau_decoder = {
5824  .name = "wmv3_vdpau",
5825  .type = AVMEDIA_TYPE_VIDEO,
5826  .id = AV_CODEC_ID_WMV3,
5827  .priv_data_size = sizeof(VC1Context),
5828  .init = vc1_decode_init,
5832  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5833  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5834  .profiles = NULL_IF_CONFIG_SMALL(profiles)
5835 };
5836 #endif
5837 
5838 #if CONFIG_VC1_VDPAU_DECODER
5839 AVCodec ff_vc1_vdpau_decoder = {
5840  .name = "vc1_vdpau",
5841  .type = AVMEDIA_TYPE_VIDEO,
5842  .id = AV_CODEC_ID_VC1,
5843  .priv_data_size = sizeof(VC1Context),
5844  .init = vc1_decode_init,
5848  .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5849  .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5850  .profiles = NULL_IF_CONFIG_SMALL(profiles)
5851 };
5852 #endif
5853 
5854 #if CONFIG_WMV3IMAGE_DECODER
5855 AVCodec ff_wmv3image_decoder = {
5856  .name = "wmv3image",
5857  .type = AVMEDIA_TYPE_VIDEO,
5858  .id = AV_CODEC_ID_WMV3IMAGE,
5859  .priv_data_size = sizeof(VC1Context),
5860  .init = vc1_decode_init,
5863  .capabilities = CODEC_CAP_DR1,
5864  .flush = vc1_sprite_flush,
5865  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5866  .pix_fmts = ff_pixfmt_list_420
5867 };
5868 #endif
5869 
5870 #if CONFIG_VC1IMAGE_DECODER
5871 AVCodec ff_vc1image_decoder = {
5872  .name = "vc1image",
5873  .type = AVMEDIA_TYPE_VIDEO,
5874  .id = AV_CODEC_ID_VC1IMAGE,
5875  .priv_data_size = sizeof(VC1Context),
5876  .init = vc1_decode_init,
5879  .capabilities = CODEC_CAP_DR1,
5880  .flush = vc1_sprite_flush,
5881  .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5882  .pix_fmts = ff_pixfmt_list_420
5883 };
5884 #endif