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 "mpegvideo.h"
34 #include "h264chroma.h"
37 #include "vc1acdata.h"
38 #include "msmpeg4data.h"
41 #include "vdpau_internal.h"
42 #include "libavutil/avassert.h"
47 #define MB_INTRA_VLC_BITS 9
51 // offset tables for interlaced picture MVDATA decoding
52 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
53 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
55 /***********************************************************************/
57 * @name VC-1 Bitplane decoding
75 /** @} */ //imode defines
77 static void init_block_index(VC1Context *v)
79 MpegEncContext *s = &v->s;
80 ff_init_block_index(s);
81 if (v->field_mode && !(v->second_field ^ v->tff)) {
82 s->dest[0] += s->current_picture_ptr->f.linesize[0];
83 s->dest[1] += s->current_picture_ptr->f.linesize[1];
84 s->dest[2] += s->current_picture_ptr->f.linesize[2];
88 /** @} */ //Bitplane group
90 static void vc1_put_signed_blocks_clamped(VC1Context *v)
92 MpegEncContext *s = &v->s;
93 int topleft_mb_pos, top_mb_pos;
94 int stride_y, fieldtx = 0;
97 /* The put pixels loop is always one MB row behind the decoding loop,
98 * because we can only put pixels when overlap filtering is done, and
99 * for filtering of the bottom edge of a MB, we need the next MB row
101 * Within the row, the put pixels loop is also one MB col behind the
102 * decoding loop. The reason for this is again, because for filtering
103 * of the right MB edge, we need the next MB present. */
104 if (!s->first_slice_line) {
106 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
107 if (v->fcm == ILACE_FRAME)
108 fieldtx = v->fieldtx_plane[topleft_mb_pos];
109 stride_y = s->linesize << fieldtx;
110 v_dist = (16 - fieldtx) >> (fieldtx == 0);
111 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
112 s->dest[0] - 16 * s->linesize - 16,
114 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
115 s->dest[0] - 16 * s->linesize - 8,
117 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
118 s->dest[0] - v_dist * s->linesize - 16,
120 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
121 s->dest[0] - v_dist * s->linesize - 8,
123 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
124 s->dest[1] - 8 * s->uvlinesize - 8,
126 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
127 s->dest[2] - 8 * s->uvlinesize - 8,
130 if (s->mb_x == s->mb_width - 1) {
131 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
132 if (v->fcm == ILACE_FRAME)
133 fieldtx = v->fieldtx_plane[top_mb_pos];
134 stride_y = s->linesize << fieldtx;
135 v_dist = fieldtx ? 15 : 8;
136 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
137 s->dest[0] - 16 * s->linesize,
139 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
140 s->dest[0] - 16 * s->linesize + 8,
142 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
143 s->dest[0] - v_dist * s->linesize,
145 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
146 s->dest[0] - v_dist * s->linesize + 8,
148 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
149 s->dest[1] - 8 * s->uvlinesize,
151 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
152 s->dest[2] - 8 * s->uvlinesize,
157 #define inc_blk_idx(idx) do { \
159 if (idx >= v->n_allocated_blks) \
163 inc_blk_idx(v->topleft_blk_idx);
164 inc_blk_idx(v->top_blk_idx);
165 inc_blk_idx(v->left_blk_idx);
166 inc_blk_idx(v->cur_blk_idx);
169 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
171 MpegEncContext *s = &v->s;
173 if (!s->first_slice_line) {
174 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
176 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
177 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
178 for (j = 0; j < 2; j++) {
179 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
181 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
184 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
186 if (s->mb_y == s->end_mb_y - 1) {
188 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
189 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
190 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
192 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
196 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
198 MpegEncContext *s = &v->s;
201 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
202 * means it runs two rows/cols behind the decoding loop. */
203 if (!s->first_slice_line) {
205 if (s->mb_y >= s->start_mb_y + 2) {
206 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
209 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
210 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
211 for (j = 0; j < 2; j++) {
212 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
214 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
218 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
221 if (s->mb_x == s->mb_width - 1) {
222 if (s->mb_y >= s->start_mb_y + 2) {
223 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
226 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
227 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
228 for (j = 0; j < 2; j++) {
229 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
231 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
235 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
238 if (s->mb_y == s->end_mb_y) {
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
242 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
244 for (j = 0; j < 2; j++) {
245 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
250 if (s->mb_x == s->mb_width - 1) {
252 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
253 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
255 for (j = 0; j < 2; j++) {
256 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
264 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
266 MpegEncContext *s = &v->s;
269 if (v->condover == CONDOVER_NONE)
272 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
274 /* Within a MB, the horizontal overlap always runs before the vertical.
275 * To accomplish that, we run the H on left and internal borders of the
276 * currently decoded MB. Then, we wait for the next overlap iteration
277 * to do H overlap on the right edge of this MB, before moving over and
278 * running the V overlap. Therefore, the V overlap makes us trail by one
279 * MB col and the H overlap filter makes us trail by one MB row. This
280 * is reflected in the time at which we run the put_pixels loop. */
281 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
282 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
283 v->over_flags_plane[mb_pos - 1])) {
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
285 v->block[v->cur_blk_idx][0]);
286 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
287 v->block[v->cur_blk_idx][2]);
288 if (!(s->flags & CODEC_FLAG_GRAY)) {
289 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
290 v->block[v->cur_blk_idx][4]);
291 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
292 v->block[v->cur_blk_idx][5]);
295 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
296 v->block[v->cur_blk_idx][1]);
297 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
298 v->block[v->cur_blk_idx][3]);
300 if (s->mb_x == s->mb_width - 1) {
301 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
302 v->over_flags_plane[mb_pos - s->mb_stride])) {
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
304 v->block[v->cur_blk_idx][0]);
305 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
306 v->block[v->cur_blk_idx][1]);
307 if (!(s->flags & CODEC_FLAG_GRAY)) {
308 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
309 v->block[v->cur_blk_idx][4]);
310 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
311 v->block[v->cur_blk_idx][5]);
314 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
315 v->block[v->cur_blk_idx][2]);
316 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
317 v->block[v->cur_blk_idx][3]);
320 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
321 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
322 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
324 v->block[v->left_blk_idx][0]);
325 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
326 v->block[v->left_blk_idx][1]);
327 if (!(s->flags & CODEC_FLAG_GRAY)) {
328 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
329 v->block[v->left_blk_idx][4]);
330 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
331 v->block[v->left_blk_idx][5]);
334 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
335 v->block[v->left_blk_idx][2]);
336 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
337 v->block[v->left_blk_idx][3]);
341 /** Do motion compensation over 1 macroblock
342 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
344 static void vc1_mc_1mv(VC1Context *v, int dir)
346 MpegEncContext *s = &v->s;
347 H264ChromaContext *h264chroma = &v->h264chroma;
348 uint8_t *srcY, *srcU, *srcV;
349 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
350 int v_edge_pos = s->v_edge_pos >> v->field_mode;
352 uint8_t (*luty)[256], (*lutuv)[256];
355 if ((!v->field_mode ||
356 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
357 !v->s.last_picture.f.data[0])
360 mx = s->mv[dir][0][0];
361 my = s->mv[dir][0][1];
363 // store motion vectors for further use in B frames
364 if (s->pict_type == AV_PICTURE_TYPE_P) {
365 for (i = 0; i < 4; i++) {
366 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
367 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
371 uvmx = (mx + ((mx & 3) == 3)) >> 1;
372 uvmy = (my + ((my & 3) == 3)) >> 1;
373 v->luma_mv[s->mb_x][0] = uvmx;
374 v->luma_mv[s->mb_x][1] = uvmy;
377 v->cur_field_type != v->ref_field_type[dir]) {
378 my = my - 2 + 4 * v->cur_field_type;
379 uvmy = uvmy - 2 + 4 * v->cur_field_type;
382 // fastuvmc shall be ignored for interlaced frame picture
383 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
384 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
385 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
388 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
389 srcY = s->current_picture.f.data[0];
390 srcU = s->current_picture.f.data[1];
391 srcV = s->current_picture.f.data[2];
393 lutuv = v->curr_lutuv;
394 use_ic = v->curr_use_ic;
396 srcY = s->last_picture.f.data[0];
397 srcU = s->last_picture.f.data[1];
398 srcV = s->last_picture.f.data[2];
400 lutuv = v->last_lutuv;
401 use_ic = v->last_use_ic;
404 srcY = s->next_picture.f.data[0];
405 srcU = s->next_picture.f.data[1];
406 srcV = s->next_picture.f.data[2];
408 lutuv = v->next_lutuv;
409 use_ic = v->next_use_ic;
415 src_x = s->mb_x * 16 + (mx >> 2);
416 src_y = s->mb_y * 16 + (my >> 2);
417 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
418 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
420 if (v->profile != PROFILE_ADVANCED) {
421 src_x = av_clip( src_x, -16, s->mb_width * 16);
422 src_y = av_clip( src_y, -16, s->mb_height * 16);
423 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
424 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
426 src_x = av_clip( src_x, -17, s->avctx->coded_width);
427 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
428 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
429 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
432 srcY += src_y * s->linesize + src_x;
433 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
434 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
436 if (v->field_mode && v->ref_field_type[dir]) {
437 srcY += s->current_picture_ptr->f.linesize[0];
438 srcU += s->current_picture_ptr->f.linesize[1];
439 srcV += s->current_picture_ptr->f.linesize[2];
442 /* for grayscale we should not try to read from unknown area */
443 if (s->flags & CODEC_FLAG_GRAY) {
444 srcU = s->edge_emu_buffer + 18 * s->linesize;
445 srcV = s->edge_emu_buffer + 18 * s->linesize;
448 if (v->rangeredfrm || use_ic
449 || s->h_edge_pos < 22 || v_edge_pos < 22
450 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
451 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
452 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
454 srcY -= s->mspel * (1 + s->linesize);
455 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
456 17 + s->mspel * 2, 17 + s->mspel * 2,
457 src_x - s->mspel, src_y - s->mspel,
458 s->h_edge_pos, v_edge_pos);
459 srcY = s->edge_emu_buffer;
460 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
461 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
462 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
463 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
466 /* if we deal with range reduction we need to scale source blocks */
467 if (v->rangeredfrm) {
472 for (j = 0; j < 17 + s->mspel * 2; j++) {
473 for (i = 0; i < 17 + s->mspel * 2; i++)
474 src[i] = ((src[i] - 128) >> 1) + 128;
479 for (j = 0; j < 9; j++) {
480 for (i = 0; i < 9; i++) {
481 src[i] = ((src[i] - 128) >> 1) + 128;
482 src2[i] = ((src2[i] - 128) >> 1) + 128;
484 src += s->uvlinesize;
485 src2 += s->uvlinesize;
488 /* if we deal with intensity compensation we need to scale source blocks */
494 for (j = 0; j < 17 + s->mspel * 2; j++) {
495 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
496 for (i = 0; i < 17 + s->mspel * 2; i++)
497 src[i] = luty[f][src[i]];
502 for (j = 0; j < 9; j++) {
503 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
504 for (i = 0; i < 9; i++) {
505 src[i] = lutuv[f][src[i]];
506 src2[i] = lutuv[f][src2[i]];
508 src += s->uvlinesize;
509 src2 += s->uvlinesize;
512 srcY += s->mspel * (1 + s->linesize);
516 dxy = ((my & 3) << 2) | (mx & 3);
517 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
518 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
519 srcY += s->linesize * 8;
520 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
521 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
522 } else { // hpel mc - always used for luma
523 dxy = (my & 2) | ((mx & 2) >> 1);
525 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
527 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
530 if (s->flags & CODEC_FLAG_GRAY) return;
531 /* Chroma MC always uses qpel bilinear */
532 uvmx = (uvmx & 3) << 1;
533 uvmy = (uvmy & 3) << 1;
535 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
536 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
538 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
539 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
543 static inline int median4(int a, int b, int c, int d)
546 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
547 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
549 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
550 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
554 /** Do motion compensation for 4-MV macroblock - luminance block
556 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
558 MpegEncContext *s = &v->s;
560 int dxy, mx, my, src_x, src_y;
562 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
563 int v_edge_pos = s->v_edge_pos >> v->field_mode;
564 uint8_t (*luty)[256];
567 if ((!v->field_mode ||
568 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
569 !v->s.last_picture.f.data[0])
572 mx = s->mv[dir][n][0];
573 my = s->mv[dir][n][1];
576 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
577 srcY = s->current_picture.f.data[0];
579 use_ic = v->curr_use_ic;
581 srcY = s->last_picture.f.data[0];
583 use_ic = v->last_use_ic;
586 srcY = s->next_picture.f.data[0];
588 use_ic = v->next_use_ic;
595 if (v->cur_field_type != v->ref_field_type[dir])
596 my = my - 2 + 4 * v->cur_field_type;
599 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
600 int same_count = 0, opp_count = 0, k;
601 int chosen_mv[2][4][2], f;
603 for (k = 0; k < 4; k++) {
604 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
605 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
606 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
610 f = opp_count > same_count;
611 switch (f ? opp_count : same_count) {
613 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
614 chosen_mv[f][2][0], chosen_mv[f][3][0]);
615 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
616 chosen_mv[f][2][1], chosen_mv[f][3][1]);
619 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
620 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
623 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
624 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
629 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
630 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
631 for (k = 0; k < 4; k++)
632 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
635 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
637 int width = s->avctx->coded_width;
638 int height = s->avctx->coded_height >> 1;
639 if (s->pict_type == AV_PICTURE_TYPE_P) {
640 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
641 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
643 qx = (s->mb_x * 16) + (mx >> 2);
644 qy = (s->mb_y * 8) + (my >> 3);
649 mx -= 4 * (qx - width);
652 else if (qy > height + 1)
653 my -= 8 * (qy - height - 1);
656 if ((v->fcm == ILACE_FRAME) && fieldmv)
657 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
659 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
661 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
663 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
665 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
667 if (v->profile != PROFILE_ADVANCED) {
668 src_x = av_clip(src_x, -16, s->mb_width * 16);
669 src_y = av_clip(src_y, -16, s->mb_height * 16);
671 src_x = av_clip(src_x, -17, s->avctx->coded_width);
672 if (v->fcm == ILACE_FRAME) {
674 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
676 src_y = av_clip(src_y, -18, s->avctx->coded_height);
678 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
682 srcY += src_y * s->linesize + src_x;
683 if (v->field_mode && v->ref_field_type[dir])
684 srcY += s->current_picture_ptr->f.linesize[0];
686 if (fieldmv && !(src_y & 1))
688 if (fieldmv && (src_y & 1) && src_y < 4)
690 if (v->rangeredfrm || use_ic
691 || s->h_edge_pos < 13 || v_edge_pos < 23
692 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
693 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
694 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
695 /* check emulate edge stride and offset */
696 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
697 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
698 src_x - s->mspel, src_y - (s->mspel << fieldmv),
699 s->h_edge_pos, v_edge_pos);
700 srcY = s->edge_emu_buffer;
701 /* if we deal with range reduction we need to scale source blocks */
702 if (v->rangeredfrm) {
707 for (j = 0; j < 9 + s->mspel * 2; j++) {
708 for (i = 0; i < 9 + s->mspel * 2; i++)
709 src[i] = ((src[i] - 128) >> 1) + 128;
710 src += s->linesize << fieldmv;
713 /* if we deal with intensity compensation we need to scale source blocks */
719 for (j = 0; j < 9 + s->mspel * 2; j++) {
720 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
721 for (i = 0; i < 9 + s->mspel * 2; i++)
722 src[i] = luty[f][src[i]];
723 src += s->linesize << fieldmv;
726 srcY += s->mspel * (1 + (s->linesize << fieldmv));
730 dxy = ((my & 3) << 2) | (mx & 3);
732 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
734 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
735 } else { // hpel mc - always used for luma
736 dxy = (my & 2) | ((mx & 2) >> 1);
738 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
740 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
744 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
747 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
749 idx = ((a[3] != flag) << 3)
750 | ((a[2] != flag) << 2)
751 | ((a[1] != flag) << 1)
754 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
755 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
757 } else if (count[idx] == 1) {
760 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
761 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
764 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
765 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
768 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
769 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
772 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
773 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
776 } else if (count[idx] == 2) {
778 for (i = 0; i < 3; i++)
783 for (i = t1 + 1; i < 4; i++)
788 *tx = (mvx[t1] + mvx[t2]) / 2;
789 *ty = (mvy[t1] + mvy[t2]) / 2;
797 /** Do motion compensation for 4-MV macroblock - both chroma blocks
799 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
801 MpegEncContext *s = &v->s;
802 H264ChromaContext *h264chroma = &v->h264chroma;
803 uint8_t *srcU, *srcV;
804 int uvmx, uvmy, uvsrc_x, uvsrc_y;
805 int k, tx = 0, ty = 0;
806 int mvx[4], mvy[4], intra[4], mv_f[4];
808 int chroma_ref_type = v->cur_field_type;
809 int v_edge_pos = s->v_edge_pos >> v->field_mode;
810 uint8_t (*lutuv)[256];
813 if (!v->field_mode && !v->s.last_picture.f.data[0])
815 if (s->flags & CODEC_FLAG_GRAY)
818 for (k = 0; k < 4; k++) {
819 mvx[k] = s->mv[dir][k][0];
820 mvy[k] = s->mv[dir][k][1];
821 intra[k] = v->mb_type[0][s->block_index[k]];
823 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
826 /* calculate chroma MV vector from four luma MVs */
827 if (!v->field_mode || (v->field_mode && !v->numref)) {
828 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
829 chroma_ref_type = v->reffield;
831 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
832 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
833 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
834 return; //no need to do MC for intra blocks
838 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
840 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
842 chroma_ref_type = !v->cur_field_type;
844 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
846 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
847 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
848 uvmx = (tx + ((tx & 3) == 3)) >> 1;
849 uvmy = (ty + ((ty & 3) == 3)) >> 1;
851 v->luma_mv[s->mb_x][0] = uvmx;
852 v->luma_mv[s->mb_x][1] = uvmy;
855 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
856 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
858 // Field conversion bias
859 if (v->cur_field_type != chroma_ref_type)
860 uvmy += 2 - 4 * chroma_ref_type;
862 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
863 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
865 if (v->profile != PROFILE_ADVANCED) {
866 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
867 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
869 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
870 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
874 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
875 srcU = s->current_picture.f.data[1];
876 srcV = s->current_picture.f.data[2];
877 lutuv = v->curr_lutuv;
878 use_ic = v->curr_use_ic;
880 srcU = s->last_picture.f.data[1];
881 srcV = s->last_picture.f.data[2];
882 lutuv = v->last_lutuv;
883 use_ic = v->last_use_ic;
886 srcU = s->next_picture.f.data[1];
887 srcV = s->next_picture.f.data[2];
888 lutuv = v->next_lutuv;
889 use_ic = v->next_use_ic;
895 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
896 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
899 if (chroma_ref_type) {
900 srcU += s->current_picture_ptr->f.linesize[1];
901 srcV += s->current_picture_ptr->f.linesize[2];
905 if (v->rangeredfrm || use_ic
906 || s->h_edge_pos < 18 || v_edge_pos < 18
907 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
908 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
909 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, 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, s->uvlinesize,
913 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
914 s->h_edge_pos >> 1, v_edge_pos >> 1);
915 srcU = s->edge_emu_buffer;
916 srcV = s->edge_emu_buffer + 16;
918 /* if we deal with range reduction we need to scale source blocks */
919 if (v->rangeredfrm) {
925 for (j = 0; j < 9; j++) {
926 for (i = 0; i < 9; i++) {
927 src[i] = ((src[i] - 128) >> 1) + 128;
928 src2[i] = ((src2[i] - 128) >> 1) + 128;
930 src += s->uvlinesize;
931 src2 += s->uvlinesize;
934 /* if we deal with intensity compensation we need to scale source blocks */
941 for (j = 0; j < 9; j++) {
942 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
943 for (i = 0; i < 9; i++) {
944 src[i] = lutuv[f][src[i]];
945 src2[i] = lutuv[f][src2[i]];
947 src += s->uvlinesize;
948 src2 += s->uvlinesize;
953 /* Chroma MC always uses qpel bilinear */
954 uvmx = (uvmx & 3) << 1;
955 uvmy = (uvmy & 3) << 1;
957 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
958 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
960 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
961 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
965 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
967 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
969 MpegEncContext *s = &v->s;
970 H264ChromaContext *h264chroma = &v->h264chroma;
971 uint8_t *srcU, *srcV;
972 int uvsrc_x, uvsrc_y;
973 int uvmx_field[4], uvmy_field[4];
975 int fieldmv = v->blk_mv_type[s->block_index[0]];
976 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
977 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
978 int v_edge_pos = s->v_edge_pos >> 1;
980 uint8_t (*lutuv)[256];
982 if (s->flags & CODEC_FLAG_GRAY)
985 for (i = 0; i < 4; i++) {
986 int d = i < 2 ? dir: dir2;
988 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
991 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
993 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
996 for (i = 0; i < 4; i++) {
997 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
998 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
999 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1000 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1001 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1002 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1003 if (i < 2 ? dir : dir2) {
1004 srcU = s->next_picture.f.data[1];
1005 srcV = s->next_picture.f.data[2];
1006 lutuv = v->next_lutuv;
1007 use_ic = v->next_use_ic;
1009 srcU = s->last_picture.f.data[1];
1010 srcV = s->last_picture.f.data[2];
1011 lutuv = v->last_lutuv;
1012 use_ic = v->last_use_ic;
1016 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1017 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1018 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1019 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1021 if (fieldmv && !(uvsrc_y & 1))
1022 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1024 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1027 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1028 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1029 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1030 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1031 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1032 s->h_edge_pos >> 1, v_edge_pos);
1033 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1034 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1035 s->h_edge_pos >> 1, v_edge_pos);
1036 srcU = s->edge_emu_buffer;
1037 srcV = s->edge_emu_buffer + 16;
1039 /* if we deal with intensity compensation we need to scale source blocks */
1042 uint8_t *src, *src2;
1046 for (j = 0; j < 5; j++) {
1047 int f = (uvsrc_y + (j << fieldmv)) & 1;
1048 for (i = 0; i < 5; i++) {
1049 src[i] = lutuv[f][src[i]];
1050 src2[i] = lutuv[f][src2[i]];
1052 src += s->uvlinesize << fieldmv;
1053 src2 += s->uvlinesize << fieldmv;
1059 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1060 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1062 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]);
1063 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]);
1067 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1068 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1070 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]);
1071 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]);
1077 /***********************************************************************/
1079 * @name VC-1 Block-level functions
1080 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1086 * @brief Get macroblock-level quantizer scale
1088 #define GET_MQUANT() \
1089 if (v->dquantfrm) { \
1091 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1092 if (v->dqbilevel) { \
1093 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1095 mqdiff = get_bits(gb, 3); \
1097 mquant = v->pq + mqdiff; \
1099 mquant = get_bits(gb, 5); \
1102 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1103 edges = 1 << v->dqsbedge; \
1104 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1105 edges = (3 << v->dqsbedge) % 15; \
1106 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1108 if ((edges&1) && !s->mb_x) \
1109 mquant = v->altpq; \
1110 if ((edges&2) && s->first_slice_line) \
1111 mquant = v->altpq; \
1112 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1113 mquant = v->altpq; \
1114 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1115 mquant = v->altpq; \
1116 if (!mquant || mquant > 31) { \
1117 av_log(v->s.avctx, AV_LOG_ERROR, \
1118 "Overriding invalid mquant %d\n", mquant); \
1124 * @def GET_MVDATA(_dmv_x, _dmv_y)
1125 * @brief Get MV differentials
1126 * @see MVDATA decoding from 8.3.5.2, p(1)20
1127 * @param _dmv_x Horizontal differential for decoded MV
1128 * @param _dmv_y Vertical differential for decoded MV
1130 #define GET_MVDATA(_dmv_x, _dmv_y) \
1131 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1132 VC1_MV_DIFF_VLC_BITS, 2); \
1134 mb_has_coeffs = 1; \
1137 mb_has_coeffs = 0; \
1140 _dmv_x = _dmv_y = 0; \
1141 } else if (index == 35) { \
1142 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1143 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1144 } else if (index == 36) { \
1149 index1 = index % 6; \
1150 if (!s->quarter_sample && index1 == 5) val = 1; \
1152 if (size_table[index1] - val > 0) \
1153 val = get_bits(gb, size_table[index1] - val); \
1155 sign = 0 - (val&1); \
1156 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1158 index1 = index / 6; \
1159 if (!s->quarter_sample && index1 == 5) val = 1; \
1161 if (size_table[index1] - val > 0) \
1162 val = get_bits(gb, size_table[index1] - val); \
1164 sign = 0 - (val & 1); \
1165 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1168 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1169 int *dmv_y, int *pred_flag)
1172 int extend_x = 0, extend_y = 0;
1173 GetBitContext *gb = &v->s.gb;
1176 const int* offs_tab;
1179 bits = VC1_2REF_MVDATA_VLC_BITS;
1182 bits = VC1_1REF_MVDATA_VLC_BITS;
1185 switch (v->dmvrange) {
1193 extend_x = extend_y = 1;
1196 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1198 *dmv_x = get_bits(gb, v->k_x);
1199 *dmv_y = get_bits(gb, v->k_y);
1202 *pred_flag = *dmv_y & 1;
1203 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1205 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1210 av_assert0(index < esc);
1212 offs_tab = offset_table2;
1214 offs_tab = offset_table1;
1215 index1 = (index + 1) % 9;
1217 val = get_bits(gb, index1 + extend_x);
1218 sign = 0 -(val & 1);
1219 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1223 offs_tab = offset_table2;
1225 offs_tab = offset_table1;
1226 index1 = (index + 1) / 9;
1227 if (index1 > v->numref) {
1228 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1229 sign = 0 - (val & 1);
1230 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1233 if (v->numref && pred_flag)
1234 *pred_flag = index1 & 1;
1238 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1240 int scaledvalue, refdist;
1241 int scalesame1, scalesame2;
1242 int scalezone1_x, zone1offset_x;
1243 int table_index = dir ^ v->second_field;
1245 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1246 refdist = v->refdist;
1248 refdist = dir ? v->brfd : v->frfd;
1251 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1252 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1253 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1254 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1259 if (FFABS(n) < scalezone1_x)
1260 scaledvalue = (n * scalesame1) >> 8;
1263 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1265 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1268 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1271 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1273 int scaledvalue, refdist;
1274 int scalesame1, scalesame2;
1275 int scalezone1_y, zone1offset_y;
1276 int table_index = dir ^ v->second_field;
1278 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1279 refdist = v->refdist;
1281 refdist = dir ? v->brfd : v->frfd;
1284 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1285 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1286 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1287 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1292 if (FFABS(n) < scalezone1_y)
1293 scaledvalue = (n * scalesame1) >> 8;
1296 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1298 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1302 if (v->cur_field_type && !v->ref_field_type[dir])
1303 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1305 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1308 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1310 int scalezone1_x, zone1offset_x;
1311 int scaleopp1, scaleopp2, brfd;
1314 brfd = FFMIN(v->brfd, 3);
1315 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1316 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1317 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1318 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1323 if (FFABS(n) < scalezone1_x)
1324 scaledvalue = (n * scaleopp1) >> 8;
1327 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1329 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1332 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1335 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1337 int scalezone1_y, zone1offset_y;
1338 int scaleopp1, scaleopp2, brfd;
1341 brfd = FFMIN(v->brfd, 3);
1342 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1343 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1344 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1345 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1350 if (FFABS(n) < scalezone1_y)
1351 scaledvalue = (n * scaleopp1) >> 8;
1354 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1356 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1359 if (v->cur_field_type && !v->ref_field_type[dir]) {
1360 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1362 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1366 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1369 int brfd, scalesame;
1370 int hpel = 1 - v->s.quarter_sample;
1373 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1375 n = scaleforsame_y(v, i, n, dir) << hpel;
1377 n = scaleforsame_x(v, n, dir) << hpel;
1380 brfd = FFMIN(v->brfd, 3);
1381 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1383 n = (n * scalesame >> 8) << hpel;
1387 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1390 int refdist, scaleopp;
1391 int hpel = 1 - v->s.quarter_sample;
1394 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1396 n = scaleforopp_y(v, n, dir) << hpel;
1398 n = scaleforopp_x(v, n) << hpel;
1401 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1402 refdist = FFMIN(v->refdist, 3);
1404 refdist = dir ? v->brfd : v->frfd;
1405 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1407 n = (n * scaleopp >> 8) << hpel;
1411 /** Predict and set motion vector
1413 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1414 int mv1, int r_x, int r_y, uint8_t* is_intra,
1415 int pred_flag, int dir)
1417 MpegEncContext *s = &v->s;
1418 int xy, wrap, off = 0;
1422 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1423 int opposite, a_f, b_f, c_f;
1424 int16_t field_predA[2];
1425 int16_t field_predB[2];
1426 int16_t field_predC[2];
1427 int a_valid, b_valid, c_valid;
1428 int hybridmv_thresh, y_bias = 0;
1430 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1431 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1435 /* scale MV difference to be quad-pel */
1436 dmv_x <<= 1 - s->quarter_sample;
1437 dmv_y <<= 1 - s->quarter_sample;
1439 wrap = s->b8_stride;
1440 xy = s->block_index[n];
1443 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1444 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1445 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1446 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1447 if (mv1) { /* duplicate motion data for 1-MV block */
1448 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1449 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1450 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1451 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1452 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1453 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1454 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1455 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1456 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1457 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1458 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1459 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1460 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1465 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1466 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1468 if (v->field_mode && mixedmv_pic)
1469 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1471 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1473 //in 4-MV mode different blocks have different B predictor position
1476 off = (s->mb_x > 0) ? -1 : 1;
1479 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1488 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1490 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1491 b_valid = a_valid && (s->mb_width > 1);
1492 c_valid = s->mb_x || (n == 1 || n == 3);
1493 if (v->field_mode) {
1494 a_valid = a_valid && !is_intra[xy - wrap];
1495 b_valid = b_valid && !is_intra[xy - wrap + off];
1496 c_valid = c_valid && !is_intra[xy - 1];
1500 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1501 num_oppfield += a_f;
1502 num_samefield += 1 - a_f;
1503 field_predA[0] = A[0];
1504 field_predA[1] = A[1];
1506 field_predA[0] = field_predA[1] = 0;
1510 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1511 num_oppfield += b_f;
1512 num_samefield += 1 - b_f;
1513 field_predB[0] = B[0];
1514 field_predB[1] = B[1];
1516 field_predB[0] = field_predB[1] = 0;
1520 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1521 num_oppfield += c_f;
1522 num_samefield += 1 - c_f;
1523 field_predC[0] = C[0];
1524 field_predC[1] = C[1];
1526 field_predC[0] = field_predC[1] = 0;
1530 if (v->field_mode) {
1532 // REFFIELD determines if the last field or the second-last field is
1533 // to be used as reference
1534 opposite = 1 - v->reffield;
1536 if (num_samefield <= num_oppfield)
1537 opposite = 1 - pred_flag;
1539 opposite = pred_flag;
1544 if (a_valid && !a_f) {
1545 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1546 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1548 if (b_valid && !b_f) {
1549 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1550 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1552 if (c_valid && !c_f) {
1553 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1554 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1556 v->mv_f[dir][xy + v->blocks_off] = 1;
1557 v->ref_field_type[dir] = !v->cur_field_type;
1559 if (a_valid && a_f) {
1560 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1561 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1563 if (b_valid && b_f) {
1564 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1565 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1567 if (c_valid && c_f) {
1568 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1569 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1571 v->mv_f[dir][xy + v->blocks_off] = 0;
1572 v->ref_field_type[dir] = v->cur_field_type;
1576 px = field_predA[0];
1577 py = field_predA[1];
1578 } else if (c_valid) {
1579 px = field_predC[0];
1580 py = field_predC[1];
1581 } else if (b_valid) {
1582 px = field_predB[0];
1583 py = field_predB[1];
1589 if (num_samefield + num_oppfield > 1) {
1590 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1591 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1594 /* Pullback MV as specified in 8.3.5.3.4 */
1595 if (!v->field_mode) {
1597 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1598 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1599 X = (s->mb_width << 6) - 4;
1600 Y = (s->mb_height << 6) - 4;
1602 if (qx + px < -60) px = -60 - qx;
1603 if (qy + py < -60) py = -60 - qy;
1605 if (qx + px < -28) px = -28 - qx;
1606 if (qy + py < -28) py = -28 - qy;
1608 if (qx + px > X) px = X - qx;
1609 if (qy + py > Y) py = Y - qy;
1612 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1613 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1614 hybridmv_thresh = 32;
1615 if (a_valid && c_valid) {
1616 if (is_intra[xy - wrap])
1617 sum = FFABS(px) + FFABS(py);
1619 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1620 if (sum > hybridmv_thresh) {
1621 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1622 px = field_predA[0];
1623 py = field_predA[1];
1625 px = field_predC[0];
1626 py = field_predC[1];
1629 if (is_intra[xy - 1])
1630 sum = FFABS(px) + FFABS(py);
1632 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1633 if (sum > hybridmv_thresh) {
1634 if (get_bits1(&s->gb)) {
1635 px = field_predA[0];
1636 py = field_predA[1];
1638 px = field_predC[0];
1639 py = field_predC[1];
1646 if (v->field_mode && v->numref)
1648 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1650 /* store MV using signed modulus of MV range defined in 4.11 */
1651 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;
1652 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;
1653 if (mv1) { /* duplicate motion data for 1-MV block */
1654 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1655 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1656 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1657 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1658 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1659 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1660 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1661 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];
1665 /** Predict and set motion vector for interlaced frame picture MBs
1667 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1668 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1670 MpegEncContext *s = &v->s;
1671 int xy, wrap, off = 0;
1672 int A[2], B[2], C[2];
1674 int a_valid = 0, b_valid = 0, c_valid = 0;
1675 int field_a, field_b, field_c; // 0: same, 1: opposit
1676 int total_valid, num_samefield, num_oppfield;
1677 int pos_c, pos_b, n_adj;
1679 wrap = s->b8_stride;
1680 xy = s->block_index[n];
1683 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1684 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1685 s->current_picture.motion_val[1][xy][0] = 0;
1686 s->current_picture.motion_val[1][xy][1] = 0;
1687 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1688 s->current_picture.motion_val[0][xy + 1][0] = 0;
1689 s->current_picture.motion_val[0][xy + 1][1] = 0;
1690 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1691 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1692 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1693 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1694 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1695 s->current_picture.motion_val[1][xy + 1][0] = 0;
1696 s->current_picture.motion_val[1][xy + 1][1] = 0;
1697 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1698 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1699 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1700 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1705 off = ((n == 0) || (n == 1)) ? 1 : -1;
1707 if (s->mb_x || (n == 1) || (n == 3)) {
1708 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1709 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1710 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1711 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1713 } else { // current block has frame mv and cand. has field MV (so average)
1714 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1715 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1716 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1717 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1720 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1726 /* Predict B and C */
1727 B[0] = B[1] = C[0] = C[1] = 0;
1728 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1729 if (!s->first_slice_line) {
1730 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1733 pos_b = s->block_index[n_adj] - 2 * wrap;
1734 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1735 n_adj = (n & 2) | (n & 1);
1737 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1738 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1739 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1740 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1741 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1744 if (s->mb_width > 1) {
1745 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1748 pos_c = s->block_index[2] - 2 * wrap + 2;
1749 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1752 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1753 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1754 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1755 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1756 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1758 if (s->mb_x == s->mb_width - 1) {
1759 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1762 pos_c = s->block_index[3] - 2 * wrap - 2;
1763 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1766 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1767 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1768 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1769 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1770 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1779 pos_b = s->block_index[1];
1781 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1782 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1783 pos_c = s->block_index[0];
1785 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1786 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1789 total_valid = a_valid + b_valid + c_valid;
1790 // check if predictor A is out of bounds
1791 if (!s->mb_x && !(n == 1 || n == 3)) {
1794 // check if predictor B is out of bounds
1795 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1796 B[0] = B[1] = C[0] = C[1] = 0;
1798 if (!v->blk_mv_type[xy]) {
1799 if (s->mb_width == 1) {
1803 if (total_valid >= 2) {
1804 px = mid_pred(A[0], B[0], C[0]);
1805 py = mid_pred(A[1], B[1], C[1]);
1806 } else if (total_valid) {
1807 if (a_valid) { px = A[0]; py = A[1]; }
1808 else if (b_valid) { px = B[0]; py = B[1]; }
1809 else if (c_valid) { px = C[0]; py = C[1]; }
1815 field_a = (A[1] & 4) ? 1 : 0;
1819 field_b = (B[1] & 4) ? 1 : 0;
1823 field_c = (C[1] & 4) ? 1 : 0;
1827 num_oppfield = field_a + field_b + field_c;
1828 num_samefield = total_valid - num_oppfield;
1829 if (total_valid == 3) {
1830 if ((num_samefield == 3) || (num_oppfield == 3)) {
1831 px = mid_pred(A[0], B[0], C[0]);
1832 py = mid_pred(A[1], B[1], C[1]);
1833 } else if (num_samefield >= num_oppfield) {
1834 /* take one MV from same field set depending on priority
1835 the check for B may not be necessary */
1836 px = !field_a ? A[0] : B[0];
1837 py = !field_a ? A[1] : B[1];
1839 px = field_a ? A[0] : B[0];
1840 py = field_a ? A[1] : B[1];
1842 } else if (total_valid == 2) {
1843 if (num_samefield >= num_oppfield) {
1844 if (!field_a && a_valid) {
1847 } else if (!field_b && b_valid) {
1850 } else /*if (c_valid)*/ {
1851 av_assert1(c_valid);
1854 } /*else px = py = 0;*/
1856 if (field_a && a_valid) {
1859 } else /*if (field_b && b_valid)*/ {
1860 av_assert1(field_b && b_valid);
1863 } /*else if (c_valid) {
1868 } else if (total_valid == 1) {
1869 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1870 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1874 /* store MV using signed modulus of MV range defined in 4.11 */
1875 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1876 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1877 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1878 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1879 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1880 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1881 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1882 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1883 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1884 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1885 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1886 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1887 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1888 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1892 /** Motion compensation for direct or interpolated blocks in B-frames
1894 static void vc1_interp_mc(VC1Context *v)
1896 MpegEncContext *s = &v->s;
1897 H264ChromaContext *h264chroma = &v->h264chroma;
1898 uint8_t *srcY, *srcU, *srcV;
1899 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1901 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1902 int use_ic = v->next_use_ic;
1904 if (!v->field_mode && !v->s.next_picture.f.data[0])
1907 mx = s->mv[1][0][0];
1908 my = s->mv[1][0][1];
1909 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1910 uvmy = (my + ((my & 3) == 3)) >> 1;
1911 if (v->field_mode) {
1912 if (v->cur_field_type != v->ref_field_type[1])
1913 my = my - 2 + 4 * v->cur_field_type;
1914 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1917 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1918 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1920 srcY = s->next_picture.f.data[0];
1921 srcU = s->next_picture.f.data[1];
1922 srcV = s->next_picture.f.data[2];
1924 src_x = s->mb_x * 16 + (mx >> 2);
1925 src_y = s->mb_y * 16 + (my >> 2);
1926 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1927 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1929 if (v->profile != PROFILE_ADVANCED) {
1930 src_x = av_clip( src_x, -16, s->mb_width * 16);
1931 src_y = av_clip( src_y, -16, s->mb_height * 16);
1932 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1933 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1935 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1936 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1937 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1938 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1941 srcY += src_y * s->linesize + src_x;
1942 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1943 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1945 if (v->field_mode && v->ref_field_type[1]) {
1946 srcY += s->current_picture_ptr->f.linesize[0];
1947 srcU += s->current_picture_ptr->f.linesize[1];
1948 srcV += s->current_picture_ptr->f.linesize[2];
1951 /* for grayscale we should not try to read from unknown area */
1952 if (s->flags & CODEC_FLAG_GRAY) {
1953 srcU = s->edge_emu_buffer + 18 * s->linesize;
1954 srcV = s->edge_emu_buffer + 18 * s->linesize;
1957 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1958 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1959 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1960 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1962 srcY -= s->mspel * (1 + s->linesize);
1963 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1964 17 + s->mspel * 2, 17 + s->mspel * 2,
1965 src_x - s->mspel, src_y - s->mspel,
1966 s->h_edge_pos, v_edge_pos);
1967 srcY = s->edge_emu_buffer;
1968 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1969 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1970 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1971 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1974 /* if we deal with range reduction we need to scale source blocks */
1975 if (v->rangeredfrm) {
1977 uint8_t *src, *src2;
1980 for (j = 0; j < 17 + s->mspel * 2; j++) {
1981 for (i = 0; i < 17 + s->mspel * 2; i++)
1982 src[i] = ((src[i] - 128) >> 1) + 128;
1987 for (j = 0; j < 9; j++) {
1988 for (i = 0; i < 9; i++) {
1989 src[i] = ((src[i] - 128) >> 1) + 128;
1990 src2[i] = ((src2[i] - 128) >> 1) + 128;
1992 src += s->uvlinesize;
1993 src2 += s->uvlinesize;
1998 uint8_t (*luty )[256] = v->next_luty;
1999 uint8_t (*lutuv)[256] = v->next_lutuv;
2001 uint8_t *src, *src2;
2004 for (j = 0; j < 17 + s->mspel * 2; j++) {
2005 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2006 for (i = 0; i < 17 + s->mspel * 2; i++)
2007 src[i] = luty[f][src[i]];
2012 for (j = 0; j < 9; j++) {
2013 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2014 for (i = 0; i < 9; i++) {
2015 src[i] = lutuv[f][src[i]];
2016 src2[i] = lutuv[f][src2[i]];
2018 src += s->uvlinesize;
2019 src2 += s->uvlinesize;
2022 srcY += s->mspel * (1 + s->linesize);
2029 dxy = ((my & 3) << 2) | (mx & 3);
2030 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2031 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2032 srcY += s->linesize * 8;
2033 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2034 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2036 dxy = (my & 2) | ((mx & 2) >> 1);
2039 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2041 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2044 if (s->flags & CODEC_FLAG_GRAY) return;
2045 /* Chroma MC always uses qpel blilinear */
2046 uvmx = (uvmx & 3) << 1;
2047 uvmy = (uvmy & 3) << 1;
2049 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2050 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2052 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2053 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2057 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2061 #if B_FRACTION_DEN==256
2065 return 2 * ((value * n + 255) >> 9);
2066 return (value * n + 128) >> 8;
2069 n -= B_FRACTION_DEN;
2071 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2072 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2076 /** Reconstruct motion vector for B-frame and do motion compensation
2078 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2079 int direct, int mode)
2086 if (mode == BMV_TYPE_INTERPOLATED) {
2092 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2095 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2096 int direct, int mvtype)
2098 MpegEncContext *s = &v->s;
2099 int xy, wrap, off = 0;
2104 const uint8_t *is_intra = v->mb_type[0];
2108 /* scale MV difference to be quad-pel */
2109 dmv_x[0] <<= 1 - s->quarter_sample;
2110 dmv_y[0] <<= 1 - s->quarter_sample;
2111 dmv_x[1] <<= 1 - s->quarter_sample;
2112 dmv_y[1] <<= 1 - s->quarter_sample;
2114 wrap = s->b8_stride;
2115 xy = s->block_index[0];
2118 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2119 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2120 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2121 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2124 if (!v->field_mode) {
2125 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2126 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2127 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2128 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2130 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2131 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));
2132 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));
2133 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));
2134 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));
2137 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2138 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2139 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2140 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2144 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2145 C = s->current_picture.motion_val[0][xy - 2];
2146 A = s->current_picture.motion_val[0][xy - wrap * 2];
2147 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2148 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2150 if (!s->mb_x) C[0] = C[1] = 0;
2151 if (!s->first_slice_line) { // predictor A is not out of bounds
2152 if (s->mb_width == 1) {
2156 px = mid_pred(A[0], B[0], C[0]);
2157 py = mid_pred(A[1], B[1], C[1]);
2159 } else if (s->mb_x) { // predictor C is not out of bounds
2165 /* Pullback MV as specified in 8.3.5.3.4 */
2168 if (v->profile < PROFILE_ADVANCED) {
2169 qx = (s->mb_x << 5);
2170 qy = (s->mb_y << 5);
2171 X = (s->mb_width << 5) - 4;
2172 Y = (s->mb_height << 5) - 4;
2173 if (qx + px < -28) px = -28 - qx;
2174 if (qy + py < -28) py = -28 - qy;
2175 if (qx + px > X) px = X - qx;
2176 if (qy + py > Y) py = Y - qy;
2178 qx = (s->mb_x << 6);
2179 qy = (s->mb_y << 6);
2180 X = (s->mb_width << 6) - 4;
2181 Y = (s->mb_height << 6) - 4;
2182 if (qx + px < -60) px = -60 - qx;
2183 if (qy + py < -60) py = -60 - qy;
2184 if (qx + px > X) px = X - qx;
2185 if (qy + py > Y) py = Y - qy;
2188 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2189 if (0 && !s->first_slice_line && s->mb_x) {
2190 if (is_intra[xy - wrap])
2191 sum = FFABS(px) + FFABS(py);
2193 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2195 if (get_bits1(&s->gb)) {
2203 if (is_intra[xy - 2])
2204 sum = FFABS(px) + FFABS(py);
2206 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2208 if (get_bits1(&s->gb)) {
2218 /* store MV using signed modulus of MV range defined in 4.11 */
2219 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2220 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2222 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2223 C = s->current_picture.motion_val[1][xy - 2];
2224 A = s->current_picture.motion_val[1][xy - wrap * 2];
2225 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2226 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2230 if (!s->first_slice_line) { // predictor A is not out of bounds
2231 if (s->mb_width == 1) {
2235 px = mid_pred(A[0], B[0], C[0]);
2236 py = mid_pred(A[1], B[1], C[1]);
2238 } else if (s->mb_x) { // predictor C is not out of bounds
2244 /* Pullback MV as specified in 8.3.5.3.4 */
2247 if (v->profile < PROFILE_ADVANCED) {
2248 qx = (s->mb_x << 5);
2249 qy = (s->mb_y << 5);
2250 X = (s->mb_width << 5) - 4;
2251 Y = (s->mb_height << 5) - 4;
2252 if (qx + px < -28) px = -28 - qx;
2253 if (qy + py < -28) py = -28 - qy;
2254 if (qx + px > X) px = X - qx;
2255 if (qy + py > Y) py = Y - qy;
2257 qx = (s->mb_x << 6);
2258 qy = (s->mb_y << 6);
2259 X = (s->mb_width << 6) - 4;
2260 Y = (s->mb_height << 6) - 4;
2261 if (qx + px < -60) px = -60 - qx;
2262 if (qy + py < -60) py = -60 - qy;
2263 if (qx + px > X) px = X - qx;
2264 if (qy + py > Y) py = Y - qy;
2267 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2268 if (0 && !s->first_slice_line && s->mb_x) {
2269 if (is_intra[xy - wrap])
2270 sum = FFABS(px) + FFABS(py);
2272 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2274 if (get_bits1(&s->gb)) {
2282 if (is_intra[xy - 2])
2283 sum = FFABS(px) + FFABS(py);
2285 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2287 if (get_bits1(&s->gb)) {
2297 /* store MV using signed modulus of MV range defined in 4.11 */
2299 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2300 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2302 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2303 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2304 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2305 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2308 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2310 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2311 MpegEncContext *s = &v->s;
2312 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2314 if (v->bmvtype == BMV_TYPE_DIRECT) {
2315 int total_opp, k, f;
2316 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2317 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2318 v->bfraction, 0, s->quarter_sample);
2319 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2320 v->bfraction, 0, s->quarter_sample);
2321 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2322 v->bfraction, 1, s->quarter_sample);
2323 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2324 v->bfraction, 1, s->quarter_sample);
2326 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2327 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2328 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2329 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2330 f = (total_opp > 2) ? 1 : 0;
2332 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2333 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2336 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2337 for (k = 0; k < 4; k++) {
2338 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2339 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2340 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2341 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2342 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2343 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2347 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2348 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);
2349 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);
2352 if (dir) { // backward
2353 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);
2354 if (n == 3 || mv1) {
2355 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2358 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);
2359 if (n == 3 || mv1) {
2360 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2365 /** Get predicted DC value for I-frames only
2366 * prediction dir: left=0, top=1
2367 * @param s MpegEncContext
2368 * @param overlap flag indicating that overlap filtering is used
2369 * @param pq integer part of picture quantizer
2370 * @param[in] n block index in the current MB
2371 * @param dc_val_ptr Pointer to DC predictor
2372 * @param dir_ptr Prediction direction for use in AC prediction
2374 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2375 int16_t **dc_val_ptr, int *dir_ptr)
2377 int a, b, c, wrap, pred, scale;
2379 static const uint16_t dcpred[32] = {
2380 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2381 114, 102, 93, 85, 79, 73, 68, 64,
2382 60, 57, 54, 51, 49, 47, 45, 43,
2383 41, 39, 38, 37, 35, 34, 33
2386 /* find prediction - wmv3_dc_scale always used here in fact */
2387 if (n < 4) scale = s->y_dc_scale;
2388 else scale = s->c_dc_scale;
2390 wrap = s->block_wrap[n];
2391 dc_val = s->dc_val[0] + s->block_index[n];
2397 b = dc_val[ - 1 - wrap];
2398 a = dc_val[ - wrap];
2400 if (pq < 9 || !overlap) {
2401 /* Set outer values */
2402 if (s->first_slice_line && (n != 2 && n != 3))
2403 b = a = dcpred[scale];
2404 if (s->mb_x == 0 && (n != 1 && n != 3))
2405 b = c = dcpred[scale];
2407 /* Set outer values */
2408 if (s->first_slice_line && (n != 2 && n != 3))
2410 if (s->mb_x == 0 && (n != 1 && n != 3))
2414 if (abs(a - b) <= abs(b - c)) {
2416 *dir_ptr = 1; // left
2419 *dir_ptr = 0; // top
2422 /* update predictor */
2423 *dc_val_ptr = &dc_val[0];
2428 /** Get predicted DC value
2429 * prediction dir: left=0, top=1
2430 * @param s MpegEncContext
2431 * @param overlap flag indicating that overlap filtering is used
2432 * @param pq integer part of picture quantizer
2433 * @param[in] n block index in the current MB
2434 * @param a_avail flag indicating top block availability
2435 * @param c_avail flag indicating left block availability
2436 * @param dc_val_ptr Pointer to DC predictor
2437 * @param dir_ptr Prediction direction for use in AC prediction
2439 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2440 int a_avail, int c_avail,
2441 int16_t **dc_val_ptr, int *dir_ptr)
2443 int a, b, c, wrap, pred;
2445 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2449 wrap = s->block_wrap[n];
2450 dc_val = s->dc_val[0] + s->block_index[n];
2456 b = dc_val[ - 1 - wrap];
2457 a = dc_val[ - wrap];
2458 /* scale predictors if needed */
2459 q1 = s->current_picture.qscale_table[mb_pos];
2460 dqscale_index = s->y_dc_scale_table[q1] - 1;
2461 if (dqscale_index < 0)
2463 if (c_avail && (n != 1 && n != 3)) {
2464 q2 = s->current_picture.qscale_table[mb_pos - 1];
2466 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2468 if (a_avail && (n != 2 && n != 3)) {
2469 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2471 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2473 if (a_avail && c_avail && (n != 3)) {
2478 off -= s->mb_stride;
2479 q2 = s->current_picture.qscale_table[off];
2481 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2484 if (a_avail && c_avail) {
2485 if (abs(a - b) <= abs(b - c)) {
2487 *dir_ptr = 1; // left
2490 *dir_ptr = 0; // top
2492 } else if (a_avail) {
2494 *dir_ptr = 0; // top
2495 } else if (c_avail) {
2497 *dir_ptr = 1; // left
2500 *dir_ptr = 1; // left
2503 /* update predictor */
2504 *dc_val_ptr = &dc_val[0];
2508 /** @} */ // Block group
2511 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2512 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2516 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2517 uint8_t **coded_block_ptr)
2519 int xy, wrap, pred, a, b, c;
2521 xy = s->block_index[n];
2522 wrap = s->b8_stride;
2527 a = s->coded_block[xy - 1 ];
2528 b = s->coded_block[xy - 1 - wrap];
2529 c = s->coded_block[xy - wrap];
2538 *coded_block_ptr = &s->coded_block[xy];
2544 * Decode one AC coefficient
2545 * @param v The VC1 context
2546 * @param last Last coefficient
2547 * @param skip How much zero coefficients to skip
2548 * @param value Decoded AC coefficient value
2549 * @param codingset set of VLC to decode data
2552 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2553 int *value, int codingset)
2555 GetBitContext *gb = &v->s.gb;
2556 int index, escape, run = 0, level = 0, lst = 0;
2558 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2559 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2560 run = vc1_index_decode_table[codingset][index][0];
2561 level = vc1_index_decode_table[codingset][index][1];
2562 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2566 escape = decode210(gb);
2568 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2569 run = vc1_index_decode_table[codingset][index][0];
2570 level = vc1_index_decode_table[codingset][index][1];
2571 lst = index >= vc1_last_decode_table[codingset];
2574 level += vc1_last_delta_level_table[codingset][run];
2576 level += vc1_delta_level_table[codingset][run];
2579 run += vc1_last_delta_run_table[codingset][level] + 1;
2581 run += vc1_delta_run_table[codingset][level] + 1;
2587 lst = get_bits1(gb);
2588 if (v->s.esc3_level_length == 0) {
2589 if (v->pq < 8 || v->dquantfrm) { // table 59
2590 v->s.esc3_level_length = get_bits(gb, 3);
2591 if (!v->s.esc3_level_length)
2592 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2593 } else { // table 60
2594 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2596 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2598 run = get_bits(gb, v->s.esc3_run_length);
2599 sign = get_bits1(gb);
2600 level = get_bits(gb, v->s.esc3_level_length);
2611 /** Decode intra block in intra frames - should be faster than decode_intra_block
2612 * @param v VC1Context
2613 * @param block block to decode
2614 * @param[in] n subblock index
2615 * @param coded are AC coeffs present or not
2616 * @param codingset set of VLC to decode data
2618 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2619 int coded, int codingset)
2621 GetBitContext *gb = &v->s.gb;
2622 MpegEncContext *s = &v->s;
2623 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2626 int16_t *ac_val, *ac_val2;
2629 /* Get DC differential */
2631 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2633 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2636 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2640 if (dcdiff == 119 /* ESC index value */) {
2641 /* TODO: Optimize */
2642 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2643 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2644 else dcdiff = get_bits(gb, 8);
2647 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2648 else if (v->pq == 2)
2649 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2656 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2659 /* Store the quantized DC coeff, used for prediction */
2661 block[0] = dcdiff * s->y_dc_scale;
2663 block[0] = dcdiff * s->c_dc_scale;
2674 int last = 0, skip, value;
2675 const uint8_t *zz_table;
2679 scale = v->pq * 2 + v->halfpq;
2683 zz_table = v->zz_8x8[2];
2685 zz_table = v->zz_8x8[3];
2687 zz_table = v->zz_8x8[1];
2689 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2691 if (dc_pred_dir) // left
2694 ac_val -= 16 * s->block_wrap[n];
2697 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2701 block[zz_table[i++]] = value;
2704 /* apply AC prediction if needed */
2706 if (dc_pred_dir) { // left
2707 for (k = 1; k < 8; k++)
2708 block[k << v->left_blk_sh] += ac_val[k];
2710 for (k = 1; k < 8; k++)
2711 block[k << v->top_blk_sh] += ac_val[k + 8];
2714 /* save AC coeffs for further prediction */
2715 for (k = 1; k < 8; k++) {
2716 ac_val2[k] = block[k << v->left_blk_sh];
2717 ac_val2[k + 8] = block[k << v->top_blk_sh];
2720 /* scale AC coeffs */
2721 for (k = 1; k < 64; k++)
2725 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2728 if (s->ac_pred) i = 63;
2734 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2738 scale = v->pq * 2 + v->halfpq;
2739 memset(ac_val2, 0, 16 * 2);
2740 if (dc_pred_dir) { // left
2743 memcpy(ac_val2, ac_val, 8 * 2);
2745 ac_val -= 16 * s->block_wrap[n];
2747 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2750 /* apply AC prediction if needed */
2752 if (dc_pred_dir) { //left
2753 for (k = 1; k < 8; k++) {
2754 block[k << v->left_blk_sh] = ac_val[k] * scale;
2755 if (!v->pquantizer && block[k << v->left_blk_sh])
2756 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2759 for (k = 1; k < 8; k++) {
2760 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2761 if (!v->pquantizer && block[k << v->top_blk_sh])
2762 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2768 s->block_last_index[n] = i;
2773 /** Decode intra block in intra frames - should be faster than decode_intra_block
2774 * @param v VC1Context
2775 * @param block block to decode
2776 * @param[in] n subblock number
2777 * @param coded are AC coeffs present or not
2778 * @param codingset set of VLC to decode data
2779 * @param mquant quantizer value for this macroblock
2781 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2782 int coded, int codingset, int mquant)
2784 GetBitContext *gb = &v->s.gb;
2785 MpegEncContext *s = &v->s;
2786 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2788 int16_t *dc_val = NULL;
2789 int16_t *ac_val, *ac_val2;
2791 int a_avail = v->a_avail, c_avail = v->c_avail;
2792 int use_pred = s->ac_pred;
2795 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2797 /* Get DC differential */
2799 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2801 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2804 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2808 if (dcdiff == 119 /* ESC index value */) {
2809 /* TODO: Optimize */
2810 if (mquant == 1) dcdiff = get_bits(gb, 10);
2811 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2812 else dcdiff = get_bits(gb, 8);
2815 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2816 else if (mquant == 2)
2817 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2824 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2827 /* Store the quantized DC coeff, used for prediction */
2829 block[0] = dcdiff * s->y_dc_scale;
2831 block[0] = dcdiff * s->c_dc_scale;
2837 /* check if AC is needed at all */
2838 if (!a_avail && !c_avail)
2840 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2843 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2845 if (dc_pred_dir) // left
2848 ac_val -= 16 * s->block_wrap[n];
2850 q1 = s->current_picture.qscale_table[mb_pos];
2851 if ( dc_pred_dir && c_avail && mb_pos)
2852 q2 = s->current_picture.qscale_table[mb_pos - 1];
2853 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2854 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2855 if ( dc_pred_dir && n == 1)
2857 if (!dc_pred_dir && n == 2)
2863 int last = 0, skip, value;
2864 const uint8_t *zz_table;
2868 if (!use_pred && v->fcm == ILACE_FRAME) {
2869 zz_table = v->zzi_8x8;
2871 if (!dc_pred_dir) // top
2872 zz_table = v->zz_8x8[2];
2874 zz_table = v->zz_8x8[3];
2877 if (v->fcm != ILACE_FRAME)
2878 zz_table = v->zz_8x8[1];
2880 zz_table = v->zzi_8x8;
2884 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2888 block[zz_table[i++]] = value;
2891 /* apply AC prediction if needed */
2893 /* scale predictors if needed*/
2894 if (q2 && q1 != q2) {
2895 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2896 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2899 return AVERROR_INVALIDDATA;
2900 if (dc_pred_dir) { // left
2901 for (k = 1; k < 8; k++)
2902 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2904 for (k = 1; k < 8; k++)
2905 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2908 if (dc_pred_dir) { //left
2909 for (k = 1; k < 8; k++)
2910 block[k << v->left_blk_sh] += ac_val[k];
2912 for (k = 1; k < 8; k++)
2913 block[k << v->top_blk_sh] += ac_val[k + 8];
2917 /* save AC coeffs for further prediction */
2918 for (k = 1; k < 8; k++) {
2919 ac_val2[k ] = block[k << v->left_blk_sh];
2920 ac_val2[k + 8] = block[k << v->top_blk_sh];
2923 /* scale AC coeffs */
2924 for (k = 1; k < 64; k++)
2928 block[k] += (block[k] < 0) ? -mquant : mquant;
2931 if (use_pred) i = 63;
2932 } else { // no AC coeffs
2935 memset(ac_val2, 0, 16 * 2);
2936 if (dc_pred_dir) { // left
2938 memcpy(ac_val2, ac_val, 8 * 2);
2939 if (q2 && q1 != q2) {
2940 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2941 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2943 return AVERROR_INVALIDDATA;
2944 for (k = 1; k < 8; k++)
2945 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2950 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2951 if (q2 && q1 != q2) {
2952 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2953 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2955 return AVERROR_INVALIDDATA;
2956 for (k = 1; k < 8; k++)
2957 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2962 /* apply AC prediction if needed */
2964 if (dc_pred_dir) { // left
2965 for (k = 1; k < 8; k++) {
2966 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2967 if (!v->pquantizer && block[k << v->left_blk_sh])
2968 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2971 for (k = 1; k < 8; k++) {
2972 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2973 if (!v->pquantizer && block[k << v->top_blk_sh])
2974 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2980 s->block_last_index[n] = i;
2985 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2986 * @param v VC1Context
2987 * @param block block to decode
2988 * @param[in] n subblock index
2989 * @param coded are AC coeffs present or not
2990 * @param mquant block quantizer
2991 * @param codingset set of VLC to decode data
2993 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2994 int coded, int mquant, int codingset)
2996 GetBitContext *gb = &v->s.gb;
2997 MpegEncContext *s = &v->s;
2998 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3000 int16_t *dc_val = NULL;
3001 int16_t *ac_val, *ac_val2;
3003 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3004 int a_avail = v->a_avail, c_avail = v->c_avail;
3005 int use_pred = s->ac_pred;
3009 s->dsp.clear_block(block);
3011 /* XXX: Guard against dumb values of mquant */
3012 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3014 /* Set DC scale - y and c use the same */
3015 s->y_dc_scale = s->y_dc_scale_table[mquant];
3016 s->c_dc_scale = s->c_dc_scale_table[mquant];
3018 /* Get DC differential */
3020 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3022 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3025 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3029 if (dcdiff == 119 /* ESC index value */) {
3030 /* TODO: Optimize */
3031 if (mquant == 1) dcdiff = get_bits(gb, 10);
3032 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3033 else dcdiff = get_bits(gb, 8);
3036 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3037 else if (mquant == 2)
3038 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3045 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3048 /* Store the quantized DC coeff, used for prediction */
3051 block[0] = dcdiff * s->y_dc_scale;
3053 block[0] = dcdiff * s->c_dc_scale;
3059 /* check if AC is needed at all and adjust direction if needed */
3060 if (!a_avail) dc_pred_dir = 1;
3061 if (!c_avail) dc_pred_dir = 0;
3062 if (!a_avail && !c_avail) use_pred = 0;
3063 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3066 scale = mquant * 2 + v->halfpq;
3068 if (dc_pred_dir) //left
3071 ac_val -= 16 * s->block_wrap[n];
3073 q1 = s->current_picture.qscale_table[mb_pos];
3074 if (dc_pred_dir && c_avail && mb_pos)
3075 q2 = s->current_picture.qscale_table[mb_pos - 1];
3076 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3077 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3078 if ( dc_pred_dir && n == 1)
3080 if (!dc_pred_dir && n == 2)
3082 if (n == 3) q2 = q1;
3085 int last = 0, skip, value;
3089 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3093 if (v->fcm == PROGRESSIVE)
3094 block[v->zz_8x8[0][i++]] = value;
3096 if (use_pred && (v->fcm == ILACE_FRAME)) {
3097 if (!dc_pred_dir) // top
3098 block[v->zz_8x8[2][i++]] = value;
3100 block[v->zz_8x8[3][i++]] = value;
3102 block[v->zzi_8x8[i++]] = value;
3107 /* apply AC prediction if needed */
3109 /* scale predictors if needed*/
3110 if (q2 && q1 != q2) {
3111 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3112 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3115 return AVERROR_INVALIDDATA;
3116 if (dc_pred_dir) { // left
3117 for (k = 1; k < 8; k++)
3118 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3120 for (k = 1; k < 8; k++)
3121 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3124 if (dc_pred_dir) { // left
3125 for (k = 1; k < 8; k++)
3126 block[k << v->left_blk_sh] += ac_val[k];
3128 for (k = 1; k < 8; k++)
3129 block[k << v->top_blk_sh] += ac_val[k + 8];
3133 /* save AC coeffs for further prediction */
3134 for (k = 1; k < 8; k++) {
3135 ac_val2[k ] = block[k << v->left_blk_sh];
3136 ac_val2[k + 8] = block[k << v->top_blk_sh];
3139 /* scale AC coeffs */
3140 for (k = 1; k < 64; k++)
3144 block[k] += (block[k] < 0) ? -mquant : mquant;
3147 if (use_pred) i = 63;
3148 } else { // no AC coeffs
3151 memset(ac_val2, 0, 16 * 2);
3152 if (dc_pred_dir) { // left
3154 memcpy(ac_val2, ac_val, 8 * 2);
3155 if (q2 && q1 != q2) {
3156 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3157 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3159 return AVERROR_INVALIDDATA;
3160 for (k = 1; k < 8; k++)
3161 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3166 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3167 if (q2 && q1 != q2) {
3168 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3169 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3171 return AVERROR_INVALIDDATA;
3172 for (k = 1; k < 8; k++)
3173 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3178 /* apply AC prediction if needed */
3180 if (dc_pred_dir) { // left
3181 for (k = 1; k < 8; k++) {
3182 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3183 if (!v->pquantizer && block[k << v->left_blk_sh])
3184 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3187 for (k = 1; k < 8; k++) {
3188 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3189 if (!v->pquantizer && block[k << v->top_blk_sh])
3190 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3196 s->block_last_index[n] = i;
3203 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3204 int mquant, int ttmb, int first_block,
3205 uint8_t *dst, int linesize, int skip_block,
3208 MpegEncContext *s = &v->s;
3209 GetBitContext *gb = &s->gb;
3212 int scale, off, idx, last, skip, value;
3213 int ttblk = ttmb & 7;
3216 s->dsp.clear_block(block);
3219 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)];
3221 if (ttblk == TT_4X4) {
3222 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3224 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3225 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3226 || (!v->res_rtm_flag && !first_block))) {
3227 subblkpat = decode012(gb);
3229 subblkpat ^= 3; // swap decoded pattern bits
3230 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3232 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3235 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3237 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3238 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3239 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3242 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3243 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3252 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3257 idx = v->zz_8x8[0][i++];
3259 idx = v->zzi_8x8[i++];
3260 block[idx] = value * scale;
3262 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3266 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3268 v->vc1dsp.vc1_inv_trans_8x8(block);
3269 s->dsp.add_pixels_clamped(block, dst, linesize);
3274 pat = ~subblkpat & 0xF;
3275 for (j = 0; j < 4; j++) {
3276 last = subblkpat & (1 << (3 - j));
3278 off = (j & 1) * 4 + (j & 2) * 16;
3280 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3285 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3287 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3288 block[idx + off] = value * scale;
3290 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3292 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3294 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3296 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3301 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3302 for (j = 0; j < 2; j++) {
3303 last = subblkpat & (1 << (1 - j));
3307 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3312 idx = v->zz_8x4[i++] + off;
3314 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3315 block[idx] = value * scale;
3317 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3319 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3321 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3323 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3328 pat = ~(subblkpat * 5) & 0xF;
3329 for (j = 0; j < 2; j++) {
3330 last = subblkpat & (1 << (1 - j));
3334 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3339 idx = v->zz_4x8[i++] + off;
3341 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3342 block[idx] = value * scale;
3344 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3346 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3348 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3350 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3356 *ttmb_out |= ttblk << (n * 4);
3360 /** @} */ // Macroblock group
3362 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3363 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3365 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3367 MpegEncContext *s = &v->s;
3368 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3369 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3370 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3371 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3372 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3375 if (block_num > 3) {
3376 dst = s->dest[block_num - 3];
3378 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3380 if (s->mb_y != s->end_mb_y || block_num < 2) {
3384 if (block_num > 3) {
3385 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3386 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3387 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3388 mv_stride = s->mb_stride;
3390 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3391 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3392 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3393 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3394 mv_stride = s->b8_stride;
3395 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3398 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3399 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3400 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3402 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3404 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3407 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3409 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3414 dst -= 4 * linesize;
3415 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3416 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3417 idx = (block_cbp | (block_cbp >> 2)) & 3;
3419 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3422 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3424 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3429 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3431 MpegEncContext *s = &v->s;
3432 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3433 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3434 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3435 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3436 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3439 if (block_num > 3) {
3440 dst = s->dest[block_num - 3] - 8 * linesize;
3442 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3445 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3448 if (block_num > 3) {
3449 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3450 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3451 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3453 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3454 : (mb_cbp >> ((block_num + 1) * 4));
3455 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3456 : (mb_is_intra >> ((block_num + 1) * 4));
3457 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3459 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3460 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3462 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3464 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3467 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3469 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3475 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3476 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3477 idx = (block_cbp | (block_cbp >> 1)) & 5;
3479 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3482 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3484 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3489 static void vc1_apply_p_loop_filter(VC1Context *v)
3491 MpegEncContext *s = &v->s;
3494 for (i = 0; i < 6; i++) {
3495 vc1_apply_p_v_loop_filter(v, i);
3498 /* V always precedes H, therefore we run H one MB before V;
3499 * at the end of a row, we catch up to complete the row */
3501 for (i = 0; i < 6; i++) {
3502 vc1_apply_p_h_loop_filter(v, i);
3504 if (s->mb_x == s->mb_width - 1) {
3506 ff_update_block_index(s);
3507 for (i = 0; i < 6; i++) {
3508 vc1_apply_p_h_loop_filter(v, i);
3514 /** Decode one P-frame MB
3516 static int vc1_decode_p_mb(VC1Context *v)
3518 MpegEncContext *s = &v->s;
3519 GetBitContext *gb = &s->gb;
3521 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3522 int cbp; /* cbp decoding stuff */
3523 int mqdiff, mquant; /* MB quantization */
3524 int ttmb = v->ttfrm; /* MB Transform type */
3526 int mb_has_coeffs = 1; /* last_flag */
3527 int dmv_x, dmv_y; /* Differential MV components */
3528 int index, index1; /* LUT indexes */
3529 int val, sign; /* temp values */
3530 int first_block = 1;
3532 int skipped, fourmv;
3533 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3535 mquant = v->pq; /* lossy initialization */
3537 if (v->mv_type_is_raw)
3538 fourmv = get_bits1(gb);
3540 fourmv = v->mv_type_mb_plane[mb_pos];
3542 skipped = get_bits1(gb);
3544 skipped = v->s.mbskip_table[mb_pos];
3546 if (!fourmv) { /* 1MV mode */
3548 GET_MVDATA(dmv_x, dmv_y);
3551 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3552 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3554 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3555 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3557 /* FIXME Set DC val for inter block ? */
3558 if (s->mb_intra && !mb_has_coeffs) {
3560 s->ac_pred = get_bits1(gb);
3562 } else if (mb_has_coeffs) {
3564 s->ac_pred = get_bits1(gb);
3565 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3571 s->current_picture.qscale_table[mb_pos] = mquant;
3573 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3574 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3575 VC1_TTMB_VLC_BITS, 2);
3576 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3578 for (i = 0; i < 6; i++) {
3579 s->dc_val[0][s->block_index[i]] = 0;
3581 val = ((cbp >> (5 - i)) & 1);
3582 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3583 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3585 /* check if prediction blocks A and C are available */
3586 v->a_avail = v->c_avail = 0;
3587 if (i == 2 || i == 3 || !s->first_slice_line)
3588 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3589 if (i == 1 || i == 3 || s->mb_x)
3590 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3592 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3593 (i & 4) ? v->codingset2 : v->codingset);
3594 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3596 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3598 for (j = 0; j < 64; j++)
3599 s->block[i][j] <<= 1;
3600 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3601 if (v->pq >= 9 && v->overlap) {
3603 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3605 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3607 block_cbp |= 0xF << (i << 2);
3608 block_intra |= 1 << i;
3610 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3611 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3612 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3613 block_cbp |= pat << (i << 2);
3614 if (!v->ttmbf && ttmb < 8)
3621 for (i = 0; i < 6; i++) {
3622 v->mb_type[0][s->block_index[i]] = 0;
3623 s->dc_val[0][s->block_index[i]] = 0;
3625 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3626 s->current_picture.qscale_table[mb_pos] = 0;
3627 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3630 } else { // 4MV mode
3631 if (!skipped /* unskipped MB */) {
3632 int intra_count = 0, coded_inter = 0;
3633 int is_intra[6], is_coded[6];
3635 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3636 for (i = 0; i < 6; i++) {
3637 val = ((cbp >> (5 - i)) & 1);
3638 s->dc_val[0][s->block_index[i]] = 0;
3645 GET_MVDATA(dmv_x, dmv_y);
3647 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3649 vc1_mc_4mv_luma(v, i, 0, 0);
3650 intra_count += s->mb_intra;
3651 is_intra[i] = s->mb_intra;
3652 is_coded[i] = mb_has_coeffs;
3655 is_intra[i] = (intra_count >= 3);
3659 vc1_mc_4mv_chroma(v, 0);
3660 v->mb_type[0][s->block_index[i]] = is_intra[i];
3662 coded_inter = !is_intra[i] & is_coded[i];
3664 // if there are no coded blocks then don't do anything more
3666 if (!intra_count && !coded_inter)
3669 s->current_picture.qscale_table[mb_pos] = mquant;
3670 /* test if block is intra and has pred */
3673 for (i = 0; i < 6; i++)
3675 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3676 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3682 s->ac_pred = get_bits1(gb);
3686 if (!v->ttmbf && coded_inter)
3687 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3688 for (i = 0; i < 6; i++) {
3690 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3691 s->mb_intra = is_intra[i];
3693 /* check if prediction blocks A and C are available */
3694 v->a_avail = v->c_avail = 0;
3695 if (i == 2 || i == 3 || !s->first_slice_line)
3696 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3697 if (i == 1 || i == 3 || s->mb_x)
3698 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3700 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3701 (i & 4) ? v->codingset2 : v->codingset);
3702 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3704 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3706 for (j = 0; j < 64; j++)
3707 s->block[i][j] <<= 1;
3708 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3709 (i & 4) ? s->uvlinesize : s->linesize);
3710 if (v->pq >= 9 && v->overlap) {
3712 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3714 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3716 block_cbp |= 0xF << (i << 2);
3717 block_intra |= 1 << i;
3718 } else if (is_coded[i]) {
3719 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3720 first_block, s->dest[dst_idx] + off,
3721 (i & 4) ? s->uvlinesize : s->linesize,
3722 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3724 block_cbp |= pat << (i << 2);
3725 if (!v->ttmbf && ttmb < 8)
3730 } else { // skipped MB
3732 s->current_picture.qscale_table[mb_pos] = 0;
3733 for (i = 0; i < 6; i++) {
3734 v->mb_type[0][s->block_index[i]] = 0;
3735 s->dc_val[0][s->block_index[i]] = 0;
3737 for (i = 0; i < 4; i++) {
3738 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3739 vc1_mc_4mv_luma(v, i, 0, 0);
3741 vc1_mc_4mv_chroma(v, 0);
3742 s->current_picture.qscale_table[mb_pos] = 0;
3746 v->cbp[s->mb_x] = block_cbp;
3747 v->ttblk[s->mb_x] = block_tt;
3748 v->is_intra[s->mb_x] = block_intra;
3753 /* Decode one macroblock in an interlaced frame p picture */
3755 static int vc1_decode_p_mb_intfr(VC1Context *v)
3757 MpegEncContext *s = &v->s;
3758 GetBitContext *gb = &s->gb;
3760 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3761 int cbp = 0; /* cbp decoding stuff */
3762 int mqdiff, mquant; /* MB quantization */
3763 int ttmb = v->ttfrm; /* MB Transform type */
3765 int mb_has_coeffs = 1; /* last_flag */
3766 int dmv_x, dmv_y; /* Differential MV components */
3767 int val; /* temp value */
3768 int first_block = 1;
3770 int skipped, fourmv = 0, twomv = 0;
3771 int block_cbp = 0, pat, block_tt = 0;
3772 int idx_mbmode = 0, mvbp;
3773 int stride_y, fieldtx;
3775 mquant = v->pq; /* Lossy initialization */
3778 skipped = get_bits1(gb);
3780 skipped = v->s.mbskip_table[mb_pos];
3782 if (v->fourmvswitch)
3783 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3785 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3786 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3787 /* store the motion vector type in a flag (useful later) */
3788 case MV_PMODE_INTFR_4MV:
3790 v->blk_mv_type[s->block_index[0]] = 0;
3791 v->blk_mv_type[s->block_index[1]] = 0;
3792 v->blk_mv_type[s->block_index[2]] = 0;
3793 v->blk_mv_type[s->block_index[3]] = 0;
3795 case MV_PMODE_INTFR_4MV_FIELD:
3797 v->blk_mv_type[s->block_index[0]] = 1;
3798 v->blk_mv_type[s->block_index[1]] = 1;
3799 v->blk_mv_type[s->block_index[2]] = 1;
3800 v->blk_mv_type[s->block_index[3]] = 1;
3802 case MV_PMODE_INTFR_2MV_FIELD:
3804 v->blk_mv_type[s->block_index[0]] = 1;
3805 v->blk_mv_type[s->block_index[1]] = 1;
3806 v->blk_mv_type[s->block_index[2]] = 1;
3807 v->blk_mv_type[s->block_index[3]] = 1;
3809 case MV_PMODE_INTFR_1MV:
3810 v->blk_mv_type[s->block_index[0]] = 0;
3811 v->blk_mv_type[s->block_index[1]] = 0;
3812 v->blk_mv_type[s->block_index[2]] = 0;
3813 v->blk_mv_type[s->block_index[3]] = 0;
3816 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3817 for (i = 0; i < 4; i++) {
3818 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3819 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3821 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3822 s->mb_intra = v->is_intra[s->mb_x] = 1;
3823 for (i = 0; i < 6; i++)
3824 v->mb_type[0][s->block_index[i]] = 1;
3825 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3826 mb_has_coeffs = get_bits1(gb);
3828 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3829 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3831 s->current_picture.qscale_table[mb_pos] = mquant;
3832 /* Set DC scale - y and c use the same (not sure if necessary here) */
3833 s->y_dc_scale = s->y_dc_scale_table[mquant];
3834 s->c_dc_scale = s->c_dc_scale_table[mquant];
3836 for (i = 0; i < 6; i++) {
3837 s->dc_val[0][s->block_index[i]] = 0;
3839 val = ((cbp >> (5 - i)) & 1);
3840 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3841 v->a_avail = v->c_avail = 0;
3842 if (i == 2 || i == 3 || !s->first_slice_line)
3843 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3844 if (i == 1 || i == 3 || s->mb_x)
3845 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3847 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3848 (i & 4) ? v->codingset2 : v->codingset);
3849 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3850 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3852 stride_y = s->linesize << fieldtx;
3853 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3855 stride_y = s->uvlinesize;
3858 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3862 } else { // inter MB
3863 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3865 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3866 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3867 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3869 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3870 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3871 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3874 s->mb_intra = v->is_intra[s->mb_x] = 0;
3875 for (i = 0; i < 6; i++)
3876 v->mb_type[0][s->block_index[i]] = 0;
3877 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3878 /* for all motion vector read MVDATA and motion compensate each block */
3882 for (i = 0; i < 6; i++) {
3885 val = ((mvbp >> (3 - i)) & 1);
3887 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3889 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3890 vc1_mc_4mv_luma(v, i, 0, 0);
3891 } else if (i == 4) {
3892 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3899 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3901 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3902 vc1_mc_4mv_luma(v, 0, 0, 0);
3903 vc1_mc_4mv_luma(v, 1, 0, 0);
3906 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3908 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3909 vc1_mc_4mv_luma(v, 2, 0, 0);
3910 vc1_mc_4mv_luma(v, 3, 0, 0);
3911 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3913 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3916 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3918 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3922 GET_MQUANT(); // p. 227
3923 s->current_picture.qscale_table[mb_pos] = mquant;
3924 if (!v->ttmbf && cbp)
3925 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3926 for (i = 0; i < 6; i++) {
3927 s->dc_val[0][s->block_index[i]] = 0;
3929 val = ((cbp >> (5 - i)) & 1);
3931 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3933 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3935 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3936 first_block, s->dest[dst_idx] + off,
3937 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3938 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3939 block_cbp |= pat << (i << 2);
3940 if (!v->ttmbf && ttmb < 8)
3947 s->mb_intra = v->is_intra[s->mb_x] = 0;
3948 for (i = 0; i < 6; i++) {
3949 v->mb_type[0][s->block_index[i]] = 0;
3950 s->dc_val[0][s->block_index[i]] = 0;
3952 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3953 s->current_picture.qscale_table[mb_pos] = 0;
3954 v->blk_mv_type[s->block_index[0]] = 0;
3955 v->blk_mv_type[s->block_index[1]] = 0;
3956 v->blk_mv_type[s->block_index[2]] = 0;
3957 v->blk_mv_type[s->block_index[3]] = 0;
3958 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3961 if (s->mb_x == s->mb_width - 1)
3962 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3966 static int vc1_decode_p_mb_intfi(VC1Context *v)
3968 MpegEncContext *s = &v->s;
3969 GetBitContext *gb = &s->gb;
3971 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3972 int cbp = 0; /* cbp decoding stuff */
3973 int mqdiff, mquant; /* MB quantization */
3974 int ttmb = v->ttfrm; /* MB Transform type */
3976 int mb_has_coeffs = 1; /* last_flag */
3977 int dmv_x, dmv_y; /* Differential MV components */
3978 int val; /* temp values */
3979 int first_block = 1;
3982 int block_cbp = 0, pat, block_tt = 0;
3985 mquant = v->pq; /* Lossy initialization */
3987 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3988 if (idx_mbmode <= 1) { // intra MB
3989 s->mb_intra = v->is_intra[s->mb_x] = 1;
3990 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3991 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3992 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3994 s->current_picture.qscale_table[mb_pos] = mquant;
3995 /* Set DC scale - y and c use the same (not sure if necessary here) */
3996 s->y_dc_scale = s->y_dc_scale_table[mquant];
3997 s->c_dc_scale = s->c_dc_scale_table[mquant];
3998 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3999 mb_has_coeffs = idx_mbmode & 1;
4001 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4003 for (i = 0; i < 6; i++) {
4004 s->dc_val[0][s->block_index[i]] = 0;
4005 v->mb_type[0][s->block_index[i]] = 1;
4007 val = ((cbp >> (5 - i)) & 1);
4008 v->a_avail = v->c_avail = 0;
4009 if (i == 2 || i == 3 || !s->first_slice_line)
4010 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4011 if (i == 1 || i == 3 || s->mb_x)
4012 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4014 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4015 (i & 4) ? v->codingset2 : v->codingset);
4016 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4018 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4019 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4020 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4021 // TODO: loop filter
4024 s->mb_intra = v->is_intra[s->mb_x] = 0;
4025 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4026 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4027 if (idx_mbmode <= 5) { // 1-MV
4028 dmv_x = dmv_y = pred_flag = 0;
4029 if (idx_mbmode & 1) {
4030 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4032 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4034 mb_has_coeffs = !(idx_mbmode & 2);
4036 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4037 for (i = 0; i < 6; i++) {
4039 dmv_x = dmv_y = pred_flag = 0;
4040 val = ((v->fourmvbp >> (3 - i)) & 1);
4042 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4044 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4045 vc1_mc_4mv_luma(v, i, 0, 0);
4047 vc1_mc_4mv_chroma(v, 0);
4049 mb_has_coeffs = idx_mbmode & 1;
4052 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4056 s->current_picture.qscale_table[mb_pos] = mquant;
4057 if (!v->ttmbf && cbp) {
4058 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4061 for (i = 0; i < 6; i++) {
4062 s->dc_val[0][s->block_index[i]] = 0;
4064 val = ((cbp >> (5 - i)) & 1);
4065 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4067 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4068 first_block, s->dest[dst_idx] + off,
4069 (i & 4) ? s->uvlinesize : s->linesize,
4070 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4072 block_cbp |= pat << (i << 2);
4073 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4078 if (s->mb_x == s->mb_width - 1)
4079 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4083 /** Decode one B-frame MB (in Main profile)
4085 static void vc1_decode_b_mb(VC1Context *v)
4087 MpegEncContext *s = &v->s;
4088 GetBitContext *gb = &s->gb;
4090 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4091 int cbp = 0; /* cbp decoding stuff */
4092 int mqdiff, mquant; /* MB quantization */
4093 int ttmb = v->ttfrm; /* MB Transform type */
4094 int mb_has_coeffs = 0; /* last_flag */
4095 int index, index1; /* LUT indexes */
4096 int val, sign; /* temp values */
4097 int first_block = 1;
4099 int skipped, direct;
4100 int dmv_x[2], dmv_y[2];
4101 int bmvtype = BMV_TYPE_BACKWARD;
4103 mquant = v->pq; /* lossy initialization */
4107 direct = get_bits1(gb);
4109 direct = v->direct_mb_plane[mb_pos];
4111 skipped = get_bits1(gb);
4113 skipped = v->s.mbskip_table[mb_pos];
4115 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4116 for (i = 0; i < 6; i++) {
4117 v->mb_type[0][s->block_index[i]] = 0;
4118 s->dc_val[0][s->block_index[i]] = 0;
4120 s->current_picture.qscale_table[mb_pos] = 0;
4124 GET_MVDATA(dmv_x[0], dmv_y[0]);
4125 dmv_x[1] = dmv_x[0];
4126 dmv_y[1] = dmv_y[0];
4128 if (skipped || !s->mb_intra) {
4129 bmvtype = decode012(gb);
4132 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4135 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4138 bmvtype = BMV_TYPE_INTERPOLATED;
4139 dmv_x[0] = dmv_y[0] = 0;
4143 for (i = 0; i < 6; i++)
4144 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4148 bmvtype = BMV_TYPE_INTERPOLATED;
4149 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4150 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4154 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4157 s->current_picture.qscale_table[mb_pos] = mquant;
4159 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4160 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4161 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4162 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4164 if (!mb_has_coeffs && !s->mb_intra) {
4165 /* no coded blocks - effectively skipped */
4166 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4167 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4170 if (s->mb_intra && !mb_has_coeffs) {
4172 s->current_picture.qscale_table[mb_pos] = mquant;
4173 s->ac_pred = get_bits1(gb);
4175 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4177 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4178 GET_MVDATA(dmv_x[0], dmv_y[0]);
4179 if (!mb_has_coeffs) {
4180 /* interpolated skipped block */
4181 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4182 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4186 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4188 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4191 s->ac_pred = get_bits1(gb);
4192 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4194 s->current_picture.qscale_table[mb_pos] = mquant;
4195 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4196 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4200 for (i = 0; i < 6; i++) {
4201 s->dc_val[0][s->block_index[i]] = 0;
4203 val = ((cbp >> (5 - i)) & 1);
4204 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4205 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4207 /* check if prediction blocks A and C are available */
4208 v->a_avail = v->c_avail = 0;
4209 if (i == 2 || i == 3 || !s->first_slice_line)
4210 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4211 if (i == 1 || i == 3 || s->mb_x)
4212 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4214 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4215 (i & 4) ? v->codingset2 : v->codingset);
4216 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4218 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4220 for (j = 0; j < 64; j++)
4221 s->block[i][j] <<= 1;
4222 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4224 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4225 first_block, s->dest[dst_idx] + off,
4226 (i & 4) ? s->uvlinesize : s->linesize,
4227 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4228 if (!v->ttmbf && ttmb < 8)
4235 /** Decode one B-frame MB (in interlaced field B picture)
4237 static void vc1_decode_b_mb_intfi(VC1Context *v)
4239 MpegEncContext *s = &v->s;
4240 GetBitContext *gb = &s->gb;
4242 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4243 int cbp = 0; /* cbp decoding stuff */
4244 int mqdiff, mquant; /* MB quantization */
4245 int ttmb = v->ttfrm; /* MB Transform type */
4246 int mb_has_coeffs = 0; /* last_flag */
4247 int val; /* temp value */
4248 int first_block = 1;
4251 int dmv_x[2], dmv_y[2], pred_flag[2];
4252 int bmvtype = BMV_TYPE_BACKWARD;
4255 mquant = v->pq; /* Lossy initialization */
4258 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4259 if (idx_mbmode <= 1) { // intra MB
4260 s->mb_intra = v->is_intra[s->mb_x] = 1;
4261 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4262 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4263 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4265 s->current_picture.qscale_table[mb_pos] = mquant;
4266 /* Set DC scale - y and c use the same (not sure if necessary here) */
4267 s->y_dc_scale = s->y_dc_scale_table[mquant];
4268 s->c_dc_scale = s->c_dc_scale_table[mquant];
4269 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4270 mb_has_coeffs = idx_mbmode & 1;
4272 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4274 for (i = 0; i < 6; i++) {
4275 s->dc_val[0][s->block_index[i]] = 0;
4277 val = ((cbp >> (5 - i)) & 1);
4278 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4279 v->a_avail = v->c_avail = 0;
4280 if (i == 2 || i == 3 || !s->first_slice_line)
4281 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4282 if (i == 1 || i == 3 || s->mb_x)
4283 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4285 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4286 (i & 4) ? v->codingset2 : v->codingset);
4287 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4289 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4291 for (j = 0; j < 64; j++)
4292 s->block[i][j] <<= 1;
4293 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4294 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4295 // TODO: yet to perform loop filter
4298 s->mb_intra = v->is_intra[s->mb_x] = 0;
4299 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4300 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4302 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4304 fwd = v->forward_mb_plane[mb_pos];
4305 if (idx_mbmode <= 5) { // 1-MV
4307 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4308 pred_flag[0] = pred_flag[1] = 0;
4310 bmvtype = BMV_TYPE_FORWARD;
4312 bmvtype = decode012(gb);
4315 bmvtype = BMV_TYPE_BACKWARD;
4318 bmvtype = BMV_TYPE_DIRECT;
4321 bmvtype = BMV_TYPE_INTERPOLATED;
4322 interpmvp = get_bits1(gb);
4325 v->bmvtype = bmvtype;
4326 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4327 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4330 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4332 if (bmvtype == BMV_TYPE_DIRECT) {
4333 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4334 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4336 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4337 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4338 mb_has_coeffs = !(idx_mbmode & 2);
4341 bmvtype = BMV_TYPE_FORWARD;
4342 v->bmvtype = bmvtype;
4343 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4344 for (i = 0; i < 6; i++) {
4346 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4347 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4348 val = ((v->fourmvbp >> (3 - i)) & 1);
4350 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4351 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4352 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4354 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4355 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4357 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4359 mb_has_coeffs = idx_mbmode & 1;
4362 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4366 s->current_picture.qscale_table[mb_pos] = mquant;
4367 if (!v->ttmbf && cbp) {
4368 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4371 for (i = 0; i < 6; i++) {
4372 s->dc_val[0][s->block_index[i]] = 0;
4374 val = ((cbp >> (5 - i)) & 1);
4375 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4377 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4378 first_block, s->dest[dst_idx] + off,
4379 (i & 4) ? s->uvlinesize : s->linesize,
4380 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4381 if (!v->ttmbf && ttmb < 8)
4389 /** Decode one B-frame MB (in interlaced frame B picture)
4391 static int vc1_decode_b_mb_intfr(VC1Context *v)
4393 MpegEncContext *s = &v->s;
4394 GetBitContext *gb = &s->gb;
4396 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4397 int cbp = 0; /* cbp decoding stuff */
4398 int mqdiff, mquant; /* MB quantization */
4399 int ttmb = v->ttfrm; /* MB Transform type */
4400 int mvsw = 0; /* motion vector switch */
4401 int mb_has_coeffs = 1; /* last_flag */
4402 int dmv_x, dmv_y; /* Differential MV components */
4403 int val; /* temp value */
4404 int first_block = 1;
4406 int skipped, direct, twomv = 0;
4407 int block_cbp = 0, pat, block_tt = 0;
4408 int idx_mbmode = 0, mvbp;
4409 int stride_y, fieldtx;
4410 int bmvtype = BMV_TYPE_BACKWARD;
4413 mquant = v->pq; /* Lossy initialization */
4416 skipped = get_bits1(gb);
4418 skipped = v->s.mbskip_table[mb_pos];
4421 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4422 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4424 v->blk_mv_type[s->block_index[0]] = 1;
4425 v->blk_mv_type[s->block_index[1]] = 1;
4426 v->blk_mv_type[s->block_index[2]] = 1;
4427 v->blk_mv_type[s->block_index[3]] = 1;
4429 v->blk_mv_type[s->block_index[0]] = 0;
4430 v->blk_mv_type[s->block_index[1]] = 0;
4431 v->blk_mv_type[s->block_index[2]] = 0;
4432 v->blk_mv_type[s->block_index[3]] = 0;
4437 direct = get_bits1(gb);
4439 direct = v->direct_mb_plane[mb_pos];
4442 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);
4443 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);
4444 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);
4445 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);
4448 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);
4449 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);
4450 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);
4451 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);
4453 for (i = 1; i < 4; i += 2) {
4454 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4455 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4456 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4457 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4460 for (i = 1; i < 4; i++) {
4461 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4462 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4463 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4464 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4469 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4470 for (i = 0; i < 4; i++) {
4471 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4472 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4473 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4474 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4476 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4477 s->mb_intra = v->is_intra[s->mb_x] = 1;
4478 for (i = 0; i < 6; i++)
4479 v->mb_type[0][s->block_index[i]] = 1;
4480 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4481 mb_has_coeffs = get_bits1(gb);
4483 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4484 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4486 s->current_picture.qscale_table[mb_pos] = mquant;
4487 /* Set DC scale - y and c use the same (not sure if necessary here) */
4488 s->y_dc_scale = s->y_dc_scale_table[mquant];
4489 s->c_dc_scale = s->c_dc_scale_table[mquant];
4491 for (i = 0; i < 6; i++) {
4492 s->dc_val[0][s->block_index[i]] = 0;
4494 val = ((cbp >> (5 - i)) & 1);
4495 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4496 v->a_avail = v->c_avail = 0;
4497 if (i == 2 || i == 3 || !s->first_slice_line)
4498 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4499 if (i == 1 || i == 3 || s->mb_x)
4500 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4502 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4503 (i & 4) ? v->codingset2 : v->codingset);
4504 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4506 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4508 stride_y = s->linesize << fieldtx;
4509 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4511 stride_y = s->uvlinesize;
4514 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4517 s->mb_intra = v->is_intra[s->mb_x] = 0;
4519 if (skipped || !s->mb_intra) {
4520 bmvtype = decode012(gb);
4523 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4526 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4529 bmvtype = BMV_TYPE_INTERPOLATED;
4533 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4534 mvsw = get_bits1(gb);
4537 if (!skipped) { // inter MB
4538 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4540 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4542 if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
4543 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4544 } else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
4545 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4549 for (i = 0; i < 6; i++)
4550 v->mb_type[0][s->block_index[i]] = 0;
4551 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4552 /* for all motion vector read MVDATA and motion compensate each block */
4556 for (i = 0; i < 4; i++) {
4557 vc1_mc_4mv_luma(v, i, 0, 0);
4558 vc1_mc_4mv_luma(v, i, 1, 1);
4560 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4561 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4566 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4568 for (i = 0; i < 4; i++) {
4571 val = ((mvbp >> (3 - i)) & 1);
4573 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4575 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4576 vc1_mc_4mv_luma(v, j, dir, dir);
4577 vc1_mc_4mv_luma(v, j+1, dir, dir);
4580 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4581 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4582 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4586 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4588 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4593 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4595 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4598 dir = bmvtype == BMV_TYPE_BACKWARD;
4605 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4606 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4610 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4611 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4614 for (i = 0; i < 2; i++) {
4615 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];
4616 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];
4617 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];
4618 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];
4621 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4622 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4625 vc1_mc_4mv_luma(v, 0, dir, 0);
4626 vc1_mc_4mv_luma(v, 1, dir, 0);
4627 vc1_mc_4mv_luma(v, 2, dir2, 0);
4628 vc1_mc_4mv_luma(v, 3, dir2, 0);
4629 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4631 dir = bmvtype == BMV_TYPE_BACKWARD;
4633 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4636 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4638 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4639 v->blk_mv_type[s->block_index[0]] = 1;
4640 v->blk_mv_type[s->block_index[1]] = 1;
4641 v->blk_mv_type[s->block_index[2]] = 1;
4642 v->blk_mv_type[s->block_index[3]] = 1;
4643 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4644 for (i = 0; i < 2; i++) {
4645 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];
4646 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];
4652 GET_MQUANT(); // p. 227
4653 s->current_picture.qscale_table[mb_pos] = mquant;
4654 if (!v->ttmbf && cbp)
4655 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4656 for (i = 0; i < 6; i++) {
4657 s->dc_val[0][s->block_index[i]] = 0;
4659 val = ((cbp >> (5 - i)) & 1);
4661 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4663 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4665 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4666 first_block, s->dest[dst_idx] + off,
4667 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4668 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4669 block_cbp |= pat << (i << 2);
4670 if (!v->ttmbf && ttmb < 8)
4678 for (i = 0; i < 6; i++) {
4679 v->mb_type[0][s->block_index[i]] = 0;
4680 s->dc_val[0][s->block_index[i]] = 0;
4682 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4683 s->current_picture.qscale_table[mb_pos] = 0;
4684 v->blk_mv_type[s->block_index[0]] = 0;
4685 v->blk_mv_type[s->block_index[1]] = 0;
4686 v->blk_mv_type[s->block_index[2]] = 0;
4687 v->blk_mv_type[s->block_index[3]] = 0;
4690 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4691 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4692 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4694 dir = bmvtype == BMV_TYPE_BACKWARD;
4695 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4700 for (i = 0; i < 2; i++) {
4701 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];
4702 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];
4703 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];
4704 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];
4707 v->blk_mv_type[s->block_index[0]] = 1;
4708 v->blk_mv_type[s->block_index[1]] = 1;
4709 v->blk_mv_type[s->block_index[2]] = 1;
4710 v->blk_mv_type[s->block_index[3]] = 1;
4711 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4712 for (i = 0; i < 2; i++) {
4713 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];
4714 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];
4721 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4726 if (s->mb_x == s->mb_width - 1)
4727 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4728 v->cbp[s->mb_x] = block_cbp;
4729 v->ttblk[s->mb_x] = block_tt;
4733 /** Decode blocks of I-frame
4735 static void vc1_decode_i_blocks(VC1Context *v)
4738 MpegEncContext *s = &v->s;
4743 /* select codingmode used for VLC tables selection */
4744 switch (v->y_ac_table_index) {
4746 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4749 v->codingset = CS_HIGH_MOT_INTRA;
4752 v->codingset = CS_MID_RATE_INTRA;
4756 switch (v->c_ac_table_index) {
4758 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4761 v->codingset2 = CS_HIGH_MOT_INTER;
4764 v->codingset2 = CS_MID_RATE_INTER;
4768 /* Set DC scale - y and c use the same */
4769 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4770 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4773 s->mb_x = s->mb_y = 0;
4775 s->first_slice_line = 1;
4776 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4778 init_block_index(v);
4779 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4781 ff_update_block_index(s);
4782 dst[0] = s->dest[0];
4783 dst[1] = dst[0] + 8;
4784 dst[2] = s->dest[0] + s->linesize * 8;
4785 dst[3] = dst[2] + 8;
4786 dst[4] = s->dest[1];
4787 dst[5] = s->dest[2];
4788 s->dsp.clear_blocks(s->block[0]);
4789 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4790 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4791 s->current_picture.qscale_table[mb_pos] = v->pq;
4792 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4793 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4795 // do actual MB decoding and displaying
4796 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4797 v->s.ac_pred = get_bits1(&v->s.gb);
4799 for (k = 0; k < 6; k++) {
4800 val = ((cbp >> (5 - k)) & 1);
4803 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4807 cbp |= val << (5 - k);
4809 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4811 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4813 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4814 if (v->pq >= 9 && v->overlap) {
4816 for (j = 0; j < 64; j++)
4817 s->block[k][j] <<= 1;
4818 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4821 for (j = 0; j < 64; j++)
4822 s->block[k][j] = (s->block[k][j] - 64) << 1;
4823 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4827 if (v->pq >= 9 && v->overlap) {
4829 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4830 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4831 if (!(s->flags & CODEC_FLAG_GRAY)) {
4832 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4833 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4836 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4837 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4838 if (!s->first_slice_line) {
4839 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4840 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4841 if (!(s->flags & CODEC_FLAG_GRAY)) {
4842 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4843 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4846 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4847 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4849 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4851 if (get_bits_count(&s->gb) > v->bits) {
4852 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4853 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4854 get_bits_count(&s->gb), v->bits);
4858 if (!v->s.loop_filter)
4859 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4861 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4863 s->first_slice_line = 0;
4865 if (v->s.loop_filter)
4866 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4868 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4869 * profile, these only differ are when decoding MSS2 rectangles. */
4870 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4873 /** Decode blocks of I-frame for advanced profile
4875 static void vc1_decode_i_blocks_adv(VC1Context *v)
4878 MpegEncContext *s = &v->s;
4884 GetBitContext *gb = &s->gb;
4886 /* select codingmode used for VLC tables selection */
4887 switch (v->y_ac_table_index) {
4889 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4892 v->codingset = CS_HIGH_MOT_INTRA;
4895 v->codingset = CS_MID_RATE_INTRA;
4899 switch (v->c_ac_table_index) {
4901 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4904 v->codingset2 = CS_HIGH_MOT_INTER;
4907 v->codingset2 = CS_MID_RATE_INTER;
4912 s->mb_x = s->mb_y = 0;
4914 s->first_slice_line = 1;
4915 s->mb_y = s->start_mb_y;
4916 if (s->start_mb_y) {
4918 init_block_index(v);
4919 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4920 (1 + s->b8_stride) * sizeof(*s->coded_block));
4922 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4924 init_block_index(v);
4925 for (;s->mb_x < s->mb_width; s->mb_x++) {
4926 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4927 ff_update_block_index(s);
4928 s->dsp.clear_blocks(block[0]);
4929 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4930 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4931 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4932 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4934 // do actual MB decoding and displaying
4935 if (v->fieldtx_is_raw)
4936 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4937 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4938 if ( v->acpred_is_raw)
4939 v->s.ac_pred = get_bits1(&v->s.gb);
4941 v->s.ac_pred = v->acpred_plane[mb_pos];
4943 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4944 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4948 s->current_picture.qscale_table[mb_pos] = mquant;
4949 /* Set DC scale - y and c use the same */
4950 s->y_dc_scale = s->y_dc_scale_table[mquant];
4951 s->c_dc_scale = s->c_dc_scale_table[mquant];
4953 for (k = 0; k < 6; k++) {
4954 val = ((cbp >> (5 - k)) & 1);
4957 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4961 cbp |= val << (5 - k);
4963 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4964 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4966 vc1_decode_i_block_adv(v, block[k], k, val,
4967 (k < 4) ? v->codingset : v->codingset2, mquant);
4969 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4971 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4974 vc1_smooth_overlap_filter_iblk(v);
4975 vc1_put_signed_blocks_clamped(v);
4976 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4978 if (get_bits_count(&s->gb) > v->bits) {
4979 // TODO: may need modification to handle slice coding
4980 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4981 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4982 get_bits_count(&s->gb), v->bits);
4986 if (!v->s.loop_filter)
4987 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4989 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4990 s->first_slice_line = 0;
4993 /* raw bottom MB row */
4995 init_block_index(v);
4997 for (;s->mb_x < s->mb_width; s->mb_x++) {
4998 ff_update_block_index(s);
4999 vc1_put_signed_blocks_clamped(v);
5000 if (v->s.loop_filter)
5001 vc1_loop_filter_iblk_delayed(v, v->pq);
5003 if (v->s.loop_filter)
5004 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5005 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5006 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5009 static void vc1_decode_p_blocks(VC1Context *v)
5011 MpegEncContext *s = &v->s;
5012 int apply_loop_filter;
5014 /* select codingmode used for VLC tables selection */
5015 switch (v->c_ac_table_index) {
5017 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5020 v->codingset = CS_HIGH_MOT_INTRA;
5023 v->codingset = CS_MID_RATE_INTRA;
5027 switch (v->c_ac_table_index) {
5029 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5032 v->codingset2 = CS_HIGH_MOT_INTER;
5035 v->codingset2 = CS_MID_RATE_INTER;
5039 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5040 v->fcm == PROGRESSIVE;
5041 s->first_slice_line = 1;
5042 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5043 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5045 init_block_index(v);
5046 for (; s->mb_x < s->mb_width; s->mb_x++) {
5047 ff_update_block_index(s);
5049 if (v->fcm == ILACE_FIELD)
5050 vc1_decode_p_mb_intfi(v);
5051 else if (v->fcm == ILACE_FRAME)
5052 vc1_decode_p_mb_intfr(v);
5053 else vc1_decode_p_mb(v);
5054 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5055 vc1_apply_p_loop_filter(v);
5056 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5057 // TODO: may need modification to handle slice coding
5058 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5059 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5060 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5064 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5065 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5066 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5067 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5068 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5069 s->first_slice_line = 0;
5071 if (apply_loop_filter) {
5073 init_block_index(v);
5074 for (; s->mb_x < s->mb_width; s->mb_x++) {
5075 ff_update_block_index(s);
5076 vc1_apply_p_loop_filter(v);
5079 if (s->end_mb_y >= s->start_mb_y)
5080 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5081 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5082 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5085 static void vc1_decode_b_blocks(VC1Context *v)
5087 MpegEncContext *s = &v->s;
5089 /* select codingmode used for VLC tables selection */
5090 switch (v->c_ac_table_index) {
5092 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5095 v->codingset = CS_HIGH_MOT_INTRA;
5098 v->codingset = CS_MID_RATE_INTRA;
5102 switch (v->c_ac_table_index) {
5104 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5107 v->codingset2 = CS_HIGH_MOT_INTER;
5110 v->codingset2 = CS_MID_RATE_INTER;
5114 s->first_slice_line = 1;
5115 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5117 init_block_index(v);
5118 for (; s->mb_x < s->mb_width; s->mb_x++) {
5119 ff_update_block_index(s);
5121 if (v->fcm == ILACE_FIELD)
5122 vc1_decode_b_mb_intfi(v);
5123 else if (v->fcm == ILACE_FRAME)
5124 vc1_decode_b_mb_intfr(v);
5127 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5128 // TODO: may need modification to handle slice coding
5129 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5130 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5131 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5134 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5136 if (!v->s.loop_filter)
5137 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5139 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5140 s->first_slice_line = 0;
5142 if (v->s.loop_filter)
5143 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5144 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5145 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5148 static void vc1_decode_skip_blocks(VC1Context *v)
5150 MpegEncContext *s = &v->s;
5152 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5153 s->first_slice_line = 1;
5154 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5156 init_block_index(v);
5157 ff_update_block_index(s);
5158 if (s->last_picture.f.data[0]) {
5159 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5160 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5161 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5163 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5164 s->first_slice_line = 0;
5166 s->pict_type = AV_PICTURE_TYPE_P;
5169 void ff_vc1_decode_blocks(VC1Context *v)
5172 v->s.esc3_level_length = 0;
5174 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5177 v->left_blk_idx = -1;
5178 v->topleft_blk_idx = 1;
5180 switch (v->s.pict_type) {
5181 case AV_PICTURE_TYPE_I:
5182 if (v->profile == PROFILE_ADVANCED)
5183 vc1_decode_i_blocks_adv(v);
5185 vc1_decode_i_blocks(v);
5187 case AV_PICTURE_TYPE_P:
5188 if (v->p_frame_skipped)
5189 vc1_decode_skip_blocks(v);
5191 vc1_decode_p_blocks(v);
5193 case AV_PICTURE_TYPE_B:
5195 if (v->profile == PROFILE_ADVANCED)
5196 vc1_decode_i_blocks_adv(v);
5198 vc1_decode_i_blocks(v);
5200 vc1_decode_b_blocks(v);
5206 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5210 * Transform coefficients for both sprites in 16.16 fixed point format,
5211 * in the order they appear in the bitstream:
5213 * rotation 1 (unused)
5215 * rotation 2 (unused)
5222 int effect_type, effect_flag;
5223 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5224 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5227 static inline int get_fp_val(GetBitContext* gb)
5229 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5232 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5236 switch (get_bits(gb, 2)) {
5239 c[2] = get_fp_val(gb);
5243 c[0] = c[4] = get_fp_val(gb);
5244 c[2] = get_fp_val(gb);
5247 c[0] = get_fp_val(gb);
5248 c[2] = get_fp_val(gb);
5249 c[4] = get_fp_val(gb);
5252 c[0] = get_fp_val(gb);
5253 c[1] = get_fp_val(gb);
5254 c[2] = get_fp_val(gb);
5255 c[3] = get_fp_val(gb);
5256 c[4] = get_fp_val(gb);
5259 c[5] = get_fp_val(gb);
5261 c[6] = get_fp_val(gb);
5266 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5268 AVCodecContext *avctx = v->s.avctx;
5271 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5272 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5273 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5274 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5275 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5276 for (i = 0; i < 7; i++)
5277 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5278 sd->coefs[sprite][i] / (1<<16),
5279 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5280 av_log(avctx, AV_LOG_DEBUG, "\n");
5284 if (sd->effect_type = get_bits_long(gb, 30)) {
5285 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5287 vc1_sprite_parse_transform(gb, sd->effect_params1);
5290 vc1_sprite_parse_transform(gb, sd->effect_params1);
5291 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5294 for (i = 0; i < sd->effect_pcount1; i++)
5295 sd->effect_params1[i] = get_fp_val(gb);
5297 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5298 // effect 13 is simple alpha blending and matches the opacity above
5299 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5300 for (i = 0; i < sd->effect_pcount1; i++)
5301 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5302 sd->effect_params1[i] / (1 << 16),
5303 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5304 av_log(avctx, AV_LOG_DEBUG, "\n");
5307 sd->effect_pcount2 = get_bits(gb, 16);
5308 if (sd->effect_pcount2 > 10) {
5309 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5311 } else if (sd->effect_pcount2) {
5313 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5314 while (++i < sd->effect_pcount2) {
5315 sd->effect_params2[i] = get_fp_val(gb);
5316 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5317 sd->effect_params2[i] / (1 << 16),
5318 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5320 av_log(avctx, AV_LOG_DEBUG, "\n");
5323 if (sd->effect_flag = get_bits1(gb))
5324 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5326 if (get_bits_count(gb) >= gb->size_in_bits +
5327 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
5328 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5329 if (get_bits_count(gb) < gb->size_in_bits - 8)
5330 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5333 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5335 int i, plane, row, sprite;
5336 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5337 uint8_t* src_h[2][2];
5338 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5340 MpegEncContext *s = &v->s;
5342 for (i = 0; i < 2; i++) {
5343 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5344 xadv[i] = sd->coefs[i][0];
5345 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5346 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5348 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5349 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5351 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5353 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5354 int width = v->output_width>>!!plane;
5356 for (row = 0; row < v->output_height>>!!plane; row++) {
5357 uint8_t *dst = v->sprite_output_frame.data[plane] +
5358 v->sprite_output_frame.linesize[plane] * row;
5360 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5361 uint8_t *iplane = s->current_picture.f.data[plane];
5362 int iline = s->current_picture.f.linesize[plane];
5363 int ycoord = yoff[sprite] + yadv[sprite] * row;
5364 int yline = ycoord >> 16;
5366 ysub[sprite] = ycoord & 0xFFFF;
5368 iplane = s->last_picture.f.data[plane];
5369 iline = s->last_picture.f.linesize[plane];
5371 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5372 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5373 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5375 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5377 if (sr_cache[sprite][0] != yline) {
5378 if (sr_cache[sprite][1] == yline) {
5379 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5380 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5382 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5383 sr_cache[sprite][0] = yline;
5386 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5387 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5388 iplane + next_line, xoff[sprite],
5389 xadv[sprite], width);
5390 sr_cache[sprite][1] = yline + 1;
5392 src_h[sprite][0] = v->sr_rows[sprite][0];
5393 src_h[sprite][1] = v->sr_rows[sprite][1];
5397 if (!v->two_sprites) {
5399 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5401 memcpy(dst, src_h[0][0], width);
5404 if (ysub[0] && ysub[1]) {
5405 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5406 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5407 } else if (ysub[0]) {
5408 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5409 src_h[1][0], alpha, width);
5410 } else if (ysub[1]) {
5411 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5412 src_h[0][0], (1<<16)-1-alpha, width);
5414 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5420 for (i = 0; i < 2; i++) {
5430 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5433 MpegEncContext *s = &v->s;
5434 AVCodecContext *avctx = s->avctx;
5437 vc1_parse_sprites(v, gb, &sd);
5439 if (!s->current_picture.f.data[0]) {
5440 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5444 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5445 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5449 av_frame_unref(&v->sprite_output_frame);
5450 if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
5453 vc1_draw_sprites(v, &sd);
5458 static void vc1_sprite_flush(AVCodecContext *avctx)
5460 VC1Context *v = avctx->priv_data;
5461 MpegEncContext *s = &v->s;
5462 AVFrame *f = &s->current_picture.f;
5465 /* Windows Media Image codecs have a convergence interval of two keyframes.
5466 Since we can't enforce it, clear to black the missing sprite. This is
5467 wrong but it looks better than doing nothing. */
5470 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5471 for (i = 0; i < v->sprite_height>>!!plane; i++)
5472 memset(f->data[plane] + i * f->linesize[plane],
5473 plane ? 128 : 0, f->linesize[plane]);
5478 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5480 MpegEncContext *s = &v->s;
5483 /* Allocate mb bitplanes */
5484 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5485 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5486 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5487 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5488 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5489 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5491 v->n_allocated_blks = s->mb_width + 2;
5492 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5493 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5494 v->cbp = v->cbp_base + s->mb_stride;
5495 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5496 v->ttblk = v->ttblk_base + s->mb_stride;
5497 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5498 v->is_intra = v->is_intra_base + s->mb_stride;
5499 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5500 v->luma_mv = v->luma_mv_base + s->mb_stride;
5502 /* allocate block type info in that way so it could be used with s->block_index[] */
5503 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5504 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5505 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5506 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5508 /* allocate memory to store block level MV info */
5509 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5510 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5511 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5512 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5513 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5514 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5515 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5516 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5518 /* Init coded blocks info */
5519 if (v->profile == PROFILE_ADVANCED) {
5520 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5522 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5526 ff_intrax8_common_init(&v->x8,s);
5528 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5529 for (i = 0; i < 4; i++)
5530 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5533 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5534 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5541 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5544 for (i = 0; i < 64; i++) {
5545 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5546 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5547 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5548 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5549 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5550 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5556 /** Initialize a VC1/WMV3 decoder
5557 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5558 * @todo TODO: Decypher remaining bits in extra_data
5560 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5562 VC1Context *v = avctx->priv_data;
5563 MpegEncContext *s = &v->s;
5566 /* save the container output size for WMImage */
5567 v->output_width = avctx->width;
5568 v->output_height = avctx->height;
5570 if (!avctx->extradata_size || !avctx->extradata)
5572 if (!(avctx->flags & CODEC_FLAG_GRAY))
5573 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5575 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5576 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5578 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5579 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5581 if (ff_vc1_init_common(v) < 0)
5583 // ensure static VLC tables are initialized
5584 if (ff_msmpeg4_decode_init(avctx) < 0)
5586 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5588 // Hack to ensure the above functions will be called
5589 // again once we know all necessary settings.
5590 // That this is necessary might indicate a bug.
5591 ff_vc1_decode_end(avctx);
5593 ff_h264chroma_init(&v->h264chroma, 8);
5594 ff_vc1dsp_init(&v->vc1dsp);
5596 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5599 // looks like WMV3 has a sequence header stored in the extradata
5600 // advanced sequence header may be before the first frame
5601 // the last byte of the extradata is a version number, 1 for the
5602 // samples we can decode
5604 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5606 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5609 count = avctx->extradata_size*8 - get_bits_count(&gb);
5611 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5612 count, get_bits(&gb, count));
5613 } else if (count < 0) {
5614 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5616 } else { // VC1/WVC1/WVP2
5617 const uint8_t *start = avctx->extradata;
5618 uint8_t *end = avctx->extradata + avctx->extradata_size;
5619 const uint8_t *next;
5620 int size, buf2_size;
5621 uint8_t *buf2 = NULL;
5622 int seq_initialized = 0, ep_initialized = 0;
5624 if (avctx->extradata_size < 16) {
5625 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5629 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5630 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5632 for (; next < end; start = next) {
5633 next = find_next_marker(start + 4, end);
5634 size = next - start - 4;
5637 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5638 init_get_bits(&gb, buf2, buf2_size * 8);
5639 switch (AV_RB32(start)) {
5640 case VC1_CODE_SEQHDR:
5641 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5645 seq_initialized = 1;
5647 case VC1_CODE_ENTRYPOINT:
5648 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5657 if (!seq_initialized || !ep_initialized) {
5658 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5661 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5664 avctx->profile = v->profile;
5665 if (v->profile == PROFILE_ADVANCED)
5666 avctx->level = v->level;
5668 avctx->has_b_frames = !!avctx->max_b_frames;
5670 s->mb_width = (avctx->coded_width + 15) >> 4;
5671 s->mb_height = (avctx->coded_height + 15) >> 4;
5673 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5674 ff_vc1_init_transposed_scantables(v);
5676 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5681 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5682 v->sprite_width = avctx->coded_width;
5683 v->sprite_height = avctx->coded_height;
5685 avctx->coded_width = avctx->width = v->output_width;
5686 avctx->coded_height = avctx->height = v->output_height;
5688 // prevent 16.16 overflows
5689 if (v->sprite_width > 1 << 14 ||
5690 v->sprite_height > 1 << 14 ||
5691 v->output_width > 1 << 14 ||
5692 v->output_height > 1 << 14) return -1;
5694 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5695 avpriv_request_sample(avctx, "odd sprites support");
5696 return AVERROR_PATCHWELCOME;
5702 /** Close a VC1/WMV3 decoder
5703 * @warning Initial try at using MpegEncContext stuff
5705 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5707 VC1Context *v = avctx->priv_data;
5710 av_frame_unref(&v->sprite_output_frame);
5712 for (i = 0; i < 4; i++)
5713 av_freep(&v->sr_rows[i >> 1][i & 1]);
5714 av_freep(&v->hrd_rate);
5715 av_freep(&v->hrd_buffer);
5716 ff_MPV_common_end(&v->s);
5717 av_freep(&v->mv_type_mb_plane);
5718 av_freep(&v->direct_mb_plane);
5719 av_freep(&v->forward_mb_plane);
5720 av_freep(&v->fieldtx_plane);
5721 av_freep(&v->acpred_plane);
5722 av_freep(&v->over_flags_plane);
5723 av_freep(&v->mb_type_base);
5724 av_freep(&v->blk_mv_type_base);
5725 av_freep(&v->mv_f_base);
5726 av_freep(&v->mv_f_next_base);
5727 av_freep(&v->block);
5728 av_freep(&v->cbp_base);
5729 av_freep(&v->ttblk_base);
5730 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5731 av_freep(&v->luma_mv_base);
5732 ff_intrax8_common_end(&v->x8);
5737 /** Decode a VC1/WMV3 frame
5738 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5740 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5741 int *got_frame, AVPacket *avpkt)
5743 const uint8_t *buf = avpkt->data;
5744 int buf_size = avpkt->size, n_slices = 0, i, ret;
5745 VC1Context *v = avctx->priv_data;
5746 MpegEncContext *s = &v->s;
5747 AVFrame *pict = data;
5748 uint8_t *buf2 = NULL;
5749 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5750 int mb_height, n_slices1=-1;
5755 } *slices = NULL, *tmp;
5757 v->second_field = 0;
5759 if(s->flags & CODEC_FLAG_LOW_DELAY)
5762 /* no supplementary picture */
5763 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5764 /* special case for last picture */
5765 if (s->low_delay == 0 && s->next_picture_ptr) {
5766 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
5768 s->next_picture_ptr = NULL;
5776 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5777 if (v->profile < PROFILE_ADVANCED)
5778 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5780 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5783 //for advanced profile we may need to parse and unescape data
5784 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5786 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5788 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5789 const uint8_t *start, *end, *next;
5793 for (start = buf, end = buf + buf_size; next < end; start = next) {
5794 next = find_next_marker(start + 4, end);
5795 size = next - start - 4;
5796 if (size <= 0) continue;
5797 switch (AV_RB32(start)) {
5798 case VC1_CODE_FRAME:
5799 if (avctx->hwaccel ||
5800 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5802 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5804 case VC1_CODE_FIELD: {
5806 if (avctx->hwaccel ||
5807 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5808 buf_start_second_field = start;
5809 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5813 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5814 if (!slices[n_slices].buf)
5816 buf_size3 = vc1_unescape_buffer(start + 4, size,
5817 slices[n_slices].buf);
5818 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5820 /* assuming that the field marker is at the exact middle,
5821 hope it's correct */
5822 slices[n_slices].mby_start = s->mb_height >> 1;
5823 n_slices1 = n_slices - 1; // index of the last slice of the first field
5827 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5828 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5829 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5830 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5832 case VC1_CODE_SLICE: {
5834 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5838 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5839 if (!slices[n_slices].buf)
5841 buf_size3 = vc1_unescape_buffer(start + 4, size,
5842 slices[n_slices].buf);
5843 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5845 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5851 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5852 const uint8_t *divider;
5855 divider = find_next_marker(buf, buf + buf_size);
5856 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5857 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5859 } else { // found field marker, unescape second field
5860 if (avctx->hwaccel ||
5861 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5862 buf_start_second_field = divider;
5863 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5867 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5868 if (!slices[n_slices].buf)
5870 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5871 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5873 slices[n_slices].mby_start = s->mb_height >> 1;
5874 n_slices1 = n_slices - 1;
5877 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5879 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5881 init_get_bits(&s->gb, buf2, buf_size2*8);
5883 init_get_bits(&s->gb, buf, buf_size*8);
5885 if (v->res_sprite) {
5886 v->new_sprite = !get_bits1(&s->gb);
5887 v->two_sprites = get_bits1(&s->gb);
5888 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5889 we're using the sprite compositor. These are intentionally kept separate
5890 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5891 the vc1 one for WVP2 */
5892 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5893 if (v->new_sprite) {
5894 // switch AVCodecContext parameters to those of the sprites
5895 avctx->width = avctx->coded_width = v->sprite_width;
5896 avctx->height = avctx->coded_height = v->sprite_height;
5903 if (s->context_initialized &&
5904 (s->width != avctx->coded_width ||
5905 s->height != avctx->coded_height)) {
5906 ff_vc1_decode_end(avctx);
5909 if (!s->context_initialized) {
5910 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5913 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5915 if (v->profile == PROFILE_ADVANCED) {
5916 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5918 s->h_edge_pos = avctx->coded_width;
5919 s->v_edge_pos = avctx->coded_height;
5923 /* We need to set current_picture_ptr before reading the header,
5924 * otherwise we cannot store anything in there. */
5925 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5926 int i = ff_find_unused_picture(s, 0);
5929 s->current_picture_ptr = &s->picture[i];
5932 // do parse frame header
5933 v->pic_header_flag = 0;
5934 v->first_pic_header_flag = 1;
5935 if (v->profile < PROFILE_ADVANCED) {
5936 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5940 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5944 v->first_pic_header_flag = 0;
5946 if (avctx->debug & FF_DEBUG_PICT_INFO)
5947 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5949 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5950 && s->pict_type != AV_PICTURE_TYPE_I) {
5951 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5955 if ((s->mb_height >> v->field_mode) == 0) {
5956 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5960 // process pulldown flags
5961 s->current_picture_ptr->f.repeat_pict = 0;
5962 // Pulldown flags are only valid when 'broadcast' has been set.
5963 // So ticks_per_frame will be 2
5966 s->current_picture_ptr->f.repeat_pict = 1;
5967 } else if (v->rptfrm) {
5969 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5972 // for skipping the frame
5973 s->current_picture.f.pict_type = s->pict_type;
5974 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5976 /* skip B-frames if we don't have reference frames */
5977 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5980 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5981 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5982 avctx->skip_frame >= AVDISCARD_ALL) {
5986 if (s->next_p_frame_damaged) {
5987 if (s->pict_type == AV_PICTURE_TYPE_B)
5990 s->next_p_frame_damaged = 0;
5993 if (ff_MPV_frame_start(s, avctx) < 0) {
5997 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5998 v->s.current_picture_ptr->f.top_field_first = v->tff;
6000 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
6001 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
6003 if ((CONFIG_VC1_VDPAU_DECODER)
6004 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6005 if (v->field_mode && buf_start_second_field) {
6006 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6007 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6009 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6011 } else if (avctx->hwaccel) {
6012 if (v->field_mode && buf_start_second_field) {
6013 // decode first field
6014 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6015 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6017 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6019 if (avctx->hwaccel->end_frame(avctx) < 0)
6022 // decode second field
6023 s->gb = slices[n_slices1 + 1].gb;
6024 s->picture_structure = PICT_TOP_FIELD + v->tff;
6025 v->second_field = 1;
6026 v->pic_header_flag = 0;
6027 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6028 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6031 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
6033 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6035 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6037 if (avctx->hwaccel->end_frame(avctx) < 0)
6040 s->picture_structure = PICT_FRAME;
6041 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6043 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6045 if (avctx->hwaccel->end_frame(avctx) < 0)
6052 ff_mpeg_er_frame_start(s);
6054 v->bits = buf_size * 8;
6055 v->end_mb_x = s->mb_width;
6056 if (v->field_mode) {
6057 s->current_picture.f.linesize[0] <<= 1;
6058 s->current_picture.f.linesize[1] <<= 1;
6059 s->current_picture.f.linesize[2] <<= 1;
6061 s->uvlinesize <<= 1;
6063 mb_height = s->mb_height >> v->field_mode;
6064 for (i = 0; i <= n_slices; i++) {
6065 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6066 if (v->field_mode <= 0) {
6067 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6068 "picture boundary (%d >= %d)\n", i,
6069 slices[i - 1].mby_start, mb_height);
6072 v->second_field = 1;
6073 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6074 v->mb_off = s->mb_stride * s->mb_height >> 1;
6076 v->second_field = 0;
6081 v->pic_header_flag = 0;
6082 if (v->field_mode && i == n_slices1 + 2) {
6083 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6084 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6087 } else if (get_bits1(&s->gb)) {
6088 v->pic_header_flag = 1;
6089 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6090 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6097 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6098 if (!v->field_mode || v->second_field)
6099 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6101 if (i >= n_slices) {
6102 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6105 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6107 if (s->end_mb_y <= s->start_mb_y) {
6108 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6111 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6112 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6115 ff_vc1_decode_blocks(v);
6117 s->gb = slices[i].gb;
6119 if (v->field_mode) {
6120 v->second_field = 0;
6121 s->current_picture.f.linesize[0] >>= 1;
6122 s->current_picture.f.linesize[1] >>= 1;
6123 s->current_picture.f.linesize[2] >>= 1;
6125 s->uvlinesize >>= 1;
6126 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6127 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6128 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6131 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6132 get_bits_count(&s->gb), s->gb.size_in_bits);
6133 // if (get_bits_count(&s->gb) > buf_size * 8)
6135 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6138 ff_er_frame_end(&s->er);
6141 ff_MPV_frame_end(s);
6143 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6145 avctx->width = avctx->coded_width = v->output_width;
6146 avctx->height = avctx->coded_height = v->output_height;
6147 if (avctx->skip_frame >= AVDISCARD_NONREF)
6149 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6150 if (vc1_decode_sprites(v, &s->gb))
6153 if ((ret = av_frame_ref(pict, &v->sprite_output_frame)) < 0)
6157 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6158 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
6160 ff_print_debug_info(s, s->current_picture_ptr, pict);
6161 } else if (s->last_picture_ptr != NULL) {
6162 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
6164 ff_print_debug_info(s, s->last_picture_ptr, pict);
6166 if (s->last_picture_ptr || s->low_delay) {
6173 for (i = 0; i < n_slices; i++)
6174 av_free(slices[i].buf);
6180 for (i = 0; i < n_slices; i++)
6181 av_free(slices[i].buf);
6187 static const AVProfile profiles[] = {
6188 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6189 { FF_PROFILE_VC1_MAIN, "Main" },
6190 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6191 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6192 { FF_PROFILE_UNKNOWN },
6195 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6197 AV_PIX_FMT_DXVA2_VLD,
6200 AV_PIX_FMT_VAAPI_VLD,
6209 AVCodec ff_vc1_decoder = {
6211 .type = AVMEDIA_TYPE_VIDEO,
6212 .id = AV_CODEC_ID_VC1,
6213 .priv_data_size = sizeof(VC1Context),
6214 .init = vc1_decode_init,
6215 .close = ff_vc1_decode_end,
6216 .decode = vc1_decode_frame,
6217 .flush = ff_mpeg_flush,
6218 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6219 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6220 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6221 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6224 #if CONFIG_WMV3_DECODER
6225 AVCodec ff_wmv3_decoder = {
6227 .type = AVMEDIA_TYPE_VIDEO,
6228 .id = AV_CODEC_ID_WMV3,
6229 .priv_data_size = sizeof(VC1Context),
6230 .init = vc1_decode_init,
6231 .close = ff_vc1_decode_end,
6232 .decode = vc1_decode_frame,
6233 .flush = ff_mpeg_flush,
6234 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6235 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6236 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6237 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6241 #if CONFIG_WMV3_VDPAU_DECODER
6242 AVCodec ff_wmv3_vdpau_decoder = {
6243 .name = "wmv3_vdpau",
6244 .type = AVMEDIA_TYPE_VIDEO,
6245 .id = AV_CODEC_ID_WMV3,
6246 .priv_data_size = sizeof(VC1Context),
6247 .init = vc1_decode_init,
6248 .close = ff_vc1_decode_end,
6249 .decode = vc1_decode_frame,
6250 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6251 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6252 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6253 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6257 #if CONFIG_VC1_VDPAU_DECODER
6258 AVCodec ff_vc1_vdpau_decoder = {
6259 .name = "vc1_vdpau",
6260 .type = AVMEDIA_TYPE_VIDEO,
6261 .id = AV_CODEC_ID_VC1,
6262 .priv_data_size = sizeof(VC1Context),
6263 .init = vc1_decode_init,
6264 .close = ff_vc1_decode_end,
6265 .decode = vc1_decode_frame,
6266 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6267 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6268 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6269 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6273 #if CONFIG_WMV3IMAGE_DECODER
6274 AVCodec ff_wmv3image_decoder = {
6275 .name = "wmv3image",
6276 .type = AVMEDIA_TYPE_VIDEO,
6277 .id = AV_CODEC_ID_WMV3IMAGE,
6278 .priv_data_size = sizeof(VC1Context),
6279 .init = vc1_decode_init,
6280 .close = ff_vc1_decode_end,
6281 .decode = vc1_decode_frame,
6282 .capabilities = CODEC_CAP_DR1,
6283 .flush = vc1_sprite_flush,
6284 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6285 .pix_fmts = ff_pixfmt_list_420
6289 #if CONFIG_VC1IMAGE_DECODER
6290 AVCodec ff_vc1image_decoder = {
6292 .type = AVMEDIA_TYPE_VIDEO,
6293 .id = AV_CODEC_ID_VC1IMAGE,
6294 .priv_data_size = sizeof(VC1Context),
6295 .init = vc1_decode_init,
6296 .close = ff_vc1_decode_end,
6297 .decode = vc1_decode_frame,
6298 .capabilities = CODEC_CAP_DR1,
6299 .flush = vc1_sprite_flush,
6300 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6301 .pix_fmts = ff_pixfmt_list_420