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