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