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