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
31 #include "error_resilience.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
35 #include "h264chroma.h"
39 #include "vc1acdata.h"
40 #include "msmpeg4data.h"
43 #include "vdpau_internal.h"
44 #include "libavutil/avassert.h"
49 #define MB_INTRA_VLC_BITS 9
53 // offset tables for interlaced picture MVDATA decoding
54 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
55 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
57 /***********************************************************************/
59 * @name VC-1 Bitplane decoding
65 static void init_block_index(VC1Context *v)
67 MpegEncContext *s = &v->s;
68 ff_init_block_index(s);
69 if (v->field_mode && !(v->second_field ^ v->tff)) {
70 s->dest[0] += s->current_picture_ptr->f->linesize[0];
71 s->dest[1] += s->current_picture_ptr->f->linesize[1];
72 s->dest[2] += s->current_picture_ptr->f->linesize[2];
76 /** @} */ //Bitplane group
78 static void vc1_put_signed_blocks_clamped(VC1Context *v)
80 MpegEncContext *s = &v->s;
81 int topleft_mb_pos, top_mb_pos;
82 int stride_y, fieldtx = 0;
85 /* The put pixels loop is always one MB row behind the decoding loop,
86 * because we can only put pixels when overlap filtering is done, and
87 * for filtering of the bottom edge of a MB, we need the next MB row
89 * Within the row, the put pixels loop is also one MB col behind the
90 * decoding loop. The reason for this is again, because for filtering
91 * of the right MB edge, we need the next MB present. */
92 if (!s->first_slice_line) {
94 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
95 if (v->fcm == ILACE_FRAME)
96 fieldtx = v->fieldtx_plane[topleft_mb_pos];
97 stride_y = s->linesize << fieldtx;
98 v_dist = (16 - fieldtx) >> (fieldtx == 0);
99 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
100 s->dest[0] - 16 * s->linesize - 16,
102 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
103 s->dest[0] - 16 * s->linesize - 8,
105 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
106 s->dest[0] - v_dist * s->linesize - 16,
108 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
109 s->dest[0] - v_dist * s->linesize - 8,
111 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
112 s->dest[1] - 8 * s->uvlinesize - 8,
114 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
115 s->dest[2] - 8 * s->uvlinesize - 8,
118 if (s->mb_x == s->mb_width - 1) {
119 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
120 if (v->fcm == ILACE_FRAME)
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 H264ChromaContext *h264chroma = &v->h264chroma;
336 uint8_t *srcY, *srcU, *srcV;
337 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
338 int v_edge_pos = s->v_edge_pos >> v->field_mode;
340 uint8_t (*luty)[256], (*lutuv)[256];
343 if ((!v->field_mode ||
344 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
345 !v->s.last_picture.f->data[0])
348 mx = s->mv[dir][0][0];
349 my = s->mv[dir][0][1];
351 // store motion vectors for further use in B frames
352 if (s->pict_type == AV_PICTURE_TYPE_P) {
353 for (i = 0; i < 4; i++) {
354 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
355 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
359 uvmx = (mx + ((mx & 3) == 3)) >> 1;
360 uvmy = (my + ((my & 3) == 3)) >> 1;
361 v->luma_mv[s->mb_x][0] = uvmx;
362 v->luma_mv[s->mb_x][1] = uvmy;
365 v->cur_field_type != v->ref_field_type[dir]) {
366 my = my - 2 + 4 * v->cur_field_type;
367 uvmy = uvmy - 2 + 4 * v->cur_field_type;
370 // fastuvmc shall be ignored for interlaced frame picture
371 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
372 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
373 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
376 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
377 srcY = s->current_picture.f->data[0];
378 srcU = s->current_picture.f->data[1];
379 srcV = s->current_picture.f->data[2];
381 lutuv = v->curr_lutuv;
382 use_ic = *v->curr_use_ic;
384 srcY = s->last_picture.f->data[0];
385 srcU = s->last_picture.f->data[1];
386 srcV = s->last_picture.f->data[2];
388 lutuv = v->last_lutuv;
389 use_ic = v->last_use_ic;
392 srcY = s->next_picture.f->data[0];
393 srcU = s->next_picture.f->data[1];
394 srcV = s->next_picture.f->data[2];
396 lutuv = v->next_lutuv;
397 use_ic = v->next_use_ic;
400 if (!srcY || !srcU) {
401 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
405 src_x = s->mb_x * 16 + (mx >> 2);
406 src_y = s->mb_y * 16 + (my >> 2);
407 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
408 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
410 if (v->profile != PROFILE_ADVANCED) {
411 src_x = av_clip( src_x, -16, s->mb_width * 16);
412 src_y = av_clip( src_y, -16, s->mb_height * 16);
413 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
414 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
416 src_x = av_clip( src_x, -17, s->avctx->coded_width);
417 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
418 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
419 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
422 srcY += src_y * s->linesize + src_x;
423 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
424 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
426 if (v->field_mode && v->ref_field_type[dir]) {
427 srcY += s->current_picture_ptr->f->linesize[0];
428 srcU += s->current_picture_ptr->f->linesize[1];
429 srcV += s->current_picture_ptr->f->linesize[2];
432 /* for grayscale we should not try to read from unknown area */
433 if (s->flags & CODEC_FLAG_GRAY) {
434 srcU = s->edge_emu_buffer + 18 * s->linesize;
435 srcV = s->edge_emu_buffer + 18 * s->linesize;
438 if (v->rangeredfrm || use_ic
439 || s->h_edge_pos < 22 || v_edge_pos < 22
440 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
441 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
442 uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
443 uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
445 srcY -= s->mspel * (1 + s->linesize);
446 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
447 s->linesize, s->linesize,
448 17 + s->mspel * 2, 17 + s->mspel * 2,
449 src_x - s->mspel, src_y - s->mspel,
450 s->h_edge_pos, v_edge_pos);
451 srcY = s->edge_emu_buffer;
452 s->vdsp.emulated_edge_mc(ubuf, srcU,
453 s->uvlinesize, s->uvlinesize,
456 s->h_edge_pos >> 1, v_edge_pos >> 1);
457 s->vdsp.emulated_edge_mc(vbuf, srcV,
458 s->uvlinesize, s->uvlinesize,
461 s->h_edge_pos >> 1, v_edge_pos >> 1);
464 /* if we deal with range reduction we need to scale source blocks */
465 if (v->rangeredfrm) {
470 for (j = 0; j < 17 + s->mspel * 2; j++) {
471 for (i = 0; i < 17 + s->mspel * 2; i++)
472 src[i] = ((src[i] - 128) >> 1) + 128;
477 for (j = 0; j < 9; j++) {
478 for (i = 0; i < 9; i++) {
479 src[i] = ((src[i] - 128) >> 1) + 128;
480 src2[i] = ((src2[i] - 128) >> 1) + 128;
482 src += s->uvlinesize;
483 src2 += s->uvlinesize;
486 /* if we deal with intensity compensation we need to scale source blocks */
492 for (j = 0; j < 17 + s->mspel * 2; j++) {
493 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
494 for (i = 0; i < 17 + s->mspel * 2; i++)
495 src[i] = luty[f][src[i]];
500 for (j = 0; j < 9; j++) {
501 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
502 for (i = 0; i < 9; i++) {
503 src[i] = lutuv[f][src[i]];
504 src2[i] = lutuv[f][src2[i]];
506 src += s->uvlinesize;
507 src2 += s->uvlinesize;
510 srcY += s->mspel * (1 + s->linesize);
514 dxy = ((my & 3) << 2) | (mx & 3);
515 v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0] , srcY , s->linesize, v->rnd);
516 } else { // hpel mc - always used for luma
517 dxy = (my & 2) | ((mx & 2) >> 1);
519 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
521 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
524 if (s->flags & CODEC_FLAG_GRAY) return;
525 /* Chroma MC always uses qpel bilinear */
526 uvmx = (uvmx & 3) << 1;
527 uvmy = (uvmy & 3) << 1;
529 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
530 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
532 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
533 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
537 static inline int median4(int a, int b, int c, int d)
540 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
541 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
543 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
544 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
548 /** Do motion compensation for 4-MV macroblock - luminance block
550 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
552 MpegEncContext *s = &v->s;
554 int dxy, mx, my, src_x, src_y;
556 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
557 int v_edge_pos = s->v_edge_pos >> v->field_mode;
558 uint8_t (*luty)[256];
561 if ((!v->field_mode ||
562 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
563 !v->s.last_picture.f->data[0])
566 mx = s->mv[dir][n][0];
567 my = s->mv[dir][n][1];
570 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
571 srcY = s->current_picture.f->data[0];
573 use_ic = *v->curr_use_ic;
575 srcY = s->last_picture.f->data[0];
577 use_ic = v->last_use_ic;
580 srcY = s->next_picture.f->data[0];
582 use_ic = v->next_use_ic;
586 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
591 if (v->cur_field_type != v->ref_field_type[dir])
592 my = my - 2 + 4 * v->cur_field_type;
595 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
596 int same_count = 0, opp_count = 0, k;
597 int chosen_mv[2][4][2], f;
599 for (k = 0; k < 4; k++) {
600 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
601 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
602 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
606 f = opp_count > same_count;
607 switch (f ? opp_count : same_count) {
609 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
610 chosen_mv[f][2][0], chosen_mv[f][3][0]);
611 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
612 chosen_mv[f][2][1], chosen_mv[f][3][1]);
615 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
616 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
619 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
620 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
625 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
626 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
627 for (k = 0; k < 4; k++)
628 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
631 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
633 int width = s->avctx->coded_width;
634 int height = s->avctx->coded_height >> 1;
635 if (s->pict_type == AV_PICTURE_TYPE_P) {
636 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
637 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
639 qx = (s->mb_x * 16) + (mx >> 2);
640 qy = (s->mb_y * 8) + (my >> 3);
645 mx -= 4 * (qx - width);
648 else if (qy > height + 1)
649 my -= 8 * (qy - height - 1);
652 if ((v->fcm == ILACE_FRAME) && fieldmv)
653 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
655 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
657 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
659 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
661 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
663 if (v->profile != PROFILE_ADVANCED) {
664 src_x = av_clip(src_x, -16, s->mb_width * 16);
665 src_y = av_clip(src_y, -16, s->mb_height * 16);
667 src_x = av_clip(src_x, -17, s->avctx->coded_width);
668 if (v->fcm == ILACE_FRAME) {
670 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
672 src_y = av_clip(src_y, -18, s->avctx->coded_height);
674 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
678 srcY += src_y * s->linesize + src_x;
679 if (v->field_mode && v->ref_field_type[dir])
680 srcY += s->current_picture_ptr->f->linesize[0];
682 if (fieldmv && !(src_y & 1))
684 if (fieldmv && (src_y & 1) && src_y < 4)
686 if (v->rangeredfrm || use_ic
687 || s->h_edge_pos < 13 || v_edge_pos < 23
688 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
689 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
690 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
691 /* check emulate edge stride and offset */
692 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
693 s->linesize, s->linesize,
694 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
695 src_x - s->mspel, src_y - (s->mspel << fieldmv),
696 s->h_edge_pos, v_edge_pos);
697 srcY = s->edge_emu_buffer;
698 /* if we deal with range reduction we need to scale source blocks */
699 if (v->rangeredfrm) {
704 for (j = 0; j < 9 + s->mspel * 2; j++) {
705 for (i = 0; i < 9 + s->mspel * 2; i++)
706 src[i] = ((src[i] - 128) >> 1) + 128;
707 src += s->linesize << fieldmv;
710 /* if we deal with intensity compensation we need to scale source blocks */
716 for (j = 0; j < 9 + s->mspel * 2; j++) {
717 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
718 for (i = 0; i < 9 + s->mspel * 2; i++)
719 src[i] = luty[f][src[i]];
720 src += s->linesize << fieldmv;
723 srcY += s->mspel * (1 + (s->linesize << fieldmv));
727 dxy = ((my & 3) << 2) | (mx & 3);
729 v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
731 v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
732 } else { // hpel mc - always used for luma
733 dxy = (my & 2) | ((mx & 2) >> 1);
735 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
737 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
741 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
744 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
746 idx = ((a[3] != flag) << 3)
747 | ((a[2] != flag) << 2)
748 | ((a[1] != flag) << 1)
751 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
752 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
754 } else if (count[idx] == 1) {
757 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
758 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
761 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
762 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
765 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
766 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
769 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
770 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
773 } else if (count[idx] == 2) {
775 for (i = 0; i < 3; i++)
780 for (i = t1 + 1; i < 4; i++)
785 *tx = (mvx[t1] + mvx[t2]) / 2;
786 *ty = (mvy[t1] + mvy[t2]) / 2;
794 /** Do motion compensation for 4-MV macroblock - both chroma blocks
796 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
798 MpegEncContext *s = &v->s;
799 H264ChromaContext *h264chroma = &v->h264chroma;
800 uint8_t *srcU, *srcV;
801 int uvmx, uvmy, uvsrc_x, uvsrc_y;
802 int k, tx = 0, ty = 0;
803 int mvx[4], mvy[4], intra[4], mv_f[4];
805 int chroma_ref_type = v->cur_field_type;
806 int v_edge_pos = s->v_edge_pos >> v->field_mode;
807 uint8_t (*lutuv)[256];
810 if (!v->field_mode && !v->s.last_picture.f->data[0])
812 if (s->flags & CODEC_FLAG_GRAY)
815 for (k = 0; k < 4; k++) {
816 mvx[k] = s->mv[dir][k][0];
817 mvy[k] = s->mv[dir][k][1];
818 intra[k] = v->mb_type[0][s->block_index[k]];
820 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
823 /* calculate chroma MV vector from four luma MVs */
824 if (!v->field_mode || (v->field_mode && !v->numref)) {
825 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
826 chroma_ref_type = v->reffield;
828 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
829 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
830 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
831 return; //no need to do MC for intra blocks
835 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
837 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
839 chroma_ref_type = !v->cur_field_type;
841 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
843 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
844 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
845 uvmx = (tx + ((tx & 3) == 3)) >> 1;
846 uvmy = (ty + ((ty & 3) == 3)) >> 1;
848 v->luma_mv[s->mb_x][0] = uvmx;
849 v->luma_mv[s->mb_x][1] = uvmy;
852 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
853 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
855 // Field conversion bias
856 if (v->cur_field_type != chroma_ref_type)
857 uvmy += 2 - 4 * chroma_ref_type;
859 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
860 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
862 if (v->profile != PROFILE_ADVANCED) {
863 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
864 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
866 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
867 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
871 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
872 srcU = s->current_picture.f->data[1];
873 srcV = s->current_picture.f->data[2];
874 lutuv = v->curr_lutuv;
875 use_ic = *v->curr_use_ic;
877 srcU = s->last_picture.f->data[1];
878 srcV = s->last_picture.f->data[2];
879 lutuv = v->last_lutuv;
880 use_ic = v->last_use_ic;
883 srcU = s->next_picture.f->data[1];
884 srcV = s->next_picture.f->data[2];
885 lutuv = v->next_lutuv;
886 use_ic = v->next_use_ic;
890 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
894 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
895 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
898 if (chroma_ref_type) {
899 srcU += s->current_picture_ptr->f->linesize[1];
900 srcV += s->current_picture_ptr->f->linesize[2];
904 if (v->rangeredfrm || use_ic
905 || s->h_edge_pos < 18 || v_edge_pos < 18
906 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
907 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
908 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
909 s->uvlinesize, s->uvlinesize,
910 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
911 s->h_edge_pos >> 1, v_edge_pos >> 1);
912 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
913 s->uvlinesize, s->uvlinesize,
914 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
915 s->h_edge_pos >> 1, v_edge_pos >> 1);
916 srcU = s->edge_emu_buffer;
917 srcV = s->edge_emu_buffer + 16;
919 /* if we deal with range reduction we need to scale source blocks */
920 if (v->rangeredfrm) {
926 for (j = 0; j < 9; j++) {
927 for (i = 0; i < 9; i++) {
928 src[i] = ((src[i] - 128) >> 1) + 128;
929 src2[i] = ((src2[i] - 128) >> 1) + 128;
931 src += s->uvlinesize;
932 src2 += s->uvlinesize;
935 /* if we deal with intensity compensation we need to scale source blocks */
942 for (j = 0; j < 9; j++) {
943 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
944 for (i = 0; i < 9; i++) {
945 src[i] = lutuv[f][src[i]];
946 src2[i] = lutuv[f][src2[i]];
948 src += s->uvlinesize;
949 src2 += s->uvlinesize;
954 /* Chroma MC always uses qpel bilinear */
955 uvmx = (uvmx & 3) << 1;
956 uvmy = (uvmy & 3) << 1;
958 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
959 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
961 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
962 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
966 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
968 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
970 MpegEncContext *s = &v->s;
971 H264ChromaContext *h264chroma = &v->h264chroma;
972 uint8_t *srcU, *srcV;
973 int uvsrc_x, uvsrc_y;
974 int uvmx_field[4], uvmy_field[4];
976 int fieldmv = v->blk_mv_type[s->block_index[0]];
977 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
978 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
979 int v_edge_pos = s->v_edge_pos >> 1;
981 uint8_t (*lutuv)[256];
983 if (s->flags & CODEC_FLAG_GRAY)
986 for (i = 0; i < 4; i++) {
987 int d = i < 2 ? dir: dir2;
989 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
992 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
994 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
997 for (i = 0; i < 4; i++) {
998 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
999 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1000 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1001 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1002 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1003 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1004 if (i < 2 ? dir : dir2) {
1005 srcU = s->next_picture.f->data[1];
1006 srcV = s->next_picture.f->data[2];
1007 lutuv = v->next_lutuv;
1008 use_ic = v->next_use_ic;
1010 srcU = s->last_picture.f->data[1];
1011 srcV = s->last_picture.f->data[2];
1012 lutuv = v->last_lutuv;
1013 use_ic = v->last_use_ic;
1017 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1018 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1019 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1020 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1022 if (fieldmv && !(uvsrc_y & 1))
1023 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1025 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1028 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1029 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1030 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1031 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
1032 s->uvlinesize, s->uvlinesize,
1033 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1034 s->h_edge_pos >> 1, v_edge_pos);
1035 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
1036 s->uvlinesize, s->uvlinesize,
1037 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1038 s->h_edge_pos >> 1, v_edge_pos);
1039 srcU = s->edge_emu_buffer;
1040 srcV = s->edge_emu_buffer + 16;
1042 /* if we deal with intensity compensation we need to scale source blocks */
1045 uint8_t *src, *src2;
1049 for (j = 0; j < 5; j++) {
1050 int f = (uvsrc_y + (j << fieldmv)) & 1;
1051 for (i = 0; i < 5; i++) {
1052 src[i] = lutuv[f][src[i]];
1053 src2[i] = lutuv[f][src2[i]];
1055 src += s->uvlinesize << fieldmv;
1056 src2 += s->uvlinesize << fieldmv;
1062 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1063 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1065 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1066 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1070 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1071 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1073 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]);
1074 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]);
1080 /***********************************************************************/
1082 * @name VC-1 Block-level functions
1083 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1089 * @brief Get macroblock-level quantizer scale
1091 #define GET_MQUANT() \
1092 if (v->dquantfrm) { \
1094 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1095 if (v->dqbilevel) { \
1096 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1098 mqdiff = get_bits(gb, 3); \
1100 mquant = v->pq + mqdiff; \
1102 mquant = get_bits(gb, 5); \
1105 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1106 edges = 1 << v->dqsbedge; \
1107 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1108 edges = (3 << v->dqsbedge) % 15; \
1109 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1111 if ((edges&1) && !s->mb_x) \
1112 mquant = v->altpq; \
1113 if ((edges&2) && s->first_slice_line) \
1114 mquant = v->altpq; \
1115 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1116 mquant = v->altpq; \
1117 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1118 mquant = v->altpq; \
1119 if (!mquant || mquant > 31) { \
1120 av_log(v->s.avctx, AV_LOG_ERROR, \
1121 "Overriding invalid mquant %d\n", mquant); \
1127 * @def GET_MVDATA(_dmv_x, _dmv_y)
1128 * @brief Get MV differentials
1129 * @see MVDATA decoding from 8.3.5.2, p(1)20
1130 * @param _dmv_x Horizontal differential for decoded MV
1131 * @param _dmv_y Vertical differential for decoded MV
1133 #define GET_MVDATA(_dmv_x, _dmv_y) \
1134 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1135 VC1_MV_DIFF_VLC_BITS, 2); \
1137 mb_has_coeffs = 1; \
1140 mb_has_coeffs = 0; \
1143 _dmv_x = _dmv_y = 0; \
1144 } else if (index == 35) { \
1145 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1146 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1147 } else if (index == 36) { \
1152 index1 = index % 6; \
1153 if (!s->quarter_sample && index1 == 5) val = 1; \
1155 if (size_table[index1] - val > 0) \
1156 val = get_bits(gb, size_table[index1] - val); \
1158 sign = 0 - (val&1); \
1159 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1161 index1 = index / 6; \
1162 if (!s->quarter_sample && index1 == 5) val = 1; \
1164 if (size_table[index1] - val > 0) \
1165 val = get_bits(gb, size_table[index1] - val); \
1167 sign = 0 - (val & 1); \
1168 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1171 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1172 int *dmv_y, int *pred_flag)
1175 int extend_x = 0, extend_y = 0;
1176 GetBitContext *gb = &v->s.gb;
1179 const int* offs_tab;
1182 bits = VC1_2REF_MVDATA_VLC_BITS;
1185 bits = VC1_1REF_MVDATA_VLC_BITS;
1188 switch (v->dmvrange) {
1196 extend_x = extend_y = 1;
1199 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1201 *dmv_x = get_bits(gb, v->k_x);
1202 *dmv_y = get_bits(gb, v->k_y);
1205 *pred_flag = *dmv_y & 1;
1206 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1208 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1213 av_assert0(index < esc);
1215 offs_tab = offset_table2;
1217 offs_tab = offset_table1;
1218 index1 = (index + 1) % 9;
1220 val = get_bits(gb, index1 + extend_x);
1221 sign = 0 -(val & 1);
1222 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1226 offs_tab = offset_table2;
1228 offs_tab = offset_table1;
1229 index1 = (index + 1) / 9;
1230 if (index1 > v->numref) {
1231 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1232 sign = 0 - (val & 1);
1233 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1236 if (v->numref && pred_flag)
1237 *pred_flag = index1 & 1;
1241 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1243 int scaledvalue, refdist;
1244 int scalesame1, scalesame2;
1245 int scalezone1_x, zone1offset_x;
1246 int table_index = dir ^ v->second_field;
1248 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1249 refdist = v->refdist;
1251 refdist = dir ? v->brfd : v->frfd;
1254 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1255 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1256 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1257 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1262 if (FFABS(n) < scalezone1_x)
1263 scaledvalue = (n * scalesame1) >> 8;
1266 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1268 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1271 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1274 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1276 int scaledvalue, refdist;
1277 int scalesame1, scalesame2;
1278 int scalezone1_y, zone1offset_y;
1279 int table_index = dir ^ v->second_field;
1281 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1282 refdist = v->refdist;
1284 refdist = dir ? v->brfd : v->frfd;
1287 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1288 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1289 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1290 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1295 if (FFABS(n) < scalezone1_y)
1296 scaledvalue = (n * scalesame1) >> 8;
1299 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1301 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1305 if (v->cur_field_type && !v->ref_field_type[dir])
1306 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1308 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1311 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1313 int scalezone1_x, zone1offset_x;
1314 int scaleopp1, scaleopp2, brfd;
1317 brfd = FFMIN(v->brfd, 3);
1318 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1319 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1320 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1321 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1326 if (FFABS(n) < scalezone1_x)
1327 scaledvalue = (n * scaleopp1) >> 8;
1330 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1332 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1335 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1338 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1340 int scalezone1_y, zone1offset_y;
1341 int scaleopp1, scaleopp2, brfd;
1344 brfd = FFMIN(v->brfd, 3);
1345 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1346 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1347 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1348 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1353 if (FFABS(n) < scalezone1_y)
1354 scaledvalue = (n * scaleopp1) >> 8;
1357 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1359 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1362 if (v->cur_field_type && !v->ref_field_type[dir]) {
1363 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1365 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1369 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1372 int brfd, scalesame;
1373 int hpel = 1 - v->s.quarter_sample;
1376 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1378 n = scaleforsame_y(v, i, n, dir) << hpel;
1380 n = scaleforsame_x(v, n, dir) << hpel;
1383 brfd = FFMIN(v->brfd, 3);
1384 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1386 n = (n * scalesame >> 8) << hpel;
1390 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1393 int refdist, scaleopp;
1394 int hpel = 1 - v->s.quarter_sample;
1397 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1399 n = scaleforopp_y(v, n, dir) << hpel;
1401 n = scaleforopp_x(v, n) << hpel;
1404 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1405 refdist = FFMIN(v->refdist, 3);
1407 refdist = dir ? v->brfd : v->frfd;
1408 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1410 n = (n * scaleopp >> 8) << hpel;
1414 /** Predict and set motion vector
1416 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1417 int mv1, int r_x, int r_y, uint8_t* is_intra,
1418 int pred_flag, int dir)
1420 MpegEncContext *s = &v->s;
1421 int xy, wrap, off = 0;
1425 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1426 int opposite, a_f, b_f, c_f;
1427 int16_t field_predA[2];
1428 int16_t field_predB[2];
1429 int16_t field_predC[2];
1430 int a_valid, b_valid, c_valid;
1431 int hybridmv_thresh, y_bias = 0;
1433 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1434 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1438 /* scale MV difference to be quad-pel */
1439 dmv_x <<= 1 - s->quarter_sample;
1440 dmv_y <<= 1 - s->quarter_sample;
1442 wrap = s->b8_stride;
1443 xy = s->block_index[n];
1446 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1447 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1448 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1449 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1450 if (mv1) { /* duplicate motion data for 1-MV block */
1451 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1452 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1453 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1454 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1455 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1456 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1457 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1458 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1459 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1460 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1461 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1462 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1463 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1468 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1469 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1471 if (v->field_mode && mixedmv_pic)
1472 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1474 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1476 //in 4-MV mode different blocks have different B predictor position
1479 off = (s->mb_x > 0) ? -1 : 1;
1482 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1491 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1493 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1494 b_valid = a_valid && (s->mb_width > 1);
1495 c_valid = s->mb_x || (n == 1 || n == 3);
1496 if (v->field_mode) {
1497 a_valid = a_valid && !is_intra[xy - wrap];
1498 b_valid = b_valid && !is_intra[xy - wrap + off];
1499 c_valid = c_valid && !is_intra[xy - 1];
1503 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1504 num_oppfield += a_f;
1505 num_samefield += 1 - a_f;
1506 field_predA[0] = A[0];
1507 field_predA[1] = A[1];
1509 field_predA[0] = field_predA[1] = 0;
1513 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1514 num_oppfield += b_f;
1515 num_samefield += 1 - b_f;
1516 field_predB[0] = B[0];
1517 field_predB[1] = B[1];
1519 field_predB[0] = field_predB[1] = 0;
1523 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1524 num_oppfield += c_f;
1525 num_samefield += 1 - c_f;
1526 field_predC[0] = C[0];
1527 field_predC[1] = C[1];
1529 field_predC[0] = field_predC[1] = 0;
1533 if (v->field_mode) {
1535 // REFFIELD determines if the last field or the second-last field is
1536 // to be used as reference
1537 opposite = 1 - v->reffield;
1539 if (num_samefield <= num_oppfield)
1540 opposite = 1 - pred_flag;
1542 opposite = pred_flag;
1547 if (a_valid && !a_f) {
1548 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1549 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1551 if (b_valid && !b_f) {
1552 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1553 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1555 if (c_valid && !c_f) {
1556 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1557 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1559 v->mv_f[dir][xy + v->blocks_off] = 1;
1560 v->ref_field_type[dir] = !v->cur_field_type;
1562 if (a_valid && a_f) {
1563 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1564 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1566 if (b_valid && b_f) {
1567 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1568 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1570 if (c_valid && c_f) {
1571 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1572 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1574 v->mv_f[dir][xy + v->blocks_off] = 0;
1575 v->ref_field_type[dir] = v->cur_field_type;
1579 px = field_predA[0];
1580 py = field_predA[1];
1581 } else if (c_valid) {
1582 px = field_predC[0];
1583 py = field_predC[1];
1584 } else if (b_valid) {
1585 px = field_predB[0];
1586 py = field_predB[1];
1592 if (num_samefield + num_oppfield > 1) {
1593 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1594 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1597 /* Pullback MV as specified in 8.3.5.3.4 */
1598 if (!v->field_mode) {
1600 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1601 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1602 X = (s->mb_width << 6) - 4;
1603 Y = (s->mb_height << 6) - 4;
1605 if (qx + px < -60) px = -60 - qx;
1606 if (qy + py < -60) py = -60 - qy;
1608 if (qx + px < -28) px = -28 - qx;
1609 if (qy + py < -28) py = -28 - qy;
1611 if (qx + px > X) px = X - qx;
1612 if (qy + py > Y) py = Y - qy;
1615 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1616 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1617 hybridmv_thresh = 32;
1618 if (a_valid && c_valid) {
1619 if (is_intra[xy - wrap])
1620 sum = FFABS(px) + FFABS(py);
1622 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1623 if (sum > hybridmv_thresh) {
1624 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1625 px = field_predA[0];
1626 py = field_predA[1];
1628 px = field_predC[0];
1629 py = field_predC[1];
1632 if (is_intra[xy - 1])
1633 sum = FFABS(px) + FFABS(py);
1635 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1636 if (sum > hybridmv_thresh) {
1637 if (get_bits1(&s->gb)) {
1638 px = field_predA[0];
1639 py = field_predA[1];
1641 px = field_predC[0];
1642 py = field_predC[1];
1649 if (v->field_mode && v->numref)
1651 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1653 /* store MV using signed modulus of MV range defined in 4.11 */
1654 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1655 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1656 if (mv1) { /* duplicate motion data for 1-MV block */
1657 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1658 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1659 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1660 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1661 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1662 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1663 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1664 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];
1668 /** Predict and set motion vector for interlaced frame picture MBs
1670 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1671 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1673 MpegEncContext *s = &v->s;
1674 int xy, wrap, off = 0;
1675 int A[2], B[2], C[2];
1677 int a_valid = 0, b_valid = 0, c_valid = 0;
1678 int field_a, field_b, field_c; // 0: same, 1: opposit
1679 int total_valid, num_samefield, num_oppfield;
1680 int pos_c, pos_b, n_adj;
1682 wrap = s->b8_stride;
1683 xy = s->block_index[n];
1686 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1687 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1688 s->current_picture.motion_val[1][xy][0] = 0;
1689 s->current_picture.motion_val[1][xy][1] = 0;
1690 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1691 s->current_picture.motion_val[0][xy + 1][0] = 0;
1692 s->current_picture.motion_val[0][xy + 1][1] = 0;
1693 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1694 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1695 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1696 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1697 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1698 s->current_picture.motion_val[1][xy + 1][0] = 0;
1699 s->current_picture.motion_val[1][xy + 1][1] = 0;
1700 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1701 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1702 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1703 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1708 off = ((n == 0) || (n == 1)) ? 1 : -1;
1710 if (s->mb_x || (n == 1) || (n == 3)) {
1711 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1712 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1713 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1714 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1716 } else { // current block has frame mv and cand. has field MV (so average)
1717 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1718 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1719 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1720 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1723 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1729 /* Predict B and C */
1730 B[0] = B[1] = C[0] = C[1] = 0;
1731 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1732 if (!s->first_slice_line) {
1733 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1736 pos_b = s->block_index[n_adj] - 2 * wrap;
1737 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1738 n_adj = (n & 2) | (n & 1);
1740 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1741 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1742 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1743 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1744 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1747 if (s->mb_width > 1) {
1748 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1751 pos_c = s->block_index[2] - 2 * wrap + 2;
1752 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1755 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1756 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1757 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1758 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1759 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1761 if (s->mb_x == s->mb_width - 1) {
1762 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1765 pos_c = s->block_index[3] - 2 * wrap - 2;
1766 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1769 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1770 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1771 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1772 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1773 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1782 pos_b = s->block_index[1];
1784 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1785 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1786 pos_c = s->block_index[0];
1788 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1789 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1792 total_valid = a_valid + b_valid + c_valid;
1793 // check if predictor A is out of bounds
1794 if (!s->mb_x && !(n == 1 || n == 3)) {
1797 // check if predictor B is out of bounds
1798 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1799 B[0] = B[1] = C[0] = C[1] = 0;
1801 if (!v->blk_mv_type[xy]) {
1802 if (s->mb_width == 1) {
1806 if (total_valid >= 2) {
1807 px = mid_pred(A[0], B[0], C[0]);
1808 py = mid_pred(A[1], B[1], C[1]);
1809 } else if (total_valid) {
1810 if (a_valid) { px = A[0]; py = A[1]; }
1811 else if (b_valid) { px = B[0]; py = B[1]; }
1812 else { px = C[0]; py = C[1]; }
1817 field_a = (A[1] & 4) ? 1 : 0;
1821 field_b = (B[1] & 4) ? 1 : 0;
1825 field_c = (C[1] & 4) ? 1 : 0;
1829 num_oppfield = field_a + field_b + field_c;
1830 num_samefield = total_valid - num_oppfield;
1831 if (total_valid == 3) {
1832 if ((num_samefield == 3) || (num_oppfield == 3)) {
1833 px = mid_pred(A[0], B[0], C[0]);
1834 py = mid_pred(A[1], B[1], C[1]);
1835 } else if (num_samefield >= num_oppfield) {
1836 /* take one MV from same field set depending on priority
1837 the check for B may not be necessary */
1838 px = !field_a ? A[0] : B[0];
1839 py = !field_a ? A[1] : B[1];
1841 px = field_a ? A[0] : B[0];
1842 py = field_a ? A[1] : B[1];
1844 } else if (total_valid == 2) {
1845 if (num_samefield >= num_oppfield) {
1846 if (!field_a && a_valid) {
1849 } else if (!field_b && b_valid) {
1852 } else /*if (c_valid)*/ {
1853 av_assert1(c_valid);
1856 } /*else px = py = 0;*/
1858 if (field_a && a_valid) {
1861 } else /*if (field_b && b_valid)*/ {
1862 av_assert1(field_b && b_valid);
1865 } /*else if (c_valid) {
1870 } else if (total_valid == 1) {
1871 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1872 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1876 /* store MV using signed modulus of MV range defined in 4.11 */
1877 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1878 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1879 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1880 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1881 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1882 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1883 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1884 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1885 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1886 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1887 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1888 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1889 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1890 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1894 /** Motion compensation for direct or interpolated blocks in B-frames
1896 static void vc1_interp_mc(VC1Context *v)
1898 MpegEncContext *s = &v->s;
1899 H264ChromaContext *h264chroma = &v->h264chroma;
1900 uint8_t *srcY, *srcU, *srcV;
1901 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1903 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1904 int use_ic = v->next_use_ic;
1906 if (!v->field_mode && !v->s.next_picture.f->data[0])
1909 mx = s->mv[1][0][0];
1910 my = s->mv[1][0][1];
1911 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1912 uvmy = (my + ((my & 3) == 3)) >> 1;
1913 if (v->field_mode) {
1914 if (v->cur_field_type != v->ref_field_type[1]) {
1915 my = my - 2 + 4 * v->cur_field_type;
1916 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1920 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1921 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1923 srcY = s->next_picture.f->data[0];
1924 srcU = s->next_picture.f->data[1];
1925 srcV = s->next_picture.f->data[2];
1927 src_x = s->mb_x * 16 + (mx >> 2);
1928 src_y = s->mb_y * 16 + (my >> 2);
1929 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1930 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1932 if (v->profile != PROFILE_ADVANCED) {
1933 src_x = av_clip( src_x, -16, s->mb_width * 16);
1934 src_y = av_clip( src_y, -16, s->mb_height * 16);
1935 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1936 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1938 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1939 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1940 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1941 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1944 srcY += src_y * s->linesize + src_x;
1945 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1946 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1948 if (v->field_mode && v->ref_field_type[1]) {
1949 srcY += s->current_picture_ptr->f->linesize[0];
1950 srcU += s->current_picture_ptr->f->linesize[1];
1951 srcV += s->current_picture_ptr->f->linesize[2];
1954 /* for grayscale we should not try to read from unknown area */
1955 if (s->flags & CODEC_FLAG_GRAY) {
1956 srcU = s->edge_emu_buffer + 18 * s->linesize;
1957 srcV = s->edge_emu_buffer + 18 * s->linesize;
1960 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1961 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1962 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1963 uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
1964 uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
1966 srcY -= s->mspel * (1 + s->linesize);
1967 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
1968 s->linesize, s->linesize,
1969 17 + s->mspel * 2, 17 + s->mspel * 2,
1970 src_x - s->mspel, src_y - s->mspel,
1971 s->h_edge_pos, v_edge_pos);
1972 srcY = s->edge_emu_buffer;
1973 s->vdsp.emulated_edge_mc(ubuf, srcU,
1974 s->uvlinesize, s->uvlinesize,
1977 s->h_edge_pos >> 1, v_edge_pos >> 1);
1978 s->vdsp.emulated_edge_mc(vbuf, srcV,
1979 s->uvlinesize, s->uvlinesize,
1982 s->h_edge_pos >> 1, v_edge_pos >> 1);
1985 /* if we deal with range reduction we need to scale source blocks */
1986 if (v->rangeredfrm) {
1988 uint8_t *src, *src2;
1991 for (j = 0; j < 17 + s->mspel * 2; j++) {
1992 for (i = 0; i < 17 + s->mspel * 2; i++)
1993 src[i] = ((src[i] - 128) >> 1) + 128;
1998 for (j = 0; j < 9; j++) {
1999 for (i = 0; i < 9; i++) {
2000 src[i] = ((src[i] - 128) >> 1) + 128;
2001 src2[i] = ((src2[i] - 128) >> 1) + 128;
2003 src += s->uvlinesize;
2004 src2 += s->uvlinesize;
2009 uint8_t (*luty )[256] = v->next_luty;
2010 uint8_t (*lutuv)[256] = v->next_lutuv;
2012 uint8_t *src, *src2;
2015 for (j = 0; j < 17 + s->mspel * 2; j++) {
2016 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2017 for (i = 0; i < 17 + s->mspel * 2; i++)
2018 src[i] = luty[f][src[i]];
2023 for (j = 0; j < 9; j++) {
2024 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2025 for (i = 0; i < 9; i++) {
2026 src[i] = lutuv[f][src[i]];
2027 src2[i] = lutuv[f][src2[i]];
2029 src += s->uvlinesize;
2030 src2 += s->uvlinesize;
2033 srcY += s->mspel * (1 + s->linesize);
2040 dxy = ((my & 3) << 2) | (mx & 3);
2041 v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2043 dxy = (my & 2) | ((mx & 2) >> 1);
2046 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2048 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2051 if (s->flags & CODEC_FLAG_GRAY) return;
2052 /* Chroma MC always uses qpel blilinear */
2053 uvmx = (uvmx & 3) << 1;
2054 uvmy = (uvmy & 3) << 1;
2056 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2057 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2059 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2060 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2064 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2068 #if B_FRACTION_DEN==256
2072 return 2 * ((value * n + 255) >> 9);
2073 return (value * n + 128) >> 8;
2076 n -= B_FRACTION_DEN;
2078 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2079 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2083 /** Reconstruct motion vector for B-frame and do motion compensation
2085 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2086 int direct, int mode)
2093 if (mode == BMV_TYPE_INTERPOLATED) {
2099 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2102 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2103 int direct, int mvtype)
2105 MpegEncContext *s = &v->s;
2106 int xy, wrap, off = 0;
2111 const uint8_t *is_intra = v->mb_type[0];
2113 av_assert0(!v->field_mode);
2117 /* scale MV difference to be quad-pel */
2118 dmv_x[0] <<= 1 - s->quarter_sample;
2119 dmv_y[0] <<= 1 - s->quarter_sample;
2120 dmv_x[1] <<= 1 - s->quarter_sample;
2121 dmv_y[1] <<= 1 - s->quarter_sample;
2123 wrap = s->b8_stride;
2124 xy = s->block_index[0];
2127 s->current_picture.motion_val[0][xy][0] =
2128 s->current_picture.motion_val[0][xy][1] =
2129 s->current_picture.motion_val[1][xy][0] =
2130 s->current_picture.motion_val[1][xy][1] = 0;
2133 if (direct && s->next_picture_ptr->field_picture)
2134 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2136 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2137 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2138 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2139 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2141 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2142 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));
2143 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));
2144 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));
2145 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));
2147 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2148 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2149 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2150 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2154 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2155 C = s->current_picture.motion_val[0][xy - 2];
2156 A = s->current_picture.motion_val[0][xy - wrap * 2];
2157 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2158 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2160 if (!s->mb_x) C[0] = C[1] = 0;
2161 if (!s->first_slice_line) { // predictor A is not out of bounds
2162 if (s->mb_width == 1) {
2166 px = mid_pred(A[0], B[0], C[0]);
2167 py = mid_pred(A[1], B[1], C[1]);
2169 } else if (s->mb_x) { // predictor C is not out of bounds
2175 /* Pullback MV as specified in 8.3.5.3.4 */
2178 if (v->profile < PROFILE_ADVANCED) {
2179 qx = (s->mb_x << 5);
2180 qy = (s->mb_y << 5);
2181 X = (s->mb_width << 5) - 4;
2182 Y = (s->mb_height << 5) - 4;
2183 if (qx + px < -28) px = -28 - qx;
2184 if (qy + py < -28) py = -28 - qy;
2185 if (qx + px > X) px = X - qx;
2186 if (qy + py > Y) py = Y - qy;
2188 qx = (s->mb_x << 6);
2189 qy = (s->mb_y << 6);
2190 X = (s->mb_width << 6) - 4;
2191 Y = (s->mb_height << 6) - 4;
2192 if (qx + px < -60) px = -60 - qx;
2193 if (qy + py < -60) py = -60 - qy;
2194 if (qx + px > X) px = X - qx;
2195 if (qy + py > Y) py = Y - qy;
2198 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2199 if (0 && !s->first_slice_line && s->mb_x) {
2200 if (is_intra[xy - wrap])
2201 sum = FFABS(px) + FFABS(py);
2203 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2205 if (get_bits1(&s->gb)) {
2213 if (is_intra[xy - 2])
2214 sum = FFABS(px) + FFABS(py);
2216 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2218 if (get_bits1(&s->gb)) {
2228 /* store MV using signed modulus of MV range defined in 4.11 */
2229 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2230 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2232 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2233 C = s->current_picture.motion_val[1][xy - 2];
2234 A = s->current_picture.motion_val[1][xy - wrap * 2];
2235 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2236 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2240 if (!s->first_slice_line) { // predictor A is not out of bounds
2241 if (s->mb_width == 1) {
2245 px = mid_pred(A[0], B[0], C[0]);
2246 py = mid_pred(A[1], B[1], C[1]);
2248 } else if (s->mb_x) { // predictor C is not out of bounds
2254 /* Pullback MV as specified in 8.3.5.3.4 */
2257 if (v->profile < PROFILE_ADVANCED) {
2258 qx = (s->mb_x << 5);
2259 qy = (s->mb_y << 5);
2260 X = (s->mb_width << 5) - 4;
2261 Y = (s->mb_height << 5) - 4;
2262 if (qx + px < -28) px = -28 - qx;
2263 if (qy + py < -28) py = -28 - qy;
2264 if (qx + px > X) px = X - qx;
2265 if (qy + py > Y) py = Y - qy;
2267 qx = (s->mb_x << 6);
2268 qy = (s->mb_y << 6);
2269 X = (s->mb_width << 6) - 4;
2270 Y = (s->mb_height << 6) - 4;
2271 if (qx + px < -60) px = -60 - qx;
2272 if (qy + py < -60) py = -60 - qy;
2273 if (qx + px > X) px = X - qx;
2274 if (qy + py > Y) py = Y - qy;
2277 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2278 if (0 && !s->first_slice_line && s->mb_x) {
2279 if (is_intra[xy - wrap])
2280 sum = FFABS(px) + FFABS(py);
2282 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2284 if (get_bits1(&s->gb)) {
2292 if (is_intra[xy - 2])
2293 sum = FFABS(px) + FFABS(py);
2295 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2297 if (get_bits1(&s->gb)) {
2307 /* store MV using signed modulus of MV range defined in 4.11 */
2309 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2310 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2312 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2313 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2314 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2315 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2318 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2320 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2321 MpegEncContext *s = &v->s;
2322 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2324 if (v->bmvtype == BMV_TYPE_DIRECT) {
2325 int total_opp, k, f;
2326 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2327 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2328 v->bfraction, 0, s->quarter_sample);
2329 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2330 v->bfraction, 0, s->quarter_sample);
2331 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2332 v->bfraction, 1, s->quarter_sample);
2333 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2334 v->bfraction, 1, s->quarter_sample);
2336 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2337 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2338 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2339 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2340 f = (total_opp > 2) ? 1 : 0;
2342 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2343 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2346 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2347 for (k = 0; k < 4; k++) {
2348 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2349 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2350 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2351 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2352 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2353 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2357 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2358 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);
2359 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);
2362 if (dir) { // backward
2363 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);
2364 if (n == 3 || mv1) {
2365 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2368 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);
2369 if (n == 3 || mv1) {
2370 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2375 /** Get predicted DC value for I-frames only
2376 * prediction dir: left=0, top=1
2377 * @param s MpegEncContext
2378 * @param overlap flag indicating that overlap filtering is used
2379 * @param pq integer part of picture quantizer
2380 * @param[in] n block index in the current MB
2381 * @param dc_val_ptr Pointer to DC predictor
2382 * @param dir_ptr Prediction direction for use in AC prediction
2384 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2385 int16_t **dc_val_ptr, int *dir_ptr)
2387 int a, b, c, wrap, pred, scale;
2389 static const uint16_t dcpred[32] = {
2390 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2391 114, 102, 93, 85, 79, 73, 68, 64,
2392 60, 57, 54, 51, 49, 47, 45, 43,
2393 41, 39, 38, 37, 35, 34, 33
2396 /* find prediction - wmv3_dc_scale always used here in fact */
2397 if (n < 4) scale = s->y_dc_scale;
2398 else scale = s->c_dc_scale;
2400 wrap = s->block_wrap[n];
2401 dc_val = s->dc_val[0] + s->block_index[n];
2407 b = dc_val[ - 1 - wrap];
2408 a = dc_val[ - wrap];
2410 if (pq < 9 || !overlap) {
2411 /* Set outer values */
2412 if (s->first_slice_line && (n != 2 && n != 3))
2413 b = a = dcpred[scale];
2414 if (s->mb_x == 0 && (n != 1 && n != 3))
2415 b = c = dcpred[scale];
2417 /* Set outer values */
2418 if (s->first_slice_line && (n != 2 && n != 3))
2420 if (s->mb_x == 0 && (n != 1 && n != 3))
2424 if (abs(a - b) <= abs(b - c)) {
2426 *dir_ptr = 1; // left
2429 *dir_ptr = 0; // top
2432 /* update predictor */
2433 *dc_val_ptr = &dc_val[0];
2438 /** Get predicted DC value
2439 * prediction dir: left=0, top=1
2440 * @param s MpegEncContext
2441 * @param overlap flag indicating that overlap filtering is used
2442 * @param pq integer part of picture quantizer
2443 * @param[in] n block index in the current MB
2444 * @param a_avail flag indicating top block availability
2445 * @param c_avail flag indicating left block availability
2446 * @param dc_val_ptr Pointer to DC predictor
2447 * @param dir_ptr Prediction direction for use in AC prediction
2449 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2450 int a_avail, int c_avail,
2451 int16_t **dc_val_ptr, int *dir_ptr)
2453 int a, b, c, wrap, pred;
2455 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2459 wrap = s->block_wrap[n];
2460 dc_val = s->dc_val[0] + s->block_index[n];
2466 b = dc_val[ - 1 - wrap];
2467 a = dc_val[ - wrap];
2468 /* scale predictors if needed */
2469 q1 = s->current_picture.qscale_table[mb_pos];
2470 dqscale_index = s->y_dc_scale_table[q1] - 1;
2471 if (dqscale_index < 0)
2473 if (c_avail && (n != 1 && n != 3)) {
2474 q2 = s->current_picture.qscale_table[mb_pos - 1];
2476 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2478 if (a_avail && (n != 2 && n != 3)) {
2479 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2481 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2483 if (a_avail && c_avail && (n != 3)) {
2488 off -= s->mb_stride;
2489 q2 = s->current_picture.qscale_table[off];
2491 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2494 if (a_avail && c_avail) {
2495 if (abs(a - b) <= abs(b - c)) {
2497 *dir_ptr = 1; // left
2500 *dir_ptr = 0; // top
2502 } else if (a_avail) {
2504 *dir_ptr = 0; // top
2505 } else if (c_avail) {
2507 *dir_ptr = 1; // left
2510 *dir_ptr = 1; // left
2513 /* update predictor */
2514 *dc_val_ptr = &dc_val[0];
2518 /** @} */ // Block group
2521 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2522 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2526 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2527 uint8_t **coded_block_ptr)
2529 int xy, wrap, pred, a, b, c;
2531 xy = s->block_index[n];
2532 wrap = s->b8_stride;
2537 a = s->coded_block[xy - 1 ];
2538 b = s->coded_block[xy - 1 - wrap];
2539 c = s->coded_block[xy - wrap];
2548 *coded_block_ptr = &s->coded_block[xy];
2554 * Decode one AC coefficient
2555 * @param v The VC1 context
2556 * @param last Last coefficient
2557 * @param skip How much zero coefficients to skip
2558 * @param value Decoded AC coefficient value
2559 * @param codingset set of VLC to decode data
2562 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2563 int *value, int codingset)
2565 GetBitContext *gb = &v->s.gb;
2566 int index, escape, run = 0, level = 0, lst = 0;
2568 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2569 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2570 run = vc1_index_decode_table[codingset][index][0];
2571 level = vc1_index_decode_table[codingset][index][1];
2572 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2576 escape = decode210(gb);
2578 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2579 run = vc1_index_decode_table[codingset][index][0];
2580 level = vc1_index_decode_table[codingset][index][1];
2581 lst = index >= vc1_last_decode_table[codingset];
2584 level += vc1_last_delta_level_table[codingset][run];
2586 level += vc1_delta_level_table[codingset][run];
2589 run += vc1_last_delta_run_table[codingset][level] + 1;
2591 run += vc1_delta_run_table[codingset][level] + 1;
2597 lst = get_bits1(gb);
2598 if (v->s.esc3_level_length == 0) {
2599 if (v->pq < 8 || v->dquantfrm) { // table 59
2600 v->s.esc3_level_length = get_bits(gb, 3);
2601 if (!v->s.esc3_level_length)
2602 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2603 } else { // table 60
2604 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2606 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2608 run = get_bits(gb, v->s.esc3_run_length);
2609 sign = get_bits1(gb);
2610 level = get_bits(gb, v->s.esc3_level_length);
2621 /** Decode intra block in intra frames - should be faster than decode_intra_block
2622 * @param v VC1Context
2623 * @param block block to decode
2624 * @param[in] n subblock index
2625 * @param coded are AC coeffs present or not
2626 * @param codingset set of VLC to decode data
2628 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2629 int coded, int codingset)
2631 GetBitContext *gb = &v->s.gb;
2632 MpegEncContext *s = &v->s;
2633 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2636 int16_t *ac_val, *ac_val2;
2639 /* Get DC differential */
2641 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2643 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2646 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2650 if (dcdiff == 119 /* ESC index value */) {
2651 /* TODO: Optimize */
2652 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2653 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2654 else dcdiff = get_bits(gb, 8);
2657 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2658 else if (v->pq == 2)
2659 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2666 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2669 /* Store the quantized DC coeff, used for prediction */
2671 block[0] = dcdiff * s->y_dc_scale;
2673 block[0] = dcdiff * s->c_dc_scale;
2684 int last = 0, skip, value;
2685 const uint8_t *zz_table;
2689 scale = v->pq * 2 + v->halfpq;
2693 zz_table = v->zz_8x8[2];
2695 zz_table = v->zz_8x8[3];
2697 zz_table = v->zz_8x8[1];
2699 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2701 if (dc_pred_dir) // left
2704 ac_val -= 16 * s->block_wrap[n];
2707 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2711 block[zz_table[i++]] = value;
2714 /* apply AC prediction if needed */
2716 if (dc_pred_dir) { // left
2717 for (k = 1; k < 8; k++)
2718 block[k << v->left_blk_sh] += ac_val[k];
2720 for (k = 1; k < 8; k++)
2721 block[k << v->top_blk_sh] += ac_val[k + 8];
2724 /* save AC coeffs for further prediction */
2725 for (k = 1; k < 8; k++) {
2726 ac_val2[k] = block[k << v->left_blk_sh];
2727 ac_val2[k + 8] = block[k << v->top_blk_sh];
2730 /* scale AC coeffs */
2731 for (k = 1; k < 64; k++)
2735 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2738 if (s->ac_pred) i = 63;
2744 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2748 scale = v->pq * 2 + v->halfpq;
2749 memset(ac_val2, 0, 16 * 2);
2750 if (dc_pred_dir) { // left
2753 memcpy(ac_val2, ac_val, 8 * 2);
2755 ac_val -= 16 * s->block_wrap[n];
2757 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2760 /* apply AC prediction if needed */
2762 if (dc_pred_dir) { //left
2763 for (k = 1; k < 8; k++) {
2764 block[k << v->left_blk_sh] = ac_val[k] * scale;
2765 if (!v->pquantizer && block[k << v->left_blk_sh])
2766 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2769 for (k = 1; k < 8; k++) {
2770 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2771 if (!v->pquantizer && block[k << v->top_blk_sh])
2772 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2778 s->block_last_index[n] = i;
2783 /** Decode intra block in intra frames - should be faster than decode_intra_block
2784 * @param v VC1Context
2785 * @param block block to decode
2786 * @param[in] n subblock number
2787 * @param coded are AC coeffs present or not
2788 * @param codingset set of VLC to decode data
2789 * @param mquant quantizer value for this macroblock
2791 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2792 int coded, int codingset, int mquant)
2794 GetBitContext *gb = &v->s.gb;
2795 MpegEncContext *s = &v->s;
2796 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2798 int16_t *dc_val = NULL;
2799 int16_t *ac_val, *ac_val2;
2801 int a_avail = v->a_avail, c_avail = v->c_avail;
2802 int use_pred = s->ac_pred;
2805 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2807 /* Get DC differential */
2809 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2811 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2814 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2818 if (dcdiff == 119 /* ESC index value */) {
2819 /* TODO: Optimize */
2820 if (mquant == 1) dcdiff = get_bits(gb, 10);
2821 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2822 else dcdiff = get_bits(gb, 8);
2825 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2826 else if (mquant == 2)
2827 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2834 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2837 /* Store the quantized DC coeff, used for prediction */
2839 block[0] = dcdiff * s->y_dc_scale;
2841 block[0] = dcdiff * s->c_dc_scale;
2847 /* check if AC is needed at all */
2848 if (!a_avail && !c_avail)
2850 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2853 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2855 if (dc_pred_dir) // left
2858 ac_val -= 16 * s->block_wrap[n];
2860 q1 = s->current_picture.qscale_table[mb_pos];
2861 if ( dc_pred_dir && c_avail && mb_pos)
2862 q2 = s->current_picture.qscale_table[mb_pos - 1];
2863 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2864 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2865 if ( dc_pred_dir && n == 1)
2867 if (!dc_pred_dir && n == 2)
2873 int last = 0, skip, value;
2874 const uint8_t *zz_table;
2878 if (!use_pred && v->fcm == ILACE_FRAME) {
2879 zz_table = v->zzi_8x8;
2881 if (!dc_pred_dir) // top
2882 zz_table = v->zz_8x8[2];
2884 zz_table = v->zz_8x8[3];
2887 if (v->fcm != ILACE_FRAME)
2888 zz_table = v->zz_8x8[1];
2890 zz_table = v->zzi_8x8;
2894 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2898 block[zz_table[i++]] = value;
2901 /* apply AC prediction if needed */
2903 /* scale predictors if needed*/
2904 if (q2 && q1 != q2) {
2905 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2906 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2909 return AVERROR_INVALIDDATA;
2910 if (dc_pred_dir) { // left
2911 for (k = 1; k < 8; k++)
2912 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2914 for (k = 1; k < 8; k++)
2915 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2918 if (dc_pred_dir) { //left
2919 for (k = 1; k < 8; k++)
2920 block[k << v->left_blk_sh] += ac_val[k];
2922 for (k = 1; k < 8; k++)
2923 block[k << v->top_blk_sh] += ac_val[k + 8];
2927 /* save AC coeffs for further prediction */
2928 for (k = 1; k < 8; k++) {
2929 ac_val2[k ] = block[k << v->left_blk_sh];
2930 ac_val2[k + 8] = block[k << v->top_blk_sh];
2933 /* scale AC coeffs */
2934 for (k = 1; k < 64; k++)
2938 block[k] += (block[k] < 0) ? -mquant : mquant;
2941 if (use_pred) i = 63;
2942 } else { // no AC coeffs
2945 memset(ac_val2, 0, 16 * 2);
2946 if (dc_pred_dir) { // left
2948 memcpy(ac_val2, ac_val, 8 * 2);
2949 if (q2 && q1 != q2) {
2950 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2951 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2953 return AVERROR_INVALIDDATA;
2954 for (k = 1; k < 8; k++)
2955 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2960 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2961 if (q2 && q1 != q2) {
2962 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2963 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2965 return AVERROR_INVALIDDATA;
2966 for (k = 1; k < 8; k++)
2967 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2972 /* apply AC prediction if needed */
2974 if (dc_pred_dir) { // left
2975 for (k = 1; k < 8; k++) {
2976 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2977 if (!v->pquantizer && block[k << v->left_blk_sh])
2978 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2981 for (k = 1; k < 8; k++) {
2982 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2983 if (!v->pquantizer && block[k << v->top_blk_sh])
2984 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2990 s->block_last_index[n] = i;
2995 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2996 * @param v VC1Context
2997 * @param block block to decode
2998 * @param[in] n subblock index
2999 * @param coded are AC coeffs present or not
3000 * @param mquant block quantizer
3001 * @param codingset set of VLC to decode data
3003 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
3004 int coded, int mquant, int codingset)
3006 GetBitContext *gb = &v->s.gb;
3007 MpegEncContext *s = &v->s;
3008 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3010 int16_t *dc_val = NULL;
3011 int16_t *ac_val, *ac_val2;
3013 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3014 int a_avail = v->a_avail, c_avail = v->c_avail;
3015 int use_pred = s->ac_pred;
3019 s->bdsp.clear_block(block);
3021 /* XXX: Guard against dumb values of mquant */
3022 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3024 /* Set DC scale - y and c use the same */
3025 s->y_dc_scale = s->y_dc_scale_table[mquant];
3026 s->c_dc_scale = s->c_dc_scale_table[mquant];
3028 /* Get DC differential */
3030 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3032 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3035 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3039 if (dcdiff == 119 /* ESC index value */) {
3040 /* TODO: Optimize */
3041 if (mquant == 1) dcdiff = get_bits(gb, 10);
3042 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3043 else dcdiff = get_bits(gb, 8);
3046 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3047 else if (mquant == 2)
3048 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3055 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3058 /* Store the quantized DC coeff, used for prediction */
3061 block[0] = dcdiff * s->y_dc_scale;
3063 block[0] = dcdiff * s->c_dc_scale;
3069 /* check if AC is needed at all and adjust direction if needed */
3070 if (!a_avail) dc_pred_dir = 1;
3071 if (!c_avail) dc_pred_dir = 0;
3072 if (!a_avail && !c_avail) use_pred = 0;
3073 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3076 scale = mquant * 2 + v->halfpq;
3078 if (dc_pred_dir) //left
3081 ac_val -= 16 * s->block_wrap[n];
3083 q1 = s->current_picture.qscale_table[mb_pos];
3084 if (dc_pred_dir && c_avail && mb_pos)
3085 q2 = s->current_picture.qscale_table[mb_pos - 1];
3086 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3087 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3088 if ( dc_pred_dir && n == 1)
3090 if (!dc_pred_dir && n == 2)
3092 if (n == 3) q2 = q1;
3095 int last = 0, skip, value;
3099 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3103 if (v->fcm == PROGRESSIVE)
3104 block[v->zz_8x8[0][i++]] = value;
3106 if (use_pred && (v->fcm == ILACE_FRAME)) {
3107 if (!dc_pred_dir) // top
3108 block[v->zz_8x8[2][i++]] = value;
3110 block[v->zz_8x8[3][i++]] = value;
3112 block[v->zzi_8x8[i++]] = value;
3117 /* apply AC prediction if needed */
3119 /* scale predictors if needed*/
3120 if (q2 && q1 != q2) {
3121 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3122 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3125 return AVERROR_INVALIDDATA;
3126 if (dc_pred_dir) { // left
3127 for (k = 1; k < 8; k++)
3128 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3130 for (k = 1; k < 8; k++)
3131 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3134 if (dc_pred_dir) { // left
3135 for (k = 1; k < 8; k++)
3136 block[k << v->left_blk_sh] += ac_val[k];
3138 for (k = 1; k < 8; k++)
3139 block[k << v->top_blk_sh] += ac_val[k + 8];
3143 /* save AC coeffs for further prediction */
3144 for (k = 1; k < 8; k++) {
3145 ac_val2[k ] = block[k << v->left_blk_sh];
3146 ac_val2[k + 8] = block[k << v->top_blk_sh];
3149 /* scale AC coeffs */
3150 for (k = 1; k < 64; k++)
3154 block[k] += (block[k] < 0) ? -mquant : mquant;
3157 if (use_pred) i = 63;
3158 } else { // no AC coeffs
3161 memset(ac_val2, 0, 16 * 2);
3162 if (dc_pred_dir) { // left
3164 memcpy(ac_val2, ac_val, 8 * 2);
3165 if (q2 && q1 != q2) {
3166 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3167 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3169 return AVERROR_INVALIDDATA;
3170 for (k = 1; k < 8; k++)
3171 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3176 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3177 if (q2 && q1 != q2) {
3178 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3179 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3181 return AVERROR_INVALIDDATA;
3182 for (k = 1; k < 8; k++)
3183 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3188 /* apply AC prediction if needed */
3190 if (dc_pred_dir) { // left
3191 for (k = 1; k < 8; k++) {
3192 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3193 if (!v->pquantizer && block[k << v->left_blk_sh])
3194 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3197 for (k = 1; k < 8; k++) {
3198 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3199 if (!v->pquantizer && block[k << v->top_blk_sh])
3200 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3206 s->block_last_index[n] = i;
3213 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3214 int mquant, int ttmb, int first_block,
3215 uint8_t *dst, int linesize, int skip_block,
3218 MpegEncContext *s = &v->s;
3219 GetBitContext *gb = &s->gb;
3222 int scale, off, idx, last, skip, value;
3223 int ttblk = ttmb & 7;
3226 s->bdsp.clear_block(block);
3229 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)];
3231 if (ttblk == TT_4X4) {
3232 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3234 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3235 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3236 || (!v->res_rtm_flag && !first_block))) {
3237 subblkpat = decode012(gb);
3239 subblkpat ^= 3; // swap decoded pattern bits
3240 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3242 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3245 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3247 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3248 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3249 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3252 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3253 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3262 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3267 idx = v->zz_8x8[0][i++];
3269 idx = v->zzi_8x8[i++];
3270 block[idx] = value * scale;
3272 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3276 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3278 v->vc1dsp.vc1_inv_trans_8x8(block);
3279 s->dsp.add_pixels_clamped(block, dst, linesize);
3284 pat = ~subblkpat & 0xF;
3285 for (j = 0; j < 4; j++) {
3286 last = subblkpat & (1 << (3 - j));
3288 off = (j & 1) * 4 + (j & 2) * 16;
3290 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3295 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3297 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3298 block[idx + off] = value * scale;
3300 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3302 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3304 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3306 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3311 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3312 for (j = 0; j < 2; j++) {
3313 last = subblkpat & (1 << (1 - j));
3317 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3322 idx = v->zz_8x4[i++] + off;
3324 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3325 block[idx] = value * scale;
3327 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3329 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3331 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3333 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3338 pat = ~(subblkpat * 5) & 0xF;
3339 for (j = 0; j < 2; j++) {
3340 last = subblkpat & (1 << (1 - j));
3344 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3349 idx = v->zz_4x8[i++] + off;
3351 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3352 block[idx] = value * scale;
3354 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3356 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3358 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3360 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3366 *ttmb_out |= ttblk << (n * 4);
3370 /** @} */ // Macroblock group
3372 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3373 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3375 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3377 MpegEncContext *s = &v->s;
3378 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3379 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3380 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3381 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3382 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3385 if (block_num > 3) {
3386 dst = s->dest[block_num - 3];
3388 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3390 if (s->mb_y != s->end_mb_y || block_num < 2) {
3394 if (block_num > 3) {
3395 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3396 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3397 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3398 mv_stride = s->mb_stride;
3400 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3401 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3402 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3403 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3404 mv_stride = s->b8_stride;
3405 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3408 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3409 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3410 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3412 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3414 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3417 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3419 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3424 dst -= 4 * linesize;
3425 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3426 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3427 idx = (block_cbp | (block_cbp >> 2)) & 3;
3429 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3432 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3434 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3439 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3441 MpegEncContext *s = &v->s;
3442 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3443 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3444 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3445 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3446 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3449 if (block_num > 3) {
3450 dst = s->dest[block_num - 3] - 8 * linesize;
3452 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3455 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3458 if (block_num > 3) {
3459 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3460 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3461 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3463 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3464 : (mb_cbp >> ((block_num + 1) * 4));
3465 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3466 : (mb_is_intra >> ((block_num + 1) * 4));
3467 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3469 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3470 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3472 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3474 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3477 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3479 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3485 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3486 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3487 idx = (block_cbp | (block_cbp >> 1)) & 5;
3489 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3492 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3494 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3499 static void vc1_apply_p_loop_filter(VC1Context *v)
3501 MpegEncContext *s = &v->s;
3504 for (i = 0; i < 6; i++) {
3505 vc1_apply_p_v_loop_filter(v, i);
3508 /* V always precedes H, therefore we run H one MB before V;
3509 * at the end of a row, we catch up to complete the row */
3511 for (i = 0; i < 6; i++) {
3512 vc1_apply_p_h_loop_filter(v, i);
3514 if (s->mb_x == s->mb_width - 1) {
3516 ff_update_block_index(s);
3517 for (i = 0; i < 6; i++) {
3518 vc1_apply_p_h_loop_filter(v, i);
3524 /** Decode one P-frame MB
3526 static int vc1_decode_p_mb(VC1Context *v)
3528 MpegEncContext *s = &v->s;
3529 GetBitContext *gb = &s->gb;
3531 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3532 int cbp; /* cbp decoding stuff */
3533 int mqdiff, mquant; /* MB quantization */
3534 int ttmb = v->ttfrm; /* MB Transform type */
3536 int mb_has_coeffs = 1; /* last_flag */
3537 int dmv_x, dmv_y; /* Differential MV components */
3538 int index, index1; /* LUT indexes */
3539 int val, sign; /* temp values */
3540 int first_block = 1;
3542 int skipped, fourmv;
3543 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3545 mquant = v->pq; /* lossy initialization */
3547 if (v->mv_type_is_raw)
3548 fourmv = get_bits1(gb);
3550 fourmv = v->mv_type_mb_plane[mb_pos];
3552 skipped = get_bits1(gb);
3554 skipped = v->s.mbskip_table[mb_pos];
3556 if (!fourmv) { /* 1MV mode */
3558 GET_MVDATA(dmv_x, dmv_y);
3561 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3562 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3564 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3565 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3567 /* FIXME Set DC val for inter block ? */
3568 if (s->mb_intra && !mb_has_coeffs) {
3570 s->ac_pred = get_bits1(gb);
3572 } else if (mb_has_coeffs) {
3574 s->ac_pred = get_bits1(gb);
3575 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3581 s->current_picture.qscale_table[mb_pos] = mquant;
3583 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3584 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3585 VC1_TTMB_VLC_BITS, 2);
3586 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3588 for (i = 0; i < 6; i++) {
3589 s->dc_val[0][s->block_index[i]] = 0;
3591 val = ((cbp >> (5 - i)) & 1);
3592 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3593 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3595 /* check if prediction blocks A and C are available */
3596 v->a_avail = v->c_avail = 0;
3597 if (i == 2 || i == 3 || !s->first_slice_line)
3598 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3599 if (i == 1 || i == 3 || s->mb_x)
3600 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3602 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3603 (i & 4) ? v->codingset2 : v->codingset);
3604 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3606 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3608 for (j = 0; j < 64; j++)
3609 s->block[i][j] <<= 1;
3610 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3611 if (v->pq >= 9 && v->overlap) {
3613 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3615 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3617 block_cbp |= 0xF << (i << 2);
3618 block_intra |= 1 << i;
3620 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3621 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3622 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3623 block_cbp |= pat << (i << 2);
3624 if (!v->ttmbf && ttmb < 8)
3631 for (i = 0; i < 6; i++) {
3632 v->mb_type[0][s->block_index[i]] = 0;
3633 s->dc_val[0][s->block_index[i]] = 0;
3635 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3636 s->current_picture.qscale_table[mb_pos] = 0;
3637 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3640 } else { // 4MV mode
3641 if (!skipped /* unskipped MB */) {
3642 int intra_count = 0, coded_inter = 0;
3643 int is_intra[6], is_coded[6];
3645 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3646 for (i = 0; i < 6; i++) {
3647 val = ((cbp >> (5 - i)) & 1);
3648 s->dc_val[0][s->block_index[i]] = 0;
3655 GET_MVDATA(dmv_x, dmv_y);
3657 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3659 vc1_mc_4mv_luma(v, i, 0, 0);
3660 intra_count += s->mb_intra;
3661 is_intra[i] = s->mb_intra;
3662 is_coded[i] = mb_has_coeffs;
3665 is_intra[i] = (intra_count >= 3);
3669 vc1_mc_4mv_chroma(v, 0);
3670 v->mb_type[0][s->block_index[i]] = is_intra[i];
3672 coded_inter = !is_intra[i] & is_coded[i];
3674 // if there are no coded blocks then don't do anything more
3676 if (!intra_count && !coded_inter)
3679 s->current_picture.qscale_table[mb_pos] = mquant;
3680 /* test if block is intra and has pred */
3683 for (i = 0; i < 6; i++)
3685 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3686 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3692 s->ac_pred = get_bits1(gb);
3696 if (!v->ttmbf && coded_inter)
3697 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3698 for (i = 0; i < 6; i++) {
3700 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3701 s->mb_intra = is_intra[i];
3703 /* check if prediction blocks A and C are available */
3704 v->a_avail = v->c_avail = 0;
3705 if (i == 2 || i == 3 || !s->first_slice_line)
3706 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3707 if (i == 1 || i == 3 || s->mb_x)
3708 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3710 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3711 (i & 4) ? v->codingset2 : v->codingset);
3712 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3714 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3716 for (j = 0; j < 64; j++)
3717 s->block[i][j] <<= 1;
3718 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3719 (i & 4) ? s->uvlinesize : s->linesize);
3720 if (v->pq >= 9 && v->overlap) {
3722 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3724 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3726 block_cbp |= 0xF << (i << 2);
3727 block_intra |= 1 << i;
3728 } else if (is_coded[i]) {
3729 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3730 first_block, s->dest[dst_idx] + off,
3731 (i & 4) ? s->uvlinesize : s->linesize,
3732 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3734 block_cbp |= pat << (i << 2);
3735 if (!v->ttmbf && ttmb < 8)
3740 } else { // skipped MB
3742 s->current_picture.qscale_table[mb_pos] = 0;
3743 for (i = 0; i < 6; i++) {
3744 v->mb_type[0][s->block_index[i]] = 0;
3745 s->dc_val[0][s->block_index[i]] = 0;
3747 for (i = 0; i < 4; i++) {
3748 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3749 vc1_mc_4mv_luma(v, i, 0, 0);
3751 vc1_mc_4mv_chroma(v, 0);
3752 s->current_picture.qscale_table[mb_pos] = 0;
3756 v->cbp[s->mb_x] = block_cbp;
3757 v->ttblk[s->mb_x] = block_tt;
3758 v->is_intra[s->mb_x] = block_intra;
3763 /* Decode one macroblock in an interlaced frame p picture */
3765 static int vc1_decode_p_mb_intfr(VC1Context *v)
3767 MpegEncContext *s = &v->s;
3768 GetBitContext *gb = &s->gb;
3770 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3771 int cbp = 0; /* cbp decoding stuff */
3772 int mqdiff, mquant; /* MB quantization */
3773 int ttmb = v->ttfrm; /* MB Transform type */
3775 int mb_has_coeffs = 1; /* last_flag */
3776 int dmv_x, dmv_y; /* Differential MV components */
3777 int val; /* temp value */
3778 int first_block = 1;
3780 int skipped, fourmv = 0, twomv = 0;
3781 int block_cbp = 0, pat, block_tt = 0;
3782 int idx_mbmode = 0, mvbp;
3783 int stride_y, fieldtx;
3785 mquant = v->pq; /* Lossy initialization */
3788 skipped = get_bits1(gb);
3790 skipped = v->s.mbskip_table[mb_pos];
3792 if (v->fourmvswitch)
3793 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3795 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3796 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3797 /* store the motion vector type in a flag (useful later) */
3798 case MV_PMODE_INTFR_4MV:
3800 v->blk_mv_type[s->block_index[0]] = 0;
3801 v->blk_mv_type[s->block_index[1]] = 0;
3802 v->blk_mv_type[s->block_index[2]] = 0;
3803 v->blk_mv_type[s->block_index[3]] = 0;
3805 case MV_PMODE_INTFR_4MV_FIELD:
3807 v->blk_mv_type[s->block_index[0]] = 1;
3808 v->blk_mv_type[s->block_index[1]] = 1;
3809 v->blk_mv_type[s->block_index[2]] = 1;
3810 v->blk_mv_type[s->block_index[3]] = 1;
3812 case MV_PMODE_INTFR_2MV_FIELD:
3814 v->blk_mv_type[s->block_index[0]] = 1;
3815 v->blk_mv_type[s->block_index[1]] = 1;
3816 v->blk_mv_type[s->block_index[2]] = 1;
3817 v->blk_mv_type[s->block_index[3]] = 1;
3819 case MV_PMODE_INTFR_1MV:
3820 v->blk_mv_type[s->block_index[0]] = 0;
3821 v->blk_mv_type[s->block_index[1]] = 0;
3822 v->blk_mv_type[s->block_index[2]] = 0;
3823 v->blk_mv_type[s->block_index[3]] = 0;
3826 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3827 for (i = 0; i < 4; i++) {
3828 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3829 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3831 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3832 s->mb_intra = v->is_intra[s->mb_x] = 1;
3833 for (i = 0; i < 6; i++)
3834 v->mb_type[0][s->block_index[i]] = 1;
3835 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3836 mb_has_coeffs = get_bits1(gb);
3838 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3839 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3841 s->current_picture.qscale_table[mb_pos] = mquant;
3842 /* Set DC scale - y and c use the same (not sure if necessary here) */
3843 s->y_dc_scale = s->y_dc_scale_table[mquant];
3844 s->c_dc_scale = s->c_dc_scale_table[mquant];
3846 for (i = 0; i < 6; i++) {
3847 s->dc_val[0][s->block_index[i]] = 0;
3849 val = ((cbp >> (5 - i)) & 1);
3850 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3851 v->a_avail = v->c_avail = 0;
3852 if (i == 2 || i == 3 || !s->first_slice_line)
3853 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3854 if (i == 1 || i == 3 || s->mb_x)
3855 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3857 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3858 (i & 4) ? v->codingset2 : v->codingset);
3859 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3860 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3862 stride_y = s->linesize << fieldtx;
3863 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3865 stride_y = s->uvlinesize;
3868 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3872 } else { // inter MB
3873 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3875 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3876 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3877 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3879 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3880 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3881 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3884 s->mb_intra = v->is_intra[s->mb_x] = 0;
3885 for (i = 0; i < 6; i++)
3886 v->mb_type[0][s->block_index[i]] = 0;
3887 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3888 /* for all motion vector read MVDATA and motion compensate each block */
3892 for (i = 0; i < 6; i++) {
3895 val = ((mvbp >> (3 - i)) & 1);
3897 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3899 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3900 vc1_mc_4mv_luma(v, i, 0, 0);
3901 } else if (i == 4) {
3902 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3909 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3911 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3912 vc1_mc_4mv_luma(v, 0, 0, 0);
3913 vc1_mc_4mv_luma(v, 1, 0, 0);
3916 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3918 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3919 vc1_mc_4mv_luma(v, 2, 0, 0);
3920 vc1_mc_4mv_luma(v, 3, 0, 0);
3921 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3923 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3926 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3928 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3932 GET_MQUANT(); // p. 227
3933 s->current_picture.qscale_table[mb_pos] = mquant;
3934 if (!v->ttmbf && cbp)
3935 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3936 for (i = 0; i < 6; i++) {
3937 s->dc_val[0][s->block_index[i]] = 0;
3939 val = ((cbp >> (5 - i)) & 1);
3941 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3943 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3945 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3946 first_block, s->dest[dst_idx] + off,
3947 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3948 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3949 block_cbp |= pat << (i << 2);
3950 if (!v->ttmbf && ttmb < 8)
3957 s->mb_intra = v->is_intra[s->mb_x] = 0;
3958 for (i = 0; i < 6; i++) {
3959 v->mb_type[0][s->block_index[i]] = 0;
3960 s->dc_val[0][s->block_index[i]] = 0;
3962 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3963 s->current_picture.qscale_table[mb_pos] = 0;
3964 v->blk_mv_type[s->block_index[0]] = 0;
3965 v->blk_mv_type[s->block_index[1]] = 0;
3966 v->blk_mv_type[s->block_index[2]] = 0;
3967 v->blk_mv_type[s->block_index[3]] = 0;
3968 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3971 if (s->mb_x == s->mb_width - 1)
3972 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3976 static int vc1_decode_p_mb_intfi(VC1Context *v)
3978 MpegEncContext *s = &v->s;
3979 GetBitContext *gb = &s->gb;
3981 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3982 int cbp = 0; /* cbp decoding stuff */
3983 int mqdiff, mquant; /* MB quantization */
3984 int ttmb = v->ttfrm; /* MB Transform type */
3986 int mb_has_coeffs = 1; /* last_flag */
3987 int dmv_x, dmv_y; /* Differential MV components */
3988 int val; /* temp values */
3989 int first_block = 1;
3992 int block_cbp = 0, pat, block_tt = 0;
3995 mquant = v->pq; /* Lossy initialization */
3997 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3998 if (idx_mbmode <= 1) { // intra MB
3999 s->mb_intra = v->is_intra[s->mb_x] = 1;
4000 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4001 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4002 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4004 s->current_picture.qscale_table[mb_pos] = mquant;
4005 /* Set DC scale - y and c use the same (not sure if necessary here) */
4006 s->y_dc_scale = s->y_dc_scale_table[mquant];
4007 s->c_dc_scale = s->c_dc_scale_table[mquant];
4008 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4009 mb_has_coeffs = idx_mbmode & 1;
4011 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4013 for (i = 0; i < 6; i++) {
4014 s->dc_val[0][s->block_index[i]] = 0;
4015 v->mb_type[0][s->block_index[i]] = 1;
4017 val = ((cbp >> (5 - i)) & 1);
4018 v->a_avail = v->c_avail = 0;
4019 if (i == 2 || i == 3 || !s->first_slice_line)
4020 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4021 if (i == 1 || i == 3 || s->mb_x)
4022 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4024 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4025 (i & 4) ? v->codingset2 : v->codingset);
4026 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4028 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4029 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4030 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4031 // TODO: loop filter
4034 s->mb_intra = v->is_intra[s->mb_x] = 0;
4035 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4036 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4037 if (idx_mbmode <= 5) { // 1-MV
4038 dmv_x = dmv_y = pred_flag = 0;
4039 if (idx_mbmode & 1) {
4040 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4042 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4044 mb_has_coeffs = !(idx_mbmode & 2);
4046 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4047 for (i = 0; i < 6; i++) {
4049 dmv_x = dmv_y = pred_flag = 0;
4050 val = ((v->fourmvbp >> (3 - i)) & 1);
4052 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4054 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4055 vc1_mc_4mv_luma(v, i, 0, 0);
4057 vc1_mc_4mv_chroma(v, 0);
4059 mb_has_coeffs = idx_mbmode & 1;
4062 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4066 s->current_picture.qscale_table[mb_pos] = mquant;
4067 if (!v->ttmbf && cbp) {
4068 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4071 for (i = 0; i < 6; i++) {
4072 s->dc_val[0][s->block_index[i]] = 0;
4074 val = ((cbp >> (5 - i)) & 1);
4075 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4077 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4078 first_block, s->dest[dst_idx] + off,
4079 (i & 4) ? s->uvlinesize : s->linesize,
4080 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4082 block_cbp |= pat << (i << 2);
4083 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4088 if (s->mb_x == s->mb_width - 1)
4089 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4093 /** Decode one B-frame MB (in Main profile)
4095 static void vc1_decode_b_mb(VC1Context *v)
4097 MpegEncContext *s = &v->s;
4098 GetBitContext *gb = &s->gb;
4100 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4101 int cbp = 0; /* cbp decoding stuff */
4102 int mqdiff, mquant; /* MB quantization */
4103 int ttmb = v->ttfrm; /* MB Transform type */
4104 int mb_has_coeffs = 0; /* last_flag */
4105 int index, index1; /* LUT indexes */
4106 int val, sign; /* temp values */
4107 int first_block = 1;
4109 int skipped, direct;
4110 int dmv_x[2], dmv_y[2];
4111 int bmvtype = BMV_TYPE_BACKWARD;
4113 mquant = v->pq; /* lossy initialization */
4117 direct = get_bits1(gb);
4119 direct = v->direct_mb_plane[mb_pos];
4121 skipped = get_bits1(gb);
4123 skipped = v->s.mbskip_table[mb_pos];
4125 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4126 for (i = 0; i < 6; i++) {
4127 v->mb_type[0][s->block_index[i]] = 0;
4128 s->dc_val[0][s->block_index[i]] = 0;
4130 s->current_picture.qscale_table[mb_pos] = 0;
4134 GET_MVDATA(dmv_x[0], dmv_y[0]);
4135 dmv_x[1] = dmv_x[0];
4136 dmv_y[1] = dmv_y[0];
4138 if (skipped || !s->mb_intra) {
4139 bmvtype = decode012(gb);
4142 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4145 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4148 bmvtype = BMV_TYPE_INTERPOLATED;
4149 dmv_x[0] = dmv_y[0] = 0;
4153 for (i = 0; i < 6; i++)
4154 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4158 bmvtype = BMV_TYPE_INTERPOLATED;
4159 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4160 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4164 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4167 s->current_picture.qscale_table[mb_pos] = mquant;
4169 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4170 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4171 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4172 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4174 if (!mb_has_coeffs && !s->mb_intra) {
4175 /* no coded blocks - effectively skipped */
4176 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4177 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4180 if (s->mb_intra && !mb_has_coeffs) {
4182 s->current_picture.qscale_table[mb_pos] = mquant;
4183 s->ac_pred = get_bits1(gb);
4185 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4187 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4188 GET_MVDATA(dmv_x[0], dmv_y[0]);
4189 if (!mb_has_coeffs) {
4190 /* interpolated skipped block */
4191 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4192 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4196 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4198 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4201 s->ac_pred = get_bits1(gb);
4202 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4204 s->current_picture.qscale_table[mb_pos] = mquant;
4205 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4206 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4210 for (i = 0; i < 6; i++) {
4211 s->dc_val[0][s->block_index[i]] = 0;
4213 val = ((cbp >> (5 - i)) & 1);
4214 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4215 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4217 /* check if prediction blocks A and C are available */
4218 v->a_avail = v->c_avail = 0;
4219 if (i == 2 || i == 3 || !s->first_slice_line)
4220 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4221 if (i == 1 || i == 3 || s->mb_x)
4222 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4224 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4225 (i & 4) ? v->codingset2 : v->codingset);
4226 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4228 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4230 for (j = 0; j < 64; j++)
4231 s->block[i][j] <<= 1;
4232 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4234 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4235 first_block, s->dest[dst_idx] + off,
4236 (i & 4) ? s->uvlinesize : s->linesize,
4237 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4238 if (!v->ttmbf && ttmb < 8)
4245 /** Decode one B-frame MB (in interlaced field B picture)
4247 static void vc1_decode_b_mb_intfi(VC1Context *v)
4249 MpegEncContext *s = &v->s;
4250 GetBitContext *gb = &s->gb;
4252 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4253 int cbp = 0; /* cbp decoding stuff */
4254 int mqdiff, mquant; /* MB quantization */
4255 int ttmb = v->ttfrm; /* MB Transform type */
4256 int mb_has_coeffs = 0; /* last_flag */
4257 int val; /* temp value */
4258 int first_block = 1;
4261 int dmv_x[2], dmv_y[2], pred_flag[2];
4262 int bmvtype = BMV_TYPE_BACKWARD;
4265 mquant = v->pq; /* Lossy initialization */
4268 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4269 if (idx_mbmode <= 1) { // intra MB
4270 s->mb_intra = v->is_intra[s->mb_x] = 1;
4271 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4272 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4273 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4275 s->current_picture.qscale_table[mb_pos] = mquant;
4276 /* Set DC scale - y and c use the same (not sure if necessary here) */
4277 s->y_dc_scale = s->y_dc_scale_table[mquant];
4278 s->c_dc_scale = s->c_dc_scale_table[mquant];
4279 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4280 mb_has_coeffs = idx_mbmode & 1;
4282 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4284 for (i = 0; i < 6; i++) {
4285 s->dc_val[0][s->block_index[i]] = 0;
4287 val = ((cbp >> (5 - i)) & 1);
4288 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4289 v->a_avail = v->c_avail = 0;
4290 if (i == 2 || i == 3 || !s->first_slice_line)
4291 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4292 if (i == 1 || i == 3 || s->mb_x)
4293 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4295 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4296 (i & 4) ? v->codingset2 : v->codingset);
4297 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4299 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4301 for (j = 0; j < 64; j++)
4302 s->block[i][j] <<= 1;
4303 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4304 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4305 // TODO: yet to perform loop filter
4308 s->mb_intra = v->is_intra[s->mb_x] = 0;
4309 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4310 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4312 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4314 fwd = v->forward_mb_plane[mb_pos];
4315 if (idx_mbmode <= 5) { // 1-MV
4317 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4318 pred_flag[0] = pred_flag[1] = 0;
4320 bmvtype = BMV_TYPE_FORWARD;
4322 bmvtype = decode012(gb);
4325 bmvtype = BMV_TYPE_BACKWARD;
4328 bmvtype = BMV_TYPE_DIRECT;
4331 bmvtype = BMV_TYPE_INTERPOLATED;
4332 interpmvp = get_bits1(gb);
4335 v->bmvtype = bmvtype;
4336 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4337 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4340 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4342 if (bmvtype == BMV_TYPE_DIRECT) {
4343 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4344 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4345 if (!s->next_picture_ptr->field_picture) {
4346 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
4350 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4351 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4352 mb_has_coeffs = !(idx_mbmode & 2);
4355 bmvtype = BMV_TYPE_FORWARD;
4356 v->bmvtype = bmvtype;
4357 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4358 for (i = 0; i < 6; i++) {
4360 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4361 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4362 val = ((v->fourmvbp >> (3 - i)) & 1);
4364 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4365 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4366 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4368 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4369 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4371 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4373 mb_has_coeffs = idx_mbmode & 1;
4376 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4380 s->current_picture.qscale_table[mb_pos] = mquant;
4381 if (!v->ttmbf && cbp) {
4382 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4385 for (i = 0; i < 6; i++) {
4386 s->dc_val[0][s->block_index[i]] = 0;
4388 val = ((cbp >> (5 - i)) & 1);
4389 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4391 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4392 first_block, s->dest[dst_idx] + off,
4393 (i & 4) ? s->uvlinesize : s->linesize,
4394 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4395 if (!v->ttmbf && ttmb < 8)
4403 /** Decode one B-frame MB (in interlaced frame B picture)
4405 static int vc1_decode_b_mb_intfr(VC1Context *v)
4407 MpegEncContext *s = &v->s;
4408 GetBitContext *gb = &s->gb;
4410 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4411 int cbp = 0; /* cbp decoding stuff */
4412 int mqdiff, mquant; /* MB quantization */
4413 int ttmb = v->ttfrm; /* MB Transform type */
4414 int mvsw = 0; /* motion vector switch */
4415 int mb_has_coeffs = 1; /* last_flag */
4416 int dmv_x, dmv_y; /* Differential MV components */
4417 int val; /* temp value */
4418 int first_block = 1;
4420 int skipped, direct, twomv = 0;
4421 int block_cbp = 0, pat, block_tt = 0;
4422 int idx_mbmode = 0, mvbp;
4423 int stride_y, fieldtx;
4424 int bmvtype = BMV_TYPE_BACKWARD;
4427 mquant = v->pq; /* Lossy initialization */
4430 skipped = get_bits1(gb);
4432 skipped = v->s.mbskip_table[mb_pos];
4435 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4436 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4438 v->blk_mv_type[s->block_index[0]] = 1;
4439 v->blk_mv_type[s->block_index[1]] = 1;
4440 v->blk_mv_type[s->block_index[2]] = 1;
4441 v->blk_mv_type[s->block_index[3]] = 1;
4443 v->blk_mv_type[s->block_index[0]] = 0;
4444 v->blk_mv_type[s->block_index[1]] = 0;
4445 v->blk_mv_type[s->block_index[2]] = 0;
4446 v->blk_mv_type[s->block_index[3]] = 0;
4451 direct = get_bits1(gb);
4453 direct = v->direct_mb_plane[mb_pos];
4456 if (s->next_picture_ptr->field_picture)
4457 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
4458 s->mv[0][0][0] = s->current_picture.motion_val[0][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 0, s->quarter_sample);
4459 s->mv[0][0][1] = s->current_picture.motion_val[0][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 0, s->quarter_sample);
4460 s->mv[1][0][0] = s->current_picture.motion_val[1][s->block_index[0]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][0], v->bfraction, 1, s->quarter_sample);
4461 s->mv[1][0][1] = s->current_picture.motion_val[1][s->block_index[0]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0]][1], v->bfraction, 1, s->quarter_sample);
4464 s->mv[0][2][0] = s->current_picture.motion_val[0][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 0, s->quarter_sample);
4465 s->mv[0][2][1] = s->current_picture.motion_val[0][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 0, s->quarter_sample);
4466 s->mv[1][2][0] = s->current_picture.motion_val[1][s->block_index[2]][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][0], v->bfraction, 1, s->quarter_sample);
4467 s->mv[1][2][1] = s->current_picture.motion_val[1][s->block_index[2]][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[2]][1], v->bfraction, 1, s->quarter_sample);
4469 for (i = 1; i < 4; i += 2) {
4470 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4471 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4472 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4473 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4476 for (i = 1; i < 4; i++) {
4477 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4478 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4479 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4480 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4485 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4486 for (i = 0; i < 4; i++) {
4487 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4488 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4489 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4490 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4492 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4493 s->mb_intra = v->is_intra[s->mb_x] = 1;
4494 for (i = 0; i < 6; i++)
4495 v->mb_type[0][s->block_index[i]] = 1;
4496 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4497 mb_has_coeffs = get_bits1(gb);
4499 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4500 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4502 s->current_picture.qscale_table[mb_pos] = mquant;
4503 /* Set DC scale - y and c use the same (not sure if necessary here) */
4504 s->y_dc_scale = s->y_dc_scale_table[mquant];
4505 s->c_dc_scale = s->c_dc_scale_table[mquant];
4507 for (i = 0; i < 6; i++) {
4508 s->dc_val[0][s->block_index[i]] = 0;
4510 val = ((cbp >> (5 - i)) & 1);
4511 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4512 v->a_avail = v->c_avail = 0;
4513 if (i == 2 || i == 3 || !s->first_slice_line)
4514 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4515 if (i == 1 || i == 3 || s->mb_x)
4516 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4518 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4519 (i & 4) ? v->codingset2 : v->codingset);
4520 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4522 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4524 stride_y = s->linesize << fieldtx;
4525 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4527 stride_y = s->uvlinesize;
4530 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4533 s->mb_intra = v->is_intra[s->mb_x] = 0;
4535 if (skipped || !s->mb_intra) {
4536 bmvtype = decode012(gb);
4539 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4542 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4545 bmvtype = BMV_TYPE_INTERPOLATED;
4549 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4550 mvsw = get_bits1(gb);
4553 if (!skipped) { // inter MB
4554 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4556 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4558 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
4559 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4560 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
4561 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4565 for (i = 0; i < 6; i++)
4566 v->mb_type[0][s->block_index[i]] = 0;
4567 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4568 /* for all motion vector read MVDATA and motion compensate each block */
4572 for (i = 0; i < 4; i++) {
4573 vc1_mc_4mv_luma(v, i, 0, 0);
4574 vc1_mc_4mv_luma(v, i, 1, 1);
4576 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4577 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4582 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4584 for (i = 0; i < 4; i++) {
4587 val = ((mvbp >> (3 - i)) & 1);
4589 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4591 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4592 vc1_mc_4mv_luma(v, j, dir, dir);
4593 vc1_mc_4mv_luma(v, j+1, dir, dir);
4596 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4597 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4598 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4602 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4604 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4609 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4611 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4614 dir = bmvtype == BMV_TYPE_BACKWARD;
4621 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4622 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4626 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4627 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4630 for (i = 0; i < 2; i++) {
4631 s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
4632 s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
4633 s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
4634 s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
4637 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4638 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4641 vc1_mc_4mv_luma(v, 0, dir, 0);
4642 vc1_mc_4mv_luma(v, 1, dir, 0);
4643 vc1_mc_4mv_luma(v, 2, dir2, 0);
4644 vc1_mc_4mv_luma(v, 3, dir2, 0);
4645 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4647 dir = bmvtype == BMV_TYPE_BACKWARD;
4649 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4652 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4654 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4655 v->blk_mv_type[s->block_index[0]] = 1;
4656 v->blk_mv_type[s->block_index[1]] = 1;
4657 v->blk_mv_type[s->block_index[2]] = 1;
4658 v->blk_mv_type[s->block_index[3]] = 1;
4659 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4660 for (i = 0; i < 2; i++) {
4661 s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
4662 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
4668 GET_MQUANT(); // p. 227
4669 s->current_picture.qscale_table[mb_pos] = mquant;
4670 if (!v->ttmbf && cbp)
4671 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4672 for (i = 0; i < 6; i++) {
4673 s->dc_val[0][s->block_index[i]] = 0;
4675 val = ((cbp >> (5 - i)) & 1);
4677 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4679 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4681 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4682 first_block, s->dest[dst_idx] + off,
4683 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4684 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4685 block_cbp |= pat << (i << 2);
4686 if (!v->ttmbf && ttmb < 8)
4694 for (i = 0; i < 6; i++) {
4695 v->mb_type[0][s->block_index[i]] = 0;
4696 s->dc_val[0][s->block_index[i]] = 0;
4698 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4699 s->current_picture.qscale_table[mb_pos] = 0;
4700 v->blk_mv_type[s->block_index[0]] = 0;
4701 v->blk_mv_type[s->block_index[1]] = 0;
4702 v->blk_mv_type[s->block_index[2]] = 0;
4703 v->blk_mv_type[s->block_index[3]] = 0;
4706 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4707 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4708 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4710 dir = bmvtype == BMV_TYPE_BACKWARD;
4711 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4716 for (i = 0; i < 2; i++) {
4717 s->mv[dir][i+2][0] = s->mv[dir][i][0] = s->current_picture.motion_val[dir][s->block_index[i+2]][0] = s->current_picture.motion_val[dir][s->block_index[i]][0];
4718 s->mv[dir][i+2][1] = s->mv[dir][i][1] = s->current_picture.motion_val[dir][s->block_index[i+2]][1] = s->current_picture.motion_val[dir][s->block_index[i]][1];
4719 s->mv[dir2][i+2][0] = s->mv[dir2][i][0] = s->current_picture.motion_val[dir2][s->block_index[i]][0] = s->current_picture.motion_val[dir2][s->block_index[i+2]][0];
4720 s->mv[dir2][i+2][1] = s->mv[dir2][i][1] = s->current_picture.motion_val[dir2][s->block_index[i]][1] = s->current_picture.motion_val[dir2][s->block_index[i+2]][1];
4723 v->blk_mv_type[s->block_index[0]] = 1;
4724 v->blk_mv_type[s->block_index[1]] = 1;
4725 v->blk_mv_type[s->block_index[2]] = 1;
4726 v->blk_mv_type[s->block_index[3]] = 1;
4727 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4728 for (i = 0; i < 2; i++) {
4729 s->mv[!dir][i+2][0] = s->mv[!dir][i][0] = s->current_picture.motion_val[!dir][s->block_index[i+2]][0] = s->current_picture.motion_val[!dir][s->block_index[i]][0];
4730 s->mv[!dir][i+2][1] = s->mv[!dir][i][1] = s->current_picture.motion_val[!dir][s->block_index[i+2]][1] = s->current_picture.motion_val[!dir][s->block_index[i]][1];
4737 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4742 if (s->mb_x == s->mb_width - 1)
4743 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4744 v->cbp[s->mb_x] = block_cbp;
4745 v->ttblk[s->mb_x] = block_tt;
4749 /** Decode blocks of I-frame
4751 static void vc1_decode_i_blocks(VC1Context *v)
4754 MpegEncContext *s = &v->s;
4759 /* select codingmode used for VLC tables selection */
4760 switch (v->y_ac_table_index) {
4762 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4765 v->codingset = CS_HIGH_MOT_INTRA;
4768 v->codingset = CS_MID_RATE_INTRA;
4772 switch (v->c_ac_table_index) {
4774 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4777 v->codingset2 = CS_HIGH_MOT_INTER;
4780 v->codingset2 = CS_MID_RATE_INTER;
4784 /* Set DC scale - y and c use the same */
4785 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4786 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4789 s->mb_x = s->mb_y = 0;
4791 s->first_slice_line = 1;
4792 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4794 init_block_index(v);
4795 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4797 ff_update_block_index(s);
4798 dst[0] = s->dest[0];
4799 dst[1] = dst[0] + 8;
4800 dst[2] = s->dest[0] + s->linesize * 8;
4801 dst[3] = dst[2] + 8;
4802 dst[4] = s->dest[1];
4803 dst[5] = s->dest[2];
4804 s->bdsp.clear_blocks(s->block[0]);
4805 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4806 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4807 s->current_picture.qscale_table[mb_pos] = v->pq;
4808 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4809 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4811 // do actual MB decoding and displaying
4812 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4813 v->s.ac_pred = get_bits1(&v->s.gb);
4815 for (k = 0; k < 6; k++) {
4816 val = ((cbp >> (5 - k)) & 1);
4819 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4823 cbp |= val << (5 - k);
4825 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4827 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4829 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4830 if (v->pq >= 9 && v->overlap) {
4832 for (j = 0; j < 64; j++)
4833 s->block[k][j] <<= 1;
4834 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4837 for (j = 0; j < 64; j++)
4838 s->block[k][j] = (s->block[k][j] - 64) << 1;
4839 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4843 if (v->pq >= 9 && v->overlap) {
4845 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4846 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4847 if (!(s->flags & CODEC_FLAG_GRAY)) {
4848 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4849 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4852 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4853 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4854 if (!s->first_slice_line) {
4855 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4856 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4857 if (!(s->flags & CODEC_FLAG_GRAY)) {
4858 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4859 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4862 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4863 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4865 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4867 if (get_bits_count(&s->gb) > v->bits) {
4868 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4869 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4870 get_bits_count(&s->gb), v->bits);
4874 if (!v->s.loop_filter)
4875 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4877 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4879 s->first_slice_line = 0;
4881 if (v->s.loop_filter)
4882 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4884 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4885 * profile, these only differ are when decoding MSS2 rectangles. */
4886 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4889 /** Decode blocks of I-frame for advanced profile
4891 static void vc1_decode_i_blocks_adv(VC1Context *v)
4894 MpegEncContext *s = &v->s;
4900 GetBitContext *gb = &s->gb;
4902 /* select codingmode used for VLC tables selection */
4903 switch (v->y_ac_table_index) {
4905 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4908 v->codingset = CS_HIGH_MOT_INTRA;
4911 v->codingset = CS_MID_RATE_INTRA;
4915 switch (v->c_ac_table_index) {
4917 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4920 v->codingset2 = CS_HIGH_MOT_INTER;
4923 v->codingset2 = CS_MID_RATE_INTER;
4928 s->mb_x = s->mb_y = 0;
4930 s->first_slice_line = 1;
4931 s->mb_y = s->start_mb_y;
4932 if (s->start_mb_y) {
4934 init_block_index(v);
4935 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4936 (1 + s->b8_stride) * sizeof(*s->coded_block));
4938 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4940 init_block_index(v);
4941 for (;s->mb_x < s->mb_width; s->mb_x++) {
4942 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4943 ff_update_block_index(s);
4944 s->bdsp.clear_blocks(block[0]);
4945 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4946 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4947 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4948 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4950 // do actual MB decoding and displaying
4951 if (v->fieldtx_is_raw)
4952 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4953 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4954 if ( v->acpred_is_raw)
4955 v->s.ac_pred = get_bits1(&v->s.gb);
4957 v->s.ac_pred = v->acpred_plane[mb_pos];
4959 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4960 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4964 s->current_picture.qscale_table[mb_pos] = mquant;
4965 /* Set DC scale - y and c use the same */
4966 s->y_dc_scale = s->y_dc_scale_table[mquant];
4967 s->c_dc_scale = s->c_dc_scale_table[mquant];
4969 for (k = 0; k < 6; k++) {
4970 val = ((cbp >> (5 - k)) & 1);
4973 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4977 cbp |= val << (5 - k);
4979 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4980 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4982 vc1_decode_i_block_adv(v, block[k], k, val,
4983 (k < 4) ? v->codingset : v->codingset2, mquant);
4985 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4987 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4990 vc1_smooth_overlap_filter_iblk(v);
4991 vc1_put_signed_blocks_clamped(v);
4992 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4994 if (get_bits_count(&s->gb) > v->bits) {
4995 // TODO: may need modification to handle slice coding
4996 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4997 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4998 get_bits_count(&s->gb), v->bits);
5002 if (!v->s.loop_filter)
5003 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5005 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
5006 s->first_slice_line = 0;
5009 /* raw bottom MB row */
5011 init_block_index(v);
5013 for (;s->mb_x < s->mb_width; s->mb_x++) {
5014 ff_update_block_index(s);
5015 vc1_put_signed_blocks_clamped(v);
5016 if (v->s.loop_filter)
5017 vc1_loop_filter_iblk_delayed(v, v->pq);
5019 if (v->s.loop_filter)
5020 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5021 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5022 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5025 static void vc1_decode_p_blocks(VC1Context *v)
5027 MpegEncContext *s = &v->s;
5028 int apply_loop_filter;
5030 /* select codingmode used for VLC tables selection */
5031 switch (v->c_ac_table_index) {
5033 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5036 v->codingset = CS_HIGH_MOT_INTRA;
5039 v->codingset = CS_MID_RATE_INTRA;
5043 switch (v->c_ac_table_index) {
5045 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5048 v->codingset2 = CS_HIGH_MOT_INTER;
5051 v->codingset2 = CS_MID_RATE_INTER;
5055 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5056 v->fcm == PROGRESSIVE;
5057 s->first_slice_line = 1;
5058 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5059 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5061 init_block_index(v);
5062 for (; s->mb_x < s->mb_width; s->mb_x++) {
5063 ff_update_block_index(s);
5065 if (v->fcm == ILACE_FIELD)
5066 vc1_decode_p_mb_intfi(v);
5067 else if (v->fcm == ILACE_FRAME)
5068 vc1_decode_p_mb_intfr(v);
5069 else vc1_decode_p_mb(v);
5070 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5071 vc1_apply_p_loop_filter(v);
5072 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5073 // TODO: may need modification to handle slice coding
5074 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5075 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5076 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5080 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5081 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5082 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5083 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5084 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5085 s->first_slice_line = 0;
5087 if (apply_loop_filter) {
5089 init_block_index(v);
5090 for (; s->mb_x < s->mb_width; s->mb_x++) {
5091 ff_update_block_index(s);
5092 vc1_apply_p_loop_filter(v);
5095 if (s->end_mb_y >= s->start_mb_y)
5096 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5097 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5098 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5101 static void vc1_decode_b_blocks(VC1Context *v)
5103 MpegEncContext *s = &v->s;
5105 /* select codingmode used for VLC tables selection */
5106 switch (v->c_ac_table_index) {
5108 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5111 v->codingset = CS_HIGH_MOT_INTRA;
5114 v->codingset = CS_MID_RATE_INTRA;
5118 switch (v->c_ac_table_index) {
5120 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5123 v->codingset2 = CS_HIGH_MOT_INTER;
5126 v->codingset2 = CS_MID_RATE_INTER;
5130 s->first_slice_line = 1;
5131 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5133 init_block_index(v);
5134 for (; s->mb_x < s->mb_width; s->mb_x++) {
5135 ff_update_block_index(s);
5137 if (v->fcm == ILACE_FIELD)
5138 vc1_decode_b_mb_intfi(v);
5139 else if (v->fcm == ILACE_FRAME)
5140 vc1_decode_b_mb_intfr(v);
5143 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5144 // TODO: may need modification to handle slice coding
5145 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5146 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5147 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5150 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5152 if (!v->s.loop_filter)
5153 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5155 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5156 s->first_slice_line = 0;
5158 if (v->s.loop_filter)
5159 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5160 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5161 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5164 static void vc1_decode_skip_blocks(VC1Context *v)
5166 MpegEncContext *s = &v->s;
5168 if (!v->s.last_picture.f->data[0])
5171 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5172 s->first_slice_line = 1;
5173 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5175 init_block_index(v);
5176 ff_update_block_index(s);
5177 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5178 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5179 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5180 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5181 s->first_slice_line = 0;
5183 s->pict_type = AV_PICTURE_TYPE_P;
5186 void ff_vc1_decode_blocks(VC1Context *v)
5189 v->s.esc3_level_length = 0;
5191 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5194 v->left_blk_idx = -1;
5195 v->topleft_blk_idx = 1;
5197 switch (v->s.pict_type) {
5198 case AV_PICTURE_TYPE_I:
5199 if (v->profile == PROFILE_ADVANCED)
5200 vc1_decode_i_blocks_adv(v);
5202 vc1_decode_i_blocks(v);
5204 case AV_PICTURE_TYPE_P:
5205 if (v->p_frame_skipped)
5206 vc1_decode_skip_blocks(v);
5208 vc1_decode_p_blocks(v);
5210 case AV_PICTURE_TYPE_B:
5212 if (v->profile == PROFILE_ADVANCED)
5213 vc1_decode_i_blocks_adv(v);
5215 vc1_decode_i_blocks(v);
5217 vc1_decode_b_blocks(v);
5223 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5227 * Transform coefficients for both sprites in 16.16 fixed point format,
5228 * in the order they appear in the bitstream:
5230 * rotation 1 (unused)
5232 * rotation 2 (unused)
5239 int effect_type, effect_flag;
5240 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5241 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5244 static inline int get_fp_val(GetBitContext* gb)
5246 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5249 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5253 switch (get_bits(gb, 2)) {
5256 c[2] = get_fp_val(gb);
5260 c[0] = c[4] = get_fp_val(gb);
5261 c[2] = get_fp_val(gb);
5264 c[0] = get_fp_val(gb);
5265 c[2] = get_fp_val(gb);
5266 c[4] = get_fp_val(gb);
5269 c[0] = get_fp_val(gb);
5270 c[1] = get_fp_val(gb);
5271 c[2] = get_fp_val(gb);
5272 c[3] = get_fp_val(gb);
5273 c[4] = get_fp_val(gb);
5276 c[5] = get_fp_val(gb);
5278 c[6] = get_fp_val(gb);
5283 static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5285 AVCodecContext *avctx = v->s.avctx;
5288 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5289 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5290 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5291 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5292 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5293 for (i = 0; i < 7; i++)
5294 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5295 sd->coefs[sprite][i] / (1<<16),
5296 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5297 av_log(avctx, AV_LOG_DEBUG, "\n");
5301 if (sd->effect_type = get_bits_long(gb, 30)) {
5302 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5304 vc1_sprite_parse_transform(gb, sd->effect_params1);
5307 vc1_sprite_parse_transform(gb, sd->effect_params1);
5308 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5311 for (i = 0; i < sd->effect_pcount1; i++)
5312 sd->effect_params1[i] = get_fp_val(gb);
5314 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5315 // effect 13 is simple alpha blending and matches the opacity above
5316 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5317 for (i = 0; i < sd->effect_pcount1; i++)
5318 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5319 sd->effect_params1[i] / (1 << 16),
5320 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5321 av_log(avctx, AV_LOG_DEBUG, "\n");
5324 sd->effect_pcount2 = get_bits(gb, 16);
5325 if (sd->effect_pcount2 > 10) {
5326 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5327 return AVERROR_INVALIDDATA;
5328 } else if (sd->effect_pcount2) {
5330 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5331 while (++i < sd->effect_pcount2) {
5332 sd->effect_params2[i] = get_fp_val(gb);
5333 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5334 sd->effect_params2[i] / (1 << 16),
5335 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5337 av_log(avctx, AV_LOG_DEBUG, "\n");
5340 if (sd->effect_flag = get_bits1(gb))
5341 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5343 if (get_bits_count(gb) >= gb->size_in_bits +
5344 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
5345 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5346 return AVERROR_INVALIDDATA;
5348 if (get_bits_count(gb) < gb->size_in_bits - 8)
5349 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5354 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5356 int i, plane, row, sprite;
5357 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5358 uint8_t* src_h[2][2];
5359 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5361 MpegEncContext *s = &v->s;
5363 for (i = 0; i <= v->two_sprites; i++) {
5364 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5365 xadv[i] = sd->coefs[i][0];
5366 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5367 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5369 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5370 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5372 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5374 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5375 int width = v->output_width>>!!plane;
5377 for (row = 0; row < v->output_height>>!!plane; row++) {
5378 uint8_t *dst = v->sprite_output_frame->data[plane] +
5379 v->sprite_output_frame->linesize[plane] * row;
5381 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5382 uint8_t *iplane = s->current_picture.f->data[plane];
5383 int iline = s->current_picture.f->linesize[plane];
5384 int ycoord = yoff[sprite] + yadv[sprite] * row;
5385 int yline = ycoord >> 16;
5387 ysub[sprite] = ycoord & 0xFFFF;
5389 iplane = s->last_picture.f->data[plane];
5390 iline = s->last_picture.f->linesize[plane];
5392 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5393 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5394 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5396 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5398 if (sr_cache[sprite][0] != yline) {
5399 if (sr_cache[sprite][1] == yline) {
5400 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5401 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5403 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5404 sr_cache[sprite][0] = yline;
5407 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5408 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5409 iplane + next_line, xoff[sprite],
5410 xadv[sprite], width);
5411 sr_cache[sprite][1] = yline + 1;
5413 src_h[sprite][0] = v->sr_rows[sprite][0];
5414 src_h[sprite][1] = v->sr_rows[sprite][1];
5418 if (!v->two_sprites) {
5420 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5422 memcpy(dst, src_h[0][0], width);
5425 if (ysub[0] && ysub[1]) {
5426 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5427 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5428 } else if (ysub[0]) {
5429 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5430 src_h[1][0], alpha, width);
5431 } else if (ysub[1]) {
5432 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5433 src_h[0][0], (1<<16)-1-alpha, width);
5435 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5441 for (i = 0; i <= v->two_sprites; i++) {
5451 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5454 MpegEncContext *s = &v->s;
5455 AVCodecContext *avctx = s->avctx;
5458 memset(&sd, 0, sizeof(sd));
5460 ret = vc1_parse_sprites(v, gb, &sd);
5464 if (!s->current_picture.f->data[0]) {
5465 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5469 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
5470 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5474 av_frame_unref(v->sprite_output_frame);
5475 if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
5478 vc1_draw_sprites(v, &sd);
5483 static void vc1_sprite_flush(AVCodecContext *avctx)
5485 VC1Context *v = avctx->priv_data;
5486 MpegEncContext *s = &v->s;
5487 AVFrame *f = s->current_picture.f;
5490 /* Windows Media Image codecs have a convergence interval of two keyframes.
5491 Since we can't enforce it, clear to black the missing sprite. This is
5492 wrong but it looks better than doing nothing. */
5495 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5496 for (i = 0; i < v->sprite_height>>!!plane; i++)
5497 memset(f->data[plane] + i * f->linesize[plane],
5498 plane ? 128 : 0, f->linesize[plane]);
5503 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5505 MpegEncContext *s = &v->s;
5507 int mb_height = FFALIGN(s->mb_height, 2);
5509 /* Allocate mb bitplanes */
5510 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
5511 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
5512 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
5513 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
5514 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
5515 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
5517 v->n_allocated_blks = s->mb_width + 2;
5518 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5519 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5520 v->cbp = v->cbp_base + s->mb_stride;
5521 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5522 v->ttblk = v->ttblk_base + s->mb_stride;
5523 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5524 v->is_intra = v->is_intra_base + s->mb_stride;
5525 v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5526 v->luma_mv = v->luma_mv_base + s->mb_stride;
5528 /* allocate block type info in that way so it could be used with s->block_index[] */
5529 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5530 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5531 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
5532 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
5534 /* allocate memory to store block level MV info */
5535 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5536 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5537 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5538 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5539 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5540 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5541 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5542 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5544 /* Init coded blocks info */
5545 if (v->profile == PROFILE_ADVANCED) {
5546 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5548 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5552 ff_intrax8_common_init(&v->x8,s);
5554 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5555 for (i = 0; i < 4; i++)
5556 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
5557 return AVERROR(ENOMEM);
5560 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5561 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5563 av_freep(&v->mv_type_mb_plane);
5564 av_freep(&v->direct_mb_plane);
5565 av_freep(&v->acpred_plane);
5566 av_freep(&v->over_flags_plane);
5567 av_freep(&v->block);
5568 av_freep(&v->cbp_base);
5569 av_freep(&v->ttblk_base);
5570 av_freep(&v->is_intra_base);
5571 av_freep(&v->luma_mv_base);
5572 av_freep(&v->mb_type_base);
5573 return AVERROR(ENOMEM);
5579 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5582 for (i = 0; i < 64; i++) {
5583 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
5584 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5585 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5586 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5587 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5588 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5594 /** Initialize a VC1/WMV3 decoder
5595 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5596 * @todo TODO: Decypher remaining bits in extra_data
5598 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5600 VC1Context *v = avctx->priv_data;
5601 MpegEncContext *s = &v->s;
5605 /* save the container output size for WMImage */
5606 v->output_width = avctx->width;
5607 v->output_height = avctx->height;
5609 if (!avctx->extradata_size || !avctx->extradata)
5611 if (!(avctx->flags & CODEC_FLAG_GRAY))
5612 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
5614 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5617 if ((ret = ff_vc1_init_common(v)) < 0)
5619 // ensure static VLC tables are initialized
5620 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
5622 if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
5624 // Hack to ensure the above functions will be called
5625 // again once we know all necessary settings.
5626 // That this is necessary might indicate a bug.
5627 ff_vc1_decode_end(avctx);
5629 ff_blockdsp_init(&s->bdsp, avctx);
5630 ff_h264chroma_init(&v->h264chroma, 8);
5631 ff_qpeldsp_init(&s->qdsp);
5633 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5636 // looks like WMV3 has a sequence header stored in the extradata
5637 // advanced sequence header may be before the first frame
5638 // the last byte of the extradata is a version number, 1 for the
5639 // samples we can decode
5641 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5643 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
5646 count = avctx->extradata_size*8 - get_bits_count(&gb);
5648 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5649 count, get_bits(&gb, count));
5650 } else if (count < 0) {
5651 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5653 } else { // VC1/WVC1/WVP2
5654 const uint8_t *start = avctx->extradata;
5655 uint8_t *end = avctx->extradata + avctx->extradata_size;
5656 const uint8_t *next;
5657 int size, buf2_size;
5658 uint8_t *buf2 = NULL;
5659 int seq_initialized = 0, ep_initialized = 0;
5661 if (avctx->extradata_size < 16) {
5662 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5666 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5667 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5669 for (; next < end; start = next) {
5670 next = find_next_marker(start + 4, end);
5671 size = next - start - 4;
5674 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5675 init_get_bits(&gb, buf2, buf2_size * 8);
5676 switch (AV_RB32(start)) {
5677 case VC1_CODE_SEQHDR:
5678 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
5682 seq_initialized = 1;
5684 case VC1_CODE_ENTRYPOINT:
5685 if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
5694 if (!seq_initialized || !ep_initialized) {
5695 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5698 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5701 v->sprite_output_frame = av_frame_alloc();
5702 if (!v->sprite_output_frame)
5703 return AVERROR(ENOMEM);
5705 avctx->profile = v->profile;
5706 if (v->profile == PROFILE_ADVANCED)
5707 avctx->level = v->level;
5709 avctx->has_b_frames = !!avctx->max_b_frames;
5711 s->mb_width = (avctx->coded_width + 15) >> 4;
5712 s->mb_height = (avctx->coded_height + 15) >> 4;
5714 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5715 ff_vc1_init_transposed_scantables(v);
5717 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5722 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5723 v->sprite_width = avctx->coded_width;
5724 v->sprite_height = avctx->coded_height;
5726 avctx->coded_width = avctx->width = v->output_width;
5727 avctx->coded_height = avctx->height = v->output_height;
5729 // prevent 16.16 overflows
5730 if (v->sprite_width > 1 << 14 ||
5731 v->sprite_height > 1 << 14 ||
5732 v->output_width > 1 << 14 ||
5733 v->output_height > 1 << 14) return -1;
5735 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5736 avpriv_request_sample(avctx, "odd sprites support");
5737 return AVERROR_PATCHWELCOME;
5743 /** Close a VC1/WMV3 decoder
5744 * @warning Initial try at using MpegEncContext stuff
5746 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5748 VC1Context *v = avctx->priv_data;
5751 av_frame_free(&v->sprite_output_frame);
5753 for (i = 0; i < 4; i++)
5754 av_freep(&v->sr_rows[i >> 1][i & 1]);
5755 av_freep(&v->hrd_rate);
5756 av_freep(&v->hrd_buffer);
5757 ff_MPV_common_end(&v->s);
5758 av_freep(&v->mv_type_mb_plane);
5759 av_freep(&v->direct_mb_plane);
5760 av_freep(&v->forward_mb_plane);
5761 av_freep(&v->fieldtx_plane);
5762 av_freep(&v->acpred_plane);
5763 av_freep(&v->over_flags_plane);
5764 av_freep(&v->mb_type_base);
5765 av_freep(&v->blk_mv_type_base);
5766 av_freep(&v->mv_f_base);
5767 av_freep(&v->mv_f_next_base);
5768 av_freep(&v->block);
5769 av_freep(&v->cbp_base);
5770 av_freep(&v->ttblk_base);
5771 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5772 av_freep(&v->luma_mv_base);
5773 ff_intrax8_common_end(&v->x8);
5778 /** Decode a VC1/WMV3 frame
5779 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5781 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5782 int *got_frame, AVPacket *avpkt)
5784 const uint8_t *buf = avpkt->data;
5785 int buf_size = avpkt->size, n_slices = 0, i, ret;
5786 VC1Context *v = avctx->priv_data;
5787 MpegEncContext *s = &v->s;
5788 AVFrame *pict = data;
5789 uint8_t *buf2 = NULL;
5790 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5791 int mb_height, n_slices1=-1;
5796 } *slices = NULL, *tmp;
5798 v->second_field = 0;
5800 if(s->flags & CODEC_FLAG_LOW_DELAY)
5803 /* no supplementary picture */
5804 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5805 /* special case for last picture */
5806 if (s->low_delay == 0 && s->next_picture_ptr) {
5807 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
5809 s->next_picture_ptr = NULL;
5817 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5818 if (v->profile < PROFILE_ADVANCED)
5819 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5821 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5824 //for advanced profile we may need to parse and unescape data
5825 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5827 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5829 return AVERROR(ENOMEM);
5831 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5832 const uint8_t *start, *end, *next;
5836 for (start = buf, end = buf + buf_size; next < end; start = next) {
5837 next = find_next_marker(start + 4, end);
5838 size = next - start - 4;
5839 if (size <= 0) continue;
5840 switch (AV_RB32(start)) {
5841 case VC1_CODE_FRAME:
5842 if (avctx->hwaccel ||
5843 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5845 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5847 case VC1_CODE_FIELD: {
5849 if (avctx->hwaccel ||
5850 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5851 buf_start_second_field = start;
5852 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5856 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5857 if (!slices[n_slices].buf)
5859 buf_size3 = vc1_unescape_buffer(start + 4, size,
5860 slices[n_slices].buf);
5861 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5863 /* assuming that the field marker is at the exact middle,
5864 hope it's correct */
5865 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5866 n_slices1 = n_slices - 1; // index of the last slice of the first field
5870 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5871 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5872 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5873 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5875 case VC1_CODE_SLICE: {
5877 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5881 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5882 if (!slices[n_slices].buf)
5884 buf_size3 = vc1_unescape_buffer(start + 4, size,
5885 slices[n_slices].buf);
5886 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5888 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5894 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5895 const uint8_t *divider;
5898 divider = find_next_marker(buf, buf + buf_size);
5899 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5900 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5902 } else { // found field marker, unescape second field
5903 if (avctx->hwaccel ||
5904 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5905 buf_start_second_field = divider;
5906 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5910 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5911 if (!slices[n_slices].buf)
5913 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5914 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5916 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5917 n_slices1 = n_slices - 1;
5920 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5922 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5924 init_get_bits(&s->gb, buf2, buf_size2*8);
5926 init_get_bits(&s->gb, buf, buf_size*8);
5928 if (v->res_sprite) {
5929 v->new_sprite = !get_bits1(&s->gb);
5930 v->two_sprites = get_bits1(&s->gb);
5931 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5932 we're using the sprite compositor. These are intentionally kept separate
5933 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5934 the vc1 one for WVP2 */
5935 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5936 if (v->new_sprite) {
5937 // switch AVCodecContext parameters to those of the sprites
5938 avctx->width = avctx->coded_width = v->sprite_width;
5939 avctx->height = avctx->coded_height = v->sprite_height;
5946 if (s->context_initialized &&
5947 (s->width != avctx->coded_width ||
5948 s->height != avctx->coded_height)) {
5949 ff_vc1_decode_end(avctx);
5952 if (!s->context_initialized) {
5953 if (ff_msmpeg4_decode_init(avctx) < 0)
5955 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
5956 ff_MPV_common_end(s);
5960 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5962 if (v->profile == PROFILE_ADVANCED) {
5963 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5965 s->h_edge_pos = avctx->coded_width;
5966 s->v_edge_pos = avctx->coded_height;
5970 // do parse frame header
5971 v->pic_header_flag = 0;
5972 v->first_pic_header_flag = 1;
5973 if (v->profile < PROFILE_ADVANCED) {
5974 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5978 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5982 v->first_pic_header_flag = 0;
5984 if (avctx->debug & FF_DEBUG_PICT_INFO)
5985 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5987 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5988 && s->pict_type != AV_PICTURE_TYPE_I) {
5989 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5993 if ((s->mb_height >> v->field_mode) == 0) {
5994 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5998 // for skipping the frame
5999 s->current_picture.f->pict_type = s->pict_type;
6000 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
6002 /* skip B-frames if we don't have reference frames */
6003 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
6004 av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
6007 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
6008 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
6009 avctx->skip_frame >= AVDISCARD_ALL) {
6013 if (s->next_p_frame_damaged) {
6014 if (s->pict_type == AV_PICTURE_TYPE_B)
6017 s->next_p_frame_damaged = 0;
6020 if (ff_MPV_frame_start(s, avctx) < 0) {
6024 v->s.current_picture_ptr->field_picture = v->field_mode;
6025 v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
6026 v->s.current_picture_ptr->f->top_field_first = v->tff;
6028 // process pulldown flags
6029 s->current_picture_ptr->f->repeat_pict = 0;
6030 // Pulldown flags are only valid when 'broadcast' has been set.
6031 // So ticks_per_frame will be 2
6034 s->current_picture_ptr->f->repeat_pict = 1;
6035 } else if (v->rptfrm) {
6037 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
6040 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
6041 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
6043 if ((CONFIG_VC1_VDPAU_DECODER)
6044 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6045 if (v->field_mode && buf_start_second_field) {
6046 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6047 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6049 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6051 } else if (avctx->hwaccel) {
6052 if (v->field_mode && buf_start_second_field) {
6053 // decode first field
6054 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6055 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6057 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6059 if (avctx->hwaccel->end_frame(avctx) < 0)
6062 // decode second field
6063 s->gb = slices[n_slices1 + 1].gb;
6064 s->picture_structure = PICT_TOP_FIELD + v->tff;
6065 v->second_field = 1;
6066 v->pic_header_flag = 0;
6067 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6068 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6071 v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
6073 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6075 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6077 if (avctx->hwaccel->end_frame(avctx) < 0)
6080 s->picture_structure = PICT_FRAME;
6081 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6083 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6085 if (avctx->hwaccel->end_frame(avctx) < 0)
6091 ff_mpeg_er_frame_start(s);
6093 v->bits = buf_size * 8;
6094 v->end_mb_x = s->mb_width;
6095 if (v->field_mode) {
6096 s->current_picture.f->linesize[0] <<= 1;
6097 s->current_picture.f->linesize[1] <<= 1;
6098 s->current_picture.f->linesize[2] <<= 1;
6100 s->uvlinesize <<= 1;
6102 mb_height = s->mb_height >> v->field_mode;
6104 av_assert0 (mb_height > 0);
6106 for (i = 0; i <= n_slices; i++) {
6107 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6108 if (v->field_mode <= 0) {
6109 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6110 "picture boundary (%d >= %d)\n", i,
6111 slices[i - 1].mby_start, mb_height);
6114 v->second_field = 1;
6115 av_assert0((s->mb_height & 1) == 0);
6116 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6117 v->mb_off = s->mb_stride * s->mb_height >> 1;
6119 v->second_field = 0;
6124 v->pic_header_flag = 0;
6125 if (v->field_mode && i == n_slices1 + 2) {
6126 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6127 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6128 if (avctx->err_recognition & AV_EF_EXPLODE)
6132 } else if (get_bits1(&s->gb)) {
6133 v->pic_header_flag = 1;
6134 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6135 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6136 if (avctx->err_recognition & AV_EF_EXPLODE)
6144 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6145 if (!v->field_mode || v->second_field)
6146 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6148 if (i >= n_slices) {
6149 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6152 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6154 if (s->end_mb_y <= s->start_mb_y) {
6155 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6158 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6159 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6162 ff_vc1_decode_blocks(v);
6164 s->gb = slices[i].gb;
6166 if (v->field_mode) {
6167 v->second_field = 0;
6168 s->current_picture.f->linesize[0] >>= 1;
6169 s->current_picture.f->linesize[1] >>= 1;
6170 s->current_picture.f->linesize[2] >>= 1;
6172 s->uvlinesize >>= 1;
6173 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6174 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6175 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6178 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6179 get_bits_count(&s->gb), s->gb.size_in_bits);
6180 // if (get_bits_count(&s->gb) > buf_size * 8)
6182 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6185 ff_er_frame_end(&s->er);
6188 ff_MPV_frame_end(s);
6190 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6192 avctx->width = avctx->coded_width = v->output_width;
6193 avctx->height = avctx->coded_height = v->output_height;
6194 if (avctx->skip_frame >= AVDISCARD_NONREF)
6196 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6197 if (vc1_decode_sprites(v, &s->gb))
6200 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
6204 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6205 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
6207 ff_print_debug_info(s, s->current_picture_ptr, pict);
6209 } else if (s->last_picture_ptr != NULL) {
6210 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
6212 ff_print_debug_info(s, s->last_picture_ptr, pict);
6219 for (i = 0; i < n_slices; i++)
6220 av_free(slices[i].buf);
6226 for (i = 0; i < n_slices; i++)
6227 av_free(slices[i].buf);
6233 static const AVProfile profiles[] = {
6234 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6235 { FF_PROFILE_VC1_MAIN, "Main" },
6236 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6237 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6238 { FF_PROFILE_UNKNOWN },
6241 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6242 #if CONFIG_VC1_DXVA2_HWACCEL
6243 AV_PIX_FMT_DXVA2_VLD,
6245 #if CONFIG_VC1_VAAPI_HWACCEL
6246 AV_PIX_FMT_VAAPI_VLD,
6248 #if CONFIG_VC1_VDPAU_HWACCEL
6255 AVCodec ff_vc1_decoder = {
6257 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6258 .type = AVMEDIA_TYPE_VIDEO,
6259 .id = AV_CODEC_ID_VC1,
6260 .priv_data_size = sizeof(VC1Context),
6261 .init = vc1_decode_init,
6262 .close = ff_vc1_decode_end,
6263 .decode = vc1_decode_frame,
6264 .flush = ff_mpeg_flush,
6265 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6266 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6267 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6270 #if CONFIG_WMV3_DECODER
6271 AVCodec ff_wmv3_decoder = {
6273 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6274 .type = AVMEDIA_TYPE_VIDEO,
6275 .id = AV_CODEC_ID_WMV3,
6276 .priv_data_size = sizeof(VC1Context),
6277 .init = vc1_decode_init,
6278 .close = ff_vc1_decode_end,
6279 .decode = vc1_decode_frame,
6280 .flush = ff_mpeg_flush,
6281 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6282 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6283 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6287 #if CONFIG_WMV3_VDPAU_DECODER
6288 AVCodec ff_wmv3_vdpau_decoder = {
6289 .name = "wmv3_vdpau",
6290 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6291 .type = AVMEDIA_TYPE_VIDEO,
6292 .id = AV_CODEC_ID_WMV3,
6293 .priv_data_size = sizeof(VC1Context),
6294 .init = vc1_decode_init,
6295 .close = ff_vc1_decode_end,
6296 .decode = vc1_decode_frame,
6297 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6298 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6299 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6303 #if CONFIG_VC1_VDPAU_DECODER
6304 AVCodec ff_vc1_vdpau_decoder = {
6305 .name = "vc1_vdpau",
6306 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6307 .type = AVMEDIA_TYPE_VIDEO,
6308 .id = AV_CODEC_ID_VC1,
6309 .priv_data_size = sizeof(VC1Context),
6310 .init = vc1_decode_init,
6311 .close = ff_vc1_decode_end,
6312 .decode = vc1_decode_frame,
6313 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6314 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6315 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6319 #if CONFIG_WMV3IMAGE_DECODER
6320 AVCodec ff_wmv3image_decoder = {
6321 .name = "wmv3image",
6322 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6323 .type = AVMEDIA_TYPE_VIDEO,
6324 .id = AV_CODEC_ID_WMV3IMAGE,
6325 .priv_data_size = sizeof(VC1Context),
6326 .init = vc1_decode_init,
6327 .close = ff_vc1_decode_end,
6328 .decode = vc1_decode_frame,
6329 .capabilities = CODEC_CAP_DR1,
6330 .flush = vc1_sprite_flush,
6331 .pix_fmts = (const enum AVPixelFormat[]) {
6338 #if CONFIG_VC1IMAGE_DECODER
6339 AVCodec ff_vc1image_decoder = {
6341 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6342 .type = AVMEDIA_TYPE_VIDEO,
6343 .id = AV_CODEC_ID_VC1IMAGE,
6344 .priv_data_size = sizeof(VC1Context),
6345 .init = vc1_decode_init,
6346 .close = ff_vc1_decode_end,
6347 .decode = vc1_decode_frame,
6348 .capabilities = CODEC_CAP_DR1,
6349 .flush = vc1_sprite_flush,
6350 .pix_fmts = (const enum AVPixelFormat[]) {