00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00029 #include "internal.h"
00030 #include "dsputil.h"
00031 #include "avcodec.h"
00032 #include "mpegvideo.h"
00033 #include "h263.h"
00034 #include "vc1.h"
00035 #include "vc1data.h"
00036 #include "vc1acdata.h"
00037 #include "msmpeg4data.h"
00038 #include "unary.h"
00039 #include "mathops.h"
00040 #include "vdpau_internal.h"
00041 #include "libavutil/avassert.h"
00042
00043 #undef NDEBUG
00044 #include <assert.h>
00045
00046 #define MB_INTRA_VLC_BITS 9
00047 #define DC_VLC_BITS 9
00048
00049
00050
00051 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
00052 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
00053
00054
00065 enum Imode {
00066 IMODE_RAW,
00067 IMODE_NORM2,
00068 IMODE_DIFF2,
00069 IMODE_NORM6,
00070 IMODE_DIFF6,
00071 IMODE_ROWSKIP,
00072 IMODE_COLSKIP
00073 };
00075
00076
00078
00079 static void vc1_put_signed_blocks_clamped(VC1Context *v)
00080 {
00081 MpegEncContext *s = &v->s;
00082 int topleft_mb_pos, top_mb_pos;
00083 int stride_y, fieldtx;
00084 int v_dist;
00085
00086
00087
00088
00089
00090
00091
00092
00093 if (!s->first_slice_line) {
00094 if (s->mb_x) {
00095 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
00096 fieldtx = v->fieldtx_plane[topleft_mb_pos];
00097 stride_y = s->linesize << fieldtx;
00098 v_dist = (16 - fieldtx) >> (fieldtx == 0);
00099 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
00100 s->dest[0] - 16 * s->linesize - 16,
00101 stride_y);
00102 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
00103 s->dest[0] - 16 * s->linesize - 8,
00104 stride_y);
00105 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
00106 s->dest[0] - v_dist * s->linesize - 16,
00107 stride_y);
00108 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
00109 s->dest[0] - v_dist * s->linesize - 8,
00110 stride_y);
00111 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
00112 s->dest[1] - 8 * s->uvlinesize - 8,
00113 s->uvlinesize);
00114 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
00115 s->dest[2] - 8 * s->uvlinesize - 8,
00116 s->uvlinesize);
00117 }
00118 if (s->mb_x == s->mb_width - 1) {
00119 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
00120 fieldtx = v->fieldtx_plane[top_mb_pos];
00121 stride_y = s->linesize << fieldtx;
00122 v_dist = fieldtx ? 15 : 8;
00123 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
00124 s->dest[0] - 16 * s->linesize,
00125 stride_y);
00126 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
00127 s->dest[0] - 16 * s->linesize + 8,
00128 stride_y);
00129 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
00130 s->dest[0] - v_dist * s->linesize,
00131 stride_y);
00132 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
00133 s->dest[0] - v_dist * s->linesize + 8,
00134 stride_y);
00135 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
00136 s->dest[1] - 8 * s->uvlinesize,
00137 s->uvlinesize);
00138 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
00139 s->dest[2] - 8 * s->uvlinesize,
00140 s->uvlinesize);
00141 }
00142 }
00143
00144 #define inc_blk_idx(idx) do { \
00145 idx++; \
00146 if (idx >= v->n_allocated_blks) \
00147 idx = 0; \
00148 } while (0)
00149
00150 inc_blk_idx(v->topleft_blk_idx);
00151 inc_blk_idx(v->top_blk_idx);
00152 inc_blk_idx(v->left_blk_idx);
00153 inc_blk_idx(v->cur_blk_idx);
00154 }
00155
00156 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
00157 {
00158 MpegEncContext *s = &v->s;
00159 int j;
00160 if (!s->first_slice_line) {
00161 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
00162 if (s->mb_x)
00163 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
00164 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
00165 for (j = 0; j < 2; j++) {
00166 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
00167 if (s->mb_x)
00168 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
00169 }
00170 }
00171 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
00172
00173 if (s->mb_y == s->end_mb_y - 1) {
00174 if (s->mb_x) {
00175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
00176 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
00177 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
00178 }
00179 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
00180 }
00181 }
00182
00183 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
00184 {
00185 MpegEncContext *s = &v->s;
00186 int j;
00187
00188
00189
00190 if (!s->first_slice_line) {
00191 if (s->mb_x) {
00192 if (s->mb_y >= s->start_mb_y + 2) {
00193 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
00194
00195 if (s->mb_x >= 2)
00196 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
00197 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
00198 for (j = 0; j < 2; j++) {
00199 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
00200 if (s->mb_x >= 2) {
00201 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
00202 }
00203 }
00204 }
00205 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
00206 }
00207
00208 if (s->mb_x == s->mb_width - 1) {
00209 if (s->mb_y >= s->start_mb_y + 2) {
00210 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
00211
00212 if (s->mb_x)
00213 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
00214 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
00215 for (j = 0; j < 2; j++) {
00216 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
00217 if (s->mb_x >= 2) {
00218 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
00219 }
00220 }
00221 }
00222 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
00223 }
00224
00225 if (s->mb_y == s->end_mb_y) {
00226 if (s->mb_x) {
00227 if (s->mb_x >= 2)
00228 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
00229 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
00230 if (s->mb_x >= 2) {
00231 for (j = 0; j < 2; j++) {
00232 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
00233 }
00234 }
00235 }
00236
00237 if (s->mb_x == s->mb_width - 1) {
00238 if (s->mb_x)
00239 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
00240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
00241 if (s->mb_x) {
00242 for (j = 0; j < 2; j++) {
00243 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
00244 }
00245 }
00246 }
00247 }
00248 }
00249 }
00250
00251 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
00252 {
00253 MpegEncContext *s = &v->s;
00254 int mb_pos;
00255
00256 if (v->condover == CONDOVER_NONE)
00257 return;
00258
00259 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
00260
00261
00262
00263
00264
00265
00266
00267
00268 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
00269 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
00270 v->over_flags_plane[mb_pos - 1])) {
00271 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
00272 v->block[v->cur_blk_idx][0]);
00273 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
00274 v->block[v->cur_blk_idx][2]);
00275 if (!(s->flags & CODEC_FLAG_GRAY)) {
00276 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
00277 v->block[v->cur_blk_idx][4]);
00278 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
00279 v->block[v->cur_blk_idx][5]);
00280 }
00281 }
00282 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
00283 v->block[v->cur_blk_idx][1]);
00284 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
00285 v->block[v->cur_blk_idx][3]);
00286
00287 if (s->mb_x == s->mb_width - 1) {
00288 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
00289 v->over_flags_plane[mb_pos - s->mb_stride])) {
00290 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
00291 v->block[v->cur_blk_idx][0]);
00292 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
00293 v->block[v->cur_blk_idx][1]);
00294 if (!(s->flags & CODEC_FLAG_GRAY)) {
00295 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
00296 v->block[v->cur_blk_idx][4]);
00297 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
00298 v->block[v->cur_blk_idx][5]);
00299 }
00300 }
00301 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
00302 v->block[v->cur_blk_idx][2]);
00303 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
00304 v->block[v->cur_blk_idx][3]);
00305 }
00306 }
00307 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
00308 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
00309 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
00310 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
00311 v->block[v->left_blk_idx][0]);
00312 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
00313 v->block[v->left_blk_idx][1]);
00314 if (!(s->flags & CODEC_FLAG_GRAY)) {
00315 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
00316 v->block[v->left_blk_idx][4]);
00317 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
00318 v->block[v->left_blk_idx][5]);
00319 }
00320 }
00321 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
00322 v->block[v->left_blk_idx][2]);
00323 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
00324 v->block[v->left_blk_idx][3]);
00325 }
00326 }
00327
00331 static void vc1_mc_1mv(VC1Context *v, int dir)
00332 {
00333 MpegEncContext *s = &v->s;
00334 DSPContext *dsp = &v->s.dsp;
00335 uint8_t *srcY, *srcU, *srcV;
00336 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
00337 int off, off_uv;
00338 int v_edge_pos = s->v_edge_pos >> v->field_mode;
00339
00340 if ((!v->field_mode ||
00341 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
00342 !v->s.last_picture.f.data[0])
00343 return;
00344
00345 mx = s->mv[dir][0][0];
00346 my = s->mv[dir][0][1];
00347
00348
00349 if (s->pict_type == AV_PICTURE_TYPE_P) {
00350 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
00351 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
00352 }
00353
00354 uvmx = (mx + ((mx & 3) == 3)) >> 1;
00355 uvmy = (my + ((my & 3) == 3)) >> 1;
00356 v->luma_mv[s->mb_x][0] = uvmx;
00357 v->luma_mv[s->mb_x][1] = uvmy;
00358
00359 if (v->field_mode &&
00360 v->cur_field_type != v->ref_field_type[dir]) {
00361 my = my - 2 + 4 * v->cur_field_type;
00362 uvmy = uvmy - 2 + 4 * v->cur_field_type;
00363 }
00364
00365
00366 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
00367 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
00368 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
00369 }
00370 if (v->field_mode) {
00371 if (!dir) {
00372 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
00373 srcY = s->current_picture.f.data[0];
00374 srcU = s->current_picture.f.data[1];
00375 srcV = s->current_picture.f.data[2];
00376 } else {
00377 srcY = s->last_picture.f.data[0];
00378 srcU = s->last_picture.f.data[1];
00379 srcV = s->last_picture.f.data[2];
00380 }
00381 } else {
00382 srcY = s->next_picture.f.data[0];
00383 srcU = s->next_picture.f.data[1];
00384 srcV = s->next_picture.f.data[2];
00385 }
00386 } else {
00387 if (!dir) {
00388 srcY = s->last_picture.f.data[0];
00389 srcU = s->last_picture.f.data[1];
00390 srcV = s->last_picture.f.data[2];
00391 } else {
00392 srcY = s->next_picture.f.data[0];
00393 srcU = s->next_picture.f.data[1];
00394 srcV = s->next_picture.f.data[2];
00395 }
00396 }
00397
00398 src_x = s->mb_x * 16 + (mx >> 2);
00399 src_y = s->mb_y * 16 + (my >> 2);
00400 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
00401 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
00402
00403 if (v->profile != PROFILE_ADVANCED) {
00404 src_x = av_clip( src_x, -16, s->mb_width * 16);
00405 src_y = av_clip( src_y, -16, s->mb_height * 16);
00406 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
00407 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
00408 } else {
00409 src_x = av_clip( src_x, -17, s->avctx->coded_width);
00410 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
00411 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
00412 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
00413 }
00414
00415 srcY += src_y * s->linesize + src_x;
00416 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
00417 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
00418
00419 if (v->field_mode && v->ref_field_type[dir]) {
00420 srcY += s->current_picture_ptr->f.linesize[0];
00421 srcU += s->current_picture_ptr->f.linesize[1];
00422 srcV += s->current_picture_ptr->f.linesize[2];
00423 }
00424
00425
00426 if (s->flags & CODEC_FLAG_GRAY) {
00427 srcU = s->edge_emu_buffer + 18 * s->linesize;
00428 srcV = s->edge_emu_buffer + 18 * s->linesize;
00429 }
00430
00431 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00432 || s->h_edge_pos < 22 || v_edge_pos < 22
00433 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
00434 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 3) {
00435 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
00436
00437 srcY -= s->mspel * (1 + s->linesize);
00438 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
00439 17 + s->mspel * 2, 17 + s->mspel * 2,
00440 src_x - s->mspel, src_y - s->mspel,
00441 s->h_edge_pos, v_edge_pos);
00442 srcY = s->edge_emu_buffer;
00443 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
00444 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
00445 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
00446 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
00447 srcU = uvbuf;
00448 srcV = uvbuf + 16;
00449
00450 if (v->rangeredfrm) {
00451 int i, j;
00452 uint8_t *src, *src2;
00453
00454 src = srcY;
00455 for (j = 0; j < 17 + s->mspel * 2; j++) {
00456 for (i = 0; i < 17 + s->mspel * 2; i++)
00457 src[i] = ((src[i] - 128) >> 1) + 128;
00458 src += s->linesize;
00459 }
00460 src = srcU;
00461 src2 = srcV;
00462 for (j = 0; j < 9; j++) {
00463 for (i = 0; i < 9; i++) {
00464 src[i] = ((src[i] - 128) >> 1) + 128;
00465 src2[i] = ((src2[i] - 128) >> 1) + 128;
00466 }
00467 src += s->uvlinesize;
00468 src2 += s->uvlinesize;
00469 }
00470 }
00471
00472 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00473 int i, j;
00474 uint8_t *src, *src2;
00475
00476 src = srcY;
00477 for (j = 0; j < 17 + s->mspel * 2; j++) {
00478 for (i = 0; i < 17 + s->mspel * 2; i++)
00479 src[i] = v->luty[src[i]];
00480 src += s->linesize;
00481 }
00482 src = srcU;
00483 src2 = srcV;
00484 for (j = 0; j < 9; j++) {
00485 for (i = 0; i < 9; i++) {
00486 src[i] = v->lutuv[src[i]];
00487 src2[i] = v->lutuv[src2[i]];
00488 }
00489 src += s->uvlinesize;
00490 src2 += s->uvlinesize;
00491 }
00492 }
00493 srcY += s->mspel * (1 + s->linesize);
00494 }
00495
00496 if (v->field_mode && v->second_field) {
00497 off = s->current_picture_ptr->f.linesize[0];
00498 off_uv = s->current_picture_ptr->f.linesize[1];
00499 } else {
00500 off = 0;
00501 off_uv = 0;
00502 }
00503 if (s->mspel) {
00504 dxy = ((my & 3) << 2) | (mx & 3);
00505 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
00506 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
00507 srcY += s->linesize * 8;
00508 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
00509 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
00510 } else {
00511 dxy = (my & 2) | ((mx & 2) >> 1);
00512 if (!v->rnd)
00513 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
00514 else
00515 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
00516 }
00517
00518 if (s->flags & CODEC_FLAG_GRAY) return;
00519
00520 uvmx = (uvmx & 3) << 1;
00521 uvmy = (uvmy & 3) << 1;
00522 if (!v->rnd) {
00523 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
00524 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
00525 } else {
00526 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
00527 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
00528 }
00529 }
00530
00531 static inline int median4(int a, int b, int c, int d)
00532 {
00533 if (a < b) {
00534 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
00535 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
00536 } else {
00537 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
00538 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
00539 }
00540 }
00541
00544 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
00545 {
00546 MpegEncContext *s = &v->s;
00547 DSPContext *dsp = &v->s.dsp;
00548 uint8_t *srcY;
00549 int dxy, mx, my, src_x, src_y;
00550 int off;
00551 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
00552 int v_edge_pos = s->v_edge_pos >> v->field_mode;
00553
00554 if ((!v->field_mode ||
00555 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
00556 !v->s.last_picture.f.data[0])
00557 return;
00558
00559 mx = s->mv[dir][n][0];
00560 my = s->mv[dir][n][1];
00561
00562 if (!dir) {
00563 if (v->field_mode) {
00564 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
00565 srcY = s->current_picture.f.data[0];
00566 else
00567 srcY = s->last_picture.f.data[0];
00568 } else
00569 srcY = s->last_picture.f.data[0];
00570 } else
00571 srcY = s->next_picture.f.data[0];
00572
00573 if (v->field_mode) {
00574 if (v->cur_field_type != v->ref_field_type[dir])
00575 my = my - 2 + 4 * v->cur_field_type;
00576 }
00577
00578 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
00579 int same_count = 0, opp_count = 0, k;
00580 int chosen_mv[2][4][2], f;
00581 int tx, ty;
00582 for (k = 0; k < 4; k++) {
00583 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
00584 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
00585 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
00586 opp_count += f;
00587 same_count += 1 - f;
00588 }
00589 f = opp_count > same_count;
00590 switch (f ? opp_count : same_count) {
00591 case 4:
00592 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
00593 chosen_mv[f][2][0], chosen_mv[f][3][0]);
00594 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
00595 chosen_mv[f][2][1], chosen_mv[f][3][1]);
00596 break;
00597 case 3:
00598 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
00599 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
00600 break;
00601 case 2:
00602 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
00603 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
00604 break;
00605 }
00606 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
00607 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
00608 for (k = 0; k < 4; k++)
00609 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
00610 }
00611
00612 if (v->fcm == ILACE_FRAME) {
00613 int qx, qy;
00614 int width = s->avctx->coded_width;
00615 int height = s->avctx->coded_height >> 1;
00616 qx = (s->mb_x * 16) + (mx >> 2);
00617 qy = (s->mb_y * 8) + (my >> 3);
00618
00619 if (qx < -17)
00620 mx -= 4 * (qx + 17);
00621 else if (qx > width)
00622 mx -= 4 * (qx - width);
00623 if (qy < -18)
00624 my -= 8 * (qy + 18);
00625 else if (qy > height + 1)
00626 my -= 8 * (qy - height - 1);
00627 }
00628
00629 if ((v->fcm == ILACE_FRAME) && fieldmv)
00630 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
00631 else
00632 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
00633 if (v->field_mode && v->second_field)
00634 off += s->current_picture_ptr->f.linesize[0];
00635
00636 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
00637 if (!fieldmv)
00638 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
00639 else
00640 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
00641
00642 if (v->profile != PROFILE_ADVANCED) {
00643 src_x = av_clip(src_x, -16, s->mb_width * 16);
00644 src_y = av_clip(src_y, -16, s->mb_height * 16);
00645 } else {
00646 src_x = av_clip(src_x, -17, s->avctx->coded_width);
00647 if (v->fcm == ILACE_FRAME) {
00648 if (src_y & 1)
00649 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
00650 else
00651 src_y = av_clip(src_y, -18, s->avctx->coded_height);
00652 } else {
00653 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
00654 }
00655 }
00656
00657 srcY += src_y * s->linesize + src_x;
00658 if (v->field_mode && v->ref_field_type[dir])
00659 srcY += s->current_picture_ptr->f.linesize[0];
00660
00661 if (fieldmv && !(src_y & 1))
00662 v_edge_pos--;
00663 if (fieldmv && (src_y & 1) && src_y < 4)
00664 src_y--;
00665 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00666 || s->h_edge_pos < 13 || v_edge_pos < 23
00667 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
00668 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
00669 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
00670
00671 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
00672 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
00673 src_x - s->mspel, src_y - (s->mspel << fieldmv),
00674 s->h_edge_pos, v_edge_pos);
00675 srcY = s->edge_emu_buffer;
00676
00677 if (v->rangeredfrm) {
00678 int i, j;
00679 uint8_t *src;
00680
00681 src = srcY;
00682 for (j = 0; j < 9 + s->mspel * 2; j++) {
00683 for (i = 0; i < 9 + s->mspel * 2; i++)
00684 src[i] = ((src[i] - 128) >> 1) + 128;
00685 src += s->linesize << fieldmv;
00686 }
00687 }
00688
00689 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00690 int i, j;
00691 uint8_t *src;
00692
00693 src = srcY;
00694 for (j = 0; j < 9 + s->mspel * 2; j++) {
00695 for (i = 0; i < 9 + s->mspel * 2; i++)
00696 src[i] = v->luty[src[i]];
00697 src += s->linesize << fieldmv;
00698 }
00699 }
00700 srcY += s->mspel * (1 + (s->linesize << fieldmv));
00701 }
00702
00703 if (s->mspel) {
00704 dxy = ((my & 3) << 2) | (mx & 3);
00705 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
00706 } else {
00707 dxy = (my & 2) | ((mx & 2) >> 1);
00708 if (!v->rnd)
00709 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
00710 else
00711 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
00712 }
00713 }
00714
00715 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
00716 {
00717 int idx, i;
00718 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
00719
00720 idx = ((a[3] != flag) << 3)
00721 | ((a[2] != flag) << 2)
00722 | ((a[1] != flag) << 1)
00723 | (a[0] != flag);
00724 if (!idx) {
00725 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
00726 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
00727 return 4;
00728 } else if (count[idx] == 1) {
00729 switch (idx) {
00730 case 0x1:
00731 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
00732 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
00733 return 3;
00734 case 0x2:
00735 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
00736 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
00737 return 3;
00738 case 0x4:
00739 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
00740 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
00741 return 3;
00742 case 0x8:
00743 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
00744 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
00745 return 3;
00746 }
00747 } else if (count[idx] == 2) {
00748 int t1 = 0, t2 = 0;
00749 for (i = 0; i < 3; i++)
00750 if (!a[i]) {
00751 t1 = i;
00752 break;
00753 }
00754 for (i = t1 + 1; i < 4; i++)
00755 if (!a[i]) {
00756 t2 = i;
00757 break;
00758 }
00759 *tx = (mvx[t1] + mvx[t2]) / 2;
00760 *ty = (mvy[t1] + mvy[t2]) / 2;
00761 return 2;
00762 } else {
00763 return 0;
00764 }
00765 return -1;
00766 }
00767
00770 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
00771 {
00772 MpegEncContext *s = &v->s;
00773 DSPContext *dsp = &v->s.dsp;
00774 uint8_t *srcU, *srcV;
00775 int uvmx, uvmy, uvsrc_x, uvsrc_y;
00776 int k, tx = 0, ty = 0;
00777 int mvx[4], mvy[4], intra[4], mv_f[4];
00778 int valid_count;
00779 int chroma_ref_type = v->cur_field_type, off = 0;
00780 int v_edge_pos = s->v_edge_pos >> v->field_mode;
00781
00782 if (!v->field_mode && !v->s.last_picture.f.data[0])
00783 return;
00784 if (s->flags & CODEC_FLAG_GRAY)
00785 return;
00786
00787 for (k = 0; k < 4; k++) {
00788 mvx[k] = s->mv[dir][k][0];
00789 mvy[k] = s->mv[dir][k][1];
00790 intra[k] = v->mb_type[0][s->block_index[k]];
00791 if (v->field_mode)
00792 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
00793 }
00794
00795
00796 if (!v->field_mode || (v->field_mode && !v->numref)) {
00797 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
00798 if (!valid_count) {
00799 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
00800 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
00801 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
00802 return;
00803 }
00804 } else {
00805 int dominant = 0;
00806 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
00807 dominant = 1;
00808 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
00809 if (dominant)
00810 chroma_ref_type = !v->cur_field_type;
00811 }
00812 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
00813 return;
00814 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
00815 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
00816 uvmx = (tx + ((tx & 3) == 3)) >> 1;
00817 uvmy = (ty + ((ty & 3) == 3)) >> 1;
00818
00819 v->luma_mv[s->mb_x][0] = uvmx;
00820 v->luma_mv[s->mb_x][1] = uvmy;
00821
00822 if (v->fastuvmc) {
00823 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
00824 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
00825 }
00826
00827 if (v->cur_field_type != chroma_ref_type)
00828 uvmy += 2 - 4 * chroma_ref_type;
00829
00830 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
00831 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
00832
00833 if (v->profile != PROFILE_ADVANCED) {
00834 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
00835 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
00836 } else {
00837 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
00838 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
00839 }
00840
00841 if (!dir) {
00842 if (v->field_mode) {
00843 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
00844 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
00845 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
00846 } else {
00847 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
00848 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
00849 }
00850 } else {
00851 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
00852 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
00853 }
00854 } else {
00855 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
00856 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
00857 }
00858
00859 if (v->field_mode) {
00860 if (chroma_ref_type) {
00861 srcU += s->current_picture_ptr->f.linesize[1];
00862 srcV += s->current_picture_ptr->f.linesize[2];
00863 }
00864 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
00865 }
00866
00867 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
00868 || s->h_edge_pos < 18 || v_edge_pos < 18
00869 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
00870 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
00871 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
00872 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
00873 s->h_edge_pos >> 1, v_edge_pos >> 1);
00874 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
00875 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
00876 s->h_edge_pos >> 1, v_edge_pos >> 1);
00877 srcU = s->edge_emu_buffer;
00878 srcV = s->edge_emu_buffer + 16;
00879
00880
00881 if (v->rangeredfrm) {
00882 int i, j;
00883 uint8_t *src, *src2;
00884
00885 src = srcU;
00886 src2 = srcV;
00887 for (j = 0; j < 9; j++) {
00888 for (i = 0; i < 9; i++) {
00889 src[i] = ((src[i] - 128) >> 1) + 128;
00890 src2[i] = ((src2[i] - 128) >> 1) + 128;
00891 }
00892 src += s->uvlinesize;
00893 src2 += s->uvlinesize;
00894 }
00895 }
00896
00897 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00898 int i, j;
00899 uint8_t *src, *src2;
00900
00901 src = srcU;
00902 src2 = srcV;
00903 for (j = 0; j < 9; j++) {
00904 for (i = 0; i < 9; i++) {
00905 src[i] = v->lutuv[src[i]];
00906 src2[i] = v->lutuv[src2[i]];
00907 }
00908 src += s->uvlinesize;
00909 src2 += s->uvlinesize;
00910 }
00911 }
00912 }
00913
00914
00915 uvmx = (uvmx & 3) << 1;
00916 uvmy = (uvmy & 3) << 1;
00917 if (!v->rnd) {
00918 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
00919 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
00920 } else {
00921 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
00922 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
00923 }
00924 }
00925
00928 static void vc1_mc_4mv_chroma4(VC1Context *v)
00929 {
00930 MpegEncContext *s = &v->s;
00931 DSPContext *dsp = &v->s.dsp;
00932 uint8_t *srcU, *srcV;
00933 int uvsrc_x, uvsrc_y;
00934 int uvmx_field[4], uvmy_field[4];
00935 int i, off, tx, ty;
00936 int fieldmv = v->blk_mv_type[s->block_index[0]];
00937 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
00938 int v_dist = fieldmv ? 1 : 4;
00939 int v_edge_pos = s->v_edge_pos >> 1;
00940
00941 if (!v->s.last_picture.f.data[0])
00942 return;
00943 if (s->flags & CODEC_FLAG_GRAY)
00944 return;
00945
00946 for (i = 0; i < 4; i++) {
00947 tx = s->mv[0][i][0];
00948 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
00949 ty = s->mv[0][i][1];
00950 if (fieldmv)
00951 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
00952 else
00953 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
00954 }
00955
00956 for (i = 0; i < 4; i++) {
00957 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
00958 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
00959 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
00960
00961 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
00962 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
00963 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
00964 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
00965 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
00966 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
00967
00968 if (fieldmv && !(uvsrc_y & 1))
00969 v_edge_pos--;
00970 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
00971 uvsrc_y--;
00972 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
00973 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
00974 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
00975 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
00976 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
00977 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
00978 s->h_edge_pos >> 1, v_edge_pos);
00979 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
00980 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
00981 s->h_edge_pos >> 1, v_edge_pos);
00982 srcU = s->edge_emu_buffer;
00983 srcV = s->edge_emu_buffer + 16;
00984
00985
00986 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
00987 int i, j;
00988 uint8_t *src, *src2;
00989
00990 src = srcU;
00991 src2 = srcV;
00992 for (j = 0; j < 5; j++) {
00993 for (i = 0; i < 5; i++) {
00994 src[i] = v->lutuv[src[i]];
00995 src2[i] = v->lutuv[src2[i]];
00996 }
00997 src += s->uvlinesize << 1;
00998 src2 += s->uvlinesize << 1;
00999 }
01000 }
01001 }
01002 if (!v->rnd) {
01003 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
01004 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
01005 } else {
01006 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
01007 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
01008 }
01009 }
01010 }
01011
01012
01023 #define GET_MQUANT() \
01024 if (v->dquantfrm) { \
01025 int edges = 0; \
01026 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
01027 if (v->dqbilevel) { \
01028 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
01029 } else { \
01030 mqdiff = get_bits(gb, 3); \
01031 if (mqdiff != 7) \
01032 mquant = v->pq + mqdiff; \
01033 else \
01034 mquant = get_bits(gb, 5); \
01035 } \
01036 } \
01037 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
01038 edges = 1 << v->dqsbedge; \
01039 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
01040 edges = (3 << v->dqsbedge) % 15; \
01041 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
01042 edges = 15; \
01043 if ((edges&1) && !s->mb_x) \
01044 mquant = v->altpq; \
01045 if ((edges&2) && s->first_slice_line) \
01046 mquant = v->altpq; \
01047 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
01048 mquant = v->altpq; \
01049 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
01050 mquant = v->altpq; \
01051 if (!mquant || mquant > 31) { \
01052 av_log(v->s.avctx, AV_LOG_ERROR, \
01053 "Overriding invalid mquant %d\n", mquant); \
01054 mquant = 1; \
01055 } \
01056 }
01057
01065 #define GET_MVDATA(_dmv_x, _dmv_y) \
01066 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
01067 VC1_MV_DIFF_VLC_BITS, 2); \
01068 if (index > 36) { \
01069 mb_has_coeffs = 1; \
01070 index -= 37; \
01071 } else \
01072 mb_has_coeffs = 0; \
01073 s->mb_intra = 0; \
01074 if (!index) { \
01075 _dmv_x = _dmv_y = 0; \
01076 } else if (index == 35) { \
01077 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
01078 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
01079 } else if (index == 36) { \
01080 _dmv_x = 0; \
01081 _dmv_y = 0; \
01082 s->mb_intra = 1; \
01083 } else { \
01084 index1 = index % 6; \
01085 if (!s->quarter_sample && index1 == 5) val = 1; \
01086 else val = 0; \
01087 if (size_table[index1] - val > 0) \
01088 val = get_bits(gb, size_table[index1] - val); \
01089 else val = 0; \
01090 sign = 0 - (val&1); \
01091 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
01092 \
01093 index1 = index / 6; \
01094 if (!s->quarter_sample && index1 == 5) val = 1; \
01095 else val = 0; \
01096 if (size_table[index1] - val > 0) \
01097 val = get_bits(gb, size_table[index1] - val); \
01098 else val = 0; \
01099 sign = 0 - (val & 1); \
01100 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
01101 }
01102
01103 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
01104 int *dmv_y, int *pred_flag)
01105 {
01106 int index, index1;
01107 int extend_x = 0, extend_y = 0;
01108 GetBitContext *gb = &v->s.gb;
01109 int bits, esc;
01110 int val, sign;
01111 const int* offs_tab;
01112
01113 if (v->numref) {
01114 bits = VC1_2REF_MVDATA_VLC_BITS;
01115 esc = 125;
01116 } else {
01117 bits = VC1_1REF_MVDATA_VLC_BITS;
01118 esc = 71;
01119 }
01120 switch (v->dmvrange) {
01121 case 1:
01122 extend_x = 1;
01123 break;
01124 case 2:
01125 extend_y = 1;
01126 break;
01127 case 3:
01128 extend_x = extend_y = 1;
01129 break;
01130 }
01131 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
01132 if (index == esc) {
01133 *dmv_x = get_bits(gb, v->k_x);
01134 *dmv_y = get_bits(gb, v->k_y);
01135 if (v->numref) {
01136 *pred_flag = *dmv_y & 1;
01137 *dmv_y = (*dmv_y + *pred_flag) >> 1;
01138 }
01139 }
01140 else {
01141 av_assert0(index < esc);
01142 if (extend_x)
01143 offs_tab = offset_table2;
01144 else
01145 offs_tab = offset_table1;
01146 index1 = (index + 1) % 9;
01147 if (index1 != 0) {
01148 val = get_bits(gb, index1 + extend_x);
01149 sign = 0 -(val & 1);
01150 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
01151 } else
01152 *dmv_x = 0;
01153 if (extend_y)
01154 offs_tab = offset_table2;
01155 else
01156 offs_tab = offset_table1;
01157 index1 = (index + 1) / 9;
01158 if (index1 > v->numref) {
01159 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
01160 sign = 0 - (val & 1);
01161 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
01162 } else
01163 *dmv_y = 0;
01164 if (v->numref)
01165 *pred_flag = index1 & 1;
01166 }
01167 }
01168
01169 static av_always_inline int scaleforsame_x(VC1Context *v, int n , int dir)
01170 {
01171 int scaledvalue, refdist;
01172 int scalesame1, scalesame2;
01173 int scalezone1_x, zone1offset_x;
01174 int table_index = dir ^ v->second_field;
01175
01176 if (v->s.pict_type != AV_PICTURE_TYPE_B)
01177 refdist = v->refdist;
01178 else
01179 refdist = dir ? v->brfd : v->frfd;
01180 if (refdist > 3)
01181 refdist = 3;
01182 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
01183 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
01184 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
01185 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
01186
01187 if (FFABS(n) > 255)
01188 scaledvalue = n;
01189 else {
01190 if (FFABS(n) < scalezone1_x)
01191 scaledvalue = (n * scalesame1) >> 8;
01192 else {
01193 if (n < 0)
01194 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
01195 else
01196 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
01197 }
01198 }
01199 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
01200 }
01201
01202 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n , int dir)
01203 {
01204 int scaledvalue, refdist;
01205 int scalesame1, scalesame2;
01206 int scalezone1_y, zone1offset_y;
01207 int table_index = dir ^ v->second_field;
01208
01209 if (v->s.pict_type != AV_PICTURE_TYPE_B)
01210 refdist = v->refdist;
01211 else
01212 refdist = dir ? v->brfd : v->frfd;
01213 if (refdist > 3)
01214 refdist = 3;
01215 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
01216 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
01217 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
01218 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
01219
01220 if (FFABS(n) > 63)
01221 scaledvalue = n;
01222 else {
01223 if (FFABS(n) < scalezone1_y)
01224 scaledvalue = (n * scalesame1) >> 8;
01225 else {
01226 if (n < 0)
01227 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
01228 else
01229 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
01230 }
01231 }
01232
01233 if (v->cur_field_type && !v->ref_field_type[dir])
01234 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
01235 else
01236 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
01237 }
01238
01239 static av_always_inline int scaleforopp_x(VC1Context *v, int n )
01240 {
01241 int scalezone1_x, zone1offset_x;
01242 int scaleopp1, scaleopp2, brfd;
01243 int scaledvalue;
01244
01245 brfd = FFMIN(v->brfd, 3);
01246 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
01247 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
01248 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
01249 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
01250
01251 if (FFABS(n) > 255)
01252 scaledvalue = n;
01253 else {
01254 if (FFABS(n) < scalezone1_x)
01255 scaledvalue = (n * scaleopp1) >> 8;
01256 else {
01257 if (n < 0)
01258 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
01259 else
01260 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
01261 }
01262 }
01263 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
01264 }
01265
01266 static av_always_inline int scaleforopp_y(VC1Context *v, int n , int dir)
01267 {
01268 int scalezone1_y, zone1offset_y;
01269 int scaleopp1, scaleopp2, brfd;
01270 int scaledvalue;
01271
01272 brfd = FFMIN(v->brfd, 3);
01273 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
01274 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
01275 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
01276 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
01277
01278 if (FFABS(n) > 63)
01279 scaledvalue = n;
01280 else {
01281 if (FFABS(n) < scalezone1_y)
01282 scaledvalue = (n * scaleopp1) >> 8;
01283 else {
01284 if (n < 0)
01285 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
01286 else
01287 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
01288 }
01289 }
01290 if (v->cur_field_type && !v->ref_field_type[dir]) {
01291 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
01292 } else {
01293 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
01294 }
01295 }
01296
01297 static av_always_inline int scaleforsame(VC1Context *v, int i, int n ,
01298 int dim, int dir)
01299 {
01300 int brfd, scalesame;
01301 int hpel = 1 - v->s.quarter_sample;
01302
01303 n >>= hpel;
01304 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
01305 if (dim)
01306 n = scaleforsame_y(v, i, n, dir) << hpel;
01307 else
01308 n = scaleforsame_x(v, n, dir) << hpel;
01309 return n;
01310 }
01311 brfd = FFMIN(v->brfd, 3);
01312 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
01313
01314 n = (n * scalesame >> 8) << hpel;
01315 return n;
01316 }
01317
01318 static av_always_inline int scaleforopp(VC1Context *v, int n ,
01319 int dim, int dir)
01320 {
01321 int refdist, scaleopp;
01322 int hpel = 1 - v->s.quarter_sample;
01323
01324 n >>= hpel;
01325 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
01326 if (dim)
01327 n = scaleforopp_y(v, n, dir) << hpel;
01328 else
01329 n = scaleforopp_x(v, n) << hpel;
01330 return n;
01331 }
01332 if (v->s.pict_type != AV_PICTURE_TYPE_B)
01333 refdist = FFMIN(v->refdist, 3);
01334 else
01335 refdist = dir ? v->brfd : v->frfd;
01336 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
01337
01338 n = (n * scaleopp >> 8) << hpel;
01339 return n;
01340 }
01341
01344 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
01345 int mv1, int r_x, int r_y, uint8_t* is_intra,
01346 int pred_flag, int dir)
01347 {
01348 MpegEncContext *s = &v->s;
01349 int xy, wrap, off = 0;
01350 int16_t *A, *B, *C;
01351 int px, py;
01352 int sum;
01353 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
01354 int opposit, a_f, b_f, c_f;
01355 int16_t field_predA[2];
01356 int16_t field_predB[2];
01357 int16_t field_predC[2];
01358 int a_valid, b_valid, c_valid;
01359 int hybridmv_thresh, y_bias = 0;
01360
01361 if (v->mv_mode == MV_PMODE_MIXED_MV ||
01362 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
01363 mixedmv_pic = 1;
01364 else
01365 mixedmv_pic = 0;
01366
01367 dmv_x <<= 1 - s->quarter_sample;
01368 dmv_y <<= 1 - s->quarter_sample;
01369
01370 wrap = s->b8_stride;
01371 xy = s->block_index[n];
01372
01373 if (s->mb_intra) {
01374 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
01375 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
01376 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
01377 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
01378 if (mv1) {
01379 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
01380 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
01381 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
01382 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
01383 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
01384 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
01385 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
01386 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
01387 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
01388 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
01389 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
01390 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
01391 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
01392 }
01393 return;
01394 }
01395
01396 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
01397 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
01398 if (mv1) {
01399 if (v->field_mode && mixedmv_pic)
01400 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
01401 else
01402 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
01403 } else {
01404
01405 switch (n) {
01406 case 0:
01407 off = (s->mb_x > 0) ? -1 : 1;
01408 break;
01409 case 1:
01410 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
01411 break;
01412 case 2:
01413 off = 1;
01414 break;
01415 case 3:
01416 off = -1;
01417 }
01418 }
01419 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
01420
01421 a_valid = !s->first_slice_line || (n == 2 || n == 3);
01422 b_valid = a_valid && (s->mb_width > 1);
01423 c_valid = s->mb_x || (n == 1 || n == 3);
01424 if (v->field_mode) {
01425 a_valid = a_valid && !is_intra[xy - wrap];
01426 b_valid = b_valid && !is_intra[xy - wrap + off];
01427 c_valid = c_valid && !is_intra[xy - 1];
01428 }
01429
01430 if (a_valid) {
01431 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
01432 num_oppfield += a_f;
01433 num_samefield += 1 - a_f;
01434 field_predA[0] = A[0];
01435 field_predA[1] = A[1];
01436 } else {
01437 field_predA[0] = field_predA[1] = 0;
01438 a_f = 0;
01439 }
01440 if (b_valid) {
01441 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
01442 num_oppfield += b_f;
01443 num_samefield += 1 - b_f;
01444 field_predB[0] = B[0];
01445 field_predB[1] = B[1];
01446 } else {
01447 field_predB[0] = field_predB[1] = 0;
01448 b_f = 0;
01449 }
01450 if (c_valid) {
01451 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
01452 num_oppfield += c_f;
01453 num_samefield += 1 - c_f;
01454 field_predC[0] = C[0];
01455 field_predC[1] = C[1];
01456 } else {
01457 field_predC[0] = field_predC[1] = 0;
01458 c_f = 0;
01459 }
01460
01461 if (v->field_mode) {
01462 if (num_samefield <= num_oppfield)
01463 opposit = 1 - pred_flag;
01464 else
01465 opposit = pred_flag;
01466 } else
01467 opposit = 0;
01468 if (opposit) {
01469 if (a_valid && !a_f) {
01470 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
01471 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
01472 }
01473 if (b_valid && !b_f) {
01474 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
01475 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
01476 }
01477 if (c_valid && !c_f) {
01478 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
01479 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
01480 }
01481 v->mv_f[dir][xy + v->blocks_off] = 1;
01482 v->ref_field_type[dir] = !v->cur_field_type;
01483 } else {
01484 if (a_valid && a_f) {
01485 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
01486 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
01487 }
01488 if (b_valid && b_f) {
01489 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
01490 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
01491 }
01492 if (c_valid && c_f) {
01493 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
01494 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
01495 }
01496 v->mv_f[dir][xy + v->blocks_off] = 0;
01497 v->ref_field_type[dir] = v->cur_field_type;
01498 }
01499
01500 if (a_valid) {
01501 px = field_predA[0];
01502 py = field_predA[1];
01503 } else if (c_valid) {
01504 px = field_predC[0];
01505 py = field_predC[1];
01506 } else if (b_valid) {
01507 px = field_predB[0];
01508 py = field_predB[1];
01509 } else {
01510 px = 0;
01511 py = 0;
01512 }
01513
01514 if (num_samefield + num_oppfield > 1) {
01515 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
01516 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
01517 }
01518
01519
01520 if (!v->field_mode) {
01521 int qx, qy, X, Y;
01522 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
01523 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
01524 X = (s->mb_width << 6) - 4;
01525 Y = (s->mb_height << 6) - 4;
01526 if (mv1) {
01527 if (qx + px < -60) px = -60 - qx;
01528 if (qy + py < -60) py = -60 - qy;
01529 } else {
01530 if (qx + px < -28) px = -28 - qx;
01531 if (qy + py < -28) py = -28 - qy;
01532 }
01533 if (qx + px > X) px = X - qx;
01534 if (qy + py > Y) py = Y - qy;
01535 }
01536
01537 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
01538
01539 hybridmv_thresh = 32;
01540 if (a_valid && c_valid) {
01541 if (is_intra[xy - wrap])
01542 sum = FFABS(px) + FFABS(py);
01543 else
01544 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
01545 if (sum > hybridmv_thresh) {
01546 if (get_bits1(&s->gb)) {
01547 px = field_predA[0];
01548 py = field_predA[1];
01549 } else {
01550 px = field_predC[0];
01551 py = field_predC[1];
01552 }
01553 } else {
01554 if (is_intra[xy - 1])
01555 sum = FFABS(px) + FFABS(py);
01556 else
01557 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
01558 if (sum > hybridmv_thresh) {
01559 if (get_bits1(&s->gb)) {
01560 px = field_predA[0];
01561 py = field_predA[1];
01562 } else {
01563 px = field_predC[0];
01564 py = field_predC[1];
01565 }
01566 }
01567 }
01568 }
01569 }
01570
01571 if (v->field_mode && !s->quarter_sample) {
01572 r_x <<= 1;
01573 r_y <<= 1;
01574 }
01575 if (v->field_mode && v->numref)
01576 r_y >>= 1;
01577 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
01578 y_bias = 1;
01579
01580 s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
01581 s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
01582 if (mv1) {
01583 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
01584 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
01585 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
01586 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
01587 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
01588 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
01589 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
01590 v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
01591 }
01592 }
01593
01596 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
01597 int mvn, int r_x, int r_y, uint8_t* is_intra)
01598 {
01599 MpegEncContext *s = &v->s;
01600 int xy, wrap, off = 0;
01601 int A[2], B[2], C[2];
01602 int px, py;
01603 int a_valid = 0, b_valid = 0, c_valid = 0;
01604 int field_a, field_b, field_c;
01605 int total_valid, num_samefield, num_oppfield;
01606 int pos_c, pos_b, n_adj;
01607
01608 wrap = s->b8_stride;
01609 xy = s->block_index[n];
01610
01611 if (s->mb_intra) {
01612 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
01613 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
01614 s->current_picture.f.motion_val[1][xy][0] = 0;
01615 s->current_picture.f.motion_val[1][xy][1] = 0;
01616 if (mvn == 1) {
01617 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
01618 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
01619 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
01620 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
01621 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
01622 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
01623 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
01624 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
01625 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
01626 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
01627 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
01628 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
01629 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
01630 }
01631 return;
01632 }
01633
01634 off = ((n == 0) || (n == 1)) ? 1 : -1;
01635
01636 if (s->mb_x || (n == 1) || (n == 3)) {
01637 if ((v->blk_mv_type[xy])
01638 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) {
01639 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
01640 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
01641 a_valid = 1;
01642 } else {
01643 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
01644 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
01645 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
01646 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
01647 a_valid = 1;
01648 }
01649 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
01650 a_valid = 0;
01651 A[0] = A[1] = 0;
01652 }
01653 } else
01654 A[0] = A[1] = 0;
01655
01656 B[0] = B[1] = C[0] = C[1] = 0;
01657 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
01658 if (!s->first_slice_line) {
01659 if (!v->is_intra[s->mb_x - s->mb_stride]) {
01660 b_valid = 1;
01661 n_adj = n | 2;
01662 pos_b = s->block_index[n_adj] - 2 * wrap;
01663 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
01664 n_adj = (n & 2) | (n & 1);
01665 }
01666 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
01667 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
01668 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
01669 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
01670 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
01671 }
01672 }
01673 if (s->mb_width > 1) {
01674 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
01675 c_valid = 1;
01676 n_adj = 2;
01677 pos_c = s->block_index[2] - 2 * wrap + 2;
01678 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
01679 n_adj = n & 2;
01680 }
01681 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
01682 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
01683 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
01684 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
01685 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
01686 }
01687 if (s->mb_x == s->mb_width - 1) {
01688 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
01689 c_valid = 1;
01690 n_adj = 3;
01691 pos_c = s->block_index[3] - 2 * wrap - 2;
01692 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
01693 n_adj = n | 1;
01694 }
01695 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
01696 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
01697 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
01698 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
01699 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
01700 }
01701 } else
01702 c_valid = 0;
01703 }
01704 }
01705 }
01706 }
01707 } else {
01708 pos_b = s->block_index[1];
01709 b_valid = 1;
01710 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
01711 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
01712 pos_c = s->block_index[0];
01713 c_valid = 1;
01714 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
01715 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
01716 }
01717
01718 total_valid = a_valid + b_valid + c_valid;
01719
01720 if (!s->mb_x && !(n == 1 || n == 3)) {
01721 A[0] = A[1] = 0;
01722 }
01723
01724 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
01725 B[0] = B[1] = C[0] = C[1] = 0;
01726 }
01727 if (!v->blk_mv_type[xy]) {
01728 if (s->mb_width == 1) {
01729 px = B[0];
01730 py = B[1];
01731 } else {
01732 if (total_valid >= 2) {
01733 px = mid_pred(A[0], B[0], C[0]);
01734 py = mid_pred(A[1], B[1], C[1]);
01735 } else if (total_valid) {
01736 if (a_valid) { px = A[0]; py = A[1]; }
01737 if (b_valid) { px = B[0]; py = B[1]; }
01738 if (c_valid) { px = C[0]; py = C[1]; }
01739 } else
01740 px = py = 0;
01741 }
01742 } else {
01743 if (a_valid)
01744 field_a = (A[1] & 4) ? 1 : 0;
01745 else
01746 field_a = 0;
01747 if (b_valid)
01748 field_b = (B[1] & 4) ? 1 : 0;
01749 else
01750 field_b = 0;
01751 if (c_valid)
01752 field_c = (C[1] & 4) ? 1 : 0;
01753 else
01754 field_c = 0;
01755
01756 num_oppfield = field_a + field_b + field_c;
01757 num_samefield = total_valid - num_oppfield;
01758 if (total_valid == 3) {
01759 if ((num_samefield == 3) || (num_oppfield == 3)) {
01760 px = mid_pred(A[0], B[0], C[0]);
01761 py = mid_pred(A[1], B[1], C[1]);
01762 } else if (num_samefield >= num_oppfield) {
01763
01764
01765 px = !field_a ? A[0] : B[0];
01766 py = !field_a ? A[1] : B[1];
01767 } else {
01768 px = field_a ? A[0] : B[0];
01769 py = field_a ? A[1] : B[1];
01770 }
01771 } else if (total_valid == 2) {
01772 if (num_samefield >= num_oppfield) {
01773 if (!field_a && a_valid) {
01774 px = A[0];
01775 py = A[1];
01776 } else if (!field_b && b_valid) {
01777 px = B[0];
01778 py = B[1];
01779 } else if (c_valid) {
01780 px = C[0];
01781 py = C[1];
01782 } else px = py = 0;
01783 } else {
01784 if (field_a && a_valid) {
01785 px = A[0];
01786 py = A[1];
01787 } else if (field_b && b_valid) {
01788 px = B[0];
01789 py = B[1];
01790 } else if (c_valid) {
01791 px = C[0];
01792 py = C[1];
01793 } else px = py = 0;
01794 }
01795 } else if (total_valid == 1) {
01796 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
01797 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
01798 } else
01799 px = py = 0;
01800 }
01801
01802
01803 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
01804 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
01805 if (mvn == 1) {
01806 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
01807 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
01808 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
01809 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
01810 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
01811 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
01812 } else if (mvn == 2) {
01813 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
01814 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
01815 s->mv[0][n + 1][0] = s->mv[0][n][0];
01816 s->mv[0][n + 1][1] = s->mv[0][n][1];
01817 }
01818 }
01819
01822 static void vc1_interp_mc(VC1Context *v)
01823 {
01824 MpegEncContext *s = &v->s;
01825 DSPContext *dsp = &v->s.dsp;
01826 uint8_t *srcY, *srcU, *srcV;
01827 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
01828 int off, off_uv;
01829 int v_edge_pos = s->v_edge_pos >> v->field_mode;
01830
01831 if (!v->field_mode && !v->s.next_picture.f.data[0])
01832 return;
01833
01834 mx = s->mv[1][0][0];
01835 my = s->mv[1][0][1];
01836 uvmx = (mx + ((mx & 3) == 3)) >> 1;
01837 uvmy = (my + ((my & 3) == 3)) >> 1;
01838 if (v->field_mode) {
01839 if (v->cur_field_type != v->ref_field_type[1])
01840 my = my - 2 + 4 * v->cur_field_type;
01841 uvmy = uvmy - 2 + 4 * v->cur_field_type;
01842 }
01843 if (v->fastuvmc) {
01844 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
01845 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
01846 }
01847 srcY = s->next_picture.f.data[0];
01848 srcU = s->next_picture.f.data[1];
01849 srcV = s->next_picture.f.data[2];
01850
01851 src_x = s->mb_x * 16 + (mx >> 2);
01852 src_y = s->mb_y * 16 + (my >> 2);
01853 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
01854 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
01855
01856 if (v->profile != PROFILE_ADVANCED) {
01857 src_x = av_clip( src_x, -16, s->mb_width * 16);
01858 src_y = av_clip( src_y, -16, s->mb_height * 16);
01859 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
01860 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
01861 } else {
01862 src_x = av_clip( src_x, -17, s->avctx->coded_width);
01863 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
01864 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
01865 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
01866 }
01867
01868 srcY += src_y * s->linesize + src_x;
01869 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
01870 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
01871
01872 if (v->field_mode && v->ref_field_type[1]) {
01873 srcY += s->current_picture_ptr->f.linesize[0];
01874 srcU += s->current_picture_ptr->f.linesize[1];
01875 srcV += s->current_picture_ptr->f.linesize[2];
01876 }
01877
01878
01879 if (s->flags & CODEC_FLAG_GRAY) {
01880 srcU = s->edge_emu_buffer + 18 * s->linesize;
01881 srcV = s->edge_emu_buffer + 18 * s->linesize;
01882 }
01883
01884 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
01885 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
01886 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
01887 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
01888
01889 srcY -= s->mspel * (1 + s->linesize);
01890 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
01891 17 + s->mspel * 2, 17 + s->mspel * 2,
01892 src_x - s->mspel, src_y - s->mspel,
01893 s->h_edge_pos, v_edge_pos);
01894 srcY = s->edge_emu_buffer;
01895 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
01896 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
01897 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
01898 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
01899 srcU = uvbuf;
01900 srcV = uvbuf + 16;
01901
01902 if (v->rangeredfrm) {
01903 int i, j;
01904 uint8_t *src, *src2;
01905
01906 src = srcY;
01907 for (j = 0; j < 17 + s->mspel * 2; j++) {
01908 for (i = 0; i < 17 + s->mspel * 2; i++)
01909 src[i] = ((src[i] - 128) >> 1) + 128;
01910 src += s->linesize;
01911 }
01912 src = srcU;
01913 src2 = srcV;
01914 for (j = 0; j < 9; j++) {
01915 for (i = 0; i < 9; i++) {
01916 src[i] = ((src[i] - 128) >> 1) + 128;
01917 src2[i] = ((src2[i] - 128) >> 1) + 128;
01918 }
01919 src += s->uvlinesize;
01920 src2 += s->uvlinesize;
01921 }
01922 }
01923 srcY += s->mspel * (1 + s->linesize);
01924 }
01925
01926 if (v->field_mode && v->second_field) {
01927 off = s->current_picture_ptr->f.linesize[0];
01928 off_uv = s->current_picture_ptr->f.linesize[1];
01929 } else {
01930 off = 0;
01931 off_uv = 0;
01932 }
01933
01934 if (s->mspel) {
01935 dxy = ((my & 3) << 2) | (mx & 3);
01936 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
01937 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
01938 srcY += s->linesize * 8;
01939 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
01940 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
01941 } else {
01942 dxy = (my & 2) | ((mx & 2) >> 1);
01943
01944 if (!v->rnd)
01945 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
01946 else
01947 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
01948 }
01949
01950 if (s->flags & CODEC_FLAG_GRAY) return;
01951
01952 uvmx = (uvmx & 3) << 1;
01953 uvmy = (uvmy & 3) << 1;
01954 if (!v->rnd) {
01955 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
01956 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
01957 } else {
01958 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
01959 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
01960 }
01961 }
01962
01963 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
01964 {
01965 int n = bfrac;
01966
01967 #if B_FRACTION_DEN==256
01968 if (inv)
01969 n -= 256;
01970 if (!qs)
01971 return 2 * ((value * n + 255) >> 9);
01972 return (value * n + 128) >> 8;
01973 #else
01974 if (inv)
01975 n -= B_FRACTION_DEN;
01976 if (!qs)
01977 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
01978 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
01979 #endif
01980 }
01981
01984 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
01985 int direct, int mode)
01986 {
01987 if (v->use_ic) {
01988 v->mv_mode2 = v->mv_mode;
01989 v->mv_mode = MV_PMODE_INTENSITY_COMP;
01990 }
01991 if (direct) {
01992 vc1_mc_1mv(v, 0);
01993 vc1_interp_mc(v);
01994 if (v->use_ic)
01995 v->mv_mode = v->mv_mode2;
01996 return;
01997 }
01998 if (mode == BMV_TYPE_INTERPOLATED) {
01999 vc1_mc_1mv(v, 0);
02000 vc1_interp_mc(v);
02001 if (v->use_ic)
02002 v->mv_mode = v->mv_mode2;
02003 return;
02004 }
02005
02006 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
02007 v->mv_mode = v->mv_mode2;
02008 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
02009 if (v->use_ic)
02010 v->mv_mode = v->mv_mode2;
02011 }
02012
02013 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
02014 int direct, int mvtype)
02015 {
02016 MpegEncContext *s = &v->s;
02017 int xy, wrap, off = 0;
02018 int16_t *A, *B, *C;
02019 int px, py;
02020 int sum;
02021 int r_x, r_y;
02022 const uint8_t *is_intra = v->mb_type[0];
02023
02024 r_x = v->range_x;
02025 r_y = v->range_y;
02026
02027 dmv_x[0] <<= 1 - s->quarter_sample;
02028 dmv_y[0] <<= 1 - s->quarter_sample;
02029 dmv_x[1] <<= 1 - s->quarter_sample;
02030 dmv_y[1] <<= 1 - s->quarter_sample;
02031
02032 wrap = s->b8_stride;
02033 xy = s->block_index[0];
02034
02035 if (s->mb_intra) {
02036 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
02037 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
02038 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
02039 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
02040 return;
02041 }
02042 if (!v->field_mode) {
02043 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
02044 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
02045 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
02046 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
02047
02048
02049 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));
02050 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));
02051 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));
02052 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));
02053 }
02054 if (direct) {
02055 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
02056 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
02057 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
02058 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
02059 return;
02060 }
02061
02062 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
02063 C = s->current_picture.f.motion_val[0][xy - 2];
02064 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
02065 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
02066 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
02067
02068 if (!s->mb_x) C[0] = C[1] = 0;
02069 if (!s->first_slice_line) {
02070 if (s->mb_width == 1) {
02071 px = A[0];
02072 py = A[1];
02073 } else {
02074 px = mid_pred(A[0], B[0], C[0]);
02075 py = mid_pred(A[1], B[1], C[1]);
02076 }
02077 } else if (s->mb_x) {
02078 px = C[0];
02079 py = C[1];
02080 } else {
02081 px = py = 0;
02082 }
02083
02084 {
02085 int qx, qy, X, Y;
02086 if (v->profile < PROFILE_ADVANCED) {
02087 qx = (s->mb_x << 5);
02088 qy = (s->mb_y << 5);
02089 X = (s->mb_width << 5) - 4;
02090 Y = (s->mb_height << 5) - 4;
02091 if (qx + px < -28) px = -28 - qx;
02092 if (qy + py < -28) py = -28 - qy;
02093 if (qx + px > X) px = X - qx;
02094 if (qy + py > Y) py = Y - qy;
02095 } else {
02096 qx = (s->mb_x << 6);
02097 qy = (s->mb_y << 6);
02098 X = (s->mb_width << 6) - 4;
02099 Y = (s->mb_height << 6) - 4;
02100 if (qx + px < -60) px = -60 - qx;
02101 if (qy + py < -60) py = -60 - qy;
02102 if (qx + px > X) px = X - qx;
02103 if (qy + py > Y) py = Y - qy;
02104 }
02105 }
02106
02107 if (0 && !s->first_slice_line && s->mb_x) {
02108 if (is_intra[xy - wrap])
02109 sum = FFABS(px) + FFABS(py);
02110 else
02111 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
02112 if (sum > 32) {
02113 if (get_bits1(&s->gb)) {
02114 px = A[0];
02115 py = A[1];
02116 } else {
02117 px = C[0];
02118 py = C[1];
02119 }
02120 } else {
02121 if (is_intra[xy - 2])
02122 sum = FFABS(px) + FFABS(py);
02123 else
02124 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
02125 if (sum > 32) {
02126 if (get_bits1(&s->gb)) {
02127 px = A[0];
02128 py = A[1];
02129 } else {
02130 px = C[0];
02131 py = C[1];
02132 }
02133 }
02134 }
02135 }
02136
02137 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
02138 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
02139 }
02140 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
02141 C = s->current_picture.f.motion_val[1][xy - 2];
02142 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
02143 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
02144 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
02145
02146 if (!s->mb_x)
02147 C[0] = C[1] = 0;
02148 if (!s->first_slice_line) {
02149 if (s->mb_width == 1) {
02150 px = A[0];
02151 py = A[1];
02152 } else {
02153 px = mid_pred(A[0], B[0], C[0]);
02154 py = mid_pred(A[1], B[1], C[1]);
02155 }
02156 } else if (s->mb_x) {
02157 px = C[0];
02158 py = C[1];
02159 } else {
02160 px = py = 0;
02161 }
02162
02163 {
02164 int qx, qy, X, Y;
02165 if (v->profile < PROFILE_ADVANCED) {
02166 qx = (s->mb_x << 5);
02167 qy = (s->mb_y << 5);
02168 X = (s->mb_width << 5) - 4;
02169 Y = (s->mb_height << 5) - 4;
02170 if (qx + px < -28) px = -28 - qx;
02171 if (qy + py < -28) py = -28 - qy;
02172 if (qx + px > X) px = X - qx;
02173 if (qy + py > Y) py = Y - qy;
02174 } else {
02175 qx = (s->mb_x << 6);
02176 qy = (s->mb_y << 6);
02177 X = (s->mb_width << 6) - 4;
02178 Y = (s->mb_height << 6) - 4;
02179 if (qx + px < -60) px = -60 - qx;
02180 if (qy + py < -60) py = -60 - qy;
02181 if (qx + px > X) px = X - qx;
02182 if (qy + py > Y) py = Y - qy;
02183 }
02184 }
02185
02186 if (0 && !s->first_slice_line && s->mb_x) {
02187 if (is_intra[xy - wrap])
02188 sum = FFABS(px) + FFABS(py);
02189 else
02190 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
02191 if (sum > 32) {
02192 if (get_bits1(&s->gb)) {
02193 px = A[0];
02194 py = A[1];
02195 } else {
02196 px = C[0];
02197 py = C[1];
02198 }
02199 } else {
02200 if (is_intra[xy - 2])
02201 sum = FFABS(px) + FFABS(py);
02202 else
02203 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
02204 if (sum > 32) {
02205 if (get_bits1(&s->gb)) {
02206 px = A[0];
02207 py = A[1];
02208 } else {
02209 px = C[0];
02210 py = C[1];
02211 }
02212 }
02213 }
02214 }
02215
02216
02217 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
02218 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
02219 }
02220 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
02221 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
02222 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
02223 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
02224 }
02225
02226 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
02227 {
02228 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
02229 MpegEncContext *s = &v->s;
02230 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
02231
02232 if (v->bmvtype == BMV_TYPE_DIRECT) {
02233 int total_opp, k, f;
02234 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
02235 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
02236 v->bfraction, 0, s->quarter_sample);
02237 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
02238 v->bfraction, 0, s->quarter_sample);
02239 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
02240 v->bfraction, 1, s->quarter_sample);
02241 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
02242 v->bfraction, 1, s->quarter_sample);
02243
02244 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
02245 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
02246 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
02247 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
02248 f = (total_opp > 2) ? 1 : 0;
02249 } else {
02250 s->mv[0][0][0] = s->mv[0][0][1] = 0;
02251 s->mv[1][0][0] = s->mv[1][0][1] = 0;
02252 f = 0;
02253 }
02254 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
02255 for (k = 0; k < 4; k++) {
02256 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
02257 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
02258 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
02259 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
02260 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
02261 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
02262 }
02263 return;
02264 }
02265 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
02266 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
02267 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
02268 return;
02269 }
02270 if (dir) {
02271 vc1_pred_mv(v, n, dmv_x[1], dmv_y[1], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[1], 1);
02272 if (n == 3 || mv1) {
02273 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
02274 }
02275 } else {
02276 vc1_pred_mv(v, n, dmv_x[0], dmv_y[0], mv1, v->range_x, v->range_y, v->mb_type[0], pred_flag[0], 0);
02277 if (n == 3 || mv1) {
02278 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
02279 }
02280 }
02281 }
02282
02292 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
02293 int16_t **dc_val_ptr, int *dir_ptr)
02294 {
02295 int a, b, c, wrap, pred, scale;
02296 int16_t *dc_val;
02297 static const uint16_t dcpred[32] = {
02298 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
02299 114, 102, 93, 85, 79, 73, 68, 64,
02300 60, 57, 54, 51, 49, 47, 45, 43,
02301 41, 39, 38, 37, 35, 34, 33
02302 };
02303
02304
02305 if (n < 4) scale = s->y_dc_scale;
02306 else scale = s->c_dc_scale;
02307
02308 wrap = s->block_wrap[n];
02309 dc_val = s->dc_val[0] + s->block_index[n];
02310
02311
02312
02313
02314 c = dc_val[ - 1];
02315 b = dc_val[ - 1 - wrap];
02316 a = dc_val[ - wrap];
02317
02318 if (pq < 9 || !overlap) {
02319
02320 if (s->first_slice_line && (n != 2 && n != 3))
02321 b = a = dcpred[scale];
02322 if (s->mb_x == 0 && (n != 1 && n != 3))
02323 b = c = dcpred[scale];
02324 } else {
02325
02326 if (s->first_slice_line && (n != 2 && n != 3))
02327 b = a = 0;
02328 if (s->mb_x == 0 && (n != 1 && n != 3))
02329 b = c = 0;
02330 }
02331
02332 if (abs(a - b) <= abs(b - c)) {
02333 pred = c;
02334 *dir_ptr = 1;
02335 } else {
02336 pred = a;
02337 *dir_ptr = 0;
02338 }
02339
02340
02341 *dc_val_ptr = &dc_val[0];
02342 return pred;
02343 }
02344
02345
02357 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
02358 int a_avail, int c_avail,
02359 int16_t **dc_val_ptr, int *dir_ptr)
02360 {
02361 int a, b, c, wrap, pred;
02362 int16_t *dc_val;
02363 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
02364 int q1, q2 = 0;
02365 int dqscale_index;
02366
02367 wrap = s->block_wrap[n];
02368 dc_val = s->dc_val[0] + s->block_index[n];
02369
02370
02371
02372
02373 c = dc_val[ - 1];
02374 b = dc_val[ - 1 - wrap];
02375 a = dc_val[ - wrap];
02376
02377 q1 = s->current_picture.f.qscale_table[mb_pos];
02378 dqscale_index = s->y_dc_scale_table[q1] - 1;
02379 if (dqscale_index < 0)
02380 return 0;
02381 if (c_avail && (n != 1 && n != 3)) {
02382 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
02383 if (q2 && q2 != q1)
02384 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
02385 }
02386 if (a_avail && (n != 2 && n != 3)) {
02387 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
02388 if (q2 && q2 != q1)
02389 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
02390 }
02391 if (a_avail && c_avail && (n != 3)) {
02392 int off = mb_pos;
02393 if (n != 1)
02394 off--;
02395 if (n != 2)
02396 off -= s->mb_stride;
02397 q2 = s->current_picture.f.qscale_table[off];
02398 if (q2 && q2 != q1)
02399 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
02400 }
02401
02402 if (a_avail && c_avail) {
02403 if (abs(a - b) <= abs(b - c)) {
02404 pred = c;
02405 *dir_ptr = 1;
02406 } else {
02407 pred = a;
02408 *dir_ptr = 0;
02409 }
02410 } else if (a_avail) {
02411 pred = a;
02412 *dir_ptr = 0;
02413 } else if (c_avail) {
02414 pred = c;
02415 *dir_ptr = 1;
02416 } else {
02417 pred = 0;
02418 *dir_ptr = 1;
02419 }
02420
02421
02422 *dc_val_ptr = &dc_val[0];
02423 return pred;
02424 }
02425
02427
02434 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
02435 uint8_t **coded_block_ptr)
02436 {
02437 int xy, wrap, pred, a, b, c;
02438
02439 xy = s->block_index[n];
02440 wrap = s->b8_stride;
02441
02442
02443
02444
02445 a = s->coded_block[xy - 1 ];
02446 b = s->coded_block[xy - 1 - wrap];
02447 c = s->coded_block[xy - wrap];
02448
02449 if (b == c) {
02450 pred = a;
02451 } else {
02452 pred = c;
02453 }
02454
02455
02456 *coded_block_ptr = &s->coded_block[xy];
02457
02458 return pred;
02459 }
02460
02470 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
02471 int *value, int codingset)
02472 {
02473 GetBitContext *gb = &v->s.gb;
02474 int index, escape, run = 0, level = 0, lst = 0;
02475
02476 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
02477 if (index != ff_vc1_ac_sizes[codingset] - 1) {
02478 run = vc1_index_decode_table[codingset][index][0];
02479 level = vc1_index_decode_table[codingset][index][1];
02480 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
02481 if (get_bits1(gb))
02482 level = -level;
02483 } else {
02484 escape = decode210(gb);
02485 if (escape != 2) {
02486 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
02487 run = vc1_index_decode_table[codingset][index][0];
02488 level = vc1_index_decode_table[codingset][index][1];
02489 lst = index >= vc1_last_decode_table[codingset];
02490 if (escape == 0) {
02491 if (lst)
02492 level += vc1_last_delta_level_table[codingset][run];
02493 else
02494 level += vc1_delta_level_table[codingset][run];
02495 } else {
02496 if (lst)
02497 run += vc1_last_delta_run_table[codingset][level] + 1;
02498 else
02499 run += vc1_delta_run_table[codingset][level] + 1;
02500 }
02501 if (get_bits1(gb))
02502 level = -level;
02503 } else {
02504 int sign;
02505 lst = get_bits1(gb);
02506 if (v->s.esc3_level_length == 0) {
02507 if (v->pq < 8 || v->dquantfrm) {
02508 v->s.esc3_level_length = get_bits(gb, 3);
02509 if (!v->s.esc3_level_length)
02510 v->s.esc3_level_length = get_bits(gb, 2) + 8;
02511 } else {
02512 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
02513 }
02514 v->s.esc3_run_length = 3 + get_bits(gb, 2);
02515 }
02516 run = get_bits(gb, v->s.esc3_run_length);
02517 sign = get_bits1(gb);
02518 level = get_bits(gb, v->s.esc3_level_length);
02519 if (sign)
02520 level = -level;
02521 }
02522 }
02523
02524 *last = lst;
02525 *skip = run;
02526 *value = level;
02527 }
02528
02536 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
02537 int coded, int codingset)
02538 {
02539 GetBitContext *gb = &v->s.gb;
02540 MpegEncContext *s = &v->s;
02541 int dc_pred_dir = 0;
02542 int i;
02543 int16_t *dc_val;
02544 int16_t *ac_val, *ac_val2;
02545 int dcdiff;
02546
02547
02548 if (n < 4) {
02549 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02550 } else {
02551 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02552 }
02553 if (dcdiff < 0) {
02554 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
02555 return -1;
02556 }
02557 if (dcdiff) {
02558 if (dcdiff == 119 ) {
02559
02560 if (v->pq == 1) dcdiff = get_bits(gb, 10);
02561 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
02562 else dcdiff = get_bits(gb, 8);
02563 } else {
02564 if (v->pq == 1)
02565 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
02566 else if (v->pq == 2)
02567 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
02568 }
02569 if (get_bits1(gb))
02570 dcdiff = -dcdiff;
02571 }
02572
02573
02574 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
02575 *dc_val = dcdiff;
02576
02577
02578 if (n < 4) {
02579 block[0] = dcdiff * s->y_dc_scale;
02580 } else {
02581 block[0] = dcdiff * s->c_dc_scale;
02582 }
02583
02584 if (!coded) {
02585 goto not_coded;
02586 }
02587
02588
02589 i = 1;
02590
02591 {
02592 int last = 0, skip, value;
02593 const uint8_t *zz_table;
02594 int scale;
02595 int k;
02596
02597 scale = v->pq * 2 + v->halfpq;
02598
02599 if (v->s.ac_pred) {
02600 if (!dc_pred_dir)
02601 zz_table = v->zz_8x8[2];
02602 else
02603 zz_table = v->zz_8x8[3];
02604 } else
02605 zz_table = v->zz_8x8[1];
02606
02607 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
02608 ac_val2 = ac_val;
02609 if (dc_pred_dir)
02610 ac_val -= 16;
02611 else
02612 ac_val -= 16 * s->block_wrap[n];
02613
02614 while (!last) {
02615 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
02616 i += skip;
02617 if (i > 63)
02618 break;
02619 block[zz_table[i++]] = value;
02620 }
02621
02622
02623 if (s->ac_pred) {
02624 if (dc_pred_dir) {
02625 for (k = 1; k < 8; k++)
02626 block[k << v->left_blk_sh] += ac_val[k];
02627 } else {
02628 for (k = 1; k < 8; k++)
02629 block[k << v->top_blk_sh] += ac_val[k + 8];
02630 }
02631 }
02632
02633 for (k = 1; k < 8; k++) {
02634 ac_val2[k] = block[k << v->left_blk_sh];
02635 ac_val2[k + 8] = block[k << v->top_blk_sh];
02636 }
02637
02638
02639 for (k = 1; k < 64; k++)
02640 if (block[k]) {
02641 block[k] *= scale;
02642 if (!v->pquantizer)
02643 block[k] += (block[k] < 0) ? -v->pq : v->pq;
02644 }
02645
02646 if (s->ac_pred) i = 63;
02647 }
02648
02649 not_coded:
02650 if (!coded) {
02651 int k, scale;
02652 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
02653 ac_val2 = ac_val;
02654
02655 i = 0;
02656 scale = v->pq * 2 + v->halfpq;
02657 memset(ac_val2, 0, 16 * 2);
02658 if (dc_pred_dir) {
02659 ac_val -= 16;
02660 if (s->ac_pred)
02661 memcpy(ac_val2, ac_val, 8 * 2);
02662 } else {
02663 ac_val -= 16 * s->block_wrap[n];
02664 if (s->ac_pred)
02665 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
02666 }
02667
02668
02669 if (s->ac_pred) {
02670 if (dc_pred_dir) {
02671 for (k = 1; k < 8; k++) {
02672 block[k << v->left_blk_sh] = ac_val[k] * scale;
02673 if (!v->pquantizer && block[k << v->left_blk_sh])
02674 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
02675 }
02676 } else {
02677 for (k = 1; k < 8; k++) {
02678 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
02679 if (!v->pquantizer && block[k << v->top_blk_sh])
02680 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
02681 }
02682 }
02683 i = 63;
02684 }
02685 }
02686 s->block_last_index[n] = i;
02687
02688 return 0;
02689 }
02690
02699 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
02700 int coded, int codingset, int mquant)
02701 {
02702 GetBitContext *gb = &v->s.gb;
02703 MpegEncContext *s = &v->s;
02704 int dc_pred_dir = 0;
02705 int i;
02706 int16_t *dc_val;
02707 int16_t *ac_val, *ac_val2;
02708 int dcdiff;
02709 int a_avail = v->a_avail, c_avail = v->c_avail;
02710 int use_pred = s->ac_pred;
02711 int scale;
02712 int q1, q2 = 0;
02713 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
02714
02715
02716 if (n < 4) {
02717 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02718 } else {
02719 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02720 }
02721 if (dcdiff < 0) {
02722 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
02723 return -1;
02724 }
02725 if (dcdiff) {
02726 if (dcdiff == 119 ) {
02727
02728 if (mquant == 1) dcdiff = get_bits(gb, 10);
02729 else if (mquant == 2) dcdiff = get_bits(gb, 9);
02730 else dcdiff = get_bits(gb, 8);
02731 } else {
02732 if (mquant == 1)
02733 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
02734 else if (mquant == 2)
02735 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
02736 }
02737 if (get_bits1(gb))
02738 dcdiff = -dcdiff;
02739 }
02740
02741
02742 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
02743 *dc_val = dcdiff;
02744
02745
02746 if (n < 4) {
02747 block[0] = dcdiff * s->y_dc_scale;
02748 } else {
02749 block[0] = dcdiff * s->c_dc_scale;
02750 }
02751
02752
02753 i = 1;
02754
02755
02756 if (!a_avail && !c_avail)
02757 use_pred = 0;
02758 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
02759 ac_val2 = ac_val;
02760
02761 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
02762
02763 if (dc_pred_dir)
02764 ac_val -= 16;
02765 else
02766 ac_val -= 16 * s->block_wrap[n];
02767
02768 q1 = s->current_picture.f.qscale_table[mb_pos];
02769 if ( dc_pred_dir && c_avail && mb_pos)
02770 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
02771 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
02772 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
02773 if ( dc_pred_dir && n == 1)
02774 q2 = q1;
02775 if (!dc_pred_dir && n == 2)
02776 q2 = q1;
02777 if (n == 3)
02778 q2 = q1;
02779
02780 if (coded) {
02781 int last = 0, skip, value;
02782 const uint8_t *zz_table;
02783 int k;
02784
02785 if (v->s.ac_pred) {
02786 if (!use_pred && v->fcm == ILACE_FRAME) {
02787 zz_table = v->zzi_8x8;
02788 } else {
02789 if (!dc_pred_dir)
02790 zz_table = v->zz_8x8[2];
02791 else
02792 zz_table = v->zz_8x8[3];
02793 }
02794 } else {
02795 if (v->fcm != ILACE_FRAME)
02796 zz_table = v->zz_8x8[1];
02797 else
02798 zz_table = v->zzi_8x8;
02799 }
02800
02801 while (!last) {
02802 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
02803 i += skip;
02804 if (i > 63)
02805 break;
02806 block[zz_table[i++]] = value;
02807 }
02808
02809
02810 if (use_pred) {
02811
02812 if (q2 && q1 != q2) {
02813 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
02814 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
02815
02816 if (q1 < 1)
02817 return AVERROR_INVALIDDATA;
02818 if (dc_pred_dir) {
02819 for (k = 1; k < 8; k++)
02820 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
02821 } else {
02822 for (k = 1; k < 8; k++)
02823 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
02824 }
02825 } else {
02826 if (dc_pred_dir) {
02827 for (k = 1; k < 8; k++)
02828 block[k << v->left_blk_sh] += ac_val[k];
02829 } else {
02830 for (k = 1; k < 8; k++)
02831 block[k << v->top_blk_sh] += ac_val[k + 8];
02832 }
02833 }
02834 }
02835
02836 for (k = 1; k < 8; k++) {
02837 ac_val2[k ] = block[k << v->left_blk_sh];
02838 ac_val2[k + 8] = block[k << v->top_blk_sh];
02839 }
02840
02841
02842 for (k = 1; k < 64; k++)
02843 if (block[k]) {
02844 block[k] *= scale;
02845 if (!v->pquantizer)
02846 block[k] += (block[k] < 0) ? -mquant : mquant;
02847 }
02848
02849 if (use_pred) i = 63;
02850 } else {
02851 int k;
02852
02853 memset(ac_val2, 0, 16 * 2);
02854 if (dc_pred_dir) {
02855 if (use_pred) {
02856 memcpy(ac_val2, ac_val, 8 * 2);
02857 if (q2 && q1 != q2) {
02858 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
02859 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
02860 if (q1 < 1)
02861 return AVERROR_INVALIDDATA;
02862 for (k = 1; k < 8; k++)
02863 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
02864 }
02865 }
02866 } else {
02867 if (use_pred) {
02868 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
02869 if (q2 && q1 != q2) {
02870 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
02871 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
02872 if (q1 < 1)
02873 return AVERROR_INVALIDDATA;
02874 for (k = 1; k < 8; k++)
02875 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
02876 }
02877 }
02878 }
02879
02880
02881 if (use_pred) {
02882 if (dc_pred_dir) {
02883 for (k = 1; k < 8; k++) {
02884 block[k << v->left_blk_sh] = ac_val2[k] * scale;
02885 if (!v->pquantizer && block[k << v->left_blk_sh])
02886 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
02887 }
02888 } else {
02889 for (k = 1; k < 8; k++) {
02890 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
02891 if (!v->pquantizer && block[k << v->top_blk_sh])
02892 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
02893 }
02894 }
02895 i = 63;
02896 }
02897 }
02898 s->block_last_index[n] = i;
02899
02900 return 0;
02901 }
02902
02911 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
02912 int coded, int mquant, int codingset)
02913 {
02914 GetBitContext *gb = &v->s.gb;
02915 MpegEncContext *s = &v->s;
02916 int dc_pred_dir = 0;
02917 int i;
02918 int16_t *dc_val;
02919 int16_t *ac_val, *ac_val2;
02920 int dcdiff;
02921 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
02922 int a_avail = v->a_avail, c_avail = v->c_avail;
02923 int use_pred = s->ac_pred;
02924 int scale;
02925 int q1, q2 = 0;
02926
02927 s->dsp.clear_block(block);
02928
02929
02930 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
02931
02932
02933 s->y_dc_scale = s->y_dc_scale_table[mquant];
02934 s->c_dc_scale = s->c_dc_scale_table[mquant];
02935
02936
02937 if (n < 4) {
02938 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02939 } else {
02940 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
02941 }
02942 if (dcdiff < 0) {
02943 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
02944 return -1;
02945 }
02946 if (dcdiff) {
02947 if (dcdiff == 119 ) {
02948
02949 if (mquant == 1) dcdiff = get_bits(gb, 10);
02950 else if (mquant == 2) dcdiff = get_bits(gb, 9);
02951 else dcdiff = get_bits(gb, 8);
02952 } else {
02953 if (mquant == 1)
02954 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
02955 else if (mquant == 2)
02956 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
02957 }
02958 if (get_bits1(gb))
02959 dcdiff = -dcdiff;
02960 }
02961
02962
02963 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
02964 *dc_val = dcdiff;
02965
02966
02967
02968 if (n < 4) {
02969 block[0] = dcdiff * s->y_dc_scale;
02970 } else {
02971 block[0] = dcdiff * s->c_dc_scale;
02972 }
02973
02974
02975 i = 1;
02976
02977
02978 if (!a_avail) dc_pred_dir = 1;
02979 if (!c_avail) dc_pred_dir = 0;
02980 if (!a_avail && !c_avail) use_pred = 0;
02981 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
02982 ac_val2 = ac_val;
02983
02984 scale = mquant * 2 + v->halfpq;
02985
02986 if (dc_pred_dir)
02987 ac_val -= 16;
02988 else
02989 ac_val -= 16 * s->block_wrap[n];
02990
02991 q1 = s->current_picture.f.qscale_table[mb_pos];
02992 if (dc_pred_dir && c_avail && mb_pos)
02993 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
02994 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
02995 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
02996 if ( dc_pred_dir && n == 1)
02997 q2 = q1;
02998 if (!dc_pred_dir && n == 2)
02999 q2 = q1;
03000 if (n == 3) q2 = q1;
03001
03002 if (coded) {
03003 int last = 0, skip, value;
03004 int k;
03005
03006 while (!last) {
03007 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
03008 i += skip;
03009 if (i > 63)
03010 break;
03011 if (v->fcm == PROGRESSIVE)
03012 block[v->zz_8x8[0][i++]] = value;
03013 else {
03014 if (use_pred && (v->fcm == ILACE_FRAME)) {
03015 if (!dc_pred_dir)
03016 block[v->zz_8x8[2][i++]] = value;
03017 else
03018 block[v->zz_8x8[3][i++]] = value;
03019 } else {
03020 block[v->zzi_8x8[i++]] = value;
03021 }
03022 }
03023 }
03024
03025
03026 if (use_pred) {
03027
03028 if (q2 && q1 != q2) {
03029 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
03030 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
03031
03032 if (q1 < 1)
03033 return AVERROR_INVALIDDATA;
03034 if (dc_pred_dir) {
03035 for (k = 1; k < 8; k++)
03036 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
03037 } else {
03038 for (k = 1; k < 8; k++)
03039 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
03040 }
03041 } else {
03042 if (dc_pred_dir) {
03043 for (k = 1; k < 8; k++)
03044 block[k << v->left_blk_sh] += ac_val[k];
03045 } else {
03046 for (k = 1; k < 8; k++)
03047 block[k << v->top_blk_sh] += ac_val[k + 8];
03048 }
03049 }
03050 }
03051
03052 for (k = 1; k < 8; k++) {
03053 ac_val2[k ] = block[k << v->left_blk_sh];
03054 ac_val2[k + 8] = block[k << v->top_blk_sh];
03055 }
03056
03057
03058 for (k = 1; k < 64; k++)
03059 if (block[k]) {
03060 block[k] *= scale;
03061 if (!v->pquantizer)
03062 block[k] += (block[k] < 0) ? -mquant : mquant;
03063 }
03064
03065 if (use_pred) i = 63;
03066 } else {
03067 int k;
03068
03069 memset(ac_val2, 0, 16 * 2);
03070 if (dc_pred_dir) {
03071 if (use_pred) {
03072 memcpy(ac_val2, ac_val, 8 * 2);
03073 if (q2 && q1 != q2) {
03074 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
03075 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
03076 if (q1 < 1)
03077 return AVERROR_INVALIDDATA;
03078 for (k = 1; k < 8; k++)
03079 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
03080 }
03081 }
03082 } else {
03083 if (use_pred) {
03084 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
03085 if (q2 && q1 != q2) {
03086 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
03087 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
03088 if (q1 < 1)
03089 return AVERROR_INVALIDDATA;
03090 for (k = 1; k < 8; k++)
03091 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
03092 }
03093 }
03094 }
03095
03096
03097 if (use_pred) {
03098 if (dc_pred_dir) {
03099 for (k = 1; k < 8; k++) {
03100 block[k << v->left_blk_sh] = ac_val2[k] * scale;
03101 if (!v->pquantizer && block[k << v->left_blk_sh])
03102 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
03103 }
03104 } else {
03105 for (k = 1; k < 8; k++) {
03106 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
03107 if (!v->pquantizer && block[k << v->top_blk_sh])
03108 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
03109 }
03110 }
03111 i = 63;
03112 }
03113 }
03114 s->block_last_index[n] = i;
03115
03116 return 0;
03117 }
03118
03121 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
03122 int mquant, int ttmb, int first_block,
03123 uint8_t *dst, int linesize, int skip_block,
03124 int *ttmb_out)
03125 {
03126 MpegEncContext *s = &v->s;
03127 GetBitContext *gb = &s->gb;
03128 int i, j;
03129 int subblkpat = 0;
03130 int scale, off, idx, last, skip, value;
03131 int ttblk = ttmb & 7;
03132 int pat = 0;
03133
03134 s->dsp.clear_block(block);
03135
03136 if (ttmb == -1) {
03137 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)];
03138 }
03139 if (ttblk == TT_4X4) {
03140 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
03141 }
03142 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
03143 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
03144 || (!v->res_rtm_flag && !first_block))) {
03145 subblkpat = decode012(gb);
03146 if (subblkpat)
03147 subblkpat ^= 3;
03148 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
03149 ttblk = TT_8X4;
03150 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
03151 ttblk = TT_4X8;
03152 }
03153 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
03154
03155
03156 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
03157 subblkpat = 2 - (ttblk == TT_8X4_TOP);
03158 ttblk = TT_8X4;
03159 }
03160 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
03161 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
03162 ttblk = TT_4X8;
03163 }
03164 switch (ttblk) {
03165 case TT_8X8:
03166 pat = 0xF;
03167 i = 0;
03168 last = 0;
03169 while (!last) {
03170 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
03171 i += skip;
03172 if (i > 63)
03173 break;
03174 if (!v->fcm)
03175 idx = v->zz_8x8[0][i++];
03176 else
03177 idx = v->zzi_8x8[i++];
03178 block[idx] = value * scale;
03179 if (!v->pquantizer)
03180 block[idx] += (block[idx] < 0) ? -mquant : mquant;
03181 }
03182 if (!skip_block) {
03183 if (i == 1)
03184 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
03185 else {
03186 v->vc1dsp.vc1_inv_trans_8x8(block);
03187 s->dsp.add_pixels_clamped(block, dst, linesize);
03188 }
03189 }
03190 break;
03191 case TT_4X4:
03192 pat = ~subblkpat & 0xF;
03193 for (j = 0; j < 4; j++) {
03194 last = subblkpat & (1 << (3 - j));
03195 i = 0;
03196 off = (j & 1) * 4 + (j & 2) * 16;
03197 while (!last) {
03198 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
03199 i += skip;
03200 if (i > 15)
03201 break;
03202 if (!v->fcm)
03203 idx = ff_vc1_simple_progressive_4x4_zz[i++];
03204 else
03205 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
03206 block[idx + off] = value * scale;
03207 if (!v->pquantizer)
03208 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
03209 }
03210 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
03211 if (i == 1)
03212 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
03213 else
03214 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
03215 }
03216 }
03217 break;
03218 case TT_8X4:
03219 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
03220 for (j = 0; j < 2; j++) {
03221 last = subblkpat & (1 << (1 - j));
03222 i = 0;
03223 off = j * 32;
03224 while (!last) {
03225 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
03226 i += skip;
03227 if (i > 31)
03228 break;
03229 if (!v->fcm)
03230 idx = v->zz_8x4[i++] + off;
03231 else
03232 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
03233 block[idx] = value * scale;
03234 if (!v->pquantizer)
03235 block[idx] += (block[idx] < 0) ? -mquant : mquant;
03236 }
03237 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
03238 if (i == 1)
03239 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
03240 else
03241 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
03242 }
03243 }
03244 break;
03245 case TT_4X8:
03246 pat = ~(subblkpat * 5) & 0xF;
03247 for (j = 0; j < 2; j++) {
03248 last = subblkpat & (1 << (1 - j));
03249 i = 0;
03250 off = j * 4;
03251 while (!last) {
03252 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
03253 i += skip;
03254 if (i > 31)
03255 break;
03256 if (!v->fcm)
03257 idx = v->zz_4x8[i++] + off;
03258 else
03259 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
03260 block[idx] = value * scale;
03261 if (!v->pquantizer)
03262 block[idx] += (block[idx] < 0) ? -mquant : mquant;
03263 }
03264 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
03265 if (i == 1)
03266 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
03267 else
03268 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
03269 }
03270 }
03271 break;
03272 }
03273 if (ttmb_out)
03274 *ttmb_out |= ttblk << (n * 4);
03275 return pat;
03276 }
03277
03279
03280 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
03281 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
03282
03283 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
03284 {
03285 MpegEncContext *s = &v->s;
03286 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
03287 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
03288 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
03289 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
03290 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
03291 uint8_t *dst;
03292
03293 if (block_num > 3) {
03294 dst = s->dest[block_num - 3];
03295 } else {
03296 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
03297 }
03298 if (s->mb_y != s->end_mb_y || block_num < 2) {
03299 int16_t (*mv)[2];
03300 int mv_stride;
03301
03302 if (block_num > 3) {
03303 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
03304 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
03305 mv = &v->luma_mv[s->mb_x - s->mb_stride];
03306 mv_stride = s->mb_stride;
03307 } else {
03308 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
03309 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
03310 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
03311 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
03312 mv_stride = s->b8_stride;
03313 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
03314 }
03315
03316 if (bottom_is_intra & 1 || block_is_intra & 1 ||
03317 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
03318 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
03319 } else {
03320 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
03321 if (idx == 3) {
03322 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
03323 } else if (idx) {
03324 if (idx == 1)
03325 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
03326 else
03327 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
03328 }
03329 }
03330 }
03331
03332 dst -= 4 * linesize;
03333 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
03334 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
03335 idx = (block_cbp | (block_cbp >> 2)) & 3;
03336 if (idx == 3) {
03337 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
03338 } else if (idx) {
03339 if (idx == 1)
03340 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
03341 else
03342 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
03343 }
03344 }
03345 }
03346
03347 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
03348 {
03349 MpegEncContext *s = &v->s;
03350 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
03351 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
03352 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
03353 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
03354 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
03355 uint8_t *dst;
03356
03357 if (block_num > 3) {
03358 dst = s->dest[block_num - 3] - 8 * linesize;
03359 } else {
03360 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
03361 }
03362
03363 if (s->mb_x != s->mb_width || !(block_num & 5)) {
03364 int16_t (*mv)[2];
03365
03366 if (block_num > 3) {
03367 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
03368 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
03369 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
03370 } else {
03371 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
03372 : (mb_cbp >> ((block_num + 1) * 4));
03373 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
03374 : (mb_is_intra >> ((block_num + 1) * 4));
03375 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
03376 }
03377 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
03378 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
03379 } else {
03380 idx = ((right_cbp >> 1) | block_cbp) & 5;
03381 if (idx == 5) {
03382 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
03383 } else if (idx) {
03384 if (idx == 1)
03385 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
03386 else
03387 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
03388 }
03389 }
03390 }
03391
03392 dst -= 4;
03393 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
03394 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
03395 idx = (block_cbp | (block_cbp >> 1)) & 5;
03396 if (idx == 5) {
03397 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
03398 } else if (idx) {
03399 if (idx == 1)
03400 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
03401 else
03402 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
03403 }
03404 }
03405 }
03406
03407 static void vc1_apply_p_loop_filter(VC1Context *v)
03408 {
03409 MpegEncContext *s = &v->s;
03410 int i;
03411
03412 for (i = 0; i < 6; i++) {
03413 vc1_apply_p_v_loop_filter(v, i);
03414 }
03415
03416
03417
03418 if (s->mb_x) {
03419 for (i = 0; i < 6; i++) {
03420 vc1_apply_p_h_loop_filter(v, i);
03421 }
03422 if (s->mb_x == s->mb_width - 1) {
03423 s->mb_x++;
03424 ff_update_block_index(s);
03425 for (i = 0; i < 6; i++) {
03426 vc1_apply_p_h_loop_filter(v, i);
03427 }
03428 }
03429 }
03430 }
03431
03434 static int vc1_decode_p_mb(VC1Context *v)
03435 {
03436 MpegEncContext *s = &v->s;
03437 GetBitContext *gb = &s->gb;
03438 int i, j;
03439 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
03440 int cbp;
03441 int mqdiff, mquant;
03442 int ttmb = v->ttfrm;
03443
03444 int mb_has_coeffs = 1;
03445 int dmv_x, dmv_y;
03446 int index, index1;
03447 int val, sign;
03448 int first_block = 1;
03449 int dst_idx, off;
03450 int skipped, fourmv;
03451 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
03452
03453 mquant = v->pq;
03454
03455 if (v->mv_type_is_raw)
03456 fourmv = get_bits1(gb);
03457 else
03458 fourmv = v->mv_type_mb_plane[mb_pos];
03459 if (v->skip_is_raw)
03460 skipped = get_bits1(gb);
03461 else
03462 skipped = v->s.mbskip_table[mb_pos];
03463
03464 if (!fourmv) {
03465 if (!skipped) {
03466 GET_MVDATA(dmv_x, dmv_y);
03467
03468 if (s->mb_intra) {
03469 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
03470 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
03471 }
03472 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
03473 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
03474
03475
03476 if (s->mb_intra && !mb_has_coeffs) {
03477 GET_MQUANT();
03478 s->ac_pred = get_bits1(gb);
03479 cbp = 0;
03480 } else if (mb_has_coeffs) {
03481 if (s->mb_intra)
03482 s->ac_pred = get_bits1(gb);
03483 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
03484 GET_MQUANT();
03485 } else {
03486 mquant = v->pq;
03487 cbp = 0;
03488 }
03489 s->current_picture.f.qscale_table[mb_pos] = mquant;
03490
03491 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
03492 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
03493 VC1_TTMB_VLC_BITS, 2);
03494 if (!s->mb_intra) vc1_mc_1mv(v, 0);
03495 dst_idx = 0;
03496 for (i = 0; i < 6; i++) {
03497 s->dc_val[0][s->block_index[i]] = 0;
03498 dst_idx += i >> 2;
03499 val = ((cbp >> (5 - i)) & 1);
03500 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
03501 v->mb_type[0][s->block_index[i]] = s->mb_intra;
03502 if (s->mb_intra) {
03503
03504 v->a_avail = v->c_avail = 0;
03505 if (i == 2 || i == 3 || !s->first_slice_line)
03506 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
03507 if (i == 1 || i == 3 || s->mb_x)
03508 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
03509
03510 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
03511 (i & 4) ? v->codingset2 : v->codingset);
03512 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
03513 continue;
03514 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
03515 if (v->rangeredfrm)
03516 for (j = 0; j < 64; j++)
03517 s->block[i][j] <<= 1;
03518 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
03519 if (v->pq >= 9 && v->overlap) {
03520 if (v->c_avail)
03521 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
03522 if (v->a_avail)
03523 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
03524 }
03525 block_cbp |= 0xF << (i << 2);
03526 block_intra |= 1 << i;
03527 } else if (val) {
03528 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
03529 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
03530 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
03531 block_cbp |= pat << (i << 2);
03532 if (!v->ttmbf && ttmb < 8)
03533 ttmb = -1;
03534 first_block = 0;
03535 }
03536 }
03537 } else {
03538 s->mb_intra = 0;
03539 for (i = 0; i < 6; i++) {
03540 v->mb_type[0][s->block_index[i]] = 0;
03541 s->dc_val[0][s->block_index[i]] = 0;
03542 }
03543 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
03544 s->current_picture.f.qscale_table[mb_pos] = 0;
03545 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
03546 vc1_mc_1mv(v, 0);
03547 }
03548 } else {
03549 if (!skipped ) {
03550 int intra_count = 0, coded_inter = 0;
03551 int is_intra[6], is_coded[6];
03552
03553 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
03554 for (i = 0; i < 6; i++) {
03555 val = ((cbp >> (5 - i)) & 1);
03556 s->dc_val[0][s->block_index[i]] = 0;
03557 s->mb_intra = 0;
03558 if (i < 4) {
03559 dmv_x = dmv_y = 0;
03560 s->mb_intra = 0;
03561 mb_has_coeffs = 0;
03562 if (val) {
03563 GET_MVDATA(dmv_x, dmv_y);
03564 }
03565 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
03566 if (!s->mb_intra)
03567 vc1_mc_4mv_luma(v, i, 0);
03568 intra_count += s->mb_intra;
03569 is_intra[i] = s->mb_intra;
03570 is_coded[i] = mb_has_coeffs;
03571 }
03572 if (i & 4) {
03573 is_intra[i] = (intra_count >= 3);
03574 is_coded[i] = val;
03575 }
03576 if (i == 4)
03577 vc1_mc_4mv_chroma(v, 0);
03578 v->mb_type[0][s->block_index[i]] = is_intra[i];
03579 if (!coded_inter)
03580 coded_inter = !is_intra[i] & is_coded[i];
03581 }
03582
03583 dst_idx = 0;
03584 if (!intra_count && !coded_inter)
03585 goto end;
03586 GET_MQUANT();
03587 s->current_picture.f.qscale_table[mb_pos] = mquant;
03588
03589 {
03590 int intrapred = 0;
03591 for (i = 0; i < 6; i++)
03592 if (is_intra[i]) {
03593 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
03594 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
03595 intrapred = 1;
03596 break;
03597 }
03598 }
03599 if (intrapred)
03600 s->ac_pred = get_bits1(gb);
03601 else
03602 s->ac_pred = 0;
03603 }
03604 if (!v->ttmbf && coded_inter)
03605 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
03606 for (i = 0; i < 6; i++) {
03607 dst_idx += i >> 2;
03608 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
03609 s->mb_intra = is_intra[i];
03610 if (is_intra[i]) {
03611
03612 v->a_avail = v->c_avail = 0;
03613 if (i == 2 || i == 3 || !s->first_slice_line)
03614 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
03615 if (i == 1 || i == 3 || s->mb_x)
03616 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
03617
03618 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
03619 (i & 4) ? v->codingset2 : v->codingset);
03620 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
03621 continue;
03622 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
03623 if (v->rangeredfrm)
03624 for (j = 0; j < 64; j++)
03625 s->block[i][j] <<= 1;
03626 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
03627 (i & 4) ? s->uvlinesize : s->linesize);
03628 if (v->pq >= 9 && v->overlap) {
03629 if (v->c_avail)
03630 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
03631 if (v->a_avail)
03632 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
03633 }
03634 block_cbp |= 0xF << (i << 2);
03635 block_intra |= 1 << i;
03636 } else if (is_coded[i]) {
03637 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
03638 first_block, s->dest[dst_idx] + off,
03639 (i & 4) ? s->uvlinesize : s->linesize,
03640 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
03641 &block_tt);
03642 block_cbp |= pat << (i << 2);
03643 if (!v->ttmbf && ttmb < 8)
03644 ttmb = -1;
03645 first_block = 0;
03646 }
03647 }
03648 } else {
03649 s->mb_intra = 0;
03650 s->current_picture.f.qscale_table[mb_pos] = 0;
03651 for (i = 0; i < 6; i++) {
03652 v->mb_type[0][s->block_index[i]] = 0;
03653 s->dc_val[0][s->block_index[i]] = 0;
03654 }
03655 for (i = 0; i < 4; i++) {
03656 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
03657 vc1_mc_4mv_luma(v, i, 0);
03658 }
03659 vc1_mc_4mv_chroma(v, 0);
03660 s->current_picture.f.qscale_table[mb_pos] = 0;
03661 }
03662 }
03663 end:
03664 v->cbp[s->mb_x] = block_cbp;
03665 v->ttblk[s->mb_x] = block_tt;
03666 v->is_intra[s->mb_x] = block_intra;
03667
03668 return 0;
03669 }
03670
03671
03672
03673 static int vc1_decode_p_mb_intfr(VC1Context *v)
03674 {
03675 MpegEncContext *s = &v->s;
03676 GetBitContext *gb = &s->gb;
03677 int i;
03678 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
03679 int cbp = 0;
03680 int mqdiff, mquant;
03681 int ttmb = v->ttfrm;
03682
03683 int mb_has_coeffs = 1;
03684 int dmv_x, dmv_y;
03685 int val;
03686 int first_block = 1;
03687 int dst_idx, off;
03688 int skipped, fourmv = 0, twomv = 0;
03689 int block_cbp = 0, pat, block_tt = 0;
03690 int idx_mbmode = 0, mvbp;
03691 int stride_y, fieldtx;
03692
03693 mquant = v->pq;
03694
03695 if (v->skip_is_raw)
03696 skipped = get_bits1(gb);
03697 else
03698 skipped = v->s.mbskip_table[mb_pos];
03699 if (!skipped) {
03700 if (v->fourmvswitch)
03701 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2);
03702 else
03703 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
03704 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
03705
03706 case MV_PMODE_INTFR_4MV:
03707 fourmv = 1;
03708 v->blk_mv_type[s->block_index[0]] = 0;
03709 v->blk_mv_type[s->block_index[1]] = 0;
03710 v->blk_mv_type[s->block_index[2]] = 0;
03711 v->blk_mv_type[s->block_index[3]] = 0;
03712 break;
03713 case MV_PMODE_INTFR_4MV_FIELD:
03714 fourmv = 1;
03715 v->blk_mv_type[s->block_index[0]] = 1;
03716 v->blk_mv_type[s->block_index[1]] = 1;
03717 v->blk_mv_type[s->block_index[2]] = 1;
03718 v->blk_mv_type[s->block_index[3]] = 1;
03719 break;
03720 case MV_PMODE_INTFR_2MV_FIELD:
03721 twomv = 1;
03722 v->blk_mv_type[s->block_index[0]] = 1;
03723 v->blk_mv_type[s->block_index[1]] = 1;
03724 v->blk_mv_type[s->block_index[2]] = 1;
03725 v->blk_mv_type[s->block_index[3]] = 1;
03726 break;
03727 case MV_PMODE_INTFR_1MV:
03728 v->blk_mv_type[s->block_index[0]] = 0;
03729 v->blk_mv_type[s->block_index[1]] = 0;
03730 v->blk_mv_type[s->block_index[2]] = 0;
03731 v->blk_mv_type[s->block_index[3]] = 0;
03732 break;
03733 }
03734 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) {
03735 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
03736 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
03737 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
03738 s->mb_intra = v->is_intra[s->mb_x] = 1;
03739 for (i = 0; i < 6; i++)
03740 v->mb_type[0][s->block_index[i]] = 1;
03741 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
03742 mb_has_coeffs = get_bits1(gb);
03743 if (mb_has_coeffs)
03744 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
03745 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
03746 GET_MQUANT();
03747 s->current_picture.f.qscale_table[mb_pos] = mquant;
03748
03749 s->y_dc_scale = s->y_dc_scale_table[mquant];
03750 s->c_dc_scale = s->c_dc_scale_table[mquant];
03751 dst_idx = 0;
03752 for (i = 0; i < 6; i++) {
03753 s->dc_val[0][s->block_index[i]] = 0;
03754 dst_idx += i >> 2;
03755 val = ((cbp >> (5 - i)) & 1);
03756 v->mb_type[0][s->block_index[i]] = s->mb_intra;
03757 v->a_avail = v->c_avail = 0;
03758 if (i == 2 || i == 3 || !s->first_slice_line)
03759 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
03760 if (i == 1 || i == 3 || s->mb_x)
03761 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
03762
03763 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
03764 (i & 4) ? v->codingset2 : v->codingset);
03765 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
03766 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
03767 if (i < 4) {
03768 stride_y = s->linesize << fieldtx;
03769 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
03770 } else {
03771 stride_y = s->uvlinesize;
03772 off = 0;
03773 }
03774 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
03775
03776 }
03777
03778 } else {
03779 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
03780 if (mb_has_coeffs)
03781 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
03782 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
03783 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
03784 } else {
03785 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
03786 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
03787 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
03788 }
03789 }
03790 s->mb_intra = v->is_intra[s->mb_x] = 0;
03791 for (i = 0; i < 6; i++)
03792 v->mb_type[0][s->block_index[i]] = 0;
03793 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
03794
03795 dst_idx = 0;
03796 if (fourmv) {
03797 mvbp = v->fourmvbp;
03798 for (i = 0; i < 6; i++) {
03799 if (i < 4) {
03800 dmv_x = dmv_y = 0;
03801 val = ((mvbp >> (3 - i)) & 1);
03802 if (val) {
03803 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
03804 }
03805 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
03806 vc1_mc_4mv_luma(v, i, 0);
03807 } else if (i == 4) {
03808 vc1_mc_4mv_chroma4(v);
03809 }
03810 }
03811 } else if (twomv) {
03812 mvbp = v->twomvbp;
03813 dmv_x = dmv_y = 0;
03814 if (mvbp & 2) {
03815 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
03816 }
03817 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
03818 vc1_mc_4mv_luma(v, 0, 0);
03819 vc1_mc_4mv_luma(v, 1, 0);
03820 dmv_x = dmv_y = 0;
03821 if (mvbp & 1) {
03822 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
03823 }
03824 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
03825 vc1_mc_4mv_luma(v, 2, 0);
03826 vc1_mc_4mv_luma(v, 3, 0);
03827 vc1_mc_4mv_chroma4(v);
03828 } else {
03829 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
03830 dmv_x = dmv_y = 0;
03831 if (mvbp) {
03832 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
03833 }
03834 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
03835 vc1_mc_1mv(v, 0);
03836 }
03837 if (cbp)
03838 GET_MQUANT();
03839 s->current_picture.f.qscale_table[mb_pos] = mquant;
03840 if (!v->ttmbf && cbp)
03841 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
03842 for (i = 0; i < 6; i++) {
03843 s->dc_val[0][s->block_index[i]] = 0;
03844 dst_idx += i >> 2;
03845 val = ((cbp >> (5 - i)) & 1);
03846 if (!fieldtx)
03847 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
03848 else
03849 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
03850 if (val) {
03851 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
03852 first_block, s->dest[dst_idx] + off,
03853 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
03854 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
03855 block_cbp |= pat << (i << 2);
03856 if (!v->ttmbf && ttmb < 8)
03857 ttmb = -1;
03858 first_block = 0;
03859 }
03860 }
03861 }
03862 } else {
03863 s->mb_intra = v->is_intra[s->mb_x] = 0;
03864 for (i = 0; i < 6; i++) {
03865 v->mb_type[0][s->block_index[i]] = 0;
03866 s->dc_val[0][s->block_index[i]] = 0;
03867 }
03868 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
03869 s->current_picture.f.qscale_table[mb_pos] = 0;
03870 v->blk_mv_type[s->block_index[0]] = 0;
03871 v->blk_mv_type[s->block_index[1]] = 0;
03872 v->blk_mv_type[s->block_index[2]] = 0;
03873 v->blk_mv_type[s->block_index[3]] = 0;
03874 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
03875 vc1_mc_1mv(v, 0);
03876 }
03877 if (s->mb_x == s->mb_width - 1)
03878 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
03879 return 0;
03880 }
03881
03882 static int vc1_decode_p_mb_intfi(VC1Context *v)
03883 {
03884 MpegEncContext *s = &v->s;
03885 GetBitContext *gb = &s->gb;
03886 int i;
03887 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
03888 int cbp = 0;
03889 int mqdiff, mquant;
03890 int ttmb = v->ttfrm;
03891
03892 int mb_has_coeffs = 1;
03893 int dmv_x, dmv_y;
03894 int val;
03895 int first_block = 1;
03896 int dst_idx, off;
03897 int pred_flag = 0;
03898 int block_cbp = 0, pat, block_tt = 0;
03899 int idx_mbmode = 0;
03900
03901 mquant = v->pq;
03902
03903 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
03904 if (idx_mbmode <= 1) {
03905 s->mb_intra = v->is_intra[s->mb_x] = 1;
03906 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
03907 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
03908 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
03909 GET_MQUANT();
03910 s->current_picture.f.qscale_table[mb_pos] = mquant;
03911
03912 s->y_dc_scale = s->y_dc_scale_table[mquant];
03913 s->c_dc_scale = s->c_dc_scale_table[mquant];
03914 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
03915 mb_has_coeffs = idx_mbmode & 1;
03916 if (mb_has_coeffs)
03917 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
03918 dst_idx = 0;
03919 for (i = 0; i < 6; i++) {
03920 s->dc_val[0][s->block_index[i]] = 0;
03921 v->mb_type[0][s->block_index[i]] = 1;
03922 dst_idx += i >> 2;
03923 val = ((cbp >> (5 - i)) & 1);
03924 v->a_avail = v->c_avail = 0;
03925 if (i == 2 || i == 3 || !s->first_slice_line)
03926 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
03927 if (i == 1 || i == 3 || s->mb_x)
03928 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
03929
03930 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
03931 (i & 4) ? v->codingset2 : v->codingset);
03932 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
03933 continue;
03934 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
03935 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
03936 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
03937 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
03938
03939 }
03940 } else {
03941 s->mb_intra = v->is_intra[s->mb_x] = 0;
03942 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
03943 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
03944 if (idx_mbmode <= 5) {
03945 dmv_x = dmv_y = pred_flag = 0;
03946 if (idx_mbmode & 1) {
03947 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
03948 }
03949 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
03950 vc1_mc_1mv(v, 0);
03951 mb_has_coeffs = !(idx_mbmode & 2);
03952 } else {
03953 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
03954 for (i = 0; i < 6; i++) {
03955 if (i < 4) {
03956 dmv_x = dmv_y = pred_flag = 0;
03957 val = ((v->fourmvbp >> (3 - i)) & 1);
03958 if (val) {
03959 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
03960 }
03961 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
03962 vc1_mc_4mv_luma(v, i, 0);
03963 } else if (i == 4)
03964 vc1_mc_4mv_chroma(v, 0);
03965 }
03966 mb_has_coeffs = idx_mbmode & 1;
03967 }
03968 if (mb_has_coeffs)
03969 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
03970 if (cbp) {
03971 GET_MQUANT();
03972 }
03973 s->current_picture.f.qscale_table[mb_pos] = mquant;
03974 if (!v->ttmbf && cbp) {
03975 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
03976 }
03977 dst_idx = 0;
03978 for (i = 0; i < 6; i++) {
03979 s->dc_val[0][s->block_index[i]] = 0;
03980 dst_idx += i >> 2;
03981 val = ((cbp >> (5 - i)) & 1);
03982 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
03983 if (v->second_field)
03984 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
03985 if (val) {
03986 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
03987 first_block, s->dest[dst_idx] + off,
03988 (i & 4) ? s->uvlinesize : s->linesize,
03989 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
03990 &block_tt);
03991 block_cbp |= pat << (i << 2);
03992 if (!v->ttmbf && ttmb < 8) ttmb = -1;
03993 first_block = 0;
03994 }
03995 }
03996 }
03997 if (s->mb_x == s->mb_width - 1)
03998 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
03999 return 0;
04000 }
04001
04004 static void vc1_decode_b_mb(VC1Context *v)
04005 {
04006 MpegEncContext *s = &v->s;
04007 GetBitContext *gb = &s->gb;
04008 int i, j;
04009 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
04010 int cbp = 0;
04011 int mqdiff, mquant;
04012 int ttmb = v->ttfrm;
04013 int mb_has_coeffs = 0;
04014 int index, index1;
04015 int val, sign;
04016 int first_block = 1;
04017 int dst_idx, off;
04018 int skipped, direct;
04019 int dmv_x[2], dmv_y[2];
04020 int bmvtype = BMV_TYPE_BACKWARD;
04021
04022 mquant = v->pq;
04023 s->mb_intra = 0;
04024
04025 if (v->dmb_is_raw)
04026 direct = get_bits1(gb);
04027 else
04028 direct = v->direct_mb_plane[mb_pos];
04029 if (v->skip_is_raw)
04030 skipped = get_bits1(gb);
04031 else
04032 skipped = v->s.mbskip_table[mb_pos];
04033
04034 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
04035 for (i = 0; i < 6; i++) {
04036 v->mb_type[0][s->block_index[i]] = 0;
04037 s->dc_val[0][s->block_index[i]] = 0;
04038 }
04039 s->current_picture.f.qscale_table[mb_pos] = 0;
04040
04041 if (!direct) {
04042 if (!skipped) {
04043 GET_MVDATA(dmv_x[0], dmv_y[0]);
04044 dmv_x[1] = dmv_x[0];
04045 dmv_y[1] = dmv_y[0];
04046 }
04047 if (skipped || !s->mb_intra) {
04048 bmvtype = decode012(gb);
04049 switch (bmvtype) {
04050 case 0:
04051 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
04052 break;
04053 case 1:
04054 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
04055 break;
04056 case 2:
04057 bmvtype = BMV_TYPE_INTERPOLATED;
04058 dmv_x[0] = dmv_y[0] = 0;
04059 }
04060 }
04061 }
04062 for (i = 0; i < 6; i++)
04063 v->mb_type[0][s->block_index[i]] = s->mb_intra;
04064
04065 if (skipped) {
04066 if (direct)
04067 bmvtype = BMV_TYPE_INTERPOLATED;
04068 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04069 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
04070 return;
04071 }
04072 if (direct) {
04073 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
04074 GET_MQUANT();
04075 s->mb_intra = 0;
04076 s->current_picture.f.qscale_table[mb_pos] = mquant;
04077 if (!v->ttmbf)
04078 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
04079 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
04080 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04081 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
04082 } else {
04083 if (!mb_has_coeffs && !s->mb_intra) {
04084
04085 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04086 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
04087 return;
04088 }
04089 if (s->mb_intra && !mb_has_coeffs) {
04090 GET_MQUANT();
04091 s->current_picture.f.qscale_table[mb_pos] = mquant;
04092 s->ac_pred = get_bits1(gb);
04093 cbp = 0;
04094 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04095 } else {
04096 if (bmvtype == BMV_TYPE_INTERPOLATED) {
04097 GET_MVDATA(dmv_x[0], dmv_y[0]);
04098 if (!mb_has_coeffs) {
04099
04100 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04101 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
04102 return;
04103 }
04104 }
04105 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
04106 if (!s->mb_intra) {
04107 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
04108 }
04109 if (s->mb_intra)
04110 s->ac_pred = get_bits1(gb);
04111 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
04112 GET_MQUANT();
04113 s->current_picture.f.qscale_table[mb_pos] = mquant;
04114 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
04115 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
04116 }
04117 }
04118 dst_idx = 0;
04119 for (i = 0; i < 6; i++) {
04120 s->dc_val[0][s->block_index[i]] = 0;
04121 dst_idx += i >> 2;
04122 val = ((cbp >> (5 - i)) & 1);
04123 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
04124 v->mb_type[0][s->block_index[i]] = s->mb_intra;
04125 if (s->mb_intra) {
04126
04127 v->a_avail = v->c_avail = 0;
04128 if (i == 2 || i == 3 || !s->first_slice_line)
04129 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
04130 if (i == 1 || i == 3 || s->mb_x)
04131 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
04132
04133 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
04134 (i & 4) ? v->codingset2 : v->codingset);
04135 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
04136 continue;
04137 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
04138 if (v->rangeredfrm)
04139 for (j = 0; j < 64; j++)
04140 s->block[i][j] <<= 1;
04141 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
04142 } else if (val) {
04143 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
04144 first_block, s->dest[dst_idx] + off,
04145 (i & 4) ? s->uvlinesize : s->linesize,
04146 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
04147 if (!v->ttmbf && ttmb < 8)
04148 ttmb = -1;
04149 first_block = 0;
04150 }
04151 }
04152 }
04153
04156 static void vc1_decode_b_mb_intfi(VC1Context *v)
04157 {
04158 MpegEncContext *s = &v->s;
04159 GetBitContext *gb = &s->gb;
04160 int i, j;
04161 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
04162 int cbp = 0;
04163 int mqdiff, mquant;
04164 int ttmb = v->ttfrm;
04165 int mb_has_coeffs = 0;
04166 int val;
04167 int first_block = 1;
04168 int dst_idx, off;
04169 int fwd;
04170 int dmv_x[2], dmv_y[2], pred_flag[2];
04171 int bmvtype = BMV_TYPE_BACKWARD;
04172 int idx_mbmode, interpmvp;
04173
04174 mquant = v->pq;
04175 s->mb_intra = 0;
04176
04177 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
04178 if (idx_mbmode <= 1) {
04179 s->mb_intra = v->is_intra[s->mb_x] = 1;
04180 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
04181 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
04182 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
04183 GET_MQUANT();
04184 s->current_picture.f.qscale_table[mb_pos] = mquant;
04185
04186 s->y_dc_scale = s->y_dc_scale_table[mquant];
04187 s->c_dc_scale = s->c_dc_scale_table[mquant];
04188 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
04189 mb_has_coeffs = idx_mbmode & 1;
04190 if (mb_has_coeffs)
04191 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
04192 dst_idx = 0;
04193 for (i = 0; i < 6; i++) {
04194 s->dc_val[0][s->block_index[i]] = 0;
04195 dst_idx += i >> 2;
04196 val = ((cbp >> (5 - i)) & 1);
04197 v->mb_type[0][s->block_index[i]] = s->mb_intra;
04198 v->a_avail = v->c_avail = 0;
04199 if (i == 2 || i == 3 || !s->first_slice_line)
04200 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
04201 if (i == 1 || i == 3 || s->mb_x)
04202 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
04203
04204 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
04205 (i & 4) ? v->codingset2 : v->codingset);
04206 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
04207 continue;
04208 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
04209 if (v->rangeredfrm)
04210 for (j = 0; j < 64; j++)
04211 s->block[i][j] <<= 1;
04212 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
04213 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
04214 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
04215
04216 }
04217 } else {
04218 s->mb_intra = v->is_intra[s->mb_x] = 0;
04219 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
04220 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
04221 if (v->fmb_is_raw)
04222 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
04223 else
04224 fwd = v->forward_mb_plane[mb_pos];
04225 if (idx_mbmode <= 5) {
04226 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
04227 pred_flag[0] = pred_flag[1] = 0;
04228 if (fwd)
04229 bmvtype = BMV_TYPE_FORWARD;
04230 else {
04231 bmvtype = decode012(gb);
04232 switch (bmvtype) {
04233 case 0:
04234 bmvtype = BMV_TYPE_BACKWARD;
04235 break;
04236 case 1:
04237 bmvtype = BMV_TYPE_DIRECT;
04238 break;
04239 case 2:
04240 bmvtype = BMV_TYPE_INTERPOLATED;
04241 interpmvp = get_bits1(gb);
04242 }
04243 }
04244 v->bmvtype = bmvtype;
04245 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
04246 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
04247 }
04248 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
04249 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
04250 }
04251 if (bmvtype == BMV_TYPE_DIRECT) {
04252 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
04253 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
04254 }
04255 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
04256 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
04257 mb_has_coeffs = !(idx_mbmode & 2);
04258 } else {
04259 if (fwd)
04260 bmvtype = BMV_TYPE_FORWARD;
04261 v->bmvtype = bmvtype;
04262 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
04263 for (i = 0; i < 6; i++) {
04264 if (i < 4) {
04265 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
04266 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
04267 val = ((v->fourmvbp >> (3 - i)) & 1);
04268 if (val) {
04269 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
04270 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
04271 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
04272 }
04273 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
04274 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
04275 } else if (i == 4)
04276 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
04277 }
04278 mb_has_coeffs = idx_mbmode & 1;
04279 }
04280 if (mb_has_coeffs)
04281 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
04282 if (cbp) {
04283 GET_MQUANT();
04284 }
04285 s->current_picture.f.qscale_table[mb_pos] = mquant;
04286 if (!v->ttmbf && cbp) {
04287 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
04288 }
04289 dst_idx = 0;
04290 for (i = 0; i < 6; i++) {
04291 s->dc_val[0][s->block_index[i]] = 0;
04292 dst_idx += i >> 2;
04293 val = ((cbp >> (5 - i)) & 1);
04294 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
04295 if (v->second_field)
04296 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
04297 if (val) {
04298 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
04299 first_block, s->dest[dst_idx] + off,
04300 (i & 4) ? s->uvlinesize : s->linesize,
04301 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
04302 if (!v->ttmbf && ttmb < 8)
04303 ttmb = -1;
04304 first_block = 0;
04305 }
04306 }
04307 }
04308 }
04309
04312 static void vc1_decode_i_blocks(VC1Context *v)
04313 {
04314 int k, j;
04315 MpegEncContext *s = &v->s;
04316 int cbp, val;
04317 uint8_t *coded_val;
04318 int mb_pos;
04319
04320
04321 switch (v->y_ac_table_index) {
04322 case 0:
04323 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
04324 break;
04325 case 1:
04326 v->codingset = CS_HIGH_MOT_INTRA;
04327 break;
04328 case 2:
04329 v->codingset = CS_MID_RATE_INTRA;
04330 break;
04331 }
04332
04333 switch (v->c_ac_table_index) {
04334 case 0:
04335 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
04336 break;
04337 case 1:
04338 v->codingset2 = CS_HIGH_MOT_INTER;
04339 break;
04340 case 2:
04341 v->codingset2 = CS_MID_RATE_INTER;
04342 break;
04343 }
04344
04345
04346 s->y_dc_scale = s->y_dc_scale_table[v->pq];
04347 s->c_dc_scale = s->c_dc_scale_table[v->pq];
04348
04349
04350 s->mb_x = s->mb_y = 0;
04351 s->mb_intra = 1;
04352 s->first_slice_line = 1;
04353 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
04354 s->mb_x = 0;
04355 ff_init_block_index(s);
04356 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
04357 uint8_t *dst[6];
04358 ff_update_block_index(s);
04359 dst[0] = s->dest[0];
04360 dst[1] = dst[0] + 8;
04361 dst[2] = s->dest[0] + s->linesize * 8;
04362 dst[3] = dst[2] + 8;
04363 dst[4] = s->dest[1];
04364 dst[5] = s->dest[2];
04365 s->dsp.clear_blocks(s->block[0]);
04366 mb_pos = s->mb_x + s->mb_y * s->mb_width;
04367 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
04368 s->current_picture.f.qscale_table[mb_pos] = v->pq;
04369 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
04370 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
04371
04372
04373 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
04374 v->s.ac_pred = get_bits1(&v->s.gb);
04375
04376 for (k = 0; k < 6; k++) {
04377 val = ((cbp >> (5 - k)) & 1);
04378
04379 if (k < 4) {
04380 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
04381 val = val ^ pred;
04382 *coded_val = val;
04383 }
04384 cbp |= val << (5 - k);
04385
04386 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
04387
04388 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
04389 continue;
04390 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
04391 if (v->pq >= 9 && v->overlap) {
04392 if (v->rangeredfrm)
04393 for (j = 0; j < 64; j++)
04394 s->block[k][j] <<= 1;
04395 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
04396 } else {
04397 if (v->rangeredfrm)
04398 for (j = 0; j < 64; j++)
04399 s->block[k][j] = (s->block[k][j] - 64) << 1;
04400 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
04401 }
04402 }
04403
04404 if (v->pq >= 9 && v->overlap) {
04405 if (s->mb_x) {
04406 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
04407 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
04408 if (!(s->flags & CODEC_FLAG_GRAY)) {
04409 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
04410 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
04411 }
04412 }
04413 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
04414 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
04415 if (!s->first_slice_line) {
04416 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
04417 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
04418 if (!(s->flags & CODEC_FLAG_GRAY)) {
04419 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
04420 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
04421 }
04422 }
04423 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
04424 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
04425 }
04426 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
04427
04428 if (get_bits_count(&s->gb) > v->bits) {
04429 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
04430 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
04431 get_bits_count(&s->gb), v->bits);
04432 return;
04433 }
04434 }
04435 if (!v->s.loop_filter)
04436 ff_draw_horiz_band(s, s->mb_y * 16, 16);
04437 else if (s->mb_y)
04438 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
04439
04440 s->first_slice_line = 0;
04441 }
04442 if (v->s.loop_filter)
04443 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
04444
04445
04446
04447 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
04448 }
04449
04452 static void vc1_decode_i_blocks_adv(VC1Context *v)
04453 {
04454 int k;
04455 MpegEncContext *s = &v->s;
04456 int cbp, val;
04457 uint8_t *coded_val;
04458 int mb_pos;
04459 int mquant = v->pq;
04460 int mqdiff;
04461 GetBitContext *gb = &s->gb;
04462
04463
04464 switch (v->y_ac_table_index) {
04465 case 0:
04466 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
04467 break;
04468 case 1:
04469 v->codingset = CS_HIGH_MOT_INTRA;
04470 break;
04471 case 2:
04472 v->codingset = CS_MID_RATE_INTRA;
04473 break;
04474 }
04475
04476 switch (v->c_ac_table_index) {
04477 case 0:
04478 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
04479 break;
04480 case 1:
04481 v->codingset2 = CS_HIGH_MOT_INTER;
04482 break;
04483 case 2:
04484 v->codingset2 = CS_MID_RATE_INTER;
04485 break;
04486 }
04487
04488
04489 s->mb_x = s->mb_y = 0;
04490 s->mb_intra = 1;
04491 s->first_slice_line = 1;
04492 s->mb_y = s->start_mb_y;
04493 if (s->start_mb_y) {
04494 s->mb_x = 0;
04495 ff_init_block_index(s);
04496 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
04497 (1 + s->b8_stride) * sizeof(*s->coded_block));
04498 }
04499 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
04500 s->mb_x = 0;
04501 ff_init_block_index(s);
04502 for (;s->mb_x < s->mb_width; s->mb_x++) {
04503 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
04504 ff_update_block_index(s);
04505 s->dsp.clear_blocks(block[0]);
04506 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
04507 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
04508 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
04509 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
04510
04511
04512 if (v->fieldtx_is_raw)
04513 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
04514 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
04515 if ( v->acpred_is_raw)
04516 v->s.ac_pred = get_bits1(&v->s.gb);
04517 else
04518 v->s.ac_pred = v->acpred_plane[mb_pos];
04519
04520 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
04521 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
04522
04523 GET_MQUANT();
04524
04525 s->current_picture.f.qscale_table[mb_pos] = mquant;
04526
04527 s->y_dc_scale = s->y_dc_scale_table[mquant];
04528 s->c_dc_scale = s->c_dc_scale_table[mquant];
04529
04530 for (k = 0; k < 6; k++) {
04531 val = ((cbp >> (5 - k)) & 1);
04532
04533 if (k < 4) {
04534 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
04535 val = val ^ pred;
04536 *coded_val = val;
04537 }
04538 cbp |= val << (5 - k);
04539
04540 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
04541 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
04542
04543 vc1_decode_i_block_adv(v, block[k], k, val,
04544 (k < 4) ? v->codingset : v->codingset2, mquant);
04545
04546 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
04547 continue;
04548 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
04549 }
04550
04551 vc1_smooth_overlap_filter_iblk(v);
04552 vc1_put_signed_blocks_clamped(v);
04553 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
04554
04555 if (get_bits_count(&s->gb) > v->bits) {
04556
04557 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
04558 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
04559 get_bits_count(&s->gb), v->bits);
04560 return;
04561 }
04562 }
04563 if (!v->s.loop_filter)
04564 ff_draw_horiz_band(s, s->mb_y * 16, 16);
04565 else if (s->mb_y)
04566 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
04567 s->first_slice_line = 0;
04568 }
04569
04570
04571 s->mb_x = 0;
04572 ff_init_block_index(s);
04573 for (;s->mb_x < s->mb_width; s->mb_x++) {
04574 ff_update_block_index(s);
04575 vc1_put_signed_blocks_clamped(v);
04576 if (v->s.loop_filter)
04577 vc1_loop_filter_iblk_delayed(v, v->pq);
04578 }
04579 if (v->s.loop_filter)
04580 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
04581 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
04582 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
04583 }
04584
04585 static void vc1_decode_p_blocks(VC1Context *v)
04586 {
04587 MpegEncContext *s = &v->s;
04588 int apply_loop_filter;
04589
04590
04591 switch (v->c_ac_table_index) {
04592 case 0:
04593 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
04594 break;
04595 case 1:
04596 v->codingset = CS_HIGH_MOT_INTRA;
04597 break;
04598 case 2:
04599 v->codingset = CS_MID_RATE_INTRA;
04600 break;
04601 }
04602
04603 switch (v->c_ac_table_index) {
04604 case 0:
04605 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
04606 break;
04607 case 1:
04608 v->codingset2 = CS_HIGH_MOT_INTER;
04609 break;
04610 case 2:
04611 v->codingset2 = CS_MID_RATE_INTER;
04612 break;
04613 }
04614
04615 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
04616 s->first_slice_line = 1;
04617 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
04618 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
04619 s->mb_x = 0;
04620 ff_init_block_index(s);
04621 for (; s->mb_x < s->mb_width; s->mb_x++) {
04622 ff_update_block_index(s);
04623
04624 if (v->fcm == ILACE_FIELD)
04625 vc1_decode_p_mb_intfi(v);
04626 else if (v->fcm == ILACE_FRAME)
04627 vc1_decode_p_mb_intfr(v);
04628 else vc1_decode_p_mb(v);
04629 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
04630 vc1_apply_p_loop_filter(v);
04631 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
04632
04633 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
04634 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
04635 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
04636 return;
04637 }
04638 }
04639 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
04640 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
04641 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
04642 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
04643 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
04644 s->first_slice_line = 0;
04645 }
04646 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
04647 s->mb_x = 0;
04648 ff_init_block_index(s);
04649 for (; s->mb_x < s->mb_width; s->mb_x++) {
04650 ff_update_block_index(s);
04651 vc1_apply_p_loop_filter(v);
04652 }
04653 }
04654 if (s->end_mb_y >= s->start_mb_y)
04655 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
04656 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
04657 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
04658 }
04659
04660 static void vc1_decode_b_blocks(VC1Context *v)
04661 {
04662 MpegEncContext *s = &v->s;
04663
04664
04665 switch (v->c_ac_table_index) {
04666 case 0:
04667 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
04668 break;
04669 case 1:
04670 v->codingset = CS_HIGH_MOT_INTRA;
04671 break;
04672 case 2:
04673 v->codingset = CS_MID_RATE_INTRA;
04674 break;
04675 }
04676
04677 switch (v->c_ac_table_index) {
04678 case 0:
04679 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
04680 break;
04681 case 1:
04682 v->codingset2 = CS_HIGH_MOT_INTER;
04683 break;
04684 case 2:
04685 v->codingset2 = CS_MID_RATE_INTER;
04686 break;
04687 }
04688
04689 s->first_slice_line = 1;
04690 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
04691 s->mb_x = 0;
04692 ff_init_block_index(s);
04693 for (; s->mb_x < s->mb_width; s->mb_x++) {
04694 ff_update_block_index(s);
04695
04696 if (v->fcm == ILACE_FIELD)
04697 vc1_decode_b_mb_intfi(v);
04698 else
04699 vc1_decode_b_mb(v);
04700 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
04701
04702 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
04703 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
04704 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
04705 return;
04706 }
04707 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
04708 }
04709 if (!v->s.loop_filter)
04710 ff_draw_horiz_band(s, s->mb_y * 16, 16);
04711 else if (s->mb_y)
04712 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
04713 s->first_slice_line = 0;
04714 }
04715 if (v->s.loop_filter)
04716 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
04717 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
04718 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
04719 }
04720
04721 static void vc1_decode_skip_blocks(VC1Context *v)
04722 {
04723 MpegEncContext *s = &v->s;
04724
04725 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
04726 s->first_slice_line = 1;
04727 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
04728 s->mb_x = 0;
04729 ff_init_block_index(s);
04730 ff_update_block_index(s);
04731 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
04732 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
04733 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
04734 ff_draw_horiz_band(s, s->mb_y * 16, 16);
04735 s->first_slice_line = 0;
04736 }
04737 s->pict_type = AV_PICTURE_TYPE_P;
04738 }
04739
04740 void ff_vc1_decode_blocks(VC1Context *v)
04741 {
04742
04743 v->s.esc3_level_length = 0;
04744 if (v->x8_type) {
04745 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
04746 } else {
04747 v->cur_blk_idx = 0;
04748 v->left_blk_idx = -1;
04749 v->topleft_blk_idx = 1;
04750 v->top_blk_idx = 2;
04751 switch (v->s.pict_type) {
04752 case AV_PICTURE_TYPE_I:
04753 if (v->profile == PROFILE_ADVANCED)
04754 vc1_decode_i_blocks_adv(v);
04755 else
04756 vc1_decode_i_blocks(v);
04757 break;
04758 case AV_PICTURE_TYPE_P:
04759 if (v->p_frame_skipped)
04760 vc1_decode_skip_blocks(v);
04761 else
04762 vc1_decode_p_blocks(v);
04763 break;
04764 case AV_PICTURE_TYPE_B:
04765 if (v->bi_type) {
04766 if (v->profile == PROFILE_ADVANCED)
04767 vc1_decode_i_blocks_adv(v);
04768 else
04769 vc1_decode_i_blocks(v);
04770 } else
04771 vc1_decode_b_blocks(v);
04772 break;
04773 }
04774 }
04775 }
04776
04777 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
04778
04779 typedef struct {
04791 int coefs[2][7];
04792
04793 int effect_type, effect_flag;
04794 int effect_pcount1, effect_pcount2;
04795 int effect_params1[15], effect_params2[10];
04796 } SpriteData;
04797
04798 static inline int get_fp_val(GetBitContext* gb)
04799 {
04800 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
04801 }
04802
04803 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
04804 {
04805 c[1] = c[3] = 0;
04806
04807 switch (get_bits(gb, 2)) {
04808 case 0:
04809 c[0] = 1 << 16;
04810 c[2] = get_fp_val(gb);
04811 c[4] = 1 << 16;
04812 break;
04813 case 1:
04814 c[0] = c[4] = get_fp_val(gb);
04815 c[2] = get_fp_val(gb);
04816 break;
04817 case 2:
04818 c[0] = get_fp_val(gb);
04819 c[2] = get_fp_val(gb);
04820 c[4] = get_fp_val(gb);
04821 break;
04822 case 3:
04823 c[0] = get_fp_val(gb);
04824 c[1] = get_fp_val(gb);
04825 c[2] = get_fp_val(gb);
04826 c[3] = get_fp_val(gb);
04827 c[4] = get_fp_val(gb);
04828 break;
04829 }
04830 c[5] = get_fp_val(gb);
04831 if (get_bits1(gb))
04832 c[6] = get_fp_val(gb);
04833 else
04834 c[6] = 1 << 16;
04835 }
04836
04837 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
04838 {
04839 AVCodecContext *avctx = v->s.avctx;
04840 int sprite, i;
04841
04842 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
04843 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
04844 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
04845 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
04846 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
04847 for (i = 0; i < 7; i++)
04848 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
04849 sd->coefs[sprite][i] / (1<<16),
04850 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
04851 av_log(avctx, AV_LOG_DEBUG, "\n");
04852 }
04853
04854 skip_bits(gb, 2);
04855 if (sd->effect_type = get_bits_long(gb, 30)) {
04856 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
04857 case 7:
04858 vc1_sprite_parse_transform(gb, sd->effect_params1);
04859 break;
04860 case 14:
04861 vc1_sprite_parse_transform(gb, sd->effect_params1);
04862 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
04863 break;
04864 default:
04865 for (i = 0; i < sd->effect_pcount1; i++)
04866 sd->effect_params1[i] = get_fp_val(gb);
04867 }
04868 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
04869
04870 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
04871 for (i = 0; i < sd->effect_pcount1; i++)
04872 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
04873 sd->effect_params1[i] / (1 << 16),
04874 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
04875 av_log(avctx, AV_LOG_DEBUG, "\n");
04876 }
04877
04878 sd->effect_pcount2 = get_bits(gb, 16);
04879 if (sd->effect_pcount2 > 10) {
04880 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
04881 return;
04882 } else if (sd->effect_pcount2) {
04883 i = -1;
04884 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
04885 while (++i < sd->effect_pcount2) {
04886 sd->effect_params2[i] = get_fp_val(gb);
04887 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
04888 sd->effect_params2[i] / (1 << 16),
04889 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
04890 }
04891 av_log(avctx, AV_LOG_DEBUG, "\n");
04892 }
04893 }
04894 if (sd->effect_flag = get_bits1(gb))
04895 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
04896
04897 if (get_bits_count(gb) >= gb->size_in_bits +
04898 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
04899 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
04900 if (get_bits_count(gb) < gb->size_in_bits - 8)
04901 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
04902 }
04903
04904 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
04905 {
04906 int i, plane, row, sprite;
04907 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
04908 uint8_t* src_h[2][2];
04909 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
04910 int ysub[2];
04911 MpegEncContext *s = &v->s;
04912
04913 for (i = 0; i < 2; i++) {
04914 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
04915 xadv[i] = sd->coefs[i][0];
04916 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
04917 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
04918
04919 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
04920 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
04921 }
04922 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
04923
04924 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
04925 int width = v->output_width>>!!plane;
04926
04927 for (row = 0; row < v->output_height>>!!plane; row++) {
04928 uint8_t *dst = v->sprite_output_frame.data[plane] +
04929 v->sprite_output_frame.linesize[plane] * row;
04930
04931 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
04932 uint8_t *iplane = s->current_picture.f.data[plane];
04933 int iline = s->current_picture.f.linesize[plane];
04934 int ycoord = yoff[sprite] + yadv[sprite] * row;
04935 int yline = ycoord >> 16;
04936 int next_line;
04937 ysub[sprite] = ycoord & 0xFFFF;
04938 if (sprite) {
04939 iplane = s->last_picture.f.data[plane];
04940 iline = s->last_picture.f.linesize[plane];
04941 }
04942 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
04943 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
04944 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
04945 if (ysub[sprite])
04946 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
04947 } else {
04948 if (sr_cache[sprite][0] != yline) {
04949 if (sr_cache[sprite][1] == yline) {
04950 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
04951 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
04952 } else {
04953 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
04954 sr_cache[sprite][0] = yline;
04955 }
04956 }
04957 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
04958 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
04959 iplane + next_line, xoff[sprite],
04960 xadv[sprite], width);
04961 sr_cache[sprite][1] = yline + 1;
04962 }
04963 src_h[sprite][0] = v->sr_rows[sprite][0];
04964 src_h[sprite][1] = v->sr_rows[sprite][1];
04965 }
04966 }
04967
04968 if (!v->two_sprites) {
04969 if (ysub[0]) {
04970 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
04971 } else {
04972 memcpy(dst, src_h[0][0], width);
04973 }
04974 } else {
04975 if (ysub[0] && ysub[1]) {
04976 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
04977 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
04978 } else if (ysub[0]) {
04979 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
04980 src_h[1][0], alpha, width);
04981 } else if (ysub[1]) {
04982 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
04983 src_h[0][0], (1<<16)-1-alpha, width);
04984 } else {
04985 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
04986 }
04987 }
04988 }
04989
04990 if (!plane) {
04991 for (i = 0; i < 2; i++) {
04992 xoff[i] >>= 1;
04993 yoff[i] >>= 1;
04994 }
04995 }
04996
04997 }
04998 }
04999
05000
05001 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
05002 {
05003 MpegEncContext *s = &v->s;
05004 AVCodecContext *avctx = s->avctx;
05005 SpriteData sd;
05006
05007 vc1_parse_sprites(v, gb, &sd);
05008
05009 if (!s->current_picture.f.data[0]) {
05010 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
05011 return -1;
05012 }
05013
05014 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
05015 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
05016 v->two_sprites = 0;
05017 }
05018
05019 if (v->sprite_output_frame.data[0])
05020 avctx->release_buffer(avctx, &v->sprite_output_frame);
05021
05022 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
05023 v->sprite_output_frame.reference = 0;
05024 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
05025 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
05026 return -1;
05027 }
05028
05029 vc1_draw_sprites(v, &sd);
05030
05031 return 0;
05032 }
05033
05034 static void vc1_sprite_flush(AVCodecContext *avctx)
05035 {
05036 VC1Context *v = avctx->priv_data;
05037 MpegEncContext *s = &v->s;
05038 AVFrame *f = &s->current_picture.f;
05039 int plane, i;
05040
05041
05042
05043
05044
05045 if (f->data[0])
05046 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
05047 for (i = 0; i < v->sprite_height>>!!plane; i++)
05048 memset(f->data[plane] + i * f->linesize[plane],
05049 plane ? 128 : 0, f->linesize[plane]);
05050 }
05051
05052 #endif
05053
05054 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
05055 {
05056 MpegEncContext *s = &v->s;
05057 int i;
05058
05059
05060 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
05061 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
05062 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
05063 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
05064 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
05065 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
05066
05067 v->n_allocated_blks = s->mb_width + 2;
05068 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
05069 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
05070 v->cbp = v->cbp_base + s->mb_stride;
05071 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
05072 v->ttblk = v->ttblk_base + s->mb_stride;
05073 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
05074 v->is_intra = v->is_intra_base + s->mb_stride;
05075 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
05076 v->luma_mv = v->luma_mv_base + s->mb_stride;
05077
05078
05079 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
05080 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
05081 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
05082 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
05083
05084
05085 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
05086 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
05087 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
05088 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
05089 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
05090 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
05091 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
05092 v->mv_f_last[1] = v->mv_f_last[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
05093 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
05094 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
05095 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
05096
05097
05098 if (v->profile == PROFILE_ADVANCED) {
05099
05100
05101
05102
05103 }
05104
05105 ff_intrax8_common_init(&v->x8,s);
05106
05107 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
05108 for (i = 0; i < 4; i++)
05109 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
05110 }
05111
05112 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
05113 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
05114 !v->mb_type_base)
05115 return -1;
05116
05117 return 0;
05118 }
05119
05120 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
05121 {
05122 int i;
05123 for (i = 0; i < 64; i++) {
05124 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
05125 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
05126 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
05127 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
05128 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
05129 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
05130 }
05131 v->left_blk_sh = 0;
05132 v->top_blk_sh = 3;
05133 }
05134
05139 static av_cold int vc1_decode_init(AVCodecContext *avctx)
05140 {
05141 VC1Context *v = avctx->priv_data;
05142 MpegEncContext *s = &v->s;
05143 GetBitContext gb;
05144
05145
05146 v->output_width = avctx->width;
05147 v->output_height = avctx->height;
05148
05149 if (!avctx->extradata_size || !avctx->extradata)
05150 return -1;
05151 if (!(avctx->flags & CODEC_FLAG_GRAY))
05152 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
05153 else
05154 avctx->pix_fmt = PIX_FMT_GRAY8;
05155 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
05156 v->s.avctx = avctx;
05157 avctx->flags |= CODEC_FLAG_EMU_EDGE;
05158 v->s.flags |= CODEC_FLAG_EMU_EDGE;
05159
05160 if (avctx->idct_algo == FF_IDCT_AUTO) {
05161 avctx->idct_algo = FF_IDCT_WMV2;
05162 }
05163
05164 if (ff_vc1_init_common(v) < 0)
05165 return -1;
05166 ff_vc1dsp_init(&v->vc1dsp);
05167
05168 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
05169 int count = 0;
05170
05171
05172
05173
05174
05175
05176 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
05177
05178 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
05179 return -1;
05180
05181 count = avctx->extradata_size*8 - get_bits_count(&gb);
05182 if (count > 0) {
05183 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
05184 count, get_bits(&gb, count));
05185 } else if (count < 0) {
05186 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
05187 }
05188 } else {
05189 const uint8_t *start = avctx->extradata;
05190 uint8_t *end = avctx->extradata + avctx->extradata_size;
05191 const uint8_t *next;
05192 int size, buf2_size;
05193 uint8_t *buf2 = NULL;
05194 int seq_initialized = 0, ep_initialized = 0;
05195
05196 if (avctx->extradata_size < 16) {
05197 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
05198 return -1;
05199 }
05200
05201 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
05202 start = find_next_marker(start, end);
05203 next = start;
05204 for (; next < end; start = next) {
05205 next = find_next_marker(start + 4, end);
05206 size = next - start - 4;
05207 if (size <= 0)
05208 continue;
05209 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
05210 init_get_bits(&gb, buf2, buf2_size * 8);
05211 switch (AV_RB32(start)) {
05212 case VC1_CODE_SEQHDR:
05213 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
05214 av_free(buf2);
05215 return -1;
05216 }
05217 seq_initialized = 1;
05218 break;
05219 case VC1_CODE_ENTRYPOINT:
05220 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
05221 av_free(buf2);
05222 return -1;
05223 }
05224 ep_initialized = 1;
05225 break;
05226 }
05227 }
05228 av_free(buf2);
05229 if (!seq_initialized || !ep_initialized) {
05230 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
05231 return -1;
05232 }
05233 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
05234 }
05235
05236 avctx->profile = v->profile;
05237 if (v->profile == PROFILE_ADVANCED)
05238 avctx->level = v->level;
05239
05240 avctx->has_b_frames = !!avctx->max_b_frames;
05241
05242 s->mb_width = (avctx->coded_width + 15) >> 4;
05243 s->mb_height = (avctx->coded_height + 15) >> 4;
05244
05245 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
05246 ff_vc1_init_transposed_scantables(v);
05247 } else {
05248 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
05249 v->left_blk_sh = 3;
05250 v->top_blk_sh = 0;
05251 }
05252
05253 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
05254 v->sprite_width = avctx->coded_width;
05255 v->sprite_height = avctx->coded_height;
05256
05257 avctx->coded_width = avctx->width = v->output_width;
05258 avctx->coded_height = avctx->height = v->output_height;
05259
05260
05261 if (v->sprite_width > 1 << 14 ||
05262 v->sprite_height > 1 << 14 ||
05263 v->output_width > 1 << 14 ||
05264 v->output_height > 1 << 14) return -1;
05265 }
05266 return 0;
05267 }
05268
05272 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
05273 {
05274 VC1Context *v = avctx->priv_data;
05275 int i;
05276
05277 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
05278 && v->sprite_output_frame.data[0])
05279 avctx->release_buffer(avctx, &v->sprite_output_frame);
05280 for (i = 0; i < 4; i++)
05281 av_freep(&v->sr_rows[i >> 1][i & 1]);
05282 av_freep(&v->hrd_rate);
05283 av_freep(&v->hrd_buffer);
05284 ff_MPV_common_end(&v->s);
05285 av_freep(&v->mv_type_mb_plane);
05286 av_freep(&v->direct_mb_plane);
05287 av_freep(&v->forward_mb_plane);
05288 av_freep(&v->fieldtx_plane);
05289 av_freep(&v->acpred_plane);
05290 av_freep(&v->over_flags_plane);
05291 av_freep(&v->mb_type_base);
05292 av_freep(&v->blk_mv_type_base);
05293 av_freep(&v->mv_f_base);
05294 av_freep(&v->mv_f_last_base);
05295 av_freep(&v->mv_f_next_base);
05296 av_freep(&v->block);
05297 av_freep(&v->cbp_base);
05298 av_freep(&v->ttblk_base);
05299 av_freep(&v->is_intra_base);
05300 av_freep(&v->luma_mv_base);
05301 ff_intrax8_common_end(&v->x8);
05302 return 0;
05303 }
05304
05305
05309 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
05310 int *data_size, AVPacket *avpkt)
05311 {
05312 const uint8_t *buf = avpkt->data;
05313 int buf_size = avpkt->size, n_slices = 0, i;
05314 VC1Context *v = avctx->priv_data;
05315 MpegEncContext *s = &v->s;
05316 AVFrame *pict = data;
05317 uint8_t *buf2 = NULL;
05318 const uint8_t *buf_start = buf;
05319 int mb_height, n_slices1=-1;
05320 struct {
05321 uint8_t *buf;
05322 GetBitContext gb;
05323 int mby_start;
05324 } *slices = NULL, *tmp;
05325
05326 if(s->flags & CODEC_FLAG_LOW_DELAY)
05327 s->low_delay = 1;
05328
05329
05330 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
05331
05332 if (s->low_delay == 0 && s->next_picture_ptr) {
05333 *pict = s->next_picture_ptr->f;
05334 s->next_picture_ptr = NULL;
05335
05336 *data_size = sizeof(AVFrame);
05337 }
05338
05339 return buf_size;
05340 }
05341
05342 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
05343 if (v->profile < PROFILE_ADVANCED)
05344 avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
05345 else
05346 avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
05347 }
05348
05349
05350 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
05351 int buf_size2 = 0;
05352 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
05353
05354 if (IS_MARKER(AV_RB32(buf))) {
05355 const uint8_t *start, *end, *next;
05356 int size;
05357
05358 next = buf;
05359 for (start = buf, end = buf + buf_size; next < end; start = next) {
05360 next = find_next_marker(start + 4, end);
05361 size = next - start - 4;
05362 if (size <= 0) continue;
05363 switch (AV_RB32(start)) {
05364 case VC1_CODE_FRAME:
05365 if (avctx->hwaccel ||
05366 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
05367 buf_start = start;
05368 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
05369 break;
05370 case VC1_CODE_FIELD: {
05371 int buf_size3;
05372 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
05373 if (!slices)
05374 goto err;
05375 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
05376 if (!slices[n_slices].buf)
05377 goto err;
05378 buf_size3 = vc1_unescape_buffer(start + 4, size,
05379 slices[n_slices].buf);
05380 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
05381 buf_size3 << 3);
05382
05383
05384 slices[n_slices].mby_start = s->mb_height >> 1;
05385 n_slices1 = n_slices - 1;
05386 n_slices++;
05387 break;
05388 }
05389 case VC1_CODE_ENTRYPOINT:
05390 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
05391 init_get_bits(&s->gb, buf2, buf_size2 * 8);
05392 ff_vc1_decode_entry_point(avctx, v, &s->gb);
05393 break;
05394 case VC1_CODE_SLICE: {
05395 int buf_size3;
05396 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
05397 if (!slices)
05398 goto err;
05399 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
05400 if (!slices[n_slices].buf)
05401 goto err;
05402 buf_size3 = vc1_unescape_buffer(start + 4, size,
05403 slices[n_slices].buf);
05404 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
05405 buf_size3 << 3);
05406 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
05407 n_slices++;
05408 break;
05409 }
05410 }
05411 }
05412 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) {
05413 const uint8_t *divider;
05414 int buf_size3;
05415
05416 divider = find_next_marker(buf, buf + buf_size);
05417 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
05418 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
05419 goto err;
05420 } else {
05421 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
05422 if (!tmp)
05423 goto err;
05424 slices = tmp;
05425 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
05426 if (!slices[n_slices].buf)
05427 goto err;
05428 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
05429 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
05430 buf_size3 << 3);
05431 slices[n_slices].mby_start = s->mb_height >> 1;
05432 n_slices1 = n_slices - 1;
05433 n_slices++;
05434 }
05435 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
05436 } else {
05437 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
05438 }
05439 init_get_bits(&s->gb, buf2, buf_size2*8);
05440 } else
05441 init_get_bits(&s->gb, buf, buf_size*8);
05442
05443 if (v->res_sprite) {
05444 v->new_sprite = !get_bits1(&s->gb);
05445 v->two_sprites = get_bits1(&s->gb);
05446
05447
05448
05449
05450 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
05451 if (v->new_sprite) {
05452
05453 avctx->width = avctx->coded_width = v->sprite_width;
05454 avctx->height = avctx->coded_height = v->sprite_height;
05455 } else {
05456 goto image;
05457 }
05458 }
05459 }
05460
05461 if (s->context_initialized &&
05462 (s->width != avctx->coded_width ||
05463 s->height != avctx->coded_height)) {
05464 ff_vc1_decode_end(avctx);
05465 }
05466
05467 if (!s->context_initialized) {
05468 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
05469 return -1;
05470
05471 s->low_delay = !avctx->has_b_frames || v->res_sprite;
05472
05473 if (v->profile == PROFILE_ADVANCED) {
05474 s->h_edge_pos = avctx->coded_width;
05475 s->v_edge_pos = avctx->coded_height;
05476 }
05477 }
05478
05479
05480
05481 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
05482 int i = ff_find_unused_picture(s, 0);
05483 if (i < 0)
05484 goto err;
05485 s->current_picture_ptr = &s->picture[i];
05486 }
05487
05488
05489 v->pic_header_flag = 0;
05490 if (v->profile < PROFILE_ADVANCED) {
05491 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
05492 goto err;
05493 }
05494 } else {
05495 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
05496 goto err;
05497 }
05498 }
05499
05500 if (avctx->debug & FF_DEBUG_PICT_INFO)
05501 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
05502
05503 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
05504 && s->pict_type != AV_PICTURE_TYPE_I) {
05505 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
05506 goto err;
05507 }
05508
05509
05510 s->current_picture_ptr->f.repeat_pict = 0;
05511
05512
05513 if (v->rff) {
05514
05515 s->current_picture_ptr->f.repeat_pict = 1;
05516 } else if (v->rptfrm) {
05517
05518 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
05519 }
05520
05521
05522 s->current_picture.f.pict_type = s->pict_type;
05523 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
05524
05525
05526 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
05527 goto err;
05528 }
05529 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
05530 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
05531 avctx->skip_frame >= AVDISCARD_ALL) {
05532 goto end;
05533 }
05534
05535 if (s->next_p_frame_damaged) {
05536 if (s->pict_type == AV_PICTURE_TYPE_B)
05537 goto end;
05538 else
05539 s->next_p_frame_damaged = 0;
05540 }
05541
05542 if (ff_MPV_frame_start(s, avctx) < 0) {
05543 goto err;
05544 }
05545
05546 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
05547 v->s.current_picture_ptr->f.top_field_first = v->tff;
05548
05549 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
05550 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
05551
05552 if ((CONFIG_VC1_VDPAU_DECODER)
05553 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
05554 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
05555 else if (avctx->hwaccel) {
05556 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
05557 goto err;
05558 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
05559 goto err;
05560 if (avctx->hwaccel->end_frame(avctx) < 0)
05561 goto err;
05562 } else {
05563 ff_er_frame_start(s);
05564
05565 v->bits = buf_size * 8;
05566 v->end_mb_x = s->mb_width;
05567 if (v->field_mode) {
05568 uint8_t *tmp[2];
05569 s->current_picture.f.linesize[0] <<= 1;
05570 s->current_picture.f.linesize[1] <<= 1;
05571 s->current_picture.f.linesize[2] <<= 1;
05572 s->linesize <<= 1;
05573 s->uvlinesize <<= 1;
05574 tmp[0] = v->mv_f_last[0];
05575 tmp[1] = v->mv_f_last[1];
05576 v->mv_f_last[0] = v->mv_f_next[0];
05577 v->mv_f_last[1] = v->mv_f_next[1];
05578 v->mv_f_next[0] = v->mv_f[0];
05579 v->mv_f_next[1] = v->mv_f[1];
05580 v->mv_f[0] = tmp[0];
05581 v->mv_f[1] = tmp[1];
05582 }
05583 mb_height = s->mb_height >> v->field_mode;
05584 for (i = 0; i <= n_slices; i++) {
05585 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
05586 if (v->field_mode <= 0) {
05587 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
05588 "picture boundary (%d >= %d)\n", i,
05589 slices[i - 1].mby_start, mb_height);
05590 continue;
05591 }
05592 v->second_field = 1;
05593 v->blocks_off = s->mb_width * s->mb_height << 1;
05594 v->mb_off = s->mb_stride * s->mb_height >> 1;
05595 } else {
05596 v->second_field = 0;
05597 v->blocks_off = 0;
05598 v->mb_off = 0;
05599 }
05600 if (i) {
05601 v->pic_header_flag = 0;
05602 if (v->field_mode && i == n_slices1 + 2) {
05603 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
05604 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
05605 continue;
05606 }
05607 } else if (get_bits1(&s->gb)) {
05608 v->pic_header_flag = 1;
05609 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
05610 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
05611 continue;
05612 }
05613 }
05614 }
05615 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
05616 if (!v->field_mode || v->second_field)
05617 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
05618 else
05619 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
05620 if (s->end_mb_y <= s->start_mb_y) {
05621 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
05622 continue;
05623 }
05624 ff_vc1_decode_blocks(v);
05625 if (i != n_slices)
05626 s->gb = slices[i].gb;
05627 }
05628 if (v->field_mode) {
05629 v->second_field = 0;
05630 if (s->pict_type == AV_PICTURE_TYPE_B) {
05631 memcpy(v->mv_f_base, v->mv_f_next_base,
05632 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
05633 }
05634 s->current_picture.f.linesize[0] >>= 1;
05635 s->current_picture.f.linesize[1] >>= 1;
05636 s->current_picture.f.linesize[2] >>= 1;
05637 s->linesize >>= 1;
05638 s->uvlinesize >>= 1;
05639 }
05640
05641
05642
05643 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
05644 goto err;
05645 if(!v->field_mode)
05646 ff_er_frame_end(s);
05647 }
05648
05649 ff_MPV_frame_end(s);
05650
05651 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
05652 image:
05653 avctx->width = avctx->coded_width = v->output_width;
05654 avctx->height = avctx->coded_height = v->output_height;
05655 if (avctx->skip_frame >= AVDISCARD_NONREF)
05656 goto end;
05657 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
05658 if (vc1_decode_sprites(v, &s->gb))
05659 goto err;
05660 #endif
05661 *pict = v->sprite_output_frame;
05662 *data_size = sizeof(AVFrame);
05663 } else {
05664 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
05665 *pict = s->current_picture_ptr->f;
05666 } else if (s->last_picture_ptr != NULL) {
05667 *pict = s->last_picture_ptr->f;
05668 }
05669 if (s->last_picture_ptr || s->low_delay) {
05670 *data_size = sizeof(AVFrame);
05671 ff_print_debug_info(s, pict);
05672 }
05673 }
05674
05675 end:
05676 av_free(buf2);
05677 for (i = 0; i < n_slices; i++)
05678 av_free(slices[i].buf);
05679 av_free(slices);
05680 return buf_size;
05681
05682 err:
05683 av_free(buf2);
05684 for (i = 0; i < n_slices; i++)
05685 av_free(slices[i].buf);
05686 av_free(slices);
05687 return -1;
05688 }
05689
05690
05691 static const AVProfile profiles[] = {
05692 { FF_PROFILE_VC1_SIMPLE, "Simple" },
05693 { FF_PROFILE_VC1_MAIN, "Main" },
05694 { FF_PROFILE_VC1_COMPLEX, "Complex" },
05695 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
05696 { FF_PROFILE_UNKNOWN },
05697 };
05698
05699 AVCodec ff_vc1_decoder = {
05700 .name = "vc1",
05701 .type = AVMEDIA_TYPE_VIDEO,
05702 .id = AV_CODEC_ID_VC1,
05703 .priv_data_size = sizeof(VC1Context),
05704 .init = vc1_decode_init,
05705 .close = ff_vc1_decode_end,
05706 .decode = vc1_decode_frame,
05707 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
05708 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
05709 .pix_fmts = ff_hwaccel_pixfmt_list_420,
05710 .profiles = NULL_IF_CONFIG_SMALL(profiles)
05711 };
05712
05713 #if CONFIG_WMV3_DECODER
05714 AVCodec ff_wmv3_decoder = {
05715 .name = "wmv3",
05716 .type = AVMEDIA_TYPE_VIDEO,
05717 .id = AV_CODEC_ID_WMV3,
05718 .priv_data_size = sizeof(VC1Context),
05719 .init = vc1_decode_init,
05720 .close = ff_vc1_decode_end,
05721 .decode = vc1_decode_frame,
05722 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
05723 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
05724 .pix_fmts = ff_hwaccel_pixfmt_list_420,
05725 .profiles = NULL_IF_CONFIG_SMALL(profiles)
05726 };
05727 #endif
05728
05729 #if CONFIG_WMV3_VDPAU_DECODER
05730 AVCodec ff_wmv3_vdpau_decoder = {
05731 .name = "wmv3_vdpau",
05732 .type = AVMEDIA_TYPE_VIDEO,
05733 .id = AV_CODEC_ID_WMV3,
05734 .priv_data_size = sizeof(VC1Context),
05735 .init = vc1_decode_init,
05736 .close = ff_vc1_decode_end,
05737 .decode = vc1_decode_frame,
05738 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
05739 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
05740 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE },
05741 .profiles = NULL_IF_CONFIG_SMALL(profiles)
05742 };
05743 #endif
05744
05745 #if CONFIG_VC1_VDPAU_DECODER
05746 AVCodec ff_vc1_vdpau_decoder = {
05747 .name = "vc1_vdpau",
05748 .type = AVMEDIA_TYPE_VIDEO,
05749 .id = AV_CODEC_ID_VC1,
05750 .priv_data_size = sizeof(VC1Context),
05751 .init = vc1_decode_init,
05752 .close = ff_vc1_decode_end,
05753 .decode = vc1_decode_frame,
05754 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
05755 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
05756 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_VC1, PIX_FMT_NONE },
05757 .profiles = NULL_IF_CONFIG_SMALL(profiles)
05758 };
05759 #endif
05760
05761 #if CONFIG_WMV3IMAGE_DECODER
05762 AVCodec ff_wmv3image_decoder = {
05763 .name = "wmv3image",
05764 .type = AVMEDIA_TYPE_VIDEO,
05765 .id = AV_CODEC_ID_WMV3IMAGE,
05766 .priv_data_size = sizeof(VC1Context),
05767 .init = vc1_decode_init,
05768 .close = ff_vc1_decode_end,
05769 .decode = vc1_decode_frame,
05770 .capabilities = CODEC_CAP_DR1,
05771 .flush = vc1_sprite_flush,
05772 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
05773 .pix_fmts = ff_pixfmt_list_420
05774 };
05775 #endif
05776
05777 #if CONFIG_VC1IMAGE_DECODER
05778 AVCodec ff_vc1image_decoder = {
05779 .name = "vc1image",
05780 .type = AVMEDIA_TYPE_VIDEO,
05781 .id = AV_CODEC_ID_VC1IMAGE,
05782 .priv_data_size = sizeof(VC1Context),
05783 .init = vc1_decode_init,
05784 .close = ff_vc1_decode_end,
05785 .decode = vc1_decode_frame,
05786 .capabilities = CODEC_CAP_DR1,
05787 .flush = vc1_sprite_flush,
05788 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
05789 .pix_fmts = ff_pixfmt_list_420
05790 };
05791 #endif