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