FFmpeg
vc1_block.c
Go to the documentation of this file.
1 /*
2  * VC-1 and WMV3 decoder
3  * Copyright (c) 2011 Mashiat Sarker Shakkhar
4  * Copyright (c) 2006-2007 Konstantin Shishkov
5  * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6  *
7  * This file is part of FFmpeg.
8  *
9  * FFmpeg is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * FFmpeg is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with FFmpeg; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22  */
23 
24 /**
25  * @file
26  * VC-1 and WMV3 block decoding routines
27  */
28 
29 #include "avcodec.h"
30 #include "mpegutils.h"
31 #include "mpegvideo.h"
32 #include "mpegvideodec.h"
33 #include "msmpeg4_vc1_data.h"
34 #include "unary.h"
35 #include "vc1.h"
36 #include "vc1_pred.h"
37 #include "vc1acdata.h"
38 #include "vc1data.h"
39 
40 // offset tables for interlaced picture MVDATA decoding
41 static const uint8_t offset_table[2][9] = {
42  { 0, 1, 2, 4, 8, 16, 32, 64, 128 },
43  { 0, 1, 3, 7, 15, 31, 63, 127, 255 },
44 };
45 
46 // mapping table for internal block representation
47 static const int block_map[6] = {0, 2, 1, 3, 4, 5};
48 
49 /***********************************************************************/
50 /**
51  * @name VC-1 Bitplane decoding
52  * @see 8.7, p56
53  * @{
54  */
55 
56 
57 static inline void init_block_index(VC1Context *v)
58 {
59  MpegEncContext *s = &v->s;
61  if (v->field_mode && !(v->second_field ^ v->tff)) {
62  s->dest[0] += s->cur_pic.ptr->f->linesize[0];
63  s->dest[1] += s->cur_pic.ptr->f->linesize[1];
64  s->dest[2] += s->cur_pic.ptr->f->linesize[2];
65  }
66 }
67 
68 static inline void update_block_index(MpegEncContext *s)
69 {
70  /* VC1 is always 420 except when using AV_CODEC_FLAG_GRAY
71  * (or a HWAccel). Shall we inline this value? */
72  ff_update_block_index(s, 8, 0, s->chroma_x_shift);
73 }
74 
75 /** @} */ //Bitplane group
76 
77 static void vc1_put_blocks_clamped(VC1Context *v, int put_signed)
78 {
79  MpegEncContext *s = &v->s;
80  uint8_t *dest;
81  int block_count = CONFIG_GRAY && (s->avctx->flags & AV_CODEC_FLAG_GRAY) ? 4 : 6;
82  int fieldtx = 0;
83  int i;
84 
85  /* The put pixels loop is one MB row and one MB column behind the decoding
86  * loop because we can only put pixels when overlap filtering is done. For
87  * interlaced frame pictures, however, the put pixels loop is only one
88  * column behind the decoding loop as interlaced frame pictures only need
89  * horizontal overlap filtering. */
90  if (!s->first_slice_line && v->fcm != ILACE_FRAME) {
91  if (s->mb_x) {
92  for (i = 0; i < block_count; i++) {
93  if (i > 3 ? v->mb_type[s->block_index[i] - s->block_wrap[i] - 1] :
94  v->mb_type[s->block_index[i] - 2 * s->block_wrap[i] - 2]) {
95  dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + ((i & 1) - 2) * 8;
96  if (put_signed)
97  s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][block_map[i]],
98  i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize - 8 : dest,
99  i > 3 ? s->uvlinesize : s->linesize);
100  else
101  s->idsp.put_pixels_clamped(v->block[v->topleft_blk_idx][block_map[i]],
102  i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize - 8 : dest,
103  i > 3 ? s->uvlinesize : s->linesize);
104  }
105  }
106  }
107  if (s->mb_x == v->end_mb_x - 1) {
108  for (i = 0; i < block_count; i++) {
109  if (i > 3 ? v->mb_type[s->block_index[i] - s->block_wrap[i]] :
110  v->mb_type[s->block_index[i] - 2 * s->block_wrap[i]]) {
111  dest = s->dest[0] + ((i & 2) - 4) * 4 * s->linesize + (i & 1) * 8;
112  if (put_signed)
113  s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][block_map[i]],
114  i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize : dest,
115  i > 3 ? s->uvlinesize : s->linesize);
116  else
117  s->idsp.put_pixels_clamped(v->block[v->top_blk_idx][block_map[i]],
118  i > 3 ? s->dest[i - 3] - 8 * s->uvlinesize : dest,
119  i > 3 ? s->uvlinesize : s->linesize);
120  }
121  }
122  }
123  }
124  if (s->mb_y == s->end_mb_y - 1 || v->fcm == ILACE_FRAME) {
125  if (s->mb_x) {
126  if (v->fcm == ILACE_FRAME)
127  fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x - 1];
128  for (i = 0; i < block_count; i++) {
129  if (i > 3 ? v->mb_type[s->block_index[i] - 1] :
130  v->mb_type[s->block_index[i] - 2]) {
131  if (fieldtx)
132  dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + ((i & 1) - 2) * 8;
133  else
134  dest = s->dest[0] + (i & 2) * 4 * s->linesize + ((i & 1) - 2) * 8;
135  if (put_signed)
136  s->idsp.put_signed_pixels_clamped(v->block[v->left_blk_idx][block_map[i]],
137  i > 3 ? s->dest[i - 3] - 8 : dest,
138  i > 3 ? s->uvlinesize : s->linesize << fieldtx);
139  else
140  s->idsp.put_pixels_clamped(v->block[v->left_blk_idx][block_map[i]],
141  i > 3 ? s->dest[i - 3] - 8 : dest,
142  i > 3 ? s->uvlinesize : s->linesize << fieldtx);
143  }
144  }
145  }
146  if (s->mb_x == v->end_mb_x - 1) {
147  if (v->fcm == ILACE_FRAME)
148  fieldtx = v->fieldtx_plane[s->mb_y * s->mb_stride + s->mb_x];
149  for (i = 0; i < block_count; i++) {
150  if (v->mb_type[s->block_index[i]]) {
151  if (fieldtx)
152  dest = s->dest[0] + ((i & 2) >> 1) * s->linesize + (i & 1) * 8;
153  else
154  dest = s->dest[0] + (i & 2) * 4 * s->linesize + (i & 1) * 8;
155  if (put_signed)
156  s->idsp.put_signed_pixels_clamped(v->block[v->cur_blk_idx][block_map[i]],
157  i > 3 ? s->dest[i - 3] : dest,
158  i > 3 ? s->uvlinesize : s->linesize << fieldtx);
159  else
160  s->idsp.put_pixels_clamped(v->block[v->cur_blk_idx][block_map[i]],
161  i > 3 ? s->dest[i - 3] : dest,
162  i > 3 ? s->uvlinesize : s->linesize << fieldtx);
163  }
164  }
165  }
166  }
167 }
168 
169 #define inc_blk_idx(idx) do { \
170  idx++; \
171  if (idx >= v->n_allocated_blks) \
172  idx = 0; \
173  } while (0)
174 
175 /***********************************************************************/
176 /**
177  * @name VC-1 Block-level functions
178  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
179  * @{
180  */
181 
182 /**
183  * @def GET_MQUANT
184  * @brief Get macroblock-level quantizer scale
185  */
186 #define GET_MQUANT() \
187  if (v->dquantfrm) { \
188  int edges = 0; \
189  if (v->dqprofile == DQPROFILE_ALL_MBS) { \
190  if (v->dqbilevel) { \
191  mquant = (get_bits1(gb)) ? -v->altpq : v->pq; \
192  } else { \
193  mqdiff = get_bits(gb, 3); \
194  if (mqdiff != 7) \
195  mquant = -v->pq - mqdiff; \
196  else \
197  mquant = -get_bits(gb, 5); \
198  } \
199  } \
200  if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
201  edges = 1 << v->dqsbedge; \
202  else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
203  edges = (3 << v->dqsbedge) % 15; \
204  else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
205  edges = 15; \
206  if ((edges&1) && !s->mb_x) \
207  mquant = -v->altpq; \
208  if ((edges&2) && !s->mb_y) \
209  mquant = -v->altpq; \
210  if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
211  mquant = -v->altpq; \
212  if ((edges&8) && \
213  s->mb_y == ((s->mb_height >> v->field_mode) - 1)) \
214  mquant = -v->altpq; \
215  if (!mquant || mquant > 31 || mquant < -31) { \
216  av_log(v->s.avctx, AV_LOG_ERROR, \
217  "Overriding invalid mquant %d\n", mquant); \
218  mquant = 1; \
219  } \
220  }
221 
222 /**
223  * @def GET_MVDATA(_dmv_x, _dmv_y)
224  * @brief Get MV differentials
225  * @see MVDATA decoding from 8.3.5.2, p(1)20
226  * @param _dmv_x Horizontal differential for decoded MV
227  * @param _dmv_y Vertical differential for decoded MV
228  */
229 #define GET_MVDATA(_dmv_x, _dmv_y) \
230  index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[v->mv_table_index], \
231  VC1_MV_DIFF_VLC_BITS, 2); \
232  if (index > 36) { \
233  mb_has_coeffs = 1; \
234  index -= 37; \
235  } else \
236  mb_has_coeffs = 0; \
237  s->mb_intra = 0; \
238  if (!index) { \
239  _dmv_x = _dmv_y = 0; \
240  } else if (index == 35) { \
241  _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
242  _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
243  } else if (index == 36) { \
244  _dmv_x = 0; \
245  _dmv_y = 0; \
246  s->mb_intra = 1; \
247  } else { \
248  index1 = index % 6; \
249  _dmv_x = offset_table[1][index1]; \
250  val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
251  if (val > 0) { \
252  val = get_bits(gb, val); \
253  sign = 0 - (val & 1); \
254  _dmv_x = (sign ^ ((val >> 1) + _dmv_x)) - sign; \
255  } \
256  \
257  index1 = index / 6; \
258  _dmv_y = offset_table[1][index1]; \
259  val = size_table[index1] - (!s->quarter_sample && index1 == 5); \
260  if (val > 0) { \
261  val = get_bits(gb, val); \
262  sign = 0 - (val & 1); \
263  _dmv_y = (sign ^ ((val >> 1) + _dmv_y)) - sign; \
264  } \
265  }
266 
268  int *dmv_y, int *pred_flag)
269 {
270  int index, index1;
271  int extend_x, extend_y;
272  GetBitContext *const gb = &v->gb;
273  int bits, esc;
274  int val, sign;
275 
276  if (v->numref) {
278  esc = 125;
279  } else {
281  esc = 71;
282  }
283  extend_x = v->dmvrange & 1;
284  extend_y = (v->dmvrange >> 1) & 1;
285  index = get_vlc2(gb, v->imv_vlc, bits, 3);
286  if (index == esc) {
287  *dmv_x = get_bits(gb, v->k_x);
288  *dmv_y = get_bits(gb, v->k_y);
289  if (v->numref) {
290  if (pred_flag)
291  *pred_flag = *dmv_y & 1;
292  *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
293  }
294  }
295  else {
296  av_assert0(index < esc);
297  index1 = (index + 1) % 9;
298  if (index1 != 0) {
299  val = get_bits(gb, index1 + extend_x);
300  sign = 0 - (val & 1);
301  *dmv_x = (sign ^ ((val >> 1) + offset_table[extend_x][index1])) - sign;
302  } else
303  *dmv_x = 0;
304  index1 = (index + 1) / 9;
305  if (index1 > v->numref) {
306  val = get_bits(gb, (index1 >> v->numref) + extend_y);
307  sign = 0 - (val & 1);
308  *dmv_y = (sign ^ ((val >> 1) + offset_table[extend_y][index1 >> v->numref])) - sign;
309  } else
310  *dmv_y = 0;
311  if (v->numref && pred_flag)
312  *pred_flag = index1 & 1;
313  }
314 }
315 
316 /** Reconstruct motion vector for B-frame and do motion compensation
317  */
318 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
319  int direct, int mode)
320 {
321  if (direct) {
322  ff_vc1_mc_1mv(v, 0);
323  ff_vc1_interp_mc(v);
324  return;
325  }
326  if (mode == BMV_TYPE_INTERPOLATED) {
327  ff_vc1_mc_1mv(v, 0);
328  ff_vc1_interp_mc(v);
329  return;
330  }
331 
333 }
334 
335 /** Get predicted DC value for I-frames only
336  * prediction dir: left=0, top=1
337  * @param s MpegEncContext
338  * @param overlap flag indicating that overlap filtering is used
339  * @param pq integer part of picture quantizer
340  * @param[in] n block index in the current MB
341  * @param dc_val_ptr Pointer to DC predictor
342  * @param dir_ptr Prediction direction for use in AC prediction
343  */
344 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
345  int16_t **dc_val_ptr, int *dir_ptr)
346 {
347  int a, b, c, wrap, pred, scale;
348  int16_t *dc_val;
349  static const uint16_t dcpred[32] = {
350  -1, 1024, 512, 341, 256, 205, 171, 146, 128,
351  114, 102, 93, 85, 79, 73, 68, 64,
352  60, 57, 54, 51, 49, 47, 45, 43,
353  41, 39, 38, 37, 35, 34, 33
354  };
355 
356  /* find prediction - wmv3_dc_scale always used here in fact */
357  scale = s->y_dc_scale;
358 
359  wrap = s->block_wrap[n];
360  dc_val = s->dc_val + s->block_index[n];
361 
362  /* B A
363  * C X
364  */
365  c = dc_val[ - 1];
366  b = dc_val[ - 1 - wrap];
367  a = dc_val[ - wrap];
368 
369  if (pq < 9 || !overlap) {
370  /* Set outer values */
371  if (s->first_slice_line && (n != 2 && n != 3))
372  b = a = dcpred[scale];
373  if (s->mb_x == 0 && (n != 1 && n != 3))
374  b = c = dcpred[scale];
375  } else {
376  /* Set outer values */
377  if (s->first_slice_line && (n != 2 && n != 3))
378  b = a = 0;
379  if (s->mb_x == 0 && (n != 1 && n != 3))
380  b = c = 0;
381  }
382 
383  if (abs(a - b) <= abs(b - c)) {
384  pred = c;
385  *dir_ptr = 1; // left
386  } else {
387  pred = a;
388  *dir_ptr = 0; // top
389  }
390 
391  /* update predictor */
392  *dc_val_ptr = &dc_val[0];
393  return pred;
394 }
395 
396 
397 /** Get predicted DC value
398  * prediction dir: left=0, top=1
399  * @param s MpegEncContext
400  * @param overlap flag indicating that overlap filtering is used
401  * @param pq integer part of picture quantizer
402  * @param[in] n block index in the current MB
403  * @param a_avail flag indicating top block availability
404  * @param c_avail flag indicating left block availability
405  * @param dc_val_ptr Pointer to DC predictor
406  * @param dir_ptr Prediction direction for use in AC prediction
407  */
408 static inline int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
409  int a_avail, int c_avail,
410  int16_t **dc_val_ptr, int *dir_ptr)
411 {
412  int a, b, c, wrap, pred;
413  int16_t *dc_val;
414  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
415  int q1, q2 = 0;
416  int dqscale_index;
417 
418  /* scale predictors if needed */
419  q1 = FFABS(s->cur_pic.qscale_table[mb_pos]);
420  dqscale_index = ff_wmv3_dc_scale_table[q1] - 1;
421  if (dqscale_index < 0)
422  return 0;
423 
424  wrap = s->block_wrap[n];
425  dc_val = s->dc_val + s->block_index[n];
426 
427  /* B A
428  * C X
429  */
430  c = dc_val[ - 1];
431  b = dc_val[ - 1 - wrap];
432  a = dc_val[ - wrap];
433 
434  if (c_avail && (n != 1 && n != 3)) {
435  q2 = FFABS(s->cur_pic.qscale_table[mb_pos - 1]);
436  if (q2 && q2 != q1)
437  c = (int)((unsigned)c * ff_wmv3_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
438  }
439  if (a_avail && (n != 2 && n != 3)) {
440  q2 = FFABS(s->cur_pic.qscale_table[mb_pos - s->mb_stride]);
441  if (q2 && q2 != q1)
442  a = (int)((unsigned)a * ff_wmv3_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
443  }
444  if (a_avail && c_avail && (n != 3)) {
445  int off = mb_pos;
446  if (n != 1)
447  off--;
448  if (n != 2)
449  off -= s->mb_stride;
450  q2 = FFABS(s->cur_pic.qscale_table[off]);
451  if (q2 && q2 != q1)
452  b = (int)((unsigned)b * ff_wmv3_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
453  }
454 
455  if (c_avail && (!a_avail || abs(a - b) <= abs(b - c))) {
456  pred = c;
457  *dir_ptr = 1; // left
458  } else if (a_avail) {
459  pred = a;
460  *dir_ptr = 0; // top
461  } else {
462  pred = 0;
463  *dir_ptr = 1; // left
464  }
465 
466  /* update predictor */
467  *dc_val_ptr = &dc_val[0];
468  return pred;
469 }
470 
471 /** @} */ // Block group
472 
473 /**
474  * @name VC1 Macroblock-level functions in Simple/Main Profiles
475  * @see 7.1.4, p91 and 8.1.1.7, p(1)04
476  * @{
477  */
478 
479 static inline int vc1_coded_block_pred(MPVContext *const s, int n, int diff)
480 {
481  int xy, wrap, pred, a, b, c;
482 
483  xy = s->block_index[n];
484  wrap = s->b8_stride;
485 
486  /* B C
487  * A X
488  */
489  a = s->coded_block[xy - 1 ];
490  b = s->coded_block[xy - 1 - wrap];
491  c = s->coded_block[xy - wrap];
492 
493  if (b == c) {
494  pred = a;
495  } else {
496  pred = c;
497  }
498 
499  /* store value */
500  s->coded_block[xy] = pred ^ diff;
501 
502  return pred ^ diff;
503 }
504 
505 /**
506  * Decode one AC coefficient
507  * @param v The VC1 context
508  * @param last Last coefficient
509  * @param skip How much zero coefficients to skip
510  * @param value Decoded AC coefficient value
511  * @param codingset set of VLC to decode data
512  * @see 8.1.3.4
513  */
514 static int vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
515  int *value, int codingset)
516 {
517  GetBitContext *const gb = &v->gb;
518  int index, run, level, lst, sign;
519 
520  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset], AC_VLC_BITS, 3);
521  if (index < 0)
522  return index;
523  if (index != ff_vc1_ac_sizes[codingset] - 1) {
524  run = vc1_index_decode_table[codingset][index][0];
525  level = vc1_index_decode_table[codingset][index][1];
526  lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
527  sign = get_bits1(gb);
528  } else {
529  int escape = decode210(gb);
530  if (escape != 2) {
531  index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset], AC_VLC_BITS, 3);
532  if (index >= ff_vc1_ac_sizes[codingset] - 1U)
533  return AVERROR_INVALIDDATA;
534  run = vc1_index_decode_table[codingset][index][0];
535  level = vc1_index_decode_table[codingset][index][1];
536  lst = index >= vc1_last_decode_table[codingset];
537  if (escape == 0) {
538  if (lst)
539  level += vc1_last_delta_level_table[codingset][run];
540  else
541  level += vc1_delta_level_table[codingset][run];
542  } else {
543  if (lst)
544  run += vc1_last_delta_run_table[codingset][level] + 1;
545  else
546  run += vc1_delta_run_table[codingset][level] + 1;
547  }
548  sign = get_bits1(gb);
549  } else {
550  lst = get_bits1(gb);
551  if (v->esc3_level_length == 0) {
552  if (v->pq < 8 || v->dquantfrm) { // table 59
553  v->esc3_level_length = get_bits(gb, 3);
554  if (!v->esc3_level_length)
555  v->esc3_level_length = get_bits(gb, 2) + 8;
556  } else { // table 60
557  v->esc3_level_length = get_unary(gb, 1, 6) + 2;
558  }
559  v->esc3_run_length = 3 + get_bits(gb, 2);
560  }
561  run = get_bits(gb, v->esc3_run_length);
562  sign = get_bits1(gb);
563  level = get_bits(gb, v->esc3_level_length);
564  }
565  }
566 
567  *last = lst;
568  *skip = run;
569  *value = (level ^ -sign) + sign;
570 
571  return 0;
572 }
573 
574 /** Decode intra block in intra frames - should be faster than decode_intra_block
575  * @param v VC1Context
576  * @param block block to decode
577  * @param[in] n subblock index
578  * @param coded are AC coeffs present or not
579  * @param codingset set of VLC to decode data
580  */
581 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
582  int coded, int codingset)
583 {
584  GetBitContext *const gb = &v->gb;
585  MpegEncContext *s = &v->s;
586  int dc_pred_dir = 0; /* Direction of the DC prediction used */
587  int16_t *dc_val;
588  int16_t *ac_val, *ac_val2;
589  int dcdiff, scale;
590 
591  /* Get DC differential */
592  dcdiff = get_vlc2(gb, ff_msmp4_dc_vlc[v->dc_table_index][n >= 4],
593  MSMP4_DC_VLC_BITS, 3);
594  if (dcdiff) {
595  const int m = (v->pq == 1 || v->pq == 2) ? 3 - v->pq : 0;
596  if (dcdiff == 119 /* ESC index value */) {
597  dcdiff = get_bits(gb, 8 + m);
598  } else {
599  if (m)
600  dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
601  }
602  if (get_bits1(gb))
603  dcdiff = -dcdiff;
604  }
605 
606  /* Prediction */
607  dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
608  *dc_val = dcdiff;
609 
610  /* Store the quantized DC coeff, used for prediction */
611  block[0] = dcdiff * s->y_dc_scale;
612 
613  ac_val = s->ac_val[s->block_index[n]];
614  ac_val2 = ac_val;
615  if (dc_pred_dir) // left
616  ac_val -= 16;
617  else // top
618  ac_val -= 16 * s->block_wrap[n];
619 
620  scale = v->pq * 2 + v->halfpq;
621 
622  //AC Decoding
623 
624  if (coded) {
625  int last = 0, skip, value;
626  const uint8_t *zz_table;
627  int k;
628 
629  if (v->s.ac_pred) {
630  if (!dc_pred_dir)
631  zz_table = v->zz_8x8[2];
632  else
633  zz_table = v->zz_8x8[3];
634  } else
635  zz_table = v->zz_8x8[1];
636 
637  for (int i = 1; !last; ++i) {
638  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
639  if (ret < 0)
640  return ret;
641  i += skip;
642  if (i > 63)
643  break;
644  block[zz_table[i]] = value;
645  }
646 
647  /* apply AC prediction if needed */
648  if (s->ac_pred) {
649  int sh;
650  if (dc_pred_dir) { // left
651  sh = v->left_blk_sh;
652  } else { // top
653  sh = v->top_blk_sh;
654  ac_val += 8;
655  }
656  for (k = 1; k < 8; k++)
657  block[k << sh] += ac_val[k];
658  }
659  /* save AC coeffs for further prediction */
660  for (k = 1; k < 8; k++) {
661  ac_val2[k] = block[k << v->left_blk_sh];
662  ac_val2[k + 8] = block[k << v->top_blk_sh];
663  }
664 
665  /* scale AC coeffs */
666  for (k = 1; k < 64; k++)
667  if (block[k]) {
668  block[k] *= scale;
669  if (!v->pquantizer)
670  block[k] += (block[k] < 0) ? -v->pq : v->pq;
671  }
672 
673  } else {
674  int k;
675 
676  memset(ac_val2, 0, 16 * 2);
677 
678  /* apply AC prediction if needed */
679  if (s->ac_pred) {
680  int sh;
681  if (dc_pred_dir) { //left
682  sh = v->left_blk_sh;
683  } else { // top
684  sh = v->top_blk_sh;
685  ac_val += 8;
686  ac_val2 += 8;
687  }
688  memcpy(ac_val2, ac_val, 8 * 2);
689  for (k = 1; k < 8; k++) {
690  block[k << sh] = ac_val[k] * scale;
691  if (!v->pquantizer && block[k << sh])
692  block[k << sh] += (block[k << sh] < 0) ? -v->pq : v->pq;
693  }
694  }
695  }
696 
697  return 0;
698 }
699 
700 /** Decode intra block in intra frames - should be faster than decode_intra_block
701  * @param v VC1Context
702  * @param block block to decode
703  * @param[in] n subblock number
704  * @param coded are AC coeffs present or not
705  * @param codingset set of VLC to decode data
706  * @param mquant quantizer value for this macroblock
707  */
708 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
709  int coded, int codingset, int mquant)
710 {
711  GetBitContext *const gb = &v->gb;
712  MpegEncContext *s = &v->s;
713  int dc_pred_dir = 0; /* Direction of the DC prediction used */
714  int16_t *dc_val = NULL;
715  int16_t *ac_val, *ac_val2;
716  int dcdiff;
717  int a_avail = v->a_avail, c_avail = v->c_avail;
718  int use_pred = s->ac_pred;
719  int scale;
720  int q1, q2 = 0;
721  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
722  int quant = FFABS(mquant);
723 
724  /* Get DC differential */
725  dcdiff = get_vlc2(gb, ff_msmp4_dc_vlc[v->dc_table_index][n >= 4],
726  MSMP4_DC_VLC_BITS, 3);
727  if (dcdiff) {
728  const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
729  if (dcdiff == 119 /* ESC index value */) {
730  dcdiff = get_bits(gb, 8 + m);
731  } else {
732  if (m)
733  dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
734  }
735  if (get_bits1(gb))
736  dcdiff = -dcdiff;
737  }
738 
739  /* Prediction */
740  dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, quant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
741  *dc_val = dcdiff;
742 
743  /* Store the quantized DC coeff, used for prediction */
744  block[0] = dcdiff * s->y_dc_scale;
745 
746  /* check if AC is needed at all */
747  if (!a_avail && !c_avail)
748  use_pred = 0;
749 
750  scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
751 
752  ac_val = s->ac_val[s->block_index[n]];
753  ac_val2 = ac_val;
754  if (dc_pred_dir) // left
755  ac_val -= 16;
756  else // top
757  ac_val -= 16 * s->block_wrap[n];
758 
759  q1 = s->cur_pic.qscale_table[mb_pos];
760  if (n == 3)
761  q2 = q1;
762  else if (dc_pred_dir) {
763  if (n == 1)
764  q2 = q1;
765  else if (c_avail && mb_pos)
766  q2 = s->cur_pic.qscale_table[mb_pos - 1];
767  } else {
768  if (n == 2)
769  q2 = q1;
770  else if (a_avail && mb_pos >= s->mb_stride)
771  q2 = s->cur_pic.qscale_table[mb_pos - s->mb_stride];
772  }
773 
774  //AC Decoding
775 
776  if (coded) {
777  int last = 0, skip, value;
778  const uint8_t *zz_table;
779  int k;
780 
781  if (v->s.ac_pred) {
782  if (!use_pred && v->fcm == ILACE_FRAME) {
783  zz_table = v->zzi_8x8;
784  } else {
785  if (!dc_pred_dir) // top
786  zz_table = v->zz_8x8[2];
787  else // left
788  zz_table = v->zz_8x8[3];
789  }
790  } else {
791  if (v->fcm != ILACE_FRAME)
792  zz_table = v->zz_8x8[1];
793  else
794  zz_table = v->zzi_8x8;
795  }
796 
797  for (int i = 1; !last; ++i) {
798  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
799  if (ret < 0)
800  return ret;
801  i += skip;
802  if (i > 63)
803  break;
804  block[zz_table[i]] = value;
805  }
806 
807  /* apply AC prediction if needed */
808  if (use_pred) {
809  int sh;
810  if (dc_pred_dir) { // left
811  sh = v->left_blk_sh;
812  } else { // top
813  sh = v->top_blk_sh;
814  ac_val += 8;
815  }
816  /* scale predictors if needed*/
817  q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
818  if (q1 < 1)
819  return AVERROR_INVALIDDATA;
820  if (q2)
821  q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
822  if (q2 && q1 != q2) {
823  for (k = 1; k < 8; k++)
824  block[k << sh] += (int)(ac_val[k] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
825  } else {
826  for (k = 1; k < 8; k++)
827  block[k << sh] += ac_val[k];
828  }
829  }
830  /* save AC coeffs for further prediction */
831  for (k = 1; k < 8; k++) {
832  ac_val2[k ] = block[k << v->left_blk_sh];
833  ac_val2[k + 8] = block[k << v->top_blk_sh];
834  }
835 
836  /* scale AC coeffs */
837  for (k = 1; k < 64; k++)
838  if (block[k]) {
839  block[k] *= scale;
840  if (!v->pquantizer)
841  block[k] += (block[k] < 0) ? -quant : quant;
842  }
843 
844  } else { // no AC coeffs
845  int k;
846 
847  memset(ac_val2, 0, 16 * 2);
848 
849  /* apply AC prediction if needed */
850  if (use_pred) {
851  int sh;
852  if (dc_pred_dir) { // left
853  sh = v->left_blk_sh;
854  } else { // top
855  sh = v->top_blk_sh;
856  ac_val += 8;
857  ac_val2 += 8;
858  }
859  memcpy(ac_val2, ac_val, 8 * 2);
860  q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
861  if (q1 < 1)
862  return AVERROR_INVALIDDATA;
863  if (q2)
864  q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
865  if (q2 && q1 != q2) {
866  for (k = 1; k < 8; k++)
867  ac_val2[k] = (int)(ac_val2[k] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
868  }
869  for (k = 1; k < 8; k++) {
870  block[k << sh] = ac_val2[k] * scale;
871  if (!v->pquantizer && block[k << sh])
872  block[k << sh] += (block[k << sh] < 0) ? -quant : quant;
873  }
874  }
875  }
876 
877  return 0;
878 }
879 
880 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
881  * @param v VC1Context
882  * @param block block to decode
883  * @param[in] n subblock index
884  * @param coded are AC coeffs present or not
885  * @param mquant block quantizer
886  * @param codingset set of VLC to decode data
887  */
888 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
889  int coded, int mquant, int codingset)
890 {
891  GetBitContext *const gb = &v->gb;
892  MpegEncContext *s = &v->s;
893  int dc_pred_dir = 0; /* Direction of the DC prediction used */
894  int16_t *dc_val = NULL;
895  int16_t *ac_val, *ac_val2;
896  int dcdiff;
897  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
898  int a_avail = v->a_avail, c_avail = v->c_avail;
899  int use_pred = s->ac_pred;
900  int scale;
901  int q1, q2 = 0;
902  int quant = FFABS(mquant);
903 
904  s->bdsp.clear_block(block);
905 
906  /* XXX: Guard against dumb values of mquant */
907  quant = av_clip_uintp2(quant, 5);
908 
909  /* Set DC scale - y and c use the same so we only set y */
910  s->y_dc_scale = ff_wmv3_dc_scale_table[quant];
911 
912  /* Get DC differential */
913  dcdiff = get_vlc2(gb, ff_msmp4_dc_vlc[v->dc_table_index][n >= 4],
914  MSMP4_DC_VLC_BITS, 3);
915  if (dcdiff) {
916  const int m = (quant == 1 || quant == 2) ? 3 - quant : 0;
917  if (dcdiff == 119 /* ESC index value */) {
918  dcdiff = get_bits(gb, 8 + m);
919  } else {
920  if (m)
921  dcdiff = (dcdiff << m) + get_bits(gb, m) - ((1 << m) - 1);
922  }
923  if (get_bits1(gb))
924  dcdiff = -dcdiff;
925  }
926 
927  /* Prediction */
928  dcdiff += ff_vc1_pred_dc(&v->s, v->overlap, quant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
929  *dc_val = dcdiff;
930 
931  /* Store the quantized DC coeff, used for prediction */
932  block[0] = dcdiff * s->y_dc_scale;
933 
934  //AC Decoding
935 
936  /* check if AC is needed at all and adjust direction if needed */
937  if (!a_avail) dc_pred_dir = 1;
938  if (!c_avail) dc_pred_dir = 0;
939  if (!a_avail && !c_avail) use_pred = 0;
940  ac_val = s->ac_val[s->block_index[n]];
941  ac_val2 = ac_val;
942 
943  scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
944 
945  if (dc_pred_dir) //left
946  ac_val -= 16;
947  else //top
948  ac_val -= 16 * s->block_wrap[n];
949 
950  q1 = s->cur_pic.qscale_table[mb_pos];
951  if (dc_pred_dir && c_avail && mb_pos)
952  q2 = s->cur_pic.qscale_table[mb_pos - 1];
953  if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
954  q2 = s->cur_pic.qscale_table[mb_pos - s->mb_stride];
955  if (dc_pred_dir && n == 1)
956  q2 = q1;
957  if (!dc_pred_dir && n == 2)
958  q2 = q1;
959  if (n == 3) q2 = q1;
960 
961  if (coded) {
962  int last = 0, skip, value;
963  int k;
964 
965  for (int i = 1; !last; ++i) {
966  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
967  if (ret < 0)
968  return ret;
969  i += skip;
970  if (i > 63)
971  break;
972  if (v->fcm == PROGRESSIVE)
973  block[v->zz_8x8[0][i]] = value;
974  else {
975  if (use_pred && (v->fcm == ILACE_FRAME)) {
976  if (!dc_pred_dir) // top
977  block[v->zz_8x8[2][i]] = value;
978  else // left
979  block[v->zz_8x8[3][i]] = value;
980  } else {
981  block[v->zzi_8x8[i]] = value;
982  }
983  }
984  }
985 
986  /* apply AC prediction if needed */
987  if (use_pred) {
988  /* scale predictors if needed*/
989  q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
990  if (q1 < 1)
991  return AVERROR_INVALIDDATA;
992  if (q2)
993  q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
994  if (q2 && q1 != q2) {
995  if (dc_pred_dir) { // left
996  for (k = 1; k < 8; k++)
997  block[k << v->left_blk_sh] += (int)(ac_val[k] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
998  } else { //top
999  for (k = 1; k < 8; k++)
1000  block[k << v->top_blk_sh] += (int)(ac_val[k + 8] * q2 * (unsigned)ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1001  }
1002  } else {
1003  if (dc_pred_dir) { // left
1004  for (k = 1; k < 8; k++)
1005  block[k << v->left_blk_sh] += ac_val[k];
1006  } else { // top
1007  for (k = 1; k < 8; k++)
1008  block[k << v->top_blk_sh] += ac_val[k + 8];
1009  }
1010  }
1011  }
1012  /* save AC coeffs for further prediction */
1013  for (k = 1; k < 8; k++) {
1014  ac_val2[k ] = block[k << v->left_blk_sh];
1015  ac_val2[k + 8] = block[k << v->top_blk_sh];
1016  }
1017 
1018  /* scale AC coeffs */
1019  for (k = 1; k < 64; k++)
1020  if (block[k]) {
1021  block[k] *= scale;
1022  if (!v->pquantizer)
1023  block[k] += (block[k] < 0) ? -quant : quant;
1024  }
1025  } else { // no AC coeffs
1026  int k;
1027 
1028  memset(ac_val2, 0, 16 * 2);
1029  if (dc_pred_dir) { // left
1030  if (use_pred) {
1031  memcpy(ac_val2, ac_val, 8 * 2);
1032  q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1033  if (q1 < 1)
1034  return AVERROR_INVALIDDATA;
1035  if (q2)
1036  q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1037  if (q2 && q1 != q2) {
1038  for (k = 1; k < 8; k++)
1039  ac_val2[k] = (int)(ac_val2[k] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1040  }
1041  }
1042  } else { // top
1043  if (use_pred) {
1044  memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1045  q1 = FFABS(q1) * 2 + ((q1 < 0) ? 0 : v->halfpq) - 1;
1046  if (q1 < 1)
1047  return AVERROR_INVALIDDATA;
1048  if (q2)
1049  q2 = FFABS(q2) * 2 + ((q2 < 0) ? 0 : v->halfpq) - 1;
1050  if (q2 && q1 != q2) {
1051  for (k = 1; k < 8; k++)
1052  ac_val2[k + 8] = (int)(ac_val2[k + 8] * (unsigned)q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1053  }
1054  }
1055  }
1056 
1057  /* apply AC prediction if needed */
1058  if (use_pred) {
1059  if (dc_pred_dir) { // left
1060  for (k = 1; k < 8; k++) {
1061  block[k << v->left_blk_sh] = ac_val2[k] * scale;
1062  if (!v->pquantizer && block[k << v->left_blk_sh])
1063  block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -quant : quant;
1064  }
1065  } else { // top
1066  for (k = 1; k < 8; k++) {
1067  block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
1068  if (!v->pquantizer && block[k << v->top_blk_sh])
1069  block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -quant : quant;
1070  }
1071  }
1072  }
1073  }
1074 
1075  return 0;
1076 }
1077 
1078 /** Decode P block
1079  */
1080 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
1081  int mquant, int ttmb, int first_block,
1082  uint8_t *dst, int linesize, int skip_block,
1083  int *ttmb_out)
1084 {
1085  MpegEncContext *s = &v->s;
1086  GetBitContext *const gb = &v->gb;
1087  int i, j;
1088  int subblkpat = 0;
1089  int scale, off, idx, last, skip, value;
1090  int ttblk = ttmb & 7;
1091  int pat = 0;
1092  int quant = FFABS(mquant);
1093 
1094  s->bdsp.clear_block(block);
1095 
1096  if (ttmb == -1) {
1098  }
1099  if (ttblk == TT_4X4) {
1100  subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index], VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1101  }
1102  if ((ttblk != TT_8X8 && ttblk != TT_4X4)
1103  && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
1104  || (!v->res_rtm_flag && !first_block))) {
1105  subblkpat = decode012(gb);
1106  if (subblkpat)
1107  subblkpat ^= 3; // swap decoded pattern bits
1108  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
1109  ttblk = TT_8X4;
1110  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
1111  ttblk = TT_4X8;
1112  }
1113  scale = quant * 2 + ((mquant < 0) ? 0 : v->halfpq);
1114 
1115  // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
1116  if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
1117  subblkpat = 2 - (ttblk == TT_8X4_TOP);
1118  ttblk = TT_8X4;
1119  }
1120  if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
1121  subblkpat = 2 - (ttblk == TT_4X8_LEFT);
1122  ttblk = TT_4X8;
1123  }
1124  switch (ttblk) {
1125  case TT_8X8:
1126  pat = 0xF;
1127  i = 0;
1128  last = 0;
1129  while (!last) {
1130  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1131  if (ret < 0)
1132  return ret;
1133  i += skip;
1134  if (i > 63)
1135  break;
1136  if (!v->fcm)
1137  idx = v->zz_8x8[0][i++];
1138  else
1139  idx = v->zzi_8x8[i++];
1140  block[idx] = value * scale;
1141  if (!v->pquantizer)
1142  block[idx] += (block[idx] < 0) ? -quant : quant;
1143  }
1144  if (!skip_block) {
1145  if (i == 1)
1146  v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
1147  else {
1149  s->idsp.add_pixels_clamped(block, dst, linesize);
1150  }
1151  }
1152  break;
1153  case TT_4X4:
1154  pat = ~subblkpat & 0xF;
1155  for (j = 0; j < 4; j++) {
1156  last = subblkpat & (1 << (3 - j));
1157  i = 0;
1158  off = (j & 1) * 4 + (j & 2) * 16;
1159  while (!last) {
1160  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1161  if (ret < 0)
1162  return ret;
1163  i += skip;
1164  if (i > 15)
1165  break;
1166  if (!v->fcm)
1168  else
1170  block[idx + off] = value * scale;
1171  if (!v->pquantizer)
1172  block[idx + off] += (block[idx + off] < 0) ? -quant : quant;
1173  }
1174  if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
1175  if (i == 1)
1176  v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1177  else
1178  v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
1179  }
1180  }
1181  break;
1182  case TT_8X4:
1183  pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
1184  for (j = 0; j < 2; j++) {
1185  last = subblkpat & (1 << (1 - j));
1186  i = 0;
1187  off = j * 32;
1188  while (!last) {
1189  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1190  if (ret < 0)
1191  return ret;
1192  i += skip;
1193  if (i > 31)
1194  break;
1195  if (!v->fcm)
1196  idx = v->zz_8x4[i++] + off;
1197  else
1198  idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
1199  block[idx] = value * scale;
1200  if (!v->pquantizer)
1201  block[idx] += (block[idx] < 0) ? -quant : quant;
1202  }
1203  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1204  if (i == 1)
1205  v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
1206  else
1207  v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
1208  }
1209  }
1210  break;
1211  case TT_4X8:
1212  pat = ~(subblkpat * 5) & 0xF;
1213  for (j = 0; j < 2; j++) {
1214  last = subblkpat & (1 << (1 - j));
1215  i = 0;
1216  off = j * 4;
1217  while (!last) {
1218  int ret = vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
1219  if (ret < 0)
1220  return ret;
1221  i += skip;
1222  if (i > 31)
1223  break;
1224  if (!v->fcm)
1225  idx = v->zz_4x8[i++] + off;
1226  else
1227  idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
1228  block[idx] = value * scale;
1229  if (!v->pquantizer)
1230  block[idx] += (block[idx] < 0) ? -quant : quant;
1231  }
1232  if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
1233  if (i == 1)
1234  v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
1235  else
1236  v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
1237  }
1238  }
1239  break;
1240  }
1241  if (ttmb_out)
1242  *ttmb_out |= ttblk << (n * 4);
1243  return pat;
1244 }
1245 
1246 /** @} */ // Macroblock group
1247 
1248 static const uint8_t size_table[6] = { 0, 2, 3, 4, 5, 8 };
1249 
1250 /** Decode one P-frame MB
1251  */
1253 {
1254  MpegEncContext *s = &v->s;
1255  GetBitContext *const gb = &v->gb;
1256  int i, j;
1257  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1258  int cbp; /* cbp decoding stuff */
1259  int mqdiff, mquant; /* MB quantization */
1260  int ttmb = v->ttfrm; /* MB Transform type */
1261 
1262  int mb_has_coeffs = 1; /* last_flag */
1263  int dmv_x, dmv_y; /* Differential MV components */
1264  int index, index1; /* LUT indexes */
1265  int val, sign; /* temp values */
1266  int first_block = 1;
1267  int dst_idx, off;
1268  int skipped, fourmv;
1269  int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
1270  int ret;
1271 
1272  mquant = v->pq; /* lossy initialization */
1273 
1274  if (v->mv_type_is_raw)
1275  fourmv = get_bits1(gb);
1276  else
1277  fourmv = v->mv_type_mb_plane[mb_pos];
1278  if (v->skip_is_raw)
1279  skipped = get_bits1(gb);
1280  else
1281  skipped = v->s.mbskip_table[mb_pos];
1282 
1283  if (!fourmv) { /* 1MV mode */
1284  if (!skipped) {
1285  GET_MVDATA(dmv_x, dmv_y);
1286 
1287  if (s->mb_intra) {
1288  s->cur_pic.motion_val[1][s->block_index[0]][0] = 0;
1289  s->cur_pic.motion_val[1][s->block_index[0]][1] = 0;
1290  }
1291  s->cur_pic.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
1292  ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type, 0, 0);
1293 
1294  /* FIXME Set DC val for inter block ? */
1295  if (s->mb_intra && !mb_has_coeffs) {
1296  GET_MQUANT();
1297  s->ac_pred = get_bits1(gb);
1298  cbp = 0;
1299  } else if (mb_has_coeffs) {
1300  if (s->mb_intra)
1301  s->ac_pred = get_bits1(gb);
1302  cbp = get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1303  GET_MQUANT();
1304  } else {
1305  mquant = v->pq;
1306  cbp = 0;
1307  }
1308  s->cur_pic.qscale_table[mb_pos] = mquant;
1309 
1310  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1311  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index],
1312  VC1_TTMB_VLC_BITS, 2);
1313  if (!s->mb_intra) ff_vc1_mc_1mv(v, 0);
1314  dst_idx = 0;
1315  for (i = 0; i < 6; i++) {
1316  s->dc_val[s->block_index[i]] = 0;
1317  dst_idx += i >> 2;
1318  val = ((cbp >> (5 - i)) & 1);
1319  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1320  v->mb_type[s->block_index[i]] = s->mb_intra;
1321  if (s->mb_intra) {
1322  /* check if prediction blocks A and C are available */
1323  v->a_avail = v->c_avail = 0;
1324  if (i == 2 || i == 3 || !s->first_slice_line)
1325  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
1326  if (i == 1 || i == 3 || s->mb_x)
1327  v->c_avail = v->mb_type[s->block_index[i] - 1];
1328 
1329  ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1330  (i & 4) ? v->codingset2 : v->codingset);
1331  if (ret < 0)
1332  return ret;
1333  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1334  continue;
1336  if (v->rangeredfrm)
1337  for (j = 0; j < 64; j++)
1338  v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1339  block_cbp |= 0xF << (i << 2);
1340  block_intra |= 1 << i;
1341  } else if (val) {
1342  pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb, first_block,
1343  s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
1344  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1345  if (pat < 0)
1346  return pat;
1347  block_cbp |= pat << (i << 2);
1348  if (!v->ttmbf && ttmb < 8)
1349  ttmb = -1;
1350  first_block = 0;
1351  }
1352  }
1353  } else { // skipped
1354  s->mb_intra = 0;
1355  for (i = 0; i < 6; i++) {
1356  v->mb_type[s->block_index[i]] = 0;
1357  s->dc_val[s->block_index[i]] = 0;
1358  }
1359  s->cur_pic.mb_type[mb_pos] = MB_TYPE_SKIP;
1360  s->cur_pic.qscale_table[mb_pos] = 0;
1361  ff_vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type, 0, 0);
1362  ff_vc1_mc_1mv(v, 0);
1363  }
1364  } else { // 4MV mode
1365  if (!skipped /* unskipped MB */) {
1366  int intra_count = 0, coded_inter = 0;
1367  int is_intra[6], is_coded[6];
1368  /* Get CBPCY */
1369  cbp = get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1370  for (i = 0; i < 6; i++) {
1371  val = ((cbp >> (5 - i)) & 1);
1372  s->dc_val[s->block_index[i]] = 0;
1373  s->mb_intra = 0;
1374  if (i < 4) {
1375  dmv_x = dmv_y = 0;
1376  s->mb_intra = 0;
1377  mb_has_coeffs = 0;
1378  if (val) {
1379  GET_MVDATA(dmv_x, dmv_y);
1380  }
1381  ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type, 0, 0);
1382  if (!s->mb_intra)
1383  ff_vc1_mc_4mv_luma(v, i, 0, 0);
1384  intra_count += s->mb_intra;
1385  is_intra[i] = s->mb_intra;
1386  is_coded[i] = mb_has_coeffs;
1387  }
1388  if (i & 4) {
1389  is_intra[i] = (intra_count >= 3);
1390  is_coded[i] = val;
1391  }
1392  if (i == 4)
1393  ff_vc1_mc_4mv_chroma(v, 0);
1394  v->mb_type[s->block_index[i]] = is_intra[i];
1395  if (!coded_inter)
1396  coded_inter = !is_intra[i] & is_coded[i];
1397  }
1398  // if there are no coded blocks then don't do anything more
1399  dst_idx = 0;
1400  if (!intra_count && !coded_inter)
1401  goto end;
1402  GET_MQUANT();
1403  s->cur_pic.qscale_table[mb_pos] = mquant;
1404  /* test if block is intra and has pred */
1405  {
1406  int intrapred = 0;
1407  for (i = 0; i < 6; i++)
1408  if (is_intra[i]) {
1409  if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[s->block_index[i] - s->block_wrap[i]])
1410  || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[s->block_index[i] - 1])) {
1411  intrapred = 1;
1412  break;
1413  }
1414  }
1415  if (intrapred)
1416  s->ac_pred = get_bits1(gb);
1417  else
1418  s->ac_pred = 0;
1419  }
1420  if (!v->ttmbf && coded_inter)
1421  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
1422  for (i = 0; i < 6; i++) {
1423  dst_idx += i >> 2;
1424  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1425  s->mb_intra = is_intra[i];
1426  if (is_intra[i]) {
1427  /* check if prediction blocks A and C are available */
1428  v->a_avail = v->c_avail = 0;
1429  if (i == 2 || i == 3 || !s->first_slice_line)
1430  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
1431  if (i == 1 || i == 3 || s->mb_x)
1432  v->c_avail = v->mb_type[s->block_index[i] - 1];
1433 
1434  ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, is_coded[i], mquant,
1435  (i & 4) ? v->codingset2 : v->codingset);
1436  if (ret < 0)
1437  return ret;
1438  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1439  continue;
1441  if (v->rangeredfrm)
1442  for (j = 0; j < 64; j++)
1443  v->block[v->cur_blk_idx][block_map[i]][j] *= 2;
1444  block_cbp |= 0xF << (i << 2);
1445  block_intra |= 1 << i;
1446  } else if (is_coded[i]) {
1447  pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1448  first_block, s->dest[dst_idx] + off,
1449  (i & 4) ? s->uvlinesize : s->linesize,
1450  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1451  &block_tt);
1452  if (pat < 0)
1453  return pat;
1454  block_cbp |= pat << (i << 2);
1455  if (!v->ttmbf && ttmb < 8)
1456  ttmb = -1;
1457  first_block = 0;
1458  }
1459  }
1460  } else { // skipped MB
1461  s->mb_intra = 0;
1462  s->cur_pic.qscale_table[mb_pos] = 0;
1463  for (i = 0; i < 6; i++) {
1464  v->mb_type[s->block_index[i]] = 0;
1465  s->dc_val[s->block_index[i]] = 0;
1466  }
1467  for (i = 0; i < 4; i++) {
1468  ff_vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type, 0, 0);
1469  ff_vc1_mc_4mv_luma(v, i, 0, 0);
1470  }
1471  ff_vc1_mc_4mv_chroma(v, 0);
1472  s->cur_pic.qscale_table[mb_pos] = 0;
1473  }
1474  }
1475 end:
1476  if (v->overlap && v->pq >= 9)
1478  vc1_put_blocks_clamped(v, 1);
1479 
1480  v->cbp[s->mb_x] = block_cbp;
1481  v->ttblk[s->mb_x] = block_tt;
1482  v->is_intra[s->mb_x] = block_intra;
1483 
1484  return 0;
1485 }
1486 
1487 /* Decode one macroblock in an interlaced frame p picture */
1488 
1490 {
1491  MpegEncContext *s = &v->s;
1492  GetBitContext *const gb = &v->gb;
1493  int i;
1494  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1495  int cbp = 0; /* cbp decoding stuff */
1496  int mqdiff, mquant; /* MB quantization */
1497  int ttmb = v->ttfrm; /* MB Transform type */
1498 
1499  int mb_has_coeffs = 1; /* last_flag */
1500  int dmv_x, dmv_y; /* Differential MV components */
1501  int val; /* temp value */
1502  int first_block = 1;
1503  int dst_idx, off;
1504  int skipped, fourmv = 0, twomv = 0;
1505  int block_cbp = 0, pat, block_tt = 0;
1506  int idx_mbmode = 0, mvbp;
1507  int fieldtx;
1508  int ret;
1509 
1510  mquant = v->pq; /* Lossy initialization */
1511 
1512  if (v->skip_is_raw)
1513  skipped = get_bits1(gb);
1514  else
1515  skipped = v->s.mbskip_table[mb_pos];
1516  if (!skipped) {
1517  if (v->fourmvswitch)
1518  idx_mbmode = get_vlc2(gb, v->mbmode_vlc, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
1519  else
1520  idx_mbmode = get_vlc2(gb, v->mbmode_vlc, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
1521  switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
1522  /* store the motion vector type in a flag (useful later) */
1523  case MV_PMODE_INTFR_4MV:
1524  fourmv = 1;
1525  v->blk_mv_type[s->block_index[0]] = 0;
1526  v->blk_mv_type[s->block_index[1]] = 0;
1527  v->blk_mv_type[s->block_index[2]] = 0;
1528  v->blk_mv_type[s->block_index[3]] = 0;
1529  break;
1531  fourmv = 1;
1532  v->blk_mv_type[s->block_index[0]] = 1;
1533  v->blk_mv_type[s->block_index[1]] = 1;
1534  v->blk_mv_type[s->block_index[2]] = 1;
1535  v->blk_mv_type[s->block_index[3]] = 1;
1536  break;
1538  twomv = 1;
1539  v->blk_mv_type[s->block_index[0]] = 1;
1540  v->blk_mv_type[s->block_index[1]] = 1;
1541  v->blk_mv_type[s->block_index[2]] = 1;
1542  v->blk_mv_type[s->block_index[3]] = 1;
1543  break;
1544  case MV_PMODE_INTFR_1MV:
1545  v->blk_mv_type[s->block_index[0]] = 0;
1546  v->blk_mv_type[s->block_index[1]] = 0;
1547  v->blk_mv_type[s->block_index[2]] = 0;
1548  v->blk_mv_type[s->block_index[3]] = 0;
1549  break;
1550  }
1551  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
1552  for (i = 0; i < 4; i++) {
1553  s->cur_pic.motion_val[1][s->block_index[i]][0] = 0;
1554  s->cur_pic.motion_val[1][s->block_index[i]][1] = 0;
1555  }
1556  v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1557  s->mb_intra = 1;
1558  s->cur_pic.mb_type[mb_pos] = MB_TYPE_INTRA;
1559  fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
1560  mb_has_coeffs = get_bits1(gb);
1561  if (mb_has_coeffs)
1562  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1563  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1564  GET_MQUANT();
1565  s->cur_pic.qscale_table[mb_pos] = mquant;
1566  /* Set DC scale - y and c use the same so we only set y */
1567  s->y_dc_scale = ff_wmv3_dc_scale_table[FFABS(mquant)];
1568  dst_idx = 0;
1569  for (i = 0; i < 6; i++) {
1570  v->a_avail = v->c_avail = 0;
1571  v->mb_type[s->block_index[i]] = 1;
1572  s->dc_val[s->block_index[i]] = 0;
1573  dst_idx += i >> 2;
1574  val = ((cbp >> (5 - i)) & 1);
1575  if (i == 2 || i == 3 || !s->first_slice_line)
1576  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
1577  if (i == 1 || i == 3 || s->mb_x)
1578  v->c_avail = v->mb_type[s->block_index[i] - 1];
1579 
1580  ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1581  (i & 4) ? v->codingset2 : v->codingset);
1582  if (ret < 0)
1583  return ret;
1584  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1585  continue;
1587  block_cbp |= 0xf << (i << 2);
1588  }
1589 
1590  } else { // inter MB
1591  mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
1592  if (mb_has_coeffs)
1593  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1594  if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
1596  } else {
1597  if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
1598  || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
1600  }
1601  }
1602  s->mb_intra = v->is_intra[s->mb_x] = 0;
1603  for (i = 0; i < 6; i++)
1604  v->mb_type[s->block_index[i]] = 0;
1605  fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
1606  /* for all motion vector read MVDATA and motion compensate each block */
1607  dst_idx = 0;
1608  if (fourmv) {
1609  mvbp = v->fourmvbp;
1610  for (i = 0; i < 4; i++) {
1611  dmv_x = dmv_y = 0;
1612  if (mvbp & (8 >> i))
1613  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1614  ff_vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, 0);
1615  ff_vc1_mc_4mv_luma(v, i, 0, 0);
1616  }
1617  ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1618  } else if (twomv) {
1619  mvbp = v->twomvbp;
1620  dmv_x = dmv_y = 0;
1621  if (mvbp & 2) {
1622  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1623  }
1624  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, 0);
1625  ff_vc1_mc_4mv_luma(v, 0, 0, 0);
1626  ff_vc1_mc_4mv_luma(v, 1, 0, 0);
1627  dmv_x = dmv_y = 0;
1628  if (mvbp & 1) {
1629  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1630  }
1631  ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, 0);
1632  ff_vc1_mc_4mv_luma(v, 2, 0, 0);
1633  ff_vc1_mc_4mv_luma(v, 3, 0, 0);
1634  ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
1635  } else {
1636  mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
1637  dmv_x = dmv_y = 0;
1638  if (mvbp) {
1639  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
1640  }
1641  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, 0);
1642  ff_vc1_mc_1mv(v, 0);
1643  }
1644  if (cbp)
1645  GET_MQUANT(); // p. 227
1646  s->cur_pic.qscale_table[mb_pos] = mquant;
1647  if (!v->ttmbf && cbp)
1648  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
1649  for (i = 0; i < 6; i++) {
1650  s->dc_val[s->block_index[i]] = 0;
1651  dst_idx += i >> 2;
1652  val = ((cbp >> (5 - i)) & 1);
1653  if (!fieldtx)
1654  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1655  else
1656  off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
1657  if (val) {
1658  pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1659  first_block, s->dest[dst_idx] + off,
1660  (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
1661  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
1662  if (pat < 0)
1663  return pat;
1664  block_cbp |= pat << (i << 2);
1665  if (!v->ttmbf && ttmb < 8)
1666  ttmb = -1;
1667  first_block = 0;
1668  }
1669  }
1670  }
1671  } else { // skipped
1672  s->mb_intra = v->is_intra[s->mb_x] = 0;
1673  for (i = 0; i < 6; i++) {
1674  v->mb_type[s->block_index[i]] = 0;
1675  s->dc_val[s->block_index[i]] = 0;
1676  }
1677  s->cur_pic.mb_type[mb_pos] = MB_TYPE_SKIP;
1678  s->cur_pic.qscale_table[mb_pos] = 0;
1679  v->blk_mv_type[s->block_index[0]] = 0;
1680  v->blk_mv_type[s->block_index[1]] = 0;
1681  v->blk_mv_type[s->block_index[2]] = 0;
1682  v->blk_mv_type[s->block_index[3]] = 0;
1683  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, 0);
1684  ff_vc1_mc_1mv(v, 0);
1685  v->fieldtx_plane[mb_pos] = 0;
1686  }
1687  if (v->overlap && v->pq >= 9)
1689  vc1_put_blocks_clamped(v, 1);
1690 
1691  v->cbp[s->mb_x] = block_cbp;
1692  v->ttblk[s->mb_x] = block_tt;
1693 
1694  return 0;
1695 }
1696 
1698 {
1699  MpegEncContext *s = &v->s;
1700  GetBitContext *const gb = &v->gb;
1701  int i;
1702  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1703  int cbp = 0; /* cbp decoding stuff */
1704  int mqdiff, mquant; /* MB quantization */
1705  int ttmb = v->ttfrm; /* MB Transform type */
1706 
1707  int mb_has_coeffs = 1; /* last_flag */
1708  int dmv_x, dmv_y; /* Differential MV components */
1709  int val; /* temp values */
1710  int first_block = 1;
1711  int dst_idx, off;
1712  int pred_flag = 0;
1713  int block_cbp = 0, pat, block_tt = 0;
1714  int idx_mbmode = 0;
1715  int ret;
1716 
1717  mquant = v->pq; /* Lossy initialization */
1718 
1719  idx_mbmode = get_vlc2(gb, v->mbmode_vlc, VC1_IF_MBMODE_VLC_BITS, 2);
1720  if (idx_mbmode <= 1) { // intra MB
1721  v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
1722  s->mb_intra = 1;
1723  s->cur_pic.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
1724  s->cur_pic.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
1725  s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
1726  GET_MQUANT();
1727  s->cur_pic.qscale_table[mb_pos] = mquant;
1728  /* Set DC scale - y and c use the same so we only set y */
1729  s->y_dc_scale = ff_wmv3_dc_scale_table[FFABS(mquant)];
1730  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
1731  mb_has_coeffs = idx_mbmode & 1;
1732  if (mb_has_coeffs)
1733  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_ICBPCY_VLC_BITS, 2);
1734  dst_idx = 0;
1735  for (i = 0; i < 6; i++) {
1736  v->a_avail = v->c_avail = 0;
1737  v->mb_type[s->block_index[i]] = 1;
1738  s->dc_val[s->block_index[i]] = 0;
1739  dst_idx += i >> 2;
1740  val = ((cbp >> (5 - i)) & 1);
1741  if (i == 2 || i == 3 || !s->first_slice_line)
1742  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
1743  if (i == 1 || i == 3 || s->mb_x)
1744  v->c_avail = v->mb_type[s->block_index[i] - 1];
1745 
1746  ret = vc1_decode_intra_block(v, v->block[v->cur_blk_idx][block_map[i]], i, val, mquant,
1747  (i & 4) ? v->codingset2 : v->codingset);
1748  if (ret < 0)
1749  return ret;
1750  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1751  continue;
1753  block_cbp |= 0xf << (i << 2);
1754  }
1755  } else {
1756  s->mb_intra = v->is_intra[s->mb_x] = 0;
1757  s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
1758  for (i = 0; i < 6; i++)
1759  v->mb_type[s->block_index[i]] = 0;
1760  if (idx_mbmode <= 5) { // 1-MV
1761  dmv_x = dmv_y = pred_flag = 0;
1762  if (idx_mbmode & 1) {
1763  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1764  }
1765  ff_vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type, pred_flag, 0);
1766  ff_vc1_mc_1mv(v, 0);
1767  mb_has_coeffs = !(idx_mbmode & 2);
1768  } else { // 4-MV
1770  for (i = 0; i < 4; i++) {
1771  dmv_x = dmv_y = pred_flag = 0;
1772  if (v->fourmvbp & (8 >> i))
1773  get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
1774  ff_vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type, pred_flag, 0);
1775  ff_vc1_mc_4mv_luma(v, i, 0, 0);
1776  }
1777  ff_vc1_mc_4mv_chroma(v, 0);
1778  mb_has_coeffs = idx_mbmode & 1;
1779  }
1780  if (mb_has_coeffs)
1781  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1782  if (cbp) {
1783  GET_MQUANT();
1784  }
1785  s->cur_pic.qscale_table[mb_pos] = mquant;
1786  if (!v->ttmbf && cbp) {
1787  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
1788  }
1789  dst_idx = 0;
1790  for (i = 0; i < 6; i++) {
1791  s->dc_val[s->block_index[i]] = 0;
1792  dst_idx += i >> 2;
1793  val = ((cbp >> (5 - i)) & 1);
1794  off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
1795  if (val) {
1796  pat = vc1_decode_p_block(v, v->block[v->cur_blk_idx][block_map[i]], i, mquant, ttmb,
1797  first_block, s->dest[dst_idx] + off,
1798  (i & 4) ? s->uvlinesize : s->linesize,
1799  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY),
1800  &block_tt);
1801  if (pat < 0)
1802  return pat;
1803  block_cbp |= pat << (i << 2);
1804  if (!v->ttmbf && ttmb < 8)
1805  ttmb = -1;
1806  first_block = 0;
1807  }
1808  }
1809  }
1810  if (v->overlap && v->pq >= 9)
1812  vc1_put_blocks_clamped(v, 1);
1813 
1814  v->cbp[s->mb_x] = block_cbp;
1815  v->ttblk[s->mb_x] = block_tt;
1816 
1817  return 0;
1818 }
1819 
1820 /** Decode one B-frame MB (in Main profile)
1821  */
1823 {
1824  MpegEncContext *s = &v->s;
1825  GetBitContext *const gb = &v->gb;
1826  int i, j;
1827  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1828  int cbp = 0; /* cbp decoding stuff */
1829  int mqdiff, mquant; /* MB quantization */
1830  int ttmb = v->ttfrm; /* MB Transform type */
1831  int mb_has_coeffs = 0; /* last_flag */
1832  int index, index1; /* LUT indexes */
1833  int val, sign; /* temp values */
1834  int first_block = 1;
1835  int dst_idx, off;
1836  int skipped, direct;
1837  int dmv_x[2], dmv_y[2];
1838  int bmvtype = BMV_TYPE_BACKWARD;
1839  int ret;
1840 
1841  mquant = v->pq; /* lossy initialization */
1842  s->mb_intra = 0;
1843 
1844  if (v->dmb_is_raw)
1845  direct = get_bits1(gb);
1846  else
1847  direct = v->direct_mb_plane[mb_pos];
1848  if (v->skip_is_raw)
1849  skipped = get_bits1(gb);
1850  else
1851  skipped = v->s.mbskip_table[mb_pos];
1852 
1853  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
1854  for (i = 0; i < 6; i++) {
1855  v->mb_type[s->block_index[i]] = 0;
1856  s->dc_val[s->block_index[i]] = 0;
1857  }
1858  s->cur_pic.qscale_table[mb_pos] = 0;
1859 
1860  if (!direct) {
1861  if (!skipped) {
1862  GET_MVDATA(dmv_x[0], dmv_y[0]);
1863  dmv_x[1] = dmv_x[0];
1864  dmv_y[1] = dmv_y[0];
1865  }
1866  if (skipped || !s->mb_intra) {
1867  bmvtype = decode012(gb);
1868  switch (bmvtype) {
1869  case 0:
1870  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
1871  break;
1872  case 1:
1873  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
1874  break;
1875  case 2:
1876  bmvtype = BMV_TYPE_INTERPOLATED;
1877  dmv_x[0] = dmv_y[0] = 0;
1878  }
1879  }
1880  }
1881  for (i = 0; i < 6; i++)
1882  v->mb_type[s->block_index[i]] = s->mb_intra;
1883 
1884  if (skipped) {
1885  if (direct)
1886  bmvtype = BMV_TYPE_INTERPOLATED;
1887  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1888  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1889  return 0;
1890  }
1891  if (direct) {
1892  cbp = get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1893  GET_MQUANT();
1894  s->mb_intra = 0;
1895  s->cur_pic.qscale_table[mb_pos] = mquant;
1896  if (!v->ttmbf)
1897  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
1898  dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
1899  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1900  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1901  } else {
1902  if (!mb_has_coeffs && !s->mb_intra) {
1903  /* no coded blocks - effectively skipped */
1904  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1905  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1906  return 0;
1907  }
1908  if (s->mb_intra && !mb_has_coeffs) {
1909  GET_MQUANT();
1910  s->cur_pic.qscale_table[mb_pos] = mquant;
1911  s->ac_pred = get_bits1(gb);
1912  cbp = 0;
1913  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1914  } else {
1915  if (bmvtype == BMV_TYPE_INTERPOLATED) {
1916  GET_MVDATA(dmv_x[0], dmv_y[0]);
1917  if (!mb_has_coeffs) {
1918  /* interpolated skipped block */
1919  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1920  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1921  return 0;
1922  }
1923  }
1924  ff_vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
1925  if (!s->mb_intra) {
1926  vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
1927  }
1928  if (s->mb_intra)
1929  s->ac_pred = get_bits1(gb);
1930  cbp = get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
1931  GET_MQUANT();
1932  s->cur_pic.qscale_table[mb_pos] = mquant;
1933  if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
1934  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
1935  }
1936  }
1937  dst_idx = 0;
1938  for (i = 0; i < 6; i++) {
1939  s->dc_val[s->block_index[i]] = 0;
1940  dst_idx += i >> 2;
1941  val = ((cbp >> (5 - i)) & 1);
1942  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
1943  v->mb_type[s->block_index[i]] = s->mb_intra;
1944  if (s->mb_intra) {
1945  /* check if prediction blocks A and C are available */
1946  v->a_avail = v->c_avail = 0;
1947  if (i == 2 || i == 3 || !s->first_slice_line)
1948  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
1949  if (i == 1 || i == 3 || s->mb_x)
1950  v->c_avail = v->mb_type[s->block_index[i] - 1];
1951 
1952  ret = vc1_decode_intra_block(v, v->blocks[i], i, val, mquant,
1953  (i & 4) ? v->codingset2 : v->codingset);
1954  if (ret < 0)
1955  return ret;
1956  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
1957  continue;
1958  v->vc1dsp.vc1_inv_trans_8x8(v->blocks[i]);
1959  if (v->rangeredfrm)
1960  for (j = 0; j < 64; j++)
1961  v->blocks[i][j] *= 2;
1962  s->idsp.put_signed_pixels_clamped(v->blocks[i],
1963  s->dest[dst_idx] + off,
1964  i & 4 ? s->uvlinesize
1965  : s->linesize);
1966  } else if (val) {
1967  int pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb,
1968  first_block, s->dest[dst_idx] + off,
1969  (i & 4) ? s->uvlinesize : s->linesize,
1970  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), NULL);
1971  if (pat < 0)
1972  return pat;
1973  if (!v->ttmbf && ttmb < 8)
1974  ttmb = -1;
1975  first_block = 0;
1976  }
1977  }
1978  return 0;
1979 }
1980 
1981 /** Decode one B-frame MB (in interlaced field B picture)
1982  */
1984 {
1985  MpegEncContext *s = &v->s;
1986  GetBitContext *const gb = &v->gb;
1987  int i, j;
1988  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1989  int cbp = 0; /* cbp decoding stuff */
1990  int mqdiff, mquant; /* MB quantization */
1991  int ttmb = v->ttfrm; /* MB Transform type */
1992  int mb_has_coeffs = 0; /* last_flag */
1993  int val; /* temp value */
1994  int first_block = 1;
1995  int dst_idx, off;
1996  int fwd;
1997  int dmv_x[2], dmv_y[2], pred_flag[2];
1998  int bmvtype = BMV_TYPE_BACKWARD;
1999  int block_cbp = 0, pat, block_tt = 0;
2000  int idx_mbmode;
2001  int ret;
2002 
2003  mquant = v->pq; /* Lossy initialization */
2004  s->mb_intra = 0;
2005 
2006  idx_mbmode = get_vlc2(gb, v->mbmode_vlc, VC1_IF_MBMODE_VLC_BITS, 2);
2007  if (idx_mbmode <= 1) { // intra MB
2008  v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2009  s->mb_intra = 1;
2010  s->cur_pic.motion_val[1][s->block_index[0]][0] = 0;
2011  s->cur_pic.motion_val[1][s->block_index[0]][1] = 0;
2012  s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2013  GET_MQUANT();
2014  s->cur_pic.qscale_table[mb_pos] = mquant;
2015  /* Set DC scale - y and c use the same so we only set y */
2016  s->y_dc_scale = ff_wmv3_dc_scale_table[FFABS(mquant)];
2017  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2018  mb_has_coeffs = idx_mbmode & 1;
2019  if (mb_has_coeffs)
2020  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_ICBPCY_VLC_BITS, 2);
2021  dst_idx = 0;
2022  for (i = 0; i < 6; i++) {
2023  v->a_avail = v->c_avail = 0;
2024  v->mb_type[s->block_index[i]] = 1;
2025  s->dc_val[s->block_index[i]] = 0;
2026  dst_idx += i >> 2;
2027  val = ((cbp >> (5 - i)) & 1);
2028  if (i == 2 || i == 3 || !s->first_slice_line)
2029  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
2030  if (i == 1 || i == 3 || s->mb_x)
2031  v->c_avail = v->mb_type[s->block_index[i] - 1];
2032 
2033  ret = vc1_decode_intra_block(v, v->blocks[i], i, val, mquant,
2034  (i & 4) ? v->codingset2 : v->codingset);
2035  if (ret < 0)
2036  return ret;
2037  if (CONFIG_GRAY && (i > 3) && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2038  continue;
2039  v->vc1dsp.vc1_inv_trans_8x8(v->blocks[i]);
2040  if (v->rangeredfrm)
2041  for (j = 0; j < 64; j++)
2042  v->blocks[i][j] <<= 1;
2043  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2044  s->idsp.put_signed_pixels_clamped(v->blocks[i],
2045  s->dest[dst_idx] + off,
2046  (i & 4) ? s->uvlinesize
2047  : s->linesize);
2048  }
2049  } else {
2050  s->mb_intra = v->is_intra[s->mb_x] = 0;
2051  s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
2052  for (i = 0; i < 6; i++)
2053  v->mb_type[s->block_index[i]] = 0;
2054  if (v->fmb_is_raw)
2055  fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
2056  else
2057  fwd = v->forward_mb_plane[mb_pos];
2058  if (idx_mbmode <= 5) { // 1-MV
2059  int interpmvp = 0;
2060  dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2061  pred_flag[0] = pred_flag[1] = 0;
2062  if (fwd)
2063  bmvtype = BMV_TYPE_FORWARD;
2064  else {
2065  bmvtype = decode012(gb);
2066  switch (bmvtype) {
2067  case 0:
2068  bmvtype = BMV_TYPE_BACKWARD;
2069  break;
2070  case 1:
2071  bmvtype = BMV_TYPE_DIRECT;
2072  break;
2073  case 2:
2074  bmvtype = BMV_TYPE_INTERPOLATED;
2075  interpmvp = get_bits1(gb);
2076  }
2077  }
2078  v->bmvtype = bmvtype;
2079  if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
2080  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2081  }
2082  if (interpmvp) {
2083  get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
2084  }
2085  if (bmvtype == BMV_TYPE_DIRECT) {
2086  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2087  dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
2088  if (!s->next_pic.ptr->field_picture) {
2089  av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
2090  return AVERROR_INVALIDDATA;
2091  }
2092  }
2093  ff_vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
2094  vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
2095  mb_has_coeffs = !(idx_mbmode & 2);
2096  } else { // 4-MV
2097  if (fwd)
2098  bmvtype = BMV_TYPE_FORWARD;
2099  v->bmvtype = bmvtype;
2101  for (i = 0; i < 4; i++) {
2102  dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
2103  dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
2104  if (v->fourmvbp & (8 >> i)) {
2105  get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
2106  &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
2107  &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
2108  }
2109  ff_vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
2110  ff_vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
2111  }
2112  ff_vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
2113  mb_has_coeffs = idx_mbmode & 1;
2114  }
2115  if (mb_has_coeffs)
2116  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
2117  if (cbp) {
2118  GET_MQUANT();
2119  }
2120  s->cur_pic.qscale_table[mb_pos] = mquant;
2121  if (!v->ttmbf && cbp) {
2122  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
2123  }
2124  dst_idx = 0;
2125  for (i = 0; i < 6; i++) {
2126  s->dc_val[s->block_index[i]] = 0;
2127  dst_idx += i >> 2;
2128  val = ((cbp >> (5 - i)) & 1);
2129  off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
2130  if (val) {
2131  pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb,
2132  first_block, s->dest[dst_idx] + off,
2133  (i & 4) ? s->uvlinesize : s->linesize,
2134  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2135  if (pat < 0)
2136  return pat;
2137  block_cbp |= pat << (i << 2);
2138  if (!v->ttmbf && ttmb < 8)
2139  ttmb = -1;
2140  first_block = 0;
2141  }
2142  }
2143  }
2144  v->cbp[s->mb_x] = block_cbp;
2145  v->ttblk[s->mb_x] = block_tt;
2146 
2147  return 0;
2148 }
2149 
2150 /** Decode one B-frame MB (in interlaced frame B picture)
2151  */
2153 {
2154  MpegEncContext *s = &v->s;
2155  GetBitContext *const gb = &v->gb;
2156  int i, j;
2157  int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2158  int cbp = 0; /* cbp decoding stuff */
2159  int mqdiff, mquant; /* MB quantization */
2160  int ttmb = v->ttfrm; /* MB Transform type */
2161  int mvsw = 0; /* motion vector switch */
2162  int mb_has_coeffs = 1; /* last_flag */
2163  int dmv_x, dmv_y; /* Differential MV components */
2164  int val; /* temp value */
2165  int first_block = 1;
2166  int dst_idx, off;
2167  int skipped, direct, twomv = 0;
2168  int block_cbp = 0, pat, block_tt = 0;
2169  int idx_mbmode = 0, mvbp;
2170  int stride_y, fieldtx;
2171  int bmvtype = BMV_TYPE_BACKWARD;
2172  int dir, dir2;
2173  int ret;
2174 
2175  mquant = v->pq; /* Lossy initialization */
2176  s->mb_intra = 0;
2177  if (v->skip_is_raw)
2178  skipped = get_bits1(gb);
2179  else
2180  skipped = v->s.mbskip_table[mb_pos];
2181 
2182  if (!skipped) {
2183  idx_mbmode = get_vlc2(gb, v->mbmode_vlc, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
2184  if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
2185  twomv = 1;
2186  v->blk_mv_type[s->block_index[0]] = 1;
2187  v->blk_mv_type[s->block_index[1]] = 1;
2188  v->blk_mv_type[s->block_index[2]] = 1;
2189  v->blk_mv_type[s->block_index[3]] = 1;
2190  } else {
2191  v->blk_mv_type[s->block_index[0]] = 0;
2192  v->blk_mv_type[s->block_index[1]] = 0;
2193  v->blk_mv_type[s->block_index[2]] = 0;
2194  v->blk_mv_type[s->block_index[3]] = 0;
2195  }
2196  }
2197 
2198  if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
2199  for (i = 0; i < 4; i++) {
2200  s->mv[0][i][0] = s->cur_pic.motion_val[0][s->block_index[i]][0] = 0;
2201  s->mv[0][i][1] = s->cur_pic.motion_val[0][s->block_index[i]][1] = 0;
2202  s->mv[1][i][0] = s->cur_pic.motion_val[1][s->block_index[i]][0] = 0;
2203  s->mv[1][i][1] = s->cur_pic.motion_val[1][s->block_index[i]][1] = 0;
2204  }
2205  v->is_intra[s->mb_x] = 0x3f; // Set the bitfield to all 1.
2206  s->mb_intra = 1;
2207  s->cur_pic.mb_type[mb_pos] = MB_TYPE_INTRA;
2208  fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
2209  mb_has_coeffs = get_bits1(gb);
2210  if (mb_has_coeffs)
2211  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
2212  v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
2213  GET_MQUANT();
2214  s->cur_pic.qscale_table[mb_pos] = mquant;
2215  /* Set DC scale - y and c use the same so we only set y */
2216  s->y_dc_scale = ff_wmv3_dc_scale_table[FFABS(mquant)];
2217  dst_idx = 0;
2218  for (i = 0; i < 6; i++) {
2219  v->a_avail = v->c_avail = 0;
2220  v->mb_type[s->block_index[i]] = 1;
2221  s->dc_val[s->block_index[i]] = 0;
2222  dst_idx += i >> 2;
2223  val = ((cbp >> (5 - i)) & 1);
2224  if (i == 2 || i == 3 || !s->first_slice_line)
2225  v->a_avail = v->mb_type[s->block_index[i] - s->block_wrap[i]];
2226  if (i == 1 || i == 3 || s->mb_x)
2227  v->c_avail = v->mb_type[s->block_index[i] - 1];
2228 
2229  ret = vc1_decode_intra_block(v, v->blocks[i], i, val, mquant,
2230  (i & 4) ? v->codingset2 : v->codingset);
2231  if (ret < 0)
2232  return ret;
2233  if (CONFIG_GRAY && i > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2234  continue;
2235  v->vc1dsp.vc1_inv_trans_8x8(v->blocks[i]);
2236  if (i < 4) {
2237  stride_y = s->linesize << fieldtx;
2238  off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
2239  } else {
2240  stride_y = s->uvlinesize;
2241  off = 0;
2242  }
2243  s->idsp.put_signed_pixels_clamped(v->blocks[i],
2244  s->dest[dst_idx] + off,
2245  stride_y);
2246  }
2247  } else {
2248  s->mb_intra = v->is_intra[s->mb_x] = 0;
2249 
2250  if (v->dmb_is_raw)
2251  direct = get_bits1(gb);
2252  else
2253  direct = v->direct_mb_plane[mb_pos];
2254 
2255  if (direct) {
2256  if (s->next_pic.ptr->field_picture)
2257  av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2258  s->mv[0][0][0] = s->cur_pic.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
2259  s->mv[0][0][1] = s->cur_pic.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
2260  s->mv[1][0][0] = s->cur_pic.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
2261  s->mv[1][0][1] = s->cur_pic.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
2262 
2263  if (twomv) {
2264  s->mv[0][2][0] = s->cur_pic.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
2265  s->mv[0][2][1] = s->cur_pic.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
2266  s->mv[1][2][0] = s->cur_pic.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_pic.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
2267  s->mv[1][2][1] = s->cur_pic.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_pic.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
2268 
2269  for (i = 1; i < 4; i += 2) {
2270  s->mv[0][i][0] = s->cur_pic.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
2271  s->mv[0][i][1] = s->cur_pic.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
2272  s->mv[1][i][0] = s->cur_pic.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
2273  s->mv[1][i][1] = s->cur_pic.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
2274  }
2275  } else {
2276  for (i = 1; i < 4; i++) {
2277  s->mv[0][i][0] = s->cur_pic.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
2278  s->mv[0][i][1] = s->cur_pic.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
2279  s->mv[1][i][0] = s->cur_pic.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
2280  s->mv[1][i][1] = s->cur_pic.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
2281  }
2282  }
2283  }
2284 
2285  if (!direct) {
2286  if (skipped || !s->mb_intra) {
2287  bmvtype = decode012(gb);
2288  switch (bmvtype) {
2289  case 0:
2290  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2291  break;
2292  case 1:
2293  bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2294  break;
2295  case 2:
2296  bmvtype = BMV_TYPE_INTERPOLATED;
2297  }
2298  }
2299 
2300  if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
2301  mvsw = get_bits1(gb);
2302  }
2303 
2304  if (!skipped) { // inter MB
2305  mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
2306  if (mb_has_coeffs)
2307  cbp = 1 + get_vlc2(gb, v->cbpcy_vlc, VC1_CBPCY_P_VLC_BITS, 2);
2308  if (!direct) {
2309  if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
2311  } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
2313  }
2314  }
2315 
2316  for (i = 0; i < 6; i++)
2317  v->mb_type[s->block_index[i]] = 0;
2318  fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
2319  /* for all motion vector read MVDATA and motion compensate each block */
2320  dst_idx = 0;
2321  if (direct) {
2322  if (twomv) {
2323  for (i = 0; i < 4; i++) {
2324  ff_vc1_mc_4mv_luma(v, i, 0, 0);
2325  ff_vc1_mc_4mv_luma(v, i, 1, 1);
2326  }
2327  ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2328  ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2329  } else {
2330  ff_vc1_mc_1mv(v, 0);
2331  ff_vc1_interp_mc(v);
2332  }
2333  } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
2334  mvbp = v->fourmvbp;
2335  for (i = 0; i < 4; i++) {
2336  dir = i==1 || i==3;
2337  dmv_x = dmv_y = 0;
2338  val = ((mvbp >> (3 - i)) & 1);
2339  if (val)
2340  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2341  j = i > 1 ? 2 : 0;
2342  ff_vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, dir);
2343  ff_vc1_mc_4mv_luma(v, j, dir, dir);
2344  ff_vc1_mc_4mv_luma(v, j+1, dir, dir);
2345  }
2346 
2347  ff_vc1_mc_4mv_chroma4(v, 0, 0, 0);
2348  ff_vc1_mc_4mv_chroma4(v, 1, 1, 1);
2349  } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
2350  mvbp = v->twomvbp;
2351  dmv_x = dmv_y = 0;
2352  if (mvbp & 2)
2353  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2354 
2355  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, 0);
2356  ff_vc1_mc_1mv(v, 0);
2357 
2358  dmv_x = dmv_y = 0;
2359  if (mvbp & 1)
2360  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2361 
2362  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, 1);
2363  ff_vc1_interp_mc(v);
2364  } else if (twomv) {
2365  dir = bmvtype == BMV_TYPE_BACKWARD;
2366  dir2 = dir;
2367  if (mvsw)
2368  dir2 = !dir;
2369  mvbp = v->twomvbp;
2370  dmv_x = dmv_y = 0;
2371  if (mvbp & 2)
2372  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2373  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, dir);
2374 
2375  dmv_x = dmv_y = 0;
2376  if (mvbp & 1)
2377  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2378  ff_vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, dir2);
2379 
2380  if (mvsw) {
2381  for (i = 0; i < 2; i++) {
2382  s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->cur_pic.motion_val[dir][s->block_index[i+2]][0] = s->cur_pic.motion_val[dir][s->block_index[i]][0];
2383  s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->cur_pic.motion_val[dir][s->block_index[i+2]][1] = s->cur_pic.motion_val[dir][s->block_index[i]][1];
2384  s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->cur_pic.motion_val[dir2][s->block_index[i]][0] = s->cur_pic.motion_val[dir2][s->block_index[i+2]][0];
2385  s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->cur_pic.motion_val[dir2][s->block_index[i]][1] = s->cur_pic.motion_val[dir2][s->block_index[i+2]][1];
2386  }
2387  } else {
2388  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, !dir);
2389  ff_vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, !dir);
2390  }
2391 
2392  ff_vc1_mc_4mv_luma(v, 0, dir, 0);
2393  ff_vc1_mc_4mv_luma(v, 1, dir, 0);
2394  ff_vc1_mc_4mv_luma(v, 2, dir2, 0);
2395  ff_vc1_mc_4mv_luma(v, 3, dir2, 0);
2396  ff_vc1_mc_4mv_chroma4(v, dir, dir2, 0);
2397  } else {
2398  dir = bmvtype == BMV_TYPE_BACKWARD;
2399 
2400  mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
2401  dmv_x = dmv_y = 0;
2402  if (mvbp)
2403  get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
2404 
2405  ff_vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, dir);
2406  v->blk_mv_type[s->block_index[0]] = 1;
2407  v->blk_mv_type[s->block_index[1]] = 1;
2408  v->blk_mv_type[s->block_index[2]] = 1;
2409  v->blk_mv_type[s->block_index[3]] = 1;
2410  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, !dir);
2411  for (i = 0; i < 2; i++) {
2412  s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->cur_pic.motion_val[!dir][s->block_index[i+2]][0] = s->cur_pic.motion_val[!dir][s->block_index[i]][0];
2413  s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->cur_pic.motion_val[!dir][s->block_index[i+2]][1] = s->cur_pic.motion_val[!dir][s->block_index[i]][1];
2414  }
2415  ff_vc1_mc_1mv(v, dir);
2416  }
2417 
2418  if (cbp)
2419  GET_MQUANT(); // p. 227
2420  s->cur_pic.qscale_table[mb_pos] = mquant;
2421  if (!v->ttmbf && cbp)
2422  ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index], VC1_TTMB_VLC_BITS, 2);
2423  for (i = 0; i < 6; i++) {
2424  s->dc_val[s->block_index[i]] = 0;
2425  dst_idx += i >> 2;
2426  val = ((cbp >> (5 - i)) & 1);
2427  if (!fieldtx)
2428  off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2429  else
2430  off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
2431  if (val) {
2432  pat = vc1_decode_p_block(v, v->blocks[i], i, mquant, ttmb,
2433  first_block, s->dest[dst_idx] + off,
2434  (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
2435  CONFIG_GRAY && (i & 4) && (s->avctx->flags & AV_CODEC_FLAG_GRAY), &block_tt);
2436  if (pat < 0)
2437  return pat;
2438  block_cbp |= pat << (i << 2);
2439  if (!v->ttmbf && ttmb < 8)
2440  ttmb = -1;
2441  first_block = 0;
2442  }
2443  }
2444 
2445  } else { // skipped
2446  dir = 0;
2447  for (i = 0; i < 6; i++) {
2448  v->mb_type[s->block_index[i]] = 0;
2449  s->dc_val[s->block_index[i]] = 0;
2450  }
2451  s->cur_pic.mb_type[mb_pos] = MB_TYPE_SKIP;
2452  s->cur_pic.qscale_table[mb_pos] = 0;
2453  v->blk_mv_type[s->block_index[0]] = 0;
2454  v->blk_mv_type[s->block_index[1]] = 0;
2455  v->blk_mv_type[s->block_index[2]] = 0;
2456  v->blk_mv_type[s->block_index[3]] = 0;
2457 
2458  if (!direct) {
2459  if (bmvtype == BMV_TYPE_INTERPOLATED) {
2460  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, 0);
2461  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, 1);
2462  } else {
2463  dir = bmvtype == BMV_TYPE_BACKWARD;
2464  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, dir);
2465  if (mvsw) {
2466  int dir2 = dir;
2467  if (mvsw)
2468  dir2 = !dir;
2469  for (i = 0; i < 2; i++) {
2470  s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->cur_pic.motion_val[dir][s->block_index[i+2]][0] = s->cur_pic.motion_val[dir][s->block_index[i]][0];
2471  s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->cur_pic.motion_val[dir][s->block_index[i+2]][1] = s->cur_pic.motion_val[dir][s->block_index[i]][1];
2472  s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->cur_pic.motion_val[dir2][s->block_index[i]][0] = s->cur_pic.motion_val[dir2][s->block_index[i+2]][0];
2473  s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->cur_pic.motion_val[dir2][s->block_index[i]][1] = s->cur_pic.motion_val[dir2][s->block_index[i+2]][1];
2474  }
2475  } else {
2476  v->blk_mv_type[s->block_index[0]] = 1;
2477  v->blk_mv_type[s->block_index[1]] = 1;
2478  v->blk_mv_type[s->block_index[2]] = 1;
2479  v->blk_mv_type[s->block_index[3]] = 1;
2480  ff_vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, !dir);
2481  for (i = 0; i < 2; i++) {
2482  s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->cur_pic.motion_val[!dir][s->block_index[i+2]][0] = s->cur_pic.motion_val[!dir][s->block_index[i]][0];
2483  s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->cur_pic.motion_val[!dir][s->block_index[i+2]][1] = s->cur_pic.motion_val[!dir][s->block_index[i]][1];
2484  }
2485  }
2486  }
2487  }
2488 
2489  ff_vc1_mc_1mv(v, dir);
2490  if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
2491  ff_vc1_interp_mc(v);
2492  }
2493  v->fieldtx_plane[mb_pos] = 0;
2494  }
2495  }
2496  v->cbp[s->mb_x] = block_cbp;
2497  v->ttblk[s->mb_x] = block_tt;
2498 
2499  return 0;
2500 }
2501 
2502 /** Decode blocks of I-frame
2503  */
2505 {
2506  int k, j;
2507  MpegEncContext *s = &v->s;
2508  int cbp, val;
2509  int mb_pos;
2510 
2511  /* select coding mode used for VLC tables selection */
2512  switch (v->y_ac_table_index) {
2513  case 0:
2515  break;
2516  case 1:
2518  break;
2519  case 2:
2521  break;
2522  }
2523 
2524  switch (v->c_ac_table_index) {
2525  case 0:
2527  break;
2528  case 1:
2530  break;
2531  case 2:
2533  break;
2534  }
2535 
2536  /* Set DC scale - y and c use the same so we only set y */
2537  s->y_dc_scale = ff_wmv3_dc_scale_table[v->pq];
2538 
2539  //do frame decode
2540  s->mb_x = s->mb_y = 0;
2541  s->mb_intra = 1;
2542  s->first_slice_line = 1;
2543  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2544  s->mb_x = 0;
2545  init_block_index(v);
2546  for (; s->mb_x < v->end_mb_x; s->mb_x++) {
2548  s->bdsp.clear_blocks(v->block[v->cur_blk_idx][0]);
2549  mb_pos = s->mb_x + s->mb_y * s->mb_width;
2550  s->cur_pic.mb_type[mb_pos] = MB_TYPE_INTRA;
2551  s->cur_pic.qscale_table[mb_pos] = v->pq;
2552  for (int i = 0; i < 4; i++) {
2553  s->cur_pic.motion_val[1][s->block_index[i]][0] = 0;
2554  s->cur_pic.motion_val[1][s->block_index[i]][1] = 0;
2555  }
2556 
2557  // do actual MB decoding and displaying
2558  cbp = get_vlc2(&v->gb, ff_msmp4_mb_i_vlc,
2560  v->s.ac_pred = get_bits1(&v->gb);
2561 
2562  for (k = 0; k < 6; k++) {
2563  v->mb_type[s->block_index[k]] = 1;
2564 
2565  val = ((cbp >> (5 - k)) & 1);
2566 
2567  if (k < 4)
2568  val = vc1_coded_block_pred(&v->s, k, val);
2569  cbp |= val << (5 - k);
2570 
2571  vc1_decode_i_block(v, v->block[v->cur_blk_idx][block_map[k]], k, val, (k < 4) ? v->codingset : v->codingset2);
2572 
2573  if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2574  continue;
2576  }
2577 
2578  if (v->overlap && v->pq >= 9) {
2580  if (v->rangeredfrm)
2581  for (k = 0; k < 6; k++)
2582  for (j = 0; j < 64; j++)
2583  v->block[v->cur_blk_idx][block_map[k]][j] *= 2;
2584  vc1_put_blocks_clamped(v, 1);
2585  } else {
2586  if (v->rangeredfrm)
2587  for (k = 0; k < 6; k++)
2588  for (j = 0; j < 64; j++)
2589  v->block[v->cur_blk_idx][block_map[k]][j] = (v->block[v->cur_blk_idx][block_map[k]][j] - 64) * 2;
2590  vc1_put_blocks_clamped(v, 0);
2591  }
2592 
2593  if (v->loop_filter)
2595 
2596  if (get_bits_left(&v->gb) < 0) {
2597  ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
2598  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2599  get_bits_count(&v->gb), v->gb.size_in_bits);
2600  return;
2601  }
2602 
2603  v->topleft_blk_idx = (v->topleft_blk_idx + 1) % (v->end_mb_x + 2);
2604  v->top_blk_idx = (v->top_blk_idx + 1) % (v->end_mb_x + 2);
2605  v->left_blk_idx = (v->left_blk_idx + 1) % (v->end_mb_x + 2);
2606  v->cur_blk_idx = (v->cur_blk_idx + 1) % (v->end_mb_x + 2);
2607  }
2608 
2609  s->first_slice_line = 0;
2610  }
2611 
2612  /* This is intentionally mb_height and not end_mb_y - unlike in advanced
2613  * profile, these only differ are when decoding MSS2 rectangles. */
2614  ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
2615 }
2616 
2617 /** Decode blocks of I-frame for advanced profile
2618  */
2620 {
2621  int k;
2622  MpegEncContext *s = &v->s;
2623  GetBitContext *const gb = &v->gb;
2624  int cbp, val;
2625  int mb_pos;
2626  int mquant;
2627  int mqdiff;
2628 
2629  if (get_bits_left(gb) <= 1)
2630  return AVERROR_INVALIDDATA;
2631 
2632  /* select coding mode used for VLC tables selection */
2633  switch (v->y_ac_table_index) {
2634  case 0:
2636  break;
2637  case 1:
2639  break;
2640  case 2:
2642  break;
2643  }
2644 
2645  switch (v->c_ac_table_index) {
2646  case 0:
2648  break;
2649  case 1:
2651  break;
2652  case 2:
2654  break;
2655  }
2656 
2657  // do frame decode
2658  s->mb_intra = 1;
2659  s->first_slice_line = 1;
2660  s->mb_x = 0;
2661  s->mb_y = s->start_mb_y;
2662  if (s->start_mb_y) {
2663  memset(&s->coded_block[(2 * s->mb_y - 1) * s->b8_stride - 2], 0,
2664  (1 + s->b8_stride) * sizeof(*s->coded_block));
2665  }
2666  for (; s->mb_y < s->end_mb_y; s->mb_y++) {
2667  s->mb_x = 0;
2668  init_block_index(v);
2669  for (;s->mb_x < s->mb_width; s->mb_x++) {
2670  mquant = v->pq;
2672  s->bdsp.clear_blocks(v->block[v->cur_blk_idx][0]);
2673  mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2674  s->cur_pic.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
2675  for (int i = 0; i < 4; i++) {
2676  s->cur_pic.motion_val[1][s->block_index[i] + v->blocks_off][0] = 0;
2677  s->cur_pic.motion_val[1][s->block_index[i] + v->blocks_off][1] = 0;
2678  }
2679 
2680  // do actual MB decoding and displaying
2681  if (v->fieldtx_is_raw)
2682  v->fieldtx_plane[mb_pos] = get_bits1(gb);
2683  if (get_bits_left(gb) <= 1) {
2684  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2685  return 0;
2686  }
2687 
2688  cbp = get_vlc2(gb, ff_msmp4_mb_i_vlc,
2690  if (v->acpred_is_raw)
2691  v->s.ac_pred = get_bits1(gb);
2692  else
2693  v->s.ac_pred = v->acpred_plane[mb_pos];
2694 
2695  if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
2696  v->over_flags_plane[mb_pos] = get_bits1(gb);
2697 
2698  GET_MQUANT();
2699 
2700  s->cur_pic.qscale_table[mb_pos] = mquant;
2701  /* Set DC scale - y and c use the same so we only set y */
2702  s->y_dc_scale = ff_wmv3_dc_scale_table[FFABS(mquant)];
2703 
2704  for (k = 0; k < 6; k++) {
2705  v->mb_type[s->block_index[k]] = 1;
2706 
2707  val = ((cbp >> (5 - k)) & 1);
2708 
2709  if (k < 4)
2710  val = vc1_coded_block_pred(&v->s, k, val);
2711  cbp |= val << (5 - k);
2712 
2713  v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
2714  v->c_avail = !!s->mb_x || (k == 1 || k == 3);
2715 
2717  (k < 4) ? v->codingset : v->codingset2, mquant);
2718 
2719  if (CONFIG_GRAY && k > 3 && (s->avctx->flags & AV_CODEC_FLAG_GRAY))
2720  continue;
2722  }
2723 
2724  if (v->overlap && (v->pq >= 9 || v->condover != CONDOVER_NONE))
2726  vc1_put_blocks_clamped(v, 1);
2727  if (v->loop_filter)
2729 
2730  if (get_bits_left(gb) < 0) {
2731  // TODO: may need modification to handle slice coding
2732  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2733  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
2734  get_bits_count(gb), gb->size_in_bits);
2735  return 0;
2736  }
2741  }
2742  s->first_slice_line = 0;
2743  }
2744 
2745  ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2746  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2747  return 0;
2748 }
2749 
2751 {
2752  MpegEncContext *s = &v->s;
2753  int apply_loop_filter;
2754  int ret;
2755 
2756  /* select coding mode used for VLC tables selection */
2757  switch (v->c_ac_table_index) {
2758  case 0:
2760  break;
2761  case 1:
2763  break;
2764  case 2:
2766  break;
2767  }
2768 
2769  switch (v->c_ac_table_index) {
2770  case 0:
2772  break;
2773  case 1:
2775  break;
2776  case 2:
2778  break;
2779  }
2780 
2781  apply_loop_filter = v->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2782  s->first_slice_line = 1;
2783  memset(v->cbp_base, 0, sizeof(v->cbp_base[0]) * 3 * s->mb_stride);
2784  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2785  s->mb_x = 0;
2786  init_block_index(v);
2787  for (; s->mb_x < s->mb_width; s->mb_x++) {
2789 
2790  if (v->fcm == ILACE_FIELD || (v->fcm == PROGRESSIVE && v->mv_type_is_raw) || v->skip_is_raw)
2791  if (get_bits_left(&v->gb) <= 1) {
2792  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2793  return;
2794  }
2795 
2796  if (v->fcm == ILACE_FIELD) {
2798  if (apply_loop_filter)
2800  } else if (v->fcm == ILACE_FRAME) {
2802  if (apply_loop_filter)
2804  } else {
2805  ret = vc1_decode_p_mb(v);
2806  if (apply_loop_filter)
2808  }
2809  if (ret < 0 || get_bits_left(&v->gb) < 0 || get_bits_count(&v->gb) < 0) {
2810  // TODO: may need modification to handle slice coding
2811  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2812  av_log(s->avctx, AV_LOG_ERROR, "Error or Bits overconsumption: %i > %i at %ix%i\n",
2813  get_bits_count(&v->gb), v->gb.size_in_bits, s->mb_x, s->mb_y);
2814  return;
2815  }
2820  }
2821  memmove(v->cbp_base,
2822  v->cbp - s->mb_stride,
2823  sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2824  memmove(v->ttblk_base,
2825  v->ttblk - s->mb_stride,
2826  sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2827  memmove(v->is_intra_base,
2828  v->is_intra - s->mb_stride,
2829  sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2830  memmove(v->luma_mv_base,
2831  v->luma_mv - s->mb_stride,
2832  sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
2833  s->first_slice_line = 0;
2834  }
2835  ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2836  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2837 }
2838 
2840 {
2841  MpegEncContext *s = &v->s;
2842 
2843  /* select coding mode used for VLC tables selection */
2844  switch (v->c_ac_table_index) {
2845  case 0:
2847  break;
2848  case 1:
2850  break;
2851  case 2:
2853  break;
2854  }
2855 
2856  switch (v->c_ac_table_index) {
2857  case 0:
2859  break;
2860  case 1:
2862  break;
2863  case 2:
2865  break;
2866  }
2867 
2868  s->first_slice_line = 1;
2869  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2870  s->mb_x = 0;
2871  init_block_index(v);
2872  for (; s->mb_x < s->mb_width; s->mb_x++) {
2874 
2875  if (v->fcm == ILACE_FIELD || v->skip_is_raw || v->dmb_is_raw)
2876  if (get_bits_left(&v->gb) <= 1) {
2877  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2878  return;
2879  }
2880 
2881  if (v->fcm == ILACE_FIELD) {
2883  if (v->loop_filter)
2885  } else if (v->fcm == ILACE_FRAME) {
2887  if (v->loop_filter)
2889  } else {
2890  vc1_decode_b_mb(v);
2891  if (v->loop_filter)
2893  }
2894  if (get_bits_left(&v->gb) < 0 || get_bits_count(&v->gb) < 0) {
2895  // TODO: may need modification to handle slice coding
2896  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
2897  av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
2898  get_bits_count(&v->gb), v->gb.size_in_bits, s->mb_x, s->mb_y);
2899  return;
2900  }
2901  }
2902  memmove(v->cbp_base,
2903  v->cbp - s->mb_stride,
2904  sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
2905  memmove(v->ttblk_base,
2906  v->ttblk - s->mb_stride,
2907  sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
2908  memmove(v->is_intra_base,
2909  v->is_intra - s->mb_stride,
2910  sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
2911  s->first_slice_line = 0;
2912  }
2913  ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
2914  (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
2915 }
2916 
2918 {
2919  MpegEncContext *s = &v->s;
2920 
2921  if (!v->s.last_pic.data[0])
2922  return;
2923 
2924  ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
2925  s->first_slice_line = 1;
2926  for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
2927  s->mb_x = 0;
2928  init_block_index(v);
2930  memcpy(s->dest[0], s->last_pic.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2931  memcpy(s->dest[1], s->last_pic.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2932  memcpy(s->dest[2], s->last_pic.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2933  s->first_slice_line = 0;
2934  }
2935 }
2936 
2938 {
2939 
2940  v->esc3_level_length = 0;
2941  if (v->x8_type) {
2943  &v->gb, &v->s.mb_x, &v->s.mb_y,
2944  2 * v->pq + v->halfpq, v->pq * !v->pquantizer,
2945  v->loop_filter, v->s.low_delay);
2946 
2947  ff_er_add_slice(&v->s.er, 0, 0,
2948  (v->s.mb_x >> 1) - 1, (v->s.mb_y >> 1) - 1,
2949  ER_MB_END);
2950  } else {
2951  v->cur_blk_idx = 0;
2952  v->left_blk_idx = -1;
2953  v->topleft_blk_idx = 1;
2954  v->top_blk_idx = 2;
2955  switch (v->s.pict_type) {
2956  case AV_PICTURE_TYPE_I:
2957  if (v->profile == PROFILE_ADVANCED)
2959  else
2961  break;
2962  case AV_PICTURE_TYPE_P:
2963  if (v->p_frame_skipped)
2965  else
2967  break;
2968  case AV_PICTURE_TYPE_B:
2969  if (v->bi_type) {
2970  if (v->profile == PROFILE_ADVANCED)
2972  else
2974  } else
2976  break;
2977  }
2978  }
2979 }
vc1_decode_p_block
static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n, int mquant, int ttmb, int first_block, uint8_t *dst, int linesize, int skip_block, int *ttmb_out)
Decode P block.
Definition: vc1_block.c:1080
VC1Context::zz_8x8
uint8_t zz_8x8[4][64]
Zigzag table for TT_8x8, permuted for IDCT.
Definition: vc1.h:243
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:216
q1
static const uint8_t q1[256]
Definition: twofish.c:100
level
uint8_t level
Definition: svq3.c:208
MpegEncContext::mb_y
int mb_y
Definition: mpegvideo.h:191
vc1_index_decode_table
static const uint8_t vc1_index_decode_table[AC_MODES][185][2]
Definition: vc1acdata.h:34
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
VC1Context
The VC1 Context.
Definition: vc1.h:176
PROGRESSIVE
@ PROGRESSIVE
in the bitstream is reported as 00b
Definition: vc1.h:152
ff_vc1_ttblk_to_tt
const int ff_vc1_ttblk_to_tt[3][8]
Table for conversion between TTBLK and TTMB.
Definition: vc1data.c:34
VC1Context::condover
uint8_t condover
Definition: vc1.h:331
VC1Context::left_blk_idx
int left_blk_idx
Definition: vc1.h:393
AC_VLC_BITS
#define AC_VLC_BITS
Definition: intrax8.c:42
block_map
static const int block_map[6]
Definition: vc1_block.c:47
VC1Context::topleft_blk_idx
int topleft_blk_idx
Definition: vc1.h:393
VC1Context::cbp
uint32_t * cbp
Definition: vc1.h:394
VC1Context::end_mb_x
int end_mb_x
Horizontal macroblock limit (used only by mss2)
Definition: vc1.h:401
vc1_b_mc
static void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
Reconstruct motion vector for B-frame and do motion compensation.
Definition: vc1_block.c:318
VC1Context::overlap
int overlap
overlapped transforms in use
Definition: vc1.h:229
get_mvdata_interlaced
static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x, int *dmv_y, int *pred_flag)
Definition: vc1_block.c:267
ff_intrax8_decode_picture
int ff_intrax8_decode_picture(IntraX8Context *w, MPVPicture *pict, GetBitContext *gb, int *mb_x, int *mb_y, int dquant, int quant_offset, int loopfilter, int lowdelay)
Decode single IntraX8 frame.
Definition: intrax8.c:718
vc1.h
av_clip_uintp2
#define av_clip_uintp2
Definition: common.h:124
ILACE_FRAME
@ ILACE_FRAME
in the bitstream is reported as 10b
Definition: vc1.h:153
vc1_decode_p_blocks
static void vc1_decode_p_blocks(VC1Context *v)
Definition: vc1_block.c:2750
GET_MVDATA
#define GET_MVDATA(_dmv_x, _dmv_y)
Get MV differentials.
Definition: vc1_block.c:229
VC1Context::dc_table_index
int dc_table_index
Definition: vc1.h:254
VC1DSPContext::vc1_inv_trans_4x4
void(* vc1_inv_trans_4x4)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:42
vc1_i_pred_dc
static int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n, int16_t **dc_val_ptr, int *dir_ptr)
Get predicted DC value for I-frames only prediction dir: left=0, top=1.
Definition: vc1_block.c:344
decode210
static int BS_FUNC() decode210(BSCTX *bc)
Return decoded truncated unary code for the values 2, 1, 0.
Definition: bitstream_template.h:455
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:254
TT_8X4_BOTTOM
@ TT_8X4_BOTTOM
Definition: vc1.h:116
GetBitContext::size_in_bits
int size_in_bits
Definition: get_bits.h:112
TT_8X4_TOP
@ TT_8X4_TOP
Definition: vc1.h:117
VC1Context::left_blk_sh
int left_blk_sh
Definition: vc1.h:244
BMV_TYPE_DIRECT
@ BMV_TYPE_DIRECT
Definition: vc1.h:108
mode
Definition: swscale.c:56
vc1acdata.h
ff_vc1_mc_4mv_luma
void ff_vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
Do motion compensation for 4-MV macroblock - luminance block.
Definition: vc1_mc.c:452
VC1_IF_MBMODE_VLC_BITS
#define VC1_IF_MBMODE_VLC_BITS
Definition: vc1data.h:96
MpegEncContext::pict_type
enum AVPictureType pict_type
AV_PICTURE_TYPE_I, AV_PICTURE_TYPE_P, AV_PICTURE_TYPE_B, ...
Definition: mpegvideo.h:154
vc1_decode_i_block
static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n, int coded, int codingset)
Decode intra block in intra frames - should be faster than decode_intra_block.
Definition: vc1_block.c:581
CS_HIGH_RATE_INTER
@ CS_HIGH_RATE_INTER
Definition: vc1.h:134
MSMP4_MB_INTRA_VLC_BITS
#define MSMP4_MB_INTRA_VLC_BITS
Definition: msmpeg4_vc1_data.h:36
b
#define b
Definition: input.c:42
VC1_SUBBLKPAT_VLC_BITS
#define VC1_SUBBLKPAT_VLC_BITS
Definition: vc1data.h:79
VC1Context::cbp_base
uint32_t * cbp_base
Definition: vc1.h:394
MB_TYPE_16x16
#define MB_TYPE_16x16
Definition: mpegutils.h:41
VC1Context::mv_type_mb_plane
uint8_t * mv_type_mb_plane
bitplane for mv_type == (4MV)
Definition: vc1.h:294
GET_MQUANT
#define GET_MQUANT()
Get macroblock-level quantizer scale.
Definition: vc1_block.c:186
ff_vc1_adv_interlaced_4x8_zz
const uint8_t ff_vc1_adv_interlaced_4x8_zz[32]
Definition: vc1data.c:225
VC1Context::zz_8x4
const uint8_t * zz_8x4
Zigzag scan table for TT_8x4 coding mode.
Definition: vc1.h:245
VC1_CBPCY_P_VLC_BITS
#define VC1_CBPCY_P_VLC_BITS
Definition: vc1data.h:69
ff_er_add_slice
void ff_er_add_slice(ERContext *s, int startx, int starty, int endx, int endy, int status)
Add a slice.
Definition: error_resilience.c:828
ff_init_block_index
void ff_init_block_index(MpegEncContext *s)
Definition: mpegvideo.c:472
VC1DSPContext::vc1_inv_trans_8x8_dc
void(* vc1_inv_trans_8x8_dc)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:43
mpegvideo.h
VC1Context::dmvrange
uint8_t dmvrange
Frame decoding info for interlaced picture.
Definition: vc1.h:339
VC1Context::loop_filter
int loop_filter
Definition: vc1.h:223
VC1DSPContext::vc1_inv_trans_4x4_dc
void(* vc1_inv_trans_4x4_dc)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:46
VC1Context::luma_mv
int16_t((* luma_mv)[2]
Definition: vc1.h:396
mpegutils.h
vc1_decode_p_mb_intfr
static int vc1_decode_p_mb_intfr(VC1Context *v)
Definition: vc1_block.c:1489
CS_HIGH_RATE_INTRA
@ CS_HIGH_RATE_INTRA
Definition: vc1.h:133
ff_vc1_interp_mc
void ff_vc1_interp_mc(VC1Context *v)
Motion compensation for direct or interpolated blocks in B-frames.
Definition: vc1_mc.c:1004
MpegEncContext::mbskip_table
uint8_t * mbskip_table
used to avoid copy if macroblock skipped (for black regions for example) and used for B-frame encodin...
Definition: mpegvideo.h:144
VC1Context::fieldtx_is_raw
int fieldtx_is_raw
Definition: vc1.h:351
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:337
ILACE_FIELD
@ ILACE_FIELD
in the bitstream is reported as 11b
Definition: vc1.h:154
VC1_INTFR_NON4MV_MBMODE_VLC_BITS
#define VC1_INTFR_NON4MV_MBMODE_VLC_BITS
Definition: vc1data.h:83
wrap
#define wrap(func)
Definition: neontest.h:65
CS_LOW_MOT_INTER
@ CS_LOW_MOT_INTER
Definition: vc1.h:130
GetBitContext
Definition: get_bits.h:109
VC1Context::numref
int numref
number of past field pictures used as reference
Definition: vc1.h:360
val
static double val(void *priv, double ch)
Definition: aeval.c:77
MV_PMODE_INTFR_2MV_FIELD
@ MV_PMODE_INTFR_2MV_FIELD
Definition: vc1.h:94
ff_vc1_p_overlap_filter
void ff_vc1_p_overlap_filter(VC1Context *v)
Definition: vc1_loopfilter.c:161
init_block_index
static void init_block_index(VC1Context *v)
Definition: vc1_block.c:57
VC1_TTMB_VLC_BITS
#define VC1_TTMB_VLC_BITS
Definition: vc1data.h:65
vc1_put_blocks_clamped
static void vc1_put_blocks_clamped(VC1Context *v, int put_signed)
Definition: vc1_block.c:77
VC1Context::c_ac_table_index
int c_ac_table_index
AC coding set indexes.
Definition: vc1.h:259
VC1Context::k_y
int k_y
Number of bits for MVs (depends on MV range)
Definition: vc1.h:240
VC1Context::imv_vlc
const VLCElem * imv_vlc
Definition: vc1.h:345
ff_vc1_decode_blocks
void ff_vc1_decode_blocks(VC1Context *v)
Definition: vc1_block.c:2937
VC1Context::esc3_level_length
int esc3_level_length
Definition: vc1.h:262
CONDOVER_NONE
@ CONDOVER_NONE
Definition: vc1.h:140
quant
static const uint8_t quant[64]
Definition: vmixdec.c:71
CS_MID_RATE_INTER
@ CS_MID_RATE_INTER
Definition: vc1.h:132
mpegvideodec.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:210
ff_wmv3_dc_scale_table
const uint8_t ff_wmv3_dc_scale_table[32]
Definition: vc1data.c:173
VC1Context::mb_type
uint8_t * mb_type
Definition: vc1.h:271
VC1DSPContext::vc1_inv_trans_8x4_dc
void(* vc1_inv_trans_8x4_dc)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:44
VC1Context::twomvbp_vlc
const VLCElem * twomvbp_vlc
Definition: vc1.h:346
MpegEncContext::cur_pic
MPVWorkPicture cur_pic
copy of the current picture structure.
Definition: mpegvideo.h:132
ff_vc1_pred_b_mv_intfi
void ff_vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
Definition: vc1_pred.c:889
s
#define s(width, name)
Definition: cbs_vp9.c:198
MPVWorkPicture::ptr
MPVPicture * ptr
RefStruct reference.
Definition: mpegpicture.h:99
ff_vc1_mc_1mv
void ff_vc1_mc_1mv(VC1Context *v, int dir)
Do motion compensation over 1 macroblock Mostly adapted hpel_motion and qpel_motion from mpegvideo....
Definition: vc1_mc.c:172
VC1Context::x8
IntraX8Context x8
Definition: vc1.h:179
vc1_decode_b_blocks
static void vc1_decode_b_blocks(VC1Context *v)
Definition: vc1_block.c:2839
VC1Context::over_flags_plane
uint8_t * over_flags_plane
Overflags bitplane.
Definition: vc1.h:329
bits
uint8_t bits
Definition: vp3data.h:128
TT_8X4
@ TT_8X4
Definition: vc1.h:118
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:41
PROFILE_ADVANCED
@ PROFILE_ADVANCED
Definition: vc1_common.h:52
vc1_decode_ac_coeff
static int vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
Decode one AC coefficient.
Definition: vc1_block.c:514
vc1_decode_i_block_adv
static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n, int coded, int codingset, int mquant)
Decode intra block in intra frames - should be faster than decode_intra_block.
Definition: vc1_block.c:708
VC1Context::tt_index
int tt_index
Index for Transform Type tables (to decode TTMB)
Definition: vc1.h:292
ff_vc1_i_overlap_filter
void ff_vc1_i_overlap_filter(VC1Context *v)
Definition: vc1_loopfilter.c:105
CS_HIGH_MOT_INTER
@ CS_HIGH_MOT_INTER
Definition: vc1.h:128
TT_4X8_LEFT
@ TT_4X8_LEFT
Definition: vc1.h:120
B_FRACTION_DEN
#define B_FRACTION_DEN
Definition: vc1data.h:100
ff_vc1_mc_4mv_chroma4
void ff_vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
Definition: vc1_mc.c:839
ff_vc1_ttblk_vlc
const VLCElem * ff_vc1_ttblk_vlc[3]
Definition: vc1data.c:115
size_table
static const uint8_t size_table[6]
Definition: vc1_block.c:1248
VC1Context::top_blk_sh
int top_blk_sh
Either 3 or 0, positions of l/t in blk[].
Definition: vc1.h:244
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:74
if
if(ret)
Definition: filter_design.txt:179
VC1_INTFR_4MV_MBMODE_VLC_BITS
#define VC1_INTFR_4MV_MBMODE_VLC_BITS
Definition: vc1data.h:81
VC1Context::pq
uint8_t pq
Definition: vc1.h:242
VC1_1REF_MVDATA_VLC_BITS
#define VC1_1REF_MVDATA_VLC_BITS
Definition: vc1data.h:89
VC1Context::forward_mb_plane
uint8_t * forward_mb_plane
bitplane for "forward" MBs
Definition: vc1.h:296
vc1_decode_p_mb_intfi
static int vc1_decode_p_mb_intfi(VC1Context *v)
Definition: vc1_block.c:1697
VC1Context::pqindex
int pqindex
raw pqindex used in coding set selection
Definition: vc1.h:269
VC1Context::skip_is_raw
int skip_is_raw
skip mb plane is not coded
Definition: vc1.h:300
NULL
#define NULL
Definition: coverity.c:32
vc1_decode_b_mb_intfi
static int vc1_decode_b_mb_intfi(VC1Context *v)
Decode one B-frame MB (in interlaced field B picture)
Definition: vc1_block.c:1983
run
uint8_t run
Definition: svq3.c:207
VC1Context::direct_mb_plane
uint8_t * direct_mb_plane
bitplane for "direct" MBs
Definition: vc1.h:295
VC1DSPContext::vc1_inv_trans_8x4
void(* vc1_inv_trans_8x4)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:40
vc1_decode_b_mb
static int vc1_decode_b_mb(VC1Context *v)
Decode one B-frame MB (in Main profile)
Definition: vc1_block.c:1822
VC1Context::field_mode
int field_mode
1 for interlaced field pictures
Definition: vc1.h:356
MPVWorkPicture::data
uint8_t * data[MPV_MAX_PLANES]
Definition: mpegpicture.h:96
VC1Context::a_avail
int a_avail
Definition: vc1.h:270
ER_MB_ERROR
#define ER_MB_ERROR
Definition: error_resilience.h:36
VC1Context::block
int16_t(* block)[6][64]
Definition: vc1.h:392
AV_PICTURE_TYPE_I
@ AV_PICTURE_TYPE_I
Intra.
Definition: avutil.h:278
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:391
CS_LOW_MOT_INTRA
@ CS_LOW_MOT_INTRA
Definition: vc1.h:129
VC1Context::fmb_is_raw
int fmb_is_raw
forward mb plane is raw
Definition: vc1.h:299
VC1Context::mbmode_vlc
const VLCElem * mbmode_vlc
Definition: vc1.h:344
VC1Context::vc1dsp
VC1DSPContext vc1dsp
Definition: vc1.h:181
VC1Context::cbpcy_vlc
const VLCElem * cbpcy_vlc
CBPCY VLC table.
Definition: vc1.h:291
offset_table
static const uint8_t offset_table[2][9]
Definition: vc1_block.c:41
abs
#define abs(x)
Definition: cuda_runtime.h:35
inc_blk_idx
#define inc_blk_idx(idx)
Definition: vc1_block.c:169
VC1Context::luma_mv_base
int16_t(* luma_mv_base)[2]
Definition: vc1.h:396
VC1_ICBPCY_VLC_BITS
#define VC1_ICBPCY_VLC_BITS
Definition: vc1data.h:71
VC1Context::esc3_run_length
int esc3_run_length
Definition: vc1.h:263
get_vlc2
static av_always_inline int get_vlc2(GetBitContext *s, const VLCElem *table, int bits, int max_depth)
Parse a vlc code.
Definition: get_bits.h:651
VC1Context::halfpq
uint8_t halfpq
Uniform quant over image and qp+.5.
Definition: vc1.h:280
VC1DSPContext::vc1_inv_trans_4x8_dc
void(* vc1_inv_trans_4x8_dc)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:45
VC1Context::ttmbf
uint8_t ttmbf
Transform type flag.
Definition: vc1.h:265
index
int index
Definition: gxfenc.c:90
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
ff_vc1_i_loop_filter
void ff_vc1_i_loop_filter(VC1Context *v)
Definition: vc1_loopfilter.c:271
ff_vc1_simple_progressive_4x4_zz
const uint8_t ff_vc1_simple_progressive_4x4_zz[16]
Definition: vc1data.c:182
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
VC1Context::fieldtx_plane
uint8_t * fieldtx_plane
Definition: vc1.h:350
VC1Context::fourmvbp
uint8_t fourmvbp
Definition: vc1.h:349
ff_msmp4_mb_i_vlc
VLCElem ff_msmp4_mb_i_vlc[536]
Definition: msmpeg4_vc1_data.c:35
AVDISCARD_NONKEY
@ AVDISCARD_NONKEY
discard all frames except keyframes
Definition: defs.h:231
ff_vc1_p_intfr_loop_filter
void ff_vc1_p_intfr_loop_filter(VC1Context *v)
Definition: vc1_loopfilter.c:911
vc1_pred.h
MV_PMODE_INTFR_4MV
@ MV_PMODE_INTFR_4MV
Definition: vc1.h:97
MpegEncContext::last_pic
MPVWorkPicture last_pic
copy of the previous picture structure.
Definition: mpegvideo.h:120
VC1_TTBLK_VLC_BITS
#define VC1_TTBLK_VLC_BITS
Definition: vc1data.h:77
VC1Context::is_intra
uint8_t * is_intra
Definition: vc1.h:395
AV_CODEC_FLAG_GRAY
#define AV_CODEC_FLAG_GRAY
Only decode/encode grayscale.
Definition: avcodec.h:302
VC1Context::ttfrm
int ttfrm
Transform type info present at frame level.
Definition: vc1.h:264
VC1Context::codingset
int codingset
index of current table set from 11.8 to use for luma block decoding
Definition: vc1.h:267
dst
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
Definition: dsp.h:87
VC1Context::ttblk_base
int * ttblk_base
Definition: vc1.h:266
VC1Context::mb_off
int mb_off
Definition: vc1.h:368
ff_update_block_index
static void ff_update_block_index(MpegEncContext *s, int bits_per_raw_sample, int lowres, int chroma_x_shift)
Definition: mpegvideo.h:335
VC1Context::bfraction
int16_t bfraction
Relative position % anchors=> how to scale MVs.
Definition: vc1.h:279
update_block_index
static void update_block_index(MpegEncContext *s)
Definition: vc1_block.c:68
ff_vc1_p_loop_filter
void ff_vc1_p_loop_filter(VC1Context *v)
Definition: vc1_loopfilter.c:471
MSMP4_DC_VLC_BITS
#define MSMP4_DC_VLC_BITS
Definition: msmpeg4_vc1_data.h:38
VC1Context::zzi_8x8
uint8_t zzi_8x8[64]
Definition: vc1.h:352
BMV_TYPE_INTERPOLATED
@ BMV_TYPE_INTERPOLATED
Definition: vc1.h:107
ff_vc1_pred_b_mv
void ff_vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
Definition: vc1_pred.c:690
ff_vc1_ac_coeff_table
const VLCElem * ff_vc1_ac_coeff_table[8]
Definition: vc1data.c:124
scale_mv
#define scale_mv(n, dim)
VC1Context::fourmvswitch
int fourmvswitch
Definition: vc1.h:340
MB_TYPE_SKIP
#define MB_TYPE_SKIP
Definition: mpegutils.h:61
VC1Context::rangeredfrm
uint8_t rangeredfrm
Frame decoding info for S/M profiles only.
Definition: vc1.h:312
VC1Context::top_blk_idx
int top_blk_idx
Definition: vc1.h:393
diff
static av_always_inline int diff(const struct color_info *a, const struct color_info *b, const int trans_thresh)
Definition: vf_paletteuse.c:166
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
vc1data.h
CS_HIGH_MOT_INTRA
@ CS_HIGH_MOT_INTRA
Definition: vc1.h:127
ff_vc1_adv_interlaced_8x4_zz
const uint8_t ff_vc1_adv_interlaced_8x4_zz[32]
Definition: vc1data.c:218
MV_PMODE_INTFR_INTRA
@ MV_PMODE_INTFR_INTRA
Definition: vc1.h:98
VC1DSPContext::vc1_inv_trans_8x8
void(* vc1_inv_trans_8x8)(int16_t *b)
Definition: vc1dsp.h:39
unary.h
apply_loop_filter
static void apply_loop_filter(Vp3DecodeContext *s, int plane, int ystart, int yend)
Definition: vp3.c:1782
ff_vc1_pred_dc
static int ff_vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n, int a_avail, int c_avail, int16_t **dc_val_ptr, int *dir_ptr)
Get predicted DC value prediction dir: left=0, top=1.
Definition: vc1_block.c:408
VC1Context::k_x
int k_x
Number of bits for MVs (depends on MV range)
Definition: vc1.h:239
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:256
MV_PMODE_INTFR_4MV_FIELD
@ MV_PMODE_INTFR_4MV_FIELD
Definition: vc1.h:96
VC1Context::range_x
int range_x
Definition: vc1.h:241
decode012
static int BS_FUNC() decode012(BSCTX *bc)
Return decoded truncated unary code for the values 0, 1, 2.
Definition: bitstream_template.h:444
VC1_4MV_BLOCK_PATTERN_VLC_BITS
#define VC1_4MV_BLOCK_PATTERN_VLC_BITS
Definition: vc1data.h:73
BMV_TYPE_FORWARD
@ BMV_TYPE_FORWARD
Definition: vc1.h:106
av_always_inline
#define av_always_inline
Definition: attributes.h:63
value
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf default value
Definition: writing_filters.txt:86
VC1Context::s
MpegEncContext s
Definition: vc1.h:177
ff_vc1_ac_sizes
const int ff_vc1_ac_sizes[AC_MODES]
Definition: vc1_vlc_data.h:1059
VC1Context::zz_4x8
const uint8_t * zz_4x8
Zigzag scan table for TT_4x8 coding mode.
Definition: vc1.h:246
VC1Context::ttblk
int * ttblk
Transform type at the block level.
Definition: vc1.h:266
ff_vc1_pred_mv_intfr
void ff_vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y, int mvn, int r_x, int r_y, int dir)
Predict and set motion vector for interlaced frame picture MBs.
Definition: vc1_pred.c:469
ff_vc1_mbmode_intfrp
const uint8_t ff_vc1_mbmode_intfrp[2][15][4]
Definition: vc1data.c:53
VC1Context::is_intra_base
uint8_t * is_intra_base
Definition: vc1.h:395
vc1_last_delta_level_table
static const uint8_t vc1_last_delta_level_table[AC_MODES][44]
Definition: vc1acdata.h:246
avcodec.h
VC1Context::fourmvbp_vlc
const VLCElem * fourmvbp_vlc
Definition: vc1.h:347
VC1Context::second_field
int second_field
Definition: vc1.h:358
ret
ret
Definition: filter_design.txt:187
VC1Context::y_ac_table_index
int y_ac_table_index
Luma index from AC2FRM element.
Definition: vc1.h:260
pred
static const float pred[4]
Definition: siprdata.h:259
vc1_decode_intra_block
static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n, int coded, int mquant, int codingset)
Decode intra block in inter frames - more generic version than vc1_decode_i_block.
Definition: vc1_block.c:888
VC1Context::twomvbp
uint8_t twomvbp
Definition: vc1.h:348
VC1Context::overflg_is_raw
int overflg_is_raw
Definition: vc1.h:330
VC1Context::pquantizer
uint8_t pquantizer
Uniform (over sequence) quantizer in use.
Definition: vc1.h:290
CS_MID_RATE_INTRA
@ CS_MID_RATE_INTRA
Definition: vc1.h:131
VC1Context::codingset2
int codingset2
index of current table set from 11.8 to use for chroma block decoding
Definition: vc1.h:268
U
#define U(x)
Definition: vpx_arith.h:37
BMV_TYPE_BACKWARD
@ BMV_TYPE_BACKWARD
Definition: vc1.h:105
ff_vc1_subblkpat_vlc
const VLCElem * ff_vc1_subblkpat_vlc[3]
Definition: vc1data.c:116
VC1_2MV_BLOCK_PATTERN_VLC_BITS
#define VC1_2MV_BLOCK_PATTERN_VLC_BITS
Definition: vc1data.h:75
vc1_decode_p_mb
static int vc1_decode_p_mb(VC1Context *v)
Decode one P-frame MB.
Definition: vc1_block.c:1252
VC1Context::p_frame_skipped
int p_frame_skipped
Definition: vc1.h:388
vc1_last_delta_run_table
static const uint8_t vc1_last_delta_run_table[AC_MODES][10]
Definition: vc1acdata.h:339
ff_vc1_adv_interlaced_4x4_zz
const uint8_t ff_vc1_adv_interlaced_4x4_zz[16]
Definition: vc1data.c:236
AV_PICTURE_TYPE_B
@ AV_PICTURE_TYPE_B
Bi-dir predicted.
Definition: avutil.h:280
VC1Context::tff
uint8_t tff
Definition: vc1.h:321
VC1Context::x8_type
int x8_type
Definition: vc1.h:390
vc1_decode_skip_blocks
static void vc1_decode_skip_blocks(VC1Context *v)
Definition: vc1_block.c:2917
VC1Context::res_rtm_flag
int res_rtm_flag
reserved, set to 1
Definition: vc1.h:193
VC1Context::profile
int profile
Sequence header data for all Profiles TODO: choose between ints, uint8_ts and monobit flags.
Definition: vc1.h:220
vc1_decode_i_blocks
static void vc1_decode_i_blocks(VC1Context *v)
Decode blocks of I-frame.
Definition: vc1_block.c:2504
VC1Context::c_avail
int c_avail
Definition: vc1.h:270
MpegEncContext::er
ERContext er
Definition: mpegvideo.h:287
ff_vc1_mc_4mv_chroma
void ff_vc1_mc_4mv_chroma(VC1Context *v, int dir)
Do motion compensation for 4-MV macroblock - both chroma blocks.
Definition: vc1_mc.c:634
VC1Context::bi_type
int bi_type
Definition: vc1.h:389
vc1_last_decode_table
static const int vc1_last_decode_table[AC_MODES]
Definition: vc1acdata.h:30
VC1Context::range_y
int range_y
MV range.
Definition: vc1.h:241
MV_PMODE_INTFR_1MV
@ MV_PMODE_INTFR_1MV
Definition: vc1.h:93
TT_4X8
@ TT_4X8
Definition: vc1.h:121
VC1Context::fcm
enum FrameCodingMode fcm
Frame decoding info for Advanced profile.
Definition: vc1.h:318
AV_PICTURE_TYPE_P
@ AV_PICTURE_TYPE_P
Predicted.
Definition: avutil.h:279
VC1Context::mv_type_is_raw
int mv_type_is_raw
mv type mb plane is not coded
Definition: vc1.h:297
ff_vc1_ttmb_vlc
const VLCElem * ff_vc1_ttmb_vlc[3]
Definition: vc1data.c:109
msmpeg4_vc1_data.h
ER_MB_END
#define ER_MB_END
Definition: error_resilience.h:37
ff_msmp4_dc_vlc
const VLCElem * ff_msmp4_dc_vlc[2][2]
Definition: msmpeg4_vc1_data.c:36
vc1_delta_run_table
static const uint8_t vc1_delta_run_table[AC_MODES][57]
Definition: vc1acdata.h:295
scale
static void scale(int *out, const int *in, const int w, const int h, const int shift)
Definition: intra.c:273
VC1Context::bmvtype
int bmvtype
Definition: vc1.h:369
VC1Context::dmb_is_raw
int dmb_is_raw
direct mb plane is raw
Definition: vc1.h:298
VC1Context::cur_blk_idx
int cur_blk_idx
Definition: vc1.h:393
VC1Context::acpred_plane
uint8_t * acpred_plane
AC prediction flags bitplane.
Definition: vc1.h:327
MpegEncContext::mb_x
int mb_x
Definition: mpegvideo.h:191
TT_8X8
@ TT_8X8
Definition: vc1.h:115
VC1_2REF_MVDATA_VLC_BITS
#define VC1_2REF_MVDATA_VLC_BITS
Definition: vc1data.h:91
VC1Context::acpred_is_raw
int acpred_is_raw
Definition: vc1.h:328
TT_4X8_RIGHT
@ TT_4X8_RIGHT
Definition: vc1.h:119
ff_vc1_pred_mv
void ff_vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t *is_intra, int pred_flag, int dir)
Predict and set motion vector.
Definition: vc1_pred.c:212
ff_vc1_dqscale
const int32_t ff_vc1_dqscale[63]
Definition: vc1data.c:245
vc1_coded_block_pred
static int vc1_coded_block_pred(MPVContext *const s, int n, int diff)
Definition: vc1_block.c:479
block
The exact code depends on how similar the blocks are and how related they are to the block
Definition: filter_design.txt:207
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
VC1Context::blk_mv_type
uint8_t * blk_mv_type
0: frame MV, 1: field MV (interlaced frame)
Definition: vc1.h:353
MpegEncContext::ac_pred
int ac_pred
Definition: mpegvideo.h:71
vc1_decode_i_blocks_adv
static int vc1_decode_i_blocks_adv(VC1Context *v)
Decode blocks of I-frame for advanced profile.
Definition: vc1_block.c:2619
ff_vc1_b_intfi_loop_filter
void ff_vc1_b_intfi_loop_filter(VC1Context *v)
Definition: vc1_loopfilter.c:1173
VC1Context::dquantfrm
uint8_t dquantfrm
pquant parameters
Definition: vc1.h:249
vc1_delta_level_table
static const uint8_t vc1_delta_level_table[AC_MODES][31]
Definition: vc1acdata.h:203
CONDOVER_SELECT
@ CONDOVER_SELECT
Definition: vc1.h:142
VC1DSPContext::vc1_inv_trans_4x8
void(* vc1_inv_trans_4x8)(uint8_t *dest, ptrdiff_t stride, int16_t *block)
Definition: vc1dsp.h:41
MpegEncContext::low_delay
int low_delay
no reordering needed / has no B-frames
Definition: mpegvideo.h:231
vc1_decode_b_mb_intfr
static int vc1_decode_b_mb_intfr(VC1Context *v)
Decode one B-frame MB (in interlaced frame B picture)
Definition: vc1_block.c:2152
VC1Context::blocks_off
int blocks_off
Definition: vc1.h:368
TT_4X4
@ TT_4X4
Definition: vc1.h:122
MpegEncContext
MpegEncContext.
Definition: mpegvideo.h:67
VC1Context::gb
GetBitContext gb
Definition: vc1.h:178
skip
static void BS_FUNC() skip(BSCTX *bc, unsigned int n)
Skip n bits in the buffer.
Definition: bitstream_template.h:383
MB_TYPE_INTRA
#define MB_TYPE_INTRA
Definition: mpegutils.h:64