2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
32 #include "mpegvideo.h"
36 #include "vc1acdata.h"
37 #include "msmpeg4data.h"
39 #include "simple_idct.h"
41 #include "vdpau_internal.h"
42 #include "libavutil/avassert.h"
47 #define MB_INTRA_VLC_BITS 9
51 // offset tables for interlaced picture MVDATA decoding
52 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
53 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
55 /***********************************************************************/
57 * @name VC-1 Bitplane decoding
75 /** @} */ //imode defines
78 /** @} */ //Bitplane group
80 static void vc1_put_signed_blocks_clamped(VC1Context *v)
82 MpegEncContext *s = &v->s;
83 int topleft_mb_pos, top_mb_pos;
84 int stride_y, fieldtx;
87 /* The put pixels loop is always one MB row behind the decoding loop,
88 * because we can only put pixels when overlap filtering is done, and
89 * for filtering of the bottom edge of a MB, we need the next MB row
91 * Within the row, the put pixels loop is also one MB col behind the
92 * decoding loop. The reason for this is again, because for filtering
93 * of the right MB edge, we need the next MB present. */
94 if (!s->first_slice_line) {
96 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
97 fieldtx = v->fieldtx_plane[topleft_mb_pos];
98 stride_y = s->linesize << fieldtx;
99 v_dist = (16 - fieldtx) >> (fieldtx == 0);
100 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
101 s->dest[0] - 16 * s->linesize - 16,
103 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
104 s->dest[0] - 16 * s->linesize - 8,
106 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
107 s->dest[0] - v_dist * s->linesize - 16,
109 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
110 s->dest[0] - v_dist * s->linesize - 8,
112 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
113 s->dest[1] - 8 * s->uvlinesize - 8,
115 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
116 s->dest[2] - 8 * s->uvlinesize - 8,
119 if (s->mb_x == s->mb_width - 1) {
120 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
121 fieldtx = v->fieldtx_plane[top_mb_pos];
122 stride_y = s->linesize << fieldtx;
123 v_dist = fieldtx ? 15 : 8;
124 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
125 s->dest[0] - 16 * s->linesize,
127 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
128 s->dest[0] - 16 * s->linesize + 8,
130 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
131 s->dest[0] - v_dist * s->linesize,
133 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
134 s->dest[0] - v_dist * s->linesize + 8,
136 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
137 s->dest[1] - 8 * s->uvlinesize,
139 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
140 s->dest[2] - 8 * s->uvlinesize,
145 #define inc_blk_idx(idx) do { \
147 if (idx >= v->n_allocated_blks) \
151 inc_blk_idx(v->topleft_blk_idx);
152 inc_blk_idx(v->top_blk_idx);
153 inc_blk_idx(v->left_blk_idx);
154 inc_blk_idx(v->cur_blk_idx);
157 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
159 MpegEncContext *s = &v->s;
161 if (!s->first_slice_line) {
162 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
164 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
165 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
166 for (j = 0; j < 2; j++) {
167 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
169 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
172 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
174 if (s->mb_y == s->end_mb_y - 1) {
176 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
177 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
178 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
180 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
184 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
186 MpegEncContext *s = &v->s;
189 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
190 * means it runs two rows/cols behind the decoding loop. */
191 if (!s->first_slice_line) {
193 if (s->mb_y >= s->start_mb_y + 2) {
194 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
197 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
198 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
199 for (j = 0; j < 2; j++) {
200 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
202 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
206 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
209 if (s->mb_x == s->mb_width - 1) {
210 if (s->mb_y >= s->start_mb_y + 2) {
211 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
214 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
215 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
216 for (j = 0; j < 2; j++) {
217 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
219 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
223 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
226 if (s->mb_y == s->end_mb_y) {
229 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
230 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
232 for (j = 0; j < 2; j++) {
233 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
238 if (s->mb_x == s->mb_width - 1) {
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
243 for (j = 0; j < 2; j++) {
244 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
252 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
254 MpegEncContext *s = &v->s;
257 if (v->condover == CONDOVER_NONE)
260 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
262 /* Within a MB, the horizontal overlap always runs before the vertical.
263 * To accomplish that, we run the H on left and internal borders of the
264 * currently decoded MB. Then, we wait for the next overlap iteration
265 * to do H overlap on the right edge of this MB, before moving over and
266 * running the V overlap. Therefore, the V overlap makes us trail by one
267 * MB col and the H overlap filter makes us trail by one MB row. This
268 * is reflected in the time at which we run the put_pixels loop. */
269 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
270 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
271 v->over_flags_plane[mb_pos - 1])) {
272 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
273 v->block[v->cur_blk_idx][0]);
274 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
275 v->block[v->cur_blk_idx][2]);
276 if (!(s->flags & CODEC_FLAG_GRAY)) {
277 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
278 v->block[v->cur_blk_idx][4]);
279 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
280 v->block[v->cur_blk_idx][5]);
283 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
284 v->block[v->cur_blk_idx][1]);
285 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
286 v->block[v->cur_blk_idx][3]);
288 if (s->mb_x == s->mb_width - 1) {
289 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
290 v->over_flags_plane[mb_pos - s->mb_stride])) {
291 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
292 v->block[v->cur_blk_idx][0]);
293 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
294 v->block[v->cur_blk_idx][1]);
295 if (!(s->flags & CODEC_FLAG_GRAY)) {
296 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
297 v->block[v->cur_blk_idx][4]);
298 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
299 v->block[v->cur_blk_idx][5]);
302 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
303 v->block[v->cur_blk_idx][2]);
304 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
305 v->block[v->cur_blk_idx][3]);
308 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
309 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
310 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
311 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
312 v->block[v->left_blk_idx][0]);
313 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
314 v->block[v->left_blk_idx][1]);
315 if (!(s->flags & CODEC_FLAG_GRAY)) {
316 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
317 v->block[v->left_blk_idx][4]);
318 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
319 v->block[v->left_blk_idx][5]);
322 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
323 v->block[v->left_blk_idx][2]);
324 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
325 v->block[v->left_blk_idx][3]);
329 /** Do motion compensation over 1 macroblock
330 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
332 static void vc1_mc_1mv(VC1Context *v, int dir)
334 MpegEncContext *s = &v->s;
335 DSPContext *dsp = &v->s.dsp;
336 uint8_t *srcY, *srcU, *srcV;
337 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
339 int v_edge_pos = s->v_edge_pos >> v->field_mode;
341 if ((!v->field_mode ||
342 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
343 !v->s.last_picture.f.data[0])
346 mx = s->mv[dir][0][0];
347 my = s->mv[dir][0][1];
349 // store motion vectors for further use in B frames
350 if (s->pict_type == AV_PICTURE_TYPE_P) {
351 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
352 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
355 uvmx = (mx + ((mx & 3) == 3)) >> 1;
356 uvmy = (my + ((my & 3) == 3)) >> 1;
357 v->luma_mv[s->mb_x][0] = uvmx;
358 v->luma_mv[s->mb_x][1] = uvmy;
361 v->cur_field_type != v->ref_field_type[dir]) {
362 my = my - 2 + 4 * v->cur_field_type;
363 uvmy = uvmy - 2 + 4 * v->cur_field_type;
366 // fastuvmc shall be ignored for interlaced frame picture
367 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
368 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
369 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
371 if (v->field_mode) { // interlaced field picture
373 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
374 srcY = s->current_picture.f.data[0];
375 srcU = s->current_picture.f.data[1];
376 srcV = s->current_picture.f.data[2];
378 srcY = s->last_picture.f.data[0];
379 srcU = s->last_picture.f.data[1];
380 srcV = s->last_picture.f.data[2];
383 srcY = s->next_picture.f.data[0];
384 srcU = s->next_picture.f.data[1];
385 srcV = s->next_picture.f.data[2];
389 srcY = s->last_picture.f.data[0];
390 srcU = s->last_picture.f.data[1];
391 srcV = s->last_picture.f.data[2];
393 srcY = s->next_picture.f.data[0];
394 srcU = s->next_picture.f.data[1];
395 srcV = s->next_picture.f.data[2];
399 src_x = s->mb_x * 16 + (mx >> 2);
400 src_y = s->mb_y * 16 + (my >> 2);
401 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
402 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
404 if (v->profile != PROFILE_ADVANCED) {
405 src_x = av_clip( src_x, -16, s->mb_width * 16);
406 src_y = av_clip( src_y, -16, s->mb_height * 16);
407 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
408 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
410 src_x = av_clip( src_x, -17, s->avctx->coded_width);
411 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
412 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
413 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
416 srcY += src_y * s->linesize + src_x;
417 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
418 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
420 if (v->field_mode && v->ref_field_type[dir]) {
421 srcY += s->current_picture_ptr->f.linesize[0];
422 srcU += s->current_picture_ptr->f.linesize[1];
423 srcV += s->current_picture_ptr->f.linesize[2];
426 /* for grayscale we should not try to read from unknown area */
427 if (s->flags & CODEC_FLAG_GRAY) {
428 srcU = s->edge_emu_buffer + 18 * s->linesize;
429 srcV = s->edge_emu_buffer + 18 * s->linesize;
432 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
433 || s->h_edge_pos < 22 || v_edge_pos < 22
434 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
435 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 3) {
436 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
438 srcY -= s->mspel * (1 + s->linesize);
439 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
440 17 + s->mspel * 2, 17 + s->mspel * 2,
441 src_x - s->mspel, src_y - s->mspel,
442 s->h_edge_pos, v_edge_pos);
443 srcY = s->edge_emu_buffer;
444 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
445 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
446 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
447 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
450 /* if we deal with range reduction we need to scale source blocks */
451 if (v->rangeredfrm) {
456 for (j = 0; j < 17 + s->mspel * 2; j++) {
457 for (i = 0; i < 17 + s->mspel * 2; i++)
458 src[i] = ((src[i] - 128) >> 1) + 128;
463 for (j = 0; j < 9; j++) {
464 for (i = 0; i < 9; i++) {
465 src[i] = ((src[i] - 128) >> 1) + 128;
466 src2[i] = ((src2[i] - 128) >> 1) + 128;
468 src += s->uvlinesize;
469 src2 += s->uvlinesize;
472 /* if we deal with intensity compensation we need to scale source blocks */
473 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
478 for (j = 0; j < 17 + s->mspel * 2; j++) {
479 for (i = 0; i < 17 + s->mspel * 2; i++)
480 src[i] = v->luty[src[i]];
485 for (j = 0; j < 9; j++) {
486 for (i = 0; i < 9; i++) {
487 src[i] = v->lutuv[src[i]];
488 src2[i] = v->lutuv[src2[i]];
490 src += s->uvlinesize;
491 src2 += s->uvlinesize;
494 srcY += s->mspel * (1 + s->linesize);
497 if (v->field_mode && v->second_field) {
498 off = s->current_picture_ptr->f.linesize[0];
499 off_uv = s->current_picture_ptr->f.linesize[1];
505 dxy = ((my & 3) << 2) | (mx & 3);
506 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
507 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
508 srcY += s->linesize * 8;
509 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
510 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
511 } else { // hpel mc - always used for luma
512 dxy = (my & 2) | ((mx & 2) >> 1);
514 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
516 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
519 if (s->flags & CODEC_FLAG_GRAY) return;
520 /* Chroma MC always uses qpel bilinear */
521 uvmx = (uvmx & 3) << 1;
522 uvmy = (uvmy & 3) << 1;
524 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
525 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
527 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
528 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
532 static inline int median4(int a, int b, int c, int d)
535 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
536 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
538 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
539 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
543 /** Do motion compensation for 4-MV macroblock - luminance block
545 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
547 MpegEncContext *s = &v->s;
548 DSPContext *dsp = &v->s.dsp;
550 int dxy, mx, my, src_x, src_y;
552 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
553 int v_edge_pos = s->v_edge_pos >> v->field_mode;
555 if ((!v->field_mode ||
556 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
557 !v->s.last_picture.f.data[0])
560 mx = s->mv[dir][n][0];
561 my = s->mv[dir][n][1];
565 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
566 srcY = s->current_picture.f.data[0];
568 srcY = s->last_picture.f.data[0];
570 srcY = s->last_picture.f.data[0];
572 srcY = s->next_picture.f.data[0];
575 if (v->cur_field_type != v->ref_field_type[dir])
576 my = my - 2 + 4 * v->cur_field_type;
579 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
580 int same_count = 0, opp_count = 0, k;
581 int chosen_mv[2][4][2], f;
583 for (k = 0; k < 4; k++) {
584 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
585 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
586 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
590 f = opp_count > same_count;
591 switch (f ? opp_count : same_count) {
593 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
594 chosen_mv[f][2][0], chosen_mv[f][3][0]);
595 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
596 chosen_mv[f][2][1], chosen_mv[f][3][1]);
599 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
600 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
603 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
604 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
607 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
608 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
609 for (k = 0; k < 4; k++)
610 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
613 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
615 int width = s->avctx->coded_width;
616 int height = s->avctx->coded_height >> 1;
617 qx = (s->mb_x * 16) + (mx >> 2);
618 qy = (s->mb_y * 8) + (my >> 3);
623 mx -= 4 * (qx - width);
626 else if (qy > height + 1)
627 my -= 8 * (qy - height - 1);
630 if ((v->fcm == ILACE_FRAME) && fieldmv)
631 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
633 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
634 if (v->field_mode && v->second_field)
635 off += s->current_picture_ptr->f.linesize[0];
637 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
639 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
641 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
643 if (v->profile != PROFILE_ADVANCED) {
644 src_x = av_clip(src_x, -16, s->mb_width * 16);
645 src_y = av_clip(src_y, -16, s->mb_height * 16);
647 src_x = av_clip(src_x, -17, s->avctx->coded_width);
648 if (v->fcm == ILACE_FRAME) {
650 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
652 src_y = av_clip(src_y, -18, s->avctx->coded_height);
654 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
658 srcY += src_y * s->linesize + src_x;
659 if (v->field_mode && v->ref_field_type[dir])
660 srcY += s->current_picture_ptr->f.linesize[0];
662 if (fieldmv && !(src_y & 1))
664 if (fieldmv && (src_y & 1) && src_y < 4)
666 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
667 || s->h_edge_pos < 13 || v_edge_pos < 23
668 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
669 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
670 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
671 /* check emulate edge stride and offset */
672 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
673 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
674 src_x - s->mspel, src_y - (s->mspel << fieldmv),
675 s->h_edge_pos, v_edge_pos);
676 srcY = s->edge_emu_buffer;
677 /* if we deal with range reduction we need to scale source blocks */
678 if (v->rangeredfrm) {
683 for (j = 0; j < 9 + s->mspel * 2; j++) {
684 for (i = 0; i < 9 + s->mspel * 2; i++)
685 src[i] = ((src[i] - 128) >> 1) + 128;
686 src += s->linesize << fieldmv;
689 /* if we deal with intensity compensation we need to scale source blocks */
690 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
695 for (j = 0; j < 9 + s->mspel * 2; j++) {
696 for (i = 0; i < 9 + s->mspel * 2; i++)
697 src[i] = v->luty[src[i]];
698 src += s->linesize << fieldmv;
701 srcY += s->mspel * (1 + (s->linesize << fieldmv));
705 dxy = ((my & 3) << 2) | (mx & 3);
706 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
707 } else { // hpel mc - always used for luma
708 dxy = (my & 2) | ((mx & 2) >> 1);
710 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
712 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
716 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
719 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
721 idx = ((a[3] != flag) << 3)
722 | ((a[2] != flag) << 2)
723 | ((a[1] != flag) << 1)
726 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
727 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
729 } else if (count[idx] == 1) {
732 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
733 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
736 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
737 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
740 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
741 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
744 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
745 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
748 } else if (count[idx] == 2) {
750 for (i = 0; i < 3; i++)
755 for (i = t1 + 1; i < 4; i++)
760 *tx = (mvx[t1] + mvx[t2]) / 2;
761 *ty = (mvy[t1] + mvy[t2]) / 2;
769 /** Do motion compensation for 4-MV macroblock - both chroma blocks
771 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
773 MpegEncContext *s = &v->s;
774 DSPContext *dsp = &v->s.dsp;
775 uint8_t *srcU, *srcV;
776 int uvmx, uvmy, uvsrc_x, uvsrc_y;
777 int k, tx = 0, ty = 0;
778 int mvx[4], mvy[4], intra[4], mv_f[4];
780 int chroma_ref_type = v->cur_field_type, off = 0;
781 int v_edge_pos = s->v_edge_pos >> v->field_mode;
783 if (!v->field_mode && !v->s.last_picture.f.data[0])
785 if (s->flags & CODEC_FLAG_GRAY)
788 for (k = 0; k < 4; k++) {
789 mvx[k] = s->mv[dir][k][0];
790 mvy[k] = s->mv[dir][k][1];
791 intra[k] = v->mb_type[0][s->block_index[k]];
793 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
796 /* calculate chroma MV vector from four luma MVs */
797 if (!v->field_mode || (v->field_mode && !v->numref)) {
798 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
800 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
801 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
802 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
803 return; //no need to do MC for intra blocks
807 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
809 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
811 chroma_ref_type = !v->cur_field_type;
813 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
815 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
816 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
817 uvmx = (tx + ((tx & 3) == 3)) >> 1;
818 uvmy = (ty + ((ty & 3) == 3)) >> 1;
820 v->luma_mv[s->mb_x][0] = uvmx;
821 v->luma_mv[s->mb_x][1] = uvmy;
824 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
825 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
827 // Field conversion bias
828 if (v->cur_field_type != chroma_ref_type)
829 uvmy += 2 - 4 * chroma_ref_type;
831 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
832 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
834 if (v->profile != PROFILE_ADVANCED) {
835 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
836 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
838 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
839 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
844 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
845 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
846 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
848 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
849 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
852 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
853 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
856 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
857 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
861 if (chroma_ref_type) {
862 srcU += s->current_picture_ptr->f.linesize[1];
863 srcV += s->current_picture_ptr->f.linesize[2];
865 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
868 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
869 || s->h_edge_pos < 18 || v_edge_pos < 18
870 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
871 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
872 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
873 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
874 s->h_edge_pos >> 1, v_edge_pos >> 1);
875 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
876 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
877 s->h_edge_pos >> 1, v_edge_pos >> 1);
878 srcU = s->edge_emu_buffer;
879 srcV = s->edge_emu_buffer + 16;
881 /* if we deal with range reduction we need to scale source blocks */
882 if (v->rangeredfrm) {
888 for (j = 0; j < 9; j++) {
889 for (i = 0; i < 9; i++) {
890 src[i] = ((src[i] - 128) >> 1) + 128;
891 src2[i] = ((src2[i] - 128) >> 1) + 128;
893 src += s->uvlinesize;
894 src2 += s->uvlinesize;
897 /* if we deal with intensity compensation we need to scale source blocks */
898 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
904 for (j = 0; j < 9; j++) {
905 for (i = 0; i < 9; i++) {
906 src[i] = v->lutuv[src[i]];
907 src2[i] = v->lutuv[src2[i]];
909 src += s->uvlinesize;
910 src2 += s->uvlinesize;
915 /* Chroma MC always uses qpel bilinear */
916 uvmx = (uvmx & 3) << 1;
917 uvmy = (uvmy & 3) << 1;
919 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
920 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
922 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
923 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
927 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
929 static void vc1_mc_4mv_chroma4(VC1Context *v)
931 MpegEncContext *s = &v->s;
932 DSPContext *dsp = &v->s.dsp;
933 uint8_t *srcU, *srcV;
934 int uvsrc_x, uvsrc_y;
935 int uvmx_field[4], uvmy_field[4];
937 int fieldmv = v->blk_mv_type[s->block_index[0]];
938 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
939 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
940 int v_edge_pos = s->v_edge_pos >> 1;
942 if (!v->s.last_picture.f.data[0])
944 if (s->flags & CODEC_FLAG_GRAY)
947 for (i = 0; i < 4; i++) {
949 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
952 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
954 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
957 for (i = 0; i < 4; i++) {
958 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
959 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
960 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
961 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
962 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
963 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
964 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
965 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
966 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
967 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
969 if (fieldmv && !(uvsrc_y & 1))
971 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
973 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
974 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
975 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
976 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
977 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
978 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
979 s->h_edge_pos >> 1, v_edge_pos);
980 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
981 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
982 s->h_edge_pos >> 1, v_edge_pos);
983 srcU = s->edge_emu_buffer;
984 srcV = s->edge_emu_buffer + 16;
986 /* if we deal with intensity compensation we need to scale source blocks */
987 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
993 for (j = 0; j < 5; j++) {
994 for (i = 0; i < 5; i++) {
995 src[i] = v->lutuv[src[i]];
996 src2[i] = v->lutuv[src2[i]];
998 src += s->uvlinesize << 1;
999 src2 += s->uvlinesize << 1;
1004 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1005 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1007 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]);
1008 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]);
1013 /***********************************************************************/
1015 * @name VC-1 Block-level functions
1016 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1022 * @brief Get macroblock-level quantizer scale
1024 #define GET_MQUANT() \
1025 if (v->dquantfrm) { \
1027 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1028 if (v->dqbilevel) { \
1029 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1031 mqdiff = get_bits(gb, 3); \
1033 mquant = v->pq + mqdiff; \
1035 mquant = get_bits(gb, 5); \
1038 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1039 edges = 1 << v->dqsbedge; \
1040 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1041 edges = (3 << v->dqsbedge) % 15; \
1042 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1044 if ((edges&1) && !s->mb_x) \
1045 mquant = v->altpq; \
1046 if ((edges&2) && s->first_slice_line) \
1047 mquant = v->altpq; \
1048 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1049 mquant = v->altpq; \
1050 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1051 mquant = v->altpq; \
1052 if (!mquant || mquant > 31) { \
1053 av_log(v->s.avctx, AV_LOG_ERROR, \
1054 "Overriding invalid mquant %d\n", mquant); \
1060 * @def GET_MVDATA(_dmv_x, _dmv_y)
1061 * @brief Get MV differentials
1062 * @see MVDATA decoding from 8.3.5.2, p(1)20
1063 * @param _dmv_x Horizontal differential for decoded MV
1064 * @param _dmv_y Vertical differential for decoded MV
1066 #define GET_MVDATA(_dmv_x, _dmv_y) \
1067 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1068 VC1_MV_DIFF_VLC_BITS, 2); \
1070 mb_has_coeffs = 1; \
1073 mb_has_coeffs = 0; \
1076 _dmv_x = _dmv_y = 0; \
1077 } else if (index == 35) { \
1078 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1079 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1080 } else if (index == 36) { \
1085 index1 = index % 6; \
1086 if (!s->quarter_sample && index1 == 5) val = 1; \
1088 if (size_table[index1] - val > 0) \
1089 val = get_bits(gb, size_table[index1] - val); \
1091 sign = 0 - (val&1); \
1092 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1094 index1 = index / 6; \
1095 if (!s->quarter_sample && index1 == 5) val = 1; \
1097 if (size_table[index1] - val > 0) \
1098 val = get_bits(gb, size_table[index1] - val); \
1100 sign = 0 - (val & 1); \
1101 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1104 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1105 int *dmv_y, int *pred_flag)
1108 int extend_x = 0, extend_y = 0;
1109 GetBitContext *gb = &v->s.gb;
1112 const int* offs_tab;
1115 bits = VC1_2REF_MVDATA_VLC_BITS;
1118 bits = VC1_1REF_MVDATA_VLC_BITS;
1121 switch (v->dmvrange) {
1129 extend_x = extend_y = 1;
1132 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1134 *dmv_x = get_bits(gb, v->k_x);
1135 *dmv_y = get_bits(gb, v->k_y);
1137 *pred_flag = *dmv_y & 1;
1138 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1142 av_assert0(index < esc);
1144 offs_tab = offset_table2;
1146 offs_tab = offset_table1;
1147 index1 = (index + 1) % 9;
1149 val = get_bits(gb, index1 + extend_x);
1150 sign = 0 -(val & 1);
1151 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1155 offs_tab = offset_table2;
1157 offs_tab = offset_table1;
1158 index1 = (index + 1) / 9;
1159 if (index1 > v->numref) {
1160 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1161 sign = 0 - (val & 1);
1162 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1166 *pred_flag = index1 & 1;
1170 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1172 int scaledvalue, refdist;
1173 int scalesame1, scalesame2;
1174 int scalezone1_x, zone1offset_x;
1175 int table_index = dir ^ v->second_field;
1177 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1178 refdist = v->refdist;
1180 refdist = dir ? v->brfd : v->frfd;
1183 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1184 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1185 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1186 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1191 if (FFABS(n) < scalezone1_x)
1192 scaledvalue = (n * scalesame1) >> 8;
1195 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1197 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1200 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1203 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1205 int scaledvalue, refdist;
1206 int scalesame1, scalesame2;
1207 int scalezone1_y, zone1offset_y;
1208 int table_index = dir ^ v->second_field;
1210 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1211 refdist = v->refdist;
1213 refdist = dir ? v->brfd : v->frfd;
1216 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1217 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1218 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1219 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1224 if (FFABS(n) < scalezone1_y)
1225 scaledvalue = (n * scalesame1) >> 8;
1228 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1230 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1234 if (v->cur_field_type && !v->ref_field_type[dir])
1235 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1237 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1240 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1242 int scalezone1_x, zone1offset_x;
1243 int scaleopp1, scaleopp2, brfd;
1246 brfd = FFMIN(v->brfd, 3);
1247 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1248 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1249 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1250 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1255 if (FFABS(n) < scalezone1_x)
1256 scaledvalue = (n * scaleopp1) >> 8;
1259 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1261 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1264 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1267 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1269 int scalezone1_y, zone1offset_y;
1270 int scaleopp1, scaleopp2, brfd;
1273 brfd = FFMIN(v->brfd, 3);
1274 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1275 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1276 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1277 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1282 if (FFABS(n) < scalezone1_y)
1283 scaledvalue = (n * scaleopp1) >> 8;
1286 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1288 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1291 if (v->cur_field_type && !v->ref_field_type[dir]) {
1292 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1294 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1298 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1301 int brfd, scalesame;
1302 int hpel = 1 - v->s.quarter_sample;
1305 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1307 n = scaleforsame_y(v, i, n, dir) << hpel;
1309 n = scaleforsame_x(v, n, dir) << hpel;
1312 brfd = FFMIN(v->brfd, 3);
1313 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1315 n = (n * scalesame >> 8) << hpel;
1319 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1322 int refdist, scaleopp;
1323 int hpel = 1 - v->s.quarter_sample;
1326 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1328 n = scaleforopp_y(v, n, dir) << hpel;
1330 n = scaleforopp_x(v, n) << hpel;
1333 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1334 refdist = FFMIN(v->refdist, 3);
1336 refdist = dir ? v->brfd : v->frfd;
1337 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1339 n = (n * scaleopp >> 8) << hpel;
1343 /** Predict and set motion vector
1345 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1346 int mv1, int r_x, int r_y, uint8_t* is_intra,
1347 int pred_flag, int dir)
1349 MpegEncContext *s = &v->s;
1350 int xy, wrap, off = 0;
1354 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1355 int opposit, a_f, b_f, c_f;
1356 int16_t field_predA[2];
1357 int16_t field_predB[2];
1358 int16_t field_predC[2];
1359 int a_valid, b_valid, c_valid;
1360 int hybridmv_thresh, y_bias = 0;
1362 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1363 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1367 /* scale MV difference to be quad-pel */
1368 dmv_x <<= 1 - s->quarter_sample;
1369 dmv_y <<= 1 - s->quarter_sample;
1371 wrap = s->b8_stride;
1372 xy = s->block_index[n];
1375 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1376 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1377 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1378 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1379 if (mv1) { /* duplicate motion data for 1-MV block */
1380 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1381 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1382 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1383 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1384 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1385 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1386 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1387 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1388 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1389 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1390 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1391 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1392 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1397 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1398 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1400 if (v->field_mode && mixedmv_pic)
1401 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1403 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1405 //in 4-MV mode different blocks have different B predictor position
1408 off = (s->mb_x > 0) ? -1 : 1;
1411 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1420 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1422 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1423 b_valid = a_valid && (s->mb_width > 1);
1424 c_valid = s->mb_x || (n == 1 || n == 3);
1425 if (v->field_mode) {
1426 a_valid = a_valid && !is_intra[xy - wrap];
1427 b_valid = b_valid && !is_intra[xy - wrap + off];
1428 c_valid = c_valid && !is_intra[xy - 1];
1432 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1433 num_oppfield += a_f;
1434 num_samefield += 1 - a_f;
1435 field_predA[0] = A[0];
1436 field_predA[1] = A[1];
1438 field_predA[0] = field_predA[1] = 0;
1442 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1443 num_oppfield += b_f;
1444 num_samefield += 1 - b_f;
1445 field_predB[0] = B[0];
1446 field_predB[1] = B[1];
1448 field_predB[0] = field_predB[1] = 0;
1452 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1453 num_oppfield += c_f;
1454 num_samefield += 1 - c_f;
1455 field_predC[0] = C[0];
1456 field_predC[1] = C[1];
1458 field_predC[0] = field_predC[1] = 0;
1462 if (v->field_mode) {
1463 if (num_samefield <= num_oppfield)
1464 opposit = 1 - pred_flag;
1466 opposit = pred_flag;
1470 if (a_valid && !a_f) {
1471 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1472 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1474 if (b_valid && !b_f) {
1475 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1476 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1478 if (c_valid && !c_f) {
1479 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1480 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1482 v->mv_f[dir][xy + v->blocks_off] = 1;
1483 v->ref_field_type[dir] = !v->cur_field_type;
1485 if (a_valid && a_f) {
1486 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1487 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1489 if (b_valid && b_f) {
1490 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1491 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1493 if (c_valid && c_f) {
1494 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1495 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1497 v->mv_f[dir][xy + v->blocks_off] = 0;
1498 v->ref_field_type[dir] = v->cur_field_type;
1502 px = field_predA[0];
1503 py = field_predA[1];
1504 } else if (c_valid) {
1505 px = field_predC[0];
1506 py = field_predC[1];
1507 } else if (b_valid) {
1508 px = field_predB[0];
1509 py = field_predB[1];
1515 if (num_samefield + num_oppfield > 1) {
1516 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1517 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1520 /* Pullback MV as specified in 8.3.5.3.4 */
1521 if (!v->field_mode) {
1523 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1524 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1525 X = (s->mb_width << 6) - 4;
1526 Y = (s->mb_height << 6) - 4;
1528 if (qx + px < -60) px = -60 - qx;
1529 if (qy + py < -60) py = -60 - qy;
1531 if (qx + px < -28) px = -28 - qx;
1532 if (qy + py < -28) py = -28 - qy;
1534 if (qx + px > X) px = X - qx;
1535 if (qy + py > Y) py = Y - qy;
1538 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1539 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1540 hybridmv_thresh = 32;
1541 if (a_valid && c_valid) {
1542 if (is_intra[xy - wrap])
1543 sum = FFABS(px) + FFABS(py);
1545 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1546 if (sum > hybridmv_thresh) {
1547 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1548 px = field_predA[0];
1549 py = field_predA[1];
1551 px = field_predC[0];
1552 py = field_predC[1];
1555 if (is_intra[xy - 1])
1556 sum = FFABS(px) + FFABS(py);
1558 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1559 if (sum > hybridmv_thresh) {
1560 if (get_bits1(&s->gb)) {
1561 px = field_predA[0];
1562 py = field_predA[1];
1564 px = field_predC[0];
1565 py = field_predC[1];
1572 if (v->field_mode && !s->quarter_sample) {
1576 if (v->field_mode && v->numref)
1578 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1580 /* store MV using signed modulus of MV range defined in 4.11 */
1581 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;
1582 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;
1583 if (mv1) { /* duplicate motion data for 1-MV block */
1584 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];
1585 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];
1586 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];
1587 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];
1588 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];
1589 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];
1590 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1591 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];
1595 /** Predict and set motion vector for interlaced frame picture MBs
1597 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1598 int mvn, int r_x, int r_y, uint8_t* is_intra)
1600 MpegEncContext *s = &v->s;
1601 int xy, wrap, off = 0;
1602 int A[2], B[2], C[2];
1604 int a_valid = 0, b_valid = 0, c_valid = 0;
1605 int field_a, field_b, field_c; // 0: same, 1: opposit
1606 int total_valid, num_samefield, num_oppfield;
1607 int pos_c, pos_b, n_adj;
1609 wrap = s->b8_stride;
1610 xy = s->block_index[n];
1613 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1614 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1615 s->current_picture.f.motion_val[1][xy][0] = 0;
1616 s->current_picture.f.motion_val[1][xy][1] = 0;
1617 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1618 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1619 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1620 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1621 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1622 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1623 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1624 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1625 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1626 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1627 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1628 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1629 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1630 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1635 off = ((n == 0) || (n == 1)) ? 1 : -1;
1637 if (s->mb_x || (n == 1) || (n == 3)) {
1638 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1639 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1640 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1641 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1643 } else { // current block has frame mv and cand. has field MV (so average)
1644 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1645 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1646 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1647 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1650 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1656 /* Predict B and C */
1657 B[0] = B[1] = C[0] = C[1] = 0;
1658 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1659 if (!s->first_slice_line) {
1660 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1663 pos_b = s->block_index[n_adj] - 2 * wrap;
1664 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1665 n_adj = (n & 2) | (n & 1);
1667 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1668 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1669 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1670 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1671 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1674 if (s->mb_width > 1) {
1675 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1678 pos_c = s->block_index[2] - 2 * wrap + 2;
1679 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1682 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1683 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1684 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1685 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1686 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1688 if (s->mb_x == s->mb_width - 1) {
1689 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1692 pos_c = s->block_index[3] - 2 * wrap - 2;
1693 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1696 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1697 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1698 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1699 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1700 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1709 pos_b = s->block_index[1];
1711 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1712 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1713 pos_c = s->block_index[0];
1715 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1716 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1719 total_valid = a_valid + b_valid + c_valid;
1720 // check if predictor A is out of bounds
1721 if (!s->mb_x && !(n == 1 || n == 3)) {
1724 // check if predictor B is out of bounds
1725 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1726 B[0] = B[1] = C[0] = C[1] = 0;
1728 if (!v->blk_mv_type[xy]) {
1729 if (s->mb_width == 1) {
1733 if (total_valid >= 2) {
1734 px = mid_pred(A[0], B[0], C[0]);
1735 py = mid_pred(A[1], B[1], C[1]);
1736 } else if (total_valid) {
1737 if (a_valid) { px = A[0]; py = A[1]; }
1738 if (b_valid) { px = B[0]; py = B[1]; }
1739 if (c_valid) { px = C[0]; py = C[1]; }
1745 field_a = (A[1] & 4) ? 1 : 0;
1749 field_b = (B[1] & 4) ? 1 : 0;
1753 field_c = (C[1] & 4) ? 1 : 0;
1757 num_oppfield = field_a + field_b + field_c;
1758 num_samefield = total_valid - num_oppfield;
1759 if (total_valid == 3) {
1760 if ((num_samefield == 3) || (num_oppfield == 3)) {
1761 px = mid_pred(A[0], B[0], C[0]);
1762 py = mid_pred(A[1], B[1], C[1]);
1763 } else if (num_samefield >= num_oppfield) {
1764 /* take one MV from same field set depending on priority
1765 the check for B may not be necessary */
1766 px = !field_a ? A[0] : B[0];
1767 py = !field_a ? A[1] : B[1];
1769 px = field_a ? A[0] : B[0];
1770 py = field_a ? A[1] : B[1];
1772 } else if (total_valid == 2) {
1773 if (num_samefield >= num_oppfield) {
1774 if (!field_a && a_valid) {
1777 } else if (!field_b && b_valid) {
1780 } else if (c_valid) {
1785 if (field_a && a_valid) {
1788 } else if (field_b && b_valid) {
1791 } else if (c_valid) {
1796 } else if (total_valid == 1) {
1797 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1798 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1803 /* store MV using signed modulus of MV range defined in 4.11 */
1804 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;
1805 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;
1806 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1807 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1808 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1809 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1810 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1811 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1812 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1813 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1814 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1815 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1816 s->mv[0][n + 1][0] = s->mv[0][n][0];
1817 s->mv[0][n + 1][1] = s->mv[0][n][1];
1821 /** Motion compensation for direct or interpolated blocks in B-frames
1823 static void vc1_interp_mc(VC1Context *v)
1825 MpegEncContext *s = &v->s;
1826 DSPContext *dsp = &v->s.dsp;
1827 uint8_t *srcY, *srcU, *srcV;
1828 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1830 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1832 if (!v->field_mode && !v->s.next_picture.f.data[0])
1835 mx = s->mv[1][0][0];
1836 my = s->mv[1][0][1];
1837 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1838 uvmy = (my + ((my & 3) == 3)) >> 1;
1839 if (v->field_mode) {
1840 if (v->cur_field_type != v->ref_field_type[1])
1841 my = my - 2 + 4 * v->cur_field_type;
1842 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1845 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1846 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1848 srcY = s->next_picture.f.data[0];
1849 srcU = s->next_picture.f.data[1];
1850 srcV = s->next_picture.f.data[2];
1852 src_x = s->mb_x * 16 + (mx >> 2);
1853 src_y = s->mb_y * 16 + (my >> 2);
1854 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1855 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1857 if (v->profile != PROFILE_ADVANCED) {
1858 src_x = av_clip( src_x, -16, s->mb_width * 16);
1859 src_y = av_clip( src_y, -16, s->mb_height * 16);
1860 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1861 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1863 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1864 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1865 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1866 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1869 srcY += src_y * s->linesize + src_x;
1870 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1871 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1873 if (v->field_mode && v->ref_field_type[1]) {
1874 srcY += s->current_picture_ptr->f.linesize[0];
1875 srcU += s->current_picture_ptr->f.linesize[1];
1876 srcV += s->current_picture_ptr->f.linesize[2];
1879 /* for grayscale we should not try to read from unknown area */
1880 if (s->flags & CODEC_FLAG_GRAY) {
1881 srcU = s->edge_emu_buffer + 18 * s->linesize;
1882 srcV = s->edge_emu_buffer + 18 * s->linesize;
1885 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1886 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 16 - s->mspel * 3
1887 || (unsigned)(src_y - s->mspel) > v_edge_pos - (my & 3) - 16 - s->mspel * 3) {
1888 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1890 srcY -= s->mspel * (1 + s->linesize);
1891 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1892 17 + s->mspel * 2, 17 + s->mspel * 2,
1893 src_x - s->mspel, src_y - s->mspel,
1894 s->h_edge_pos, v_edge_pos);
1895 srcY = s->edge_emu_buffer;
1896 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1897 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1898 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1899 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1902 /* if we deal with range reduction we need to scale source blocks */
1903 if (v->rangeredfrm) {
1905 uint8_t *src, *src2;
1908 for (j = 0; j < 17 + s->mspel * 2; j++) {
1909 for (i = 0; i < 17 + s->mspel * 2; i++)
1910 src[i] = ((src[i] - 128) >> 1) + 128;
1915 for (j = 0; j < 9; j++) {
1916 for (i = 0; i < 9; i++) {
1917 src[i] = ((src[i] - 128) >> 1) + 128;
1918 src2[i] = ((src2[i] - 128) >> 1) + 128;
1920 src += s->uvlinesize;
1921 src2 += s->uvlinesize;
1924 srcY += s->mspel * (1 + s->linesize);
1927 if (v->field_mode && v->second_field) {
1928 off = s->current_picture_ptr->f.linesize[0];
1929 off_uv = s->current_picture_ptr->f.linesize[1];
1936 dxy = ((my & 3) << 2) | (mx & 3);
1937 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1938 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1939 srcY += s->linesize * 8;
1940 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1941 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1943 dxy = (my & 2) | ((mx & 2) >> 1);
1946 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1948 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1951 if (s->flags & CODEC_FLAG_GRAY) return;
1952 /* Chroma MC always uses qpel blilinear */
1953 uvmx = (uvmx & 3) << 1;
1954 uvmy = (uvmy & 3) << 1;
1956 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1957 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1959 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1960 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1964 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1968 #if B_FRACTION_DEN==256
1972 return 2 * ((value * n + 255) >> 9);
1973 return (value * n + 128) >> 8;
1976 n -= B_FRACTION_DEN;
1978 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1979 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1983 static av_always_inline int scale_mv_intfi(int value, int bfrac, int inv,
1984 int qs, int qs_last)
1992 return (value * n + 255) >> 9;
1994 return (value * n + 128) >> 8;
1997 /** Reconstruct motion vector for B-frame and do motion compensation
1999 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2000 int direct, int mode)
2003 v->mv_mode2 = v->mv_mode;
2004 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2010 v->mv_mode = v->mv_mode2;
2013 if (mode == BMV_TYPE_INTERPOLATED) {
2017 v->mv_mode = v->mv_mode2;
2021 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2022 v->mv_mode = v->mv_mode2;
2023 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2025 v->mv_mode = v->mv_mode2;
2028 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2029 int direct, int mvtype)
2031 MpegEncContext *s = &v->s;
2032 int xy, wrap, off = 0;
2037 const uint8_t *is_intra = v->mb_type[0];
2041 /* scale MV difference to be quad-pel */
2042 dmv_x[0] <<= 1 - s->quarter_sample;
2043 dmv_y[0] <<= 1 - s->quarter_sample;
2044 dmv_x[1] <<= 1 - s->quarter_sample;
2045 dmv_y[1] <<= 1 - s->quarter_sample;
2047 wrap = s->b8_stride;
2048 xy = s->block_index[0];
2051 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2052 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2053 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2054 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2057 if (!v->field_mode) {
2058 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2059 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2060 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2061 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2063 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2064 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));
2065 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));
2066 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));
2067 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));
2070 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2071 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2072 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2073 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2077 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2078 C = s->current_picture.f.motion_val[0][xy - 2];
2079 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2080 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2081 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2083 if (!s->mb_x) C[0] = C[1] = 0;
2084 if (!s->first_slice_line) { // predictor A is not out of bounds
2085 if (s->mb_width == 1) {
2089 px = mid_pred(A[0], B[0], C[0]);
2090 py = mid_pred(A[1], B[1], C[1]);
2092 } else if (s->mb_x) { // predictor C is not out of bounds
2098 /* Pullback MV as specified in 8.3.5.3.4 */
2101 if (v->profile < PROFILE_ADVANCED) {
2102 qx = (s->mb_x << 5);
2103 qy = (s->mb_y << 5);
2104 X = (s->mb_width << 5) - 4;
2105 Y = (s->mb_height << 5) - 4;
2106 if (qx + px < -28) px = -28 - qx;
2107 if (qy + py < -28) py = -28 - qy;
2108 if (qx + px > X) px = X - qx;
2109 if (qy + py > Y) py = Y - qy;
2111 qx = (s->mb_x << 6);
2112 qy = (s->mb_y << 6);
2113 X = (s->mb_width << 6) - 4;
2114 Y = (s->mb_height << 6) - 4;
2115 if (qx + px < -60) px = -60 - qx;
2116 if (qy + py < -60) py = -60 - qy;
2117 if (qx + px > X) px = X - qx;
2118 if (qy + py > Y) py = Y - qy;
2121 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2122 if (0 && !s->first_slice_line && s->mb_x) {
2123 if (is_intra[xy - wrap])
2124 sum = FFABS(px) + FFABS(py);
2126 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2128 if (get_bits1(&s->gb)) {
2136 if (is_intra[xy - 2])
2137 sum = FFABS(px) + FFABS(py);
2139 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2141 if (get_bits1(&s->gb)) {
2151 /* store MV using signed modulus of MV range defined in 4.11 */
2152 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2153 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2155 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2156 C = s->current_picture.f.motion_val[1][xy - 2];
2157 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2158 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2159 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2163 if (!s->first_slice_line) { // predictor A is not out of bounds
2164 if (s->mb_width == 1) {
2168 px = mid_pred(A[0], B[0], C[0]);
2169 py = mid_pred(A[1], B[1], C[1]);
2171 } else if (s->mb_x) { // predictor C is not out of bounds
2177 /* Pullback MV as specified in 8.3.5.3.4 */
2180 if (v->profile < PROFILE_ADVANCED) {
2181 qx = (s->mb_x << 5);
2182 qy = (s->mb_y << 5);
2183 X = (s->mb_width << 5) - 4;
2184 Y = (s->mb_height << 5) - 4;
2185 if (qx + px < -28) px = -28 - qx;
2186 if (qy + py < -28) py = -28 - qy;
2187 if (qx + px > X) px = X - qx;
2188 if (qy + py > Y) py = Y - qy;
2190 qx = (s->mb_x << 6);
2191 qy = (s->mb_y << 6);
2192 X = (s->mb_width << 6) - 4;
2193 Y = (s->mb_height << 6) - 4;
2194 if (qx + px < -60) px = -60 - qx;
2195 if (qy + py < -60) py = -60 - qy;
2196 if (qx + px > X) px = X - qx;
2197 if (qy + py > Y) py = Y - qy;
2200 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2201 if (0 && !s->first_slice_line && s->mb_x) {
2202 if (is_intra[xy - wrap])
2203 sum = FFABS(px) + FFABS(py);
2205 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2207 if (get_bits1(&s->gb)) {
2215 if (is_intra[xy - 2])
2216 sum = FFABS(px) + FFABS(py);
2218 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2220 if (get_bits1(&s->gb)) {
2230 /* store MV using signed modulus of MV range defined in 4.11 */
2232 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2233 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2235 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2236 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2237 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2238 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2241 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2243 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2244 MpegEncContext *s = &v->s;
2245 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2247 if (v->bmvtype == BMV_TYPE_DIRECT) {
2248 int total_opp, k, f;
2249 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2250 s->mv[0][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2251 v->bfraction, 0, s->quarter_sample, v->qs_last);
2252 s->mv[0][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2253 v->bfraction, 0, s->quarter_sample, v->qs_last);
2254 s->mv[1][0][0] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2255 v->bfraction, 1, s->quarter_sample, v->qs_last);
2256 s->mv[1][0][1] = scale_mv_intfi(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2257 v->bfraction, 1, s->quarter_sample, v->qs_last);
2259 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2260 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2261 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2262 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2263 f = (total_opp > 2) ? 1 : 0;
2265 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2266 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2269 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2270 for (k = 0; k < 4; k++) {
2271 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2272 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2273 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2274 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2275 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2276 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2280 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2281 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);
2282 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);
2285 if (dir) { // backward
2286 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);
2287 if (n == 3 || mv1) {
2288 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2291 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);
2292 if (n == 3 || mv1) {
2293 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2298 /** Get predicted DC value for I-frames only
2299 * prediction dir: left=0, top=1
2300 * @param s MpegEncContext
2301 * @param overlap flag indicating that overlap filtering is used
2302 * @param pq integer part of picture quantizer
2303 * @param[in] n block index in the current MB
2304 * @param dc_val_ptr Pointer to DC predictor
2305 * @param dir_ptr Prediction direction for use in AC prediction
2307 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2308 int16_t **dc_val_ptr, int *dir_ptr)
2310 int a, b, c, wrap, pred, scale;
2312 static const uint16_t dcpred[32] = {
2313 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2314 114, 102, 93, 85, 79, 73, 68, 64,
2315 60, 57, 54, 51, 49, 47, 45, 43,
2316 41, 39, 38, 37, 35, 34, 33
2319 /* find prediction - wmv3_dc_scale always used here in fact */
2320 if (n < 4) scale = s->y_dc_scale;
2321 else scale = s->c_dc_scale;
2323 wrap = s->block_wrap[n];
2324 dc_val = s->dc_val[0] + s->block_index[n];
2330 b = dc_val[ - 1 - wrap];
2331 a = dc_val[ - wrap];
2333 if (pq < 9 || !overlap) {
2334 /* Set outer values */
2335 if (s->first_slice_line && (n != 2 && n != 3))
2336 b = a = dcpred[scale];
2337 if (s->mb_x == 0 && (n != 1 && n != 3))
2338 b = c = dcpred[scale];
2340 /* Set outer values */
2341 if (s->first_slice_line && (n != 2 && n != 3))
2343 if (s->mb_x == 0 && (n != 1 && n != 3))
2347 if (abs(a - b) <= abs(b - c)) {
2349 *dir_ptr = 1; // left
2352 *dir_ptr = 0; // top
2355 /* update predictor */
2356 *dc_val_ptr = &dc_val[0];
2361 /** Get predicted DC value
2362 * prediction dir: left=0, top=1
2363 * @param s MpegEncContext
2364 * @param overlap flag indicating that overlap filtering is used
2365 * @param pq integer part of picture quantizer
2366 * @param[in] n block index in the current MB
2367 * @param a_avail flag indicating top block availability
2368 * @param c_avail flag indicating left block availability
2369 * @param dc_val_ptr Pointer to DC predictor
2370 * @param dir_ptr Prediction direction for use in AC prediction
2372 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2373 int a_avail, int c_avail,
2374 int16_t **dc_val_ptr, int *dir_ptr)
2376 int a, b, c, wrap, pred;
2378 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2382 wrap = s->block_wrap[n];
2383 dc_val = s->dc_val[0] + s->block_index[n];
2389 b = dc_val[ - 1 - wrap];
2390 a = dc_val[ - wrap];
2391 /* scale predictors if needed */
2392 q1 = s->current_picture.f.qscale_table[mb_pos];
2393 dqscale_index = s->y_dc_scale_table[q1] - 1;
2394 if (dqscale_index < 0)
2396 if (c_avail && (n != 1 && n != 3)) {
2397 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2399 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2401 if (a_avail && (n != 2 && n != 3)) {
2402 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2404 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2406 if (a_avail && c_avail && (n != 3)) {
2411 off -= s->mb_stride;
2412 q2 = s->current_picture.f.qscale_table[off];
2414 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2417 if (a_avail && c_avail) {
2418 if (abs(a - b) <= abs(b - c)) {
2420 *dir_ptr = 1; // left
2423 *dir_ptr = 0; // top
2425 } else if (a_avail) {
2427 *dir_ptr = 0; // top
2428 } else if (c_avail) {
2430 *dir_ptr = 1; // left
2433 *dir_ptr = 1; // left
2436 /* update predictor */
2437 *dc_val_ptr = &dc_val[0];
2441 /** @} */ // Block group
2444 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2445 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2449 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2450 uint8_t **coded_block_ptr)
2452 int xy, wrap, pred, a, b, c;
2454 xy = s->block_index[n];
2455 wrap = s->b8_stride;
2460 a = s->coded_block[xy - 1 ];
2461 b = s->coded_block[xy - 1 - wrap];
2462 c = s->coded_block[xy - wrap];
2471 *coded_block_ptr = &s->coded_block[xy];
2477 * Decode one AC coefficient
2478 * @param v The VC1 context
2479 * @param last Last coefficient
2480 * @param skip How much zero coefficients to skip
2481 * @param value Decoded AC coefficient value
2482 * @param codingset set of VLC to decode data
2485 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2486 int *value, int codingset)
2488 GetBitContext *gb = &v->s.gb;
2489 int index, escape, run = 0, level = 0, lst = 0;
2491 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2492 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2493 run = vc1_index_decode_table[codingset][index][0];
2494 level = vc1_index_decode_table[codingset][index][1];
2495 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2499 escape = decode210(gb);
2501 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2502 run = vc1_index_decode_table[codingset][index][0];
2503 level = vc1_index_decode_table[codingset][index][1];
2504 lst = index >= vc1_last_decode_table[codingset];
2507 level += vc1_last_delta_level_table[codingset][run];
2509 level += vc1_delta_level_table[codingset][run];
2512 run += vc1_last_delta_run_table[codingset][level] + 1;
2514 run += vc1_delta_run_table[codingset][level] + 1;
2520 lst = get_bits1(gb);
2521 if (v->s.esc3_level_length == 0) {
2522 if (v->pq < 8 || v->dquantfrm) { // table 59
2523 v->s.esc3_level_length = get_bits(gb, 3);
2524 if (!v->s.esc3_level_length)
2525 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2526 } else { // table 60
2527 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2529 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2531 run = get_bits(gb, v->s.esc3_run_length);
2532 sign = get_bits1(gb);
2533 level = get_bits(gb, v->s.esc3_level_length);
2544 /** Decode intra block in intra frames - should be faster than decode_intra_block
2545 * @param v VC1Context
2546 * @param block block to decode
2547 * @param[in] n subblock index
2548 * @param coded are AC coeffs present or not
2549 * @param codingset set of VLC to decode data
2551 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2552 int coded, int codingset)
2554 GetBitContext *gb = &v->s.gb;
2555 MpegEncContext *s = &v->s;
2556 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2559 int16_t *ac_val, *ac_val2;
2562 /* Get DC differential */
2564 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2566 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2569 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2573 if (dcdiff == 119 /* ESC index value */) {
2574 /* TODO: Optimize */
2575 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2576 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2577 else dcdiff = get_bits(gb, 8);
2580 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2581 else if (v->pq == 2)
2582 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2589 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2592 /* Store the quantized DC coeff, used for prediction */
2594 block[0] = dcdiff * s->y_dc_scale;
2596 block[0] = dcdiff * s->c_dc_scale;
2607 int last = 0, skip, value;
2608 const uint8_t *zz_table;
2612 scale = v->pq * 2 + v->halfpq;
2616 zz_table = v->zz_8x8[2];
2618 zz_table = v->zz_8x8[3];
2620 zz_table = v->zz_8x8[1];
2622 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2624 if (dc_pred_dir) // left
2627 ac_val -= 16 * s->block_wrap[n];
2630 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2634 block[zz_table[i++]] = value;
2637 /* apply AC prediction if needed */
2639 if (dc_pred_dir) { // left
2640 for (k = 1; k < 8; k++)
2641 block[k << v->left_blk_sh] += ac_val[k];
2643 for (k = 1; k < 8; k++)
2644 block[k << v->top_blk_sh] += ac_val[k + 8];
2647 /* save AC coeffs for further prediction */
2648 for (k = 1; k < 8; k++) {
2649 ac_val2[k] = block[k << v->left_blk_sh];
2650 ac_val2[k + 8] = block[k << v->top_blk_sh];
2653 /* scale AC coeffs */
2654 for (k = 1; k < 64; k++)
2658 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2661 if (s->ac_pred) i = 63;
2667 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2671 scale = v->pq * 2 + v->halfpq;
2672 memset(ac_val2, 0, 16 * 2);
2673 if (dc_pred_dir) { // left
2676 memcpy(ac_val2, ac_val, 8 * 2);
2678 ac_val -= 16 * s->block_wrap[n];
2680 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2683 /* apply AC prediction if needed */
2685 if (dc_pred_dir) { //left
2686 for (k = 1; k < 8; k++) {
2687 block[k << v->left_blk_sh] = ac_val[k] * scale;
2688 if (!v->pquantizer && block[k << v->left_blk_sh])
2689 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2692 for (k = 1; k < 8; k++) {
2693 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2694 if (!v->pquantizer && block[k << v->top_blk_sh])
2695 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2701 s->block_last_index[n] = i;
2706 /** Decode intra block in intra frames - should be faster than decode_intra_block
2707 * @param v VC1Context
2708 * @param block block to decode
2709 * @param[in] n subblock number
2710 * @param coded are AC coeffs present or not
2711 * @param codingset set of VLC to decode data
2712 * @param mquant quantizer value for this macroblock
2714 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2715 int coded, int codingset, int mquant)
2717 GetBitContext *gb = &v->s.gb;
2718 MpegEncContext *s = &v->s;
2719 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2722 int16_t *ac_val, *ac_val2;
2724 int a_avail = v->a_avail, c_avail = v->c_avail;
2725 int use_pred = s->ac_pred;
2728 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2730 /* Get DC differential */
2732 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2734 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2737 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2741 if (dcdiff == 119 /* ESC index value */) {
2742 /* TODO: Optimize */
2743 if (mquant == 1) dcdiff = get_bits(gb, 10);
2744 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2745 else dcdiff = get_bits(gb, 8);
2748 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2749 else if (mquant == 2)
2750 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2757 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2760 /* Store the quantized DC coeff, used for prediction */
2762 block[0] = dcdiff * s->y_dc_scale;
2764 block[0] = dcdiff * s->c_dc_scale;
2770 /* check if AC is needed at all */
2771 if (!a_avail && !c_avail)
2773 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2776 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2778 if (dc_pred_dir) // left
2781 ac_val -= 16 * s->block_wrap[n];
2783 q1 = s->current_picture.f.qscale_table[mb_pos];
2784 if ( dc_pred_dir && c_avail && mb_pos)
2785 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2786 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2787 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2788 if ( dc_pred_dir && n == 1)
2790 if (!dc_pred_dir && n == 2)
2796 int last = 0, skip, value;
2797 const uint8_t *zz_table;
2801 if (!use_pred && v->fcm == ILACE_FRAME) {
2802 zz_table = v->zzi_8x8;
2804 if (!dc_pred_dir) // top
2805 zz_table = v->zz_8x8[2];
2807 zz_table = v->zz_8x8[3];
2810 if (v->fcm != ILACE_FRAME)
2811 zz_table = v->zz_8x8[1];
2813 zz_table = v->zzi_8x8;
2817 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2821 block[zz_table[i++]] = value;
2824 /* apply AC prediction if needed */
2826 /* scale predictors if needed*/
2827 if (q2 && q1 != q2) {
2828 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2829 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2832 return AVERROR_INVALIDDATA;
2833 if (dc_pred_dir) { // left
2834 for (k = 1; k < 8; k++)
2835 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2837 for (k = 1; k < 8; k++)
2838 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2841 if (dc_pred_dir) { //left
2842 for (k = 1; k < 8; k++)
2843 block[k << v->left_blk_sh] += ac_val[k];
2845 for (k = 1; k < 8; k++)
2846 block[k << v->top_blk_sh] += ac_val[k + 8];
2850 /* save AC coeffs for further prediction */
2851 for (k = 1; k < 8; k++) {
2852 ac_val2[k ] = block[k << v->left_blk_sh];
2853 ac_val2[k + 8] = block[k << v->top_blk_sh];
2856 /* scale AC coeffs */
2857 for (k = 1; k < 64; k++)
2861 block[k] += (block[k] < 0) ? -mquant : mquant;
2864 if (use_pred) i = 63;
2865 } else { // no AC coeffs
2868 memset(ac_val2, 0, 16 * 2);
2869 if (dc_pred_dir) { // left
2871 memcpy(ac_val2, ac_val, 8 * 2);
2872 if (q2 && q1 != q2) {
2873 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2874 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2876 return AVERROR_INVALIDDATA;
2877 for (k = 1; k < 8; k++)
2878 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2883 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2884 if (q2 && q1 != q2) {
2885 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2886 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2888 return AVERROR_INVALIDDATA;
2889 for (k = 1; k < 8; k++)
2890 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2895 /* apply AC prediction if needed */
2897 if (dc_pred_dir) { // left
2898 for (k = 1; k < 8; k++) {
2899 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2900 if (!v->pquantizer && block[k << v->left_blk_sh])
2901 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2904 for (k = 1; k < 8; k++) {
2905 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2906 if (!v->pquantizer && block[k << v->top_blk_sh])
2907 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2913 s->block_last_index[n] = i;
2918 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2919 * @param v VC1Context
2920 * @param block block to decode
2921 * @param[in] n subblock index
2922 * @param coded are AC coeffs present or not
2923 * @param mquant block quantizer
2924 * @param codingset set of VLC to decode data
2926 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2927 int coded, int mquant, int codingset)
2929 GetBitContext *gb = &v->s.gb;
2930 MpegEncContext *s = &v->s;
2931 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2934 int16_t *ac_val, *ac_val2;
2936 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2937 int a_avail = v->a_avail, c_avail = v->c_avail;
2938 int use_pred = s->ac_pred;
2942 s->dsp.clear_block(block);
2944 /* XXX: Guard against dumb values of mquant */
2945 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2947 /* Set DC scale - y and c use the same */
2948 s->y_dc_scale = s->y_dc_scale_table[mquant];
2949 s->c_dc_scale = s->c_dc_scale_table[mquant];
2951 /* Get DC differential */
2953 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2955 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2958 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2962 if (dcdiff == 119 /* ESC index value */) {
2963 /* TODO: Optimize */
2964 if (mquant == 1) dcdiff = get_bits(gb, 10);
2965 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2966 else dcdiff = get_bits(gb, 8);
2969 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2970 else if (mquant == 2)
2971 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2978 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2981 /* Store the quantized DC coeff, used for prediction */
2984 block[0] = dcdiff * s->y_dc_scale;
2986 block[0] = dcdiff * s->c_dc_scale;
2992 /* check if AC is needed at all and adjust direction if needed */
2993 if (!a_avail) dc_pred_dir = 1;
2994 if (!c_avail) dc_pred_dir = 0;
2995 if (!a_avail && !c_avail) use_pred = 0;
2996 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2999 scale = mquant * 2 + v->halfpq;
3001 if (dc_pred_dir) //left
3004 ac_val -= 16 * s->block_wrap[n];
3006 q1 = s->current_picture.f.qscale_table[mb_pos];
3007 if (dc_pred_dir && c_avail && mb_pos)
3008 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3009 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3010 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3011 if ( dc_pred_dir && n == 1)
3013 if (!dc_pred_dir && n == 2)
3015 if (n == 3) q2 = q1;
3018 int last = 0, skip, value;
3022 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3026 if (v->fcm == PROGRESSIVE)
3027 block[v->zz_8x8[0][i++]] = value;
3029 if (use_pred && (v->fcm == ILACE_FRAME)) {
3030 if (!dc_pred_dir) // top
3031 block[v->zz_8x8[2][i++]] = value;
3033 block[v->zz_8x8[3][i++]] = value;
3035 block[v->zzi_8x8[i++]] = value;
3040 /* apply AC prediction if needed */
3042 /* scale predictors if needed*/
3043 if (q2 && q1 != q2) {
3044 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3045 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3048 return AVERROR_INVALIDDATA;
3049 if (dc_pred_dir) { // left
3050 for (k = 1; k < 8; k++)
3051 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3053 for (k = 1; k < 8; k++)
3054 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3057 if (dc_pred_dir) { // left
3058 for (k = 1; k < 8; k++)
3059 block[k << v->left_blk_sh] += ac_val[k];
3061 for (k = 1; k < 8; k++)
3062 block[k << v->top_blk_sh] += ac_val[k + 8];
3066 /* save AC coeffs for further prediction */
3067 for (k = 1; k < 8; k++) {
3068 ac_val2[k ] = block[k << v->left_blk_sh];
3069 ac_val2[k + 8] = block[k << v->top_blk_sh];
3072 /* scale AC coeffs */
3073 for (k = 1; k < 64; k++)
3077 block[k] += (block[k] < 0) ? -mquant : mquant;
3080 if (use_pred) i = 63;
3081 } else { // no AC coeffs
3084 memset(ac_val2, 0, 16 * 2);
3085 if (dc_pred_dir) { // left
3087 memcpy(ac_val2, ac_val, 8 * 2);
3088 if (q2 && q1 != q2) {
3089 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3090 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3092 return AVERROR_INVALIDDATA;
3093 for (k = 1; k < 8; k++)
3094 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3099 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3100 if (q2 && q1 != q2) {
3101 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3102 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3104 return AVERROR_INVALIDDATA;
3105 for (k = 1; k < 8; k++)
3106 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3111 /* apply AC prediction if needed */
3113 if (dc_pred_dir) { // left
3114 for (k = 1; k < 8; k++) {
3115 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3116 if (!v->pquantizer && block[k << v->left_blk_sh])
3117 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3120 for (k = 1; k < 8; k++) {
3121 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3122 if (!v->pquantizer && block[k << v->top_blk_sh])
3123 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3129 s->block_last_index[n] = i;
3136 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3137 int mquant, int ttmb, int first_block,
3138 uint8_t *dst, int linesize, int skip_block,
3141 MpegEncContext *s = &v->s;
3142 GetBitContext *gb = &s->gb;
3145 int scale, off, idx, last, skip, value;
3146 int ttblk = ttmb & 7;
3149 s->dsp.clear_block(block);
3152 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)];
3154 if (ttblk == TT_4X4) {
3155 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3157 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3158 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3159 || (!v->res_rtm_flag && !first_block))) {
3160 subblkpat = decode012(gb);
3162 subblkpat ^= 3; // swap decoded pattern bits
3163 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3165 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3168 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3170 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3171 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3172 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3175 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3176 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3185 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3190 idx = v->zz_8x8[0][i++];
3192 idx = v->zzi_8x8[i++];
3193 block[idx] = value * scale;
3195 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3199 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3201 v->vc1dsp.vc1_inv_trans_8x8(block);
3202 s->dsp.add_pixels_clamped(block, dst, linesize);
3207 pat = ~subblkpat & 0xF;
3208 for (j = 0; j < 4; j++) {
3209 last = subblkpat & (1 << (3 - j));
3211 off = (j & 1) * 4 + (j & 2) * 16;
3213 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3218 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3220 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3221 block[idx + off] = value * scale;
3223 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3225 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3227 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3229 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3234 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3235 for (j = 0; j < 2; j++) {
3236 last = subblkpat & (1 << (1 - j));
3240 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3245 idx = v->zz_8x4[i++] + off;
3247 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3248 block[idx] = value * scale;
3250 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3252 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3254 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3256 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3261 pat = ~(subblkpat * 5) & 0xF;
3262 for (j = 0; j < 2; j++) {
3263 last = subblkpat & (1 << (1 - j));
3267 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3272 idx = v->zz_4x8[i++] + off;
3274 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3275 block[idx] = value * scale;
3277 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3279 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3281 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3283 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3289 *ttmb_out |= ttblk << (n * 4);
3293 /** @} */ // Macroblock group
3295 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3296 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3298 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3300 MpegEncContext *s = &v->s;
3301 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3302 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3303 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3304 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3305 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3308 if (block_num > 3) {
3309 dst = s->dest[block_num - 3];
3311 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3313 if (s->mb_y != s->end_mb_y || block_num < 2) {
3317 if (block_num > 3) {
3318 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3319 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3320 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3321 mv_stride = s->mb_stride;
3323 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3324 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3325 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3326 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3327 mv_stride = s->b8_stride;
3328 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3331 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3332 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3333 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3335 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3337 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3340 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3342 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3347 dst -= 4 * linesize;
3348 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3349 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3350 idx = (block_cbp | (block_cbp >> 2)) & 3;
3352 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3355 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3357 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3362 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3364 MpegEncContext *s = &v->s;
3365 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3366 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3367 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3368 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3369 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3372 if (block_num > 3) {
3373 dst = s->dest[block_num - 3] - 8 * linesize;
3375 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3378 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3381 if (block_num > 3) {
3382 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3383 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3384 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3386 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3387 : (mb_cbp >> ((block_num + 1) * 4));
3388 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3389 : (mb_is_intra >> ((block_num + 1) * 4));
3390 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3392 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3393 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3395 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3397 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3400 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3402 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3408 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3409 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3410 idx = (block_cbp | (block_cbp >> 1)) & 5;
3412 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3415 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3417 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3422 static void vc1_apply_p_loop_filter(VC1Context *v)
3424 MpegEncContext *s = &v->s;
3427 for (i = 0; i < 6; i++) {
3428 vc1_apply_p_v_loop_filter(v, i);
3431 /* V always precedes H, therefore we run H one MB before V;
3432 * at the end of a row, we catch up to complete the row */
3434 for (i = 0; i < 6; i++) {
3435 vc1_apply_p_h_loop_filter(v, i);
3437 if (s->mb_x == s->mb_width - 1) {
3439 ff_update_block_index(s);
3440 for (i = 0; i < 6; i++) {
3441 vc1_apply_p_h_loop_filter(v, i);
3447 /** Decode one P-frame MB
3449 static int vc1_decode_p_mb(VC1Context *v)
3451 MpegEncContext *s = &v->s;
3452 GetBitContext *gb = &s->gb;
3454 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3455 int cbp; /* cbp decoding stuff */
3456 int mqdiff, mquant; /* MB quantization */
3457 int ttmb = v->ttfrm; /* MB Transform type */
3459 int mb_has_coeffs = 1; /* last_flag */
3460 int dmv_x, dmv_y; /* Differential MV components */
3461 int index, index1; /* LUT indexes */
3462 int val, sign; /* temp values */
3463 int first_block = 1;
3465 int skipped, fourmv;
3466 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3468 mquant = v->pq; /* lossy initialization */
3470 if (v->mv_type_is_raw)
3471 fourmv = get_bits1(gb);
3473 fourmv = v->mv_type_mb_plane[mb_pos];
3475 skipped = get_bits1(gb);
3477 skipped = v->s.mbskip_table[mb_pos];
3479 if (!fourmv) { /* 1MV mode */
3481 GET_MVDATA(dmv_x, dmv_y);
3484 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3485 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3487 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3488 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3490 /* FIXME Set DC val for inter block ? */
3491 if (s->mb_intra && !mb_has_coeffs) {
3493 s->ac_pred = get_bits1(gb);
3495 } else if (mb_has_coeffs) {
3497 s->ac_pred = get_bits1(gb);
3498 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3504 s->current_picture.f.qscale_table[mb_pos] = mquant;
3506 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3507 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3508 VC1_TTMB_VLC_BITS, 2);
3509 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3511 for (i = 0; i < 6; i++) {
3512 s->dc_val[0][s->block_index[i]] = 0;
3514 val = ((cbp >> (5 - i)) & 1);
3515 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3516 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3518 /* check if prediction blocks A and C are available */
3519 v->a_avail = v->c_avail = 0;
3520 if (i == 2 || i == 3 || !s->first_slice_line)
3521 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3522 if (i == 1 || i == 3 || s->mb_x)
3523 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3525 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3526 (i & 4) ? v->codingset2 : v->codingset);
3527 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3529 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3531 for (j = 0; j < 64; j++)
3532 s->block[i][j] <<= 1;
3533 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3534 if (v->pq >= 9 && v->overlap) {
3536 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3538 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3540 block_cbp |= 0xF << (i << 2);
3541 block_intra |= 1 << i;
3543 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3544 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3545 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3546 block_cbp |= pat << (i << 2);
3547 if (!v->ttmbf && ttmb < 8)
3554 for (i = 0; i < 6; i++) {
3555 v->mb_type[0][s->block_index[i]] = 0;
3556 s->dc_val[0][s->block_index[i]] = 0;
3558 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3559 s->current_picture.f.qscale_table[mb_pos] = 0;
3560 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3563 } else { // 4MV mode
3564 if (!skipped /* unskipped MB */) {
3565 int intra_count = 0, coded_inter = 0;
3566 int is_intra[6], is_coded[6];
3568 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3569 for (i = 0; i < 6; i++) {
3570 val = ((cbp >> (5 - i)) & 1);
3571 s->dc_val[0][s->block_index[i]] = 0;
3578 GET_MVDATA(dmv_x, dmv_y);
3580 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3582 vc1_mc_4mv_luma(v, i, 0);
3583 intra_count += s->mb_intra;
3584 is_intra[i] = s->mb_intra;
3585 is_coded[i] = mb_has_coeffs;
3588 is_intra[i] = (intra_count >= 3);
3592 vc1_mc_4mv_chroma(v, 0);
3593 v->mb_type[0][s->block_index[i]] = is_intra[i];
3595 coded_inter = !is_intra[i] & is_coded[i];
3597 // if there are no coded blocks then don't do anything more
3599 if (!intra_count && !coded_inter)
3602 s->current_picture.f.qscale_table[mb_pos] = mquant;
3603 /* test if block is intra and has pred */
3606 for (i = 0; i < 6; i++)
3608 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3609 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3615 s->ac_pred = get_bits1(gb);
3619 if (!v->ttmbf && coded_inter)
3620 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3621 for (i = 0; i < 6; i++) {
3623 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3624 s->mb_intra = is_intra[i];
3626 /* check if prediction blocks A and C are available */
3627 v->a_avail = v->c_avail = 0;
3628 if (i == 2 || i == 3 || !s->first_slice_line)
3629 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3630 if (i == 1 || i == 3 || s->mb_x)
3631 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3633 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3634 (i & 4) ? v->codingset2 : v->codingset);
3635 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3637 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3639 for (j = 0; j < 64; j++)
3640 s->block[i][j] <<= 1;
3641 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3642 (i & 4) ? s->uvlinesize : s->linesize);
3643 if (v->pq >= 9 && v->overlap) {
3645 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3647 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3649 block_cbp |= 0xF << (i << 2);
3650 block_intra |= 1 << i;
3651 } else if (is_coded[i]) {
3652 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3653 first_block, s->dest[dst_idx] + off,
3654 (i & 4) ? s->uvlinesize : s->linesize,
3655 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3657 block_cbp |= pat << (i << 2);
3658 if (!v->ttmbf && ttmb < 8)
3663 } else { // skipped MB
3665 s->current_picture.f.qscale_table[mb_pos] = 0;
3666 for (i = 0; i < 6; i++) {
3667 v->mb_type[0][s->block_index[i]] = 0;
3668 s->dc_val[0][s->block_index[i]] = 0;
3670 for (i = 0; i < 4; i++) {
3671 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3672 vc1_mc_4mv_luma(v, i, 0);
3674 vc1_mc_4mv_chroma(v, 0);
3675 s->current_picture.f.qscale_table[mb_pos] = 0;
3679 v->cbp[s->mb_x] = block_cbp;
3680 v->ttblk[s->mb_x] = block_tt;
3681 v->is_intra[s->mb_x] = block_intra;
3686 /* Decode one macroblock in an interlaced frame p picture */
3688 static int vc1_decode_p_mb_intfr(VC1Context *v)
3690 MpegEncContext *s = &v->s;
3691 GetBitContext *gb = &s->gb;
3693 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3694 int cbp = 0; /* cbp decoding stuff */
3695 int mqdiff, mquant; /* MB quantization */
3696 int ttmb = v->ttfrm; /* MB Transform type */
3698 int mb_has_coeffs = 1; /* last_flag */
3699 int dmv_x, dmv_y; /* Differential MV components */
3700 int val; /* temp value */
3701 int first_block = 1;
3703 int skipped, fourmv = 0, twomv = 0;
3704 int block_cbp = 0, pat, block_tt = 0;
3705 int idx_mbmode = 0, mvbp;
3706 int stride_y, fieldtx;
3708 mquant = v->pq; /* Lossy initialization */
3711 skipped = get_bits1(gb);
3713 skipped = v->s.mbskip_table[mb_pos];
3715 if (v->fourmvswitch)
3716 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3718 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3719 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3720 /* store the motion vector type in a flag (useful later) */
3721 case MV_PMODE_INTFR_4MV:
3723 v->blk_mv_type[s->block_index[0]] = 0;
3724 v->blk_mv_type[s->block_index[1]] = 0;
3725 v->blk_mv_type[s->block_index[2]] = 0;
3726 v->blk_mv_type[s->block_index[3]] = 0;
3728 case MV_PMODE_INTFR_4MV_FIELD:
3730 v->blk_mv_type[s->block_index[0]] = 1;
3731 v->blk_mv_type[s->block_index[1]] = 1;
3732 v->blk_mv_type[s->block_index[2]] = 1;
3733 v->blk_mv_type[s->block_index[3]] = 1;
3735 case MV_PMODE_INTFR_2MV_FIELD:
3737 v->blk_mv_type[s->block_index[0]] = 1;
3738 v->blk_mv_type[s->block_index[1]] = 1;
3739 v->blk_mv_type[s->block_index[2]] = 1;
3740 v->blk_mv_type[s->block_index[3]] = 1;
3742 case MV_PMODE_INTFR_1MV:
3743 v->blk_mv_type[s->block_index[0]] = 0;
3744 v->blk_mv_type[s->block_index[1]] = 0;
3745 v->blk_mv_type[s->block_index[2]] = 0;
3746 v->blk_mv_type[s->block_index[3]] = 0;
3749 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3750 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3751 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3752 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3753 s->mb_intra = v->is_intra[s->mb_x] = 1;
3754 for (i = 0; i < 6; i++)
3755 v->mb_type[0][s->block_index[i]] = 1;
3756 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3757 mb_has_coeffs = get_bits1(gb);
3759 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3760 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3762 s->current_picture.f.qscale_table[mb_pos] = mquant;
3763 /* Set DC scale - y and c use the same (not sure if necessary here) */
3764 s->y_dc_scale = s->y_dc_scale_table[mquant];
3765 s->c_dc_scale = s->c_dc_scale_table[mquant];
3767 for (i = 0; i < 6; i++) {
3768 s->dc_val[0][s->block_index[i]] = 0;
3770 val = ((cbp >> (5 - i)) & 1);
3771 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3772 v->a_avail = v->c_avail = 0;
3773 if (i == 2 || i == 3 || !s->first_slice_line)
3774 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3775 if (i == 1 || i == 3 || s->mb_x)
3776 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3778 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3779 (i & 4) ? v->codingset2 : v->codingset);
3780 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3781 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3783 stride_y = s->linesize << fieldtx;
3784 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3786 stride_y = s->uvlinesize;
3789 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3793 } else { // inter MB
3794 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3796 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3797 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3798 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3800 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3801 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3802 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3805 s->mb_intra = v->is_intra[s->mb_x] = 0;
3806 for (i = 0; i < 6; i++)
3807 v->mb_type[0][s->block_index[i]] = 0;
3808 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3809 /* for all motion vector read MVDATA and motion compensate each block */
3813 for (i = 0; i < 6; i++) {
3816 val = ((mvbp >> (3 - i)) & 1);
3818 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3820 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3821 vc1_mc_4mv_luma(v, i, 0);
3822 } else if (i == 4) {
3823 vc1_mc_4mv_chroma4(v);
3830 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3832 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3833 vc1_mc_4mv_luma(v, 0, 0);
3834 vc1_mc_4mv_luma(v, 1, 0);
3837 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3839 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3840 vc1_mc_4mv_luma(v, 2, 0);
3841 vc1_mc_4mv_luma(v, 3, 0);
3842 vc1_mc_4mv_chroma4(v);
3844 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3847 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3849 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3853 GET_MQUANT(); // p. 227
3854 s->current_picture.f.qscale_table[mb_pos] = mquant;
3855 if (!v->ttmbf && cbp)
3856 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3857 for (i = 0; i < 6; i++) {
3858 s->dc_val[0][s->block_index[i]] = 0;
3860 val = ((cbp >> (5 - i)) & 1);
3862 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3864 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3866 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3867 first_block, s->dest[dst_idx] + off,
3868 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3869 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3870 block_cbp |= pat << (i << 2);
3871 if (!v->ttmbf && ttmb < 8)
3878 s->mb_intra = v->is_intra[s->mb_x] = 0;
3879 for (i = 0; i < 6; i++) {
3880 v->mb_type[0][s->block_index[i]] = 0;
3881 s->dc_val[0][s->block_index[i]] = 0;
3883 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3884 s->current_picture.f.qscale_table[mb_pos] = 0;
3885 v->blk_mv_type[s->block_index[0]] = 0;
3886 v->blk_mv_type[s->block_index[1]] = 0;
3887 v->blk_mv_type[s->block_index[2]] = 0;
3888 v->blk_mv_type[s->block_index[3]] = 0;
3889 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3892 if (s->mb_x == s->mb_width - 1)
3893 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3897 static int vc1_decode_p_mb_intfi(VC1Context *v)
3899 MpegEncContext *s = &v->s;
3900 GetBitContext *gb = &s->gb;
3902 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3903 int cbp = 0; /* cbp decoding stuff */
3904 int mqdiff, mquant; /* MB quantization */
3905 int ttmb = v->ttfrm; /* MB Transform type */
3907 int mb_has_coeffs = 1; /* last_flag */
3908 int dmv_x, dmv_y; /* Differential MV components */
3909 int val; /* temp values */
3910 int first_block = 1;
3913 int block_cbp = 0, pat, block_tt = 0;
3916 mquant = v->pq; /* Lossy initialization */
3918 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3919 if (idx_mbmode <= 1) { // intra MB
3920 s->mb_intra = v->is_intra[s->mb_x] = 1;
3921 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3922 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3923 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3925 s->current_picture.f.qscale_table[mb_pos] = mquant;
3926 /* Set DC scale - y and c use the same (not sure if necessary here) */
3927 s->y_dc_scale = s->y_dc_scale_table[mquant];
3928 s->c_dc_scale = s->c_dc_scale_table[mquant];
3929 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3930 mb_has_coeffs = idx_mbmode & 1;
3932 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3934 for (i = 0; i < 6; i++) {
3935 s->dc_val[0][s->block_index[i]] = 0;
3936 v->mb_type[0][s->block_index[i]] = 1;
3938 val = ((cbp >> (5 - i)) & 1);
3939 v->a_avail = v->c_avail = 0;
3940 if (i == 2 || i == 3 || !s->first_slice_line)
3941 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3942 if (i == 1 || i == 3 || s->mb_x)
3943 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3945 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3946 (i & 4) ? v->codingset2 : v->codingset);
3947 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3949 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3950 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3951 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3952 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3953 // TODO: loop filter
3956 s->mb_intra = v->is_intra[s->mb_x] = 0;
3957 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3958 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3959 if (idx_mbmode <= 5) { // 1-MV
3960 dmv_x = dmv_y = pred_flag = 0;
3961 if (idx_mbmode & 1) {
3962 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3964 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3966 mb_has_coeffs = !(idx_mbmode & 2);
3968 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3969 for (i = 0; i < 6; i++) {
3971 dmv_x = dmv_y = pred_flag = 0;
3972 val = ((v->fourmvbp >> (3 - i)) & 1);
3974 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3976 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3977 vc1_mc_4mv_luma(v, i, 0);
3979 vc1_mc_4mv_chroma(v, 0);
3981 mb_has_coeffs = idx_mbmode & 1;
3984 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3988 s->current_picture.f.qscale_table[mb_pos] = mquant;
3989 if (!v->ttmbf && cbp) {
3990 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3993 for (i = 0; i < 6; i++) {
3994 s->dc_val[0][s->block_index[i]] = 0;
3996 val = ((cbp >> (5 - i)) & 1);
3997 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
3998 if (v->second_field)
3999 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4001 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4002 first_block, s->dest[dst_idx] + off,
4003 (i & 4) ? s->uvlinesize : s->linesize,
4004 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4006 block_cbp |= pat << (i << 2);
4007 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4012 if (s->mb_x == s->mb_width - 1)
4013 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4017 /** Decode one B-frame MB (in Main profile)
4019 static void vc1_decode_b_mb(VC1Context *v)
4021 MpegEncContext *s = &v->s;
4022 GetBitContext *gb = &s->gb;
4024 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4025 int cbp = 0; /* cbp decoding stuff */
4026 int mqdiff, mquant; /* MB quantization */
4027 int ttmb = v->ttfrm; /* MB Transform type */
4028 int mb_has_coeffs = 0; /* last_flag */
4029 int index, index1; /* LUT indexes */
4030 int val, sign; /* temp values */
4031 int first_block = 1;
4033 int skipped, direct;
4034 int dmv_x[2], dmv_y[2];
4035 int bmvtype = BMV_TYPE_BACKWARD;
4037 mquant = v->pq; /* lossy initialization */
4041 direct = get_bits1(gb);
4043 direct = v->direct_mb_plane[mb_pos];
4045 skipped = get_bits1(gb);
4047 skipped = v->s.mbskip_table[mb_pos];
4049 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4050 for (i = 0; i < 6; i++) {
4051 v->mb_type[0][s->block_index[i]] = 0;
4052 s->dc_val[0][s->block_index[i]] = 0;
4054 s->current_picture.f.qscale_table[mb_pos] = 0;
4058 GET_MVDATA(dmv_x[0], dmv_y[0]);
4059 dmv_x[1] = dmv_x[0];
4060 dmv_y[1] = dmv_y[0];
4062 if (skipped || !s->mb_intra) {
4063 bmvtype = decode012(gb);
4066 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4069 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4072 bmvtype = BMV_TYPE_INTERPOLATED;
4073 dmv_x[0] = dmv_y[0] = 0;
4077 for (i = 0; i < 6; i++)
4078 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4082 bmvtype = BMV_TYPE_INTERPOLATED;
4083 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4084 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4088 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4091 s->current_picture.f.qscale_table[mb_pos] = mquant;
4093 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4094 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4095 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4096 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4098 if (!mb_has_coeffs && !s->mb_intra) {
4099 /* no coded blocks - effectively skipped */
4100 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4101 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4104 if (s->mb_intra && !mb_has_coeffs) {
4106 s->current_picture.f.qscale_table[mb_pos] = mquant;
4107 s->ac_pred = get_bits1(gb);
4109 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4111 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4112 GET_MVDATA(dmv_x[0], dmv_y[0]);
4113 if (!mb_has_coeffs) {
4114 /* interpolated skipped block */
4115 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4116 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4120 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4122 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4125 s->ac_pred = get_bits1(gb);
4126 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4128 s->current_picture.f.qscale_table[mb_pos] = mquant;
4129 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4130 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4134 for (i = 0; i < 6; i++) {
4135 s->dc_val[0][s->block_index[i]] = 0;
4137 val = ((cbp >> (5 - i)) & 1);
4138 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4139 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4141 /* check if prediction blocks A and C are available */
4142 v->a_avail = v->c_avail = 0;
4143 if (i == 2 || i == 3 || !s->first_slice_line)
4144 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4145 if (i == 1 || i == 3 || s->mb_x)
4146 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4148 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4149 (i & 4) ? v->codingset2 : v->codingset);
4150 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4152 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4154 for (j = 0; j < 64; j++)
4155 s->block[i][j] <<= 1;
4156 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4158 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4159 first_block, s->dest[dst_idx] + off,
4160 (i & 4) ? s->uvlinesize : s->linesize,
4161 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4162 if (!v->ttmbf && ttmb < 8)
4169 /** Decode one B-frame MB (in interlaced field B picture)
4171 static void vc1_decode_b_mb_intfi(VC1Context *v)
4173 MpegEncContext *s = &v->s;
4174 GetBitContext *gb = &s->gb;
4176 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4177 int cbp = 0; /* cbp decoding stuff */
4178 int mqdiff, mquant; /* MB quantization */
4179 int ttmb = v->ttfrm; /* MB Transform type */
4180 int mb_has_coeffs = 0; /* last_flag */
4181 int val; /* temp value */
4182 int first_block = 1;
4185 int dmv_x[2], dmv_y[2], pred_flag[2];
4186 int bmvtype = BMV_TYPE_BACKWARD;
4187 int idx_mbmode, interpmvp;
4189 mquant = v->pq; /* Lossy initialization */
4192 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4193 if (idx_mbmode <= 1) { // intra MB
4194 s->mb_intra = v->is_intra[s->mb_x] = 1;
4195 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4196 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4197 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4199 s->current_picture.f.qscale_table[mb_pos] = mquant;
4200 /* Set DC scale - y and c use the same (not sure if necessary here) */
4201 s->y_dc_scale = s->y_dc_scale_table[mquant];
4202 s->c_dc_scale = s->c_dc_scale_table[mquant];
4203 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4204 mb_has_coeffs = idx_mbmode & 1;
4206 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4208 for (i = 0; i < 6; i++) {
4209 s->dc_val[0][s->block_index[i]] = 0;
4211 val = ((cbp >> (5 - i)) & 1);
4212 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4213 v->a_avail = v->c_avail = 0;
4214 if (i == 2 || i == 3 || !s->first_slice_line)
4215 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4216 if (i == 1 || i == 3 || s->mb_x)
4217 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4219 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4220 (i & 4) ? v->codingset2 : v->codingset);
4221 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4223 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4225 for (j = 0; j < 64; j++)
4226 s->block[i][j] <<= 1;
4227 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4228 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4229 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4230 // TODO: yet to perform loop filter
4233 s->mb_intra = v->is_intra[s->mb_x] = 0;
4234 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4235 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4237 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4239 fwd = v->forward_mb_plane[mb_pos];
4240 if (idx_mbmode <= 5) { // 1-MV
4241 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4242 pred_flag[0] = pred_flag[1] = 0;
4244 bmvtype = BMV_TYPE_FORWARD;
4246 bmvtype = decode012(gb);
4249 bmvtype = BMV_TYPE_BACKWARD;
4252 bmvtype = BMV_TYPE_DIRECT;
4255 bmvtype = BMV_TYPE_INTERPOLATED;
4256 interpmvp = get_bits1(gb);
4259 v->bmvtype = bmvtype;
4260 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4261 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4263 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4264 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4266 if (bmvtype == BMV_TYPE_DIRECT) {
4267 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4268 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4270 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4271 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4272 mb_has_coeffs = !(idx_mbmode & 2);
4275 bmvtype = BMV_TYPE_FORWARD;
4276 v->bmvtype = bmvtype;
4277 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4278 for (i = 0; i < 6; i++) {
4280 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4281 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4282 val = ((v->fourmvbp >> (3 - i)) & 1);
4284 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4285 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4286 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4288 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4289 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4291 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4293 mb_has_coeffs = idx_mbmode & 1;
4296 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4300 s->current_picture.f.qscale_table[mb_pos] = mquant;
4301 if (!v->ttmbf && cbp) {
4302 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4305 for (i = 0; i < 6; i++) {
4306 s->dc_val[0][s->block_index[i]] = 0;
4308 val = ((cbp >> (5 - i)) & 1);
4309 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4310 if (v->second_field)
4311 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4313 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4314 first_block, s->dest[dst_idx] + off,
4315 (i & 4) ? s->uvlinesize : s->linesize,
4316 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4317 if (!v->ttmbf && ttmb < 8)
4325 /** Decode blocks of I-frame
4327 static void vc1_decode_i_blocks(VC1Context *v)
4330 MpegEncContext *s = &v->s;
4335 /* select codingmode used for VLC tables selection */
4336 switch (v->y_ac_table_index) {
4338 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4341 v->codingset = CS_HIGH_MOT_INTRA;
4344 v->codingset = CS_MID_RATE_INTRA;
4348 switch (v->c_ac_table_index) {
4350 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4353 v->codingset2 = CS_HIGH_MOT_INTER;
4356 v->codingset2 = CS_MID_RATE_INTER;
4360 /* Set DC scale - y and c use the same */
4361 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4362 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4365 s->mb_x = s->mb_y = 0;
4367 s->first_slice_line = 1;
4368 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4370 ff_init_block_index(s);
4371 for (; s->mb_x < s->mb_width; s->mb_x++) {
4373 ff_update_block_index(s);
4374 dst[0] = s->dest[0];
4375 dst[1] = dst[0] + 8;
4376 dst[2] = s->dest[0] + s->linesize * 8;
4377 dst[3] = dst[2] + 8;
4378 dst[4] = s->dest[1];
4379 dst[5] = s->dest[2];
4380 s->dsp.clear_blocks(s->block[0]);
4381 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4382 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4383 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4384 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4385 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4387 // do actual MB decoding and displaying
4388 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4389 v->s.ac_pred = get_bits1(&v->s.gb);
4391 for (k = 0; k < 6; k++) {
4392 val = ((cbp >> (5 - k)) & 1);
4395 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4399 cbp |= val << (5 - k);
4401 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4403 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4405 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4406 if (v->pq >= 9 && v->overlap) {
4408 for (j = 0; j < 64; j++)
4409 s->block[k][j] <<= 1;
4410 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4413 for (j = 0; j < 64; j++)
4414 s->block[k][j] = (s->block[k][j] - 64) << 1;
4415 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4419 if (v->pq >= 9 && v->overlap) {
4421 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4422 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4423 if (!(s->flags & CODEC_FLAG_GRAY)) {
4424 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4425 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4428 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4429 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4430 if (!s->first_slice_line) {
4431 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4432 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4433 if (!(s->flags & CODEC_FLAG_GRAY)) {
4434 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4435 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4438 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4439 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4441 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4443 if (get_bits_count(&s->gb) > v->bits) {
4444 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4445 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4446 get_bits_count(&s->gb), v->bits);
4450 if (!v->s.loop_filter)
4451 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4453 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4455 s->first_slice_line = 0;
4457 if (v->s.loop_filter)
4458 ff_draw_horiz_band(s, (s->mb_height - 1) * 16, 16);
4459 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4462 /** Decode blocks of I-frame for advanced profile
4464 static void vc1_decode_i_blocks_adv(VC1Context *v)
4467 MpegEncContext *s = &v->s;
4473 GetBitContext *gb = &s->gb;
4475 /* select codingmode used for VLC tables selection */
4476 switch (v->y_ac_table_index) {
4478 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4481 v->codingset = CS_HIGH_MOT_INTRA;
4484 v->codingset = CS_MID_RATE_INTRA;
4488 switch (v->c_ac_table_index) {
4490 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4493 v->codingset2 = CS_HIGH_MOT_INTER;
4496 v->codingset2 = CS_MID_RATE_INTER;
4501 s->mb_x = s->mb_y = 0;
4503 s->first_slice_line = 1;
4504 s->mb_y = s->start_mb_y;
4505 if (s->start_mb_y) {
4507 ff_init_block_index(s);
4508 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4509 (1 + s->b8_stride) * sizeof(*s->coded_block));
4511 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4513 ff_init_block_index(s);
4514 for (;s->mb_x < s->mb_width; s->mb_x++) {
4515 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4516 ff_update_block_index(s);
4517 s->dsp.clear_blocks(block[0]);
4518 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4519 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4520 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4521 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4523 // do actual MB decoding and displaying
4524 if (v->fieldtx_is_raw)
4525 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4526 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4527 if ( v->acpred_is_raw)
4528 v->s.ac_pred = get_bits1(&v->s.gb);
4530 v->s.ac_pred = v->acpred_plane[mb_pos];
4532 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4533 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4537 s->current_picture.f.qscale_table[mb_pos] = mquant;
4538 /* Set DC scale - y and c use the same */
4539 s->y_dc_scale = s->y_dc_scale_table[mquant];
4540 s->c_dc_scale = s->c_dc_scale_table[mquant];
4542 for (k = 0; k < 6; k++) {
4543 val = ((cbp >> (5 - k)) & 1);
4546 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4550 cbp |= val << (5 - k);
4552 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4553 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4555 vc1_decode_i_block_adv(v, block[k], k, val,
4556 (k < 4) ? v->codingset : v->codingset2, mquant);
4558 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4560 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4563 vc1_smooth_overlap_filter_iblk(v);
4564 vc1_put_signed_blocks_clamped(v);
4565 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4567 if (get_bits_count(&s->gb) > v->bits) {
4568 // TODO: may need modification to handle slice coding
4569 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4570 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4571 get_bits_count(&s->gb), v->bits);
4575 if (!v->s.loop_filter)
4576 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4578 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4579 s->first_slice_line = 0;
4582 /* raw bottom MB row */
4584 ff_init_block_index(s);
4585 for (;s->mb_x < s->mb_width; s->mb_x++) {
4586 ff_update_block_index(s);
4587 vc1_put_signed_blocks_clamped(v);
4588 if (v->s.loop_filter)
4589 vc1_loop_filter_iblk_delayed(v, v->pq);
4591 if (v->s.loop_filter)
4592 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4593 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4594 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4597 static void vc1_decode_p_blocks(VC1Context *v)
4599 MpegEncContext *s = &v->s;
4600 int apply_loop_filter;
4602 /* select codingmode used for VLC tables selection */
4603 switch (v->c_ac_table_index) {
4605 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4608 v->codingset = CS_HIGH_MOT_INTRA;
4611 v->codingset = CS_MID_RATE_INTRA;
4615 switch (v->c_ac_table_index) {
4617 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4620 v->codingset2 = CS_HIGH_MOT_INTER;
4623 v->codingset2 = CS_MID_RATE_INTER;
4627 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4628 s->first_slice_line = 1;
4629 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4630 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4632 ff_init_block_index(s);
4633 for (; s->mb_x < s->mb_width; s->mb_x++) {
4634 ff_update_block_index(s);
4636 if (v->fcm == ILACE_FIELD)
4637 vc1_decode_p_mb_intfi(v);
4638 else if (v->fcm == ILACE_FRAME)
4639 vc1_decode_p_mb_intfr(v);
4640 else vc1_decode_p_mb(v);
4641 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4642 vc1_apply_p_loop_filter(v);
4643 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4644 // TODO: may need modification to handle slice coding
4645 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4646 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4647 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4651 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4652 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4653 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4654 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4655 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4656 s->first_slice_line = 0;
4658 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4660 ff_init_block_index(s);
4661 for (; s->mb_x < s->mb_width; s->mb_x++) {
4662 ff_update_block_index(s);
4663 vc1_apply_p_loop_filter(v);
4666 if (s->end_mb_y >= s->start_mb_y)
4667 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4668 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4669 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4672 static void vc1_decode_b_blocks(VC1Context *v)
4674 MpegEncContext *s = &v->s;
4676 /* select codingmode used for VLC tables selection */
4677 switch (v->c_ac_table_index) {
4679 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4682 v->codingset = CS_HIGH_MOT_INTRA;
4685 v->codingset = CS_MID_RATE_INTRA;
4689 switch (v->c_ac_table_index) {
4691 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4694 v->codingset2 = CS_HIGH_MOT_INTER;
4697 v->codingset2 = CS_MID_RATE_INTER;
4701 s->first_slice_line = 1;
4702 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4704 ff_init_block_index(s);
4705 for (; s->mb_x < s->mb_width; s->mb_x++) {
4706 ff_update_block_index(s);
4708 if (v->fcm == ILACE_FIELD)
4709 vc1_decode_b_mb_intfi(v);
4712 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4713 // TODO: may need modification to handle slice coding
4714 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4715 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4716 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4719 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4721 if (!v->s.loop_filter)
4722 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4724 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4725 s->first_slice_line = 0;
4727 if (v->s.loop_filter)
4728 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4729 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4730 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4733 static void vc1_decode_skip_blocks(VC1Context *v)
4735 MpegEncContext *s = &v->s;
4737 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4738 s->first_slice_line = 1;
4739 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4741 ff_init_block_index(s);
4742 ff_update_block_index(s);
4743 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4744 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4745 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4746 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4747 s->first_slice_line = 0;
4749 s->pict_type = AV_PICTURE_TYPE_P;
4752 static void vc1_decode_blocks(VC1Context *v)
4755 v->s.esc3_level_length = 0;
4757 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4760 v->left_blk_idx = -1;
4761 v->topleft_blk_idx = 1;
4763 switch (v->s.pict_type) {
4764 case AV_PICTURE_TYPE_I:
4765 if (v->profile == PROFILE_ADVANCED)
4766 vc1_decode_i_blocks_adv(v);
4768 vc1_decode_i_blocks(v);
4770 case AV_PICTURE_TYPE_P:
4771 if (v->p_frame_skipped)
4772 vc1_decode_skip_blocks(v);
4774 vc1_decode_p_blocks(v);
4776 case AV_PICTURE_TYPE_B:
4778 if (v->profile == PROFILE_ADVANCED)
4779 vc1_decode_i_blocks_adv(v);
4781 vc1_decode_i_blocks(v);
4783 vc1_decode_b_blocks(v);
4789 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4793 * Transform coefficients for both sprites in 16.16 fixed point format,
4794 * in the order they appear in the bitstream:
4796 * rotation 1 (unused)
4798 * rotation 2 (unused)
4805 int effect_type, effect_flag;
4806 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4807 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4810 static inline int get_fp_val(GetBitContext* gb)
4812 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4815 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4819 switch (get_bits(gb, 2)) {
4822 c[2] = get_fp_val(gb);
4826 c[0] = c[4] = get_fp_val(gb);
4827 c[2] = get_fp_val(gb);
4830 c[0] = get_fp_val(gb);
4831 c[2] = get_fp_val(gb);
4832 c[4] = get_fp_val(gb);
4835 c[0] = get_fp_val(gb);
4836 c[1] = get_fp_val(gb);
4837 c[2] = get_fp_val(gb);
4838 c[3] = get_fp_val(gb);
4839 c[4] = get_fp_val(gb);
4842 c[5] = get_fp_val(gb);
4844 c[6] = get_fp_val(gb);
4849 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4851 AVCodecContext *avctx = v->s.avctx;
4854 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4855 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4856 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4857 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4858 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4859 for (i = 0; i < 7; i++)
4860 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4861 sd->coefs[sprite][i] / (1<<16),
4862 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4863 av_log(avctx, AV_LOG_DEBUG, "\n");
4867 if (sd->effect_type = get_bits_long(gb, 30)) {
4868 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4870 vc1_sprite_parse_transform(gb, sd->effect_params1);
4873 vc1_sprite_parse_transform(gb, sd->effect_params1);
4874 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4877 for (i = 0; i < sd->effect_pcount1; i++)
4878 sd->effect_params1[i] = get_fp_val(gb);
4880 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4881 // effect 13 is simple alpha blending and matches the opacity above
4882 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4883 for (i = 0; i < sd->effect_pcount1; i++)
4884 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4885 sd->effect_params1[i] / (1 << 16),
4886 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4887 av_log(avctx, AV_LOG_DEBUG, "\n");
4890 sd->effect_pcount2 = get_bits(gb, 16);
4891 if (sd->effect_pcount2 > 10) {
4892 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4894 } else if (sd->effect_pcount2) {
4896 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4897 while (++i < sd->effect_pcount2) {
4898 sd->effect_params2[i] = get_fp_val(gb);
4899 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4900 sd->effect_params2[i] / (1 << 16),
4901 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4903 av_log(avctx, AV_LOG_DEBUG, "\n");
4906 if (sd->effect_flag = get_bits1(gb))
4907 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4909 if (get_bits_count(gb) >= gb->size_in_bits +
4910 (avctx->codec_id == CODEC_ID_WMV3IMAGE ? 64 : 0))
4911 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4912 if (get_bits_count(gb) < gb->size_in_bits - 8)
4913 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4916 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4918 int i, plane, row, sprite;
4919 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4920 uint8_t* src_h[2][2];
4921 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4923 MpegEncContext *s = &v->s;
4925 for (i = 0; i < 2; i++) {
4926 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4927 xadv[i] = sd->coefs[i][0];
4928 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4929 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4931 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4932 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4934 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4936 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4937 int width = v->output_width>>!!plane;
4939 for (row = 0; row < v->output_height>>!!plane; row++) {
4940 uint8_t *dst = v->sprite_output_frame.data[plane] +
4941 v->sprite_output_frame.linesize[plane] * row;
4943 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4944 uint8_t *iplane = s->current_picture.f.data[plane];
4945 int iline = s->current_picture.f.linesize[plane];
4946 int ycoord = yoff[sprite] + yadv[sprite] * row;
4947 int yline = ycoord >> 16;
4949 ysub[sprite] = ycoord & 0xFFFF;
4951 iplane = s->last_picture.f.data[plane];
4952 iline = s->last_picture.f.linesize[plane];
4954 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4955 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4956 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4958 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4960 if (sr_cache[sprite][0] != yline) {
4961 if (sr_cache[sprite][1] == yline) {
4962 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4963 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4965 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4966 sr_cache[sprite][0] = yline;
4969 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4970 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4971 iplane + next_line, xoff[sprite],
4972 xadv[sprite], width);
4973 sr_cache[sprite][1] = yline + 1;
4975 src_h[sprite][0] = v->sr_rows[sprite][0];
4976 src_h[sprite][1] = v->sr_rows[sprite][1];
4980 if (!v->two_sprites) {
4982 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4984 memcpy(dst, src_h[0][0], width);
4987 if (ysub[0] && ysub[1]) {
4988 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4989 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4990 } else if (ysub[0]) {
4991 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
4992 src_h[1][0], alpha, width);
4993 } else if (ysub[1]) {
4994 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
4995 src_h[0][0], (1<<16)-1-alpha, width);
4997 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5003 for (i = 0; i < 2; i++) {
5013 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5015 MpegEncContext *s = &v->s;
5016 AVCodecContext *avctx = s->avctx;
5019 vc1_parse_sprites(v, gb, &sd);
5021 if (!s->current_picture.f.data[0]) {
5022 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5026 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5027 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5031 if (v->sprite_output_frame.data[0])
5032 avctx->release_buffer(avctx, &v->sprite_output_frame);
5034 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5035 v->sprite_output_frame.reference = 0;
5036 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
5037 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5041 vc1_draw_sprites(v, &sd);
5046 static void vc1_sprite_flush(AVCodecContext *avctx)
5048 VC1Context *v = avctx->priv_data;
5049 MpegEncContext *s = &v->s;
5050 AVFrame *f = &s->current_picture.f;
5053 /* Windows Media Image codecs have a convergence interval of two keyframes.
5054 Since we can't enforce it, clear to black the missing sprite. This is
5055 wrong but it looks better than doing nothing. */
5058 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5059 for (i = 0; i < v->sprite_height>>!!plane; i++)
5060 memset(f->data[plane] + i * f->linesize[plane],
5061 plane ? 128 : 0, f->linesize[plane]);
5066 static av_cold int vc1_decode_init_alloc_tables(VC1Context *v)
5068 MpegEncContext *s = &v->s;
5071 /* Allocate mb bitplanes */
5072 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5073 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5074 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5075 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5076 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5077 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5079 v->n_allocated_blks = s->mb_width + 2;
5080 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5081 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5082 v->cbp = v->cbp_base + s->mb_stride;
5083 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5084 v->ttblk = v->ttblk_base + s->mb_stride;
5085 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5086 v->is_intra = v->is_intra_base + s->mb_stride;
5087 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5088 v->luma_mv = v->luma_mv_base + s->mb_stride;
5090 /* allocate block type info in that way so it could be used with s->block_index[] */
5091 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5092 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5093 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5094 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5096 /* allocate memory to store block level MV info */
5097 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5098 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5099 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5100 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5101 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5102 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5103 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5104 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);
5105 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5106 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5107 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);
5109 /* Init coded blocks info */
5110 if (v->profile == PROFILE_ADVANCED) {
5111 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5113 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5117 ff_intrax8_common_init(&v->x8,s);
5119 if (s->avctx->codec_id == CODEC_ID_WMV3IMAGE || s->avctx->codec_id == CODEC_ID_VC1IMAGE) {
5120 for (i = 0; i < 4; i++)
5121 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5124 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5125 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5132 /** Initialize a VC1/WMV3 decoder
5133 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5134 * @todo TODO: Decypher remaining bits in extra_data
5136 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5138 VC1Context *v = avctx->priv_data;
5139 MpegEncContext *s = &v->s;
5143 /* save the container output size for WMImage */
5144 v->output_width = avctx->width;
5145 v->output_height = avctx->height;
5147 if (!avctx->extradata_size || !avctx->extradata)
5149 if (!(avctx->flags & CODEC_FLAG_GRAY))
5150 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5152 avctx->pix_fmt = PIX_FMT_GRAY8;
5153 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5155 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5156 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5158 if (avctx->idct_algo == FF_IDCT_AUTO) {
5159 avctx->idct_algo = FF_IDCT_WMV2;
5162 if (ff_vc1_init_common(v) < 0)
5164 ff_vc1dsp_init(&v->vc1dsp);
5166 if (avctx->codec_id == CODEC_ID_WMV3 || avctx->codec_id == CODEC_ID_WMV3IMAGE) {
5169 // looks like WMV3 has a sequence header stored in the extradata
5170 // advanced sequence header may be before the first frame
5171 // the last byte of the extradata is a version number, 1 for the
5172 // samples we can decode
5174 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5176 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5179 count = avctx->extradata_size*8 - get_bits_count(&gb);
5181 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5182 count, get_bits(&gb, count));
5183 } else if (count < 0) {
5184 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5186 } else { // VC1/WVC1/WVP2
5187 const uint8_t *start = avctx->extradata;
5188 uint8_t *end = avctx->extradata + avctx->extradata_size;
5189 const uint8_t *next;
5190 int size, buf2_size;
5191 uint8_t *buf2 = NULL;
5192 int seq_initialized = 0, ep_initialized = 0;
5194 if (avctx->extradata_size < 16) {
5195 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5199 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5200 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5202 for (; next < end; start = next) {
5203 next = find_next_marker(start + 4, end);
5204 size = next - start - 4;
5207 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5208 init_get_bits(&gb, buf2, buf2_size * 8);
5209 switch (AV_RB32(start)) {
5210 case VC1_CODE_SEQHDR:
5211 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5215 seq_initialized = 1;
5217 case VC1_CODE_ENTRYPOINT:
5218 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5227 if (!seq_initialized || !ep_initialized) {
5228 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5231 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5234 avctx->profile = v->profile;
5235 if (v->profile == PROFILE_ADVANCED)
5236 avctx->level = v->level;
5238 avctx->has_b_frames = !!avctx->max_b_frames;
5240 s->mb_width = (avctx->coded_width + 15) >> 4;
5241 s->mb_height = (avctx->coded_height + 15) >> 4;
5243 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5244 for (i = 0; i < 64; i++) {
5245 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5246 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5247 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5248 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5249 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5250 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5255 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5260 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5261 v->sprite_width = avctx->coded_width;
5262 v->sprite_height = avctx->coded_height;
5264 avctx->coded_width = avctx->width = v->output_width;
5265 avctx->coded_height = avctx->height = v->output_height;
5267 // prevent 16.16 overflows
5268 if (v->sprite_width > 1 << 14 ||
5269 v->sprite_height > 1 << 14 ||
5270 v->output_width > 1 << 14 ||
5271 v->output_height > 1 << 14) return -1;
5276 /** Close a VC1/WMV3 decoder
5277 * @warning Initial try at using MpegEncContext stuff
5279 static av_cold int vc1_decode_end(AVCodecContext *avctx)
5281 VC1Context *v = avctx->priv_data;
5284 if ((avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE)
5285 && v->sprite_output_frame.data[0])
5286 avctx->release_buffer(avctx, &v->sprite_output_frame);
5287 for (i = 0; i < 4; i++)
5288 av_freep(&v->sr_rows[i >> 1][i & 1]);
5289 av_freep(&v->hrd_rate);
5290 av_freep(&v->hrd_buffer);
5291 ff_MPV_common_end(&v->s);
5292 av_freep(&v->mv_type_mb_plane);
5293 av_freep(&v->direct_mb_plane);
5294 av_freep(&v->forward_mb_plane);
5295 av_freep(&v->fieldtx_plane);
5296 av_freep(&v->acpred_plane);
5297 av_freep(&v->over_flags_plane);
5298 av_freep(&v->mb_type_base);
5299 av_freep(&v->blk_mv_type_base);
5300 av_freep(&v->mv_f_base);
5301 av_freep(&v->mv_f_last_base);
5302 av_freep(&v->mv_f_next_base);
5303 av_freep(&v->block);
5304 av_freep(&v->cbp_base);
5305 av_freep(&v->ttblk_base);
5306 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5307 av_freep(&v->luma_mv_base);
5308 ff_intrax8_common_end(&v->x8);
5313 /** Decode a VC1/WMV3 frame
5314 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5316 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5317 int *data_size, AVPacket *avpkt)
5319 const uint8_t *buf = avpkt->data;
5320 int buf_size = avpkt->size, n_slices = 0, i;
5321 VC1Context *v = avctx->priv_data;
5322 MpegEncContext *s = &v->s;
5323 AVFrame *pict = data;
5324 uint8_t *buf2 = NULL;
5325 const uint8_t *buf_start = buf;
5326 int mb_height, n_slices1=-1;
5331 } *slices = NULL, *tmp;
5333 if(s->flags & CODEC_FLAG_LOW_DELAY)
5336 /* no supplementary picture */
5337 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5338 /* special case for last picture */
5339 if (s->low_delay == 0 && s->next_picture_ptr) {
5340 *pict = s->next_picture_ptr->f;
5341 s->next_picture_ptr = NULL;
5343 *data_size = sizeof(AVFrame);
5349 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5350 if (v->profile < PROFILE_ADVANCED)
5351 avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
5353 avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
5356 //for advanced profile we may need to parse and unescape data
5357 if (avctx->codec_id == CODEC_ID_VC1 || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5359 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5361 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5362 const uint8_t *start, *end, *next;
5366 for (start = buf, end = buf + buf_size; next < end; start = next) {
5367 next = find_next_marker(start + 4, end);
5368 size = next - start - 4;
5369 if (size <= 0) continue;
5370 switch (AV_RB32(start)) {
5371 case VC1_CODE_FRAME:
5372 if (avctx->hwaccel ||
5373 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5375 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5377 case VC1_CODE_FIELD: {
5379 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5382 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5383 if (!slices[n_slices].buf)
5385 buf_size3 = vc1_unescape_buffer(start + 4, size,
5386 slices[n_slices].buf);
5387 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5389 /* assuming that the field marker is at the exact middle,
5390 hope it's correct */
5391 slices[n_slices].mby_start = s->mb_height >> 1;
5392 n_slices1 = n_slices - 1; // index of the last slice of the first field
5396 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5397 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5398 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5399 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5401 case VC1_CODE_SLICE: {
5403 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5406 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5407 if (!slices[n_slices].buf)
5409 buf_size3 = vc1_unescape_buffer(start + 4, size,
5410 slices[n_slices].buf);
5411 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5413 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5419 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5420 const uint8_t *divider;
5423 divider = find_next_marker(buf, buf + buf_size);
5424 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5425 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5427 } else { // found field marker, unescape second field
5428 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5432 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5433 if (!slices[n_slices].buf)
5435 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5436 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5438 slices[n_slices].mby_start = s->mb_height >> 1;
5439 n_slices1 = n_slices - 1;
5442 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5444 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5446 init_get_bits(&s->gb, buf2, buf_size2*8);
5448 init_get_bits(&s->gb, buf, buf_size*8);
5450 if (v->res_sprite) {
5451 v->new_sprite = !get_bits1(&s->gb);
5452 v->two_sprites = get_bits1(&s->gb);
5453 /* res_sprite means a Windows Media Image stream, CODEC_ID_*IMAGE means
5454 we're using the sprite compositor. These are intentionally kept separate
5455 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5456 the vc1 one for WVP2 */
5457 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5458 if (v->new_sprite) {
5459 // switch AVCodecContext parameters to those of the sprites
5460 avctx->width = avctx->coded_width = v->sprite_width;
5461 avctx->height = avctx->coded_height = v->sprite_height;
5468 if (s->context_initialized &&
5469 (s->width != avctx->coded_width ||
5470 s->height != avctx->coded_height)) {
5471 vc1_decode_end(avctx);
5474 if (!s->context_initialized) {
5475 if (ff_msmpeg4_decode_init(avctx) < 0 || vc1_decode_init_alloc_tables(v) < 0)
5478 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5480 if (v->profile == PROFILE_ADVANCED) {
5481 s->h_edge_pos = avctx->coded_width;
5482 s->v_edge_pos = avctx->coded_height;
5486 /* We need to set current_picture_ptr before reading the header,
5487 * otherwise we cannot store anything in there. */
5488 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5489 int i = ff_find_unused_picture(s, 0);
5492 s->current_picture_ptr = &s->picture[i];
5495 // do parse frame header
5496 v->pic_header_flag = 0;
5497 if (v->profile < PROFILE_ADVANCED) {
5498 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5502 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5507 if ((avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE)
5508 && s->pict_type != AV_PICTURE_TYPE_I) {
5509 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5513 // process pulldown flags
5514 s->current_picture_ptr->f.repeat_pict = 0;
5515 // Pulldown flags are only valid when 'broadcast' has been set.
5516 // So ticks_per_frame will be 2
5519 s->current_picture_ptr->f.repeat_pict = 1;
5520 } else if (v->rptfrm) {
5522 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5525 // for skipping the frame
5526 s->current_picture.f.pict_type = s->pict_type;
5527 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5529 /* skip B-frames if we don't have reference frames */
5530 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
5533 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5534 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5535 avctx->skip_frame >= AVDISCARD_ALL) {
5539 if (s->next_p_frame_damaged) {
5540 if (s->pict_type == AV_PICTURE_TYPE_B)
5543 s->next_p_frame_damaged = 0;
5546 if (ff_MPV_frame_start(s, avctx) < 0) {
5550 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5551 v->s.current_picture_ptr->f.top_field_first = v->tff;
5553 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5554 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5556 if ((CONFIG_VC1_VDPAU_DECODER)
5557 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5558 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5559 else if (avctx->hwaccel) {
5560 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
5562 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5564 if (avctx->hwaccel->end_frame(avctx) < 0)
5567 ff_er_frame_start(s);
5569 v->bits = buf_size * 8;
5570 if (v->field_mode) {
5572 s->current_picture.f.linesize[0] <<= 1;
5573 s->current_picture.f.linesize[1] <<= 1;
5574 s->current_picture.f.linesize[2] <<= 1;
5576 s->uvlinesize <<= 1;
5577 tmp[0] = v->mv_f_last[0];
5578 tmp[1] = v->mv_f_last[1];
5579 v->mv_f_last[0] = v->mv_f_next[0];
5580 v->mv_f_last[1] = v->mv_f_next[1];
5581 v->mv_f_next[0] = v->mv_f[0];
5582 v->mv_f_next[1] = v->mv_f[1];
5583 v->mv_f[0] = tmp[0];
5584 v->mv_f[1] = tmp[1];
5586 mb_height = s->mb_height >> v->field_mode;
5587 for (i = 0; i <= n_slices; i++) {
5588 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5589 if (v->field_mode <= 0) {
5590 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5591 "picture boundary (%d >= %d)\n", i,
5592 slices[i - 1].mby_start, mb_height);
5595 v->second_field = 1;
5596 v->blocks_off = s->mb_width * s->mb_height << 1;
5597 v->mb_off = s->mb_stride * s->mb_height >> 1;
5599 v->second_field = 0;
5604 v->pic_header_flag = 0;
5605 if (v->field_mode && i == n_slices1 + 2) {
5606 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5607 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5610 } else if (get_bits1(&s->gb)) {
5611 v->pic_header_flag = 1;
5612 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5613 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5618 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5619 if (!v->field_mode || v->second_field)
5620 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5622 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5623 if (s->end_mb_y <= s->start_mb_y) {
5624 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5627 vc1_decode_blocks(v);
5629 s->gb = slices[i].gb;
5631 if (v->field_mode) {
5632 v->second_field = 0;
5633 if (s->pict_type == AV_PICTURE_TYPE_B) {
5634 memcpy(v->mv_f_base, v->mv_f_next_base,
5635 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5637 s->current_picture.f.linesize[0] >>= 1;
5638 s->current_picture.f.linesize[1] >>= 1;
5639 s->current_picture.f.linesize[2] >>= 1;
5641 s->uvlinesize >>= 1;
5643 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), s->gb.size_in_bits);
5644 // if (get_bits_count(&s->gb) > buf_size * 8)
5646 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5652 ff_MPV_frame_end(s);
5654 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5656 avctx->width = avctx->coded_width = v->output_width;
5657 avctx->height = avctx->coded_height = v->output_height;
5658 if (avctx->skip_frame >= AVDISCARD_NONREF)
5660 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5661 if (vc1_decode_sprites(v, &s->gb))
5664 *pict = v->sprite_output_frame;
5665 *data_size = sizeof(AVFrame);
5667 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5668 *pict = s->current_picture_ptr->f;
5669 } else if (s->last_picture_ptr != NULL) {
5670 *pict = s->last_picture_ptr->f;
5672 if (s->last_picture_ptr || s->low_delay) {
5673 *data_size = sizeof(AVFrame);
5674 ff_print_debug_info(s, pict);
5680 for (i = 0; i < n_slices; i++)
5681 av_free(slices[i].buf);
5687 for (i = 0; i < n_slices; i++)
5688 av_free(slices[i].buf);
5694 static const AVProfile profiles[] = {
5695 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5696 { FF_PROFILE_VC1_MAIN, "Main" },
5697 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5698 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5699 { FF_PROFILE_UNKNOWN },
5702 AVCodec ff_vc1_decoder = {
5704 .type = AVMEDIA_TYPE_VIDEO,
5706 .priv_data_size = sizeof(VC1Context),
5707 .init = vc1_decode_init,
5708 .close = vc1_decode_end,
5709 .decode = vc1_decode_frame,
5710 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5711 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5712 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5713 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5716 #if CONFIG_WMV3_DECODER
5717 AVCodec ff_wmv3_decoder = {
5719 .type = AVMEDIA_TYPE_VIDEO,
5720 .id = CODEC_ID_WMV3,
5721 .priv_data_size = sizeof(VC1Context),
5722 .init = vc1_decode_init,
5723 .close = vc1_decode_end,
5724 .decode = vc1_decode_frame,
5725 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5726 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5727 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5728 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5732 #if CONFIG_WMV3_VDPAU_DECODER
5733 AVCodec ff_wmv3_vdpau_decoder = {
5734 .name = "wmv3_vdpau",
5735 .type = AVMEDIA_TYPE_VIDEO,
5736 .id = CODEC_ID_WMV3,
5737 .priv_data_size = sizeof(VC1Context),
5738 .init = vc1_decode_init,
5739 .close = vc1_decode_end,
5740 .decode = vc1_decode_frame,
5741 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5742 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5743 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE },
5744 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5748 #if CONFIG_VC1_VDPAU_DECODER
5749 AVCodec ff_vc1_vdpau_decoder = {
5750 .name = "vc1_vdpau",
5751 .type = AVMEDIA_TYPE_VIDEO,
5753 .priv_data_size = sizeof(VC1Context),
5754 .init = vc1_decode_init,
5755 .close = vc1_decode_end,
5756 .decode = vc1_decode_frame,
5757 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5758 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5759 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_VC1, PIX_FMT_NONE },
5760 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5764 #if CONFIG_WMV3IMAGE_DECODER
5765 AVCodec ff_wmv3image_decoder = {
5766 .name = "wmv3image",
5767 .type = AVMEDIA_TYPE_VIDEO,
5768 .id = CODEC_ID_WMV3IMAGE,
5769 .priv_data_size = sizeof(VC1Context),
5770 .init = vc1_decode_init,
5771 .close = vc1_decode_end,
5772 .decode = vc1_decode_frame,
5773 .capabilities = CODEC_CAP_DR1,
5774 .flush = vc1_sprite_flush,
5775 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5776 .pix_fmts = ff_pixfmt_list_420
5780 #if CONFIG_VC1IMAGE_DECODER
5781 AVCodec ff_vc1image_decoder = {
5783 .type = AVMEDIA_TYPE_VIDEO,
5784 .id = CODEC_ID_VC1IMAGE,
5785 .priv_data_size = sizeof(VC1Context),
5786 .init = vc1_decode_init,
5787 .close = vc1_decode_end,
5788 .decode = vc1_decode_frame,
5789 .capabilities = CODEC_CAP_DR1,
5790 .flush = vc1_sprite_flush,
5791 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5792 .pix_fmts = ff_pixfmt_list_420