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) {
1851 if (field_a && a_valid) {
1854 } else if (field_b && b_valid) {
1857 } else if (c_valid) {
1862 } else if (total_valid == 1) {
1863 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1864 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1868 /* store MV using signed modulus of MV range defined in 4.11 */
1869 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1870 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1871 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1872 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1873 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1874 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1875 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1876 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1877 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1878 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1879 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1880 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1881 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1882 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1886 /** Motion compensation for direct or interpolated blocks in B-frames
1888 static void vc1_interp_mc(VC1Context *v)
1890 MpegEncContext *s = &v->s;
1891 H264ChromaContext *h264chroma = &v->h264chroma;
1892 uint8_t *srcY, *srcU, *srcV;
1893 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1895 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1896 int use_ic = v->next_use_ic;
1898 if (!v->field_mode && !v->s.next_picture.f.data[0])
1901 mx = s->mv[1][0][0];
1902 my = s->mv[1][0][1];
1903 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1904 uvmy = (my + ((my & 3) == 3)) >> 1;
1905 if (v->field_mode) {
1906 if (v->cur_field_type != v->ref_field_type[1])
1907 my = my - 2 + 4 * v->cur_field_type;
1908 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1911 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1912 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1914 srcY = s->next_picture.f.data[0];
1915 srcU = s->next_picture.f.data[1];
1916 srcV = s->next_picture.f.data[2];
1918 src_x = s->mb_x * 16 + (mx >> 2);
1919 src_y = s->mb_y * 16 + (my >> 2);
1920 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1921 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1923 if (v->profile != PROFILE_ADVANCED) {
1924 src_x = av_clip( src_x, -16, s->mb_width * 16);
1925 src_y = av_clip( src_y, -16, s->mb_height * 16);
1926 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1927 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1929 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1930 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1931 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1932 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1935 srcY += src_y * s->linesize + src_x;
1936 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1937 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1939 if (v->field_mode && v->ref_field_type[1]) {
1940 srcY += s->current_picture_ptr->f.linesize[0];
1941 srcU += s->current_picture_ptr->f.linesize[1];
1942 srcV += s->current_picture_ptr->f.linesize[2];
1945 /* for grayscale we should not try to read from unknown area */
1946 if (s->flags & CODEC_FLAG_GRAY) {
1947 srcU = s->edge_emu_buffer + 18 * s->linesize;
1948 srcV = s->edge_emu_buffer + 18 * s->linesize;
1951 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1952 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1953 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1954 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1956 srcY -= s->mspel * (1 + s->linesize);
1957 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1958 17 + s->mspel * 2, 17 + s->mspel * 2,
1959 src_x - s->mspel, src_y - s->mspel,
1960 s->h_edge_pos, v_edge_pos);
1961 srcY = s->edge_emu_buffer;
1962 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1963 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1964 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1965 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1968 /* if we deal with range reduction we need to scale source blocks */
1969 if (v->rangeredfrm) {
1971 uint8_t *src, *src2;
1974 for (j = 0; j < 17 + s->mspel * 2; j++) {
1975 for (i = 0; i < 17 + s->mspel * 2; i++)
1976 src[i] = ((src[i] - 128) >> 1) + 128;
1981 for (j = 0; j < 9; j++) {
1982 for (i = 0; i < 9; i++) {
1983 src[i] = ((src[i] - 128) >> 1) + 128;
1984 src2[i] = ((src2[i] - 128) >> 1) + 128;
1986 src += s->uvlinesize;
1987 src2 += s->uvlinesize;
1992 uint8_t (*luty )[256] = v->next_luty;
1993 uint8_t (*lutuv)[256] = v->next_lutuv;
1995 uint8_t *src, *src2;
1998 for (j = 0; j < 17 + s->mspel * 2; j++) {
1999 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2000 for (i = 0; i < 17 + s->mspel * 2; i++)
2001 src[i] = luty[f][src[i]];
2006 for (j = 0; j < 9; j++) {
2007 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2008 for (i = 0; i < 9; i++) {
2009 src[i] = lutuv[f][src[i]];
2010 src2[i] = lutuv[f][src2[i]];
2012 src += s->uvlinesize;
2013 src2 += s->uvlinesize;
2016 srcY += s->mspel * (1 + s->linesize);
2023 dxy = ((my & 3) << 2) | (mx & 3);
2024 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2025 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2026 srcY += s->linesize * 8;
2027 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2028 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2030 dxy = (my & 2) | ((mx & 2) >> 1);
2033 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2035 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2038 if (s->flags & CODEC_FLAG_GRAY) return;
2039 /* Chroma MC always uses qpel blilinear */
2040 uvmx = (uvmx & 3) << 1;
2041 uvmy = (uvmy & 3) << 1;
2043 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2044 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2046 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2047 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2051 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2055 #if B_FRACTION_DEN==256
2059 return 2 * ((value * n + 255) >> 9);
2060 return (value * n + 128) >> 8;
2063 n -= B_FRACTION_DEN;
2065 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2066 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2070 /** Reconstruct motion vector for B-frame and do motion compensation
2072 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2073 int direct, int mode)
2080 if (mode == BMV_TYPE_INTERPOLATED) {
2086 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2089 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2090 int direct, int mvtype)
2092 MpegEncContext *s = &v->s;
2093 int xy, wrap, off = 0;
2098 const uint8_t *is_intra = v->mb_type[0];
2102 /* scale MV difference to be quad-pel */
2103 dmv_x[0] <<= 1 - s->quarter_sample;
2104 dmv_y[0] <<= 1 - s->quarter_sample;
2105 dmv_x[1] <<= 1 - s->quarter_sample;
2106 dmv_y[1] <<= 1 - s->quarter_sample;
2108 wrap = s->b8_stride;
2109 xy = s->block_index[0];
2112 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2113 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2114 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2115 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2118 if (!v->field_mode) {
2119 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2120 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2121 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2122 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2124 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2125 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));
2126 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));
2127 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));
2128 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));
2131 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2132 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2133 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2134 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2138 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2139 C = s->current_picture.motion_val[0][xy - 2];
2140 A = s->current_picture.motion_val[0][xy - wrap * 2];
2141 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2142 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2144 if (!s->mb_x) C[0] = C[1] = 0;
2145 if (!s->first_slice_line) { // predictor A is not out of bounds
2146 if (s->mb_width == 1) {
2150 px = mid_pred(A[0], B[0], C[0]);
2151 py = mid_pred(A[1], B[1], C[1]);
2153 } else if (s->mb_x) { // predictor C is not out of bounds
2159 /* Pullback MV as specified in 8.3.5.3.4 */
2162 if (v->profile < PROFILE_ADVANCED) {
2163 qx = (s->mb_x << 5);
2164 qy = (s->mb_y << 5);
2165 X = (s->mb_width << 5) - 4;
2166 Y = (s->mb_height << 5) - 4;
2167 if (qx + px < -28) px = -28 - qx;
2168 if (qy + py < -28) py = -28 - qy;
2169 if (qx + px > X) px = X - qx;
2170 if (qy + py > Y) py = Y - qy;
2172 qx = (s->mb_x << 6);
2173 qy = (s->mb_y << 6);
2174 X = (s->mb_width << 6) - 4;
2175 Y = (s->mb_height << 6) - 4;
2176 if (qx + px < -60) px = -60 - qx;
2177 if (qy + py < -60) py = -60 - qy;
2178 if (qx + px > X) px = X - qx;
2179 if (qy + py > Y) py = Y - qy;
2182 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2183 if (0 && !s->first_slice_line && s->mb_x) {
2184 if (is_intra[xy - wrap])
2185 sum = FFABS(px) + FFABS(py);
2187 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2189 if (get_bits1(&s->gb)) {
2197 if (is_intra[xy - 2])
2198 sum = FFABS(px) + FFABS(py);
2200 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2202 if (get_bits1(&s->gb)) {
2212 /* store MV using signed modulus of MV range defined in 4.11 */
2213 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2214 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2216 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2217 C = s->current_picture.motion_val[1][xy - 2];
2218 A = s->current_picture.motion_val[1][xy - wrap * 2];
2219 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2220 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2224 if (!s->first_slice_line) { // predictor A is not out of bounds
2225 if (s->mb_width == 1) {
2229 px = mid_pred(A[0], B[0], C[0]);
2230 py = mid_pred(A[1], B[1], C[1]);
2232 } else if (s->mb_x) { // predictor C is not out of bounds
2238 /* Pullback MV as specified in 8.3.5.3.4 */
2241 if (v->profile < PROFILE_ADVANCED) {
2242 qx = (s->mb_x << 5);
2243 qy = (s->mb_y << 5);
2244 X = (s->mb_width << 5) - 4;
2245 Y = (s->mb_height << 5) - 4;
2246 if (qx + px < -28) px = -28 - qx;
2247 if (qy + py < -28) py = -28 - qy;
2248 if (qx + px > X) px = X - qx;
2249 if (qy + py > Y) py = Y - qy;
2251 qx = (s->mb_x << 6);
2252 qy = (s->mb_y << 6);
2253 X = (s->mb_width << 6) - 4;
2254 Y = (s->mb_height << 6) - 4;
2255 if (qx + px < -60) px = -60 - qx;
2256 if (qy + py < -60) py = -60 - qy;
2257 if (qx + px > X) px = X - qx;
2258 if (qy + py > Y) py = Y - qy;
2261 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2262 if (0 && !s->first_slice_line && s->mb_x) {
2263 if (is_intra[xy - wrap])
2264 sum = FFABS(px) + FFABS(py);
2266 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2268 if (get_bits1(&s->gb)) {
2276 if (is_intra[xy - 2])
2277 sum = FFABS(px) + FFABS(py);
2279 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2281 if (get_bits1(&s->gb)) {
2291 /* store MV using signed modulus of MV range defined in 4.11 */
2293 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2294 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2296 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2297 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2298 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2299 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2302 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2304 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2305 MpegEncContext *s = &v->s;
2306 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2308 if (v->bmvtype == BMV_TYPE_DIRECT) {
2309 int total_opp, k, f;
2310 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2311 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2312 v->bfraction, 0, s->quarter_sample);
2313 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2314 v->bfraction, 0, s->quarter_sample);
2315 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2316 v->bfraction, 1, s->quarter_sample);
2317 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2318 v->bfraction, 1, s->quarter_sample);
2320 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2321 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2322 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2323 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2324 f = (total_opp > 2) ? 1 : 0;
2326 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2327 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2330 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2331 for (k = 0; k < 4; k++) {
2332 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2333 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2334 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2335 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2336 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2337 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2341 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2342 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);
2343 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);
2346 if (dir) { // backward
2347 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);
2348 if (n == 3 || mv1) {
2349 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2352 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);
2353 if (n == 3 || mv1) {
2354 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2359 /** Get predicted DC value for I-frames only
2360 * prediction dir: left=0, top=1
2361 * @param s MpegEncContext
2362 * @param overlap flag indicating that overlap filtering is used
2363 * @param pq integer part of picture quantizer
2364 * @param[in] n block index in the current MB
2365 * @param dc_val_ptr Pointer to DC predictor
2366 * @param dir_ptr Prediction direction for use in AC prediction
2368 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2369 int16_t **dc_val_ptr, int *dir_ptr)
2371 int a, b, c, wrap, pred, scale;
2373 static const uint16_t dcpred[32] = {
2374 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2375 114, 102, 93, 85, 79, 73, 68, 64,
2376 60, 57, 54, 51, 49, 47, 45, 43,
2377 41, 39, 38, 37, 35, 34, 33
2380 /* find prediction - wmv3_dc_scale always used here in fact */
2381 if (n < 4) scale = s->y_dc_scale;
2382 else scale = s->c_dc_scale;
2384 wrap = s->block_wrap[n];
2385 dc_val = s->dc_val[0] + s->block_index[n];
2391 b = dc_val[ - 1 - wrap];
2392 a = dc_val[ - wrap];
2394 if (pq < 9 || !overlap) {
2395 /* Set outer values */
2396 if (s->first_slice_line && (n != 2 && n != 3))
2397 b = a = dcpred[scale];
2398 if (s->mb_x == 0 && (n != 1 && n != 3))
2399 b = c = dcpred[scale];
2401 /* Set outer values */
2402 if (s->first_slice_line && (n != 2 && n != 3))
2404 if (s->mb_x == 0 && (n != 1 && n != 3))
2408 if (abs(a - b) <= abs(b - c)) {
2410 *dir_ptr = 1; // left
2413 *dir_ptr = 0; // top
2416 /* update predictor */
2417 *dc_val_ptr = &dc_val[0];
2422 /** Get predicted DC value
2423 * prediction dir: left=0, top=1
2424 * @param s MpegEncContext
2425 * @param overlap flag indicating that overlap filtering is used
2426 * @param pq integer part of picture quantizer
2427 * @param[in] n block index in the current MB
2428 * @param a_avail flag indicating top block availability
2429 * @param c_avail flag indicating left block availability
2430 * @param dc_val_ptr Pointer to DC predictor
2431 * @param dir_ptr Prediction direction for use in AC prediction
2433 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2434 int a_avail, int c_avail,
2435 int16_t **dc_val_ptr, int *dir_ptr)
2437 int a, b, c, wrap, pred;
2439 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2443 wrap = s->block_wrap[n];
2444 dc_val = s->dc_val[0] + s->block_index[n];
2450 b = dc_val[ - 1 - wrap];
2451 a = dc_val[ - wrap];
2452 /* scale predictors if needed */
2453 q1 = s->current_picture.qscale_table[mb_pos];
2454 dqscale_index = s->y_dc_scale_table[q1] - 1;
2455 if (dqscale_index < 0)
2457 if (c_avail && (n != 1 && n != 3)) {
2458 q2 = s->current_picture.qscale_table[mb_pos - 1];
2460 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2462 if (a_avail && (n != 2 && n != 3)) {
2463 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2465 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2467 if (a_avail && c_avail && (n != 3)) {
2472 off -= s->mb_stride;
2473 q2 = s->current_picture.qscale_table[off];
2475 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2478 if (a_avail && c_avail) {
2479 if (abs(a - b) <= abs(b - c)) {
2481 *dir_ptr = 1; // left
2484 *dir_ptr = 0; // top
2486 } else if (a_avail) {
2488 *dir_ptr = 0; // top
2489 } else if (c_avail) {
2491 *dir_ptr = 1; // left
2494 *dir_ptr = 1; // left
2497 /* update predictor */
2498 *dc_val_ptr = &dc_val[0];
2502 /** @} */ // Block group
2505 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2506 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2510 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2511 uint8_t **coded_block_ptr)
2513 int xy, wrap, pred, a, b, c;
2515 xy = s->block_index[n];
2516 wrap = s->b8_stride;
2521 a = s->coded_block[xy - 1 ];
2522 b = s->coded_block[xy - 1 - wrap];
2523 c = s->coded_block[xy - wrap];
2532 *coded_block_ptr = &s->coded_block[xy];
2538 * Decode one AC coefficient
2539 * @param v The VC1 context
2540 * @param last Last coefficient
2541 * @param skip How much zero coefficients to skip
2542 * @param value Decoded AC coefficient value
2543 * @param codingset set of VLC to decode data
2546 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2547 int *value, int codingset)
2549 GetBitContext *gb = &v->s.gb;
2550 int index, escape, run = 0, level = 0, lst = 0;
2552 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2553 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2554 run = vc1_index_decode_table[codingset][index][0];
2555 level = vc1_index_decode_table[codingset][index][1];
2556 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2560 escape = decode210(gb);
2562 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2563 run = vc1_index_decode_table[codingset][index][0];
2564 level = vc1_index_decode_table[codingset][index][1];
2565 lst = index >= vc1_last_decode_table[codingset];
2568 level += vc1_last_delta_level_table[codingset][run];
2570 level += vc1_delta_level_table[codingset][run];
2573 run += vc1_last_delta_run_table[codingset][level] + 1;
2575 run += vc1_delta_run_table[codingset][level] + 1;
2581 lst = get_bits1(gb);
2582 if (v->s.esc3_level_length == 0) {
2583 if (v->pq < 8 || v->dquantfrm) { // table 59
2584 v->s.esc3_level_length = get_bits(gb, 3);
2585 if (!v->s.esc3_level_length)
2586 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2587 } else { // table 60
2588 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2590 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2592 run = get_bits(gb, v->s.esc3_run_length);
2593 sign = get_bits1(gb);
2594 level = get_bits(gb, v->s.esc3_level_length);
2605 /** Decode intra block in intra frames - should be faster than decode_intra_block
2606 * @param v VC1Context
2607 * @param block block to decode
2608 * @param[in] n subblock index
2609 * @param coded are AC coeffs present or not
2610 * @param codingset set of VLC to decode data
2612 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2613 int coded, int codingset)
2615 GetBitContext *gb = &v->s.gb;
2616 MpegEncContext *s = &v->s;
2617 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2620 int16_t *ac_val, *ac_val2;
2623 /* Get DC differential */
2625 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2627 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2630 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2634 if (dcdiff == 119 /* ESC index value */) {
2635 /* TODO: Optimize */
2636 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2637 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2638 else dcdiff = get_bits(gb, 8);
2641 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2642 else if (v->pq == 2)
2643 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2650 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2653 /* Store the quantized DC coeff, used for prediction */
2655 block[0] = dcdiff * s->y_dc_scale;
2657 block[0] = dcdiff * s->c_dc_scale;
2668 int last = 0, skip, value;
2669 const uint8_t *zz_table;
2673 scale = v->pq * 2 + v->halfpq;
2677 zz_table = v->zz_8x8[2];
2679 zz_table = v->zz_8x8[3];
2681 zz_table = v->zz_8x8[1];
2683 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2685 if (dc_pred_dir) // left
2688 ac_val -= 16 * s->block_wrap[n];
2691 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2695 block[zz_table[i++]] = value;
2698 /* apply AC prediction if needed */
2700 if (dc_pred_dir) { // left
2701 for (k = 1; k < 8; k++)
2702 block[k << v->left_blk_sh] += ac_val[k];
2704 for (k = 1; k < 8; k++)
2705 block[k << v->top_blk_sh] += ac_val[k + 8];
2708 /* save AC coeffs for further prediction */
2709 for (k = 1; k < 8; k++) {
2710 ac_val2[k] = block[k << v->left_blk_sh];
2711 ac_val2[k + 8] = block[k << v->top_blk_sh];
2714 /* scale AC coeffs */
2715 for (k = 1; k < 64; k++)
2719 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2722 if (s->ac_pred) i = 63;
2728 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2732 scale = v->pq * 2 + v->halfpq;
2733 memset(ac_val2, 0, 16 * 2);
2734 if (dc_pred_dir) { // left
2737 memcpy(ac_val2, ac_val, 8 * 2);
2739 ac_val -= 16 * s->block_wrap[n];
2741 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2744 /* apply AC prediction if needed */
2746 if (dc_pred_dir) { //left
2747 for (k = 1; k < 8; k++) {
2748 block[k << v->left_blk_sh] = ac_val[k] * scale;
2749 if (!v->pquantizer && block[k << v->left_blk_sh])
2750 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2753 for (k = 1; k < 8; k++) {
2754 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2755 if (!v->pquantizer && block[k << v->top_blk_sh])
2756 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2762 s->block_last_index[n] = i;
2767 /** Decode intra block in intra frames - should be faster than decode_intra_block
2768 * @param v VC1Context
2769 * @param block block to decode
2770 * @param[in] n subblock number
2771 * @param coded are AC coeffs present or not
2772 * @param codingset set of VLC to decode data
2773 * @param mquant quantizer value for this macroblock
2775 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2776 int coded, int codingset, int mquant)
2778 GetBitContext *gb = &v->s.gb;
2779 MpegEncContext *s = &v->s;
2780 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2782 int16_t *dc_val = NULL;
2783 int16_t *ac_val, *ac_val2;
2785 int a_avail = v->a_avail, c_avail = v->c_avail;
2786 int use_pred = s->ac_pred;
2789 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2791 /* Get DC differential */
2793 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2795 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2798 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2802 if (dcdiff == 119 /* ESC index value */) {
2803 /* TODO: Optimize */
2804 if (mquant == 1) dcdiff = get_bits(gb, 10);
2805 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2806 else dcdiff = get_bits(gb, 8);
2809 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2810 else if (mquant == 2)
2811 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2818 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2821 /* Store the quantized DC coeff, used for prediction */
2823 block[0] = dcdiff * s->y_dc_scale;
2825 block[0] = dcdiff * s->c_dc_scale;
2831 /* check if AC is needed at all */
2832 if (!a_avail && !c_avail)
2834 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2837 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2839 if (dc_pred_dir) // left
2842 ac_val -= 16 * s->block_wrap[n];
2844 q1 = s->current_picture.qscale_table[mb_pos];
2845 if ( dc_pred_dir && c_avail && mb_pos)
2846 q2 = s->current_picture.qscale_table[mb_pos - 1];
2847 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2848 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2849 if ( dc_pred_dir && n == 1)
2851 if (!dc_pred_dir && n == 2)
2857 int last = 0, skip, value;
2858 const uint8_t *zz_table;
2862 if (!use_pred && v->fcm == ILACE_FRAME) {
2863 zz_table = v->zzi_8x8;
2865 if (!dc_pred_dir) // top
2866 zz_table = v->zz_8x8[2];
2868 zz_table = v->zz_8x8[3];
2871 if (v->fcm != ILACE_FRAME)
2872 zz_table = v->zz_8x8[1];
2874 zz_table = v->zzi_8x8;
2878 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2882 block[zz_table[i++]] = value;
2885 /* apply AC prediction if needed */
2887 /* scale predictors if needed*/
2888 if (q2 && q1 != q2) {
2889 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2890 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2893 return AVERROR_INVALIDDATA;
2894 if (dc_pred_dir) { // left
2895 for (k = 1; k < 8; k++)
2896 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2898 for (k = 1; k < 8; k++)
2899 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2902 if (dc_pred_dir) { //left
2903 for (k = 1; k < 8; k++)
2904 block[k << v->left_blk_sh] += ac_val[k];
2906 for (k = 1; k < 8; k++)
2907 block[k << v->top_blk_sh] += ac_val[k + 8];
2911 /* save AC coeffs for further prediction */
2912 for (k = 1; k < 8; k++) {
2913 ac_val2[k ] = block[k << v->left_blk_sh];
2914 ac_val2[k + 8] = block[k << v->top_blk_sh];
2917 /* scale AC coeffs */
2918 for (k = 1; k < 64; k++)
2922 block[k] += (block[k] < 0) ? -mquant : mquant;
2925 if (use_pred) i = 63;
2926 } else { // no AC coeffs
2929 memset(ac_val2, 0, 16 * 2);
2930 if (dc_pred_dir) { // left
2932 memcpy(ac_val2, ac_val, 8 * 2);
2933 if (q2 && q1 != q2) {
2934 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2935 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2937 return AVERROR_INVALIDDATA;
2938 for (k = 1; k < 8; k++)
2939 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2944 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2945 if (q2 && q1 != q2) {
2946 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2947 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2949 return AVERROR_INVALIDDATA;
2950 for (k = 1; k < 8; k++)
2951 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2956 /* apply AC prediction if needed */
2958 if (dc_pred_dir) { // left
2959 for (k = 1; k < 8; k++) {
2960 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2961 if (!v->pquantizer && block[k << v->left_blk_sh])
2962 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2965 for (k = 1; k < 8; k++) {
2966 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2967 if (!v->pquantizer && block[k << v->top_blk_sh])
2968 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2974 s->block_last_index[n] = i;
2979 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2980 * @param v VC1Context
2981 * @param block block to decode
2982 * @param[in] n subblock index
2983 * @param coded are AC coeffs present or not
2984 * @param mquant block quantizer
2985 * @param codingset set of VLC to decode data
2987 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2988 int coded, int mquant, int codingset)
2990 GetBitContext *gb = &v->s.gb;
2991 MpegEncContext *s = &v->s;
2992 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2994 int16_t *dc_val = NULL;
2995 int16_t *ac_val, *ac_val2;
2997 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2998 int a_avail = v->a_avail, c_avail = v->c_avail;
2999 int use_pred = s->ac_pred;
3003 s->dsp.clear_block(block);
3005 /* XXX: Guard against dumb values of mquant */
3006 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3008 /* Set DC scale - y and c use the same */
3009 s->y_dc_scale = s->y_dc_scale_table[mquant];
3010 s->c_dc_scale = s->c_dc_scale_table[mquant];
3012 /* Get DC differential */
3014 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3016 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3019 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3023 if (dcdiff == 119 /* ESC index value */) {
3024 /* TODO: Optimize */
3025 if (mquant == 1) dcdiff = get_bits(gb, 10);
3026 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3027 else dcdiff = get_bits(gb, 8);
3030 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3031 else if (mquant == 2)
3032 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3039 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3042 /* Store the quantized DC coeff, used for prediction */
3045 block[0] = dcdiff * s->y_dc_scale;
3047 block[0] = dcdiff * s->c_dc_scale;
3053 /* check if AC is needed at all and adjust direction if needed */
3054 if (!a_avail) dc_pred_dir = 1;
3055 if (!c_avail) dc_pred_dir = 0;
3056 if (!a_avail && !c_avail) use_pred = 0;
3057 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3060 scale = mquant * 2 + v->halfpq;
3062 if (dc_pred_dir) //left
3065 ac_val -= 16 * s->block_wrap[n];
3067 q1 = s->current_picture.qscale_table[mb_pos];
3068 if (dc_pred_dir && c_avail && mb_pos)
3069 q2 = s->current_picture.qscale_table[mb_pos - 1];
3070 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3071 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3072 if ( dc_pred_dir && n == 1)
3074 if (!dc_pred_dir && n == 2)
3076 if (n == 3) q2 = q1;
3079 int last = 0, skip, value;
3083 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3087 if (v->fcm == PROGRESSIVE)
3088 block[v->zz_8x8[0][i++]] = value;
3090 if (use_pred && (v->fcm == ILACE_FRAME)) {
3091 if (!dc_pred_dir) // top
3092 block[v->zz_8x8[2][i++]] = value;
3094 block[v->zz_8x8[3][i++]] = value;
3096 block[v->zzi_8x8[i++]] = value;
3101 /* apply AC prediction if needed */
3103 /* scale predictors if needed*/
3104 if (q2 && q1 != q2) {
3105 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3106 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3109 return AVERROR_INVALIDDATA;
3110 if (dc_pred_dir) { // left
3111 for (k = 1; k < 8; k++)
3112 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3114 for (k = 1; k < 8; k++)
3115 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3118 if (dc_pred_dir) { // left
3119 for (k = 1; k < 8; k++)
3120 block[k << v->left_blk_sh] += ac_val[k];
3122 for (k = 1; k < 8; k++)
3123 block[k << v->top_blk_sh] += ac_val[k + 8];
3127 /* save AC coeffs for further prediction */
3128 for (k = 1; k < 8; k++) {
3129 ac_val2[k ] = block[k << v->left_blk_sh];
3130 ac_val2[k + 8] = block[k << v->top_blk_sh];
3133 /* scale AC coeffs */
3134 for (k = 1; k < 64; k++)
3138 block[k] += (block[k] < 0) ? -mquant : mquant;
3141 if (use_pred) i = 63;
3142 } else { // no AC coeffs
3145 memset(ac_val2, 0, 16 * 2);
3146 if (dc_pred_dir) { // left
3148 memcpy(ac_val2, ac_val, 8 * 2);
3149 if (q2 && q1 != q2) {
3150 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3151 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3153 return AVERROR_INVALIDDATA;
3154 for (k = 1; k < 8; k++)
3155 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3160 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3161 if (q2 && q1 != q2) {
3162 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3163 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3165 return AVERROR_INVALIDDATA;
3166 for (k = 1; k < 8; k++)
3167 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3172 /* apply AC prediction if needed */
3174 if (dc_pred_dir) { // left
3175 for (k = 1; k < 8; k++) {
3176 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3177 if (!v->pquantizer && block[k << v->left_blk_sh])
3178 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3181 for (k = 1; k < 8; k++) {
3182 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3183 if (!v->pquantizer && block[k << v->top_blk_sh])
3184 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3190 s->block_last_index[n] = i;
3197 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3198 int mquant, int ttmb, int first_block,
3199 uint8_t *dst, int linesize, int skip_block,
3202 MpegEncContext *s = &v->s;
3203 GetBitContext *gb = &s->gb;
3206 int scale, off, idx, last, skip, value;
3207 int ttblk = ttmb & 7;
3210 s->dsp.clear_block(block);
3213 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)];
3215 if (ttblk == TT_4X4) {
3216 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3218 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3219 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3220 || (!v->res_rtm_flag && !first_block))) {
3221 subblkpat = decode012(gb);
3223 subblkpat ^= 3; // swap decoded pattern bits
3224 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3226 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3229 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3231 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3232 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3233 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3236 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3237 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3246 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3251 idx = v->zz_8x8[0][i++];
3253 idx = v->zzi_8x8[i++];
3254 block[idx] = value * scale;
3256 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3260 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3262 v->vc1dsp.vc1_inv_trans_8x8(block);
3263 s->dsp.add_pixels_clamped(block, dst, linesize);
3268 pat = ~subblkpat & 0xF;
3269 for (j = 0; j < 4; j++) {
3270 last = subblkpat & (1 << (3 - j));
3272 off = (j & 1) * 4 + (j & 2) * 16;
3274 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3279 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3281 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3282 block[idx + off] = value * scale;
3284 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3286 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3288 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3290 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3295 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3296 for (j = 0; j < 2; j++) {
3297 last = subblkpat & (1 << (1 - j));
3301 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3306 idx = v->zz_8x4[i++] + off;
3308 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3309 block[idx] = value * scale;
3311 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3313 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3315 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3317 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3322 pat = ~(subblkpat * 5) & 0xF;
3323 for (j = 0; j < 2; j++) {
3324 last = subblkpat & (1 << (1 - j));
3328 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3333 idx = v->zz_4x8[i++] + off;
3335 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3336 block[idx] = value * scale;
3338 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3340 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3342 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3344 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3350 *ttmb_out |= ttblk << (n * 4);
3354 /** @} */ // Macroblock group
3356 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3357 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3359 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3361 MpegEncContext *s = &v->s;
3362 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3363 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3364 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3365 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3366 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3369 if (block_num > 3) {
3370 dst = s->dest[block_num - 3];
3372 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3374 if (s->mb_y != s->end_mb_y || block_num < 2) {
3378 if (block_num > 3) {
3379 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3380 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3381 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3382 mv_stride = s->mb_stride;
3384 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3385 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3386 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3387 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3388 mv_stride = s->b8_stride;
3389 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3392 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3393 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3394 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3396 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3398 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3401 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3403 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3408 dst -= 4 * linesize;
3409 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3410 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3411 idx = (block_cbp | (block_cbp >> 2)) & 3;
3413 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3416 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3418 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3423 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3425 MpegEncContext *s = &v->s;
3426 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3427 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3428 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3429 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3430 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3433 if (block_num > 3) {
3434 dst = s->dest[block_num - 3] - 8 * linesize;
3436 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3439 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3442 if (block_num > 3) {
3443 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3444 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3445 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3447 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3448 : (mb_cbp >> ((block_num + 1) * 4));
3449 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3450 : (mb_is_intra >> ((block_num + 1) * 4));
3451 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3453 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3454 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3456 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3458 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3461 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3463 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3469 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3470 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3471 idx = (block_cbp | (block_cbp >> 1)) & 5;
3473 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3476 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3478 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3483 static void vc1_apply_p_loop_filter(VC1Context *v)
3485 MpegEncContext *s = &v->s;
3488 for (i = 0; i < 6; i++) {
3489 vc1_apply_p_v_loop_filter(v, i);
3492 /* V always precedes H, therefore we run H one MB before V;
3493 * at the end of a row, we catch up to complete the row */
3495 for (i = 0; i < 6; i++) {
3496 vc1_apply_p_h_loop_filter(v, i);
3498 if (s->mb_x == s->mb_width - 1) {
3500 ff_update_block_index(s);
3501 for (i = 0; i < 6; i++) {
3502 vc1_apply_p_h_loop_filter(v, i);
3508 /** Decode one P-frame MB
3510 static int vc1_decode_p_mb(VC1Context *v)
3512 MpegEncContext *s = &v->s;
3513 GetBitContext *gb = &s->gb;
3515 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3516 int cbp; /* cbp decoding stuff */
3517 int mqdiff, mquant; /* MB quantization */
3518 int ttmb = v->ttfrm; /* MB Transform type */
3520 int mb_has_coeffs = 1; /* last_flag */
3521 int dmv_x, dmv_y; /* Differential MV components */
3522 int index, index1; /* LUT indexes */
3523 int val, sign; /* temp values */
3524 int first_block = 1;
3526 int skipped, fourmv;
3527 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3529 mquant = v->pq; /* lossy initialization */
3531 if (v->mv_type_is_raw)
3532 fourmv = get_bits1(gb);
3534 fourmv = v->mv_type_mb_plane[mb_pos];
3536 skipped = get_bits1(gb);
3538 skipped = v->s.mbskip_table[mb_pos];
3540 if (!fourmv) { /* 1MV mode */
3542 GET_MVDATA(dmv_x, dmv_y);
3545 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3546 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3548 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3549 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3551 /* FIXME Set DC val for inter block ? */
3552 if (s->mb_intra && !mb_has_coeffs) {
3554 s->ac_pred = get_bits1(gb);
3556 } else if (mb_has_coeffs) {
3558 s->ac_pred = get_bits1(gb);
3559 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3565 s->current_picture.qscale_table[mb_pos] = mquant;
3567 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3568 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3569 VC1_TTMB_VLC_BITS, 2);
3570 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3572 for (i = 0; i < 6; i++) {
3573 s->dc_val[0][s->block_index[i]] = 0;
3575 val = ((cbp >> (5 - i)) & 1);
3576 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3577 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3579 /* check if prediction blocks A and C are available */
3580 v->a_avail = v->c_avail = 0;
3581 if (i == 2 || i == 3 || !s->first_slice_line)
3582 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3583 if (i == 1 || i == 3 || s->mb_x)
3584 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3586 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3587 (i & 4) ? v->codingset2 : v->codingset);
3588 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3590 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3592 for (j = 0; j < 64; j++)
3593 s->block[i][j] <<= 1;
3594 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3595 if (v->pq >= 9 && v->overlap) {
3597 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3599 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3601 block_cbp |= 0xF << (i << 2);
3602 block_intra |= 1 << i;
3604 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3605 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3606 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3607 block_cbp |= pat << (i << 2);
3608 if (!v->ttmbf && ttmb < 8)
3615 for (i = 0; i < 6; i++) {
3616 v->mb_type[0][s->block_index[i]] = 0;
3617 s->dc_val[0][s->block_index[i]] = 0;
3619 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3620 s->current_picture.qscale_table[mb_pos] = 0;
3621 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3624 } else { // 4MV mode
3625 if (!skipped /* unskipped MB */) {
3626 int intra_count = 0, coded_inter = 0;
3627 int is_intra[6], is_coded[6];
3629 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3630 for (i = 0; i < 6; i++) {
3631 val = ((cbp >> (5 - i)) & 1);
3632 s->dc_val[0][s->block_index[i]] = 0;
3639 GET_MVDATA(dmv_x, dmv_y);
3641 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3643 vc1_mc_4mv_luma(v, i, 0, 0);
3644 intra_count += s->mb_intra;
3645 is_intra[i] = s->mb_intra;
3646 is_coded[i] = mb_has_coeffs;
3649 is_intra[i] = (intra_count >= 3);
3653 vc1_mc_4mv_chroma(v, 0);
3654 v->mb_type[0][s->block_index[i]] = is_intra[i];
3656 coded_inter = !is_intra[i] & is_coded[i];
3658 // if there are no coded blocks then don't do anything more
3660 if (!intra_count && !coded_inter)
3663 s->current_picture.qscale_table[mb_pos] = mquant;
3664 /* test if block is intra and has pred */
3667 for (i = 0; i < 6; i++)
3669 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3670 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3676 s->ac_pred = get_bits1(gb);
3680 if (!v->ttmbf && coded_inter)
3681 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3682 for (i = 0; i < 6; i++) {
3684 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3685 s->mb_intra = is_intra[i];
3687 /* check if prediction blocks A and C are available */
3688 v->a_avail = v->c_avail = 0;
3689 if (i == 2 || i == 3 || !s->first_slice_line)
3690 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3691 if (i == 1 || i == 3 || s->mb_x)
3692 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3694 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3695 (i & 4) ? v->codingset2 : v->codingset);
3696 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3698 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3700 for (j = 0; j < 64; j++)
3701 s->block[i][j] <<= 1;
3702 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3703 (i & 4) ? s->uvlinesize : s->linesize);
3704 if (v->pq >= 9 && v->overlap) {
3706 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3708 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3710 block_cbp |= 0xF << (i << 2);
3711 block_intra |= 1 << i;
3712 } else if (is_coded[i]) {
3713 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3714 first_block, s->dest[dst_idx] + off,
3715 (i & 4) ? s->uvlinesize : s->linesize,
3716 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3718 block_cbp |= pat << (i << 2);
3719 if (!v->ttmbf && ttmb < 8)
3724 } else { // skipped MB
3726 s->current_picture.qscale_table[mb_pos] = 0;
3727 for (i = 0; i < 6; i++) {
3728 v->mb_type[0][s->block_index[i]] = 0;
3729 s->dc_val[0][s->block_index[i]] = 0;
3731 for (i = 0; i < 4; i++) {
3732 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3733 vc1_mc_4mv_luma(v, i, 0, 0);
3735 vc1_mc_4mv_chroma(v, 0);
3736 s->current_picture.qscale_table[mb_pos] = 0;
3740 v->cbp[s->mb_x] = block_cbp;
3741 v->ttblk[s->mb_x] = block_tt;
3742 v->is_intra[s->mb_x] = block_intra;
3747 /* Decode one macroblock in an interlaced frame p picture */
3749 static int vc1_decode_p_mb_intfr(VC1Context *v)
3751 MpegEncContext *s = &v->s;
3752 GetBitContext *gb = &s->gb;
3754 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3755 int cbp = 0; /* cbp decoding stuff */
3756 int mqdiff, mquant; /* MB quantization */
3757 int ttmb = v->ttfrm; /* MB Transform type */
3759 int mb_has_coeffs = 1; /* last_flag */
3760 int dmv_x, dmv_y; /* Differential MV components */
3761 int val; /* temp value */
3762 int first_block = 1;
3764 int skipped, fourmv = 0, twomv = 0;
3765 int block_cbp = 0, pat, block_tt = 0;
3766 int idx_mbmode = 0, mvbp;
3767 int stride_y, fieldtx;
3769 mquant = v->pq; /* Lossy initialization */
3772 skipped = get_bits1(gb);
3774 skipped = v->s.mbskip_table[mb_pos];
3776 if (v->fourmvswitch)
3777 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3779 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3780 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3781 /* store the motion vector type in a flag (useful later) */
3782 case MV_PMODE_INTFR_4MV:
3784 v->blk_mv_type[s->block_index[0]] = 0;
3785 v->blk_mv_type[s->block_index[1]] = 0;
3786 v->blk_mv_type[s->block_index[2]] = 0;
3787 v->blk_mv_type[s->block_index[3]] = 0;
3789 case MV_PMODE_INTFR_4MV_FIELD:
3791 v->blk_mv_type[s->block_index[0]] = 1;
3792 v->blk_mv_type[s->block_index[1]] = 1;
3793 v->blk_mv_type[s->block_index[2]] = 1;
3794 v->blk_mv_type[s->block_index[3]] = 1;
3796 case MV_PMODE_INTFR_2MV_FIELD:
3798 v->blk_mv_type[s->block_index[0]] = 1;
3799 v->blk_mv_type[s->block_index[1]] = 1;
3800 v->blk_mv_type[s->block_index[2]] = 1;
3801 v->blk_mv_type[s->block_index[3]] = 1;
3803 case MV_PMODE_INTFR_1MV:
3804 v->blk_mv_type[s->block_index[0]] = 0;
3805 v->blk_mv_type[s->block_index[1]] = 0;
3806 v->blk_mv_type[s->block_index[2]] = 0;
3807 v->blk_mv_type[s->block_index[3]] = 0;
3810 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3811 for (i = 0; i < 4; i++) {
3812 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3813 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3815 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3816 s->mb_intra = v->is_intra[s->mb_x] = 1;
3817 for (i = 0; i < 6; i++)
3818 v->mb_type[0][s->block_index[i]] = 1;
3819 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3820 mb_has_coeffs = get_bits1(gb);
3822 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3823 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3825 s->current_picture.qscale_table[mb_pos] = mquant;
3826 /* Set DC scale - y and c use the same (not sure if necessary here) */
3827 s->y_dc_scale = s->y_dc_scale_table[mquant];
3828 s->c_dc_scale = s->c_dc_scale_table[mquant];
3830 for (i = 0; i < 6; i++) {
3831 s->dc_val[0][s->block_index[i]] = 0;
3833 val = ((cbp >> (5 - i)) & 1);
3834 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3835 v->a_avail = v->c_avail = 0;
3836 if (i == 2 || i == 3 || !s->first_slice_line)
3837 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3838 if (i == 1 || i == 3 || s->mb_x)
3839 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3841 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3842 (i & 4) ? v->codingset2 : v->codingset);
3843 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3844 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3846 stride_y = s->linesize << fieldtx;
3847 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3849 stride_y = s->uvlinesize;
3852 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3856 } else { // inter MB
3857 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3859 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3860 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3861 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3863 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3864 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3865 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3868 s->mb_intra = v->is_intra[s->mb_x] = 0;
3869 for (i = 0; i < 6; i++)
3870 v->mb_type[0][s->block_index[i]] = 0;
3871 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3872 /* for all motion vector read MVDATA and motion compensate each block */
3876 for (i = 0; i < 6; i++) {
3879 val = ((mvbp >> (3 - i)) & 1);
3881 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3883 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3884 vc1_mc_4mv_luma(v, i, 0, 0);
3885 } else if (i == 4) {
3886 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3893 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3895 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3896 vc1_mc_4mv_luma(v, 0, 0, 0);
3897 vc1_mc_4mv_luma(v, 1, 0, 0);
3900 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3902 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3903 vc1_mc_4mv_luma(v, 2, 0, 0);
3904 vc1_mc_4mv_luma(v, 3, 0, 0);
3905 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3907 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3910 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3912 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3916 GET_MQUANT(); // p. 227
3917 s->current_picture.qscale_table[mb_pos] = mquant;
3918 if (!v->ttmbf && cbp)
3919 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3920 for (i = 0; i < 6; i++) {
3921 s->dc_val[0][s->block_index[i]] = 0;
3923 val = ((cbp >> (5 - i)) & 1);
3925 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3927 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3929 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3930 first_block, s->dest[dst_idx] + off,
3931 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3932 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3933 block_cbp |= pat << (i << 2);
3934 if (!v->ttmbf && ttmb < 8)
3941 s->mb_intra = v->is_intra[s->mb_x] = 0;
3942 for (i = 0; i < 6; i++) {
3943 v->mb_type[0][s->block_index[i]] = 0;
3944 s->dc_val[0][s->block_index[i]] = 0;
3946 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3947 s->current_picture.qscale_table[mb_pos] = 0;
3948 v->blk_mv_type[s->block_index[0]] = 0;
3949 v->blk_mv_type[s->block_index[1]] = 0;
3950 v->blk_mv_type[s->block_index[2]] = 0;
3951 v->blk_mv_type[s->block_index[3]] = 0;
3952 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3955 if (s->mb_x == s->mb_width - 1)
3956 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3960 static int vc1_decode_p_mb_intfi(VC1Context *v)
3962 MpegEncContext *s = &v->s;
3963 GetBitContext *gb = &s->gb;
3965 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3966 int cbp = 0; /* cbp decoding stuff */
3967 int mqdiff, mquant; /* MB quantization */
3968 int ttmb = v->ttfrm; /* MB Transform type */
3970 int mb_has_coeffs = 1; /* last_flag */
3971 int dmv_x, dmv_y; /* Differential MV components */
3972 int val; /* temp values */
3973 int first_block = 1;
3976 int block_cbp = 0, pat, block_tt = 0;
3979 mquant = v->pq; /* Lossy initialization */
3981 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3982 if (idx_mbmode <= 1) { // intra MB
3983 s->mb_intra = v->is_intra[s->mb_x] = 1;
3984 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3985 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3986 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3988 s->current_picture.qscale_table[mb_pos] = mquant;
3989 /* Set DC scale - y and c use the same (not sure if necessary here) */
3990 s->y_dc_scale = s->y_dc_scale_table[mquant];
3991 s->c_dc_scale = s->c_dc_scale_table[mquant];
3992 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3993 mb_has_coeffs = idx_mbmode & 1;
3995 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3997 for (i = 0; i < 6; i++) {
3998 s->dc_val[0][s->block_index[i]] = 0;
3999 v->mb_type[0][s->block_index[i]] = 1;
4001 val = ((cbp >> (5 - i)) & 1);
4002 v->a_avail = v->c_avail = 0;
4003 if (i == 2 || i == 3 || !s->first_slice_line)
4004 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4005 if (i == 1 || i == 3 || s->mb_x)
4006 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4008 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4009 (i & 4) ? v->codingset2 : v->codingset);
4010 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4012 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4013 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4014 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4015 // TODO: loop filter
4018 s->mb_intra = v->is_intra[s->mb_x] = 0;
4019 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4020 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4021 if (idx_mbmode <= 5) { // 1-MV
4022 dmv_x = dmv_y = pred_flag = 0;
4023 if (idx_mbmode & 1) {
4024 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4026 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4028 mb_has_coeffs = !(idx_mbmode & 2);
4030 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4031 for (i = 0; i < 6; i++) {
4033 dmv_x = dmv_y = pred_flag = 0;
4034 val = ((v->fourmvbp >> (3 - i)) & 1);
4036 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4038 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4039 vc1_mc_4mv_luma(v, i, 0, 0);
4041 vc1_mc_4mv_chroma(v, 0);
4043 mb_has_coeffs = idx_mbmode & 1;
4046 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4050 s->current_picture.qscale_table[mb_pos] = mquant;
4051 if (!v->ttmbf && cbp) {
4052 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4055 for (i = 0; i < 6; i++) {
4056 s->dc_val[0][s->block_index[i]] = 0;
4058 val = ((cbp >> (5 - i)) & 1);
4059 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4061 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4062 first_block, s->dest[dst_idx] + off,
4063 (i & 4) ? s->uvlinesize : s->linesize,
4064 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4066 block_cbp |= pat << (i << 2);
4067 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4072 if (s->mb_x == s->mb_width - 1)
4073 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4077 /** Decode one B-frame MB (in Main profile)
4079 static void vc1_decode_b_mb(VC1Context *v)
4081 MpegEncContext *s = &v->s;
4082 GetBitContext *gb = &s->gb;
4084 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4085 int cbp = 0; /* cbp decoding stuff */
4086 int mqdiff, mquant; /* MB quantization */
4087 int ttmb = v->ttfrm; /* MB Transform type */
4088 int mb_has_coeffs = 0; /* last_flag */
4089 int index, index1; /* LUT indexes */
4090 int val, sign; /* temp values */
4091 int first_block = 1;
4093 int skipped, direct;
4094 int dmv_x[2], dmv_y[2];
4095 int bmvtype = BMV_TYPE_BACKWARD;
4097 mquant = v->pq; /* lossy initialization */
4101 direct = get_bits1(gb);
4103 direct = v->direct_mb_plane[mb_pos];
4105 skipped = get_bits1(gb);
4107 skipped = v->s.mbskip_table[mb_pos];
4109 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4110 for (i = 0; i < 6; i++) {
4111 v->mb_type[0][s->block_index[i]] = 0;
4112 s->dc_val[0][s->block_index[i]] = 0;
4114 s->current_picture.qscale_table[mb_pos] = 0;
4118 GET_MVDATA(dmv_x[0], dmv_y[0]);
4119 dmv_x[1] = dmv_x[0];
4120 dmv_y[1] = dmv_y[0];
4122 if (skipped || !s->mb_intra) {
4123 bmvtype = decode012(gb);
4126 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4129 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4132 bmvtype = BMV_TYPE_INTERPOLATED;
4133 dmv_x[0] = dmv_y[0] = 0;
4137 for (i = 0; i < 6; i++)
4138 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4142 bmvtype = BMV_TYPE_INTERPOLATED;
4143 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4144 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4148 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4151 s->current_picture.qscale_table[mb_pos] = mquant;
4153 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4154 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4155 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4156 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4158 if (!mb_has_coeffs && !s->mb_intra) {
4159 /* no coded blocks - effectively skipped */
4160 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4161 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4164 if (s->mb_intra && !mb_has_coeffs) {
4166 s->current_picture.qscale_table[mb_pos] = mquant;
4167 s->ac_pred = get_bits1(gb);
4169 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4171 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4172 GET_MVDATA(dmv_x[0], dmv_y[0]);
4173 if (!mb_has_coeffs) {
4174 /* interpolated skipped block */
4175 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4176 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4180 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4182 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4185 s->ac_pred = get_bits1(gb);
4186 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4188 s->current_picture.qscale_table[mb_pos] = mquant;
4189 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4190 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4194 for (i = 0; i < 6; i++) {
4195 s->dc_val[0][s->block_index[i]] = 0;
4197 val = ((cbp >> (5 - i)) & 1);
4198 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4199 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4201 /* check if prediction blocks A and C are available */
4202 v->a_avail = v->c_avail = 0;
4203 if (i == 2 || i == 3 || !s->first_slice_line)
4204 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4205 if (i == 1 || i == 3 || s->mb_x)
4206 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4208 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4209 (i & 4) ? v->codingset2 : v->codingset);
4210 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4212 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4214 for (j = 0; j < 64; j++)
4215 s->block[i][j] <<= 1;
4216 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4218 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4219 first_block, s->dest[dst_idx] + off,
4220 (i & 4) ? s->uvlinesize : s->linesize,
4221 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4222 if (!v->ttmbf && ttmb < 8)
4229 /** Decode one B-frame MB (in interlaced field B picture)
4231 static void vc1_decode_b_mb_intfi(VC1Context *v)
4233 MpegEncContext *s = &v->s;
4234 GetBitContext *gb = &s->gb;
4236 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4237 int cbp = 0; /* cbp decoding stuff */
4238 int mqdiff, mquant; /* MB quantization */
4239 int ttmb = v->ttfrm; /* MB Transform type */
4240 int mb_has_coeffs = 0; /* last_flag */
4241 int val; /* temp value */
4242 int first_block = 1;
4245 int dmv_x[2], dmv_y[2], pred_flag[2];
4246 int bmvtype = BMV_TYPE_BACKWARD;
4249 mquant = v->pq; /* Lossy initialization */
4252 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4253 if (idx_mbmode <= 1) { // intra MB
4254 s->mb_intra = v->is_intra[s->mb_x] = 1;
4255 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4256 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4257 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4259 s->current_picture.qscale_table[mb_pos] = mquant;
4260 /* Set DC scale - y and c use the same (not sure if necessary here) */
4261 s->y_dc_scale = s->y_dc_scale_table[mquant];
4262 s->c_dc_scale = s->c_dc_scale_table[mquant];
4263 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4264 mb_has_coeffs = idx_mbmode & 1;
4266 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4268 for (i = 0; i < 6; i++) {
4269 s->dc_val[0][s->block_index[i]] = 0;
4271 val = ((cbp >> (5 - i)) & 1);
4272 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4273 v->a_avail = v->c_avail = 0;
4274 if (i == 2 || i == 3 || !s->first_slice_line)
4275 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4276 if (i == 1 || i == 3 || s->mb_x)
4277 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4279 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4280 (i & 4) ? v->codingset2 : v->codingset);
4281 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4283 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4285 for (j = 0; j < 64; j++)
4286 s->block[i][j] <<= 1;
4287 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4288 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4289 // TODO: yet to perform loop filter
4292 s->mb_intra = v->is_intra[s->mb_x] = 0;
4293 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4294 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4296 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4298 fwd = v->forward_mb_plane[mb_pos];
4299 if (idx_mbmode <= 5) { // 1-MV
4301 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4302 pred_flag[0] = pred_flag[1] = 0;
4304 bmvtype = BMV_TYPE_FORWARD;
4306 bmvtype = decode012(gb);
4309 bmvtype = BMV_TYPE_BACKWARD;
4312 bmvtype = BMV_TYPE_DIRECT;
4315 bmvtype = BMV_TYPE_INTERPOLATED;
4316 interpmvp = get_bits1(gb);
4319 v->bmvtype = bmvtype;
4320 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4321 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4324 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4326 if (bmvtype == BMV_TYPE_DIRECT) {
4327 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4328 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4330 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4331 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4332 mb_has_coeffs = !(idx_mbmode & 2);
4335 bmvtype = BMV_TYPE_FORWARD;
4336 v->bmvtype = bmvtype;
4337 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4338 for (i = 0; i < 6; i++) {
4340 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4341 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4342 val = ((v->fourmvbp >> (3 - i)) & 1);
4344 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4345 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4346 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4348 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4349 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4351 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4353 mb_has_coeffs = idx_mbmode & 1;
4356 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4360 s->current_picture.qscale_table[mb_pos] = mquant;
4361 if (!v->ttmbf && cbp) {
4362 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4365 for (i = 0; i < 6; i++) {
4366 s->dc_val[0][s->block_index[i]] = 0;
4368 val = ((cbp >> (5 - i)) & 1);
4369 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4371 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4372 first_block, s->dest[dst_idx] + off,
4373 (i & 4) ? s->uvlinesize : s->linesize,
4374 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4375 if (!v->ttmbf && ttmb < 8)
4383 /** Decode one B-frame MB (in interlaced frame B picture)
4385 static int vc1_decode_b_mb_intfr(VC1Context *v)
4387 MpegEncContext *s = &v->s;
4388 GetBitContext *gb = &s->gb;
4390 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4391 int cbp = 0; /* cbp decoding stuff */
4392 int mqdiff, mquant; /* MB quantization */
4393 int ttmb = v->ttfrm; /* MB Transform type */
4394 int mvsw = 0; /* motion vector switch */
4395 int mb_has_coeffs = 1; /* last_flag */
4396 int dmv_x, dmv_y; /* Differential MV components */
4397 int val; /* temp value */
4398 int first_block = 1;
4400 int skipped, direct, twomv = 0;
4401 int block_cbp = 0, pat, block_tt = 0;
4402 int idx_mbmode = 0, mvbp;
4403 int stride_y, fieldtx;
4404 int bmvtype = BMV_TYPE_BACKWARD;
4407 mquant = v->pq; /* Lossy initialization */
4410 skipped = get_bits1(gb);
4412 skipped = v->s.mbskip_table[mb_pos];
4415 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4416 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4418 v->blk_mv_type[s->block_index[0]] = 1;
4419 v->blk_mv_type[s->block_index[1]] = 1;
4420 v->blk_mv_type[s->block_index[2]] = 1;
4421 v->blk_mv_type[s->block_index[3]] = 1;
4423 v->blk_mv_type[s->block_index[0]] = 0;
4424 v->blk_mv_type[s->block_index[1]] = 0;
4425 v->blk_mv_type[s->block_index[2]] = 0;
4426 v->blk_mv_type[s->block_index[3]] = 0;
4431 direct = get_bits1(gb);
4433 direct = v->direct_mb_plane[mb_pos];
4436 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);
4437 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);
4438 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);
4439 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);
4442 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);
4443 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);
4444 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);
4445 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);
4447 for (i = 1; i < 4; i += 2) {
4448 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4449 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4450 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4451 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4454 for (i = 1; i < 4; i++) {
4455 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4456 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4457 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4458 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4463 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4464 for (i = 0; i < 4; i++) {
4465 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4466 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4467 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4468 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4470 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4471 s->mb_intra = v->is_intra[s->mb_x] = 1;
4472 for (i = 0; i < 6; i++)
4473 v->mb_type[0][s->block_index[i]] = 1;
4474 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4475 mb_has_coeffs = get_bits1(gb);
4477 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4478 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4480 s->current_picture.qscale_table[mb_pos] = mquant;
4481 /* Set DC scale - y and c use the same (not sure if necessary here) */
4482 s->y_dc_scale = s->y_dc_scale_table[mquant];
4483 s->c_dc_scale = s->c_dc_scale_table[mquant];
4485 for (i = 0; i < 6; i++) {
4486 s->dc_val[0][s->block_index[i]] = 0;
4488 val = ((cbp >> (5 - i)) & 1);
4489 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4490 v->a_avail = v->c_avail = 0;
4491 if (i == 2 || i == 3 || !s->first_slice_line)
4492 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4493 if (i == 1 || i == 3 || s->mb_x)
4494 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4496 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4497 (i & 4) ? v->codingset2 : v->codingset);
4498 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4500 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4502 stride_y = s->linesize << fieldtx;
4503 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4505 stride_y = s->uvlinesize;
4508 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4511 s->mb_intra = v->is_intra[s->mb_x] = 0;
4513 if (skipped || !s->mb_intra) {
4514 bmvtype = decode012(gb);
4517 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4520 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4523 bmvtype = BMV_TYPE_INTERPOLATED;
4527 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4528 mvsw = get_bits1(gb);
4531 if (!skipped) { // inter MB
4532 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4534 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4536 if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
4537 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4538 } else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
4539 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4543 for (i = 0; i < 6; i++)
4544 v->mb_type[0][s->block_index[i]] = 0;
4545 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4546 /* for all motion vector read MVDATA and motion compensate each block */
4550 for (i = 0; i < 4; i++) {
4551 vc1_mc_4mv_luma(v, i, 0, 0);
4552 vc1_mc_4mv_luma(v, i, 1, 1);
4554 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4555 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4560 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4562 for (i = 0; i < 4; i++) {
4565 val = ((mvbp >> (3 - i)) & 1);
4567 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4569 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4570 vc1_mc_4mv_luma(v, j, dir, dir);
4571 vc1_mc_4mv_luma(v, j+1, dir, dir);
4574 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4575 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4576 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4580 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4582 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4587 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4589 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4592 dir = bmvtype == BMV_TYPE_BACKWARD;
4599 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4600 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4604 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4605 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4608 for (i = 0; i < 2; i++) {
4609 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];
4610 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];
4611 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];
4612 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];
4615 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4616 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4619 vc1_mc_4mv_luma(v, 0, dir, 0);
4620 vc1_mc_4mv_luma(v, 1, dir, 0);
4621 vc1_mc_4mv_luma(v, 2, dir2, 0);
4622 vc1_mc_4mv_luma(v, 3, dir2, 0);
4623 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4625 dir = bmvtype == BMV_TYPE_BACKWARD;
4627 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4630 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4632 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4633 v->blk_mv_type[s->block_index[0]] = 1;
4634 v->blk_mv_type[s->block_index[1]] = 1;
4635 v->blk_mv_type[s->block_index[2]] = 1;
4636 v->blk_mv_type[s->block_index[3]] = 1;
4637 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4638 for (i = 0; i < 2; i++) {
4639 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];
4640 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];
4646 GET_MQUANT(); // p. 227
4647 s->current_picture.qscale_table[mb_pos] = mquant;
4648 if (!v->ttmbf && cbp)
4649 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4650 for (i = 0; i < 6; i++) {
4651 s->dc_val[0][s->block_index[i]] = 0;
4653 val = ((cbp >> (5 - i)) & 1);
4655 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4657 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4659 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4660 first_block, s->dest[dst_idx] + off,
4661 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4662 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4663 block_cbp |= pat << (i << 2);
4664 if (!v->ttmbf && ttmb < 8)
4672 for (i = 0; i < 6; i++) {
4673 v->mb_type[0][s->block_index[i]] = 0;
4674 s->dc_val[0][s->block_index[i]] = 0;
4676 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4677 s->current_picture.qscale_table[mb_pos] = 0;
4678 v->blk_mv_type[s->block_index[0]] = 0;
4679 v->blk_mv_type[s->block_index[1]] = 0;
4680 v->blk_mv_type[s->block_index[2]] = 0;
4681 v->blk_mv_type[s->block_index[3]] = 0;
4684 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4685 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4686 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4688 dir = bmvtype == BMV_TYPE_BACKWARD;
4689 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4694 for (i = 0; i < 2; i++) {
4695 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];
4696 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];
4697 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];
4698 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];
4701 v->blk_mv_type[s->block_index[0]] = 1;
4702 v->blk_mv_type[s->block_index[1]] = 1;
4703 v->blk_mv_type[s->block_index[2]] = 1;
4704 v->blk_mv_type[s->block_index[3]] = 1;
4705 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4706 for (i = 0; i < 2; i++) {
4707 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];
4708 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];
4715 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4720 if (s->mb_x == s->mb_width - 1)
4721 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4722 v->cbp[s->mb_x] = block_cbp;
4723 v->ttblk[s->mb_x] = block_tt;
4727 /** Decode blocks of I-frame
4729 static void vc1_decode_i_blocks(VC1Context *v)
4732 MpegEncContext *s = &v->s;
4737 /* select codingmode used for VLC tables selection */
4738 switch (v->y_ac_table_index) {
4740 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4743 v->codingset = CS_HIGH_MOT_INTRA;
4746 v->codingset = CS_MID_RATE_INTRA;
4750 switch (v->c_ac_table_index) {
4752 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4755 v->codingset2 = CS_HIGH_MOT_INTER;
4758 v->codingset2 = CS_MID_RATE_INTER;
4762 /* Set DC scale - y and c use the same */
4763 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4764 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4767 s->mb_x = s->mb_y = 0;
4769 s->first_slice_line = 1;
4770 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4772 init_block_index(v);
4773 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4775 ff_update_block_index(s);
4776 dst[0] = s->dest[0];
4777 dst[1] = dst[0] + 8;
4778 dst[2] = s->dest[0] + s->linesize * 8;
4779 dst[3] = dst[2] + 8;
4780 dst[4] = s->dest[1];
4781 dst[5] = s->dest[2];
4782 s->dsp.clear_blocks(s->block[0]);
4783 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4784 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4785 s->current_picture.qscale_table[mb_pos] = v->pq;
4786 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4787 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4789 // do actual MB decoding and displaying
4790 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4791 v->s.ac_pred = get_bits1(&v->s.gb);
4793 for (k = 0; k < 6; k++) {
4794 val = ((cbp >> (5 - k)) & 1);
4797 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4801 cbp |= val << (5 - k);
4803 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4805 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4807 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4808 if (v->pq >= 9 && v->overlap) {
4810 for (j = 0; j < 64; j++)
4811 s->block[k][j] <<= 1;
4812 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4815 for (j = 0; j < 64; j++)
4816 s->block[k][j] = (s->block[k][j] - 64) << 1;
4817 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4821 if (v->pq >= 9 && v->overlap) {
4823 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4824 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4825 if (!(s->flags & CODEC_FLAG_GRAY)) {
4826 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4827 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4830 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4831 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4832 if (!s->first_slice_line) {
4833 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4834 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4835 if (!(s->flags & CODEC_FLAG_GRAY)) {
4836 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4837 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4840 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4841 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4843 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4845 if (get_bits_count(&s->gb) > v->bits) {
4846 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4847 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4848 get_bits_count(&s->gb), v->bits);
4852 if (!v->s.loop_filter)
4853 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4855 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4857 s->first_slice_line = 0;
4859 if (v->s.loop_filter)
4860 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4862 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4863 * profile, these only differ are when decoding MSS2 rectangles. */
4864 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4867 /** Decode blocks of I-frame for advanced profile
4869 static void vc1_decode_i_blocks_adv(VC1Context *v)
4872 MpegEncContext *s = &v->s;
4878 GetBitContext *gb = &s->gb;
4880 /* select codingmode used for VLC tables selection */
4881 switch (v->y_ac_table_index) {
4883 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4886 v->codingset = CS_HIGH_MOT_INTRA;
4889 v->codingset = CS_MID_RATE_INTRA;
4893 switch (v->c_ac_table_index) {
4895 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4898 v->codingset2 = CS_HIGH_MOT_INTER;
4901 v->codingset2 = CS_MID_RATE_INTER;
4906 s->mb_x = s->mb_y = 0;
4908 s->first_slice_line = 1;
4909 s->mb_y = s->start_mb_y;
4910 if (s->start_mb_y) {
4912 init_block_index(v);
4913 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4914 (1 + s->b8_stride) * sizeof(*s->coded_block));
4916 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4918 init_block_index(v);
4919 for (;s->mb_x < s->mb_width; s->mb_x++) {
4920 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4921 ff_update_block_index(s);
4922 s->dsp.clear_blocks(block[0]);
4923 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4924 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4925 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4926 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4928 // do actual MB decoding and displaying
4929 if (v->fieldtx_is_raw)
4930 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4931 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4932 if ( v->acpred_is_raw)
4933 v->s.ac_pred = get_bits1(&v->s.gb);
4935 v->s.ac_pred = v->acpred_plane[mb_pos];
4937 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4938 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4942 s->current_picture.qscale_table[mb_pos] = mquant;
4943 /* Set DC scale - y and c use the same */
4944 s->y_dc_scale = s->y_dc_scale_table[mquant];
4945 s->c_dc_scale = s->c_dc_scale_table[mquant];
4947 for (k = 0; k < 6; k++) {
4948 val = ((cbp >> (5 - k)) & 1);
4951 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4955 cbp |= val << (5 - k);
4957 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4958 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4960 vc1_decode_i_block_adv(v, block[k], k, val,
4961 (k < 4) ? v->codingset : v->codingset2, mquant);
4963 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4965 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4968 vc1_smooth_overlap_filter_iblk(v);
4969 vc1_put_signed_blocks_clamped(v);
4970 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4972 if (get_bits_count(&s->gb) > v->bits) {
4973 // TODO: may need modification to handle slice coding
4974 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4975 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4976 get_bits_count(&s->gb), v->bits);
4980 if (!v->s.loop_filter)
4981 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4983 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4984 s->first_slice_line = 0;
4987 /* raw bottom MB row */
4989 init_block_index(v);
4991 for (;s->mb_x < s->mb_width; s->mb_x++) {
4992 ff_update_block_index(s);
4993 vc1_put_signed_blocks_clamped(v);
4994 if (v->s.loop_filter)
4995 vc1_loop_filter_iblk_delayed(v, v->pq);
4997 if (v->s.loop_filter)
4998 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4999 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5000 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5003 static void vc1_decode_p_blocks(VC1Context *v)
5005 MpegEncContext *s = &v->s;
5006 int apply_loop_filter;
5008 /* select codingmode used for VLC tables selection */
5009 switch (v->c_ac_table_index) {
5011 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5014 v->codingset = CS_HIGH_MOT_INTRA;
5017 v->codingset = CS_MID_RATE_INTRA;
5021 switch (v->c_ac_table_index) {
5023 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5026 v->codingset2 = CS_HIGH_MOT_INTER;
5029 v->codingset2 = CS_MID_RATE_INTER;
5033 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5034 v->fcm == PROGRESSIVE;
5035 s->first_slice_line = 1;
5036 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5037 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5039 init_block_index(v);
5040 for (; s->mb_x < s->mb_width; s->mb_x++) {
5041 ff_update_block_index(s);
5043 if (v->fcm == ILACE_FIELD)
5044 vc1_decode_p_mb_intfi(v);
5045 else if (v->fcm == ILACE_FRAME)
5046 vc1_decode_p_mb_intfr(v);
5047 else vc1_decode_p_mb(v);
5048 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5049 vc1_apply_p_loop_filter(v);
5050 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5051 // TODO: may need modification to handle slice coding
5052 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5053 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5054 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5058 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5059 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5060 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5061 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5062 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5063 s->first_slice_line = 0;
5065 if (apply_loop_filter) {
5067 init_block_index(v);
5068 for (; s->mb_x < s->mb_width; s->mb_x++) {
5069 ff_update_block_index(s);
5070 vc1_apply_p_loop_filter(v);
5073 if (s->end_mb_y >= s->start_mb_y)
5074 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5075 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5076 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5079 static void vc1_decode_b_blocks(VC1Context *v)
5081 MpegEncContext *s = &v->s;
5083 /* select codingmode used for VLC tables selection */
5084 switch (v->c_ac_table_index) {
5086 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5089 v->codingset = CS_HIGH_MOT_INTRA;
5092 v->codingset = CS_MID_RATE_INTRA;
5096 switch (v->c_ac_table_index) {
5098 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5101 v->codingset2 = CS_HIGH_MOT_INTER;
5104 v->codingset2 = CS_MID_RATE_INTER;
5108 s->first_slice_line = 1;
5109 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5111 init_block_index(v);
5112 for (; s->mb_x < s->mb_width; s->mb_x++) {
5113 ff_update_block_index(s);
5115 if (v->fcm == ILACE_FIELD)
5116 vc1_decode_b_mb_intfi(v);
5117 else if (v->fcm == ILACE_FRAME)
5118 vc1_decode_b_mb_intfr(v);
5121 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5122 // TODO: may need modification to handle slice coding
5123 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5124 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5125 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5128 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5130 if (!v->s.loop_filter)
5131 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5133 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5134 s->first_slice_line = 0;
5136 if (v->s.loop_filter)
5137 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5138 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5139 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5142 static void vc1_decode_skip_blocks(VC1Context *v)
5144 MpegEncContext *s = &v->s;
5146 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5147 s->first_slice_line = 1;
5148 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5150 init_block_index(v);
5151 ff_update_block_index(s);
5152 if (s->last_picture.f.data[0]) {
5153 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5154 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5155 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5157 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5158 s->first_slice_line = 0;
5160 s->pict_type = AV_PICTURE_TYPE_P;
5163 void ff_vc1_decode_blocks(VC1Context *v)
5166 v->s.esc3_level_length = 0;
5168 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5171 v->left_blk_idx = -1;
5172 v->topleft_blk_idx = 1;
5174 switch (v->s.pict_type) {
5175 case AV_PICTURE_TYPE_I:
5176 if (v->profile == PROFILE_ADVANCED)
5177 vc1_decode_i_blocks_adv(v);
5179 vc1_decode_i_blocks(v);
5181 case AV_PICTURE_TYPE_P:
5182 if (v->p_frame_skipped)
5183 vc1_decode_skip_blocks(v);
5185 vc1_decode_p_blocks(v);
5187 case AV_PICTURE_TYPE_B:
5189 if (v->profile == PROFILE_ADVANCED)
5190 vc1_decode_i_blocks_adv(v);
5192 vc1_decode_i_blocks(v);
5194 vc1_decode_b_blocks(v);
5200 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5204 * Transform coefficients for both sprites in 16.16 fixed point format,
5205 * in the order they appear in the bitstream:
5207 * rotation 1 (unused)
5209 * rotation 2 (unused)
5216 int effect_type, effect_flag;
5217 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5218 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5221 static inline int get_fp_val(GetBitContext* gb)
5223 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5226 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5230 switch (get_bits(gb, 2)) {
5233 c[2] = get_fp_val(gb);
5237 c[0] = c[4] = get_fp_val(gb);
5238 c[2] = get_fp_val(gb);
5241 c[0] = get_fp_val(gb);
5242 c[2] = get_fp_val(gb);
5243 c[4] = get_fp_val(gb);
5246 c[0] = get_fp_val(gb);
5247 c[1] = get_fp_val(gb);
5248 c[2] = get_fp_val(gb);
5249 c[3] = get_fp_val(gb);
5250 c[4] = get_fp_val(gb);
5253 c[5] = get_fp_val(gb);
5255 c[6] = get_fp_val(gb);
5260 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5262 AVCodecContext *avctx = v->s.avctx;
5265 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5266 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5267 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5268 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5269 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5270 for (i = 0; i < 7; i++)
5271 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5272 sd->coefs[sprite][i] / (1<<16),
5273 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5274 av_log(avctx, AV_LOG_DEBUG, "\n");
5278 if (sd->effect_type = get_bits_long(gb, 30)) {
5279 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5281 vc1_sprite_parse_transform(gb, sd->effect_params1);
5284 vc1_sprite_parse_transform(gb, sd->effect_params1);
5285 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5288 for (i = 0; i < sd->effect_pcount1; i++)
5289 sd->effect_params1[i] = get_fp_val(gb);
5291 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5292 // effect 13 is simple alpha blending and matches the opacity above
5293 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5294 for (i = 0; i < sd->effect_pcount1; i++)
5295 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5296 sd->effect_params1[i] / (1 << 16),
5297 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5298 av_log(avctx, AV_LOG_DEBUG, "\n");
5301 sd->effect_pcount2 = get_bits(gb, 16);
5302 if (sd->effect_pcount2 > 10) {
5303 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5305 } else if (sd->effect_pcount2) {
5307 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5308 while (++i < sd->effect_pcount2) {
5309 sd->effect_params2[i] = get_fp_val(gb);
5310 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5311 sd->effect_params2[i] / (1 << 16),
5312 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5314 av_log(avctx, AV_LOG_DEBUG, "\n");
5317 if (sd->effect_flag = get_bits1(gb))
5318 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5320 if (get_bits_count(gb) >= gb->size_in_bits +
5321 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
5322 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5323 if (get_bits_count(gb) < gb->size_in_bits - 8)
5324 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5327 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5329 int i, plane, row, sprite;
5330 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5331 uint8_t* src_h[2][2];
5332 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5334 MpegEncContext *s = &v->s;
5336 for (i = 0; i < 2; i++) {
5337 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5338 xadv[i] = sd->coefs[i][0];
5339 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5340 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5342 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5343 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5345 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5347 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5348 int width = v->output_width>>!!plane;
5350 for (row = 0; row < v->output_height>>!!plane; row++) {
5351 uint8_t *dst = v->sprite_output_frame.data[plane] +
5352 v->sprite_output_frame.linesize[plane] * row;
5354 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5355 uint8_t *iplane = s->current_picture.f.data[plane];
5356 int iline = s->current_picture.f.linesize[plane];
5357 int ycoord = yoff[sprite] + yadv[sprite] * row;
5358 int yline = ycoord >> 16;
5360 ysub[sprite] = ycoord & 0xFFFF;
5362 iplane = s->last_picture.f.data[plane];
5363 iline = s->last_picture.f.linesize[plane];
5365 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5366 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5367 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5369 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5371 if (sr_cache[sprite][0] != yline) {
5372 if (sr_cache[sprite][1] == yline) {
5373 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5374 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5376 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5377 sr_cache[sprite][0] = yline;
5380 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5381 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5382 iplane + next_line, xoff[sprite],
5383 xadv[sprite], width);
5384 sr_cache[sprite][1] = yline + 1;
5386 src_h[sprite][0] = v->sr_rows[sprite][0];
5387 src_h[sprite][1] = v->sr_rows[sprite][1];
5391 if (!v->two_sprites) {
5393 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5395 memcpy(dst, src_h[0][0], width);
5398 if (ysub[0] && ysub[1]) {
5399 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5400 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5401 } else if (ysub[0]) {
5402 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5403 src_h[1][0], alpha, width);
5404 } else if (ysub[1]) {
5405 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5406 src_h[0][0], (1<<16)-1-alpha, width);
5408 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5414 for (i = 0; i < 2; i++) {
5424 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5427 MpegEncContext *s = &v->s;
5428 AVCodecContext *avctx = s->avctx;
5431 vc1_parse_sprites(v, gb, &sd);
5433 if (!s->current_picture.f.data[0]) {
5434 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5438 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5439 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5443 av_frame_unref(&v->sprite_output_frame);
5444 if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
5447 vc1_draw_sprites(v, &sd);
5452 static void vc1_sprite_flush(AVCodecContext *avctx)
5454 VC1Context *v = avctx->priv_data;
5455 MpegEncContext *s = &v->s;
5456 AVFrame *f = &s->current_picture.f;
5459 /* Windows Media Image codecs have a convergence interval of two keyframes.
5460 Since we can't enforce it, clear to black the missing sprite. This is
5461 wrong but it looks better than doing nothing. */
5464 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5465 for (i = 0; i < v->sprite_height>>!!plane; i++)
5466 memset(f->data[plane] + i * f->linesize[plane],
5467 plane ? 128 : 0, f->linesize[plane]);
5472 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5474 MpegEncContext *s = &v->s;
5477 /* Allocate mb bitplanes */
5478 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5479 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5480 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5481 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5482 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5483 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5485 v->n_allocated_blks = s->mb_width + 2;
5486 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5487 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5488 v->cbp = v->cbp_base + s->mb_stride;
5489 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5490 v->ttblk = v->ttblk_base + s->mb_stride;
5491 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5492 v->is_intra = v->is_intra_base + s->mb_stride;
5493 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5494 v->luma_mv = v->luma_mv_base + s->mb_stride;
5496 /* allocate block type info in that way so it could be used with s->block_index[] */
5497 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5498 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5499 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5500 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5502 /* allocate memory to store block level MV info */
5503 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5504 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5505 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5506 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5507 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5508 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5509 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5510 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);
5512 /* Init coded blocks info */
5513 if (v->profile == PROFILE_ADVANCED) {
5514 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5516 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5520 ff_intrax8_common_init(&v->x8,s);
5522 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5523 for (i = 0; i < 4; i++)
5524 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5527 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5528 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5535 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5538 for (i = 0; i < 64; i++) {
5539 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5540 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5541 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5542 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5543 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5544 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5550 /** Initialize a VC1/WMV3 decoder
5551 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5552 * @todo TODO: Decypher remaining bits in extra_data
5554 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5556 VC1Context *v = avctx->priv_data;
5557 MpegEncContext *s = &v->s;
5560 /* save the container output size for WMImage */
5561 v->output_width = avctx->width;
5562 v->output_height = avctx->height;
5564 if (!avctx->extradata_size || !avctx->extradata)
5566 if (!(avctx->flags & CODEC_FLAG_GRAY))
5567 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5569 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5570 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5572 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5573 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5575 if (ff_vc1_init_common(v) < 0)
5577 // ensure static VLC tables are initialized
5578 if (ff_msmpeg4_decode_init(avctx) < 0)
5580 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5582 // Hack to ensure the above functions will be called
5583 // again once we know all necessary settings.
5584 // That this is necessary might indicate a bug.
5585 ff_vc1_decode_end(avctx);
5587 ff_h264chroma_init(&v->h264chroma, 8);
5588 ff_vc1dsp_init(&v->vc1dsp);
5590 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5593 // looks like WMV3 has a sequence header stored in the extradata
5594 // advanced sequence header may be before the first frame
5595 // the last byte of the extradata is a version number, 1 for the
5596 // samples we can decode
5598 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5600 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5603 count = avctx->extradata_size*8 - get_bits_count(&gb);
5605 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5606 count, get_bits(&gb, count));
5607 } else if (count < 0) {
5608 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5610 } else { // VC1/WVC1/WVP2
5611 const uint8_t *start = avctx->extradata;
5612 uint8_t *end = avctx->extradata + avctx->extradata_size;
5613 const uint8_t *next;
5614 int size, buf2_size;
5615 uint8_t *buf2 = NULL;
5616 int seq_initialized = 0, ep_initialized = 0;
5618 if (avctx->extradata_size < 16) {
5619 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5623 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5624 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5626 for (; next < end; start = next) {
5627 next = find_next_marker(start + 4, end);
5628 size = next - start - 4;
5631 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5632 init_get_bits(&gb, buf2, buf2_size * 8);
5633 switch (AV_RB32(start)) {
5634 case VC1_CODE_SEQHDR:
5635 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5639 seq_initialized = 1;
5641 case VC1_CODE_ENTRYPOINT:
5642 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5651 if (!seq_initialized || !ep_initialized) {
5652 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5655 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5658 avctx->profile = v->profile;
5659 if (v->profile == PROFILE_ADVANCED)
5660 avctx->level = v->level;
5662 avctx->has_b_frames = !!avctx->max_b_frames;
5664 s->mb_width = (avctx->coded_width + 15) >> 4;
5665 s->mb_height = (avctx->coded_height + 15) >> 4;
5667 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5668 ff_vc1_init_transposed_scantables(v);
5670 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5675 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5676 v->sprite_width = avctx->coded_width;
5677 v->sprite_height = avctx->coded_height;
5679 avctx->coded_width = avctx->width = v->output_width;
5680 avctx->coded_height = avctx->height = v->output_height;
5682 // prevent 16.16 overflows
5683 if (v->sprite_width > 1 << 14 ||
5684 v->sprite_height > 1 << 14 ||
5685 v->output_width > 1 << 14 ||
5686 v->output_height > 1 << 14) return -1;
5688 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5689 avpriv_request_sample(avctx, "odd sprites support");
5690 return AVERROR_PATCHWELCOME;
5696 /** Close a VC1/WMV3 decoder
5697 * @warning Initial try at using MpegEncContext stuff
5699 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5701 VC1Context *v = avctx->priv_data;
5704 av_frame_unref(&v->sprite_output_frame);
5706 for (i = 0; i < 4; i++)
5707 av_freep(&v->sr_rows[i >> 1][i & 1]);
5708 av_freep(&v->hrd_rate);
5709 av_freep(&v->hrd_buffer);
5710 ff_MPV_common_end(&v->s);
5711 av_freep(&v->mv_type_mb_plane);
5712 av_freep(&v->direct_mb_plane);
5713 av_freep(&v->forward_mb_plane);
5714 av_freep(&v->fieldtx_plane);
5715 av_freep(&v->acpred_plane);
5716 av_freep(&v->over_flags_plane);
5717 av_freep(&v->mb_type_base);
5718 av_freep(&v->blk_mv_type_base);
5719 av_freep(&v->mv_f_base);
5720 av_freep(&v->mv_f_next_base);
5721 av_freep(&v->block);
5722 av_freep(&v->cbp_base);
5723 av_freep(&v->ttblk_base);
5724 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5725 av_freep(&v->luma_mv_base);
5726 ff_intrax8_common_end(&v->x8);
5731 /** Decode a VC1/WMV3 frame
5732 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5734 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5735 int *got_frame, AVPacket *avpkt)
5737 const uint8_t *buf = avpkt->data;
5738 int buf_size = avpkt->size, n_slices = 0, i, ret;
5739 VC1Context *v = avctx->priv_data;
5740 MpegEncContext *s = &v->s;
5741 AVFrame *pict = data;
5742 uint8_t *buf2 = NULL;
5743 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5744 int mb_height, n_slices1=-1;
5749 } *slices = NULL, *tmp;
5751 v->second_field = 0;
5753 if(s->flags & CODEC_FLAG_LOW_DELAY)
5756 /* no supplementary picture */
5757 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5758 /* special case for last picture */
5759 if (s->low_delay == 0 && s->next_picture_ptr) {
5760 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
5762 s->next_picture_ptr = NULL;
5770 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5771 if (v->profile < PROFILE_ADVANCED)
5772 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5774 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5777 //for advanced profile we may need to parse and unescape data
5778 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5780 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5782 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5783 const uint8_t *start, *end, *next;
5787 for (start = buf, end = buf + buf_size; next < end; start = next) {
5788 next = find_next_marker(start + 4, end);
5789 size = next - start - 4;
5790 if (size <= 0) continue;
5791 switch (AV_RB32(start)) {
5792 case VC1_CODE_FRAME:
5793 if (avctx->hwaccel ||
5794 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5796 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5798 case VC1_CODE_FIELD: {
5800 if (avctx->hwaccel ||
5801 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5802 buf_start_second_field = start;
5803 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5807 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5808 if (!slices[n_slices].buf)
5810 buf_size3 = vc1_unescape_buffer(start + 4, size,
5811 slices[n_slices].buf);
5812 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5814 /* assuming that the field marker is at the exact middle,
5815 hope it's correct */
5816 slices[n_slices].mby_start = s->mb_height >> 1;
5817 n_slices1 = n_slices - 1; // index of the last slice of the first field
5821 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5822 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5823 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5824 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5826 case VC1_CODE_SLICE: {
5828 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5832 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5833 if (!slices[n_slices].buf)
5835 buf_size3 = vc1_unescape_buffer(start + 4, size,
5836 slices[n_slices].buf);
5837 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5839 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5845 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5846 const uint8_t *divider;
5849 divider = find_next_marker(buf, buf + buf_size);
5850 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5851 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5853 } else { // found field marker, unescape second field
5854 if (avctx->hwaccel ||
5855 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5856 buf_start_second_field = divider;
5857 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5861 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5862 if (!slices[n_slices].buf)
5864 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5865 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5867 slices[n_slices].mby_start = s->mb_height >> 1;
5868 n_slices1 = n_slices - 1;
5871 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5873 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5875 init_get_bits(&s->gb, buf2, buf_size2*8);
5877 init_get_bits(&s->gb, buf, buf_size*8);
5879 if (v->res_sprite) {
5880 v->new_sprite = !get_bits1(&s->gb);
5881 v->two_sprites = get_bits1(&s->gb);
5882 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5883 we're using the sprite compositor. These are intentionally kept separate
5884 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5885 the vc1 one for WVP2 */
5886 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5887 if (v->new_sprite) {
5888 // switch AVCodecContext parameters to those of the sprites
5889 avctx->width = avctx->coded_width = v->sprite_width;
5890 avctx->height = avctx->coded_height = v->sprite_height;
5897 if (s->context_initialized &&
5898 (s->width != avctx->coded_width ||
5899 s->height != avctx->coded_height)) {
5900 ff_vc1_decode_end(avctx);
5903 if (!s->context_initialized) {
5904 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5907 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5909 if (v->profile == PROFILE_ADVANCED) {
5910 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5912 s->h_edge_pos = avctx->coded_width;
5913 s->v_edge_pos = avctx->coded_height;
5917 /* We need to set current_picture_ptr before reading the header,
5918 * otherwise we cannot store anything in there. */
5919 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5920 int i = ff_find_unused_picture(s, 0);
5923 s->current_picture_ptr = &s->picture[i];
5926 // do parse frame header
5927 v->pic_header_flag = 0;
5928 v->first_pic_header_flag = 1;
5929 if (v->profile < PROFILE_ADVANCED) {
5930 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5934 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5938 v->first_pic_header_flag = 0;
5940 if (avctx->debug & FF_DEBUG_PICT_INFO)
5941 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5943 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5944 && s->pict_type != AV_PICTURE_TYPE_I) {
5945 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5949 if ((s->mb_height >> v->field_mode) == 0) {
5950 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5954 // process pulldown flags
5955 s->current_picture_ptr->f.repeat_pict = 0;
5956 // Pulldown flags are only valid when 'broadcast' has been set.
5957 // So ticks_per_frame will be 2
5960 s->current_picture_ptr->f.repeat_pict = 1;
5961 } else if (v->rptfrm) {
5963 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5966 // for skipping the frame
5967 s->current_picture.f.pict_type = s->pict_type;
5968 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5970 /* skip B-frames if we don't have reference frames */
5971 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5974 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5975 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5976 avctx->skip_frame >= AVDISCARD_ALL) {
5980 if (s->next_p_frame_damaged) {
5981 if (s->pict_type == AV_PICTURE_TYPE_B)
5984 s->next_p_frame_damaged = 0;
5987 if (ff_MPV_frame_start(s, avctx) < 0) {
5991 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5992 v->s.current_picture_ptr->f.top_field_first = v->tff;
5994 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5995 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5997 if ((CONFIG_VC1_VDPAU_DECODER)
5998 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5999 if (v->field_mode && buf_start_second_field) {
6000 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6001 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6003 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6005 } else if (avctx->hwaccel) {
6006 if (v->field_mode && buf_start_second_field) {
6007 // decode first field
6008 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6009 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6011 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6013 if (avctx->hwaccel->end_frame(avctx) < 0)
6016 // decode second field
6017 s->gb = slices[n_slices1 + 1].gb;
6018 s->picture_structure = PICT_TOP_FIELD + v->tff;
6019 v->second_field = 1;
6020 v->pic_header_flag = 0;
6021 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6022 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6025 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
6027 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6029 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6031 if (avctx->hwaccel->end_frame(avctx) < 0)
6034 s->picture_structure = PICT_FRAME;
6035 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6037 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6039 if (avctx->hwaccel->end_frame(avctx) < 0)
6046 ff_mpeg_er_frame_start(s);
6048 v->bits = buf_size * 8;
6049 v->end_mb_x = s->mb_width;
6050 if (v->field_mode) {
6051 s->current_picture.f.linesize[0] <<= 1;
6052 s->current_picture.f.linesize[1] <<= 1;
6053 s->current_picture.f.linesize[2] <<= 1;
6055 s->uvlinesize <<= 1;
6057 mb_height = s->mb_height >> v->field_mode;
6058 for (i = 0; i <= n_slices; i++) {
6059 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6060 if (v->field_mode <= 0) {
6061 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6062 "picture boundary (%d >= %d)\n", i,
6063 slices[i - 1].mby_start, mb_height);
6066 v->second_field = 1;
6067 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6068 v->mb_off = s->mb_stride * s->mb_height >> 1;
6070 v->second_field = 0;
6075 v->pic_header_flag = 0;
6076 if (v->field_mode && i == n_slices1 + 2) {
6077 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6078 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6081 } else if (get_bits1(&s->gb)) {
6082 v->pic_header_flag = 1;
6083 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6084 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6091 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6092 if (!v->field_mode || v->second_field)
6093 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6095 if (i >= n_slices) {
6096 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6099 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6101 if (s->end_mb_y <= s->start_mb_y) {
6102 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6105 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6106 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6109 ff_vc1_decode_blocks(v);
6111 s->gb = slices[i].gb;
6113 if (v->field_mode) {
6114 v->second_field = 0;
6115 s->current_picture.f.linesize[0] >>= 1;
6116 s->current_picture.f.linesize[1] >>= 1;
6117 s->current_picture.f.linesize[2] >>= 1;
6119 s->uvlinesize >>= 1;
6120 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6121 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6122 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6125 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6126 get_bits_count(&s->gb), s->gb.size_in_bits);
6127 // if (get_bits_count(&s->gb) > buf_size * 8)
6129 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6132 ff_er_frame_end(&s->er);
6135 ff_MPV_frame_end(s);
6137 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6139 avctx->width = avctx->coded_width = v->output_width;
6140 avctx->height = avctx->coded_height = v->output_height;
6141 if (avctx->skip_frame >= AVDISCARD_NONREF)
6143 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6144 if (vc1_decode_sprites(v, &s->gb))
6147 if ((ret = av_frame_ref(pict, &v->sprite_output_frame)) < 0)
6151 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6152 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
6154 ff_print_debug_info(s, s->current_picture_ptr, pict);
6155 } else if (s->last_picture_ptr != NULL) {
6156 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
6158 ff_print_debug_info(s, s->last_picture_ptr, pict);
6160 if (s->last_picture_ptr || s->low_delay) {
6167 for (i = 0; i < n_slices; i++)
6168 av_free(slices[i].buf);
6174 for (i = 0; i < n_slices; i++)
6175 av_free(slices[i].buf);
6181 static const AVProfile profiles[] = {
6182 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6183 { FF_PROFILE_VC1_MAIN, "Main" },
6184 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6185 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6186 { FF_PROFILE_UNKNOWN },
6189 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6191 AV_PIX_FMT_DXVA2_VLD,
6194 AV_PIX_FMT_VAAPI_VLD,
6203 AVCodec ff_vc1_decoder = {
6205 .type = AVMEDIA_TYPE_VIDEO,
6206 .id = AV_CODEC_ID_VC1,
6207 .priv_data_size = sizeof(VC1Context),
6208 .init = vc1_decode_init,
6209 .close = ff_vc1_decode_end,
6210 .decode = vc1_decode_frame,
6211 .flush = ff_mpeg_flush,
6212 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6213 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6214 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6215 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6218 #if CONFIG_WMV3_DECODER
6219 AVCodec ff_wmv3_decoder = {
6221 .type = AVMEDIA_TYPE_VIDEO,
6222 .id = AV_CODEC_ID_WMV3,
6223 .priv_data_size = sizeof(VC1Context),
6224 .init = vc1_decode_init,
6225 .close = ff_vc1_decode_end,
6226 .decode = vc1_decode_frame,
6227 .flush = ff_mpeg_flush,
6228 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6229 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6230 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6231 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6235 #if CONFIG_WMV3_VDPAU_DECODER
6236 AVCodec ff_wmv3_vdpau_decoder = {
6237 .name = "wmv3_vdpau",
6238 .type = AVMEDIA_TYPE_VIDEO,
6239 .id = AV_CODEC_ID_WMV3,
6240 .priv_data_size = sizeof(VC1Context),
6241 .init = vc1_decode_init,
6242 .close = ff_vc1_decode_end,
6243 .decode = vc1_decode_frame,
6244 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6245 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6246 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6247 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6251 #if CONFIG_VC1_VDPAU_DECODER
6252 AVCodec ff_vc1_vdpau_decoder = {
6253 .name = "vc1_vdpau",
6254 .type = AVMEDIA_TYPE_VIDEO,
6255 .id = AV_CODEC_ID_VC1,
6256 .priv_data_size = sizeof(VC1Context),
6257 .init = vc1_decode_init,
6258 .close = ff_vc1_decode_end,
6259 .decode = vc1_decode_frame,
6260 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6261 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6262 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6263 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6267 #if CONFIG_WMV3IMAGE_DECODER
6268 AVCodec ff_wmv3image_decoder = {
6269 .name = "wmv3image",
6270 .type = AVMEDIA_TYPE_VIDEO,
6271 .id = AV_CODEC_ID_WMV3IMAGE,
6272 .priv_data_size = sizeof(VC1Context),
6273 .init = vc1_decode_init,
6274 .close = ff_vc1_decode_end,
6275 .decode = vc1_decode_frame,
6276 .capabilities = CODEC_CAP_DR1,
6277 .flush = vc1_sprite_flush,
6278 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6279 .pix_fmts = ff_pixfmt_list_420
6283 #if CONFIG_VC1IMAGE_DECODER
6284 AVCodec ff_vc1image_decoder = {
6286 .type = AVMEDIA_TYPE_VIDEO,
6287 .id = AV_CODEC_ID_VC1IMAGE,
6288 .priv_data_size = sizeof(VC1Context),
6289 .init = vc1_decode_init,
6290 .close = ff_vc1_decode_end,
6291 .decode = vc1_decode_frame,
6292 .capabilities = CODEC_CAP_DR1,
6293 .flush = vc1_sprite_flush,
6294 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6295 .pix_fmts = ff_pixfmt_list_420