2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2006-2007 Konstantin Shishkov
4 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
6 * This file is part of FFmpeg.
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
25 * VC-1 and WMV3 decoder
31 #include "mpegvideo.h"
35 #include "vc1acdata.h"
36 #include "msmpeg4data.h"
38 #include "simple_idct.h"
40 #include "vdpau_internal.h"
45 #define MB_INTRA_VLC_BITS 9
48 static const uint16_t table_mb_intra[64][2];
51 static const uint16_t vlc_offs[] = {
52 0, 520, 552, 616, 1128, 1160, 1224, 1740, 1772, 1836, 1900, 2436,
53 2986, 3050, 3610, 4154, 4218, 4746, 5326, 5390, 5902, 6554, 7658, 8620,
54 9262, 10202, 10756, 11310, 12228, 15078
58 * Init VC-1 specific tables and VC1Context members
59 * @param v The VC1Context to initialize
62 static int vc1_init_common(VC1Context *v)
66 static VLC_TYPE vlc_table[15078][2];
68 v->hrd_rate = v->hrd_buffer = NULL;
73 INIT_VLC_STATIC(&ff_vc1_bfraction_vlc, VC1_BFRACTION_VLC_BITS, 23,
74 ff_vc1_bfraction_bits, 1, 1,
75 ff_vc1_bfraction_codes, 1, 1, 1 << VC1_BFRACTION_VLC_BITS);
76 INIT_VLC_STATIC(&ff_vc1_norm2_vlc, VC1_NORM2_VLC_BITS, 4,
77 ff_vc1_norm2_bits, 1, 1,
78 ff_vc1_norm2_codes, 1, 1, 1 << VC1_NORM2_VLC_BITS);
79 INIT_VLC_STATIC(&ff_vc1_norm6_vlc, VC1_NORM6_VLC_BITS, 64,
80 ff_vc1_norm6_bits, 1, 1,
81 ff_vc1_norm6_codes, 2, 2, 556);
82 INIT_VLC_STATIC(&ff_vc1_imode_vlc, VC1_IMODE_VLC_BITS, 7,
83 ff_vc1_imode_bits, 1, 1,
84 ff_vc1_imode_codes, 1, 1, 1 << VC1_IMODE_VLC_BITS);
87 ff_vc1_ttmb_vlc[i].table = &vlc_table[vlc_offs[i*3+0]];
88 ff_vc1_ttmb_vlc[i].table_allocated = vlc_offs[i*3+1] - vlc_offs[i*3+0];
89 init_vlc(&ff_vc1_ttmb_vlc[i], VC1_TTMB_VLC_BITS, 16,
90 ff_vc1_ttmb_bits[i], 1, 1,
91 ff_vc1_ttmb_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
92 ff_vc1_ttblk_vlc[i].table = &vlc_table[vlc_offs[i*3+1]];
93 ff_vc1_ttblk_vlc[i].table_allocated = vlc_offs[i*3+2] - vlc_offs[i*3+1];
94 init_vlc(&ff_vc1_ttblk_vlc[i], VC1_TTBLK_VLC_BITS, 8,
95 ff_vc1_ttblk_bits[i], 1, 1,
96 ff_vc1_ttblk_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
97 ff_vc1_subblkpat_vlc[i].table = &vlc_table[vlc_offs[i*3+2]];
98 ff_vc1_subblkpat_vlc[i].table_allocated = vlc_offs[i*3+3] - vlc_offs[i*3+2];
99 init_vlc(&ff_vc1_subblkpat_vlc[i], VC1_SUBBLKPAT_VLC_BITS, 15,
100 ff_vc1_subblkpat_bits[i], 1, 1,
101 ff_vc1_subblkpat_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
105 ff_vc1_4mv_block_pattern_vlc[i].table = &vlc_table[vlc_offs[i*3+9]];
106 ff_vc1_4mv_block_pattern_vlc[i].table_allocated = vlc_offs[i*3+10] - vlc_offs[i*3+9];
107 init_vlc(&ff_vc1_4mv_block_pattern_vlc[i], VC1_4MV_BLOCK_PATTERN_VLC_BITS, 16,
108 ff_vc1_4mv_block_pattern_bits[i], 1, 1,
109 ff_vc1_4mv_block_pattern_codes[i], 1, 1, INIT_VLC_USE_NEW_STATIC);
110 ff_vc1_cbpcy_p_vlc[i].table = &vlc_table[vlc_offs[i*3+10]];
111 ff_vc1_cbpcy_p_vlc[i].table_allocated = vlc_offs[i*3+11] - vlc_offs[i*3+10];
112 init_vlc(&ff_vc1_cbpcy_p_vlc[i], VC1_CBPCY_P_VLC_BITS, 64,
113 ff_vc1_cbpcy_p_bits[i], 1, 1,
114 ff_vc1_cbpcy_p_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
115 ff_vc1_mv_diff_vlc[i].table = &vlc_table[vlc_offs[i*3+11]];
116 ff_vc1_mv_diff_vlc[i].table_allocated = vlc_offs[i*3+12] - vlc_offs[i*3+11];
117 init_vlc(&ff_vc1_mv_diff_vlc[i], VC1_MV_DIFF_VLC_BITS, 73,
118 ff_vc1_mv_diff_bits[i], 1, 1,
119 ff_vc1_mv_diff_codes[i], 2, 2, INIT_VLC_USE_NEW_STATIC);
122 ff_vc1_ac_coeff_table[i].table = &vlc_table[vlc_offs[i+21]];
123 ff_vc1_ac_coeff_table[i].table_allocated = vlc_offs[i+22] - vlc_offs[i+21];
124 init_vlc(&ff_vc1_ac_coeff_table[i], AC_VLC_BITS, vc1_ac_sizes[i],
125 &vc1_ac_tables[i][0][1], 8, 4,
126 &vc1_ac_tables[i][0][0], 8, 4, INIT_VLC_USE_NEW_STATIC);
133 v->mvrange = 0; /* 7.1.1.18, p80 */
138 /***********************************************************************/
140 * @defgroup vc1bitplane VC-1 Bitplane decoding
158 /** @} */ //imode defines
161 /** @} */ //Bitplane group
163 static void vc1_loop_filter_iblk(MpegEncContext *s, int pq)
166 if(!s->first_slice_line)
167 s->dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
168 s->dsp.vc1_v_loop_filter16(s->dest[0] + 8*s->linesize, s->linesize, pq);
169 for(i = !s->mb_x*8; i < 16; i += 8)
170 s->dsp.vc1_h_loop_filter16(s->dest[0] + i, s->linesize, pq);
171 for(j = 0; j < 2; j++){
172 if(!s->first_slice_line)
173 s->dsp.vc1_v_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
175 s->dsp.vc1_h_loop_filter8(s->dest[j+1], s->uvlinesize, pq);
179 /** Put block onto picture
181 static void vc1_put_block(VC1Context *v, DCTELEM block[6][64])
185 DSPContext *dsp = &v->s.dsp;
189 for(k = 0; k < 6; k++)
190 for(j = 0; j < 8; j++)
191 for(i = 0; i < 8; i++)
192 block[k][i + j*8] = ((block[k][i + j*8] - 128) << 1) + 128;
195 ys = v->s.current_picture.linesize[0];
196 us = v->s.current_picture.linesize[1];
197 vs = v->s.current_picture.linesize[2];
200 dsp->put_pixels_clamped(block[0], Y, ys);
201 dsp->put_pixels_clamped(block[1], Y + 8, ys);
203 dsp->put_pixels_clamped(block[2], Y, ys);
204 dsp->put_pixels_clamped(block[3], Y + 8, ys);
206 if(!(v->s.flags & CODEC_FLAG_GRAY)) {
207 dsp->put_pixels_clamped(block[4], v->s.dest[1], us);
208 dsp->put_pixels_clamped(block[5], v->s.dest[2], vs);
212 /** Do motion compensation over 1 macroblock
213 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
215 static void vc1_mc_1mv(VC1Context *v, int dir)
217 MpegEncContext *s = &v->s;
218 DSPContext *dsp = &v->s.dsp;
219 uint8_t *srcY, *srcU, *srcV;
220 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
222 if(!v->s.last_picture.data[0])return;
224 mx = s->mv[dir][0][0];
225 my = s->mv[dir][0][1];
227 // store motion vectors for further use in B frames
228 if(s->pict_type == FF_P_TYPE) {
229 s->current_picture.motion_val[1][s->block_index[0]][0] = mx;
230 s->current_picture.motion_val[1][s->block_index[0]][1] = my;
232 uvmx = (mx + ((mx & 3) == 3)) >> 1;
233 uvmy = (my + ((my & 3) == 3)) >> 1;
235 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
236 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
239 srcY = s->last_picture.data[0];
240 srcU = s->last_picture.data[1];
241 srcV = s->last_picture.data[2];
243 srcY = s->next_picture.data[0];
244 srcU = s->next_picture.data[1];
245 srcV = s->next_picture.data[2];
248 src_x = s->mb_x * 16 + (mx >> 2);
249 src_y = s->mb_y * 16 + (my >> 2);
250 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
251 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
253 if(v->profile != PROFILE_ADVANCED){
254 src_x = av_clip( src_x, -16, s->mb_width * 16);
255 src_y = av_clip( src_y, -16, s->mb_height * 16);
256 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
257 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
259 src_x = av_clip( src_x, -17, s->avctx->coded_width);
260 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
261 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
262 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
265 srcY += src_y * s->linesize + src_x;
266 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
267 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
269 /* for grayscale we should not try to read from unknown area */
270 if(s->flags & CODEC_FLAG_GRAY) {
271 srcU = s->edge_emu_buffer + 18 * s->linesize;
272 srcV = s->edge_emu_buffer + 18 * s->linesize;
275 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
276 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
277 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
278 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
280 srcY -= s->mspel * (1 + s->linesize);
281 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
282 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
283 srcY = s->edge_emu_buffer;
284 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
285 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
286 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
287 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
290 /* if we deal with range reduction we need to scale source blocks */
296 for(j = 0; j < 17 + s->mspel*2; j++) {
297 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
300 src = srcU; src2 = srcV;
301 for(j = 0; j < 9; j++) {
302 for(i = 0; i < 9; i++) {
303 src[i] = ((src[i] - 128) >> 1) + 128;
304 src2[i] = ((src2[i] - 128) >> 1) + 128;
306 src += s->uvlinesize;
307 src2 += s->uvlinesize;
310 /* if we deal with intensity compensation we need to scale source blocks */
311 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
316 for(j = 0; j < 17 + s->mspel*2; j++) {
317 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = v->luty[src[i]];
320 src = srcU; src2 = srcV;
321 for(j = 0; j < 9; j++) {
322 for(i = 0; i < 9; i++) {
323 src[i] = v->lutuv[src[i]];
324 src2[i] = v->lutuv[src2[i]];
326 src += s->uvlinesize;
327 src2 += s->uvlinesize;
330 srcY += s->mspel * (1 + s->linesize);
334 dxy = ((my & 3) << 2) | (mx & 3);
335 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
336 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
337 srcY += s->linesize * 8;
338 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
339 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
340 } else { // hpel mc - always used for luma
341 dxy = (my & 2) | ((mx & 2) >> 1);
344 dsp->put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
346 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
349 if(s->flags & CODEC_FLAG_GRAY) return;
350 /* Chroma MC always uses qpel bilinear */
354 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
355 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
357 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
358 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
362 /** Do motion compensation for 4-MV macroblock - luminance block
364 static void vc1_mc_4mv_luma(VC1Context *v, int n)
366 MpegEncContext *s = &v->s;
367 DSPContext *dsp = &v->s.dsp;
369 int dxy, mx, my, src_x, src_y;
372 if(!v->s.last_picture.data[0])return;
375 srcY = s->last_picture.data[0];
377 off = s->linesize * 4 * (n&2) + (n&1) * 8;
379 src_x = s->mb_x * 16 + (n&1) * 8 + (mx >> 2);
380 src_y = s->mb_y * 16 + (n&2) * 4 + (my >> 2);
382 if(v->profile != PROFILE_ADVANCED){
383 src_x = av_clip( src_x, -16, s->mb_width * 16);
384 src_y = av_clip( src_y, -16, s->mb_height * 16);
386 src_x = av_clip( src_x, -17, s->avctx->coded_width);
387 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
390 srcY += src_y * s->linesize + src_x;
392 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
393 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 8 - s->mspel*2
394 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 8 - s->mspel*2){
395 srcY -= s->mspel * (1 + s->linesize);
396 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 9+s->mspel*2, 9+s->mspel*2,
397 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
398 srcY = s->edge_emu_buffer;
399 /* if we deal with range reduction we need to scale source blocks */
405 for(j = 0; j < 9 + s->mspel*2; j++) {
406 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
410 /* if we deal with intensity compensation we need to scale source blocks */
411 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
416 for(j = 0; j < 9 + s->mspel*2; j++) {
417 for(i = 0; i < 9 + s->mspel*2; i++) src[i] = v->luty[src[i]];
421 srcY += s->mspel * (1 + s->linesize);
425 dxy = ((my & 3) << 2) | (mx & 3);
426 dsp->put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, v->rnd);
427 } else { // hpel mc - always used for luma
428 dxy = (my & 2) | ((mx & 2) >> 1);
430 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
432 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
436 static inline int median4(int a, int b, int c, int d)
439 if(c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
440 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
442 if(c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
443 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
448 /** Do motion compensation for 4-MV macroblock - both chroma blocks
450 static void vc1_mc_4mv_chroma(VC1Context *v)
452 MpegEncContext *s = &v->s;
453 DSPContext *dsp = &v->s.dsp;
454 uint8_t *srcU, *srcV;
455 int uvmx, uvmy, uvsrc_x, uvsrc_y;
456 int i, idx, tx = 0, ty = 0;
457 int mvx[4], mvy[4], intra[4];
458 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
460 if(!v->s.last_picture.data[0])return;
461 if(s->flags & CODEC_FLAG_GRAY) return;
463 for(i = 0; i < 4; i++) {
464 mvx[i] = s->mv[0][i][0];
465 mvy[i] = s->mv[0][i][1];
466 intra[i] = v->mb_type[0][s->block_index[i]];
469 /* calculate chroma MV vector from four luma MVs */
470 idx = (intra[3] << 3) | (intra[2] << 2) | (intra[1] << 1) | intra[0];
471 if(!idx) { // all blocks are inter
472 tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
473 ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
474 } else if(count[idx] == 1) { // 3 inter blocks
477 tx = mid_pred(mvx[1], mvx[2], mvx[3]);
478 ty = mid_pred(mvy[1], mvy[2], mvy[3]);
481 tx = mid_pred(mvx[0], mvx[2], mvx[3]);
482 ty = mid_pred(mvy[0], mvy[2], mvy[3]);
485 tx = mid_pred(mvx[0], mvx[1], mvx[3]);
486 ty = mid_pred(mvy[0], mvy[1], mvy[3]);
489 tx = mid_pred(mvx[0], mvx[1], mvx[2]);
490 ty = mid_pred(mvy[0], mvy[1], mvy[2]);
493 } else if(count[idx] == 2) {
495 for(i=0; i<3;i++) if(!intra[i]) {t1 = i; break;}
496 for(i= t1+1; i<4; i++)if(!intra[i]) {t2 = i; break;}
497 tx = (mvx[t1] + mvx[t2]) / 2;
498 ty = (mvy[t1] + mvy[t2]) / 2;
500 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
501 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
502 return; //no need to do MC for inter blocks
505 s->current_picture.motion_val[1][s->block_index[0]][0] = tx;
506 s->current_picture.motion_val[1][s->block_index[0]][1] = ty;
507 uvmx = (tx + ((tx&3) == 3)) >> 1;
508 uvmy = (ty + ((ty&3) == 3)) >> 1;
510 uvmx = uvmx + ((uvmx<0)?(uvmx&1):-(uvmx&1));
511 uvmy = uvmy + ((uvmy<0)?(uvmy&1):-(uvmy&1));
514 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
515 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
517 if(v->profile != PROFILE_ADVANCED){
518 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
519 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
521 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
522 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
525 srcU = s->last_picture.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
526 srcV = s->last_picture.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
527 if(v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
528 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
529 || (unsigned)uvsrc_y > (s->v_edge_pos >> 1) - 9){
530 ff_emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize, 8+1, 8+1,
531 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
532 ff_emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize, 8+1, 8+1,
533 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
534 srcU = s->edge_emu_buffer;
535 srcV = s->edge_emu_buffer + 16;
537 /* if we deal with range reduction we need to scale source blocks */
542 src = srcU; src2 = srcV;
543 for(j = 0; j < 9; j++) {
544 for(i = 0; i < 9; i++) {
545 src[i] = ((src[i] - 128) >> 1) + 128;
546 src2[i] = ((src2[i] - 128) >> 1) + 128;
548 src += s->uvlinesize;
549 src2 += s->uvlinesize;
552 /* if we deal with intensity compensation we need to scale source blocks */
553 if(v->mv_mode == MV_PMODE_INTENSITY_COMP) {
557 src = srcU; src2 = srcV;
558 for(j = 0; j < 9; j++) {
559 for(i = 0; i < 9; i++) {
560 src[i] = v->lutuv[src[i]];
561 src2[i] = v->lutuv[src2[i]];
563 src += s->uvlinesize;
564 src2 += s->uvlinesize;
569 /* Chroma MC always uses qpel bilinear */
573 dsp->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
574 dsp->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
576 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
577 dsp->put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
581 /***********************************************************************/
583 * @defgroup vc1block VC-1 Block-level functions
584 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
590 * @brief Get macroblock-level quantizer scale
592 #define GET_MQUANT() \
596 if (v->dqprofile == DQPROFILE_ALL_MBS) \
600 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
604 mqdiff = get_bits(gb, 3); \
605 if (mqdiff != 7) mquant = v->pq + mqdiff; \
606 else mquant = get_bits(gb, 5); \
609 if(v->dqprofile == DQPROFILE_SINGLE_EDGE) \
610 edges = 1 << v->dqsbedge; \
611 else if(v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
612 edges = (3 << v->dqsbedge) % 15; \
613 else if(v->dqprofile == DQPROFILE_FOUR_EDGES) \
615 if((edges&1) && !s->mb_x) \
617 if((edges&2) && s->first_slice_line) \
619 if((edges&4) && s->mb_x == (s->mb_width - 1)) \
621 if((edges&8) && s->mb_y == (s->mb_height - 1)) \
626 * @def GET_MVDATA(_dmv_x, _dmv_y)
627 * @brief Get MV differentials
628 * @see MVDATA decoding from 8.3.5.2, p(1)20
629 * @param _dmv_x Horizontal differential for decoded MV
630 * @param _dmv_y Vertical differential for decoded MV
632 #define GET_MVDATA(_dmv_x, _dmv_y) \
633 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table,\
634 VC1_MV_DIFF_VLC_BITS, 2); \
640 else mb_has_coeffs = 0; \
642 if (!index) { _dmv_x = _dmv_y = 0; } \
643 else if (index == 35) \
645 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
646 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
648 else if (index == 36) \
657 if (!s->quarter_sample && index1 == 5) val = 1; \
659 if(size_table[index1] - val > 0) \
660 val = get_bits(gb, size_table[index1] - val); \
662 sign = 0 - (val&1); \
663 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
666 if (!s->quarter_sample && index1 == 5) val = 1; \
668 if(size_table[index1] - val > 0) \
669 val = get_bits(gb, size_table[index1] - val); \
671 sign = 0 - (val&1); \
672 _dmv_y = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
675 /** Predict and set motion vector
677 static inline void vc1_pred_mv(MpegEncContext *s, int n, int dmv_x, int dmv_y, int mv1, int r_x, int r_y, uint8_t* is_intra)
679 int xy, wrap, off = 0;
684 /* scale MV difference to be quad-pel */
685 dmv_x <<= 1 - s->quarter_sample;
686 dmv_y <<= 1 - s->quarter_sample;
689 xy = s->block_index[n];
692 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
693 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
694 s->current_picture.motion_val[1][xy][0] = 0;
695 s->current_picture.motion_val[1][xy][1] = 0;
696 if(mv1) { /* duplicate motion data for 1-MV block */
697 s->current_picture.motion_val[0][xy + 1][0] = 0;
698 s->current_picture.motion_val[0][xy + 1][1] = 0;
699 s->current_picture.motion_val[0][xy + wrap][0] = 0;
700 s->current_picture.motion_val[0][xy + wrap][1] = 0;
701 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
702 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
703 s->current_picture.motion_val[1][xy + 1][0] = 0;
704 s->current_picture.motion_val[1][xy + 1][1] = 0;
705 s->current_picture.motion_val[1][xy + wrap][0] = 0;
706 s->current_picture.motion_val[1][xy + wrap][1] = 0;
707 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
708 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
713 C = s->current_picture.motion_val[0][xy - 1];
714 A = s->current_picture.motion_val[0][xy - wrap];
716 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
718 //in 4-MV mode different blocks have different B predictor position
721 off = (s->mb_x > 0) ? -1 : 1;
724 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
733 B = s->current_picture.motion_val[0][xy - wrap + off];
735 if(!s->first_slice_line || (n==2 || n==3)) { // predictor A is not out of bounds
736 if(s->mb_width == 1) {
740 px = mid_pred(A[0], B[0], C[0]);
741 py = mid_pred(A[1], B[1], C[1]);
743 } else if(s->mb_x || (n==1 || n==3)) { // predictor C is not out of bounds
749 /* Pullback MV as specified in 8.3.5.3.4 */
752 qx = (s->mb_x << 6) + ((n==1 || n==3) ? 32 : 0);
753 qy = (s->mb_y << 6) + ((n==2 || n==3) ? 32 : 0);
754 X = (s->mb_width << 6) - 4;
755 Y = (s->mb_height << 6) - 4;
757 if(qx + px < -60) px = -60 - qx;
758 if(qy + py < -60) py = -60 - qy;
760 if(qx + px < -28) px = -28 - qx;
761 if(qy + py < -28) py = -28 - qy;
763 if(qx + px > X) px = X - qx;
764 if(qy + py > Y) py = Y - qy;
766 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
767 if((!s->first_slice_line || (n==2 || n==3)) && (s->mb_x || (n==1 || n==3))) {
768 if(is_intra[xy - wrap])
769 sum = FFABS(px) + FFABS(py);
771 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
773 if(get_bits1(&s->gb)) {
782 sum = FFABS(px) + FFABS(py);
784 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
786 if(get_bits1(&s->gb)) {
796 /* store MV using signed modulus of MV range defined in 4.11 */
797 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
798 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
799 if(mv1) { /* duplicate motion data for 1-MV block */
800 s->current_picture.motion_val[0][xy + 1][0] = s->current_picture.motion_val[0][xy][0];
801 s->current_picture.motion_val[0][xy + 1][1] = s->current_picture.motion_val[0][xy][1];
802 s->current_picture.motion_val[0][xy + wrap][0] = s->current_picture.motion_val[0][xy][0];
803 s->current_picture.motion_val[0][xy + wrap][1] = s->current_picture.motion_val[0][xy][1];
804 s->current_picture.motion_val[0][xy + wrap + 1][0] = s->current_picture.motion_val[0][xy][0];
805 s->current_picture.motion_val[0][xy + wrap + 1][1] = s->current_picture.motion_val[0][xy][1];
809 /** Motion compensation for direct or interpolated blocks in B-frames
811 static void vc1_interp_mc(VC1Context *v)
813 MpegEncContext *s = &v->s;
814 DSPContext *dsp = &v->s.dsp;
815 uint8_t *srcY, *srcU, *srcV;
816 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
818 if(!v->s.next_picture.data[0])return;
822 uvmx = (mx + ((mx & 3) == 3)) >> 1;
823 uvmy = (my + ((my & 3) == 3)) >> 1;
825 uvmx = uvmx + ((uvmx<0)?-(uvmx&1):(uvmx&1));
826 uvmy = uvmy + ((uvmy<0)?-(uvmy&1):(uvmy&1));
828 srcY = s->next_picture.data[0];
829 srcU = s->next_picture.data[1];
830 srcV = s->next_picture.data[2];
832 src_x = s->mb_x * 16 + (mx >> 2);
833 src_y = s->mb_y * 16 + (my >> 2);
834 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
835 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
837 if(v->profile != PROFILE_ADVANCED){
838 src_x = av_clip( src_x, -16, s->mb_width * 16);
839 src_y = av_clip( src_y, -16, s->mb_height * 16);
840 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
841 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
843 src_x = av_clip( src_x, -17, s->avctx->coded_width);
844 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
845 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
846 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
849 srcY += src_y * s->linesize + src_x;
850 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
851 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
853 /* for grayscale we should not try to read from unknown area */
854 if(s->flags & CODEC_FLAG_GRAY) {
855 srcU = s->edge_emu_buffer + 18 * s->linesize;
856 srcV = s->edge_emu_buffer + 18 * s->linesize;
860 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel*3
861 || (unsigned)(src_y - s->mspel) > s->v_edge_pos - (my&3) - 16 - s->mspel*3){
862 uint8_t *uvbuf= s->edge_emu_buffer + 19 * s->linesize;
864 srcY -= s->mspel * (1 + s->linesize);
865 ff_emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize, 17+s->mspel*2, 17+s->mspel*2,
866 src_x - s->mspel, src_y - s->mspel, s->h_edge_pos, s->v_edge_pos);
867 srcY = s->edge_emu_buffer;
868 ff_emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8+1, 8+1,
869 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
870 ff_emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8+1, 8+1,
871 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, s->v_edge_pos >> 1);
874 /* if we deal with range reduction we need to scale source blocks */
880 for(j = 0; j < 17 + s->mspel*2; j++) {
881 for(i = 0; i < 17 + s->mspel*2; i++) src[i] = ((src[i] - 128) >> 1) + 128;
884 src = srcU; src2 = srcV;
885 for(j = 0; j < 9; j++) {
886 for(i = 0; i < 9; i++) {
887 src[i] = ((src[i] - 128) >> 1) + 128;
888 src2[i] = ((src2[i] - 128) >> 1) + 128;
890 src += s->uvlinesize;
891 src2 += s->uvlinesize;
894 srcY += s->mspel * (1 + s->linesize);
898 dxy = ((my & 3) << 2) | (mx & 3);
899 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
900 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
901 srcY += s->linesize * 8;
902 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
903 dsp->avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
905 dxy = (my & 2) | ((mx & 2) >> 1);
908 dsp->avg_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
910 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
913 if(s->flags & CODEC_FLAG_GRAY) return;
914 /* Chroma MC always uses qpel blilinear */
918 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
919 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
921 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
922 dsp->avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
926 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
930 #if B_FRACTION_DEN==256
934 return 2 * ((value * n + 255) >> 9);
935 return (value * n + 128) >> 8;
940 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
941 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
945 /** Reconstruct motion vector for B-frame and do motion compensation
947 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mode)
950 v->mv_mode2 = v->mv_mode;
951 v->mv_mode = MV_PMODE_INTENSITY_COMP;
956 if(v->use_ic) v->mv_mode = v->mv_mode2;
959 if(mode == BMV_TYPE_INTERPOLATED) {
962 if(v->use_ic) v->mv_mode = v->mv_mode2;
966 if(v->use_ic && (mode == BMV_TYPE_BACKWARD)) v->mv_mode = v->mv_mode2;
967 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
968 if(v->use_ic) v->mv_mode = v->mv_mode2;
971 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2], int direct, int mvtype)
973 MpegEncContext *s = &v->s;
974 int xy, wrap, off = 0;
979 const uint8_t *is_intra = v->mb_type[0];
983 /* scale MV difference to be quad-pel */
984 dmv_x[0] <<= 1 - s->quarter_sample;
985 dmv_y[0] <<= 1 - s->quarter_sample;
986 dmv_x[1] <<= 1 - s->quarter_sample;
987 dmv_y[1] <<= 1 - s->quarter_sample;
990 xy = s->block_index[0];
993 s->current_picture.motion_val[0][xy][0] =
994 s->current_picture.motion_val[0][xy][1] =
995 s->current_picture.motion_val[1][xy][0] =
996 s->current_picture.motion_val[1][xy][1] = 0;
999 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
1000 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
1001 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
1002 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
1004 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
1005 s->mv[0][0][0] = av_clip(s->mv[0][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
1006 s->mv[0][0][1] = av_clip(s->mv[0][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
1007 s->mv[1][0][0] = av_clip(s->mv[1][0][0], -60 - (s->mb_x << 6), (s->mb_width << 6) - 4 - (s->mb_x << 6));
1008 s->mv[1][0][1] = av_clip(s->mv[1][0][1], -60 - (s->mb_y << 6), (s->mb_height << 6) - 4 - (s->mb_y << 6));
1010 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1011 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1012 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1013 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1017 if((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1018 C = s->current_picture.motion_val[0][xy - 2];
1019 A = s->current_picture.motion_val[0][xy - wrap*2];
1020 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1021 B = s->current_picture.motion_val[0][xy - wrap*2 + off];
1023 if(!s->mb_x) C[0] = C[1] = 0;
1024 if(!s->first_slice_line) { // predictor A is not out of bounds
1025 if(s->mb_width == 1) {
1029 px = mid_pred(A[0], B[0], C[0]);
1030 py = mid_pred(A[1], B[1], C[1]);
1032 } else if(s->mb_x) { // predictor C is not out of bounds
1038 /* Pullback MV as specified in 8.3.5.3.4 */
1041 if(v->profile < PROFILE_ADVANCED) {
1042 qx = (s->mb_x << 5);
1043 qy = (s->mb_y << 5);
1044 X = (s->mb_width << 5) - 4;
1045 Y = (s->mb_height << 5) - 4;
1046 if(qx + px < -28) px = -28 - qx;
1047 if(qy + py < -28) py = -28 - qy;
1048 if(qx + px > X) px = X - qx;
1049 if(qy + py > Y) py = Y - qy;
1051 qx = (s->mb_x << 6);
1052 qy = (s->mb_y << 6);
1053 X = (s->mb_width << 6) - 4;
1054 Y = (s->mb_height << 6) - 4;
1055 if(qx + px < -60) px = -60 - qx;
1056 if(qy + py < -60) py = -60 - qy;
1057 if(qx + px > X) px = X - qx;
1058 if(qy + py > Y) py = Y - qy;
1061 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1062 if(0 && !s->first_slice_line && s->mb_x) {
1063 if(is_intra[xy - wrap])
1064 sum = FFABS(px) + FFABS(py);
1066 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1068 if(get_bits1(&s->gb)) {
1076 if(is_intra[xy - 2])
1077 sum = FFABS(px) + FFABS(py);
1079 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1081 if(get_bits1(&s->gb)) {
1091 /* store MV using signed modulus of MV range defined in 4.11 */
1092 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
1093 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
1095 if((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
1096 C = s->current_picture.motion_val[1][xy - 2];
1097 A = s->current_picture.motion_val[1][xy - wrap*2];
1098 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1099 B = s->current_picture.motion_val[1][xy - wrap*2 + off];
1101 if(!s->mb_x) C[0] = C[1] = 0;
1102 if(!s->first_slice_line) { // predictor A is not out of bounds
1103 if(s->mb_width == 1) {
1107 px = mid_pred(A[0], B[0], C[0]);
1108 py = mid_pred(A[1], B[1], C[1]);
1110 } else if(s->mb_x) { // predictor C is not out of bounds
1116 /* Pullback MV as specified in 8.3.5.3.4 */
1119 if(v->profile < PROFILE_ADVANCED) {
1120 qx = (s->mb_x << 5);
1121 qy = (s->mb_y << 5);
1122 X = (s->mb_width << 5) - 4;
1123 Y = (s->mb_height << 5) - 4;
1124 if(qx + px < -28) px = -28 - qx;
1125 if(qy + py < -28) py = -28 - qy;
1126 if(qx + px > X) px = X - qx;
1127 if(qy + py > Y) py = Y - qy;
1129 qx = (s->mb_x << 6);
1130 qy = (s->mb_y << 6);
1131 X = (s->mb_width << 6) - 4;
1132 Y = (s->mb_height << 6) - 4;
1133 if(qx + px < -60) px = -60 - qx;
1134 if(qy + py < -60) py = -60 - qy;
1135 if(qx + px > X) px = X - qx;
1136 if(qy + py > Y) py = Y - qy;
1139 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
1140 if(0 && !s->first_slice_line && s->mb_x) {
1141 if(is_intra[xy - wrap])
1142 sum = FFABS(px) + FFABS(py);
1144 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
1146 if(get_bits1(&s->gb)) {
1154 if(is_intra[xy - 2])
1155 sum = FFABS(px) + FFABS(py);
1157 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
1159 if(get_bits1(&s->gb)) {
1169 /* store MV using signed modulus of MV range defined in 4.11 */
1171 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
1172 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
1174 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
1175 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
1176 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
1177 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
1180 /** Get predicted DC value for I-frames only
1181 * prediction dir: left=0, top=1
1182 * @param s MpegEncContext
1183 * @param overlap flag indicating that overlap filtering is used
1184 * @param pq integer part of picture quantizer
1185 * @param[in] n block index in the current MB
1186 * @param dc_val_ptr Pointer to DC predictor
1187 * @param dir_ptr Prediction direction for use in AC prediction
1189 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1190 int16_t **dc_val_ptr, int *dir_ptr)
1192 int a, b, c, wrap, pred, scale;
1194 static const uint16_t dcpred[32] = {
1195 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
1196 114, 102, 93, 85, 79, 73, 68, 64,
1197 60, 57, 54, 51, 49, 47, 45, 43,
1198 41, 39, 38, 37, 35, 34, 33
1201 /* find prediction - wmv3_dc_scale always used here in fact */
1202 if (n < 4) scale = s->y_dc_scale;
1203 else scale = s->c_dc_scale;
1205 wrap = s->block_wrap[n];
1206 dc_val= s->dc_val[0] + s->block_index[n];
1212 b = dc_val[ - 1 - wrap];
1213 a = dc_val[ - wrap];
1215 if (pq < 9 || !overlap)
1217 /* Set outer values */
1218 if (s->first_slice_line && (n!=2 && n!=3)) b=a=dcpred[scale];
1219 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=dcpred[scale];
1223 /* Set outer values */
1224 if (s->first_slice_line && (n!=2 && n!=3)) b=a=0;
1225 if (s->mb_x == 0 && (n!=1 && n!=3)) b=c=0;
1228 if (abs(a - b) <= abs(b - c)) {
1236 /* update predictor */
1237 *dc_val_ptr = &dc_val[0];
1242 /** Get predicted DC value
1243 * prediction dir: left=0, top=1
1244 * @param s MpegEncContext
1245 * @param overlap flag indicating that overlap filtering is used
1246 * @param pq integer part of picture quantizer
1247 * @param[in] n block index in the current MB
1248 * @param a_avail flag indicating top block availability
1249 * @param c_avail flag indicating left block availability
1250 * @param dc_val_ptr Pointer to DC predictor
1251 * @param dir_ptr Prediction direction for use in AC prediction
1253 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
1254 int a_avail, int c_avail,
1255 int16_t **dc_val_ptr, int *dir_ptr)
1257 int a, b, c, wrap, pred;
1259 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1262 wrap = s->block_wrap[n];
1263 dc_val= s->dc_val[0] + s->block_index[n];
1269 b = dc_val[ - 1 - wrap];
1270 a = dc_val[ - wrap];
1271 /* scale predictors if needed */
1272 q1 = s->current_picture.qscale_table[mb_pos];
1273 if(c_avail && (n!= 1 && n!=3)) {
1274 q2 = s->current_picture.qscale_table[mb_pos - 1];
1276 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1278 if(a_avail && (n!= 2 && n!=3)) {
1279 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1281 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1283 if(a_avail && c_avail && (n!=3)) {
1286 if(n != 2) off -= s->mb_stride;
1287 q2 = s->current_picture.qscale_table[off];
1289 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[s->y_dc_scale_table[q1] - 1] + 0x20000) >> 18;
1292 if(a_avail && c_avail) {
1293 if(abs(a - b) <= abs(b - c)) {
1300 } else if(a_avail) {
1303 } else if(c_avail) {
1311 /* update predictor */
1312 *dc_val_ptr = &dc_val[0];
1316 /** @} */ // Block group
1319 * @defgroup vc1_std_mb VC1 Macroblock-level functions in Simple/Main Profiles
1320 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1324 static inline int vc1_coded_block_pred(MpegEncContext * s, int n, uint8_t **coded_block_ptr)
1326 int xy, wrap, pred, a, b, c;
1328 xy = s->block_index[n];
1329 wrap = s->b8_stride;
1334 a = s->coded_block[xy - 1 ];
1335 b = s->coded_block[xy - 1 - wrap];
1336 c = s->coded_block[xy - wrap];
1345 *coded_block_ptr = &s->coded_block[xy];
1351 * Decode one AC coefficient
1352 * @param v The VC1 context
1353 * @param last Last coefficient
1354 * @param skip How much zero coefficients to skip
1355 * @param value Decoded AC coefficient value
1356 * @param codingset set of VLC to decode data
1359 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip, int *value, int codingset)
1361 GetBitContext *gb = &v->s.gb;
1362 int index, escape, run = 0, level = 0, lst = 0;
1364 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1365 if (index != vc1_ac_sizes[codingset] - 1) {
1366 run = vc1_index_decode_table[codingset][index][0];
1367 level = vc1_index_decode_table[codingset][index][1];
1368 lst = index >= vc1_last_decode_table[codingset];
1372 escape = decode210(gb);
1374 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
1375 run = vc1_index_decode_table[codingset][index][0];
1376 level = vc1_index_decode_table[codingset][index][1];
1377 lst = index >= vc1_last_decode_table[codingset];
1380 level += vc1_last_delta_level_table[codingset][run];
1382 level += vc1_delta_level_table[codingset][run];
1385 run += vc1_last_delta_run_table[codingset][level] + 1;
1387 run += vc1_delta_run_table[codingset][level] + 1;
1393 lst = get_bits1(gb);
1394 if(v->s.esc3_level_length == 0) {
1395 if(v->pq < 8 || v->dquantfrm) { // table 59
1396 v->s.esc3_level_length = get_bits(gb, 3);
1397 if(!v->s.esc3_level_length)
1398 v->s.esc3_level_length = get_bits(gb, 2) + 8;
1400 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
1402 v->s.esc3_run_length = 3 + get_bits(gb, 2);
1404 run = get_bits(gb, v->s.esc3_run_length);
1405 sign = get_bits1(gb);
1406 level = get_bits(gb, v->s.esc3_level_length);
1417 /** Decode intra block in intra frames - should be faster than decode_intra_block
1418 * @param v VC1Context
1419 * @param block block to decode
1420 * @param[in] n subblock index
1421 * @param coded are AC coeffs present or not
1422 * @param codingset set of VLC to decode data
1424 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset)
1426 GetBitContext *gb = &v->s.gb;
1427 MpegEncContext *s = &v->s;
1428 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1431 int16_t *ac_val, *ac_val2;
1434 /* Get DC differential */
1436 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1438 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1441 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1446 if (dcdiff == 119 /* ESC index value */)
1448 /* TODO: Optimize */
1449 if (v->pq == 1) dcdiff = get_bits(gb, 10);
1450 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
1451 else dcdiff = get_bits(gb, 8);
1456 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1457 else if (v->pq == 2)
1458 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1465 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
1468 /* Store the quantized DC coeff, used for prediction */
1470 block[0] = dcdiff * s->y_dc_scale;
1472 block[0] = dcdiff * s->c_dc_scale;
1483 int last = 0, skip, value;
1484 const int8_t *zz_table;
1488 scale = v->pq * 2 + v->halfpq;
1492 zz_table = wmv1_scantable[2];
1494 zz_table = wmv1_scantable[3];
1496 zz_table = wmv1_scantable[1];
1498 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1500 if(dc_pred_dir) //left
1503 ac_val -= 16 * s->block_wrap[n];
1506 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1510 block[zz_table[i++]] = value;
1513 /* apply AC prediction if needed */
1515 if(dc_pred_dir) { //left
1516 for(k = 1; k < 8; k++)
1517 block[k << 3] += ac_val[k];
1519 for(k = 1; k < 8; k++)
1520 block[k] += ac_val[k + 8];
1523 /* save AC coeffs for further prediction */
1524 for(k = 1; k < 8; k++) {
1525 ac_val2[k] = block[k << 3];
1526 ac_val2[k + 8] = block[k];
1529 /* scale AC coeffs */
1530 for(k = 1; k < 64; k++)
1534 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1537 if(s->ac_pred) i = 63;
1543 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1547 scale = v->pq * 2 + v->halfpq;
1548 memset(ac_val2, 0, 16 * 2);
1549 if(dc_pred_dir) {//left
1552 memcpy(ac_val2, ac_val, 8 * 2);
1554 ac_val -= 16 * s->block_wrap[n];
1556 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1559 /* apply AC prediction if needed */
1561 if(dc_pred_dir) { //left
1562 for(k = 1; k < 8; k++) {
1563 block[k << 3] = ac_val[k] * scale;
1564 if(!v->pquantizer && block[k << 3])
1565 block[k << 3] += (block[k << 3] < 0) ? -v->pq : v->pq;
1568 for(k = 1; k < 8; k++) {
1569 block[k] = ac_val[k + 8] * scale;
1570 if(!v->pquantizer && block[k])
1571 block[k] += (block[k] < 0) ? -v->pq : v->pq;
1577 s->block_last_index[n] = i;
1582 /** Decode intra block in intra frames - should be faster than decode_intra_block
1583 * @param v VC1Context
1584 * @param block block to decode
1585 * @param[in] n subblock number
1586 * @param coded are AC coeffs present or not
1587 * @param codingset set of VLC to decode data
1588 * @param mquant quantizer value for this macroblock
1590 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n, int coded, int codingset, int mquant)
1592 GetBitContext *gb = &v->s.gb;
1593 MpegEncContext *s = &v->s;
1594 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1597 int16_t *ac_val, *ac_val2;
1599 int a_avail = v->a_avail, c_avail = v->c_avail;
1600 int use_pred = s->ac_pred;
1603 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1605 /* Get DC differential */
1607 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1609 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1612 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1617 if (dcdiff == 119 /* ESC index value */)
1619 /* TODO: Optimize */
1620 if (mquant == 1) dcdiff = get_bits(gb, 10);
1621 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1622 else dcdiff = get_bits(gb, 8);
1627 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1628 else if (mquant == 2)
1629 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1636 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
1639 /* Store the quantized DC coeff, used for prediction */
1641 block[0] = dcdiff * s->y_dc_scale;
1643 block[0] = dcdiff * s->c_dc_scale;
1649 /* check if AC is needed at all */
1650 if(!a_avail && !c_avail) use_pred = 0;
1651 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1654 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
1656 if(dc_pred_dir) //left
1659 ac_val -= 16 * s->block_wrap[n];
1661 q1 = s->current_picture.qscale_table[mb_pos];
1662 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1663 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1664 if(dc_pred_dir && n==1) q2 = q1;
1665 if(!dc_pred_dir && n==2) q2 = q1;
1669 int last = 0, skip, value;
1670 const int8_t *zz_table;
1675 zz_table = wmv1_scantable[2];
1677 zz_table = wmv1_scantable[3];
1679 zz_table = wmv1_scantable[1];
1682 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1686 block[zz_table[i++]] = value;
1689 /* apply AC prediction if needed */
1691 /* scale predictors if needed*/
1693 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1694 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1696 if(dc_pred_dir) { //left
1697 for(k = 1; k < 8; k++)
1698 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1700 for(k = 1; k < 8; k++)
1701 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1704 if(dc_pred_dir) { //left
1705 for(k = 1; k < 8; k++)
1706 block[k << 3] += ac_val[k];
1708 for(k = 1; k < 8; k++)
1709 block[k] += ac_val[k + 8];
1713 /* save AC coeffs for further prediction */
1714 for(k = 1; k < 8; k++) {
1715 ac_val2[k] = block[k << 3];
1716 ac_val2[k + 8] = block[k];
1719 /* scale AC coeffs */
1720 for(k = 1; k < 64; k++)
1724 block[k] += (block[k] < 0) ? -mquant : mquant;
1727 if(use_pred) i = 63;
1728 } else { // no AC coeffs
1731 memset(ac_val2, 0, 16 * 2);
1732 if(dc_pred_dir) {//left
1734 memcpy(ac_val2, ac_val, 8 * 2);
1736 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1737 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1738 for(k = 1; k < 8; k++)
1739 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1744 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1746 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1747 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1748 for(k = 1; k < 8; k++)
1749 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1754 /* apply AC prediction if needed */
1756 if(dc_pred_dir) { //left
1757 for(k = 1; k < 8; k++) {
1758 block[k << 3] = ac_val2[k] * scale;
1759 if(!v->pquantizer && block[k << 3])
1760 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1763 for(k = 1; k < 8; k++) {
1764 block[k] = ac_val2[k + 8] * scale;
1765 if(!v->pquantizer && block[k])
1766 block[k] += (block[k] < 0) ? -mquant : mquant;
1772 s->block_last_index[n] = i;
1777 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
1778 * @param v VC1Context
1779 * @param block block to decode
1780 * @param[in] n subblock index
1781 * @param coded are AC coeffs present or not
1782 * @param mquant block quantizer
1783 * @param codingset set of VLC to decode data
1785 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n, int coded, int mquant, int codingset)
1787 GetBitContext *gb = &v->s.gb;
1788 MpegEncContext *s = &v->s;
1789 int dc_pred_dir = 0; /* Direction of the DC prediction used */
1792 int16_t *ac_val, *ac_val2;
1794 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
1795 int a_avail = v->a_avail, c_avail = v->c_avail;
1796 int use_pred = s->ac_pred;
1800 s->dsp.clear_block(block);
1802 /* XXX: Guard against dumb values of mquant */
1803 mquant = (mquant < 1) ? 0 : ( (mquant>31) ? 31 : mquant );
1805 /* Set DC scale - y and c use the same */
1806 s->y_dc_scale = s->y_dc_scale_table[mquant];
1807 s->c_dc_scale = s->c_dc_scale_table[mquant];
1809 /* Get DC differential */
1811 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1813 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
1816 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
1821 if (dcdiff == 119 /* ESC index value */)
1823 /* TODO: Optimize */
1824 if (mquant == 1) dcdiff = get_bits(gb, 10);
1825 else if (mquant == 2) dcdiff = get_bits(gb, 9);
1826 else dcdiff = get_bits(gb, 8);
1831 dcdiff = (dcdiff<<2) + get_bits(gb, 2) - 3;
1832 else if (mquant == 2)
1833 dcdiff = (dcdiff<<1) + get_bits1(gb) - 1;
1840 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
1843 /* Store the quantized DC coeff, used for prediction */
1846 block[0] = dcdiff * s->y_dc_scale;
1848 block[0] = dcdiff * s->c_dc_scale;
1854 /* check if AC is needed at all and adjust direction if needed */
1855 if(!a_avail) dc_pred_dir = 1;
1856 if(!c_avail) dc_pred_dir = 0;
1857 if(!a_avail && !c_avail) use_pred = 0;
1858 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
1861 scale = mquant * 2 + v->halfpq;
1863 if(dc_pred_dir) //left
1866 ac_val -= 16 * s->block_wrap[n];
1868 q1 = s->current_picture.qscale_table[mb_pos];
1869 if(dc_pred_dir && c_avail && mb_pos) q2 = s->current_picture.qscale_table[mb_pos - 1];
1870 if(!dc_pred_dir && a_avail && mb_pos >= s->mb_stride) q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
1871 if(dc_pred_dir && n==1) q2 = q1;
1872 if(!dc_pred_dir && n==2) q2 = q1;
1876 int last = 0, skip, value;
1877 const int8_t *zz_table;
1880 zz_table = wmv1_scantable[0];
1883 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
1887 block[zz_table[i++]] = value;
1890 /* apply AC prediction if needed */
1892 /* scale predictors if needed*/
1894 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1895 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1897 if(dc_pred_dir) { //left
1898 for(k = 1; k < 8; k++)
1899 block[k << 3] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1901 for(k = 1; k < 8; k++)
1902 block[k] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1905 if(dc_pred_dir) { //left
1906 for(k = 1; k < 8; k++)
1907 block[k << 3] += ac_val[k];
1909 for(k = 1; k < 8; k++)
1910 block[k] += ac_val[k + 8];
1914 /* save AC coeffs for further prediction */
1915 for(k = 1; k < 8; k++) {
1916 ac_val2[k] = block[k << 3];
1917 ac_val2[k + 8] = block[k];
1920 /* scale AC coeffs */
1921 for(k = 1; k < 64; k++)
1925 block[k] += (block[k] < 0) ? -mquant : mquant;
1928 if(use_pred) i = 63;
1929 } else { // no AC coeffs
1932 memset(ac_val2, 0, 16 * 2);
1933 if(dc_pred_dir) {//left
1935 memcpy(ac_val2, ac_val, 8 * 2);
1937 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1938 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1939 for(k = 1; k < 8; k++)
1940 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1945 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
1947 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
1948 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
1949 for(k = 1; k < 8; k++)
1950 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
1955 /* apply AC prediction if needed */
1957 if(dc_pred_dir) { //left
1958 for(k = 1; k < 8; k++) {
1959 block[k << 3] = ac_val2[k] * scale;
1960 if(!v->pquantizer && block[k << 3])
1961 block[k << 3] += (block[k << 3] < 0) ? -mquant : mquant;
1964 for(k = 1; k < 8; k++) {
1965 block[k] = ac_val2[k + 8] * scale;
1966 if(!v->pquantizer && block[k])
1967 block[k] += (block[k] < 0) ? -mquant : mquant;
1973 s->block_last_index[n] = i;
1980 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n, int mquant, int ttmb, int first_block,
1981 uint8_t *dst, int linesize, int skip_block, int apply_filter, int cbp_top, int cbp_left)
1983 MpegEncContext *s = &v->s;
1984 GetBitContext *gb = &s->gb;
1987 int scale, off, idx, last, skip, value;
1988 int ttblk = ttmb & 7;
1991 s->dsp.clear_block(block);
1994 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)];
1996 if(ttblk == TT_4X4) {
1997 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
1999 if((ttblk != TT_8X8 && ttblk != TT_4X4) && (v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))) {
2000 subblkpat = decode012(gb);
2001 if(subblkpat) subblkpat ^= 3; //swap decoded pattern bits
2002 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) ttblk = TT_8X4;
2003 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) ttblk = TT_4X8;
2005 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
2007 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
2008 if(ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
2009 subblkpat = 2 - (ttblk == TT_8X4_TOP);
2012 if(ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
2013 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
2022 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2026 idx = wmv1_scantable[0][i++];
2027 block[idx] = value * scale;
2029 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2033 s->dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
2035 s->dsp.vc1_inv_trans_8x8(block);
2036 s->dsp.add_pixels_clamped(block, dst, linesize);
2038 if(apply_filter && cbp_top & 0xC)
2039 s->dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
2040 if(apply_filter && cbp_left & 0xA)
2041 s->dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
2045 pat = ~subblkpat & 0xF;
2046 for(j = 0; j < 4; j++) {
2047 last = subblkpat & (1 << (3 - j));
2049 off = (j & 1) * 4 + (j & 2) * 16;
2051 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2055 idx = ff_vc1_simple_progressive_4x4_zz[i++];
2056 block[idx + off] = value * scale;
2058 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
2060 if(!(subblkpat & (1 << (3 - j))) && !skip_block){
2062 s->dsp.vc1_inv_trans_4x4_dc(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2064 s->dsp.vc1_inv_trans_4x4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, block + off);
2065 if(apply_filter && (j&2 ? pat & (1<<(j-2)) : (cbp_top & (1 << (j + 2)))))
2066 s->dsp.vc1_v_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2067 if(apply_filter && (j&1 ? pat & (1<<(j-1)) : (cbp_left & (1 << (j + 1)))))
2068 s->dsp.vc1_h_loop_filter4(dst + (j&1)*4 + (j&2)*2*linesize, linesize, v->pq);
2073 pat = ~((subblkpat & 2)*6 + (subblkpat & 1)*3) & 0xF;
2074 for(j = 0; j < 2; j++) {
2075 last = subblkpat & (1 << (1 - j));
2079 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2083 idx = v->zz_8x4[i++]+off;
2084 block[idx] = value * scale;
2086 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2088 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2090 s->dsp.vc1_inv_trans_8x4_dc(dst + j*4*linesize, linesize, block + off);
2092 s->dsp.vc1_inv_trans_8x4(dst + j*4*linesize, linesize, block + off);
2093 if(apply_filter && j ? pat & 0x3 : (cbp_top & 0xC))
2094 s->dsp.vc1_v_loop_filter8(dst + j*4*linesize, linesize, v->pq);
2095 if(apply_filter && cbp_left & (2 << j))
2096 s->dsp.vc1_h_loop_filter4(dst + j*4*linesize, linesize, v->pq);
2101 pat = ~(subblkpat*5) & 0xF;
2102 for(j = 0; j < 2; j++) {
2103 last = subblkpat & (1 << (1 - j));
2107 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
2111 idx = v->zz_4x8[i++]+off;
2112 block[idx] = value * scale;
2114 block[idx] += (block[idx] < 0) ? -mquant : mquant;
2116 if(!(subblkpat & (1 << (1 - j))) && !skip_block){
2118 s->dsp.vc1_inv_trans_4x8_dc(dst + j*4, linesize, block + off);
2120 s->dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
2121 if(apply_filter && cbp_top & (2 << j))
2122 s->dsp.vc1_v_loop_filter4(dst + j*4, linesize, v->pq);
2123 if(apply_filter && j ? pat & 0x5 : (cbp_left & 0xA))
2124 s->dsp.vc1_h_loop_filter8(dst + j*4, linesize, v->pq);
2132 /** @} */ // Macroblock group
2134 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
2135 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
2137 /** Decode one P-frame MB (in Simple/Main profile)
2139 static int vc1_decode_p_mb(VC1Context *v)
2141 MpegEncContext *s = &v->s;
2142 GetBitContext *gb = &s->gb;
2144 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2145 int cbp; /* cbp decoding stuff */
2146 int mqdiff, mquant; /* MB quantization */
2147 int ttmb = v->ttfrm; /* MB Transform type */
2149 int mb_has_coeffs = 1; /* last_flag */
2150 int dmv_x, dmv_y; /* Differential MV components */
2151 int index, index1; /* LUT indexes */
2152 int val, sign; /* temp values */
2153 int first_block = 1;
2155 int skipped, fourmv;
2156 int block_cbp = 0, pat;
2157 int apply_loop_filter;
2159 mquant = v->pq; /* Loosy initialization */
2161 if (v->mv_type_is_raw)
2162 fourmv = get_bits1(gb);
2164 fourmv = v->mv_type_mb_plane[mb_pos];
2166 skipped = get_bits1(gb);
2168 skipped = v->s.mbskip_table[mb_pos];
2170 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
2171 if (!fourmv) /* 1MV mode */
2175 GET_MVDATA(dmv_x, dmv_y);
2178 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2179 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2181 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
2182 vc1_pred_mv(s, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
2184 /* FIXME Set DC val for inter block ? */
2185 if (s->mb_intra && !mb_has_coeffs)
2188 s->ac_pred = get_bits1(gb);
2191 else if (mb_has_coeffs)
2193 if (s->mb_intra) s->ac_pred = get_bits1(gb);
2194 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2202 s->current_picture.qscale_table[mb_pos] = mquant;
2204 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2205 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
2206 VC1_TTMB_VLC_BITS, 2);
2207 if(!s->mb_intra) vc1_mc_1mv(v, 0);
2211 s->dc_val[0][s->block_index[i]] = 0;
2213 val = ((cbp >> (5 - i)) & 1);
2214 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2215 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2217 /* check if prediction blocks A and C are available */
2218 v->a_avail = v->c_avail = 0;
2219 if(i == 2 || i == 3 || !s->first_slice_line)
2220 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2221 if(i == 1 || i == 3 || s->mb_x)
2222 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2224 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2225 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2226 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2227 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2228 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2229 if(v->pq >= 9 && v->overlap) {
2231 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2233 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2235 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2236 int left_cbp, top_cbp;
2238 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2239 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2241 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2242 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2245 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2247 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2249 block_cbp |= 0xF << (i << 2);
2251 int left_cbp = 0, top_cbp = 0, filter = 0;
2252 if(apply_loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2255 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2256 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2258 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2259 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2262 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2264 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2266 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2267 block_cbp |= pat << (i << 2);
2268 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2276 for(i = 0; i < 6; i++) {
2277 v->mb_type[0][s->block_index[i]] = 0;
2278 s->dc_val[0][s->block_index[i]] = 0;
2280 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
2281 s->current_picture.qscale_table[mb_pos] = 0;
2282 vc1_pred_mv(s, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
2289 if (!skipped /* unskipped MB */)
2291 int intra_count = 0, coded_inter = 0;
2292 int is_intra[6], is_coded[6];
2294 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2297 val = ((cbp >> (5 - i)) & 1);
2298 s->dc_val[0][s->block_index[i]] = 0;
2305 GET_MVDATA(dmv_x, dmv_y);
2307 vc1_pred_mv(s, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
2308 if(!s->mb_intra) vc1_mc_4mv_luma(v, i);
2309 intra_count += s->mb_intra;
2310 is_intra[i] = s->mb_intra;
2311 is_coded[i] = mb_has_coeffs;
2314 is_intra[i] = (intra_count >= 3);
2317 if(i == 4) vc1_mc_4mv_chroma(v);
2318 v->mb_type[0][s->block_index[i]] = is_intra[i];
2319 if(!coded_inter) coded_inter = !is_intra[i] & is_coded[i];
2321 // if there are no coded blocks then don't do anything more
2322 if(!intra_count && !coded_inter) return 0;
2325 s->current_picture.qscale_table[mb_pos] = mquant;
2326 /* test if block is intra and has pred */
2331 if(((!s->first_slice_line || (i==2 || i==3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
2332 || ((s->mb_x || (i==1 || i==3)) && v->mb_type[0][s->block_index[i] - 1])) {
2337 if(intrapred)s->ac_pred = get_bits1(gb);
2338 else s->ac_pred = 0;
2340 if (!v->ttmbf && coded_inter)
2341 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2345 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2346 s->mb_intra = is_intra[i];
2348 /* check if prediction blocks A and C are available */
2349 v->a_avail = v->c_avail = 0;
2350 if(i == 2 || i == 3 || !s->first_slice_line)
2351 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2352 if(i == 1 || i == 3 || s->mb_x)
2353 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2355 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant, (i&4)?v->codingset2:v->codingset);
2356 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2357 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2358 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2359 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize);
2360 if(v->pq >= 9 && v->overlap) {
2362 s->dsp.vc1_h_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2364 s->dsp.vc1_v_overlap(s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2366 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2367 int left_cbp, top_cbp;
2369 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2370 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2372 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2373 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2376 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2378 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2380 block_cbp |= 0xF << (i << 2);
2381 } else if(is_coded[i]) {
2382 int left_cbp = 0, top_cbp = 0, filter = 0;
2383 if(v->s.loop_filter && s->mb_x && s->mb_x != (s->mb_width - 1) && s->mb_y && s->mb_y != (s->mb_height - 1)){
2386 left_cbp = v->cbp[s->mb_x - 1] >> (i * 4);
2387 top_cbp = v->cbp[s->mb_x - s->mb_stride] >> (i * 4);
2389 left_cbp = (i & 1) ? (cbp >> ((i-1)*4)) : (v->cbp[s->mb_x - 1] >> ((i+1)*4));
2390 top_cbp = (i & 2) ? (cbp >> ((i-2)*4)) : (v->cbp[s->mb_x - s->mb_stride] >> ((i+2)*4));
2393 s->dsp.vc1_v_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2395 s->dsp.vc1_h_loop_filter8(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize, v->pq);
2397 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), filter, left_cbp, top_cbp);
2398 block_cbp |= pat << (i << 2);
2399 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2408 s->current_picture.qscale_table[mb_pos] = 0;
2409 for (i=0; i<6; i++) {
2410 v->mb_type[0][s->block_index[i]] = 0;
2411 s->dc_val[0][s->block_index[i]] = 0;
2415 vc1_pred_mv(s, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0]);
2416 vc1_mc_4mv_luma(v, i);
2418 vc1_mc_4mv_chroma(v);
2419 s->current_picture.qscale_table[mb_pos] = 0;
2423 v->cbp[s->mb_x] = block_cbp;
2425 /* Should never happen */
2429 /** Decode one B-frame MB (in Main profile)
2431 static void vc1_decode_b_mb(VC1Context *v)
2433 MpegEncContext *s = &v->s;
2434 GetBitContext *gb = &s->gb;
2436 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2437 int cbp = 0; /* cbp decoding stuff */
2438 int mqdiff, mquant; /* MB quantization */
2439 int ttmb = v->ttfrm; /* MB Transform type */
2440 int mb_has_coeffs = 0; /* last_flag */
2441 int index, index1; /* LUT indexes */
2442 int val, sign; /* temp values */
2443 int first_block = 1;
2445 int skipped, direct;
2446 int dmv_x[2], dmv_y[2];
2447 int bmvtype = BMV_TYPE_BACKWARD;
2449 mquant = v->pq; /* Loosy initialization */
2453 direct = get_bits1(gb);
2455 direct = v->direct_mb_plane[mb_pos];
2457 skipped = get_bits1(gb);
2459 skipped = v->s.mbskip_table[mb_pos];
2461 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
2462 for(i = 0; i < 6; i++) {
2463 v->mb_type[0][s->block_index[i]] = 0;
2464 s->dc_val[0][s->block_index[i]] = 0;
2466 s->current_picture.qscale_table[mb_pos] = 0;
2470 GET_MVDATA(dmv_x[0], dmv_y[0]);
2471 dmv_x[1] = dmv_x[0];
2472 dmv_y[1] = dmv_y[0];
2474 if(skipped || !s->mb_intra) {
2475 bmvtype = decode012(gb);
2478 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
2481 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
2484 bmvtype = BMV_TYPE_INTERPOLATED;
2485 dmv_x[0] = dmv_y[0] = 0;
2489 for(i = 0; i < 6; i++)
2490 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2493 if(direct) bmvtype = BMV_TYPE_INTERPOLATED;
2494 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2495 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2499 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2502 s->current_picture.qscale_table[mb_pos] = mquant;
2504 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2505 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
2506 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2507 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2509 if(!mb_has_coeffs && !s->mb_intra) {
2510 /* no coded blocks - effectively skipped */
2511 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2512 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2515 if(s->mb_intra && !mb_has_coeffs) {
2517 s->current_picture.qscale_table[mb_pos] = mquant;
2518 s->ac_pred = get_bits1(gb);
2520 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2522 if(bmvtype == BMV_TYPE_INTERPOLATED) {
2523 GET_MVDATA(dmv_x[0], dmv_y[0]);
2524 if(!mb_has_coeffs) {
2525 /* interpolated skipped block */
2526 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2527 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2531 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
2533 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
2536 s->ac_pred = get_bits1(gb);
2537 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
2539 s->current_picture.qscale_table[mb_pos] = mquant;
2540 if(!v->ttmbf && !s->mb_intra && mb_has_coeffs)
2541 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
2547 s->dc_val[0][s->block_index[i]] = 0;
2549 val = ((cbp >> (5 - i)) & 1);
2550 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
2551 v->mb_type[0][s->block_index[i]] = s->mb_intra;
2553 /* check if prediction blocks A and C are available */
2554 v->a_avail = v->c_avail = 0;
2555 if(i == 2 || i == 3 || !s->first_slice_line)
2556 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
2557 if(i == 1 || i == 3 || s->mb_x)
2558 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
2560 vc1_decode_intra_block(v, s->block[i], i, val, mquant, (i&4)?v->codingset2:v->codingset);
2561 if((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
2562 s->dsp.vc1_inv_trans_8x8(s->block[i]);
2563 if(v->rangeredfrm) for(j = 0; j < 64; j++) s->block[i][j] <<= 1;
2564 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, s->linesize >> ((i & 4) >> 2));
2566 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block, s->dest[dst_idx] + off, (i&4)?s->uvlinesize:s->linesize, (i&4) && (s->flags & CODEC_FLAG_GRAY), 0, 0, 0);
2567 if(!v->ttmbf && ttmb < 8) ttmb = -1;
2573 /** Decode blocks of I-frame
2575 static void vc1_decode_i_blocks(VC1Context *v)
2578 MpegEncContext *s = &v->s;
2583 /* select codingmode used for VLC tables selection */
2584 switch(v->y_ac_table_index){
2586 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2589 v->codingset = CS_HIGH_MOT_INTRA;
2592 v->codingset = CS_MID_RATE_INTRA;
2596 switch(v->c_ac_table_index){
2598 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2601 v->codingset2 = CS_HIGH_MOT_INTER;
2604 v->codingset2 = CS_MID_RATE_INTER;
2608 /* Set DC scale - y and c use the same */
2609 s->y_dc_scale = s->y_dc_scale_table[v->pq];
2610 s->c_dc_scale = s->c_dc_scale_table[v->pq];
2613 s->mb_x = s->mb_y = 0;
2615 s->first_slice_line = 1;
2616 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2618 ff_init_block_index(s);
2619 for(; s->mb_x < s->mb_width; s->mb_x++) {
2620 ff_update_block_index(s);
2621 s->dsp.clear_blocks(s->block[0]);
2622 mb_pos = s->mb_x + s->mb_y * s->mb_width;
2623 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2624 s->current_picture.qscale_table[mb_pos] = v->pq;
2625 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2626 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2628 // do actual MB decoding and displaying
2629 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2630 v->s.ac_pred = get_bits1(&v->s.gb);
2632 for(k = 0; k < 6; k++) {
2633 val = ((cbp >> (5 - k)) & 1);
2636 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2640 cbp |= val << (5 - k);
2642 vc1_decode_i_block(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2);
2644 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2645 if(v->pq >= 9 && v->overlap) {
2646 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2650 vc1_put_block(v, s->block);
2651 if(v->pq >= 9 && v->overlap) {
2653 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2654 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2655 if(!(s->flags & CODEC_FLAG_GRAY)) {
2656 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2657 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2660 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2661 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2662 if(!s->first_slice_line) {
2663 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2664 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2665 if(!(s->flags & CODEC_FLAG_GRAY)) {
2666 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2667 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2670 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2671 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2673 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2675 if(get_bits_count(&s->gb) > v->bits) {
2676 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2677 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2681 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2682 s->first_slice_line = 0;
2684 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2687 /** Decode blocks of I-frame for advanced profile
2689 static void vc1_decode_i_blocks_adv(VC1Context *v)
2692 MpegEncContext *s = &v->s;
2699 GetBitContext *gb = &s->gb;
2701 /* select codingmode used for VLC tables selection */
2702 switch(v->y_ac_table_index){
2704 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2707 v->codingset = CS_HIGH_MOT_INTRA;
2710 v->codingset = CS_MID_RATE_INTRA;
2714 switch(v->c_ac_table_index){
2716 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2719 v->codingset2 = CS_HIGH_MOT_INTER;
2722 v->codingset2 = CS_MID_RATE_INTER;
2727 s->mb_x = s->mb_y = 0;
2729 s->first_slice_line = 1;
2730 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2732 ff_init_block_index(s);
2733 for(;s->mb_x < s->mb_width; s->mb_x++) {
2734 ff_update_block_index(s);
2735 s->dsp.clear_blocks(s->block[0]);
2736 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2737 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
2738 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
2739 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
2741 // do actual MB decoding and displaying
2742 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
2743 if(v->acpred_is_raw)
2744 v->s.ac_pred = get_bits1(&v->s.gb);
2746 v->s.ac_pred = v->acpred_plane[mb_pos];
2748 if(v->condover == CONDOVER_SELECT) {
2749 if(v->overflg_is_raw)
2750 overlap = get_bits1(&v->s.gb);
2752 overlap = v->over_flags_plane[mb_pos];
2754 overlap = (v->condover == CONDOVER_ALL);
2758 s->current_picture.qscale_table[mb_pos] = mquant;
2759 /* Set DC scale - y and c use the same */
2760 s->y_dc_scale = s->y_dc_scale_table[mquant];
2761 s->c_dc_scale = s->c_dc_scale_table[mquant];
2763 for(k = 0; k < 6; k++) {
2764 val = ((cbp >> (5 - k)) & 1);
2767 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
2771 cbp |= val << (5 - k);
2773 v->a_avail = !s->first_slice_line || (k==2 || k==3);
2774 v->c_avail = !!s->mb_x || (k==1 || k==3);
2776 vc1_decode_i_block_adv(v, s->block[k], k, val, (k<4)? v->codingset : v->codingset2, mquant);
2778 s->dsp.vc1_inv_trans_8x8(s->block[k]);
2779 for(j = 0; j < 64; j++) s->block[k][j] += 128;
2782 vc1_put_block(v, s->block);
2785 s->dsp.vc1_h_overlap(s->dest[0], s->linesize);
2786 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2787 if(!(s->flags & CODEC_FLAG_GRAY)) {
2788 s->dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
2789 s->dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
2792 s->dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
2793 s->dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2794 if(!s->first_slice_line) {
2795 s->dsp.vc1_v_overlap(s->dest[0], s->linesize);
2796 s->dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
2797 if(!(s->flags & CODEC_FLAG_GRAY)) {
2798 s->dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
2799 s->dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
2802 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
2803 s->dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
2805 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2807 if(get_bits_count(&s->gb) > v->bits) {
2808 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2809 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n", get_bits_count(&s->gb), v->bits);
2813 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2814 s->first_slice_line = 0;
2816 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2819 static void vc1_decode_p_blocks(VC1Context *v)
2821 MpegEncContext *s = &v->s;
2823 /* select codingmode used for VLC tables selection */
2824 switch(v->c_ac_table_index){
2826 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2829 v->codingset = CS_HIGH_MOT_INTRA;
2832 v->codingset = CS_MID_RATE_INTRA;
2836 switch(v->c_ac_table_index){
2838 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2841 v->codingset2 = CS_HIGH_MOT_INTER;
2844 v->codingset2 = CS_MID_RATE_INTER;
2848 s->first_slice_line = 1;
2849 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
2850 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2852 ff_init_block_index(s);
2853 for(; s->mb_x < s->mb_width; s->mb_x++) {
2854 ff_update_block_index(s);
2857 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2858 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2859 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2863 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0])*s->mb_stride);
2864 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2865 s->first_slice_line = 0;
2867 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2870 static void vc1_decode_b_blocks(VC1Context *v)
2872 MpegEncContext *s = &v->s;
2874 /* select codingmode used for VLC tables selection */
2875 switch(v->c_ac_table_index){
2877 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
2880 v->codingset = CS_HIGH_MOT_INTRA;
2883 v->codingset = CS_MID_RATE_INTRA;
2887 switch(v->c_ac_table_index){
2889 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
2892 v->codingset2 = CS_HIGH_MOT_INTER;
2895 v->codingset2 = CS_MID_RATE_INTER;
2899 s->first_slice_line = 1;
2900 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2902 ff_init_block_index(s);
2903 for(; s->mb_x < s->mb_width; s->mb_x++) {
2904 ff_update_block_index(s);
2907 if(get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
2908 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, (AC_END|DC_END|MV_END));
2909 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n", get_bits_count(&s->gb), v->bits,s->mb_x,s->mb_y);
2912 if(v->s.loop_filter) vc1_loop_filter_iblk(s, v->pq);
2914 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2915 s->first_slice_line = 0;
2917 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2920 static void vc1_decode_skip_blocks(VC1Context *v)
2922 MpegEncContext *s = &v->s;
2924 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, (AC_END|DC_END|MV_END));
2925 s->first_slice_line = 1;
2926 for(s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
2928 ff_init_block_index(s);
2929 ff_update_block_index(s);
2930 memcpy(s->dest[0], s->last_picture.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
2931 memcpy(s->dest[1], s->last_picture.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2932 memcpy(s->dest[2], s->last_picture.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
2933 ff_draw_horiz_band(s, s->mb_y * 16, 16);
2934 s->first_slice_line = 0;
2936 s->pict_type = FF_P_TYPE;
2939 static void vc1_decode_blocks(VC1Context *v)
2942 v->s.esc3_level_length = 0;
2944 ff_intrax8_decode_picture(&v->x8, 2*v->pq+v->halfpq, v->pq*(!v->pquantizer) );
2947 switch(v->s.pict_type) {
2949 if(v->profile == PROFILE_ADVANCED)
2950 vc1_decode_i_blocks_adv(v);
2952 vc1_decode_i_blocks(v);
2955 if(v->p_frame_skipped)
2956 vc1_decode_skip_blocks(v);
2958 vc1_decode_p_blocks(v);
2962 if(v->profile == PROFILE_ADVANCED)
2963 vc1_decode_i_blocks_adv(v);
2965 vc1_decode_i_blocks(v);
2967 vc1_decode_b_blocks(v);
2973 /** Initialize a VC1/WMV3 decoder
2974 * @todo TODO: Handle VC-1 IDUs (Transport level?)
2975 * @todo TODO: Decypher remaining bits in extra_data
2977 static av_cold int vc1_decode_init(AVCodecContext *avctx)
2979 VC1Context *v = avctx->priv_data;
2980 MpegEncContext *s = &v->s;
2983 if (!avctx->extradata_size || !avctx->extradata) return -1;
2984 if (!(avctx->flags & CODEC_FLAG_GRAY))
2985 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
2987 avctx->pix_fmt = PIX_FMT_GRAY8;
2988 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
2990 avctx->flags |= CODEC_FLAG_EMU_EDGE;
2991 v->s.flags |= CODEC_FLAG_EMU_EDGE;
2993 if(avctx->idct_algo==FF_IDCT_AUTO){
2994 avctx->idct_algo=FF_IDCT_WMV2;
2997 if(ff_msmpeg4_decode_init(avctx) < 0)
2999 if (vc1_init_common(v) < 0) return -1;
3001 avctx->coded_width = avctx->width;
3002 avctx->coded_height = avctx->height;
3003 if (avctx->codec_id == CODEC_ID_WMV3)
3007 // looks like WMV3 has a sequence header stored in the extradata
3008 // advanced sequence header may be before the first frame
3009 // the last byte of the extradata is a version number, 1 for the
3010 // samples we can decode
3012 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
3014 if (vc1_decode_sequence_header(avctx, v, &gb) < 0)
3017 count = avctx->extradata_size*8 - get_bits_count(&gb);
3020 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
3021 count, get_bits(&gb, count));
3025 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
3027 } else { // VC1/WVC1
3028 const uint8_t *start = avctx->extradata;
3029 uint8_t *end = avctx->extradata + avctx->extradata_size;
3030 const uint8_t *next;
3031 int size, buf2_size;
3032 uint8_t *buf2 = NULL;
3033 int seq_initialized = 0, ep_initialized = 0;
3035 if(avctx->extradata_size < 16) {
3036 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
3040 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
3041 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
3043 for(; next < end; start = next){
3044 next = find_next_marker(start + 4, end);
3045 size = next - start - 4;
3046 if(size <= 0) continue;
3047 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
3048 init_get_bits(&gb, buf2, buf2_size * 8);
3049 switch(AV_RB32(start)){
3050 case VC1_CODE_SEQHDR:
3051 if(vc1_decode_sequence_header(avctx, v, &gb) < 0){
3055 seq_initialized = 1;
3057 case VC1_CODE_ENTRYPOINT:
3058 if(vc1_decode_entry_point(avctx, v, &gb) < 0){
3067 if(!seq_initialized || !ep_initialized){
3068 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
3072 avctx->has_b_frames= !!(avctx->max_b_frames);
3073 s->low_delay = !avctx->has_b_frames;
3075 s->mb_width = (avctx->coded_width+15)>>4;
3076 s->mb_height = (avctx->coded_height+15)>>4;
3078 /* Allocate mb bitplanes */
3079 v->mv_type_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3080 v->direct_mb_plane = av_malloc(s->mb_stride * s->mb_height);
3081 v->acpred_plane = av_malloc(s->mb_stride * s->mb_height);
3082 v->over_flags_plane = av_malloc(s->mb_stride * s->mb_height);
3084 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
3085 v->cbp = v->cbp_base + s->mb_stride;
3087 /* allocate block type info in that way so it could be used with s->block_index[] */
3088 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
3089 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
3090 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
3091 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
3093 /* Init coded blocks info */
3094 if (v->profile == PROFILE_ADVANCED)
3096 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
3098 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
3102 ff_intrax8_common_init(&v->x8,s);
3107 /** Decode a VC1/WMV3 frame
3108 * @todo TODO: Handle VC-1 IDUs (Transport level?)
3110 static int vc1_decode_frame(AVCodecContext *avctx,
3111 void *data, int *data_size,
3114 const uint8_t *buf = avpkt->data;
3115 int buf_size = avpkt->size;
3116 VC1Context *v = avctx->priv_data;
3117 MpegEncContext *s = &v->s;
3118 AVFrame *pict = data;
3119 uint8_t *buf2 = NULL;
3120 const uint8_t *buf_start = buf;
3122 /* no supplementary picture */
3123 if (buf_size == 0) {
3124 /* special case for last picture */
3125 if (s->low_delay==0 && s->next_picture_ptr) {
3126 *pict= *(AVFrame*)s->next_picture_ptr;
3127 s->next_picture_ptr= NULL;
3129 *data_size = sizeof(AVFrame);
3135 /* We need to set current_picture_ptr before reading the header,
3136 * otherwise we cannot store anything in there. */
3137 if(s->current_picture_ptr==NULL || s->current_picture_ptr->data[0]){
3138 int i= ff_find_unused_picture(s, 0);
3139 s->current_picture_ptr= &s->picture[i];
3142 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU){
3143 if (v->profile < PROFILE_ADVANCED)
3144 avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
3146 avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
3149 //for advanced profile we may need to parse and unescape data
3150 if (avctx->codec_id == CODEC_ID_VC1) {
3152 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
3154 if(IS_MARKER(AV_RB32(buf))){ /* frame starts with marker and needs to be parsed */
3155 const uint8_t *start, *end, *next;
3159 for(start = buf, end = buf + buf_size; next < end; start = next){
3160 next = find_next_marker(start + 4, end);
3161 size = next - start - 4;
3162 if(size <= 0) continue;
3163 switch(AV_RB32(start)){
3164 case VC1_CODE_FRAME:
3165 if (avctx->hwaccel ||
3166 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3168 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3170 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
3171 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
3172 init_get_bits(&s->gb, buf2, buf_size2*8);
3173 vc1_decode_entry_point(avctx, v, &s->gb);
3175 case VC1_CODE_SLICE:
3176 av_log(avctx, AV_LOG_ERROR, "Sliced decoding is not implemented (yet)\n");
3181 }else if(v->interlace && ((buf[0] & 0xC0) == 0xC0)){ /* WVC1 interlaced stores both fields divided by marker */
3182 const uint8_t *divider;
3184 divider = find_next_marker(buf, buf + buf_size);
3185 if((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD){
3186 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
3191 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
3193 if(!v->warn_interlaced++)
3194 av_log(v->s.avctx, AV_LOG_ERROR, "Interlaced WVC1 support is not implemented\n");
3195 av_free(buf2);return -1;
3197 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
3199 init_get_bits(&s->gb, buf2, buf_size2*8);
3201 init_get_bits(&s->gb, buf, buf_size*8);
3202 // do parse frame header
3203 if(v->profile < PROFILE_ADVANCED) {
3204 if(vc1_parse_frame_header(v, &s->gb) == -1) {
3209 if(vc1_parse_frame_header_adv(v, &s->gb) == -1) {
3215 if(s->pict_type != FF_I_TYPE && !v->res_rtm_flag){
3221 s->current_picture.pict_type= s->pict_type;
3222 s->current_picture.key_frame= s->pict_type == FF_I_TYPE;
3224 /* skip B-frames if we don't have reference frames */
3225 if(s->last_picture_ptr==NULL && (s->pict_type==FF_B_TYPE || s->dropable)){
3227 return -1;//buf_size;
3229 /* skip b frames if we are in a hurry */
3230 if(avctx->hurry_up && s->pict_type==FF_B_TYPE) return -1;//buf_size;
3231 if( (avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type==FF_B_TYPE)
3232 || (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type!=FF_I_TYPE)
3233 || avctx->skip_frame >= AVDISCARD_ALL) {
3237 /* skip everything if we are in a hurry>=5 */
3238 if(avctx->hurry_up>=5) {
3240 return -1;//buf_size;
3243 if(s->next_p_frame_damaged){
3244 if(s->pict_type==FF_B_TYPE)
3247 s->next_p_frame_damaged=0;
3250 if(MPV_frame_start(s, avctx) < 0) {
3255 s->me.qpel_put= s->dsp.put_qpel_pixels_tab;
3256 s->me.qpel_avg= s->dsp.avg_qpel_pixels_tab;
3258 if ((CONFIG_VC1_VDPAU_DECODER)
3259 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
3260 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
3261 else if (avctx->hwaccel) {
3262 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
3264 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
3266 if (avctx->hwaccel->end_frame(avctx) < 0)
3269 ff_er_frame_start(s);
3271 v->bits = buf_size * 8;
3272 vc1_decode_blocks(v);
3273 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), buf_size*8);
3274 // if(get_bits_count(&s->gb) > buf_size * 8)
3281 assert(s->current_picture.pict_type == s->current_picture_ptr->pict_type);
3282 assert(s->current_picture.pict_type == s->pict_type);
3283 if (s->pict_type == FF_B_TYPE || s->low_delay) {
3284 *pict= *(AVFrame*)s->current_picture_ptr;
3285 } else if (s->last_picture_ptr != NULL) {
3286 *pict= *(AVFrame*)s->last_picture_ptr;
3289 if(s->last_picture_ptr || s->low_delay){
3290 *data_size = sizeof(AVFrame);
3291 ff_print_debug_info(s, pict);
3299 /** Close a VC1/WMV3 decoder
3300 * @warning Initial try at using MpegEncContext stuff
3302 static av_cold int vc1_decode_end(AVCodecContext *avctx)
3304 VC1Context *v = avctx->priv_data;
3306 av_freep(&v->hrd_rate);
3307 av_freep(&v->hrd_buffer);
3308 MPV_common_end(&v->s);
3309 av_freep(&v->mv_type_mb_plane);
3310 av_freep(&v->direct_mb_plane);
3311 av_freep(&v->acpred_plane);
3312 av_freep(&v->over_flags_plane);
3313 av_freep(&v->mb_type_base);
3314 av_freep(&v->cbp_base);
3315 ff_intrax8_common_end(&v->x8);
3320 AVCodec vc1_decoder = {
3329 CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3331 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
3332 .pix_fmts = ff_hwaccel_pixfmt_list_420
3335 #if CONFIG_WMV3_DECODER
3336 AVCodec wmv3_decoder = {
3345 CODEC_CAP_DR1 | CODEC_CAP_DELAY,
3347 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
3348 .pix_fmts = ff_hwaccel_pixfmt_list_420
3352 #if CONFIG_WMV3_VDPAU_DECODER
3353 AVCodec wmv3_vdpau_decoder = {
3362 CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3364 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
3365 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE}
3369 #if CONFIG_VC1_VDPAU_DECODER
3370 AVCodec vc1_vdpau_decoder = {
3379 CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
3381 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
3382 .pix_fmts = (const enum PixelFormat[]){PIX_FMT_VDPAU_VC1, PIX_FMT_NONE}