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