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