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] + uvsrc_y * s->uvlinesize + uvsrc_x;
1005 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1006 lutuv = v->next_lutuv;
1007 use_ic = v->next_use_ic;
1009 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1010 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1011 lutuv = v->last_lutuv;
1012 use_ic = v->last_use_ic;
1014 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1015 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1017 if (fieldmv && !(uvsrc_y & 1))
1018 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1020 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1023 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1024 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1025 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1026 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1027 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1028 s->h_edge_pos >> 1, v_edge_pos);
1029 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1030 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1031 s->h_edge_pos >> 1, v_edge_pos);
1032 srcU = s->edge_emu_buffer;
1033 srcV = s->edge_emu_buffer + 16;
1035 /* if we deal with intensity compensation we need to scale source blocks */
1038 uint8_t *src, *src2;
1042 for (j = 0; j < 5; j++) {
1043 int f = (uvsrc_y + (j << fieldmv)) & 1;
1044 for (i = 0; i < 5; i++) {
1045 src[i] = lutuv[f][src[i]];
1046 src2[i] = lutuv[f][src2[i]];
1048 src += s->uvlinesize << fieldmv;
1049 src2 += s->uvlinesize << fieldmv;
1055 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1056 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1058 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]);
1059 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]);
1063 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1064 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1066 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]);
1067 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]);
1073 /***********************************************************************/
1075 * @name VC-1 Block-level functions
1076 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1082 * @brief Get macroblock-level quantizer scale
1084 #define GET_MQUANT() \
1085 if (v->dquantfrm) { \
1087 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1088 if (v->dqbilevel) { \
1089 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1091 mqdiff = get_bits(gb, 3); \
1093 mquant = v->pq + mqdiff; \
1095 mquant = get_bits(gb, 5); \
1098 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1099 edges = 1 << v->dqsbedge; \
1100 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1101 edges = (3 << v->dqsbedge) % 15; \
1102 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1104 if ((edges&1) && !s->mb_x) \
1105 mquant = v->altpq; \
1106 if ((edges&2) && s->first_slice_line) \
1107 mquant = v->altpq; \
1108 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1109 mquant = v->altpq; \
1110 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1111 mquant = v->altpq; \
1112 if (!mquant || mquant > 31) { \
1113 av_log(v->s.avctx, AV_LOG_ERROR, \
1114 "Overriding invalid mquant %d\n", mquant); \
1120 * @def GET_MVDATA(_dmv_x, _dmv_y)
1121 * @brief Get MV differentials
1122 * @see MVDATA decoding from 8.3.5.2, p(1)20
1123 * @param _dmv_x Horizontal differential for decoded MV
1124 * @param _dmv_y Vertical differential for decoded MV
1126 #define GET_MVDATA(_dmv_x, _dmv_y) \
1127 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1128 VC1_MV_DIFF_VLC_BITS, 2); \
1130 mb_has_coeffs = 1; \
1133 mb_has_coeffs = 0; \
1136 _dmv_x = _dmv_y = 0; \
1137 } else if (index == 35) { \
1138 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1139 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1140 } else if (index == 36) { \
1145 index1 = index % 6; \
1146 if (!s->quarter_sample && index1 == 5) val = 1; \
1148 if (size_table[index1] - val > 0) \
1149 val = get_bits(gb, size_table[index1] - val); \
1151 sign = 0 - (val&1); \
1152 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1154 index1 = index / 6; \
1155 if (!s->quarter_sample && index1 == 5) val = 1; \
1157 if (size_table[index1] - val > 0) \
1158 val = get_bits(gb, size_table[index1] - val); \
1160 sign = 0 - (val & 1); \
1161 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1164 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1165 int *dmv_y, int *pred_flag)
1168 int extend_x = 0, extend_y = 0;
1169 GetBitContext *gb = &v->s.gb;
1172 const int* offs_tab;
1175 bits = VC1_2REF_MVDATA_VLC_BITS;
1178 bits = VC1_1REF_MVDATA_VLC_BITS;
1181 switch (v->dmvrange) {
1189 extend_x = extend_y = 1;
1192 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1194 *dmv_x = get_bits(gb, v->k_x);
1195 *dmv_y = get_bits(gb, v->k_y);
1198 *pred_flag = *dmv_y & 1;
1199 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1201 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1206 av_assert0(index < esc);
1208 offs_tab = offset_table2;
1210 offs_tab = offset_table1;
1211 index1 = (index + 1) % 9;
1213 val = get_bits(gb, index1 + extend_x);
1214 sign = 0 -(val & 1);
1215 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1219 offs_tab = offset_table2;
1221 offs_tab = offset_table1;
1222 index1 = (index + 1) / 9;
1223 if (index1 > v->numref) {
1224 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1225 sign = 0 - (val & 1);
1226 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1229 if (v->numref && pred_flag)
1230 *pred_flag = index1 & 1;
1234 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1236 int scaledvalue, refdist;
1237 int scalesame1, scalesame2;
1238 int scalezone1_x, zone1offset_x;
1239 int table_index = dir ^ v->second_field;
1241 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1242 refdist = v->refdist;
1244 refdist = dir ? v->brfd : v->frfd;
1247 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1248 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1249 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1250 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1255 if (FFABS(n) < scalezone1_x)
1256 scaledvalue = (n * scalesame1) >> 8;
1259 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1261 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1264 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1267 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1269 int scaledvalue, refdist;
1270 int scalesame1, scalesame2;
1271 int scalezone1_y, zone1offset_y;
1272 int table_index = dir ^ v->second_field;
1274 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1275 refdist = v->refdist;
1277 refdist = dir ? v->brfd : v->frfd;
1280 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1281 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1282 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1283 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1288 if (FFABS(n) < scalezone1_y)
1289 scaledvalue = (n * scalesame1) >> 8;
1292 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1294 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1298 if (v->cur_field_type && !v->ref_field_type[dir])
1299 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1301 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1304 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1306 int scalezone1_x, zone1offset_x;
1307 int scaleopp1, scaleopp2, brfd;
1310 brfd = FFMIN(v->brfd, 3);
1311 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1312 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1313 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1314 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1319 if (FFABS(n) < scalezone1_x)
1320 scaledvalue = (n * scaleopp1) >> 8;
1323 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1325 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1328 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1331 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1333 int scalezone1_y, zone1offset_y;
1334 int scaleopp1, scaleopp2, brfd;
1337 brfd = FFMIN(v->brfd, 3);
1338 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1339 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1340 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1341 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1346 if (FFABS(n) < scalezone1_y)
1347 scaledvalue = (n * scaleopp1) >> 8;
1350 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1352 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1355 if (v->cur_field_type && !v->ref_field_type[dir]) {
1356 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1358 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1362 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1365 int brfd, scalesame;
1366 int hpel = 1 - v->s.quarter_sample;
1369 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1371 n = scaleforsame_y(v, i, n, dir) << hpel;
1373 n = scaleforsame_x(v, n, dir) << hpel;
1376 brfd = FFMIN(v->brfd, 3);
1377 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1379 n = (n * scalesame >> 8) << hpel;
1383 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1386 int refdist, scaleopp;
1387 int hpel = 1 - v->s.quarter_sample;
1390 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1392 n = scaleforopp_y(v, n, dir) << hpel;
1394 n = scaleforopp_x(v, n) << hpel;
1397 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1398 refdist = FFMIN(v->refdist, 3);
1400 refdist = dir ? v->brfd : v->frfd;
1401 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1403 n = (n * scaleopp >> 8) << hpel;
1407 /** Predict and set motion vector
1409 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1410 int mv1, int r_x, int r_y, uint8_t* is_intra,
1411 int pred_flag, int dir)
1413 MpegEncContext *s = &v->s;
1414 int xy, wrap, off = 0;
1418 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1419 int opposite, a_f, b_f, c_f;
1420 int16_t field_predA[2];
1421 int16_t field_predB[2];
1422 int16_t field_predC[2];
1423 int a_valid, b_valid, c_valid;
1424 int hybridmv_thresh, y_bias = 0;
1426 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1427 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1431 /* scale MV difference to be quad-pel */
1432 dmv_x <<= 1 - s->quarter_sample;
1433 dmv_y <<= 1 - s->quarter_sample;
1435 wrap = s->b8_stride;
1436 xy = s->block_index[n];
1439 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1440 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1441 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1442 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1443 if (mv1) { /* duplicate motion data for 1-MV block */
1444 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1445 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1446 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1447 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1448 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1449 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1450 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1451 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1452 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1453 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1454 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1455 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1456 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1461 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1462 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1464 if (v->field_mode && mixedmv_pic)
1465 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1467 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1469 //in 4-MV mode different blocks have different B predictor position
1472 off = (s->mb_x > 0) ? -1 : 1;
1475 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1484 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1486 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1487 b_valid = a_valid && (s->mb_width > 1);
1488 c_valid = s->mb_x || (n == 1 || n == 3);
1489 if (v->field_mode) {
1490 a_valid = a_valid && !is_intra[xy - wrap];
1491 b_valid = b_valid && !is_intra[xy - wrap + off];
1492 c_valid = c_valid && !is_intra[xy - 1];
1496 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1497 num_oppfield += a_f;
1498 num_samefield += 1 - a_f;
1499 field_predA[0] = A[0];
1500 field_predA[1] = A[1];
1502 field_predA[0] = field_predA[1] = 0;
1506 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1507 num_oppfield += b_f;
1508 num_samefield += 1 - b_f;
1509 field_predB[0] = B[0];
1510 field_predB[1] = B[1];
1512 field_predB[0] = field_predB[1] = 0;
1516 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1517 num_oppfield += c_f;
1518 num_samefield += 1 - c_f;
1519 field_predC[0] = C[0];
1520 field_predC[1] = C[1];
1522 field_predC[0] = field_predC[1] = 0;
1526 if (v->field_mode) {
1528 // REFFIELD determines if the last field or the second-last field is
1529 // to be used as reference
1530 opposite = 1 - v->reffield;
1532 if (num_samefield <= num_oppfield)
1533 opposite = 1 - pred_flag;
1535 opposite = pred_flag;
1540 if (a_valid && !a_f) {
1541 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1542 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1544 if (b_valid && !b_f) {
1545 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1546 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1548 if (c_valid && !c_f) {
1549 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1550 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1552 v->mv_f[dir][xy + v->blocks_off] = 1;
1553 v->ref_field_type[dir] = !v->cur_field_type;
1555 if (a_valid && a_f) {
1556 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1557 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1559 if (b_valid && b_f) {
1560 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1561 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1563 if (c_valid && c_f) {
1564 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1565 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1567 v->mv_f[dir][xy + v->blocks_off] = 0;
1568 v->ref_field_type[dir] = v->cur_field_type;
1572 px = field_predA[0];
1573 py = field_predA[1];
1574 } else if (c_valid) {
1575 px = field_predC[0];
1576 py = field_predC[1];
1577 } else if (b_valid) {
1578 px = field_predB[0];
1579 py = field_predB[1];
1585 if (num_samefield + num_oppfield > 1) {
1586 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1587 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1590 /* Pullback MV as specified in 8.3.5.3.4 */
1591 if (!v->field_mode) {
1593 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1594 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1595 X = (s->mb_width << 6) - 4;
1596 Y = (s->mb_height << 6) - 4;
1598 if (qx + px < -60) px = -60 - qx;
1599 if (qy + py < -60) py = -60 - qy;
1601 if (qx + px < -28) px = -28 - qx;
1602 if (qy + py < -28) py = -28 - qy;
1604 if (qx + px > X) px = X - qx;
1605 if (qy + py > Y) py = Y - qy;
1608 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1609 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1610 hybridmv_thresh = 32;
1611 if (a_valid && c_valid) {
1612 if (is_intra[xy - wrap])
1613 sum = FFABS(px) + FFABS(py);
1615 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1616 if (sum > hybridmv_thresh) {
1617 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1618 px = field_predA[0];
1619 py = field_predA[1];
1621 px = field_predC[0];
1622 py = field_predC[1];
1625 if (is_intra[xy - 1])
1626 sum = FFABS(px) + FFABS(py);
1628 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1629 if (sum > hybridmv_thresh) {
1630 if (get_bits1(&s->gb)) {
1631 px = field_predA[0];
1632 py = field_predA[1];
1634 px = field_predC[0];
1635 py = field_predC[1];
1642 if (v->field_mode && v->numref)
1644 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1646 /* store MV using signed modulus of MV range defined in 4.11 */
1647 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;
1648 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;
1649 if (mv1) { /* duplicate motion data for 1-MV block */
1650 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1651 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1652 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1653 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1654 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1655 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1656 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1657 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];
1661 /** Predict and set motion vector for interlaced frame picture MBs
1663 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1664 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1666 MpegEncContext *s = &v->s;
1667 int xy, wrap, off = 0;
1668 int A[2], B[2], C[2];
1670 int a_valid = 0, b_valid = 0, c_valid = 0;
1671 int field_a, field_b, field_c; // 0: same, 1: opposit
1672 int total_valid, num_samefield, num_oppfield;
1673 int pos_c, pos_b, n_adj;
1675 wrap = s->b8_stride;
1676 xy = s->block_index[n];
1679 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1680 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1681 s->current_picture.motion_val[1][xy][0] = 0;
1682 s->current_picture.motion_val[1][xy][1] = 0;
1683 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1684 s->current_picture.motion_val[0][xy + 1][0] = 0;
1685 s->current_picture.motion_val[0][xy + 1][1] = 0;
1686 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1687 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1688 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1689 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1690 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1691 s->current_picture.motion_val[1][xy + 1][0] = 0;
1692 s->current_picture.motion_val[1][xy + 1][1] = 0;
1693 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1694 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1695 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1696 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1701 off = ((n == 0) || (n == 1)) ? 1 : -1;
1703 if (s->mb_x || (n == 1) || (n == 3)) {
1704 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1705 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1706 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1707 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1709 } else { // current block has frame mv and cand. has field MV (so average)
1710 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1711 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1712 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1713 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1716 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1722 /* Predict B and C */
1723 B[0] = B[1] = C[0] = C[1] = 0;
1724 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1725 if (!s->first_slice_line) {
1726 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1729 pos_b = s->block_index[n_adj] - 2 * wrap;
1730 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1731 n_adj = (n & 2) | (n & 1);
1733 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1734 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1735 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1736 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1737 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1740 if (s->mb_width > 1) {
1741 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1744 pos_c = s->block_index[2] - 2 * wrap + 2;
1745 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1748 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1749 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1750 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1751 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1752 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1754 if (s->mb_x == s->mb_width - 1) {
1755 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1758 pos_c = s->block_index[3] - 2 * wrap - 2;
1759 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1762 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1763 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1764 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1765 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1766 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1775 pos_b = s->block_index[1];
1777 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1778 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1779 pos_c = s->block_index[0];
1781 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1782 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1785 total_valid = a_valid + b_valid + c_valid;
1786 // check if predictor A is out of bounds
1787 if (!s->mb_x && !(n == 1 || n == 3)) {
1790 // check if predictor B is out of bounds
1791 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1792 B[0] = B[1] = C[0] = C[1] = 0;
1794 if (!v->blk_mv_type[xy]) {
1795 if (s->mb_width == 1) {
1799 if (total_valid >= 2) {
1800 px = mid_pred(A[0], B[0], C[0]);
1801 py = mid_pred(A[1], B[1], C[1]);
1802 } else if (total_valid) {
1803 if (a_valid) { px = A[0]; py = A[1]; }
1804 else if (b_valid) { px = B[0]; py = B[1]; }
1805 else if (c_valid) { px = C[0]; py = C[1]; }
1811 field_a = (A[1] & 4) ? 1 : 0;
1815 field_b = (B[1] & 4) ? 1 : 0;
1819 field_c = (C[1] & 4) ? 1 : 0;
1823 num_oppfield = field_a + field_b + field_c;
1824 num_samefield = total_valid - num_oppfield;
1825 if (total_valid == 3) {
1826 if ((num_samefield == 3) || (num_oppfield == 3)) {
1827 px = mid_pred(A[0], B[0], C[0]);
1828 py = mid_pred(A[1], B[1], C[1]);
1829 } else if (num_samefield >= num_oppfield) {
1830 /* take one MV from same field set depending on priority
1831 the check for B may not be necessary */
1832 px = !field_a ? A[0] : B[0];
1833 py = !field_a ? A[1] : B[1];
1835 px = field_a ? A[0] : B[0];
1836 py = field_a ? A[1] : B[1];
1838 } else if (total_valid == 2) {
1839 if (num_samefield >= num_oppfield) {
1840 if (!field_a && a_valid) {
1843 } else if (!field_b && b_valid) {
1846 } else /*if (c_valid)*/ {
1847 av_assert1(c_valid);
1850 } /*else px = py = 0;*/
1852 if (field_a && a_valid) {
1855 } else /*if (field_b && b_valid)*/ {
1856 av_assert1(field_b && b_valid);
1859 } /*else if (c_valid) {
1864 } else if (total_valid == 1) {
1865 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1866 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1870 /* store MV using signed modulus of MV range defined in 4.11 */
1871 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1872 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1873 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1874 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1875 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1876 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1877 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1878 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1879 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1880 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1881 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1882 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1883 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1884 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1888 /** Motion compensation for direct or interpolated blocks in B-frames
1890 static void vc1_interp_mc(VC1Context *v)
1892 MpegEncContext *s = &v->s;
1893 H264ChromaContext *h264chroma = &v->h264chroma;
1894 uint8_t *srcY, *srcU, *srcV;
1895 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1897 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1898 int use_ic = v->next_use_ic;
1900 if (!v->field_mode && !v->s.next_picture.f.data[0])
1903 mx = s->mv[1][0][0];
1904 my = s->mv[1][0][1];
1905 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1906 uvmy = (my + ((my & 3) == 3)) >> 1;
1907 if (v->field_mode) {
1908 if (v->cur_field_type != v->ref_field_type[1])
1909 my = my - 2 + 4 * v->cur_field_type;
1910 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1913 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1914 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1916 srcY = s->next_picture.f.data[0];
1917 srcU = s->next_picture.f.data[1];
1918 srcV = s->next_picture.f.data[2];
1920 src_x = s->mb_x * 16 + (mx >> 2);
1921 src_y = s->mb_y * 16 + (my >> 2);
1922 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1923 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1925 if (v->profile != PROFILE_ADVANCED) {
1926 src_x = av_clip( src_x, -16, s->mb_width * 16);
1927 src_y = av_clip( src_y, -16, s->mb_height * 16);
1928 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1929 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1931 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1932 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1933 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1934 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1937 srcY += src_y * s->linesize + src_x;
1938 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1939 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1941 if (v->field_mode && v->ref_field_type[1]) {
1942 srcY += s->current_picture_ptr->f.linesize[0];
1943 srcU += s->current_picture_ptr->f.linesize[1];
1944 srcV += s->current_picture_ptr->f.linesize[2];
1947 /* for grayscale we should not try to read from unknown area */
1948 if (s->flags & CODEC_FLAG_GRAY) {
1949 srcU = s->edge_emu_buffer + 18 * s->linesize;
1950 srcV = s->edge_emu_buffer + 18 * s->linesize;
1953 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1954 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1955 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1956 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1958 srcY -= s->mspel * (1 + s->linesize);
1959 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1960 17 + s->mspel * 2, 17 + s->mspel * 2,
1961 src_x - s->mspel, src_y - s->mspel,
1962 s->h_edge_pos, v_edge_pos);
1963 srcY = s->edge_emu_buffer;
1964 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1965 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1966 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1967 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1970 /* if we deal with range reduction we need to scale source blocks */
1971 if (v->rangeredfrm) {
1973 uint8_t *src, *src2;
1976 for (j = 0; j < 17 + s->mspel * 2; j++) {
1977 for (i = 0; i < 17 + s->mspel * 2; i++)
1978 src[i] = ((src[i] - 128) >> 1) + 128;
1983 for (j = 0; j < 9; j++) {
1984 for (i = 0; i < 9; i++) {
1985 src[i] = ((src[i] - 128) >> 1) + 128;
1986 src2[i] = ((src2[i] - 128) >> 1) + 128;
1988 src += s->uvlinesize;
1989 src2 += s->uvlinesize;
1994 uint8_t (*luty )[256] = v->next_luty;
1995 uint8_t (*lutuv)[256] = v->next_lutuv;
1997 uint8_t *src, *src2;
2000 for (j = 0; j < 17 + s->mspel * 2; j++) {
2001 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2002 for (i = 0; i < 17 + s->mspel * 2; i++)
2003 src[i] = luty[f][src[i]];
2008 for (j = 0; j < 9; j++) {
2009 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2010 for (i = 0; i < 9; i++) {
2011 src[i] = lutuv[f][src[i]];
2012 src2[i] = lutuv[f][src2[i]];
2014 src += s->uvlinesize;
2015 src2 += s->uvlinesize;
2018 srcY += s->mspel * (1 + s->linesize);
2025 dxy = ((my & 3) << 2) | (mx & 3);
2026 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2027 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2028 srcY += s->linesize * 8;
2029 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2030 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2032 dxy = (my & 2) | ((mx & 2) >> 1);
2035 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2037 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2040 if (s->flags & CODEC_FLAG_GRAY) return;
2041 /* Chroma MC always uses qpel blilinear */
2042 uvmx = (uvmx & 3) << 1;
2043 uvmy = (uvmy & 3) << 1;
2045 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2046 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2048 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2049 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2053 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2057 #if B_FRACTION_DEN==256
2061 return 2 * ((value * n + 255) >> 9);
2062 return (value * n + 128) >> 8;
2065 n -= B_FRACTION_DEN;
2067 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2068 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2072 /** Reconstruct motion vector for B-frame and do motion compensation
2074 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2075 int direct, int mode)
2082 if (mode == BMV_TYPE_INTERPOLATED) {
2088 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2091 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2092 int direct, int mvtype)
2094 MpegEncContext *s = &v->s;
2095 int xy, wrap, off = 0;
2100 const uint8_t *is_intra = v->mb_type[0];
2104 /* scale MV difference to be quad-pel */
2105 dmv_x[0] <<= 1 - s->quarter_sample;
2106 dmv_y[0] <<= 1 - s->quarter_sample;
2107 dmv_x[1] <<= 1 - s->quarter_sample;
2108 dmv_y[1] <<= 1 - s->quarter_sample;
2110 wrap = s->b8_stride;
2111 xy = s->block_index[0];
2114 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2115 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2116 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2117 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2120 if (!v->field_mode) {
2121 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2122 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2123 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2124 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2126 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2127 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));
2128 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));
2129 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));
2130 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));
2133 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2134 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2135 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2136 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2140 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2141 C = s->current_picture.motion_val[0][xy - 2];
2142 A = s->current_picture.motion_val[0][xy - wrap * 2];
2143 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2144 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2146 if (!s->mb_x) C[0] = C[1] = 0;
2147 if (!s->first_slice_line) { // predictor A is not out of bounds
2148 if (s->mb_width == 1) {
2152 px = mid_pred(A[0], B[0], C[0]);
2153 py = mid_pred(A[1], B[1], C[1]);
2155 } else if (s->mb_x) { // predictor C is not out of bounds
2161 /* Pullback MV as specified in 8.3.5.3.4 */
2164 if (v->profile < PROFILE_ADVANCED) {
2165 qx = (s->mb_x << 5);
2166 qy = (s->mb_y << 5);
2167 X = (s->mb_width << 5) - 4;
2168 Y = (s->mb_height << 5) - 4;
2169 if (qx + px < -28) px = -28 - qx;
2170 if (qy + py < -28) py = -28 - qy;
2171 if (qx + px > X) px = X - qx;
2172 if (qy + py > Y) py = Y - qy;
2174 qx = (s->mb_x << 6);
2175 qy = (s->mb_y << 6);
2176 X = (s->mb_width << 6) - 4;
2177 Y = (s->mb_height << 6) - 4;
2178 if (qx + px < -60) px = -60 - qx;
2179 if (qy + py < -60) py = -60 - qy;
2180 if (qx + px > X) px = X - qx;
2181 if (qy + py > Y) py = Y - qy;
2184 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2185 if (0 && !s->first_slice_line && s->mb_x) {
2186 if (is_intra[xy - wrap])
2187 sum = FFABS(px) + FFABS(py);
2189 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2191 if (get_bits1(&s->gb)) {
2199 if (is_intra[xy - 2])
2200 sum = FFABS(px) + FFABS(py);
2202 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2204 if (get_bits1(&s->gb)) {
2214 /* store MV using signed modulus of MV range defined in 4.11 */
2215 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2216 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2218 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2219 C = s->current_picture.motion_val[1][xy - 2];
2220 A = s->current_picture.motion_val[1][xy - wrap * 2];
2221 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2222 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2226 if (!s->first_slice_line) { // predictor A is not out of bounds
2227 if (s->mb_width == 1) {
2231 px = mid_pred(A[0], B[0], C[0]);
2232 py = mid_pred(A[1], B[1], C[1]);
2234 } else if (s->mb_x) { // predictor C is not out of bounds
2240 /* Pullback MV as specified in 8.3.5.3.4 */
2243 if (v->profile < PROFILE_ADVANCED) {
2244 qx = (s->mb_x << 5);
2245 qy = (s->mb_y << 5);
2246 X = (s->mb_width << 5) - 4;
2247 Y = (s->mb_height << 5) - 4;
2248 if (qx + px < -28) px = -28 - qx;
2249 if (qy + py < -28) py = -28 - qy;
2250 if (qx + px > X) px = X - qx;
2251 if (qy + py > Y) py = Y - qy;
2253 qx = (s->mb_x << 6);
2254 qy = (s->mb_y << 6);
2255 X = (s->mb_width << 6) - 4;
2256 Y = (s->mb_height << 6) - 4;
2257 if (qx + px < -60) px = -60 - qx;
2258 if (qy + py < -60) py = -60 - qy;
2259 if (qx + px > X) px = X - qx;
2260 if (qy + py > Y) py = Y - qy;
2263 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2264 if (0 && !s->first_slice_line && s->mb_x) {
2265 if (is_intra[xy - wrap])
2266 sum = FFABS(px) + FFABS(py);
2268 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2270 if (get_bits1(&s->gb)) {
2278 if (is_intra[xy - 2])
2279 sum = FFABS(px) + FFABS(py);
2281 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2283 if (get_bits1(&s->gb)) {
2293 /* store MV using signed modulus of MV range defined in 4.11 */
2295 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2296 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2298 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2299 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2300 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2301 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2304 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2306 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2307 MpegEncContext *s = &v->s;
2308 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2310 if (v->bmvtype == BMV_TYPE_DIRECT) {
2311 int total_opp, k, f;
2312 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2313 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2314 v->bfraction, 0, s->quarter_sample);
2315 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2316 v->bfraction, 0, s->quarter_sample);
2317 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2318 v->bfraction, 1, s->quarter_sample);
2319 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2320 v->bfraction, 1, s->quarter_sample);
2322 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2323 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2324 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2325 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2326 f = (total_opp > 2) ? 1 : 0;
2328 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2329 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2332 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2333 for (k = 0; k < 4; k++) {
2334 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2335 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2336 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2337 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2338 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2339 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2343 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2344 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);
2345 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);
2348 if (dir) { // backward
2349 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);
2350 if (n == 3 || mv1) {
2351 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2354 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);
2355 if (n == 3 || mv1) {
2356 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2361 /** Get predicted DC value for I-frames only
2362 * prediction dir: left=0, top=1
2363 * @param s MpegEncContext
2364 * @param overlap flag indicating that overlap filtering is used
2365 * @param pq integer part of picture quantizer
2366 * @param[in] n block index in the current MB
2367 * @param dc_val_ptr Pointer to DC predictor
2368 * @param dir_ptr Prediction direction for use in AC prediction
2370 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2371 int16_t **dc_val_ptr, int *dir_ptr)
2373 int a, b, c, wrap, pred, scale;
2375 static const uint16_t dcpred[32] = {
2376 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2377 114, 102, 93, 85, 79, 73, 68, 64,
2378 60, 57, 54, 51, 49, 47, 45, 43,
2379 41, 39, 38, 37, 35, 34, 33
2382 /* find prediction - wmv3_dc_scale always used here in fact */
2383 if (n < 4) scale = s->y_dc_scale;
2384 else scale = s->c_dc_scale;
2386 wrap = s->block_wrap[n];
2387 dc_val = s->dc_val[0] + s->block_index[n];
2393 b = dc_val[ - 1 - wrap];
2394 a = dc_val[ - wrap];
2396 if (pq < 9 || !overlap) {
2397 /* Set outer values */
2398 if (s->first_slice_line && (n != 2 && n != 3))
2399 b = a = dcpred[scale];
2400 if (s->mb_x == 0 && (n != 1 && n != 3))
2401 b = c = dcpred[scale];
2403 /* Set outer values */
2404 if (s->first_slice_line && (n != 2 && n != 3))
2406 if (s->mb_x == 0 && (n != 1 && n != 3))
2410 if (abs(a - b) <= abs(b - c)) {
2412 *dir_ptr = 1; // left
2415 *dir_ptr = 0; // top
2418 /* update predictor */
2419 *dc_val_ptr = &dc_val[0];
2424 /** Get predicted DC value
2425 * prediction dir: left=0, top=1
2426 * @param s MpegEncContext
2427 * @param overlap flag indicating that overlap filtering is used
2428 * @param pq integer part of picture quantizer
2429 * @param[in] n block index in the current MB
2430 * @param a_avail flag indicating top block availability
2431 * @param c_avail flag indicating left block availability
2432 * @param dc_val_ptr Pointer to DC predictor
2433 * @param dir_ptr Prediction direction for use in AC prediction
2435 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2436 int a_avail, int c_avail,
2437 int16_t **dc_val_ptr, int *dir_ptr)
2439 int a, b, c, wrap, pred;
2441 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2445 wrap = s->block_wrap[n];
2446 dc_val = s->dc_val[0] + s->block_index[n];
2452 b = dc_val[ - 1 - wrap];
2453 a = dc_val[ - wrap];
2454 /* scale predictors if needed */
2455 q1 = s->current_picture.qscale_table[mb_pos];
2456 dqscale_index = s->y_dc_scale_table[q1] - 1;
2457 if (dqscale_index < 0)
2459 if (c_avail && (n != 1 && n != 3)) {
2460 q2 = s->current_picture.qscale_table[mb_pos - 1];
2462 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2464 if (a_avail && (n != 2 && n != 3)) {
2465 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2467 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2469 if (a_avail && c_avail && (n != 3)) {
2474 off -= s->mb_stride;
2475 q2 = s->current_picture.qscale_table[off];
2477 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2480 if (a_avail && c_avail) {
2481 if (abs(a - b) <= abs(b - c)) {
2483 *dir_ptr = 1; // left
2486 *dir_ptr = 0; // top
2488 } else if (a_avail) {
2490 *dir_ptr = 0; // top
2491 } else if (c_avail) {
2493 *dir_ptr = 1; // left
2496 *dir_ptr = 1; // left
2499 /* update predictor */
2500 *dc_val_ptr = &dc_val[0];
2504 /** @} */ // Block group
2507 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2508 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2512 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2513 uint8_t **coded_block_ptr)
2515 int xy, wrap, pred, a, b, c;
2517 xy = s->block_index[n];
2518 wrap = s->b8_stride;
2523 a = s->coded_block[xy - 1 ];
2524 b = s->coded_block[xy - 1 - wrap];
2525 c = s->coded_block[xy - wrap];
2534 *coded_block_ptr = &s->coded_block[xy];
2540 * Decode one AC coefficient
2541 * @param v The VC1 context
2542 * @param last Last coefficient
2543 * @param skip How much zero coefficients to skip
2544 * @param value Decoded AC coefficient value
2545 * @param codingset set of VLC to decode data
2548 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2549 int *value, int codingset)
2551 GetBitContext *gb = &v->s.gb;
2552 int index, escape, run = 0, level = 0, lst = 0;
2554 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2555 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2556 run = vc1_index_decode_table[codingset][index][0];
2557 level = vc1_index_decode_table[codingset][index][1];
2558 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2562 escape = decode210(gb);
2564 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2565 run = vc1_index_decode_table[codingset][index][0];
2566 level = vc1_index_decode_table[codingset][index][1];
2567 lst = index >= vc1_last_decode_table[codingset];
2570 level += vc1_last_delta_level_table[codingset][run];
2572 level += vc1_delta_level_table[codingset][run];
2575 run += vc1_last_delta_run_table[codingset][level] + 1;
2577 run += vc1_delta_run_table[codingset][level] + 1;
2583 lst = get_bits1(gb);
2584 if (v->s.esc3_level_length == 0) {
2585 if (v->pq < 8 || v->dquantfrm) { // table 59
2586 v->s.esc3_level_length = get_bits(gb, 3);
2587 if (!v->s.esc3_level_length)
2588 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2589 } else { // table 60
2590 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2592 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2594 run = get_bits(gb, v->s.esc3_run_length);
2595 sign = get_bits1(gb);
2596 level = get_bits(gb, v->s.esc3_level_length);
2607 /** Decode intra block in intra frames - should be faster than decode_intra_block
2608 * @param v VC1Context
2609 * @param block block to decode
2610 * @param[in] n subblock index
2611 * @param coded are AC coeffs present or not
2612 * @param codingset set of VLC to decode data
2614 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2615 int coded, int codingset)
2617 GetBitContext *gb = &v->s.gb;
2618 MpegEncContext *s = &v->s;
2619 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2622 int16_t *ac_val, *ac_val2;
2625 /* Get DC differential */
2627 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2629 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2632 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2636 if (dcdiff == 119 /* ESC index value */) {
2637 /* TODO: Optimize */
2638 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2639 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2640 else dcdiff = get_bits(gb, 8);
2643 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2644 else if (v->pq == 2)
2645 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2652 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2655 /* Store the quantized DC coeff, used for prediction */
2657 block[0] = dcdiff * s->y_dc_scale;
2659 block[0] = dcdiff * s->c_dc_scale;
2670 int last = 0, skip, value;
2671 const uint8_t *zz_table;
2675 scale = v->pq * 2 + v->halfpq;
2679 zz_table = v->zz_8x8[2];
2681 zz_table = v->zz_8x8[3];
2683 zz_table = v->zz_8x8[1];
2685 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2687 if (dc_pred_dir) // left
2690 ac_val -= 16 * s->block_wrap[n];
2693 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2697 block[zz_table[i++]] = value;
2700 /* apply AC prediction if needed */
2702 if (dc_pred_dir) { // left
2703 for (k = 1; k < 8; k++)
2704 block[k << v->left_blk_sh] += ac_val[k];
2706 for (k = 1; k < 8; k++)
2707 block[k << v->top_blk_sh] += ac_val[k + 8];
2710 /* save AC coeffs for further prediction */
2711 for (k = 1; k < 8; k++) {
2712 ac_val2[k] = block[k << v->left_blk_sh];
2713 ac_val2[k + 8] = block[k << v->top_blk_sh];
2716 /* scale AC coeffs */
2717 for (k = 1; k < 64; k++)
2721 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2724 if (s->ac_pred) i = 63;
2730 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2734 scale = v->pq * 2 + v->halfpq;
2735 memset(ac_val2, 0, 16 * 2);
2736 if (dc_pred_dir) { // left
2739 memcpy(ac_val2, ac_val, 8 * 2);
2741 ac_val -= 16 * s->block_wrap[n];
2743 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2746 /* apply AC prediction if needed */
2748 if (dc_pred_dir) { //left
2749 for (k = 1; k < 8; k++) {
2750 block[k << v->left_blk_sh] = ac_val[k] * scale;
2751 if (!v->pquantizer && block[k << v->left_blk_sh])
2752 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2755 for (k = 1; k < 8; k++) {
2756 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2757 if (!v->pquantizer && block[k << v->top_blk_sh])
2758 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2764 s->block_last_index[n] = i;
2769 /** Decode intra block in intra frames - should be faster than decode_intra_block
2770 * @param v VC1Context
2771 * @param block block to decode
2772 * @param[in] n subblock number
2773 * @param coded are AC coeffs present or not
2774 * @param codingset set of VLC to decode data
2775 * @param mquant quantizer value for this macroblock
2777 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2778 int coded, int codingset, int mquant)
2780 GetBitContext *gb = &v->s.gb;
2781 MpegEncContext *s = &v->s;
2782 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2784 int16_t *dc_val = NULL;
2785 int16_t *ac_val, *ac_val2;
2787 int a_avail = v->a_avail, c_avail = v->c_avail;
2788 int use_pred = s->ac_pred;
2791 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2793 /* Get DC differential */
2795 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2797 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2800 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2804 if (dcdiff == 119 /* ESC index value */) {
2805 /* TODO: Optimize */
2806 if (mquant == 1) dcdiff = get_bits(gb, 10);
2807 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2808 else dcdiff = get_bits(gb, 8);
2811 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2812 else if (mquant == 2)
2813 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2820 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2823 /* Store the quantized DC coeff, used for prediction */
2825 block[0] = dcdiff * s->y_dc_scale;
2827 block[0] = dcdiff * s->c_dc_scale;
2833 /* check if AC is needed at all */
2834 if (!a_avail && !c_avail)
2836 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2839 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2841 if (dc_pred_dir) // left
2844 ac_val -= 16 * s->block_wrap[n];
2846 q1 = s->current_picture.qscale_table[mb_pos];
2847 if ( dc_pred_dir && c_avail && mb_pos)
2848 q2 = s->current_picture.qscale_table[mb_pos - 1];
2849 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2850 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2851 if ( dc_pred_dir && n == 1)
2853 if (!dc_pred_dir && n == 2)
2859 int last = 0, skip, value;
2860 const uint8_t *zz_table;
2864 if (!use_pred && v->fcm == ILACE_FRAME) {
2865 zz_table = v->zzi_8x8;
2867 if (!dc_pred_dir) // top
2868 zz_table = v->zz_8x8[2];
2870 zz_table = v->zz_8x8[3];
2873 if (v->fcm != ILACE_FRAME)
2874 zz_table = v->zz_8x8[1];
2876 zz_table = v->zzi_8x8;
2880 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2884 block[zz_table[i++]] = value;
2887 /* apply AC prediction if needed */
2889 /* scale predictors if needed*/
2890 if (q2 && q1 != q2) {
2891 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2892 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2895 return AVERROR_INVALIDDATA;
2896 if (dc_pred_dir) { // left
2897 for (k = 1; k < 8; k++)
2898 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2900 for (k = 1; k < 8; k++)
2901 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2904 if (dc_pred_dir) { //left
2905 for (k = 1; k < 8; k++)
2906 block[k << v->left_blk_sh] += ac_val[k];
2908 for (k = 1; k < 8; k++)
2909 block[k << v->top_blk_sh] += ac_val[k + 8];
2913 /* save AC coeffs for further prediction */
2914 for (k = 1; k < 8; k++) {
2915 ac_val2[k ] = block[k << v->left_blk_sh];
2916 ac_val2[k + 8] = block[k << v->top_blk_sh];
2919 /* scale AC coeffs */
2920 for (k = 1; k < 64; k++)
2924 block[k] += (block[k] < 0) ? -mquant : mquant;
2927 if (use_pred) i = 63;
2928 } else { // no AC coeffs
2931 memset(ac_val2, 0, 16 * 2);
2932 if (dc_pred_dir) { // left
2934 memcpy(ac_val2, ac_val, 8 * 2);
2935 if (q2 && q1 != q2) {
2936 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2937 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2939 return AVERROR_INVALIDDATA;
2940 for (k = 1; k < 8; k++)
2941 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2946 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2947 if (q2 && q1 != q2) {
2948 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2949 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2951 return AVERROR_INVALIDDATA;
2952 for (k = 1; k < 8; k++)
2953 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2958 /* apply AC prediction if needed */
2960 if (dc_pred_dir) { // left
2961 for (k = 1; k < 8; k++) {
2962 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2963 if (!v->pquantizer && block[k << v->left_blk_sh])
2964 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2967 for (k = 1; k < 8; k++) {
2968 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2969 if (!v->pquantizer && block[k << v->top_blk_sh])
2970 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2976 s->block_last_index[n] = i;
2981 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2982 * @param v VC1Context
2983 * @param block block to decode
2984 * @param[in] n subblock index
2985 * @param coded are AC coeffs present or not
2986 * @param mquant block quantizer
2987 * @param codingset set of VLC to decode data
2989 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2990 int coded, int mquant, int codingset)
2992 GetBitContext *gb = &v->s.gb;
2993 MpegEncContext *s = &v->s;
2994 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2996 int16_t *dc_val = NULL;
2997 int16_t *ac_val, *ac_val2;
2999 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3000 int a_avail = v->a_avail, c_avail = v->c_avail;
3001 int use_pred = s->ac_pred;
3005 s->dsp.clear_block(block);
3007 /* XXX: Guard against dumb values of mquant */
3008 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3010 /* Set DC scale - y and c use the same */
3011 s->y_dc_scale = s->y_dc_scale_table[mquant];
3012 s->c_dc_scale = s->c_dc_scale_table[mquant];
3014 /* Get DC differential */
3016 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3018 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3021 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3025 if (dcdiff == 119 /* ESC index value */) {
3026 /* TODO: Optimize */
3027 if (mquant == 1) dcdiff = get_bits(gb, 10);
3028 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3029 else dcdiff = get_bits(gb, 8);
3032 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3033 else if (mquant == 2)
3034 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3041 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3044 /* Store the quantized DC coeff, used for prediction */
3047 block[0] = dcdiff * s->y_dc_scale;
3049 block[0] = dcdiff * s->c_dc_scale;
3055 /* check if AC is needed at all and adjust direction if needed */
3056 if (!a_avail) dc_pred_dir = 1;
3057 if (!c_avail) dc_pred_dir = 0;
3058 if (!a_avail && !c_avail) use_pred = 0;
3059 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3062 scale = mquant * 2 + v->halfpq;
3064 if (dc_pred_dir) //left
3067 ac_val -= 16 * s->block_wrap[n];
3069 q1 = s->current_picture.qscale_table[mb_pos];
3070 if (dc_pred_dir && c_avail && mb_pos)
3071 q2 = s->current_picture.qscale_table[mb_pos - 1];
3072 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3073 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3074 if ( dc_pred_dir && n == 1)
3076 if (!dc_pred_dir && n == 2)
3078 if (n == 3) q2 = q1;
3081 int last = 0, skip, value;
3085 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3089 if (v->fcm == PROGRESSIVE)
3090 block[v->zz_8x8[0][i++]] = value;
3092 if (use_pred && (v->fcm == ILACE_FRAME)) {
3093 if (!dc_pred_dir) // top
3094 block[v->zz_8x8[2][i++]] = value;
3096 block[v->zz_8x8[3][i++]] = value;
3098 block[v->zzi_8x8[i++]] = value;
3103 /* apply AC prediction if needed */
3105 /* scale predictors if needed*/
3106 if (q2 && q1 != q2) {
3107 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3108 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3111 return AVERROR_INVALIDDATA;
3112 if (dc_pred_dir) { // left
3113 for (k = 1; k < 8; k++)
3114 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3116 for (k = 1; k < 8; k++)
3117 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3120 if (dc_pred_dir) { // left
3121 for (k = 1; k < 8; k++)
3122 block[k << v->left_blk_sh] += ac_val[k];
3124 for (k = 1; k < 8; k++)
3125 block[k << v->top_blk_sh] += ac_val[k + 8];
3129 /* save AC coeffs for further prediction */
3130 for (k = 1; k < 8; k++) {
3131 ac_val2[k ] = block[k << v->left_blk_sh];
3132 ac_val2[k + 8] = block[k << v->top_blk_sh];
3135 /* scale AC coeffs */
3136 for (k = 1; k < 64; k++)
3140 block[k] += (block[k] < 0) ? -mquant : mquant;
3143 if (use_pred) i = 63;
3144 } else { // no AC coeffs
3147 memset(ac_val2, 0, 16 * 2);
3148 if (dc_pred_dir) { // left
3150 memcpy(ac_val2, ac_val, 8 * 2);
3151 if (q2 && q1 != q2) {
3152 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3153 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3155 return AVERROR_INVALIDDATA;
3156 for (k = 1; k < 8; k++)
3157 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3162 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3163 if (q2 && q1 != q2) {
3164 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3165 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3167 return AVERROR_INVALIDDATA;
3168 for (k = 1; k < 8; k++)
3169 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3174 /* apply AC prediction if needed */
3176 if (dc_pred_dir) { // left
3177 for (k = 1; k < 8; k++) {
3178 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3179 if (!v->pquantizer && block[k << v->left_blk_sh])
3180 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3183 for (k = 1; k < 8; k++) {
3184 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3185 if (!v->pquantizer && block[k << v->top_blk_sh])
3186 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3192 s->block_last_index[n] = i;
3199 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3200 int mquant, int ttmb, int first_block,
3201 uint8_t *dst, int linesize, int skip_block,
3204 MpegEncContext *s = &v->s;
3205 GetBitContext *gb = &s->gb;
3208 int scale, off, idx, last, skip, value;
3209 int ttblk = ttmb & 7;
3212 s->dsp.clear_block(block);
3215 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)];
3217 if (ttblk == TT_4X4) {
3218 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3220 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3221 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3222 || (!v->res_rtm_flag && !first_block))) {
3223 subblkpat = decode012(gb);
3225 subblkpat ^= 3; // swap decoded pattern bits
3226 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3228 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3231 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3233 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3234 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3235 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3238 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3239 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3248 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3253 idx = v->zz_8x8[0][i++];
3255 idx = v->zzi_8x8[i++];
3256 block[idx] = value * scale;
3258 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3262 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3264 v->vc1dsp.vc1_inv_trans_8x8(block);
3265 s->dsp.add_pixels_clamped(block, dst, linesize);
3270 pat = ~subblkpat & 0xF;
3271 for (j = 0; j < 4; j++) {
3272 last = subblkpat & (1 << (3 - j));
3274 off = (j & 1) * 4 + (j & 2) * 16;
3276 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3281 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3283 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3284 block[idx + off] = value * scale;
3286 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3288 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3290 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3292 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3297 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3298 for (j = 0; j < 2; j++) {
3299 last = subblkpat & (1 << (1 - j));
3303 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3308 idx = v->zz_8x4[i++] + off;
3310 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3311 block[idx] = value * scale;
3313 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3315 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3317 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3319 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3324 pat = ~(subblkpat * 5) & 0xF;
3325 for (j = 0; j < 2; j++) {
3326 last = subblkpat & (1 << (1 - j));
3330 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3335 idx = v->zz_4x8[i++] + off;
3337 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3338 block[idx] = value * scale;
3340 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3342 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3344 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3346 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3352 *ttmb_out |= ttblk << (n * 4);
3356 /** @} */ // Macroblock group
3358 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3359 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3361 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3363 MpegEncContext *s = &v->s;
3364 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3365 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3366 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3367 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3368 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3371 if (block_num > 3) {
3372 dst = s->dest[block_num - 3];
3374 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3376 if (s->mb_y != s->end_mb_y || block_num < 2) {
3380 if (block_num > 3) {
3381 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3382 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3383 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3384 mv_stride = s->mb_stride;
3386 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3387 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3388 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3389 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3390 mv_stride = s->b8_stride;
3391 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3394 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3395 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3396 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3398 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3400 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3403 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3405 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3410 dst -= 4 * linesize;
3411 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3412 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3413 idx = (block_cbp | (block_cbp >> 2)) & 3;
3415 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3418 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3420 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3425 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3427 MpegEncContext *s = &v->s;
3428 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3429 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3430 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3431 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3432 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3435 if (block_num > 3) {
3436 dst = s->dest[block_num - 3] - 8 * linesize;
3438 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3441 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3444 if (block_num > 3) {
3445 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3446 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3447 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3449 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3450 : (mb_cbp >> ((block_num + 1) * 4));
3451 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3452 : (mb_is_intra >> ((block_num + 1) * 4));
3453 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3455 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3456 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3458 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3460 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3463 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3465 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3471 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3472 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3473 idx = (block_cbp | (block_cbp >> 1)) & 5;
3475 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3478 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3480 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3485 static void vc1_apply_p_loop_filter(VC1Context *v)
3487 MpegEncContext *s = &v->s;
3490 for (i = 0; i < 6; i++) {
3491 vc1_apply_p_v_loop_filter(v, i);
3494 /* V always precedes H, therefore we run H one MB before V;
3495 * at the end of a row, we catch up to complete the row */
3497 for (i = 0; i < 6; i++) {
3498 vc1_apply_p_h_loop_filter(v, i);
3500 if (s->mb_x == s->mb_width - 1) {
3502 ff_update_block_index(s);
3503 for (i = 0; i < 6; i++) {
3504 vc1_apply_p_h_loop_filter(v, i);
3510 /** Decode one P-frame MB
3512 static int vc1_decode_p_mb(VC1Context *v)
3514 MpegEncContext *s = &v->s;
3515 GetBitContext *gb = &s->gb;
3517 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3518 int cbp; /* cbp decoding stuff */
3519 int mqdiff, mquant; /* MB quantization */
3520 int ttmb = v->ttfrm; /* MB Transform type */
3522 int mb_has_coeffs = 1; /* last_flag */
3523 int dmv_x, dmv_y; /* Differential MV components */
3524 int index, index1; /* LUT indexes */
3525 int val, sign; /* temp values */
3526 int first_block = 1;
3528 int skipped, fourmv;
3529 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3531 mquant = v->pq; /* lossy initialization */
3533 if (v->mv_type_is_raw)
3534 fourmv = get_bits1(gb);
3536 fourmv = v->mv_type_mb_plane[mb_pos];
3538 skipped = get_bits1(gb);
3540 skipped = v->s.mbskip_table[mb_pos];
3542 if (!fourmv) { /* 1MV mode */
3544 GET_MVDATA(dmv_x, dmv_y);
3547 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3548 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3550 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3551 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3553 /* FIXME Set DC val for inter block ? */
3554 if (s->mb_intra && !mb_has_coeffs) {
3556 s->ac_pred = get_bits1(gb);
3558 } else if (mb_has_coeffs) {
3560 s->ac_pred = get_bits1(gb);
3561 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3567 s->current_picture.qscale_table[mb_pos] = mquant;
3569 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3570 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3571 VC1_TTMB_VLC_BITS, 2);
3572 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3574 for (i = 0; i < 6; i++) {
3575 s->dc_val[0][s->block_index[i]] = 0;
3577 val = ((cbp >> (5 - i)) & 1);
3578 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3579 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3581 /* check if prediction blocks A and C are available */
3582 v->a_avail = v->c_avail = 0;
3583 if (i == 2 || i == 3 || !s->first_slice_line)
3584 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3585 if (i == 1 || i == 3 || s->mb_x)
3586 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3588 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3589 (i & 4) ? v->codingset2 : v->codingset);
3590 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3592 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3594 for (j = 0; j < 64; j++)
3595 s->block[i][j] <<= 1;
3596 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3597 if (v->pq >= 9 && v->overlap) {
3599 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3601 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3603 block_cbp |= 0xF << (i << 2);
3604 block_intra |= 1 << i;
3606 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3607 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3608 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3609 block_cbp |= pat << (i << 2);
3610 if (!v->ttmbf && ttmb < 8)
3617 for (i = 0; i < 6; i++) {
3618 v->mb_type[0][s->block_index[i]] = 0;
3619 s->dc_val[0][s->block_index[i]] = 0;
3621 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3622 s->current_picture.qscale_table[mb_pos] = 0;
3623 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3626 } else { // 4MV mode
3627 if (!skipped /* unskipped MB */) {
3628 int intra_count = 0, coded_inter = 0;
3629 int is_intra[6], is_coded[6];
3631 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3632 for (i = 0; i < 6; i++) {
3633 val = ((cbp >> (5 - i)) & 1);
3634 s->dc_val[0][s->block_index[i]] = 0;
3641 GET_MVDATA(dmv_x, dmv_y);
3643 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3645 vc1_mc_4mv_luma(v, i, 0, 0);
3646 intra_count += s->mb_intra;
3647 is_intra[i] = s->mb_intra;
3648 is_coded[i] = mb_has_coeffs;
3651 is_intra[i] = (intra_count >= 3);
3655 vc1_mc_4mv_chroma(v, 0);
3656 v->mb_type[0][s->block_index[i]] = is_intra[i];
3658 coded_inter = !is_intra[i] & is_coded[i];
3660 // if there are no coded blocks then don't do anything more
3662 if (!intra_count && !coded_inter)
3665 s->current_picture.qscale_table[mb_pos] = mquant;
3666 /* test if block is intra and has pred */
3669 for (i = 0; i < 6; i++)
3671 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3672 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3678 s->ac_pred = get_bits1(gb);
3682 if (!v->ttmbf && coded_inter)
3683 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3684 for (i = 0; i < 6; i++) {
3686 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3687 s->mb_intra = is_intra[i];
3689 /* check if prediction blocks A and C are available */
3690 v->a_avail = v->c_avail = 0;
3691 if (i == 2 || i == 3 || !s->first_slice_line)
3692 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3693 if (i == 1 || i == 3 || s->mb_x)
3694 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3696 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3697 (i & 4) ? v->codingset2 : v->codingset);
3698 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3700 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3702 for (j = 0; j < 64; j++)
3703 s->block[i][j] <<= 1;
3704 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3705 (i & 4) ? s->uvlinesize : s->linesize);
3706 if (v->pq >= 9 && v->overlap) {
3708 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3710 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3712 block_cbp |= 0xF << (i << 2);
3713 block_intra |= 1 << i;
3714 } else if (is_coded[i]) {
3715 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3716 first_block, s->dest[dst_idx] + off,
3717 (i & 4) ? s->uvlinesize : s->linesize,
3718 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3720 block_cbp |= pat << (i << 2);
3721 if (!v->ttmbf && ttmb < 8)
3726 } else { // skipped MB
3728 s->current_picture.qscale_table[mb_pos] = 0;
3729 for (i = 0; i < 6; i++) {
3730 v->mb_type[0][s->block_index[i]] = 0;
3731 s->dc_val[0][s->block_index[i]] = 0;
3733 for (i = 0; i < 4; i++) {
3734 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3735 vc1_mc_4mv_luma(v, i, 0, 0);
3737 vc1_mc_4mv_chroma(v, 0);
3738 s->current_picture.qscale_table[mb_pos] = 0;
3742 v->cbp[s->mb_x] = block_cbp;
3743 v->ttblk[s->mb_x] = block_tt;
3744 v->is_intra[s->mb_x] = block_intra;
3749 /* Decode one macroblock in an interlaced frame p picture */
3751 static int vc1_decode_p_mb_intfr(VC1Context *v)
3753 MpegEncContext *s = &v->s;
3754 GetBitContext *gb = &s->gb;
3756 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3757 int cbp = 0; /* cbp decoding stuff */
3758 int mqdiff, mquant; /* MB quantization */
3759 int ttmb = v->ttfrm; /* MB Transform type */
3761 int mb_has_coeffs = 1; /* last_flag */
3762 int dmv_x, dmv_y; /* Differential MV components */
3763 int val; /* temp value */
3764 int first_block = 1;
3766 int skipped, fourmv = 0, twomv = 0;
3767 int block_cbp = 0, pat, block_tt = 0;
3768 int idx_mbmode = 0, mvbp;
3769 int stride_y, fieldtx;
3771 mquant = v->pq; /* Lossy initialization */
3774 skipped = get_bits1(gb);
3776 skipped = v->s.mbskip_table[mb_pos];
3778 if (v->fourmvswitch)
3779 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3781 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3782 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3783 /* store the motion vector type in a flag (useful later) */
3784 case MV_PMODE_INTFR_4MV:
3786 v->blk_mv_type[s->block_index[0]] = 0;
3787 v->blk_mv_type[s->block_index[1]] = 0;
3788 v->blk_mv_type[s->block_index[2]] = 0;
3789 v->blk_mv_type[s->block_index[3]] = 0;
3791 case MV_PMODE_INTFR_4MV_FIELD:
3793 v->blk_mv_type[s->block_index[0]] = 1;
3794 v->blk_mv_type[s->block_index[1]] = 1;
3795 v->blk_mv_type[s->block_index[2]] = 1;
3796 v->blk_mv_type[s->block_index[3]] = 1;
3798 case MV_PMODE_INTFR_2MV_FIELD:
3800 v->blk_mv_type[s->block_index[0]] = 1;
3801 v->blk_mv_type[s->block_index[1]] = 1;
3802 v->blk_mv_type[s->block_index[2]] = 1;
3803 v->blk_mv_type[s->block_index[3]] = 1;
3805 case MV_PMODE_INTFR_1MV:
3806 v->blk_mv_type[s->block_index[0]] = 0;
3807 v->blk_mv_type[s->block_index[1]] = 0;
3808 v->blk_mv_type[s->block_index[2]] = 0;
3809 v->blk_mv_type[s->block_index[3]] = 0;
3812 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3813 for (i = 0; i < 4; i++) {
3814 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3815 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3817 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3818 s->mb_intra = v->is_intra[s->mb_x] = 1;
3819 for (i = 0; i < 6; i++)
3820 v->mb_type[0][s->block_index[i]] = 1;
3821 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3822 mb_has_coeffs = get_bits1(gb);
3824 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3825 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3827 s->current_picture.qscale_table[mb_pos] = mquant;
3828 /* Set DC scale - y and c use the same (not sure if necessary here) */
3829 s->y_dc_scale = s->y_dc_scale_table[mquant];
3830 s->c_dc_scale = s->c_dc_scale_table[mquant];
3832 for (i = 0; i < 6; i++) {
3833 s->dc_val[0][s->block_index[i]] = 0;
3835 val = ((cbp >> (5 - i)) & 1);
3836 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3837 v->a_avail = v->c_avail = 0;
3838 if (i == 2 || i == 3 || !s->first_slice_line)
3839 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3840 if (i == 1 || i == 3 || s->mb_x)
3841 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3843 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3844 (i & 4) ? v->codingset2 : v->codingset);
3845 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3846 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3848 stride_y = s->linesize << fieldtx;
3849 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3851 stride_y = s->uvlinesize;
3854 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3858 } else { // inter MB
3859 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3861 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3862 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3863 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3865 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3866 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3867 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3870 s->mb_intra = v->is_intra[s->mb_x] = 0;
3871 for (i = 0; i < 6; i++)
3872 v->mb_type[0][s->block_index[i]] = 0;
3873 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3874 /* for all motion vector read MVDATA and motion compensate each block */
3878 for (i = 0; i < 6; i++) {
3881 val = ((mvbp >> (3 - i)) & 1);
3883 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3885 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3886 vc1_mc_4mv_luma(v, i, 0, 0);
3887 } else if (i == 4) {
3888 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3895 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3897 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3898 vc1_mc_4mv_luma(v, 0, 0, 0);
3899 vc1_mc_4mv_luma(v, 1, 0, 0);
3902 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3904 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3905 vc1_mc_4mv_luma(v, 2, 0, 0);
3906 vc1_mc_4mv_luma(v, 3, 0, 0);
3907 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3909 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3912 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3914 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3918 GET_MQUANT(); // p. 227
3919 s->current_picture.qscale_table[mb_pos] = mquant;
3920 if (!v->ttmbf && cbp)
3921 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3922 for (i = 0; i < 6; i++) {
3923 s->dc_val[0][s->block_index[i]] = 0;
3925 val = ((cbp >> (5 - i)) & 1);
3927 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3929 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3931 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3932 first_block, s->dest[dst_idx] + off,
3933 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3934 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3935 block_cbp |= pat << (i << 2);
3936 if (!v->ttmbf && ttmb < 8)
3943 s->mb_intra = v->is_intra[s->mb_x] = 0;
3944 for (i = 0; i < 6; i++) {
3945 v->mb_type[0][s->block_index[i]] = 0;
3946 s->dc_val[0][s->block_index[i]] = 0;
3948 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3949 s->current_picture.qscale_table[mb_pos] = 0;
3950 v->blk_mv_type[s->block_index[0]] = 0;
3951 v->blk_mv_type[s->block_index[1]] = 0;
3952 v->blk_mv_type[s->block_index[2]] = 0;
3953 v->blk_mv_type[s->block_index[3]] = 0;
3954 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3957 if (s->mb_x == s->mb_width - 1)
3958 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3962 static int vc1_decode_p_mb_intfi(VC1Context *v)
3964 MpegEncContext *s = &v->s;
3965 GetBitContext *gb = &s->gb;
3967 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3968 int cbp = 0; /* cbp decoding stuff */
3969 int mqdiff, mquant; /* MB quantization */
3970 int ttmb = v->ttfrm; /* MB Transform type */
3972 int mb_has_coeffs = 1; /* last_flag */
3973 int dmv_x, dmv_y; /* Differential MV components */
3974 int val; /* temp values */
3975 int first_block = 1;
3978 int block_cbp = 0, pat, block_tt = 0;
3981 mquant = v->pq; /* Lossy initialization */
3983 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3984 if (idx_mbmode <= 1) { // intra MB
3985 s->mb_intra = v->is_intra[s->mb_x] = 1;
3986 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3987 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3988 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3990 s->current_picture.qscale_table[mb_pos] = mquant;
3991 /* Set DC scale - y and c use the same (not sure if necessary here) */
3992 s->y_dc_scale = s->y_dc_scale_table[mquant];
3993 s->c_dc_scale = s->c_dc_scale_table[mquant];
3994 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3995 mb_has_coeffs = idx_mbmode & 1;
3997 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3999 for (i = 0; i < 6; i++) {
4000 s->dc_val[0][s->block_index[i]] = 0;
4001 v->mb_type[0][s->block_index[i]] = 1;
4003 val = ((cbp >> (5 - i)) & 1);
4004 v->a_avail = v->c_avail = 0;
4005 if (i == 2 || i == 3 || !s->first_slice_line)
4006 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4007 if (i == 1 || i == 3 || s->mb_x)
4008 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4010 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4011 (i & 4) ? v->codingset2 : v->codingset);
4012 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4014 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4015 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4016 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4017 // TODO: loop filter
4020 s->mb_intra = v->is_intra[s->mb_x] = 0;
4021 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4022 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4023 if (idx_mbmode <= 5) { // 1-MV
4024 dmv_x = dmv_y = pred_flag = 0;
4025 if (idx_mbmode & 1) {
4026 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4028 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4030 mb_has_coeffs = !(idx_mbmode & 2);
4032 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4033 for (i = 0; i < 6; i++) {
4035 dmv_x = dmv_y = pred_flag = 0;
4036 val = ((v->fourmvbp >> (3 - i)) & 1);
4038 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4040 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4041 vc1_mc_4mv_luma(v, i, 0, 0);
4043 vc1_mc_4mv_chroma(v, 0);
4045 mb_has_coeffs = idx_mbmode & 1;
4048 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4052 s->current_picture.qscale_table[mb_pos] = mquant;
4053 if (!v->ttmbf && cbp) {
4054 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4057 for (i = 0; i < 6; i++) {
4058 s->dc_val[0][s->block_index[i]] = 0;
4060 val = ((cbp >> (5 - i)) & 1);
4061 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4063 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4064 first_block, s->dest[dst_idx] + off,
4065 (i & 4) ? s->uvlinesize : s->linesize,
4066 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4068 block_cbp |= pat << (i << 2);
4069 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4074 if (s->mb_x == s->mb_width - 1)
4075 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4079 /** Decode one B-frame MB (in Main profile)
4081 static void vc1_decode_b_mb(VC1Context *v)
4083 MpegEncContext *s = &v->s;
4084 GetBitContext *gb = &s->gb;
4086 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4087 int cbp = 0; /* cbp decoding stuff */
4088 int mqdiff, mquant; /* MB quantization */
4089 int ttmb = v->ttfrm; /* MB Transform type */
4090 int mb_has_coeffs = 0; /* last_flag */
4091 int index, index1; /* LUT indexes */
4092 int val, sign; /* temp values */
4093 int first_block = 1;
4095 int skipped, direct;
4096 int dmv_x[2], dmv_y[2];
4097 int bmvtype = BMV_TYPE_BACKWARD;
4099 mquant = v->pq; /* lossy initialization */
4103 direct = get_bits1(gb);
4105 direct = v->direct_mb_plane[mb_pos];
4107 skipped = get_bits1(gb);
4109 skipped = v->s.mbskip_table[mb_pos];
4111 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4112 for (i = 0; i < 6; i++) {
4113 v->mb_type[0][s->block_index[i]] = 0;
4114 s->dc_val[0][s->block_index[i]] = 0;
4116 s->current_picture.qscale_table[mb_pos] = 0;
4120 GET_MVDATA(dmv_x[0], dmv_y[0]);
4121 dmv_x[1] = dmv_x[0];
4122 dmv_y[1] = dmv_y[0];
4124 if (skipped || !s->mb_intra) {
4125 bmvtype = decode012(gb);
4128 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4131 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4134 bmvtype = BMV_TYPE_INTERPOLATED;
4135 dmv_x[0] = dmv_y[0] = 0;
4139 for (i = 0; i < 6; i++)
4140 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4144 bmvtype = BMV_TYPE_INTERPOLATED;
4145 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4146 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4150 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4153 s->current_picture.qscale_table[mb_pos] = mquant;
4155 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4156 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4157 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4158 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4160 if (!mb_has_coeffs && !s->mb_intra) {
4161 /* no coded blocks - effectively skipped */
4162 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4163 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4166 if (s->mb_intra && !mb_has_coeffs) {
4168 s->current_picture.qscale_table[mb_pos] = mquant;
4169 s->ac_pred = get_bits1(gb);
4171 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4173 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4174 GET_MVDATA(dmv_x[0], dmv_y[0]);
4175 if (!mb_has_coeffs) {
4176 /* interpolated skipped block */
4177 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4178 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4182 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4184 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4187 s->ac_pred = get_bits1(gb);
4188 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4190 s->current_picture.qscale_table[mb_pos] = mquant;
4191 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4192 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4196 for (i = 0; i < 6; i++) {
4197 s->dc_val[0][s->block_index[i]] = 0;
4199 val = ((cbp >> (5 - i)) & 1);
4200 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4201 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4203 /* check if prediction blocks A and C are available */
4204 v->a_avail = v->c_avail = 0;
4205 if (i == 2 || i == 3 || !s->first_slice_line)
4206 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4207 if (i == 1 || i == 3 || s->mb_x)
4208 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4210 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4211 (i & 4) ? v->codingset2 : v->codingset);
4212 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4214 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4216 for (j = 0; j < 64; j++)
4217 s->block[i][j] <<= 1;
4218 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4220 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4221 first_block, s->dest[dst_idx] + off,
4222 (i & 4) ? s->uvlinesize : s->linesize,
4223 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4224 if (!v->ttmbf && ttmb < 8)
4231 /** Decode one B-frame MB (in interlaced field B picture)
4233 static void vc1_decode_b_mb_intfi(VC1Context *v)
4235 MpegEncContext *s = &v->s;
4236 GetBitContext *gb = &s->gb;
4238 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4239 int cbp = 0; /* cbp decoding stuff */
4240 int mqdiff, mquant; /* MB quantization */
4241 int ttmb = v->ttfrm; /* MB Transform type */
4242 int mb_has_coeffs = 0; /* last_flag */
4243 int val; /* temp value */
4244 int first_block = 1;
4247 int dmv_x[2], dmv_y[2], pred_flag[2];
4248 int bmvtype = BMV_TYPE_BACKWARD;
4251 mquant = v->pq; /* Lossy initialization */
4254 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4255 if (idx_mbmode <= 1) { // intra MB
4256 s->mb_intra = v->is_intra[s->mb_x] = 1;
4257 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4258 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4259 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4261 s->current_picture.qscale_table[mb_pos] = mquant;
4262 /* Set DC scale - y and c use the same (not sure if necessary here) */
4263 s->y_dc_scale = s->y_dc_scale_table[mquant];
4264 s->c_dc_scale = s->c_dc_scale_table[mquant];
4265 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4266 mb_has_coeffs = idx_mbmode & 1;
4268 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4270 for (i = 0; i < 6; i++) {
4271 s->dc_val[0][s->block_index[i]] = 0;
4273 val = ((cbp >> (5 - i)) & 1);
4274 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4275 v->a_avail = v->c_avail = 0;
4276 if (i == 2 || i == 3 || !s->first_slice_line)
4277 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4278 if (i == 1 || i == 3 || s->mb_x)
4279 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4281 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4282 (i & 4) ? v->codingset2 : v->codingset);
4283 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4285 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4287 for (j = 0; j < 64; j++)
4288 s->block[i][j] <<= 1;
4289 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4290 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4291 // TODO: yet to perform loop filter
4294 s->mb_intra = v->is_intra[s->mb_x] = 0;
4295 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4296 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4298 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4300 fwd = v->forward_mb_plane[mb_pos];
4301 if (idx_mbmode <= 5) { // 1-MV
4303 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4304 pred_flag[0] = pred_flag[1] = 0;
4306 bmvtype = BMV_TYPE_FORWARD;
4308 bmvtype = decode012(gb);
4311 bmvtype = BMV_TYPE_BACKWARD;
4314 bmvtype = BMV_TYPE_DIRECT;
4317 bmvtype = BMV_TYPE_INTERPOLATED;
4318 interpmvp = get_bits1(gb);
4321 v->bmvtype = bmvtype;
4322 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4323 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4326 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4328 if (bmvtype == BMV_TYPE_DIRECT) {
4329 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4330 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4332 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4333 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4334 mb_has_coeffs = !(idx_mbmode & 2);
4337 bmvtype = BMV_TYPE_FORWARD;
4338 v->bmvtype = bmvtype;
4339 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4340 for (i = 0; i < 6; i++) {
4342 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4343 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4344 val = ((v->fourmvbp >> (3 - i)) & 1);
4346 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4347 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4348 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4350 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4351 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4353 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4355 mb_has_coeffs = idx_mbmode & 1;
4358 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4362 s->current_picture.qscale_table[mb_pos] = mquant;
4363 if (!v->ttmbf && cbp) {
4364 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4367 for (i = 0; i < 6; i++) {
4368 s->dc_val[0][s->block_index[i]] = 0;
4370 val = ((cbp >> (5 - i)) & 1);
4371 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4373 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4374 first_block, s->dest[dst_idx] + off,
4375 (i & 4) ? s->uvlinesize : s->linesize,
4376 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4377 if (!v->ttmbf && ttmb < 8)
4385 /** Decode one B-frame MB (in interlaced frame B picture)
4387 static int vc1_decode_b_mb_intfr(VC1Context *v)
4389 MpegEncContext *s = &v->s;
4390 GetBitContext *gb = &s->gb;
4392 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4393 int cbp = 0; /* cbp decoding stuff */
4394 int mqdiff, mquant; /* MB quantization */
4395 int ttmb = v->ttfrm; /* MB Transform type */
4396 int mvsw = 0; /* motion vector switch */
4397 int mb_has_coeffs = 1; /* last_flag */
4398 int dmv_x, dmv_y; /* Differential MV components */
4399 int val; /* temp value */
4400 int first_block = 1;
4402 int skipped, direct, twomv = 0;
4403 int block_cbp = 0, pat, block_tt = 0;
4404 int idx_mbmode = 0, mvbp;
4405 int stride_y, fieldtx;
4406 int bmvtype = BMV_TYPE_BACKWARD;
4409 mquant = v->pq; /* Lossy initialization */
4412 skipped = get_bits1(gb);
4414 skipped = v->s.mbskip_table[mb_pos];
4417 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4418 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4420 v->blk_mv_type[s->block_index[0]] = 1;
4421 v->blk_mv_type[s->block_index[1]] = 1;
4422 v->blk_mv_type[s->block_index[2]] = 1;
4423 v->blk_mv_type[s->block_index[3]] = 1;
4425 v->blk_mv_type[s->block_index[0]] = 0;
4426 v->blk_mv_type[s->block_index[1]] = 0;
4427 v->blk_mv_type[s->block_index[2]] = 0;
4428 v->blk_mv_type[s->block_index[3]] = 0;
4433 direct = get_bits1(gb);
4435 direct = v->direct_mb_plane[mb_pos];
4438 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);
4439 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);
4440 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);
4441 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);
4444 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);
4445 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);
4446 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);
4447 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);
4449 for (i = 1; i < 4; i += 2) {
4450 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4451 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4452 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4453 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4456 for (i = 1; i < 4; i++) {
4457 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4458 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4459 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4460 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4465 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4466 for (i = 0; i < 4; i++) {
4467 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4468 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4469 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4470 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4472 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4473 s->mb_intra = v->is_intra[s->mb_x] = 1;
4474 for (i = 0; i < 6; i++)
4475 v->mb_type[0][s->block_index[i]] = 1;
4476 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4477 mb_has_coeffs = get_bits1(gb);
4479 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4480 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4482 s->current_picture.qscale_table[mb_pos] = mquant;
4483 /* Set DC scale - y and c use the same (not sure if necessary here) */
4484 s->y_dc_scale = s->y_dc_scale_table[mquant];
4485 s->c_dc_scale = s->c_dc_scale_table[mquant];
4487 for (i = 0; i < 6; i++) {
4488 s->dc_val[0][s->block_index[i]] = 0;
4490 val = ((cbp >> (5 - i)) & 1);
4491 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4492 v->a_avail = v->c_avail = 0;
4493 if (i == 2 || i == 3 || !s->first_slice_line)
4494 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4495 if (i == 1 || i == 3 || s->mb_x)
4496 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4498 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4499 (i & 4) ? v->codingset2 : v->codingset);
4500 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4502 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4504 stride_y = s->linesize << fieldtx;
4505 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4507 stride_y = s->uvlinesize;
4510 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4513 s->mb_intra = v->is_intra[s->mb_x] = 0;
4515 if (skipped || !s->mb_intra) {
4516 bmvtype = decode012(gb);
4519 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4522 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4525 bmvtype = BMV_TYPE_INTERPOLATED;
4529 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4530 mvsw = get_bits1(gb);
4533 if (!skipped) { // inter MB
4534 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4536 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4538 if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
4539 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4540 } else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
4541 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4545 for (i = 0; i < 6; i++)
4546 v->mb_type[0][s->block_index[i]] = 0;
4547 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4548 /* for all motion vector read MVDATA and motion compensate each block */
4552 for (i = 0; i < 4; i++) {
4553 vc1_mc_4mv_luma(v, i, 0, 0);
4554 vc1_mc_4mv_luma(v, i, 1, 1);
4556 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4557 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4562 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4564 for (i = 0; i < 4; i++) {
4567 val = ((mvbp >> (3 - i)) & 1);
4569 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4571 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4572 vc1_mc_4mv_luma(v, j, dir, dir);
4573 vc1_mc_4mv_luma(v, j+1, dir, dir);
4576 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4577 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4578 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4582 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4584 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4589 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4591 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4594 dir = bmvtype == BMV_TYPE_BACKWARD;
4601 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4602 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4606 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4607 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4610 for (i = 0; i < 2; i++) {
4611 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];
4612 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];
4613 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];
4614 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];
4617 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4618 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4621 vc1_mc_4mv_luma(v, 0, dir, 0);
4622 vc1_mc_4mv_luma(v, 1, dir, 0);
4623 vc1_mc_4mv_luma(v, 2, dir2, 0);
4624 vc1_mc_4mv_luma(v, 3, dir2, 0);
4625 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4627 dir = bmvtype == BMV_TYPE_BACKWARD;
4629 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4632 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4634 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4635 v->blk_mv_type[s->block_index[0]] = 1;
4636 v->blk_mv_type[s->block_index[1]] = 1;
4637 v->blk_mv_type[s->block_index[2]] = 1;
4638 v->blk_mv_type[s->block_index[3]] = 1;
4639 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4640 for (i = 0; i < 2; i++) {
4641 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];
4642 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];
4648 GET_MQUANT(); // p. 227
4649 s->current_picture.qscale_table[mb_pos] = mquant;
4650 if (!v->ttmbf && cbp)
4651 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4652 for (i = 0; i < 6; i++) {
4653 s->dc_val[0][s->block_index[i]] = 0;
4655 val = ((cbp >> (5 - i)) & 1);
4657 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4659 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4661 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4662 first_block, s->dest[dst_idx] + off,
4663 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4664 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4665 block_cbp |= pat << (i << 2);
4666 if (!v->ttmbf && ttmb < 8)
4674 for (i = 0; i < 6; i++) {
4675 v->mb_type[0][s->block_index[i]] = 0;
4676 s->dc_val[0][s->block_index[i]] = 0;
4678 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4679 s->current_picture.qscale_table[mb_pos] = 0;
4680 v->blk_mv_type[s->block_index[0]] = 0;
4681 v->blk_mv_type[s->block_index[1]] = 0;
4682 v->blk_mv_type[s->block_index[2]] = 0;
4683 v->blk_mv_type[s->block_index[3]] = 0;
4686 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4687 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4688 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4690 dir = bmvtype == BMV_TYPE_BACKWARD;
4691 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4696 for (i = 0; i < 2; i++) {
4697 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];
4698 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];
4699 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];
4700 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];
4703 v->blk_mv_type[s->block_index[0]] = 1;
4704 v->blk_mv_type[s->block_index[1]] = 1;
4705 v->blk_mv_type[s->block_index[2]] = 1;
4706 v->blk_mv_type[s->block_index[3]] = 1;
4707 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4708 for (i = 0; i < 2; i++) {
4709 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];
4710 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];
4717 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4722 if (s->mb_x == s->mb_width - 1)
4723 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4724 v->cbp[s->mb_x] = block_cbp;
4725 v->ttblk[s->mb_x] = block_tt;
4729 /** Decode blocks of I-frame
4731 static void vc1_decode_i_blocks(VC1Context *v)
4734 MpegEncContext *s = &v->s;
4739 /* select codingmode used for VLC tables selection */
4740 switch (v->y_ac_table_index) {
4742 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4745 v->codingset = CS_HIGH_MOT_INTRA;
4748 v->codingset = CS_MID_RATE_INTRA;
4752 switch (v->c_ac_table_index) {
4754 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4757 v->codingset2 = CS_HIGH_MOT_INTER;
4760 v->codingset2 = CS_MID_RATE_INTER;
4764 /* Set DC scale - y and c use the same */
4765 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4766 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4769 s->mb_x = s->mb_y = 0;
4771 s->first_slice_line = 1;
4772 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4774 init_block_index(v);
4775 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4777 ff_update_block_index(s);
4778 dst[0] = s->dest[0];
4779 dst[1] = dst[0] + 8;
4780 dst[2] = s->dest[0] + s->linesize * 8;
4781 dst[3] = dst[2] + 8;
4782 dst[4] = s->dest[1];
4783 dst[5] = s->dest[2];
4784 s->dsp.clear_blocks(s->block[0]);
4785 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4786 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4787 s->current_picture.qscale_table[mb_pos] = v->pq;
4788 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4789 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4791 // do actual MB decoding and displaying
4792 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4793 v->s.ac_pred = get_bits1(&v->s.gb);
4795 for (k = 0; k < 6; k++) {
4796 val = ((cbp >> (5 - k)) & 1);
4799 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4803 cbp |= val << (5 - k);
4805 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4807 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4809 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4810 if (v->pq >= 9 && v->overlap) {
4812 for (j = 0; j < 64; j++)
4813 s->block[k][j] <<= 1;
4814 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4817 for (j = 0; j < 64; j++)
4818 s->block[k][j] = (s->block[k][j] - 64) << 1;
4819 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4823 if (v->pq >= 9 && v->overlap) {
4825 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4826 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4827 if (!(s->flags & CODEC_FLAG_GRAY)) {
4828 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4829 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4832 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4833 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4834 if (!s->first_slice_line) {
4835 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4836 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4837 if (!(s->flags & CODEC_FLAG_GRAY)) {
4838 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4839 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4842 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4843 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4845 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4847 if (get_bits_count(&s->gb) > v->bits) {
4848 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4849 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4850 get_bits_count(&s->gb), v->bits);
4854 if (!v->s.loop_filter)
4855 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4857 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4859 s->first_slice_line = 0;
4861 if (v->s.loop_filter)
4862 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4864 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4865 * profile, these only differ are when decoding MSS2 rectangles. */
4866 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4869 /** Decode blocks of I-frame for advanced profile
4871 static void vc1_decode_i_blocks_adv(VC1Context *v)
4874 MpegEncContext *s = &v->s;
4880 GetBitContext *gb = &s->gb;
4882 /* select codingmode used for VLC tables selection */
4883 switch (v->y_ac_table_index) {
4885 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4888 v->codingset = CS_HIGH_MOT_INTRA;
4891 v->codingset = CS_MID_RATE_INTRA;
4895 switch (v->c_ac_table_index) {
4897 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4900 v->codingset2 = CS_HIGH_MOT_INTER;
4903 v->codingset2 = CS_MID_RATE_INTER;
4908 s->mb_x = s->mb_y = 0;
4910 s->first_slice_line = 1;
4911 s->mb_y = s->start_mb_y;
4912 if (s->start_mb_y) {
4914 init_block_index(v);
4915 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4916 (1 + s->b8_stride) * sizeof(*s->coded_block));
4918 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4920 init_block_index(v);
4921 for (;s->mb_x < s->mb_width; s->mb_x++) {
4922 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4923 ff_update_block_index(s);
4924 s->dsp.clear_blocks(block[0]);
4925 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4926 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4927 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4928 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4930 // do actual MB decoding and displaying
4931 if (v->fieldtx_is_raw)
4932 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4933 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4934 if ( v->acpred_is_raw)
4935 v->s.ac_pred = get_bits1(&v->s.gb);
4937 v->s.ac_pred = v->acpred_plane[mb_pos];
4939 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4940 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4944 s->current_picture.qscale_table[mb_pos] = mquant;
4945 /* Set DC scale - y and c use the same */
4946 s->y_dc_scale = s->y_dc_scale_table[mquant];
4947 s->c_dc_scale = s->c_dc_scale_table[mquant];
4949 for (k = 0; k < 6; k++) {
4950 val = ((cbp >> (5 - k)) & 1);
4953 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4957 cbp |= val << (5 - k);
4959 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4960 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4962 vc1_decode_i_block_adv(v, block[k], k, val,
4963 (k < 4) ? v->codingset : v->codingset2, mquant);
4965 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4967 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4970 vc1_smooth_overlap_filter_iblk(v);
4971 vc1_put_signed_blocks_clamped(v);
4972 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4974 if (get_bits_count(&s->gb) > v->bits) {
4975 // TODO: may need modification to handle slice coding
4976 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4977 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4978 get_bits_count(&s->gb), v->bits);
4982 if (!v->s.loop_filter)
4983 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4985 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4986 s->first_slice_line = 0;
4989 /* raw bottom MB row */
4991 init_block_index(v);
4993 for (;s->mb_x < s->mb_width; s->mb_x++) {
4994 ff_update_block_index(s);
4995 vc1_put_signed_blocks_clamped(v);
4996 if (v->s.loop_filter)
4997 vc1_loop_filter_iblk_delayed(v, v->pq);
4999 if (v->s.loop_filter)
5000 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5001 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5002 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5005 static void vc1_decode_p_blocks(VC1Context *v)
5007 MpegEncContext *s = &v->s;
5008 int apply_loop_filter;
5010 /* select codingmode used for VLC tables selection */
5011 switch (v->c_ac_table_index) {
5013 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5016 v->codingset = CS_HIGH_MOT_INTRA;
5019 v->codingset = CS_MID_RATE_INTRA;
5023 switch (v->c_ac_table_index) {
5025 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5028 v->codingset2 = CS_HIGH_MOT_INTER;
5031 v->codingset2 = CS_MID_RATE_INTER;
5035 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5036 v->fcm == PROGRESSIVE;
5037 s->first_slice_line = 1;
5038 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5039 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5041 init_block_index(v);
5042 for (; s->mb_x < s->mb_width; s->mb_x++) {
5043 ff_update_block_index(s);
5045 if (v->fcm == ILACE_FIELD)
5046 vc1_decode_p_mb_intfi(v);
5047 else if (v->fcm == ILACE_FRAME)
5048 vc1_decode_p_mb_intfr(v);
5049 else vc1_decode_p_mb(v);
5050 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5051 vc1_apply_p_loop_filter(v);
5052 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5053 // TODO: may need modification to handle slice coding
5054 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5055 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5056 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5060 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5061 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5062 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5063 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5064 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5065 s->first_slice_line = 0;
5067 if (apply_loop_filter) {
5069 init_block_index(v);
5070 for (; s->mb_x < s->mb_width; s->mb_x++) {
5071 ff_update_block_index(s);
5072 vc1_apply_p_loop_filter(v);
5075 if (s->end_mb_y >= s->start_mb_y)
5076 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5077 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5078 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5081 static void vc1_decode_b_blocks(VC1Context *v)
5083 MpegEncContext *s = &v->s;
5085 /* select codingmode used for VLC tables selection */
5086 switch (v->c_ac_table_index) {
5088 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5091 v->codingset = CS_HIGH_MOT_INTRA;
5094 v->codingset = CS_MID_RATE_INTRA;
5098 switch (v->c_ac_table_index) {
5100 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5103 v->codingset2 = CS_HIGH_MOT_INTER;
5106 v->codingset2 = CS_MID_RATE_INTER;
5110 s->first_slice_line = 1;
5111 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5113 init_block_index(v);
5114 for (; s->mb_x < s->mb_width; s->mb_x++) {
5115 ff_update_block_index(s);
5117 if (v->fcm == ILACE_FIELD)
5118 vc1_decode_b_mb_intfi(v);
5119 else if (v->fcm == ILACE_FRAME)
5120 vc1_decode_b_mb_intfr(v);
5123 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5124 // TODO: may need modification to handle slice coding
5125 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5126 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5127 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5130 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5132 if (!v->s.loop_filter)
5133 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5135 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5136 s->first_slice_line = 0;
5138 if (v->s.loop_filter)
5139 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5140 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5141 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5144 static void vc1_decode_skip_blocks(VC1Context *v)
5146 MpegEncContext *s = &v->s;
5148 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5149 s->first_slice_line = 1;
5150 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5152 init_block_index(v);
5153 ff_update_block_index(s);
5154 if (s->last_picture.f.data[0]) {
5155 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5156 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5157 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5159 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5160 s->first_slice_line = 0;
5162 s->pict_type = AV_PICTURE_TYPE_P;
5165 void ff_vc1_decode_blocks(VC1Context *v)
5168 v->s.esc3_level_length = 0;
5170 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5173 v->left_blk_idx = -1;
5174 v->topleft_blk_idx = 1;
5176 switch (v->s.pict_type) {
5177 case AV_PICTURE_TYPE_I:
5178 if (v->profile == PROFILE_ADVANCED)
5179 vc1_decode_i_blocks_adv(v);
5181 vc1_decode_i_blocks(v);
5183 case AV_PICTURE_TYPE_P:
5184 if (v->p_frame_skipped)
5185 vc1_decode_skip_blocks(v);
5187 vc1_decode_p_blocks(v);
5189 case AV_PICTURE_TYPE_B:
5191 if (v->profile == PROFILE_ADVANCED)
5192 vc1_decode_i_blocks_adv(v);
5194 vc1_decode_i_blocks(v);
5196 vc1_decode_b_blocks(v);
5202 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5206 * Transform coefficients for both sprites in 16.16 fixed point format,
5207 * in the order they appear in the bitstream:
5209 * rotation 1 (unused)
5211 * rotation 2 (unused)
5218 int effect_type, effect_flag;
5219 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5220 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5223 static inline int get_fp_val(GetBitContext* gb)
5225 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5228 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5232 switch (get_bits(gb, 2)) {
5235 c[2] = get_fp_val(gb);
5239 c[0] = c[4] = get_fp_val(gb);
5240 c[2] = get_fp_val(gb);
5243 c[0] = get_fp_val(gb);
5244 c[2] = get_fp_val(gb);
5245 c[4] = get_fp_val(gb);
5248 c[0] = get_fp_val(gb);
5249 c[1] = get_fp_val(gb);
5250 c[2] = get_fp_val(gb);
5251 c[3] = get_fp_val(gb);
5252 c[4] = get_fp_val(gb);
5255 c[5] = get_fp_val(gb);
5257 c[6] = get_fp_val(gb);
5262 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5264 AVCodecContext *avctx = v->s.avctx;
5267 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5268 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5269 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5270 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5271 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5272 for (i = 0; i < 7; i++)
5273 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5274 sd->coefs[sprite][i] / (1<<16),
5275 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5276 av_log(avctx, AV_LOG_DEBUG, "\n");
5280 if (sd->effect_type = get_bits_long(gb, 30)) {
5281 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5283 vc1_sprite_parse_transform(gb, sd->effect_params1);
5286 vc1_sprite_parse_transform(gb, sd->effect_params1);
5287 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5290 for (i = 0; i < sd->effect_pcount1; i++)
5291 sd->effect_params1[i] = get_fp_val(gb);
5293 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5294 // effect 13 is simple alpha blending and matches the opacity above
5295 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5296 for (i = 0; i < sd->effect_pcount1; i++)
5297 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5298 sd->effect_params1[i] / (1 << 16),
5299 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5300 av_log(avctx, AV_LOG_DEBUG, "\n");
5303 sd->effect_pcount2 = get_bits(gb, 16);
5304 if (sd->effect_pcount2 > 10) {
5305 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5307 } else if (sd->effect_pcount2) {
5309 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5310 while (++i < sd->effect_pcount2) {
5311 sd->effect_params2[i] = get_fp_val(gb);
5312 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5313 sd->effect_params2[i] / (1 << 16),
5314 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5316 av_log(avctx, AV_LOG_DEBUG, "\n");
5319 if (sd->effect_flag = get_bits1(gb))
5320 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5322 if (get_bits_count(gb) >= gb->size_in_bits +
5323 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
5324 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5325 if (get_bits_count(gb) < gb->size_in_bits - 8)
5326 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5329 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5331 int i, plane, row, sprite;
5332 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5333 uint8_t* src_h[2][2];
5334 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5336 MpegEncContext *s = &v->s;
5338 for (i = 0; i < 2; i++) {
5339 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5340 xadv[i] = sd->coefs[i][0];
5341 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5342 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5344 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5345 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5347 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5349 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5350 int width = v->output_width>>!!plane;
5352 for (row = 0; row < v->output_height>>!!plane; row++) {
5353 uint8_t *dst = v->sprite_output_frame.data[plane] +
5354 v->sprite_output_frame.linesize[plane] * row;
5356 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5357 uint8_t *iplane = s->current_picture.f.data[plane];
5358 int iline = s->current_picture.f.linesize[plane];
5359 int ycoord = yoff[sprite] + yadv[sprite] * row;
5360 int yline = ycoord >> 16;
5362 ysub[sprite] = ycoord & 0xFFFF;
5364 iplane = s->last_picture.f.data[plane];
5365 iline = s->last_picture.f.linesize[plane];
5367 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5368 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5369 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5371 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5373 if (sr_cache[sprite][0] != yline) {
5374 if (sr_cache[sprite][1] == yline) {
5375 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5376 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5378 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5379 sr_cache[sprite][0] = yline;
5382 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5383 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5384 iplane + next_line, xoff[sprite],
5385 xadv[sprite], width);
5386 sr_cache[sprite][1] = yline + 1;
5388 src_h[sprite][0] = v->sr_rows[sprite][0];
5389 src_h[sprite][1] = v->sr_rows[sprite][1];
5393 if (!v->two_sprites) {
5395 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5397 memcpy(dst, src_h[0][0], width);
5400 if (ysub[0] && ysub[1]) {
5401 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5402 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5403 } else if (ysub[0]) {
5404 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5405 src_h[1][0], alpha, width);
5406 } else if (ysub[1]) {
5407 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5408 src_h[0][0], (1<<16)-1-alpha, width);
5410 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5416 for (i = 0; i < 2; i++) {
5426 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5429 MpegEncContext *s = &v->s;
5430 AVCodecContext *avctx = s->avctx;
5433 vc1_parse_sprites(v, gb, &sd);
5435 if (!s->current_picture.f.data[0]) {
5436 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5440 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5441 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5445 av_frame_unref(&v->sprite_output_frame);
5446 if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
5449 vc1_draw_sprites(v, &sd);
5454 static void vc1_sprite_flush(AVCodecContext *avctx)
5456 VC1Context *v = avctx->priv_data;
5457 MpegEncContext *s = &v->s;
5458 AVFrame *f = &s->current_picture.f;
5461 /* Windows Media Image codecs have a convergence interval of two keyframes.
5462 Since we can't enforce it, clear to black the missing sprite. This is
5463 wrong but it looks better than doing nothing. */
5466 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5467 for (i = 0; i < v->sprite_height>>!!plane; i++)
5468 memset(f->data[plane] + i * f->linesize[plane],
5469 plane ? 128 : 0, f->linesize[plane]);
5474 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5476 MpegEncContext *s = &v->s;
5479 /* Allocate mb bitplanes */
5480 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5481 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5482 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5483 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5484 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5485 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5487 v->n_allocated_blks = s->mb_width + 2;
5488 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5489 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5490 v->cbp = v->cbp_base + s->mb_stride;
5491 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5492 v->ttblk = v->ttblk_base + s->mb_stride;
5493 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5494 v->is_intra = v->is_intra_base + s->mb_stride;
5495 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5496 v->luma_mv = v->luma_mv_base + s->mb_stride;
5498 /* allocate block type info in that way so it could be used with s->block_index[] */
5499 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5500 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5501 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5502 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5504 /* allocate memory to store block level MV info */
5505 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5506 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5507 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5508 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5509 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5510 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5511 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5512 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);
5514 /* Init coded blocks info */
5515 if (v->profile == PROFILE_ADVANCED) {
5516 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5518 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5522 ff_intrax8_common_init(&v->x8,s);
5524 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5525 for (i = 0; i < 4; i++)
5526 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5529 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5530 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5537 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5540 for (i = 0; i < 64; i++) {
5541 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5542 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5543 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5544 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5545 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5546 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5552 /** Initialize a VC1/WMV3 decoder
5553 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5554 * @todo TODO: Decypher remaining bits in extra_data
5556 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5558 VC1Context *v = avctx->priv_data;
5559 MpegEncContext *s = &v->s;
5562 /* save the container output size for WMImage */
5563 v->output_width = avctx->width;
5564 v->output_height = avctx->height;
5566 if (!avctx->extradata_size || !avctx->extradata)
5568 if (!(avctx->flags & CODEC_FLAG_GRAY))
5569 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5571 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5572 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5574 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5575 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5577 if (ff_vc1_init_common(v) < 0)
5579 // ensure static VLC tables are initialized
5580 if (ff_msmpeg4_decode_init(avctx) < 0)
5582 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5584 // Hack to ensure the above functions will be called
5585 // again once we know all necessary settings.
5586 // That this is necessary might indicate a bug.
5587 ff_vc1_decode_end(avctx);
5589 ff_h264chroma_init(&v->h264chroma, 8);
5590 ff_vc1dsp_init(&v->vc1dsp);
5592 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5595 // looks like WMV3 has a sequence header stored in the extradata
5596 // advanced sequence header may be before the first frame
5597 // the last byte of the extradata is a version number, 1 for the
5598 // samples we can decode
5600 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5602 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5605 count = avctx->extradata_size*8 - get_bits_count(&gb);
5607 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5608 count, get_bits(&gb, count));
5609 } else if (count < 0) {
5610 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5612 } else { // VC1/WVC1/WVP2
5613 const uint8_t *start = avctx->extradata;
5614 uint8_t *end = avctx->extradata + avctx->extradata_size;
5615 const uint8_t *next;
5616 int size, buf2_size;
5617 uint8_t *buf2 = NULL;
5618 int seq_initialized = 0, ep_initialized = 0;
5620 if (avctx->extradata_size < 16) {
5621 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5625 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5626 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5628 for (; next < end; start = next) {
5629 next = find_next_marker(start + 4, end);
5630 size = next - start - 4;
5633 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5634 init_get_bits(&gb, buf2, buf2_size * 8);
5635 switch (AV_RB32(start)) {
5636 case VC1_CODE_SEQHDR:
5637 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5641 seq_initialized = 1;
5643 case VC1_CODE_ENTRYPOINT:
5644 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5653 if (!seq_initialized || !ep_initialized) {
5654 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5657 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5660 avctx->profile = v->profile;
5661 if (v->profile == PROFILE_ADVANCED)
5662 avctx->level = v->level;
5664 avctx->has_b_frames = !!avctx->max_b_frames;
5666 s->mb_width = (avctx->coded_width + 15) >> 4;
5667 s->mb_height = (avctx->coded_height + 15) >> 4;
5669 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5670 ff_vc1_init_transposed_scantables(v);
5672 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5677 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5678 v->sprite_width = avctx->coded_width;
5679 v->sprite_height = avctx->coded_height;
5681 avctx->coded_width = avctx->width = v->output_width;
5682 avctx->coded_height = avctx->height = v->output_height;
5684 // prevent 16.16 overflows
5685 if (v->sprite_width > 1 << 14 ||
5686 v->sprite_height > 1 << 14 ||
5687 v->output_width > 1 << 14 ||
5688 v->output_height > 1 << 14) return -1;
5690 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5691 avpriv_request_sample(avctx, "odd sprites support");
5692 return AVERROR_PATCHWELCOME;
5698 /** Close a VC1/WMV3 decoder
5699 * @warning Initial try at using MpegEncContext stuff
5701 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5703 VC1Context *v = avctx->priv_data;
5706 av_frame_unref(&v->sprite_output_frame);
5708 for (i = 0; i < 4; i++)
5709 av_freep(&v->sr_rows[i >> 1][i & 1]);
5710 av_freep(&v->hrd_rate);
5711 av_freep(&v->hrd_buffer);
5712 ff_MPV_common_end(&v->s);
5713 av_freep(&v->mv_type_mb_plane);
5714 av_freep(&v->direct_mb_plane);
5715 av_freep(&v->forward_mb_plane);
5716 av_freep(&v->fieldtx_plane);
5717 av_freep(&v->acpred_plane);
5718 av_freep(&v->over_flags_plane);
5719 av_freep(&v->mb_type_base);
5720 av_freep(&v->blk_mv_type_base);
5721 av_freep(&v->mv_f_base);
5722 av_freep(&v->mv_f_next_base);
5723 av_freep(&v->block);
5724 av_freep(&v->cbp_base);
5725 av_freep(&v->ttblk_base);
5726 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5727 av_freep(&v->luma_mv_base);
5728 ff_intrax8_common_end(&v->x8);
5733 /** Decode a VC1/WMV3 frame
5734 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5736 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5737 int *got_frame, AVPacket *avpkt)
5739 const uint8_t *buf = avpkt->data;
5740 int buf_size = avpkt->size, n_slices = 0, i, ret;
5741 VC1Context *v = avctx->priv_data;
5742 MpegEncContext *s = &v->s;
5743 AVFrame *pict = data;
5744 uint8_t *buf2 = NULL;
5745 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5746 int mb_height, n_slices1=-1;
5751 } *slices = NULL, *tmp;
5753 v->second_field = 0;
5755 if(s->flags & CODEC_FLAG_LOW_DELAY)
5758 /* no supplementary picture */
5759 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5760 /* special case for last picture */
5761 if (s->low_delay == 0 && s->next_picture_ptr) {
5762 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
5764 s->next_picture_ptr = NULL;
5772 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5773 if (v->profile < PROFILE_ADVANCED)
5774 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5776 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5779 //for advanced profile we may need to parse and unescape data
5780 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5782 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5784 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5785 const uint8_t *start, *end, *next;
5789 for (start = buf, end = buf + buf_size; next < end; start = next) {
5790 next = find_next_marker(start + 4, end);
5791 size = next - start - 4;
5792 if (size <= 0) continue;
5793 switch (AV_RB32(start)) {
5794 case VC1_CODE_FRAME:
5795 if (avctx->hwaccel ||
5796 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5798 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5800 case VC1_CODE_FIELD: {
5802 if (avctx->hwaccel ||
5803 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5804 buf_start_second_field = start;
5805 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5809 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5810 if (!slices[n_slices].buf)
5812 buf_size3 = vc1_unescape_buffer(start + 4, size,
5813 slices[n_slices].buf);
5814 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5816 /* assuming that the field marker is at the exact middle,
5817 hope it's correct */
5818 slices[n_slices].mby_start = s->mb_height >> 1;
5819 n_slices1 = n_slices - 1; // index of the last slice of the first field
5823 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5824 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5825 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5826 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5828 case VC1_CODE_SLICE: {
5830 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5834 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5835 if (!slices[n_slices].buf)
5837 buf_size3 = vc1_unescape_buffer(start + 4, size,
5838 slices[n_slices].buf);
5839 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5841 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5847 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5848 const uint8_t *divider;
5851 divider = find_next_marker(buf, buf + buf_size);
5852 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5853 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5855 } else { // found field marker, unescape second field
5856 if (avctx->hwaccel ||
5857 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5858 buf_start_second_field = divider;
5859 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5863 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5864 if (!slices[n_slices].buf)
5866 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5867 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5869 slices[n_slices].mby_start = s->mb_height >> 1;
5870 n_slices1 = n_slices - 1;
5873 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5875 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5877 init_get_bits(&s->gb, buf2, buf_size2*8);
5879 init_get_bits(&s->gb, buf, buf_size*8);
5881 if (v->res_sprite) {
5882 v->new_sprite = !get_bits1(&s->gb);
5883 v->two_sprites = get_bits1(&s->gb);
5884 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5885 we're using the sprite compositor. These are intentionally kept separate
5886 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5887 the vc1 one for WVP2 */
5888 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5889 if (v->new_sprite) {
5890 // switch AVCodecContext parameters to those of the sprites
5891 avctx->width = avctx->coded_width = v->sprite_width;
5892 avctx->height = avctx->coded_height = v->sprite_height;
5899 if (s->context_initialized &&
5900 (s->width != avctx->coded_width ||
5901 s->height != avctx->coded_height)) {
5902 ff_vc1_decode_end(avctx);
5905 if (!s->context_initialized) {
5906 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5909 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5911 if (v->profile == PROFILE_ADVANCED) {
5912 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5914 s->h_edge_pos = avctx->coded_width;
5915 s->v_edge_pos = avctx->coded_height;
5919 /* We need to set current_picture_ptr before reading the header,
5920 * otherwise we cannot store anything in there. */
5921 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5922 int i = ff_find_unused_picture(s, 0);
5925 s->current_picture_ptr = &s->picture[i];
5928 // do parse frame header
5929 v->pic_header_flag = 0;
5930 v->first_pic_header_flag = 1;
5931 if (v->profile < PROFILE_ADVANCED) {
5932 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5936 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5940 v->first_pic_header_flag = 0;
5942 if (avctx->debug & FF_DEBUG_PICT_INFO)
5943 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5945 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5946 && s->pict_type != AV_PICTURE_TYPE_I) {
5947 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5951 if ((s->mb_height >> v->field_mode) == 0) {
5952 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5956 // process pulldown flags
5957 s->current_picture_ptr->f.repeat_pict = 0;
5958 // Pulldown flags are only valid when 'broadcast' has been set.
5959 // So ticks_per_frame will be 2
5962 s->current_picture_ptr->f.repeat_pict = 1;
5963 } else if (v->rptfrm) {
5965 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5968 // for skipping the frame
5969 s->current_picture.f.pict_type = s->pict_type;
5970 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5972 /* skip B-frames if we don't have reference frames */
5973 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5976 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5977 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5978 avctx->skip_frame >= AVDISCARD_ALL) {
5982 if (s->next_p_frame_damaged) {
5983 if (s->pict_type == AV_PICTURE_TYPE_B)
5986 s->next_p_frame_damaged = 0;
5989 if (ff_MPV_frame_start(s, avctx) < 0) {
5993 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5994 v->s.current_picture_ptr->f.top_field_first = v->tff;
5996 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5997 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5999 if ((CONFIG_VC1_VDPAU_DECODER)
6000 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6001 if (v->field_mode && buf_start_second_field) {
6002 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6003 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6005 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6007 } else if (avctx->hwaccel) {
6008 if (v->field_mode && buf_start_second_field) {
6009 // decode first field
6010 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6011 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6013 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6015 if (avctx->hwaccel->end_frame(avctx) < 0)
6018 // decode second field
6019 s->gb = slices[n_slices1 + 1].gb;
6020 s->picture_structure = PICT_TOP_FIELD + v->tff;
6021 v->second_field = 1;
6022 v->pic_header_flag = 0;
6023 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6024 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6027 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
6029 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6031 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6033 if (avctx->hwaccel->end_frame(avctx) < 0)
6036 s->picture_structure = PICT_FRAME;
6037 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6039 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6041 if (avctx->hwaccel->end_frame(avctx) < 0)
6048 ff_mpeg_er_frame_start(s);
6050 v->bits = buf_size * 8;
6051 v->end_mb_x = s->mb_width;
6052 if (v->field_mode) {
6053 s->current_picture.f.linesize[0] <<= 1;
6054 s->current_picture.f.linesize[1] <<= 1;
6055 s->current_picture.f.linesize[2] <<= 1;
6057 s->uvlinesize <<= 1;
6059 mb_height = s->mb_height >> v->field_mode;
6060 for (i = 0; i <= n_slices; i++) {
6061 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6062 if (v->field_mode <= 0) {
6063 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6064 "picture boundary (%d >= %d)\n", i,
6065 slices[i - 1].mby_start, mb_height);
6068 v->second_field = 1;
6069 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6070 v->mb_off = s->mb_stride * s->mb_height >> 1;
6072 v->second_field = 0;
6077 v->pic_header_flag = 0;
6078 if (v->field_mode && i == n_slices1 + 2) {
6079 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6080 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6083 } else if (get_bits1(&s->gb)) {
6084 v->pic_header_flag = 1;
6085 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6086 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6093 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6094 if (!v->field_mode || v->second_field)
6095 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6097 if (i >= n_slices) {
6098 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6101 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6103 if (s->end_mb_y <= s->start_mb_y) {
6104 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6107 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6108 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6111 ff_vc1_decode_blocks(v);
6113 s->gb = slices[i].gb;
6115 if (v->field_mode) {
6116 v->second_field = 0;
6117 s->current_picture.f.linesize[0] >>= 1;
6118 s->current_picture.f.linesize[1] >>= 1;
6119 s->current_picture.f.linesize[2] >>= 1;
6121 s->uvlinesize >>= 1;
6122 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6123 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6124 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6127 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6128 get_bits_count(&s->gb), s->gb.size_in_bits);
6129 // if (get_bits_count(&s->gb) > buf_size * 8)
6131 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6134 ff_er_frame_end(&s->er);
6137 ff_MPV_frame_end(s);
6139 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6141 avctx->width = avctx->coded_width = v->output_width;
6142 avctx->height = avctx->coded_height = v->output_height;
6143 if (avctx->skip_frame >= AVDISCARD_NONREF)
6145 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6146 if (vc1_decode_sprites(v, &s->gb))
6149 if ((ret = av_frame_ref(pict, &v->sprite_output_frame)) < 0)
6153 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6154 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
6156 ff_print_debug_info(s, s->current_picture_ptr, pict);
6157 } else if (s->last_picture_ptr != NULL) {
6158 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
6160 ff_print_debug_info(s, s->last_picture_ptr, pict);
6162 if (s->last_picture_ptr || s->low_delay) {
6169 for (i = 0; i < n_slices; i++)
6170 av_free(slices[i].buf);
6176 for (i = 0; i < n_slices; i++)
6177 av_free(slices[i].buf);
6183 static const AVProfile profiles[] = {
6184 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6185 { FF_PROFILE_VC1_MAIN, "Main" },
6186 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6187 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6188 { FF_PROFILE_UNKNOWN },
6191 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6193 AV_PIX_FMT_DXVA2_VLD,
6196 AV_PIX_FMT_VAAPI_VLD,
6205 AVCodec ff_vc1_decoder = {
6207 .type = AVMEDIA_TYPE_VIDEO,
6208 .id = AV_CODEC_ID_VC1,
6209 .priv_data_size = sizeof(VC1Context),
6210 .init = vc1_decode_init,
6211 .close = ff_vc1_decode_end,
6212 .decode = vc1_decode_frame,
6213 .flush = ff_mpeg_flush,
6214 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6215 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6216 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6217 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6220 #if CONFIG_WMV3_DECODER
6221 AVCodec ff_wmv3_decoder = {
6223 .type = AVMEDIA_TYPE_VIDEO,
6224 .id = AV_CODEC_ID_WMV3,
6225 .priv_data_size = sizeof(VC1Context),
6226 .init = vc1_decode_init,
6227 .close = ff_vc1_decode_end,
6228 .decode = vc1_decode_frame,
6229 .flush = ff_mpeg_flush,
6230 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6231 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6232 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6233 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6237 #if CONFIG_WMV3_VDPAU_DECODER
6238 AVCodec ff_wmv3_vdpau_decoder = {
6239 .name = "wmv3_vdpau",
6240 .type = AVMEDIA_TYPE_VIDEO,
6241 .id = AV_CODEC_ID_WMV3,
6242 .priv_data_size = sizeof(VC1Context),
6243 .init = vc1_decode_init,
6244 .close = ff_vc1_decode_end,
6245 .decode = vc1_decode_frame,
6246 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6247 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6248 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6249 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6253 #if CONFIG_VC1_VDPAU_DECODER
6254 AVCodec ff_vc1_vdpau_decoder = {
6255 .name = "vc1_vdpau",
6256 .type = AVMEDIA_TYPE_VIDEO,
6257 .id = AV_CODEC_ID_VC1,
6258 .priv_data_size = sizeof(VC1Context),
6259 .init = vc1_decode_init,
6260 .close = ff_vc1_decode_end,
6261 .decode = vc1_decode_frame,
6262 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6263 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6264 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6265 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6269 #if CONFIG_WMV3IMAGE_DECODER
6270 AVCodec ff_wmv3image_decoder = {
6271 .name = "wmv3image",
6272 .type = AVMEDIA_TYPE_VIDEO,
6273 .id = AV_CODEC_ID_WMV3IMAGE,
6274 .priv_data_size = sizeof(VC1Context),
6275 .init = vc1_decode_init,
6276 .close = ff_vc1_decode_end,
6277 .decode = vc1_decode_frame,
6278 .capabilities = CODEC_CAP_DR1,
6279 .flush = vc1_sprite_flush,
6280 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6281 .pix_fmts = ff_pixfmt_list_420
6285 #if CONFIG_VC1IMAGE_DECODER
6286 AVCodec ff_vc1image_decoder = {
6288 .type = AVMEDIA_TYPE_VIDEO,
6289 .id = AV_CODEC_ID_VC1IMAGE,
6290 .priv_data_size = sizeof(VC1Context),
6291 .init = vc1_decode_init,
6292 .close = ff_vc1_decode_end,
6293 .decode = vc1_decode_frame,
6294 .capabilities = CODEC_CAP_DR1,
6295 .flush = vc1_sprite_flush,
6296 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6297 .pix_fmts = ff_pixfmt_list_420