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