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 "libavutil/avassert.h"
46 #define MB_INTRA_VLC_BITS 9
50 // offset tables for interlaced picture MVDATA decoding
51 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
52 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
54 /***********************************************************************/
56 * @name VC-1 Bitplane decoding
74 /** @} */ //imode defines
76 static void init_block_index(VC1Context *v)
78 MpegEncContext *s = &v->s;
79 ff_init_block_index(s);
80 if (v->field_mode && !(v->second_field ^ v->tff)) {
81 s->dest[0] += s->current_picture_ptr->f.linesize[0];
82 s->dest[1] += s->current_picture_ptr->f.linesize[1];
83 s->dest[2] += s->current_picture_ptr->f.linesize[2];
87 /** @} */ //Bitplane group
89 static void vc1_put_signed_blocks_clamped(VC1Context *v)
91 MpegEncContext *s = &v->s;
92 int topleft_mb_pos, top_mb_pos;
93 int stride_y, fieldtx = 0;
96 /* The put pixels loop is always one MB row behind the decoding loop,
97 * because we can only put pixels when overlap filtering is done, and
98 * for filtering of the bottom edge of a MB, we need the next MB row
100 * Within the row, the put pixels loop is also one MB col behind the
101 * decoding loop. The reason for this is again, because for filtering
102 * of the right MB edge, we need the next MB present. */
103 if (!s->first_slice_line) {
105 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
106 if (v->fcm == ILACE_FRAME)
107 fieldtx = v->fieldtx_plane[topleft_mb_pos];
108 stride_y = s->linesize << fieldtx;
109 v_dist = (16 - fieldtx) >> (fieldtx == 0);
110 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
111 s->dest[0] - 16 * s->linesize - 16,
113 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
114 s->dest[0] - 16 * s->linesize - 8,
116 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
117 s->dest[0] - v_dist * s->linesize - 16,
119 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
120 s->dest[0] - v_dist * s->linesize - 8,
122 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
123 s->dest[1] - 8 * s->uvlinesize - 8,
125 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
126 s->dest[2] - 8 * s->uvlinesize - 8,
129 if (s->mb_x == s->mb_width - 1) {
130 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
131 if (v->fcm == ILACE_FRAME)
132 fieldtx = v->fieldtx_plane[top_mb_pos];
133 stride_y = s->linesize << fieldtx;
134 v_dist = fieldtx ? 15 : 8;
135 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
136 s->dest[0] - 16 * s->linesize,
138 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
139 s->dest[0] - 16 * s->linesize + 8,
141 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
142 s->dest[0] - v_dist * s->linesize,
144 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
145 s->dest[0] - v_dist * s->linesize + 8,
147 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
148 s->dest[1] - 8 * s->uvlinesize,
150 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
151 s->dest[2] - 8 * s->uvlinesize,
156 #define inc_blk_idx(idx) do { \
158 if (idx >= v->n_allocated_blks) \
162 inc_blk_idx(v->topleft_blk_idx);
163 inc_blk_idx(v->top_blk_idx);
164 inc_blk_idx(v->left_blk_idx);
165 inc_blk_idx(v->cur_blk_idx);
168 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
170 MpegEncContext *s = &v->s;
172 if (!s->first_slice_line) {
173 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
176 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
177 for (j = 0; j < 2; j++) {
178 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
180 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
183 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
185 if (s->mb_y == s->end_mb_y - 1) {
187 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
188 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
189 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
191 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
195 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
197 MpegEncContext *s = &v->s;
200 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
201 * means it runs two rows/cols behind the decoding loop. */
202 if (!s->first_slice_line) {
204 if (s->mb_y >= s->start_mb_y + 2) {
205 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
208 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
209 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
210 for (j = 0; j < 2; j++) {
211 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
213 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
217 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
220 if (s->mb_x == s->mb_width - 1) {
221 if (s->mb_y >= s->start_mb_y + 2) {
222 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
225 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
226 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
227 for (j = 0; j < 2; j++) {
228 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
230 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
234 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
237 if (s->mb_y == s->end_mb_y) {
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
243 for (j = 0; j < 2; j++) {
244 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
249 if (s->mb_x == s->mb_width - 1) {
251 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
252 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
254 for (j = 0; j < 2; j++) {
255 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
263 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
265 MpegEncContext *s = &v->s;
268 if (v->condover == CONDOVER_NONE)
271 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
273 /* Within a MB, the horizontal overlap always runs before the vertical.
274 * To accomplish that, we run the H on left and internal borders of the
275 * currently decoded MB. Then, we wait for the next overlap iteration
276 * to do H overlap on the right edge of this MB, before moving over and
277 * running the V overlap. Therefore, the V overlap makes us trail by one
278 * MB col and the H overlap filter makes us trail by one MB row. This
279 * is reflected in the time at which we run the put_pixels loop. */
280 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
281 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
282 v->over_flags_plane[mb_pos - 1])) {
283 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
284 v->block[v->cur_blk_idx][0]);
285 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
286 v->block[v->cur_blk_idx][2]);
287 if (!(s->flags & CODEC_FLAG_GRAY)) {
288 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
289 v->block[v->cur_blk_idx][4]);
290 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
291 v->block[v->cur_blk_idx][5]);
294 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
295 v->block[v->cur_blk_idx][1]);
296 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
297 v->block[v->cur_blk_idx][3]);
299 if (s->mb_x == s->mb_width - 1) {
300 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
301 v->over_flags_plane[mb_pos - s->mb_stride])) {
302 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
303 v->block[v->cur_blk_idx][0]);
304 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
305 v->block[v->cur_blk_idx][1]);
306 if (!(s->flags & CODEC_FLAG_GRAY)) {
307 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
308 v->block[v->cur_blk_idx][4]);
309 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
310 v->block[v->cur_blk_idx][5]);
313 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
314 v->block[v->cur_blk_idx][2]);
315 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
316 v->block[v->cur_blk_idx][3]);
319 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
320 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
321 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
322 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
323 v->block[v->left_blk_idx][0]);
324 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
325 v->block[v->left_blk_idx][1]);
326 if (!(s->flags & CODEC_FLAG_GRAY)) {
327 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
328 v->block[v->left_blk_idx][4]);
329 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
330 v->block[v->left_blk_idx][5]);
333 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
334 v->block[v->left_blk_idx][2]);
335 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
336 v->block[v->left_blk_idx][3]);
340 /** Do motion compensation over 1 macroblock
341 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
343 static void vc1_mc_1mv(VC1Context *v, int dir)
345 MpegEncContext *s = &v->s;
346 H264ChromaContext *h264chroma = &v->h264chroma;
347 uint8_t *srcY, *srcU, *srcV;
348 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
349 int v_edge_pos = s->v_edge_pos >> v->field_mode;
351 uint8_t (*luty)[256], (*lutuv)[256];
354 if ((!v->field_mode ||
355 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
356 !v->s.last_picture.f.data[0])
359 mx = s->mv[dir][0][0];
360 my = s->mv[dir][0][1];
362 // store motion vectors for further use in B frames
363 if (s->pict_type == AV_PICTURE_TYPE_P) {
364 for (i = 0; i < 4; i++) {
365 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
366 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
370 uvmx = (mx + ((mx & 3) == 3)) >> 1;
371 uvmy = (my + ((my & 3) == 3)) >> 1;
372 v->luma_mv[s->mb_x][0] = uvmx;
373 v->luma_mv[s->mb_x][1] = uvmy;
376 v->cur_field_type != v->ref_field_type[dir]) {
377 my = my - 2 + 4 * v->cur_field_type;
378 uvmy = uvmy - 2 + 4 * v->cur_field_type;
381 // fastuvmc shall be ignored for interlaced frame picture
382 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
383 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
384 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
387 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
388 srcY = s->current_picture.f.data[0];
389 srcU = s->current_picture.f.data[1];
390 srcV = s->current_picture.f.data[2];
392 lutuv = v->curr_lutuv;
393 use_ic = v->curr_use_ic;
395 srcY = s->last_picture.f.data[0];
396 srcU = s->last_picture.f.data[1];
397 srcV = s->last_picture.f.data[2];
399 lutuv = v->last_lutuv;
400 use_ic = v->last_use_ic;
403 srcY = s->next_picture.f.data[0];
404 srcU = s->next_picture.f.data[1];
405 srcV = s->next_picture.f.data[2];
407 lutuv = v->next_lutuv;
408 use_ic = v->next_use_ic;
411 if (!srcY || !srcU) {
412 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
416 src_x = s->mb_x * 16 + (mx >> 2);
417 src_y = s->mb_y * 16 + (my >> 2);
418 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
419 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
421 if (v->profile != PROFILE_ADVANCED) {
422 src_x = av_clip( src_x, -16, s->mb_width * 16);
423 src_y = av_clip( src_y, -16, s->mb_height * 16);
424 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
425 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
427 src_x = av_clip( src_x, -17, s->avctx->coded_width);
428 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
429 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
430 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
433 srcY += src_y * s->linesize + src_x;
434 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
435 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
437 if (v->field_mode && v->ref_field_type[dir]) {
438 srcY += s->current_picture_ptr->f.linesize[0];
439 srcU += s->current_picture_ptr->f.linesize[1];
440 srcV += s->current_picture_ptr->f.linesize[2];
443 /* for grayscale we should not try to read from unknown area */
444 if (s->flags & CODEC_FLAG_GRAY) {
445 srcU = s->edge_emu_buffer + 18 * s->linesize;
446 srcV = s->edge_emu_buffer + 18 * s->linesize;
449 if (v->rangeredfrm || use_ic
450 || s->h_edge_pos < 22 || v_edge_pos < 22
451 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
452 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
453 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
455 srcY -= s->mspel * (1 + s->linesize);
456 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
457 17 + s->mspel * 2, 17 + s->mspel * 2,
458 src_x - s->mspel, src_y - s->mspel,
459 s->h_edge_pos, v_edge_pos);
460 srcY = s->edge_emu_buffer;
461 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
462 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
463 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
464 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
467 /* if we deal with range reduction we need to scale source blocks */
468 if (v->rangeredfrm) {
473 for (j = 0; j < 17 + s->mspel * 2; j++) {
474 for (i = 0; i < 17 + s->mspel * 2; i++)
475 src[i] = ((src[i] - 128) >> 1) + 128;
480 for (j = 0; j < 9; j++) {
481 for (i = 0; i < 9; i++) {
482 src[i] = ((src[i] - 128) >> 1) + 128;
483 src2[i] = ((src2[i] - 128) >> 1) + 128;
485 src += s->uvlinesize;
486 src2 += s->uvlinesize;
489 /* if we deal with intensity compensation we need to scale source blocks */
495 for (j = 0; j < 17 + s->mspel * 2; j++) {
496 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
497 for (i = 0; i < 17 + s->mspel * 2; i++)
498 src[i] = luty[f][src[i]];
503 for (j = 0; j < 9; j++) {
504 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
505 for (i = 0; i < 9; i++) {
506 src[i] = lutuv[f][src[i]];
507 src2[i] = lutuv[f][src2[i]];
509 src += s->uvlinesize;
510 src2 += s->uvlinesize;
513 srcY += s->mspel * (1 + s->linesize);
517 dxy = ((my & 3) << 2) | (mx & 3);
518 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
519 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
520 srcY += s->linesize * 8;
521 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
522 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
523 } else { // hpel mc - always used for luma
524 dxy = (my & 2) | ((mx & 2) >> 1);
526 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
528 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
531 if (s->flags & CODEC_FLAG_GRAY) return;
532 /* Chroma MC always uses qpel bilinear */
533 uvmx = (uvmx & 3) << 1;
534 uvmy = (uvmy & 3) << 1;
536 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
537 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
539 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
540 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
544 static inline int median4(int a, int b, int c, int d)
547 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
548 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
550 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
551 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
555 /** Do motion compensation for 4-MV macroblock - luminance block
557 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
559 MpegEncContext *s = &v->s;
561 int dxy, mx, my, src_x, src_y;
563 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
564 int v_edge_pos = s->v_edge_pos >> v->field_mode;
565 uint8_t (*luty)[256];
568 if ((!v->field_mode ||
569 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
570 !v->s.last_picture.f.data[0])
573 mx = s->mv[dir][n][0];
574 my = s->mv[dir][n][1];
577 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
578 srcY = s->current_picture.f.data[0];
580 use_ic = v->curr_use_ic;
582 srcY = s->last_picture.f.data[0];
584 use_ic = v->last_use_ic;
587 srcY = s->next_picture.f.data[0];
589 use_ic = v->next_use_ic;
593 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
598 if (v->cur_field_type != v->ref_field_type[dir])
599 my = my - 2 + 4 * v->cur_field_type;
602 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
603 int same_count = 0, opp_count = 0, k;
604 int chosen_mv[2][4][2], f;
606 for (k = 0; k < 4; k++) {
607 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
608 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
609 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
613 f = opp_count > same_count;
614 switch (f ? opp_count : same_count) {
616 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
617 chosen_mv[f][2][0], chosen_mv[f][3][0]);
618 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
619 chosen_mv[f][2][1], chosen_mv[f][3][1]);
622 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
623 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
626 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
627 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
632 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
633 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
634 for (k = 0; k < 4; k++)
635 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
638 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
640 int width = s->avctx->coded_width;
641 int height = s->avctx->coded_height >> 1;
642 if (s->pict_type == AV_PICTURE_TYPE_P) {
643 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
644 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
646 qx = (s->mb_x * 16) + (mx >> 2);
647 qy = (s->mb_y * 8) + (my >> 3);
652 mx -= 4 * (qx - width);
655 else if (qy > height + 1)
656 my -= 8 * (qy - height - 1);
659 if ((v->fcm == ILACE_FRAME) && fieldmv)
660 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
662 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
664 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
666 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
668 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
670 if (v->profile != PROFILE_ADVANCED) {
671 src_x = av_clip(src_x, -16, s->mb_width * 16);
672 src_y = av_clip(src_y, -16, s->mb_height * 16);
674 src_x = av_clip(src_x, -17, s->avctx->coded_width);
675 if (v->fcm == ILACE_FRAME) {
677 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
679 src_y = av_clip(src_y, -18, s->avctx->coded_height);
681 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
685 srcY += src_y * s->linesize + src_x;
686 if (v->field_mode && v->ref_field_type[dir])
687 srcY += s->current_picture_ptr->f.linesize[0];
689 if (fieldmv && !(src_y & 1))
691 if (fieldmv && (src_y & 1) && src_y < 4)
693 if (v->rangeredfrm || use_ic
694 || s->h_edge_pos < 13 || v_edge_pos < 23
695 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
696 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
697 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
698 /* check emulate edge stride and offset */
699 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
700 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
701 src_x - s->mspel, src_y - (s->mspel << fieldmv),
702 s->h_edge_pos, v_edge_pos);
703 srcY = s->edge_emu_buffer;
704 /* if we deal with range reduction we need to scale source blocks */
705 if (v->rangeredfrm) {
710 for (j = 0; j < 9 + s->mspel * 2; j++) {
711 for (i = 0; i < 9 + s->mspel * 2; i++)
712 src[i] = ((src[i] - 128) >> 1) + 128;
713 src += s->linesize << fieldmv;
716 /* if we deal with intensity compensation we need to scale source blocks */
722 for (j = 0; j < 9 + s->mspel * 2; j++) {
723 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
724 for (i = 0; i < 9 + s->mspel * 2; i++)
725 src[i] = luty[f][src[i]];
726 src += s->linesize << fieldmv;
729 srcY += s->mspel * (1 + (s->linesize << fieldmv));
733 dxy = ((my & 3) << 2) | (mx & 3);
735 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
737 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
738 } else { // hpel mc - always used for luma
739 dxy = (my & 2) | ((mx & 2) >> 1);
741 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
743 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
747 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
750 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
752 idx = ((a[3] != flag) << 3)
753 | ((a[2] != flag) << 2)
754 | ((a[1] != flag) << 1)
757 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
758 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
760 } else if (count[idx] == 1) {
763 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
764 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
767 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
768 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
771 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
772 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
775 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
776 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
779 } else if (count[idx] == 2) {
781 for (i = 0; i < 3; i++)
786 for (i = t1 + 1; i < 4; i++)
791 *tx = (mvx[t1] + mvx[t2]) / 2;
792 *ty = (mvy[t1] + mvy[t2]) / 2;
800 /** Do motion compensation for 4-MV macroblock - both chroma blocks
802 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
804 MpegEncContext *s = &v->s;
805 H264ChromaContext *h264chroma = &v->h264chroma;
806 uint8_t *srcU, *srcV;
807 int uvmx, uvmy, uvsrc_x, uvsrc_y;
808 int k, tx = 0, ty = 0;
809 int mvx[4], mvy[4], intra[4], mv_f[4];
811 int chroma_ref_type = v->cur_field_type;
812 int v_edge_pos = s->v_edge_pos >> v->field_mode;
813 uint8_t (*lutuv)[256];
816 if (!v->field_mode && !v->s.last_picture.f.data[0])
818 if (s->flags & CODEC_FLAG_GRAY)
821 for (k = 0; k < 4; k++) {
822 mvx[k] = s->mv[dir][k][0];
823 mvy[k] = s->mv[dir][k][1];
824 intra[k] = v->mb_type[0][s->block_index[k]];
826 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
829 /* calculate chroma MV vector from four luma MVs */
830 if (!v->field_mode || (v->field_mode && !v->numref)) {
831 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
832 chroma_ref_type = v->reffield;
834 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
835 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
836 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
837 return; //no need to do MC for intra blocks
841 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
843 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
845 chroma_ref_type = !v->cur_field_type;
847 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
849 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
850 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
851 uvmx = (tx + ((tx & 3) == 3)) >> 1;
852 uvmy = (ty + ((ty & 3) == 3)) >> 1;
854 v->luma_mv[s->mb_x][0] = uvmx;
855 v->luma_mv[s->mb_x][1] = uvmy;
858 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
859 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
861 // Field conversion bias
862 if (v->cur_field_type != chroma_ref_type)
863 uvmy += 2 - 4 * chroma_ref_type;
865 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
866 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
868 if (v->profile != PROFILE_ADVANCED) {
869 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
870 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
872 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
873 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
877 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
878 srcU = s->current_picture.f.data[1];
879 srcV = s->current_picture.f.data[2];
880 lutuv = v->curr_lutuv;
881 use_ic = v->curr_use_ic;
883 srcU = s->last_picture.f.data[1];
884 srcV = s->last_picture.f.data[2];
885 lutuv = v->last_lutuv;
886 use_ic = v->last_use_ic;
889 srcU = s->next_picture.f.data[1];
890 srcV = s->next_picture.f.data[2];
891 lutuv = v->next_lutuv;
892 use_ic = v->next_use_ic;
896 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
900 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
901 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
904 if (chroma_ref_type) {
905 srcU += s->current_picture_ptr->f.linesize[1];
906 srcV += s->current_picture_ptr->f.linesize[2];
910 if (v->rangeredfrm || use_ic
911 || s->h_edge_pos < 18 || v_edge_pos < 18
912 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
913 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
914 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
915 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
916 s->h_edge_pos >> 1, v_edge_pos >> 1);
917 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
918 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
919 s->h_edge_pos >> 1, v_edge_pos >> 1);
920 srcU = s->edge_emu_buffer;
921 srcV = s->edge_emu_buffer + 16;
923 /* if we deal with range reduction we need to scale source blocks */
924 if (v->rangeredfrm) {
930 for (j = 0; j < 9; j++) {
931 for (i = 0; i < 9; i++) {
932 src[i] = ((src[i] - 128) >> 1) + 128;
933 src2[i] = ((src2[i] - 128) >> 1) + 128;
935 src += s->uvlinesize;
936 src2 += s->uvlinesize;
939 /* if we deal with intensity compensation we need to scale source blocks */
946 for (j = 0; j < 9; j++) {
947 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
948 for (i = 0; i < 9; i++) {
949 src[i] = lutuv[f][src[i]];
950 src2[i] = lutuv[f][src2[i]];
952 src += s->uvlinesize;
953 src2 += s->uvlinesize;
958 /* Chroma MC always uses qpel bilinear */
959 uvmx = (uvmx & 3) << 1;
960 uvmy = (uvmy & 3) << 1;
962 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
963 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
965 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
966 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
970 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
972 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
974 MpegEncContext *s = &v->s;
975 H264ChromaContext *h264chroma = &v->h264chroma;
976 uint8_t *srcU, *srcV;
977 int uvsrc_x, uvsrc_y;
978 int uvmx_field[4], uvmy_field[4];
980 int fieldmv = v->blk_mv_type[s->block_index[0]];
981 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
982 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
983 int v_edge_pos = s->v_edge_pos >> 1;
985 uint8_t (*lutuv)[256];
987 if (s->flags & CODEC_FLAG_GRAY)
990 for (i = 0; i < 4; i++) {
991 int d = i < 2 ? dir: dir2;
993 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
996 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
998 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
1001 for (i = 0; i < 4; i++) {
1002 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
1003 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1004 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1005 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1006 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1007 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1008 if (i < 2 ? dir : dir2) {
1009 srcU = s->next_picture.f.data[1];
1010 srcV = s->next_picture.f.data[2];
1011 lutuv = v->next_lutuv;
1012 use_ic = v->next_use_ic;
1014 srcU = s->last_picture.f.data[1];
1015 srcV = s->last_picture.f.data[2];
1016 lutuv = v->last_lutuv;
1017 use_ic = v->last_use_ic;
1021 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1022 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1023 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1024 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1026 if (fieldmv && !(uvsrc_y & 1))
1027 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1029 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1032 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1033 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1034 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1035 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1036 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1037 s->h_edge_pos >> 1, v_edge_pos);
1038 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1039 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1040 s->h_edge_pos >> 1, v_edge_pos);
1041 srcU = s->edge_emu_buffer;
1042 srcV = s->edge_emu_buffer + 16;
1044 /* if we deal with intensity compensation we need to scale source blocks */
1047 uint8_t *src, *src2;
1051 for (j = 0; j < 5; j++) {
1052 int f = (uvsrc_y + (j << fieldmv)) & 1;
1053 for (i = 0; i < 5; i++) {
1054 src[i] = lutuv[f][src[i]];
1055 src2[i] = lutuv[f][src2[i]];
1057 src += s->uvlinesize << fieldmv;
1058 src2 += s->uvlinesize << fieldmv;
1064 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1065 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1067 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]);
1068 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]);
1072 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1073 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1075 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]);
1076 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]);
1082 /***********************************************************************/
1084 * @name VC-1 Block-level functions
1085 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1091 * @brief Get macroblock-level quantizer scale
1093 #define GET_MQUANT() \
1094 if (v->dquantfrm) { \
1096 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1097 if (v->dqbilevel) { \
1098 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1100 mqdiff = get_bits(gb, 3); \
1102 mquant = v->pq + mqdiff; \
1104 mquant = get_bits(gb, 5); \
1107 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1108 edges = 1 << v->dqsbedge; \
1109 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1110 edges = (3 << v->dqsbedge) % 15; \
1111 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1113 if ((edges&1) && !s->mb_x) \
1114 mquant = v->altpq; \
1115 if ((edges&2) && s->first_slice_line) \
1116 mquant = v->altpq; \
1117 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1118 mquant = v->altpq; \
1119 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1120 mquant = v->altpq; \
1121 if (!mquant || mquant > 31) { \
1122 av_log(v->s.avctx, AV_LOG_ERROR, \
1123 "Overriding invalid mquant %d\n", mquant); \
1129 * @def GET_MVDATA(_dmv_x, _dmv_y)
1130 * @brief Get MV differentials
1131 * @see MVDATA decoding from 8.3.5.2, p(1)20
1132 * @param _dmv_x Horizontal differential for decoded MV
1133 * @param _dmv_y Vertical differential for decoded MV
1135 #define GET_MVDATA(_dmv_x, _dmv_y) \
1136 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1137 VC1_MV_DIFF_VLC_BITS, 2); \
1139 mb_has_coeffs = 1; \
1142 mb_has_coeffs = 0; \
1145 _dmv_x = _dmv_y = 0; \
1146 } else if (index == 35) { \
1147 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1148 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1149 } else if (index == 36) { \
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_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1163 index1 = index / 6; \
1164 if (!s->quarter_sample && index1 == 5) val = 1; \
1166 if (size_table[index1] - val > 0) \
1167 val = get_bits(gb, size_table[index1] - val); \
1169 sign = 0 - (val & 1); \
1170 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1173 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1174 int *dmv_y, int *pred_flag)
1177 int extend_x = 0, extend_y = 0;
1178 GetBitContext *gb = &v->s.gb;
1181 const int* offs_tab;
1184 bits = VC1_2REF_MVDATA_VLC_BITS;
1187 bits = VC1_1REF_MVDATA_VLC_BITS;
1190 switch (v->dmvrange) {
1198 extend_x = extend_y = 1;
1201 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1203 *dmv_x = get_bits(gb, v->k_x);
1204 *dmv_y = get_bits(gb, v->k_y);
1207 *pred_flag = *dmv_y & 1;
1208 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1210 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1215 av_assert0(index < esc);
1217 offs_tab = offset_table2;
1219 offs_tab = offset_table1;
1220 index1 = (index + 1) % 9;
1222 val = get_bits(gb, index1 + extend_x);
1223 sign = 0 -(val & 1);
1224 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1228 offs_tab = offset_table2;
1230 offs_tab = offset_table1;
1231 index1 = (index + 1) / 9;
1232 if (index1 > v->numref) {
1233 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1234 sign = 0 - (val & 1);
1235 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1238 if (v->numref && pred_flag)
1239 *pred_flag = index1 & 1;
1243 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1245 int scaledvalue, refdist;
1246 int scalesame1, scalesame2;
1247 int scalezone1_x, zone1offset_x;
1248 int table_index = dir ^ v->second_field;
1250 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1251 refdist = v->refdist;
1253 refdist = dir ? v->brfd : v->frfd;
1256 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1257 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1258 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1259 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1264 if (FFABS(n) < scalezone1_x)
1265 scaledvalue = (n * scalesame1) >> 8;
1268 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1270 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1273 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1276 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1278 int scaledvalue, refdist;
1279 int scalesame1, scalesame2;
1280 int scalezone1_y, zone1offset_y;
1281 int table_index = dir ^ v->second_field;
1283 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1284 refdist = v->refdist;
1286 refdist = dir ? v->brfd : v->frfd;
1289 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1290 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1291 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1292 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1297 if (FFABS(n) < scalezone1_y)
1298 scaledvalue = (n * scalesame1) >> 8;
1301 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1303 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1307 if (v->cur_field_type && !v->ref_field_type[dir])
1308 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1310 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1313 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1315 int scalezone1_x, zone1offset_x;
1316 int scaleopp1, scaleopp2, brfd;
1319 brfd = FFMIN(v->brfd, 3);
1320 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1321 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1322 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1323 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1328 if (FFABS(n) < scalezone1_x)
1329 scaledvalue = (n * scaleopp1) >> 8;
1332 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1334 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1337 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1340 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1342 int scalezone1_y, zone1offset_y;
1343 int scaleopp1, scaleopp2, brfd;
1346 brfd = FFMIN(v->brfd, 3);
1347 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1348 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1349 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1350 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1355 if (FFABS(n) < scalezone1_y)
1356 scaledvalue = (n * scaleopp1) >> 8;
1359 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1361 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1364 if (v->cur_field_type && !v->ref_field_type[dir]) {
1365 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1367 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1371 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1374 int brfd, scalesame;
1375 int hpel = 1 - v->s.quarter_sample;
1378 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1380 n = scaleforsame_y(v, i, n, dir) << hpel;
1382 n = scaleforsame_x(v, n, dir) << hpel;
1385 brfd = FFMIN(v->brfd, 3);
1386 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1388 n = (n * scalesame >> 8) << hpel;
1392 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1395 int refdist, scaleopp;
1396 int hpel = 1 - v->s.quarter_sample;
1399 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1401 n = scaleforopp_y(v, n, dir) << hpel;
1403 n = scaleforopp_x(v, n) << hpel;
1406 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1407 refdist = FFMIN(v->refdist, 3);
1409 refdist = dir ? v->brfd : v->frfd;
1410 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1412 n = (n * scaleopp >> 8) << hpel;
1416 /** Predict and set motion vector
1418 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1419 int mv1, int r_x, int r_y, uint8_t* is_intra,
1420 int pred_flag, int dir)
1422 MpegEncContext *s = &v->s;
1423 int xy, wrap, off = 0;
1427 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1428 int opposite, a_f, b_f, c_f;
1429 int16_t field_predA[2];
1430 int16_t field_predB[2];
1431 int16_t field_predC[2];
1432 int a_valid, b_valid, c_valid;
1433 int hybridmv_thresh, y_bias = 0;
1435 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1436 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1440 /* scale MV difference to be quad-pel */
1441 dmv_x <<= 1 - s->quarter_sample;
1442 dmv_y <<= 1 - s->quarter_sample;
1444 wrap = s->b8_stride;
1445 xy = s->block_index[n];
1448 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1449 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1450 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1451 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1452 if (mv1) { /* duplicate motion data for 1-MV block */
1453 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1454 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1455 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1456 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1457 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1458 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1459 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1460 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1461 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1462 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1463 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1464 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1465 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1470 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1471 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1473 if (v->field_mode && mixedmv_pic)
1474 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1476 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1478 //in 4-MV mode different blocks have different B predictor position
1481 off = (s->mb_x > 0) ? -1 : 1;
1484 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1493 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1495 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1496 b_valid = a_valid && (s->mb_width > 1);
1497 c_valid = s->mb_x || (n == 1 || n == 3);
1498 if (v->field_mode) {
1499 a_valid = a_valid && !is_intra[xy - wrap];
1500 b_valid = b_valid && !is_intra[xy - wrap + off];
1501 c_valid = c_valid && !is_intra[xy - 1];
1505 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1506 num_oppfield += a_f;
1507 num_samefield += 1 - a_f;
1508 field_predA[0] = A[0];
1509 field_predA[1] = A[1];
1511 field_predA[0] = field_predA[1] = 0;
1515 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1516 num_oppfield += b_f;
1517 num_samefield += 1 - b_f;
1518 field_predB[0] = B[0];
1519 field_predB[1] = B[1];
1521 field_predB[0] = field_predB[1] = 0;
1525 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1526 num_oppfield += c_f;
1527 num_samefield += 1 - c_f;
1528 field_predC[0] = C[0];
1529 field_predC[1] = C[1];
1531 field_predC[0] = field_predC[1] = 0;
1535 if (v->field_mode) {
1537 // REFFIELD determines if the last field or the second-last field is
1538 // to be used as reference
1539 opposite = 1 - v->reffield;
1541 if (num_samefield <= num_oppfield)
1542 opposite = 1 - pred_flag;
1544 opposite = pred_flag;
1549 if (a_valid && !a_f) {
1550 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1551 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1553 if (b_valid && !b_f) {
1554 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1555 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1557 if (c_valid && !c_f) {
1558 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1559 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1561 v->mv_f[dir][xy + v->blocks_off] = 1;
1562 v->ref_field_type[dir] = !v->cur_field_type;
1564 if (a_valid && a_f) {
1565 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1566 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1568 if (b_valid && b_f) {
1569 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1570 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1572 if (c_valid && c_f) {
1573 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1574 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1576 v->mv_f[dir][xy + v->blocks_off] = 0;
1577 v->ref_field_type[dir] = v->cur_field_type;
1581 px = field_predA[0];
1582 py = field_predA[1];
1583 } else if (c_valid) {
1584 px = field_predC[0];
1585 py = field_predC[1];
1586 } else if (b_valid) {
1587 px = field_predB[0];
1588 py = field_predB[1];
1594 if (num_samefield + num_oppfield > 1) {
1595 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1596 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1599 /* Pullback MV as specified in 8.3.5.3.4 */
1600 if (!v->field_mode) {
1602 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1603 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1604 X = (s->mb_width << 6) - 4;
1605 Y = (s->mb_height << 6) - 4;
1607 if (qx + px < -60) px = -60 - qx;
1608 if (qy + py < -60) py = -60 - qy;
1610 if (qx + px < -28) px = -28 - qx;
1611 if (qy + py < -28) py = -28 - qy;
1613 if (qx + px > X) px = X - qx;
1614 if (qy + py > Y) py = Y - qy;
1617 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1618 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1619 hybridmv_thresh = 32;
1620 if (a_valid && c_valid) {
1621 if (is_intra[xy - wrap])
1622 sum = FFABS(px) + FFABS(py);
1624 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1625 if (sum > hybridmv_thresh) {
1626 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1627 px = field_predA[0];
1628 py = field_predA[1];
1630 px = field_predC[0];
1631 py = field_predC[1];
1634 if (is_intra[xy - 1])
1635 sum = FFABS(px) + FFABS(py);
1637 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1638 if (sum > hybridmv_thresh) {
1639 if (get_bits1(&s->gb)) {
1640 px = field_predA[0];
1641 py = field_predA[1];
1643 px = field_predC[0];
1644 py = field_predC[1];
1651 if (v->field_mode && v->numref)
1653 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1655 /* store MV using signed modulus of MV range defined in 4.11 */
1656 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;
1657 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;
1658 if (mv1) { /* duplicate motion data for 1-MV block */
1659 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1660 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1661 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1662 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1663 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1664 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1665 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1666 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];
1670 /** Predict and set motion vector for interlaced frame picture MBs
1672 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1673 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1675 MpegEncContext *s = &v->s;
1676 int xy, wrap, off = 0;
1677 int A[2], B[2], C[2];
1679 int a_valid = 0, b_valid = 0, c_valid = 0;
1680 int field_a, field_b, field_c; // 0: same, 1: opposit
1681 int total_valid, num_samefield, num_oppfield;
1682 int pos_c, pos_b, n_adj;
1684 wrap = s->b8_stride;
1685 xy = s->block_index[n];
1688 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1689 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1690 s->current_picture.motion_val[1][xy][0] = 0;
1691 s->current_picture.motion_val[1][xy][1] = 0;
1692 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1693 s->current_picture.motion_val[0][xy + 1][0] = 0;
1694 s->current_picture.motion_val[0][xy + 1][1] = 0;
1695 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1696 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1697 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1698 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1699 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1700 s->current_picture.motion_val[1][xy + 1][0] = 0;
1701 s->current_picture.motion_val[1][xy + 1][1] = 0;
1702 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1703 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1704 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1705 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1710 off = ((n == 0) || (n == 1)) ? 1 : -1;
1712 if (s->mb_x || (n == 1) || (n == 3)) {
1713 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1714 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1715 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1716 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1718 } else { // current block has frame mv and cand. has field MV (so average)
1719 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1720 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1721 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1722 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1725 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1731 /* Predict B and C */
1732 B[0] = B[1] = C[0] = C[1] = 0;
1733 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1734 if (!s->first_slice_line) {
1735 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1738 pos_b = s->block_index[n_adj] - 2 * wrap;
1739 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1740 n_adj = (n & 2) | (n & 1);
1742 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1743 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1744 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1745 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1746 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1749 if (s->mb_width > 1) {
1750 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1753 pos_c = s->block_index[2] - 2 * wrap + 2;
1754 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1757 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1758 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1759 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1760 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1761 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1763 if (s->mb_x == s->mb_width - 1) {
1764 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1767 pos_c = s->block_index[3] - 2 * wrap - 2;
1768 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1771 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1772 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1773 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1774 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1775 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1784 pos_b = s->block_index[1];
1786 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1787 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1788 pos_c = s->block_index[0];
1790 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1791 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1794 total_valid = a_valid + b_valid + c_valid;
1795 // check if predictor A is out of bounds
1796 if (!s->mb_x && !(n == 1 || n == 3)) {
1799 // check if predictor B is out of bounds
1800 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1801 B[0] = B[1] = C[0] = C[1] = 0;
1803 if (!v->blk_mv_type[xy]) {
1804 if (s->mb_width == 1) {
1808 if (total_valid >= 2) {
1809 px = mid_pred(A[0], B[0], C[0]);
1810 py = mid_pred(A[1], B[1], C[1]);
1811 } else if (total_valid) {
1812 if (a_valid) { px = A[0]; py = A[1]; }
1813 else if (b_valid) { px = B[0]; py = B[1]; }
1814 else if (c_valid) { px = C[0]; py = C[1]; }
1820 field_a = (A[1] & 4) ? 1 : 0;
1824 field_b = (B[1] & 4) ? 1 : 0;
1828 field_c = (C[1] & 4) ? 1 : 0;
1832 num_oppfield = field_a + field_b + field_c;
1833 num_samefield = total_valid - num_oppfield;
1834 if (total_valid == 3) {
1835 if ((num_samefield == 3) || (num_oppfield == 3)) {
1836 px = mid_pred(A[0], B[0], C[0]);
1837 py = mid_pred(A[1], B[1], C[1]);
1838 } else if (num_samefield >= num_oppfield) {
1839 /* take one MV from same field set depending on priority
1840 the check for B may not be necessary */
1841 px = !field_a ? A[0] : B[0];
1842 py = !field_a ? A[1] : B[1];
1844 px = field_a ? A[0] : B[0];
1845 py = field_a ? A[1] : B[1];
1847 } else if (total_valid == 2) {
1848 if (num_samefield >= num_oppfield) {
1849 if (!field_a && a_valid) {
1852 } else if (!field_b && b_valid) {
1855 } else /*if (c_valid)*/ {
1856 av_assert1(c_valid);
1859 } /*else px = py = 0;*/
1861 if (field_a && a_valid) {
1864 } else /*if (field_b && b_valid)*/ {
1865 av_assert1(field_b && b_valid);
1868 } /*else if (c_valid) {
1873 } else if (total_valid == 1) {
1874 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1875 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1879 /* store MV using signed modulus of MV range defined in 4.11 */
1880 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1881 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1882 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1883 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1884 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1885 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1886 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1887 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1888 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1889 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1890 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1891 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1892 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1893 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1897 /** Motion compensation for direct or interpolated blocks in B-frames
1899 static void vc1_interp_mc(VC1Context *v)
1901 MpegEncContext *s = &v->s;
1902 H264ChromaContext *h264chroma = &v->h264chroma;
1903 uint8_t *srcY, *srcU, *srcV;
1904 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1906 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1907 int use_ic = v->next_use_ic;
1909 if (!v->field_mode && !v->s.next_picture.f.data[0])
1912 mx = s->mv[1][0][0];
1913 my = s->mv[1][0][1];
1914 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1915 uvmy = (my + ((my & 3) == 3)) >> 1;
1916 if (v->field_mode) {
1917 if (v->cur_field_type != v->ref_field_type[1])
1918 my = my - 2 + 4 * v->cur_field_type;
1919 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1922 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1923 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1925 srcY = s->next_picture.f.data[0];
1926 srcU = s->next_picture.f.data[1];
1927 srcV = s->next_picture.f.data[2];
1929 src_x = s->mb_x * 16 + (mx >> 2);
1930 src_y = s->mb_y * 16 + (my >> 2);
1931 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1932 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1934 if (v->profile != PROFILE_ADVANCED) {
1935 src_x = av_clip( src_x, -16, s->mb_width * 16);
1936 src_y = av_clip( src_y, -16, s->mb_height * 16);
1937 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1938 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1940 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1941 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1942 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1943 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1946 srcY += src_y * s->linesize + src_x;
1947 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1948 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1950 if (v->field_mode && v->ref_field_type[1]) {
1951 srcY += s->current_picture_ptr->f.linesize[0];
1952 srcU += s->current_picture_ptr->f.linesize[1];
1953 srcV += s->current_picture_ptr->f.linesize[2];
1956 /* for grayscale we should not try to read from unknown area */
1957 if (s->flags & CODEC_FLAG_GRAY) {
1958 srcU = s->edge_emu_buffer + 18 * s->linesize;
1959 srcV = s->edge_emu_buffer + 18 * s->linesize;
1962 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1963 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1964 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1965 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1967 srcY -= s->mspel * (1 + s->linesize);
1968 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1969 17 + s->mspel * 2, 17 + s->mspel * 2,
1970 src_x - s->mspel, src_y - s->mspel,
1971 s->h_edge_pos, v_edge_pos);
1972 srcY = s->edge_emu_buffer;
1973 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1974 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1975 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1976 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1979 /* if we deal with range reduction we need to scale source blocks */
1980 if (v->rangeredfrm) {
1982 uint8_t *src, *src2;
1985 for (j = 0; j < 17 + s->mspel * 2; j++) {
1986 for (i = 0; i < 17 + s->mspel * 2; i++)
1987 src[i] = ((src[i] - 128) >> 1) + 128;
1992 for (j = 0; j < 9; j++) {
1993 for (i = 0; i < 9; i++) {
1994 src[i] = ((src[i] - 128) >> 1) + 128;
1995 src2[i] = ((src2[i] - 128) >> 1) + 128;
1997 src += s->uvlinesize;
1998 src2 += s->uvlinesize;
2003 uint8_t (*luty )[256] = v->next_luty;
2004 uint8_t (*lutuv)[256] = v->next_lutuv;
2006 uint8_t *src, *src2;
2009 for (j = 0; j < 17 + s->mspel * 2; j++) {
2010 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2011 for (i = 0; i < 17 + s->mspel * 2; i++)
2012 src[i] = luty[f][src[i]];
2017 for (j = 0; j < 9; j++) {
2018 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2019 for (i = 0; i < 9; i++) {
2020 src[i] = lutuv[f][src[i]];
2021 src2[i] = lutuv[f][src2[i]];
2023 src += s->uvlinesize;
2024 src2 += s->uvlinesize;
2027 srcY += s->mspel * (1 + s->linesize);
2034 dxy = ((my & 3) << 2) | (mx & 3);
2035 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2036 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2037 srcY += s->linesize * 8;
2038 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2039 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2041 dxy = (my & 2) | ((mx & 2) >> 1);
2044 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2046 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2049 if (s->flags & CODEC_FLAG_GRAY) return;
2050 /* Chroma MC always uses qpel blilinear */
2051 uvmx = (uvmx & 3) << 1;
2052 uvmy = (uvmy & 3) << 1;
2054 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2055 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2057 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2058 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2062 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2066 #if B_FRACTION_DEN==256
2070 return 2 * ((value * n + 255) >> 9);
2071 return (value * n + 128) >> 8;
2074 n -= B_FRACTION_DEN;
2076 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2077 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2081 /** Reconstruct motion vector for B-frame and do motion compensation
2083 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2084 int direct, int mode)
2091 if (mode == BMV_TYPE_INTERPOLATED) {
2097 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2100 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2101 int direct, int mvtype)
2103 MpegEncContext *s = &v->s;
2104 int xy, wrap, off = 0;
2109 const uint8_t *is_intra = v->mb_type[0];
2113 /* scale MV difference to be quad-pel */
2114 dmv_x[0] <<= 1 - s->quarter_sample;
2115 dmv_y[0] <<= 1 - s->quarter_sample;
2116 dmv_x[1] <<= 1 - s->quarter_sample;
2117 dmv_y[1] <<= 1 - s->quarter_sample;
2119 wrap = s->b8_stride;
2120 xy = s->block_index[0];
2123 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2124 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2125 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2126 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2129 if (!v->field_mode) {
2130 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2131 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2132 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2133 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2135 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2136 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));
2137 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));
2138 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));
2139 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));
2142 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2143 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2144 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2145 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2149 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2150 C = s->current_picture.motion_val[0][xy - 2];
2151 A = s->current_picture.motion_val[0][xy - wrap * 2];
2152 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2153 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2155 if (!s->mb_x) C[0] = C[1] = 0;
2156 if (!s->first_slice_line) { // predictor A is not out of bounds
2157 if (s->mb_width == 1) {
2161 px = mid_pred(A[0], B[0], C[0]);
2162 py = mid_pred(A[1], B[1], C[1]);
2164 } else if (s->mb_x) { // predictor C is not out of bounds
2170 /* Pullback MV as specified in 8.3.5.3.4 */
2173 if (v->profile < PROFILE_ADVANCED) {
2174 qx = (s->mb_x << 5);
2175 qy = (s->mb_y << 5);
2176 X = (s->mb_width << 5) - 4;
2177 Y = (s->mb_height << 5) - 4;
2178 if (qx + px < -28) px = -28 - qx;
2179 if (qy + py < -28) py = -28 - qy;
2180 if (qx + px > X) px = X - qx;
2181 if (qy + py > Y) py = Y - qy;
2183 qx = (s->mb_x << 6);
2184 qy = (s->mb_y << 6);
2185 X = (s->mb_width << 6) - 4;
2186 Y = (s->mb_height << 6) - 4;
2187 if (qx + px < -60) px = -60 - qx;
2188 if (qy + py < -60) py = -60 - qy;
2189 if (qx + px > X) px = X - qx;
2190 if (qy + py > Y) py = Y - qy;
2193 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2194 if (0 && !s->first_slice_line && s->mb_x) {
2195 if (is_intra[xy - wrap])
2196 sum = FFABS(px) + FFABS(py);
2198 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2200 if (get_bits1(&s->gb)) {
2208 if (is_intra[xy - 2])
2209 sum = FFABS(px) + FFABS(py);
2211 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2213 if (get_bits1(&s->gb)) {
2223 /* store MV using signed modulus of MV range defined in 4.11 */
2224 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2225 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2227 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2228 C = s->current_picture.motion_val[1][xy - 2];
2229 A = s->current_picture.motion_val[1][xy - wrap * 2];
2230 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2231 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2235 if (!s->first_slice_line) { // predictor A is not out of bounds
2236 if (s->mb_width == 1) {
2240 px = mid_pred(A[0], B[0], C[0]);
2241 py = mid_pred(A[1], B[1], C[1]);
2243 } else if (s->mb_x) { // predictor C is not out of bounds
2249 /* Pullback MV as specified in 8.3.5.3.4 */
2252 if (v->profile < PROFILE_ADVANCED) {
2253 qx = (s->mb_x << 5);
2254 qy = (s->mb_y << 5);
2255 X = (s->mb_width << 5) - 4;
2256 Y = (s->mb_height << 5) - 4;
2257 if (qx + px < -28) px = -28 - qx;
2258 if (qy + py < -28) py = -28 - qy;
2259 if (qx + px > X) px = X - qx;
2260 if (qy + py > Y) py = Y - qy;
2262 qx = (s->mb_x << 6);
2263 qy = (s->mb_y << 6);
2264 X = (s->mb_width << 6) - 4;
2265 Y = (s->mb_height << 6) - 4;
2266 if (qx + px < -60) px = -60 - qx;
2267 if (qy + py < -60) py = -60 - qy;
2268 if (qx + px > X) px = X - qx;
2269 if (qy + py > Y) py = Y - qy;
2272 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2273 if (0 && !s->first_slice_line && s->mb_x) {
2274 if (is_intra[xy - wrap])
2275 sum = FFABS(px) + FFABS(py);
2277 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2279 if (get_bits1(&s->gb)) {
2287 if (is_intra[xy - 2])
2288 sum = FFABS(px) + FFABS(py);
2290 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2292 if (get_bits1(&s->gb)) {
2302 /* store MV using signed modulus of MV range defined in 4.11 */
2304 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2305 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2307 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2308 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2309 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2310 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2313 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2315 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2316 MpegEncContext *s = &v->s;
2317 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2319 if (v->bmvtype == BMV_TYPE_DIRECT) {
2320 int total_opp, k, f;
2321 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2322 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2323 v->bfraction, 0, s->quarter_sample);
2324 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2325 v->bfraction, 0, s->quarter_sample);
2326 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2327 v->bfraction, 1, s->quarter_sample);
2328 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2329 v->bfraction, 1, s->quarter_sample);
2331 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2332 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2333 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2334 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2335 f = (total_opp > 2) ? 1 : 0;
2337 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2338 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2341 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2342 for (k = 0; k < 4; k++) {
2343 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2344 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2345 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2346 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2347 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2348 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2352 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2353 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);
2354 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);
2357 if (dir) { // backward
2358 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);
2359 if (n == 3 || mv1) {
2360 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2363 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);
2364 if (n == 3 || mv1) {
2365 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2370 /** Get predicted DC value for I-frames only
2371 * prediction dir: left=0, top=1
2372 * @param s MpegEncContext
2373 * @param overlap flag indicating that overlap filtering is used
2374 * @param pq integer part of picture quantizer
2375 * @param[in] n block index in the current MB
2376 * @param dc_val_ptr Pointer to DC predictor
2377 * @param dir_ptr Prediction direction for use in AC prediction
2379 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2380 int16_t **dc_val_ptr, int *dir_ptr)
2382 int a, b, c, wrap, pred, scale;
2384 static const uint16_t dcpred[32] = {
2385 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2386 114, 102, 93, 85, 79, 73, 68, 64,
2387 60, 57, 54, 51, 49, 47, 45, 43,
2388 41, 39, 38, 37, 35, 34, 33
2391 /* find prediction - wmv3_dc_scale always used here in fact */
2392 if (n < 4) scale = s->y_dc_scale;
2393 else scale = s->c_dc_scale;
2395 wrap = s->block_wrap[n];
2396 dc_val = s->dc_val[0] + s->block_index[n];
2402 b = dc_val[ - 1 - wrap];
2403 a = dc_val[ - wrap];
2405 if (pq < 9 || !overlap) {
2406 /* Set outer values */
2407 if (s->first_slice_line && (n != 2 && n != 3))
2408 b = a = dcpred[scale];
2409 if (s->mb_x == 0 && (n != 1 && n != 3))
2410 b = c = dcpred[scale];
2412 /* Set outer values */
2413 if (s->first_slice_line && (n != 2 && n != 3))
2415 if (s->mb_x == 0 && (n != 1 && n != 3))
2419 if (abs(a - b) <= abs(b - c)) {
2421 *dir_ptr = 1; // left
2424 *dir_ptr = 0; // top
2427 /* update predictor */
2428 *dc_val_ptr = &dc_val[0];
2433 /** Get predicted DC value
2434 * prediction dir: left=0, top=1
2435 * @param s MpegEncContext
2436 * @param overlap flag indicating that overlap filtering is used
2437 * @param pq integer part of picture quantizer
2438 * @param[in] n block index in the current MB
2439 * @param a_avail flag indicating top block availability
2440 * @param c_avail flag indicating left block availability
2441 * @param dc_val_ptr Pointer to DC predictor
2442 * @param dir_ptr Prediction direction for use in AC prediction
2444 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2445 int a_avail, int c_avail,
2446 int16_t **dc_val_ptr, int *dir_ptr)
2448 int a, b, c, wrap, pred;
2450 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2454 wrap = s->block_wrap[n];
2455 dc_val = s->dc_val[0] + s->block_index[n];
2461 b = dc_val[ - 1 - wrap];
2462 a = dc_val[ - wrap];
2463 /* scale predictors if needed */
2464 q1 = s->current_picture.qscale_table[mb_pos];
2465 dqscale_index = s->y_dc_scale_table[q1] - 1;
2466 if (dqscale_index < 0)
2468 if (c_avail && (n != 1 && n != 3)) {
2469 q2 = s->current_picture.qscale_table[mb_pos - 1];
2471 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2473 if (a_avail && (n != 2 && n != 3)) {
2474 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2476 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2478 if (a_avail && c_avail && (n != 3)) {
2483 off -= s->mb_stride;
2484 q2 = s->current_picture.qscale_table[off];
2486 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2489 if (a_avail && c_avail) {
2490 if (abs(a - b) <= abs(b - c)) {
2492 *dir_ptr = 1; // left
2495 *dir_ptr = 0; // top
2497 } else if (a_avail) {
2499 *dir_ptr = 0; // top
2500 } else if (c_avail) {
2502 *dir_ptr = 1; // left
2505 *dir_ptr = 1; // left
2508 /* update predictor */
2509 *dc_val_ptr = &dc_val[0];
2513 /** @} */ // Block group
2516 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2517 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2521 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2522 uint8_t **coded_block_ptr)
2524 int xy, wrap, pred, a, b, c;
2526 xy = s->block_index[n];
2527 wrap = s->b8_stride;
2532 a = s->coded_block[xy - 1 ];
2533 b = s->coded_block[xy - 1 - wrap];
2534 c = s->coded_block[xy - wrap];
2543 *coded_block_ptr = &s->coded_block[xy];
2549 * Decode one AC coefficient
2550 * @param v The VC1 context
2551 * @param last Last coefficient
2552 * @param skip How much zero coefficients to skip
2553 * @param value Decoded AC coefficient value
2554 * @param codingset set of VLC to decode data
2557 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2558 int *value, int codingset)
2560 GetBitContext *gb = &v->s.gb;
2561 int index, escape, run = 0, level = 0, lst = 0;
2563 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2564 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2565 run = vc1_index_decode_table[codingset][index][0];
2566 level = vc1_index_decode_table[codingset][index][1];
2567 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2571 escape = decode210(gb);
2573 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2574 run = vc1_index_decode_table[codingset][index][0];
2575 level = vc1_index_decode_table[codingset][index][1];
2576 lst = index >= vc1_last_decode_table[codingset];
2579 level += vc1_last_delta_level_table[codingset][run];
2581 level += vc1_delta_level_table[codingset][run];
2584 run += vc1_last_delta_run_table[codingset][level] + 1;
2586 run += vc1_delta_run_table[codingset][level] + 1;
2592 lst = get_bits1(gb);
2593 if (v->s.esc3_level_length == 0) {
2594 if (v->pq < 8 || v->dquantfrm) { // table 59
2595 v->s.esc3_level_length = get_bits(gb, 3);
2596 if (!v->s.esc3_level_length)
2597 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2598 } else { // table 60
2599 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2601 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2603 run = get_bits(gb, v->s.esc3_run_length);
2604 sign = get_bits1(gb);
2605 level = get_bits(gb, v->s.esc3_level_length);
2616 /** Decode intra block in intra frames - should be faster than decode_intra_block
2617 * @param v VC1Context
2618 * @param block block to decode
2619 * @param[in] n subblock index
2620 * @param coded are AC coeffs present or not
2621 * @param codingset set of VLC to decode data
2623 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2624 int coded, int codingset)
2626 GetBitContext *gb = &v->s.gb;
2627 MpegEncContext *s = &v->s;
2628 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2631 int16_t *ac_val, *ac_val2;
2634 /* Get DC differential */
2636 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2638 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2641 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2645 if (dcdiff == 119 /* ESC index value */) {
2646 /* TODO: Optimize */
2647 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2648 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2649 else dcdiff = get_bits(gb, 8);
2652 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2653 else if (v->pq == 2)
2654 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2661 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2664 /* Store the quantized DC coeff, used for prediction */
2666 block[0] = dcdiff * s->y_dc_scale;
2668 block[0] = dcdiff * s->c_dc_scale;
2679 int last = 0, skip, value;
2680 const uint8_t *zz_table;
2684 scale = v->pq * 2 + v->halfpq;
2688 zz_table = v->zz_8x8[2];
2690 zz_table = v->zz_8x8[3];
2692 zz_table = v->zz_8x8[1];
2694 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2696 if (dc_pred_dir) // left
2699 ac_val -= 16 * s->block_wrap[n];
2702 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2706 block[zz_table[i++]] = value;
2709 /* apply AC prediction if needed */
2711 if (dc_pred_dir) { // left
2712 for (k = 1; k < 8; k++)
2713 block[k << v->left_blk_sh] += ac_val[k];
2715 for (k = 1; k < 8; k++)
2716 block[k << v->top_blk_sh] += ac_val[k + 8];
2719 /* save AC coeffs for further prediction */
2720 for (k = 1; k < 8; k++) {
2721 ac_val2[k] = block[k << v->left_blk_sh];
2722 ac_val2[k + 8] = block[k << v->top_blk_sh];
2725 /* scale AC coeffs */
2726 for (k = 1; k < 64; k++)
2730 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2733 if (s->ac_pred) i = 63;
2739 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2743 scale = v->pq * 2 + v->halfpq;
2744 memset(ac_val2, 0, 16 * 2);
2745 if (dc_pred_dir) { // left
2748 memcpy(ac_val2, ac_val, 8 * 2);
2750 ac_val -= 16 * s->block_wrap[n];
2752 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2755 /* apply AC prediction if needed */
2757 if (dc_pred_dir) { //left
2758 for (k = 1; k < 8; k++) {
2759 block[k << v->left_blk_sh] = ac_val[k] * scale;
2760 if (!v->pquantizer && block[k << v->left_blk_sh])
2761 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2764 for (k = 1; k < 8; k++) {
2765 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2766 if (!v->pquantizer && block[k << v->top_blk_sh])
2767 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2773 s->block_last_index[n] = i;
2778 /** Decode intra block in intra frames - should be faster than decode_intra_block
2779 * @param v VC1Context
2780 * @param block block to decode
2781 * @param[in] n subblock number
2782 * @param coded are AC coeffs present or not
2783 * @param codingset set of VLC to decode data
2784 * @param mquant quantizer value for this macroblock
2786 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2787 int coded, int codingset, int mquant)
2789 GetBitContext *gb = &v->s.gb;
2790 MpegEncContext *s = &v->s;
2791 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2793 int16_t *dc_val = NULL;
2794 int16_t *ac_val, *ac_val2;
2796 int a_avail = v->a_avail, c_avail = v->c_avail;
2797 int use_pred = s->ac_pred;
2800 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2802 /* Get DC differential */
2804 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2806 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2809 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2813 if (dcdiff == 119 /* ESC index value */) {
2814 /* TODO: Optimize */
2815 if (mquant == 1) dcdiff = get_bits(gb, 10);
2816 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2817 else dcdiff = get_bits(gb, 8);
2820 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2821 else if (mquant == 2)
2822 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2829 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2832 /* Store the quantized DC coeff, used for prediction */
2834 block[0] = dcdiff * s->y_dc_scale;
2836 block[0] = dcdiff * s->c_dc_scale;
2842 /* check if AC is needed at all */
2843 if (!a_avail && !c_avail)
2845 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2848 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2850 if (dc_pred_dir) // left
2853 ac_val -= 16 * s->block_wrap[n];
2855 q1 = s->current_picture.qscale_table[mb_pos];
2856 if ( dc_pred_dir && c_avail && mb_pos)
2857 q2 = s->current_picture.qscale_table[mb_pos - 1];
2858 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2859 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2860 if ( dc_pred_dir && n == 1)
2862 if (!dc_pred_dir && n == 2)
2868 int last = 0, skip, value;
2869 const uint8_t *zz_table;
2873 if (!use_pred && v->fcm == ILACE_FRAME) {
2874 zz_table = v->zzi_8x8;
2876 if (!dc_pred_dir) // top
2877 zz_table = v->zz_8x8[2];
2879 zz_table = v->zz_8x8[3];
2882 if (v->fcm != ILACE_FRAME)
2883 zz_table = v->zz_8x8[1];
2885 zz_table = v->zzi_8x8;
2889 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2893 block[zz_table[i++]] = value;
2896 /* apply AC prediction if needed */
2898 /* scale predictors if needed*/
2899 if (q2 && q1 != q2) {
2900 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2901 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2904 return AVERROR_INVALIDDATA;
2905 if (dc_pred_dir) { // left
2906 for (k = 1; k < 8; k++)
2907 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2909 for (k = 1; k < 8; k++)
2910 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2913 if (dc_pred_dir) { //left
2914 for (k = 1; k < 8; k++)
2915 block[k << v->left_blk_sh] += ac_val[k];
2917 for (k = 1; k < 8; k++)
2918 block[k << v->top_blk_sh] += ac_val[k + 8];
2922 /* save AC coeffs for further prediction */
2923 for (k = 1; k < 8; k++) {
2924 ac_val2[k ] = block[k << v->left_blk_sh];
2925 ac_val2[k + 8] = block[k << v->top_blk_sh];
2928 /* scale AC coeffs */
2929 for (k = 1; k < 64; k++)
2933 block[k] += (block[k] < 0) ? -mquant : mquant;
2936 if (use_pred) i = 63;
2937 } else { // no AC coeffs
2940 memset(ac_val2, 0, 16 * 2);
2941 if (dc_pred_dir) { // left
2943 memcpy(ac_val2, ac_val, 8 * 2);
2944 if (q2 && q1 != q2) {
2945 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2946 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2948 return AVERROR_INVALIDDATA;
2949 for (k = 1; k < 8; k++)
2950 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2955 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2956 if (q2 && q1 != q2) {
2957 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2958 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2960 return AVERROR_INVALIDDATA;
2961 for (k = 1; k < 8; k++)
2962 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2967 /* apply AC prediction if needed */
2969 if (dc_pred_dir) { // left
2970 for (k = 1; k < 8; k++) {
2971 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2972 if (!v->pquantizer && block[k << v->left_blk_sh])
2973 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2976 for (k = 1; k < 8; k++) {
2977 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2978 if (!v->pquantizer && block[k << v->top_blk_sh])
2979 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2985 s->block_last_index[n] = i;
2990 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2991 * @param v VC1Context
2992 * @param block block to decode
2993 * @param[in] n subblock index
2994 * @param coded are AC coeffs present or not
2995 * @param mquant block quantizer
2996 * @param codingset set of VLC to decode data
2998 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2999 int coded, int mquant, int codingset)
3001 GetBitContext *gb = &v->s.gb;
3002 MpegEncContext *s = &v->s;
3003 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3005 int16_t *dc_val = NULL;
3006 int16_t *ac_val, *ac_val2;
3008 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3009 int a_avail = v->a_avail, c_avail = v->c_avail;
3010 int use_pred = s->ac_pred;
3014 s->dsp.clear_block(block);
3016 /* XXX: Guard against dumb values of mquant */
3017 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3019 /* Set DC scale - y and c use the same */
3020 s->y_dc_scale = s->y_dc_scale_table[mquant];
3021 s->c_dc_scale = s->c_dc_scale_table[mquant];
3023 /* Get DC differential */
3025 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3027 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3030 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3034 if (dcdiff == 119 /* ESC index value */) {
3035 /* TODO: Optimize */
3036 if (mquant == 1) dcdiff = get_bits(gb, 10);
3037 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3038 else dcdiff = get_bits(gb, 8);
3041 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3042 else if (mquant == 2)
3043 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3050 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3053 /* Store the quantized DC coeff, used for prediction */
3056 block[0] = dcdiff * s->y_dc_scale;
3058 block[0] = dcdiff * s->c_dc_scale;
3064 /* check if AC is needed at all and adjust direction if needed */
3065 if (!a_avail) dc_pred_dir = 1;
3066 if (!c_avail) dc_pred_dir = 0;
3067 if (!a_avail && !c_avail) use_pred = 0;
3068 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3071 scale = mquant * 2 + v->halfpq;
3073 if (dc_pred_dir) //left
3076 ac_val -= 16 * s->block_wrap[n];
3078 q1 = s->current_picture.qscale_table[mb_pos];
3079 if (dc_pred_dir && c_avail && mb_pos)
3080 q2 = s->current_picture.qscale_table[mb_pos - 1];
3081 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3082 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3083 if ( dc_pred_dir && n == 1)
3085 if (!dc_pred_dir && n == 2)
3087 if (n == 3) q2 = q1;
3090 int last = 0, skip, value;
3094 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3098 if (v->fcm == PROGRESSIVE)
3099 block[v->zz_8x8[0][i++]] = value;
3101 if (use_pred && (v->fcm == ILACE_FRAME)) {
3102 if (!dc_pred_dir) // top
3103 block[v->zz_8x8[2][i++]] = value;
3105 block[v->zz_8x8[3][i++]] = value;
3107 block[v->zzi_8x8[i++]] = value;
3112 /* apply AC prediction if needed */
3114 /* scale predictors if needed*/
3115 if (q2 && q1 != q2) {
3116 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3117 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3120 return AVERROR_INVALIDDATA;
3121 if (dc_pred_dir) { // left
3122 for (k = 1; k < 8; k++)
3123 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3125 for (k = 1; k < 8; k++)
3126 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3129 if (dc_pred_dir) { // left
3130 for (k = 1; k < 8; k++)
3131 block[k << v->left_blk_sh] += ac_val[k];
3133 for (k = 1; k < 8; k++)
3134 block[k << v->top_blk_sh] += ac_val[k + 8];
3138 /* save AC coeffs for further prediction */
3139 for (k = 1; k < 8; k++) {
3140 ac_val2[k ] = block[k << v->left_blk_sh];
3141 ac_val2[k + 8] = block[k << v->top_blk_sh];
3144 /* scale AC coeffs */
3145 for (k = 1; k < 64; k++)
3149 block[k] += (block[k] < 0) ? -mquant : mquant;
3152 if (use_pred) i = 63;
3153 } else { // no AC coeffs
3156 memset(ac_val2, 0, 16 * 2);
3157 if (dc_pred_dir) { // left
3159 memcpy(ac_val2, ac_val, 8 * 2);
3160 if (q2 && q1 != q2) {
3161 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3162 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3164 return AVERROR_INVALIDDATA;
3165 for (k = 1; k < 8; k++)
3166 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3171 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3172 if (q2 && q1 != q2) {
3173 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3174 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3176 return AVERROR_INVALIDDATA;
3177 for (k = 1; k < 8; k++)
3178 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3183 /* apply AC prediction if needed */
3185 if (dc_pred_dir) { // left
3186 for (k = 1; k < 8; k++) {
3187 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3188 if (!v->pquantizer && block[k << v->left_blk_sh])
3189 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3192 for (k = 1; k < 8; k++) {
3193 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3194 if (!v->pquantizer && block[k << v->top_blk_sh])
3195 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3201 s->block_last_index[n] = i;
3208 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3209 int mquant, int ttmb, int first_block,
3210 uint8_t *dst, int linesize, int skip_block,
3213 MpegEncContext *s = &v->s;
3214 GetBitContext *gb = &s->gb;
3217 int scale, off, idx, last, skip, value;
3218 int ttblk = ttmb & 7;
3221 s->dsp.clear_block(block);
3224 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)];
3226 if (ttblk == TT_4X4) {
3227 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3229 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3230 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3231 || (!v->res_rtm_flag && !first_block))) {
3232 subblkpat = decode012(gb);
3234 subblkpat ^= 3; // swap decoded pattern bits
3235 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3237 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3240 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3242 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3243 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3244 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3247 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3248 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3257 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3262 idx = v->zz_8x8[0][i++];
3264 idx = v->zzi_8x8[i++];
3265 block[idx] = value * scale;
3267 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3271 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3273 v->vc1dsp.vc1_inv_trans_8x8(block);
3274 s->dsp.add_pixels_clamped(block, dst, linesize);
3279 pat = ~subblkpat & 0xF;
3280 for (j = 0; j < 4; j++) {
3281 last = subblkpat & (1 << (3 - j));
3283 off = (j & 1) * 4 + (j & 2) * 16;
3285 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3290 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3292 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3293 block[idx + off] = value * scale;
3295 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3297 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3299 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3301 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3306 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3307 for (j = 0; j < 2; j++) {
3308 last = subblkpat & (1 << (1 - j));
3312 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3317 idx = v->zz_8x4[i++] + off;
3319 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3320 block[idx] = value * scale;
3322 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3324 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3326 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3328 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3333 pat = ~(subblkpat * 5) & 0xF;
3334 for (j = 0; j < 2; j++) {
3335 last = subblkpat & (1 << (1 - j));
3339 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3344 idx = v->zz_4x8[i++] + off;
3346 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3347 block[idx] = value * scale;
3349 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3351 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3353 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3355 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3361 *ttmb_out |= ttblk << (n * 4);
3365 /** @} */ // Macroblock group
3367 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3368 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3370 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3372 MpegEncContext *s = &v->s;
3373 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3374 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3375 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3376 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3377 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3380 if (block_num > 3) {
3381 dst = s->dest[block_num - 3];
3383 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3385 if (s->mb_y != s->end_mb_y || block_num < 2) {
3389 if (block_num > 3) {
3390 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3391 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3392 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3393 mv_stride = s->mb_stride;
3395 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3396 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3397 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3398 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3399 mv_stride = s->b8_stride;
3400 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3403 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3404 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3405 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3407 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3409 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3412 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3414 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3419 dst -= 4 * linesize;
3420 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3421 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3422 idx = (block_cbp | (block_cbp >> 2)) & 3;
3424 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3427 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3429 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3434 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3436 MpegEncContext *s = &v->s;
3437 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3438 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3439 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3440 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3441 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3444 if (block_num > 3) {
3445 dst = s->dest[block_num - 3] - 8 * linesize;
3447 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3450 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3453 if (block_num > 3) {
3454 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3455 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3456 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3458 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3459 : (mb_cbp >> ((block_num + 1) * 4));
3460 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3461 : (mb_is_intra >> ((block_num + 1) * 4));
3462 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3464 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3465 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3467 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3469 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3472 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3474 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3480 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3481 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3482 idx = (block_cbp | (block_cbp >> 1)) & 5;
3484 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3487 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3489 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3494 static void vc1_apply_p_loop_filter(VC1Context *v)
3496 MpegEncContext *s = &v->s;
3499 for (i = 0; i < 6; i++) {
3500 vc1_apply_p_v_loop_filter(v, i);
3503 /* V always precedes H, therefore we run H one MB before V;
3504 * at the end of a row, we catch up to complete the row */
3506 for (i = 0; i < 6; i++) {
3507 vc1_apply_p_h_loop_filter(v, i);
3509 if (s->mb_x == s->mb_width - 1) {
3511 ff_update_block_index(s);
3512 for (i = 0; i < 6; i++) {
3513 vc1_apply_p_h_loop_filter(v, i);
3519 /** Decode one P-frame MB
3521 static int vc1_decode_p_mb(VC1Context *v)
3523 MpegEncContext *s = &v->s;
3524 GetBitContext *gb = &s->gb;
3526 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3527 int cbp; /* cbp decoding stuff */
3528 int mqdiff, mquant; /* MB quantization */
3529 int ttmb = v->ttfrm; /* MB Transform type */
3531 int mb_has_coeffs = 1; /* last_flag */
3532 int dmv_x, dmv_y; /* Differential MV components */
3533 int index, index1; /* LUT indexes */
3534 int val, sign; /* temp values */
3535 int first_block = 1;
3537 int skipped, fourmv;
3538 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3540 mquant = v->pq; /* lossy initialization */
3542 if (v->mv_type_is_raw)
3543 fourmv = get_bits1(gb);
3545 fourmv = v->mv_type_mb_plane[mb_pos];
3547 skipped = get_bits1(gb);
3549 skipped = v->s.mbskip_table[mb_pos];
3551 if (!fourmv) { /* 1MV mode */
3553 GET_MVDATA(dmv_x, dmv_y);
3556 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3557 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3559 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3560 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3562 /* FIXME Set DC val for inter block ? */
3563 if (s->mb_intra && !mb_has_coeffs) {
3565 s->ac_pred = get_bits1(gb);
3567 } else if (mb_has_coeffs) {
3569 s->ac_pred = get_bits1(gb);
3570 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3576 s->current_picture.qscale_table[mb_pos] = mquant;
3578 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3579 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3580 VC1_TTMB_VLC_BITS, 2);
3581 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3583 for (i = 0; i < 6; i++) {
3584 s->dc_val[0][s->block_index[i]] = 0;
3586 val = ((cbp >> (5 - i)) & 1);
3587 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3588 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3590 /* check if prediction blocks A and C are available */
3591 v->a_avail = v->c_avail = 0;
3592 if (i == 2 || i == 3 || !s->first_slice_line)
3593 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3594 if (i == 1 || i == 3 || s->mb_x)
3595 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3597 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3598 (i & 4) ? v->codingset2 : v->codingset);
3599 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3601 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3603 for (j = 0; j < 64; j++)
3604 s->block[i][j] <<= 1;
3605 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3606 if (v->pq >= 9 && v->overlap) {
3608 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3610 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3612 block_cbp |= 0xF << (i << 2);
3613 block_intra |= 1 << i;
3615 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3616 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3617 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3618 block_cbp |= pat << (i << 2);
3619 if (!v->ttmbf && ttmb < 8)
3626 for (i = 0; i < 6; i++) {
3627 v->mb_type[0][s->block_index[i]] = 0;
3628 s->dc_val[0][s->block_index[i]] = 0;
3630 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3631 s->current_picture.qscale_table[mb_pos] = 0;
3632 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3635 } else { // 4MV mode
3636 if (!skipped /* unskipped MB */) {
3637 int intra_count = 0, coded_inter = 0;
3638 int is_intra[6], is_coded[6];
3640 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3641 for (i = 0; i < 6; i++) {
3642 val = ((cbp >> (5 - i)) & 1);
3643 s->dc_val[0][s->block_index[i]] = 0;
3650 GET_MVDATA(dmv_x, dmv_y);
3652 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3654 vc1_mc_4mv_luma(v, i, 0, 0);
3655 intra_count += s->mb_intra;
3656 is_intra[i] = s->mb_intra;
3657 is_coded[i] = mb_has_coeffs;
3660 is_intra[i] = (intra_count >= 3);
3664 vc1_mc_4mv_chroma(v, 0);
3665 v->mb_type[0][s->block_index[i]] = is_intra[i];
3667 coded_inter = !is_intra[i] & is_coded[i];
3669 // if there are no coded blocks then don't do anything more
3671 if (!intra_count && !coded_inter)
3674 s->current_picture.qscale_table[mb_pos] = mquant;
3675 /* test if block is intra and has pred */
3678 for (i = 0; i < 6; i++)
3680 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3681 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3687 s->ac_pred = get_bits1(gb);
3691 if (!v->ttmbf && coded_inter)
3692 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3693 for (i = 0; i < 6; i++) {
3695 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3696 s->mb_intra = is_intra[i];
3698 /* check if prediction blocks A and C are available */
3699 v->a_avail = v->c_avail = 0;
3700 if (i == 2 || i == 3 || !s->first_slice_line)
3701 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3702 if (i == 1 || i == 3 || s->mb_x)
3703 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3705 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3706 (i & 4) ? v->codingset2 : v->codingset);
3707 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3709 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3711 for (j = 0; j < 64; j++)
3712 s->block[i][j] <<= 1;
3713 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3714 (i & 4) ? s->uvlinesize : s->linesize);
3715 if (v->pq >= 9 && v->overlap) {
3717 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3719 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3721 block_cbp |= 0xF << (i << 2);
3722 block_intra |= 1 << i;
3723 } else if (is_coded[i]) {
3724 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3725 first_block, s->dest[dst_idx] + off,
3726 (i & 4) ? s->uvlinesize : s->linesize,
3727 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3729 block_cbp |= pat << (i << 2);
3730 if (!v->ttmbf && ttmb < 8)
3735 } else { // skipped MB
3737 s->current_picture.qscale_table[mb_pos] = 0;
3738 for (i = 0; i < 6; i++) {
3739 v->mb_type[0][s->block_index[i]] = 0;
3740 s->dc_val[0][s->block_index[i]] = 0;
3742 for (i = 0; i < 4; i++) {
3743 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3744 vc1_mc_4mv_luma(v, i, 0, 0);
3746 vc1_mc_4mv_chroma(v, 0);
3747 s->current_picture.qscale_table[mb_pos] = 0;
3751 v->cbp[s->mb_x] = block_cbp;
3752 v->ttblk[s->mb_x] = block_tt;
3753 v->is_intra[s->mb_x] = block_intra;
3758 /* Decode one macroblock in an interlaced frame p picture */
3760 static int vc1_decode_p_mb_intfr(VC1Context *v)
3762 MpegEncContext *s = &v->s;
3763 GetBitContext *gb = &s->gb;
3765 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3766 int cbp = 0; /* cbp decoding stuff */
3767 int mqdiff, mquant; /* MB quantization */
3768 int ttmb = v->ttfrm; /* MB Transform type */
3770 int mb_has_coeffs = 1; /* last_flag */
3771 int dmv_x, dmv_y; /* Differential MV components */
3772 int val; /* temp value */
3773 int first_block = 1;
3775 int skipped, fourmv = 0, twomv = 0;
3776 int block_cbp = 0, pat, block_tt = 0;
3777 int idx_mbmode = 0, mvbp;
3778 int stride_y, fieldtx;
3780 mquant = v->pq; /* Lossy initialization */
3783 skipped = get_bits1(gb);
3785 skipped = v->s.mbskip_table[mb_pos];
3787 if (v->fourmvswitch)
3788 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3790 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3791 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3792 /* store the motion vector type in a flag (useful later) */
3793 case MV_PMODE_INTFR_4MV:
3795 v->blk_mv_type[s->block_index[0]] = 0;
3796 v->blk_mv_type[s->block_index[1]] = 0;
3797 v->blk_mv_type[s->block_index[2]] = 0;
3798 v->blk_mv_type[s->block_index[3]] = 0;
3800 case MV_PMODE_INTFR_4MV_FIELD:
3802 v->blk_mv_type[s->block_index[0]] = 1;
3803 v->blk_mv_type[s->block_index[1]] = 1;
3804 v->blk_mv_type[s->block_index[2]] = 1;
3805 v->blk_mv_type[s->block_index[3]] = 1;
3807 case MV_PMODE_INTFR_2MV_FIELD:
3809 v->blk_mv_type[s->block_index[0]] = 1;
3810 v->blk_mv_type[s->block_index[1]] = 1;
3811 v->blk_mv_type[s->block_index[2]] = 1;
3812 v->blk_mv_type[s->block_index[3]] = 1;
3814 case MV_PMODE_INTFR_1MV:
3815 v->blk_mv_type[s->block_index[0]] = 0;
3816 v->blk_mv_type[s->block_index[1]] = 0;
3817 v->blk_mv_type[s->block_index[2]] = 0;
3818 v->blk_mv_type[s->block_index[3]] = 0;
3821 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3822 for (i = 0; i < 4; i++) {
3823 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3824 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3826 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3827 s->mb_intra = v->is_intra[s->mb_x] = 1;
3828 for (i = 0; i < 6; i++)
3829 v->mb_type[0][s->block_index[i]] = 1;
3830 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3831 mb_has_coeffs = get_bits1(gb);
3833 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3834 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3836 s->current_picture.qscale_table[mb_pos] = mquant;
3837 /* Set DC scale - y and c use the same (not sure if necessary here) */
3838 s->y_dc_scale = s->y_dc_scale_table[mquant];
3839 s->c_dc_scale = s->c_dc_scale_table[mquant];
3841 for (i = 0; i < 6; i++) {
3842 s->dc_val[0][s->block_index[i]] = 0;
3844 val = ((cbp >> (5 - i)) & 1);
3845 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3846 v->a_avail = v->c_avail = 0;
3847 if (i == 2 || i == 3 || !s->first_slice_line)
3848 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3849 if (i == 1 || i == 3 || s->mb_x)
3850 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3852 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3853 (i & 4) ? v->codingset2 : v->codingset);
3854 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3855 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3857 stride_y = s->linesize << fieldtx;
3858 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3860 stride_y = s->uvlinesize;
3863 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3867 } else { // inter MB
3868 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3870 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3871 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3872 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3874 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3875 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3876 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3879 s->mb_intra = v->is_intra[s->mb_x] = 0;
3880 for (i = 0; i < 6; i++)
3881 v->mb_type[0][s->block_index[i]] = 0;
3882 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3883 /* for all motion vector read MVDATA and motion compensate each block */
3887 for (i = 0; i < 6; i++) {
3890 val = ((mvbp >> (3 - i)) & 1);
3892 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3894 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3895 vc1_mc_4mv_luma(v, i, 0, 0);
3896 } else if (i == 4) {
3897 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3904 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3906 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3907 vc1_mc_4mv_luma(v, 0, 0, 0);
3908 vc1_mc_4mv_luma(v, 1, 0, 0);
3911 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3913 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3914 vc1_mc_4mv_luma(v, 2, 0, 0);
3915 vc1_mc_4mv_luma(v, 3, 0, 0);
3916 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3918 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3921 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3923 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3927 GET_MQUANT(); // p. 227
3928 s->current_picture.qscale_table[mb_pos] = mquant;
3929 if (!v->ttmbf && cbp)
3930 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3931 for (i = 0; i < 6; i++) {
3932 s->dc_val[0][s->block_index[i]] = 0;
3934 val = ((cbp >> (5 - i)) & 1);
3936 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3938 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3940 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3941 first_block, s->dest[dst_idx] + off,
3942 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3943 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3944 block_cbp |= pat << (i << 2);
3945 if (!v->ttmbf && ttmb < 8)
3952 s->mb_intra = v->is_intra[s->mb_x] = 0;
3953 for (i = 0; i < 6; i++) {
3954 v->mb_type[0][s->block_index[i]] = 0;
3955 s->dc_val[0][s->block_index[i]] = 0;
3957 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3958 s->current_picture.qscale_table[mb_pos] = 0;
3959 v->blk_mv_type[s->block_index[0]] = 0;
3960 v->blk_mv_type[s->block_index[1]] = 0;
3961 v->blk_mv_type[s->block_index[2]] = 0;
3962 v->blk_mv_type[s->block_index[3]] = 0;
3963 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3966 if (s->mb_x == s->mb_width - 1)
3967 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3971 static int vc1_decode_p_mb_intfi(VC1Context *v)
3973 MpegEncContext *s = &v->s;
3974 GetBitContext *gb = &s->gb;
3976 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3977 int cbp = 0; /* cbp decoding stuff */
3978 int mqdiff, mquant; /* MB quantization */
3979 int ttmb = v->ttfrm; /* MB Transform type */
3981 int mb_has_coeffs = 1; /* last_flag */
3982 int dmv_x, dmv_y; /* Differential MV components */
3983 int val; /* temp values */
3984 int first_block = 1;
3987 int block_cbp = 0, pat, block_tt = 0;
3990 mquant = v->pq; /* Lossy initialization */
3992 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3993 if (idx_mbmode <= 1) { // intra MB
3994 s->mb_intra = v->is_intra[s->mb_x] = 1;
3995 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3996 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3997 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3999 s->current_picture.qscale_table[mb_pos] = mquant;
4000 /* Set DC scale - y and c use the same (not sure if necessary here) */
4001 s->y_dc_scale = s->y_dc_scale_table[mquant];
4002 s->c_dc_scale = s->c_dc_scale_table[mquant];
4003 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4004 mb_has_coeffs = idx_mbmode & 1;
4006 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4008 for (i = 0; i < 6; i++) {
4009 s->dc_val[0][s->block_index[i]] = 0;
4010 v->mb_type[0][s->block_index[i]] = 1;
4012 val = ((cbp >> (5 - i)) & 1);
4013 v->a_avail = v->c_avail = 0;
4014 if (i == 2 || i == 3 || !s->first_slice_line)
4015 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4016 if (i == 1 || i == 3 || s->mb_x)
4017 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4019 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4020 (i & 4) ? v->codingset2 : v->codingset);
4021 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4023 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4024 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4025 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4026 // TODO: loop filter
4029 s->mb_intra = v->is_intra[s->mb_x] = 0;
4030 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4031 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4032 if (idx_mbmode <= 5) { // 1-MV
4033 dmv_x = dmv_y = pred_flag = 0;
4034 if (idx_mbmode & 1) {
4035 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4037 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4039 mb_has_coeffs = !(idx_mbmode & 2);
4041 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4042 for (i = 0; i < 6; i++) {
4044 dmv_x = dmv_y = pred_flag = 0;
4045 val = ((v->fourmvbp >> (3 - i)) & 1);
4047 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4049 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4050 vc1_mc_4mv_luma(v, i, 0, 0);
4052 vc1_mc_4mv_chroma(v, 0);
4054 mb_has_coeffs = idx_mbmode & 1;
4057 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4061 s->current_picture.qscale_table[mb_pos] = mquant;
4062 if (!v->ttmbf && cbp) {
4063 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4066 for (i = 0; i < 6; i++) {
4067 s->dc_val[0][s->block_index[i]] = 0;
4069 val = ((cbp >> (5 - i)) & 1);
4070 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4072 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4073 first_block, s->dest[dst_idx] + off,
4074 (i & 4) ? s->uvlinesize : s->linesize,
4075 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4077 block_cbp |= pat << (i << 2);
4078 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4083 if (s->mb_x == s->mb_width - 1)
4084 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4088 /** Decode one B-frame MB (in Main profile)
4090 static void vc1_decode_b_mb(VC1Context *v)
4092 MpegEncContext *s = &v->s;
4093 GetBitContext *gb = &s->gb;
4095 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4096 int cbp = 0; /* cbp decoding stuff */
4097 int mqdiff, mquant; /* MB quantization */
4098 int ttmb = v->ttfrm; /* MB Transform type */
4099 int mb_has_coeffs = 0; /* last_flag */
4100 int index, index1; /* LUT indexes */
4101 int val, sign; /* temp values */
4102 int first_block = 1;
4104 int skipped, direct;
4105 int dmv_x[2], dmv_y[2];
4106 int bmvtype = BMV_TYPE_BACKWARD;
4108 mquant = v->pq; /* lossy initialization */
4112 direct = get_bits1(gb);
4114 direct = v->direct_mb_plane[mb_pos];
4116 skipped = get_bits1(gb);
4118 skipped = v->s.mbskip_table[mb_pos];
4120 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4121 for (i = 0; i < 6; i++) {
4122 v->mb_type[0][s->block_index[i]] = 0;
4123 s->dc_val[0][s->block_index[i]] = 0;
4125 s->current_picture.qscale_table[mb_pos] = 0;
4129 GET_MVDATA(dmv_x[0], dmv_y[0]);
4130 dmv_x[1] = dmv_x[0];
4131 dmv_y[1] = dmv_y[0];
4133 if (skipped || !s->mb_intra) {
4134 bmvtype = decode012(gb);
4137 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4140 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4143 bmvtype = BMV_TYPE_INTERPOLATED;
4144 dmv_x[0] = dmv_y[0] = 0;
4148 for (i = 0; i < 6; i++)
4149 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4153 bmvtype = BMV_TYPE_INTERPOLATED;
4154 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4155 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4159 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4162 s->current_picture.qscale_table[mb_pos] = mquant;
4164 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4165 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4166 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4167 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4169 if (!mb_has_coeffs && !s->mb_intra) {
4170 /* no coded blocks - effectively skipped */
4171 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4172 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4175 if (s->mb_intra && !mb_has_coeffs) {
4177 s->current_picture.qscale_table[mb_pos] = mquant;
4178 s->ac_pred = get_bits1(gb);
4180 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4182 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4183 GET_MVDATA(dmv_x[0], dmv_y[0]);
4184 if (!mb_has_coeffs) {
4185 /* interpolated skipped block */
4186 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4187 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4191 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4193 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4196 s->ac_pred = get_bits1(gb);
4197 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4199 s->current_picture.qscale_table[mb_pos] = mquant;
4200 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4201 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4205 for (i = 0; i < 6; i++) {
4206 s->dc_val[0][s->block_index[i]] = 0;
4208 val = ((cbp >> (5 - i)) & 1);
4209 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4210 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4212 /* check if prediction blocks A and C are available */
4213 v->a_avail = v->c_avail = 0;
4214 if (i == 2 || i == 3 || !s->first_slice_line)
4215 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4216 if (i == 1 || i == 3 || s->mb_x)
4217 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4219 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4220 (i & 4) ? v->codingset2 : v->codingset);
4221 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4223 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4225 for (j = 0; j < 64; j++)
4226 s->block[i][j] <<= 1;
4227 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4229 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4230 first_block, s->dest[dst_idx] + off,
4231 (i & 4) ? s->uvlinesize : s->linesize,
4232 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4233 if (!v->ttmbf && ttmb < 8)
4240 /** Decode one B-frame MB (in interlaced field B picture)
4242 static void vc1_decode_b_mb_intfi(VC1Context *v)
4244 MpegEncContext *s = &v->s;
4245 GetBitContext *gb = &s->gb;
4247 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4248 int cbp = 0; /* cbp decoding stuff */
4249 int mqdiff, mquant; /* MB quantization */
4250 int ttmb = v->ttfrm; /* MB Transform type */
4251 int mb_has_coeffs = 0; /* last_flag */
4252 int val; /* temp value */
4253 int first_block = 1;
4256 int dmv_x[2], dmv_y[2], pred_flag[2];
4257 int bmvtype = BMV_TYPE_BACKWARD;
4260 mquant = v->pq; /* Lossy initialization */
4263 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4264 if (idx_mbmode <= 1) { // intra MB
4265 s->mb_intra = v->is_intra[s->mb_x] = 1;
4266 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4267 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4268 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4270 s->current_picture.qscale_table[mb_pos] = mquant;
4271 /* Set DC scale - y and c use the same (not sure if necessary here) */
4272 s->y_dc_scale = s->y_dc_scale_table[mquant];
4273 s->c_dc_scale = s->c_dc_scale_table[mquant];
4274 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4275 mb_has_coeffs = idx_mbmode & 1;
4277 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4279 for (i = 0; i < 6; i++) {
4280 s->dc_val[0][s->block_index[i]] = 0;
4282 val = ((cbp >> (5 - i)) & 1);
4283 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4284 v->a_avail = v->c_avail = 0;
4285 if (i == 2 || i == 3 || !s->first_slice_line)
4286 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4287 if (i == 1 || i == 3 || s->mb_x)
4288 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4290 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4291 (i & 4) ? v->codingset2 : v->codingset);
4292 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4294 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4296 for (j = 0; j < 64; j++)
4297 s->block[i][j] <<= 1;
4298 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4299 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4300 // TODO: yet to perform loop filter
4303 s->mb_intra = v->is_intra[s->mb_x] = 0;
4304 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4305 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4307 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4309 fwd = v->forward_mb_plane[mb_pos];
4310 if (idx_mbmode <= 5) { // 1-MV
4312 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4313 pred_flag[0] = pred_flag[1] = 0;
4315 bmvtype = BMV_TYPE_FORWARD;
4317 bmvtype = decode012(gb);
4320 bmvtype = BMV_TYPE_BACKWARD;
4323 bmvtype = BMV_TYPE_DIRECT;
4326 bmvtype = BMV_TYPE_INTERPOLATED;
4327 interpmvp = get_bits1(gb);
4330 v->bmvtype = bmvtype;
4331 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4332 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4335 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4337 if (bmvtype == BMV_TYPE_DIRECT) {
4338 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4339 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4341 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4342 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4343 mb_has_coeffs = !(idx_mbmode & 2);
4346 bmvtype = BMV_TYPE_FORWARD;
4347 v->bmvtype = bmvtype;
4348 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4349 for (i = 0; i < 6; i++) {
4351 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4352 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4353 val = ((v->fourmvbp >> (3 - i)) & 1);
4355 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4356 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4357 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4359 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4360 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4362 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4364 mb_has_coeffs = idx_mbmode & 1;
4367 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4371 s->current_picture.qscale_table[mb_pos] = mquant;
4372 if (!v->ttmbf && cbp) {
4373 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4376 for (i = 0; i < 6; i++) {
4377 s->dc_val[0][s->block_index[i]] = 0;
4379 val = ((cbp >> (5 - i)) & 1);
4380 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4382 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4383 first_block, s->dest[dst_idx] + off,
4384 (i & 4) ? s->uvlinesize : s->linesize,
4385 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4386 if (!v->ttmbf && ttmb < 8)
4394 /** Decode one B-frame MB (in interlaced frame B picture)
4396 static int vc1_decode_b_mb_intfr(VC1Context *v)
4398 MpegEncContext *s = &v->s;
4399 GetBitContext *gb = &s->gb;
4401 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4402 int cbp = 0; /* cbp decoding stuff */
4403 int mqdiff, mquant; /* MB quantization */
4404 int ttmb = v->ttfrm; /* MB Transform type */
4405 int mvsw = 0; /* motion vector switch */
4406 int mb_has_coeffs = 1; /* last_flag */
4407 int dmv_x, dmv_y; /* Differential MV components */
4408 int val; /* temp value */
4409 int first_block = 1;
4411 int skipped, direct, twomv = 0;
4412 int block_cbp = 0, pat, block_tt = 0;
4413 int idx_mbmode = 0, mvbp;
4414 int stride_y, fieldtx;
4415 int bmvtype = BMV_TYPE_BACKWARD;
4418 mquant = v->pq; /* Lossy initialization */
4421 skipped = get_bits1(gb);
4423 skipped = v->s.mbskip_table[mb_pos];
4426 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4427 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4429 v->blk_mv_type[s->block_index[0]] = 1;
4430 v->blk_mv_type[s->block_index[1]] = 1;
4431 v->blk_mv_type[s->block_index[2]] = 1;
4432 v->blk_mv_type[s->block_index[3]] = 1;
4434 v->blk_mv_type[s->block_index[0]] = 0;
4435 v->blk_mv_type[s->block_index[1]] = 0;
4436 v->blk_mv_type[s->block_index[2]] = 0;
4437 v->blk_mv_type[s->block_index[3]] = 0;
4442 direct = get_bits1(gb);
4444 direct = v->direct_mb_plane[mb_pos];
4447 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);
4448 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);
4449 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);
4450 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);
4453 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);
4454 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);
4455 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);
4456 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);
4458 for (i = 1; i < 4; i += 2) {
4459 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4460 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4461 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4462 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4465 for (i = 1; i < 4; i++) {
4466 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4467 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4468 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4469 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4474 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4475 for (i = 0; i < 4; i++) {
4476 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4477 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4478 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4479 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4481 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4482 s->mb_intra = v->is_intra[s->mb_x] = 1;
4483 for (i = 0; i < 6; i++)
4484 v->mb_type[0][s->block_index[i]] = 1;
4485 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4486 mb_has_coeffs = get_bits1(gb);
4488 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4489 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4491 s->current_picture.qscale_table[mb_pos] = mquant;
4492 /* Set DC scale - y and c use the same (not sure if necessary here) */
4493 s->y_dc_scale = s->y_dc_scale_table[mquant];
4494 s->c_dc_scale = s->c_dc_scale_table[mquant];
4496 for (i = 0; i < 6; i++) {
4497 s->dc_val[0][s->block_index[i]] = 0;
4499 val = ((cbp >> (5 - i)) & 1);
4500 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4501 v->a_avail = v->c_avail = 0;
4502 if (i == 2 || i == 3 || !s->first_slice_line)
4503 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4504 if (i == 1 || i == 3 || s->mb_x)
4505 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4507 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4508 (i & 4) ? v->codingset2 : v->codingset);
4509 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4511 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4513 stride_y = s->linesize << fieldtx;
4514 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4516 stride_y = s->uvlinesize;
4519 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4522 s->mb_intra = v->is_intra[s->mb_x] = 0;
4524 if (skipped || !s->mb_intra) {
4525 bmvtype = decode012(gb);
4528 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4531 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4534 bmvtype = BMV_TYPE_INTERPOLATED;
4538 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4539 mvsw = get_bits1(gb);
4542 if (!skipped) { // inter MB
4543 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4545 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4547 if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
4548 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4549 } else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
4550 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4554 for (i = 0; i < 6; i++)
4555 v->mb_type[0][s->block_index[i]] = 0;
4556 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4557 /* for all motion vector read MVDATA and motion compensate each block */
4561 for (i = 0; i < 4; i++) {
4562 vc1_mc_4mv_luma(v, i, 0, 0);
4563 vc1_mc_4mv_luma(v, i, 1, 1);
4565 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4566 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4571 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4573 for (i = 0; i < 4; i++) {
4576 val = ((mvbp >> (3 - i)) & 1);
4578 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4580 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4581 vc1_mc_4mv_luma(v, j, dir, dir);
4582 vc1_mc_4mv_luma(v, j+1, dir, dir);
4585 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4586 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4587 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4591 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4593 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4598 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4600 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4603 dir = bmvtype == BMV_TYPE_BACKWARD;
4610 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4611 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4615 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4616 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4619 for (i = 0; i < 2; i++) {
4620 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];
4621 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];
4622 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];
4623 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];
4626 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4627 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4630 vc1_mc_4mv_luma(v, 0, dir, 0);
4631 vc1_mc_4mv_luma(v, 1, dir, 0);
4632 vc1_mc_4mv_luma(v, 2, dir2, 0);
4633 vc1_mc_4mv_luma(v, 3, dir2, 0);
4634 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4636 dir = bmvtype == BMV_TYPE_BACKWARD;
4638 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4641 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4643 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4644 v->blk_mv_type[s->block_index[0]] = 1;
4645 v->blk_mv_type[s->block_index[1]] = 1;
4646 v->blk_mv_type[s->block_index[2]] = 1;
4647 v->blk_mv_type[s->block_index[3]] = 1;
4648 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4649 for (i = 0; i < 2; i++) {
4650 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];
4651 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];
4657 GET_MQUANT(); // p. 227
4658 s->current_picture.qscale_table[mb_pos] = mquant;
4659 if (!v->ttmbf && cbp)
4660 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4661 for (i = 0; i < 6; i++) {
4662 s->dc_val[0][s->block_index[i]] = 0;
4664 val = ((cbp >> (5 - i)) & 1);
4666 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4668 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4670 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4671 first_block, s->dest[dst_idx] + off,
4672 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4673 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4674 block_cbp |= pat << (i << 2);
4675 if (!v->ttmbf && ttmb < 8)
4683 for (i = 0; i < 6; i++) {
4684 v->mb_type[0][s->block_index[i]] = 0;
4685 s->dc_val[0][s->block_index[i]] = 0;
4687 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4688 s->current_picture.qscale_table[mb_pos] = 0;
4689 v->blk_mv_type[s->block_index[0]] = 0;
4690 v->blk_mv_type[s->block_index[1]] = 0;
4691 v->blk_mv_type[s->block_index[2]] = 0;
4692 v->blk_mv_type[s->block_index[3]] = 0;
4695 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4696 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4697 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4699 dir = bmvtype == BMV_TYPE_BACKWARD;
4700 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4705 for (i = 0; i < 2; i++) {
4706 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];
4707 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];
4708 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];
4709 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];
4712 v->blk_mv_type[s->block_index[0]] = 1;
4713 v->blk_mv_type[s->block_index[1]] = 1;
4714 v->blk_mv_type[s->block_index[2]] = 1;
4715 v->blk_mv_type[s->block_index[3]] = 1;
4716 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4717 for (i = 0; i < 2; i++) {
4718 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];
4719 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];
4726 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4731 if (s->mb_x == s->mb_width - 1)
4732 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4733 v->cbp[s->mb_x] = block_cbp;
4734 v->ttblk[s->mb_x] = block_tt;
4738 /** Decode blocks of I-frame
4740 static void vc1_decode_i_blocks(VC1Context *v)
4743 MpegEncContext *s = &v->s;
4748 /* select codingmode used for VLC tables selection */
4749 switch (v->y_ac_table_index) {
4751 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4754 v->codingset = CS_HIGH_MOT_INTRA;
4757 v->codingset = CS_MID_RATE_INTRA;
4761 switch (v->c_ac_table_index) {
4763 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4766 v->codingset2 = CS_HIGH_MOT_INTER;
4769 v->codingset2 = CS_MID_RATE_INTER;
4773 /* Set DC scale - y and c use the same */
4774 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4775 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4778 s->mb_x = s->mb_y = 0;
4780 s->first_slice_line = 1;
4781 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4783 init_block_index(v);
4784 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4786 ff_update_block_index(s);
4787 dst[0] = s->dest[0];
4788 dst[1] = dst[0] + 8;
4789 dst[2] = s->dest[0] + s->linesize * 8;
4790 dst[3] = dst[2] + 8;
4791 dst[4] = s->dest[1];
4792 dst[5] = s->dest[2];
4793 s->dsp.clear_blocks(s->block[0]);
4794 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4795 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4796 s->current_picture.qscale_table[mb_pos] = v->pq;
4797 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4798 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4800 // do actual MB decoding and displaying
4801 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4802 v->s.ac_pred = get_bits1(&v->s.gb);
4804 for (k = 0; k < 6; k++) {
4805 val = ((cbp >> (5 - k)) & 1);
4808 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4812 cbp |= val << (5 - k);
4814 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4816 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4818 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4819 if (v->pq >= 9 && v->overlap) {
4821 for (j = 0; j < 64; j++)
4822 s->block[k][j] <<= 1;
4823 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4826 for (j = 0; j < 64; j++)
4827 s->block[k][j] = (s->block[k][j] - 64) << 1;
4828 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4832 if (v->pq >= 9 && v->overlap) {
4834 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4835 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4836 if (!(s->flags & CODEC_FLAG_GRAY)) {
4837 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4838 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4841 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4842 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4843 if (!s->first_slice_line) {
4844 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4845 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4846 if (!(s->flags & CODEC_FLAG_GRAY)) {
4847 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4848 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4851 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4852 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4854 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4856 if (get_bits_count(&s->gb) > v->bits) {
4857 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4858 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4859 get_bits_count(&s->gb), v->bits);
4863 if (!v->s.loop_filter)
4864 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4866 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4868 s->first_slice_line = 0;
4870 if (v->s.loop_filter)
4871 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4873 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4874 * profile, these only differ are when decoding MSS2 rectangles. */
4875 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4878 /** Decode blocks of I-frame for advanced profile
4880 static void vc1_decode_i_blocks_adv(VC1Context *v)
4883 MpegEncContext *s = &v->s;
4889 GetBitContext *gb = &s->gb;
4891 /* select codingmode used for VLC tables selection */
4892 switch (v->y_ac_table_index) {
4894 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4897 v->codingset = CS_HIGH_MOT_INTRA;
4900 v->codingset = CS_MID_RATE_INTRA;
4904 switch (v->c_ac_table_index) {
4906 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4909 v->codingset2 = CS_HIGH_MOT_INTER;
4912 v->codingset2 = CS_MID_RATE_INTER;
4917 s->mb_x = s->mb_y = 0;
4919 s->first_slice_line = 1;
4920 s->mb_y = s->start_mb_y;
4921 if (s->start_mb_y) {
4923 init_block_index(v);
4924 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4925 (1 + s->b8_stride) * sizeof(*s->coded_block));
4927 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4929 init_block_index(v);
4930 for (;s->mb_x < s->mb_width; s->mb_x++) {
4931 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4932 ff_update_block_index(s);
4933 s->dsp.clear_blocks(block[0]);
4934 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4935 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4936 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4937 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4939 // do actual MB decoding and displaying
4940 if (v->fieldtx_is_raw)
4941 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4942 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4943 if ( v->acpred_is_raw)
4944 v->s.ac_pred = get_bits1(&v->s.gb);
4946 v->s.ac_pred = v->acpred_plane[mb_pos];
4948 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4949 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4953 s->current_picture.qscale_table[mb_pos] = mquant;
4954 /* Set DC scale - y and c use the same */
4955 s->y_dc_scale = s->y_dc_scale_table[mquant];
4956 s->c_dc_scale = s->c_dc_scale_table[mquant];
4958 for (k = 0; k < 6; k++) {
4959 val = ((cbp >> (5 - k)) & 1);
4962 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4966 cbp |= val << (5 - k);
4968 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4969 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4971 vc1_decode_i_block_adv(v, block[k], k, val,
4972 (k < 4) ? v->codingset : v->codingset2, mquant);
4974 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4976 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4979 vc1_smooth_overlap_filter_iblk(v);
4980 vc1_put_signed_blocks_clamped(v);
4981 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4983 if (get_bits_count(&s->gb) > v->bits) {
4984 // TODO: may need modification to handle slice coding
4985 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4986 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4987 get_bits_count(&s->gb), v->bits);
4991 if (!v->s.loop_filter)
4992 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4994 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4995 s->first_slice_line = 0;
4998 /* raw bottom MB row */
5000 init_block_index(v);
5002 for (;s->mb_x < s->mb_width; s->mb_x++) {
5003 ff_update_block_index(s);
5004 vc1_put_signed_blocks_clamped(v);
5005 if (v->s.loop_filter)
5006 vc1_loop_filter_iblk_delayed(v, v->pq);
5008 if (v->s.loop_filter)
5009 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5010 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5011 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5014 static void vc1_decode_p_blocks(VC1Context *v)
5016 MpegEncContext *s = &v->s;
5017 int apply_loop_filter;
5019 /* select codingmode used for VLC tables selection */
5020 switch (v->c_ac_table_index) {
5022 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5025 v->codingset = CS_HIGH_MOT_INTRA;
5028 v->codingset = CS_MID_RATE_INTRA;
5032 switch (v->c_ac_table_index) {
5034 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5037 v->codingset2 = CS_HIGH_MOT_INTER;
5040 v->codingset2 = CS_MID_RATE_INTER;
5044 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5045 v->fcm == PROGRESSIVE;
5046 s->first_slice_line = 1;
5047 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5048 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5050 init_block_index(v);
5051 for (; s->mb_x < s->mb_width; s->mb_x++) {
5052 ff_update_block_index(s);
5054 if (v->fcm == ILACE_FIELD)
5055 vc1_decode_p_mb_intfi(v);
5056 else if (v->fcm == ILACE_FRAME)
5057 vc1_decode_p_mb_intfr(v);
5058 else vc1_decode_p_mb(v);
5059 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5060 vc1_apply_p_loop_filter(v);
5061 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5062 // TODO: may need modification to handle slice coding
5063 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5064 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5065 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5069 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5070 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5071 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5072 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5073 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5074 s->first_slice_line = 0;
5076 if (apply_loop_filter) {
5078 init_block_index(v);
5079 for (; s->mb_x < s->mb_width; s->mb_x++) {
5080 ff_update_block_index(s);
5081 vc1_apply_p_loop_filter(v);
5084 if (s->end_mb_y >= s->start_mb_y)
5085 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5086 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5087 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5090 static void vc1_decode_b_blocks(VC1Context *v)
5092 MpegEncContext *s = &v->s;
5094 /* select codingmode used for VLC tables selection */
5095 switch (v->c_ac_table_index) {
5097 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5100 v->codingset = CS_HIGH_MOT_INTRA;
5103 v->codingset = CS_MID_RATE_INTRA;
5107 switch (v->c_ac_table_index) {
5109 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5112 v->codingset2 = CS_HIGH_MOT_INTER;
5115 v->codingset2 = CS_MID_RATE_INTER;
5119 s->first_slice_line = 1;
5120 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5122 init_block_index(v);
5123 for (; s->mb_x < s->mb_width; s->mb_x++) {
5124 ff_update_block_index(s);
5126 if (v->fcm == ILACE_FIELD)
5127 vc1_decode_b_mb_intfi(v);
5128 else if (v->fcm == ILACE_FRAME)
5129 vc1_decode_b_mb_intfr(v);
5132 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5133 // TODO: may need modification to handle slice coding
5134 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5135 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5136 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5139 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5141 if (!v->s.loop_filter)
5142 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5144 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5145 s->first_slice_line = 0;
5147 if (v->s.loop_filter)
5148 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5149 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5150 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5153 static void vc1_decode_skip_blocks(VC1Context *v)
5155 MpegEncContext *s = &v->s;
5157 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5158 s->first_slice_line = 1;
5159 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5161 init_block_index(v);
5162 ff_update_block_index(s);
5163 if (s->last_picture.f.data[0]) {
5164 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5165 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5166 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5168 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5169 s->first_slice_line = 0;
5171 s->pict_type = AV_PICTURE_TYPE_P;
5174 void ff_vc1_decode_blocks(VC1Context *v)
5177 v->s.esc3_level_length = 0;
5179 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5182 v->left_blk_idx = -1;
5183 v->topleft_blk_idx = 1;
5185 switch (v->s.pict_type) {
5186 case AV_PICTURE_TYPE_I:
5187 if (v->profile == PROFILE_ADVANCED)
5188 vc1_decode_i_blocks_adv(v);
5190 vc1_decode_i_blocks(v);
5192 case AV_PICTURE_TYPE_P:
5193 if (v->p_frame_skipped)
5194 vc1_decode_skip_blocks(v);
5196 vc1_decode_p_blocks(v);
5198 case AV_PICTURE_TYPE_B:
5200 if (v->profile == PROFILE_ADVANCED)
5201 vc1_decode_i_blocks_adv(v);
5203 vc1_decode_i_blocks(v);
5205 vc1_decode_b_blocks(v);
5211 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5215 * Transform coefficients for both sprites in 16.16 fixed point format,
5216 * in the order they appear in the bitstream:
5218 * rotation 1 (unused)
5220 * rotation 2 (unused)
5227 int effect_type, effect_flag;
5228 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5229 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5232 static inline int get_fp_val(GetBitContext* gb)
5234 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5237 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5241 switch (get_bits(gb, 2)) {
5244 c[2] = get_fp_val(gb);
5248 c[0] = c[4] = get_fp_val(gb);
5249 c[2] = get_fp_val(gb);
5252 c[0] = get_fp_val(gb);
5253 c[2] = get_fp_val(gb);
5254 c[4] = get_fp_val(gb);
5257 c[0] = get_fp_val(gb);
5258 c[1] = get_fp_val(gb);
5259 c[2] = get_fp_val(gb);
5260 c[3] = get_fp_val(gb);
5261 c[4] = get_fp_val(gb);
5264 c[5] = get_fp_val(gb);
5266 c[6] = get_fp_val(gb);
5271 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5273 AVCodecContext *avctx = v->s.avctx;
5276 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5277 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5278 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5279 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5280 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5281 for (i = 0; i < 7; i++)
5282 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5283 sd->coefs[sprite][i] / (1<<16),
5284 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5285 av_log(avctx, AV_LOG_DEBUG, "\n");
5289 if (sd->effect_type = get_bits_long(gb, 30)) {
5290 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5292 vc1_sprite_parse_transform(gb, sd->effect_params1);
5295 vc1_sprite_parse_transform(gb, sd->effect_params1);
5296 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5299 for (i = 0; i < sd->effect_pcount1; i++)
5300 sd->effect_params1[i] = get_fp_val(gb);
5302 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5303 // effect 13 is simple alpha blending and matches the opacity above
5304 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5305 for (i = 0; i < sd->effect_pcount1; i++)
5306 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5307 sd->effect_params1[i] / (1 << 16),
5308 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5309 av_log(avctx, AV_LOG_DEBUG, "\n");
5312 sd->effect_pcount2 = get_bits(gb, 16);
5313 if (sd->effect_pcount2 > 10) {
5314 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5316 } else if (sd->effect_pcount2) {
5318 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5319 while (++i < sd->effect_pcount2) {
5320 sd->effect_params2[i] = get_fp_val(gb);
5321 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5322 sd->effect_params2[i] / (1 << 16),
5323 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5325 av_log(avctx, AV_LOG_DEBUG, "\n");
5328 if (sd->effect_flag = get_bits1(gb))
5329 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5331 if (get_bits_count(gb) >= gb->size_in_bits +
5332 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
5333 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5334 if (get_bits_count(gb) < gb->size_in_bits - 8)
5335 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5338 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5340 int i, plane, row, sprite;
5341 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5342 uint8_t* src_h[2][2];
5343 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5345 MpegEncContext *s = &v->s;
5347 for (i = 0; i < 2; i++) {
5348 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5349 xadv[i] = sd->coefs[i][0];
5350 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5351 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5353 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5354 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5356 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5358 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5359 int width = v->output_width>>!!plane;
5361 for (row = 0; row < v->output_height>>!!plane; row++) {
5362 uint8_t *dst = v->sprite_output_frame.data[plane] +
5363 v->sprite_output_frame.linesize[plane] * row;
5365 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5366 uint8_t *iplane = s->current_picture.f.data[plane];
5367 int iline = s->current_picture.f.linesize[plane];
5368 int ycoord = yoff[sprite] + yadv[sprite] * row;
5369 int yline = ycoord >> 16;
5371 ysub[sprite] = ycoord & 0xFFFF;
5373 iplane = s->last_picture.f.data[plane];
5374 iline = s->last_picture.f.linesize[plane];
5376 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5377 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5378 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5380 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5382 if (sr_cache[sprite][0] != yline) {
5383 if (sr_cache[sprite][1] == yline) {
5384 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5385 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5387 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5388 sr_cache[sprite][0] = yline;
5391 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5392 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5393 iplane + next_line, xoff[sprite],
5394 xadv[sprite], width);
5395 sr_cache[sprite][1] = yline + 1;
5397 src_h[sprite][0] = v->sr_rows[sprite][0];
5398 src_h[sprite][1] = v->sr_rows[sprite][1];
5402 if (!v->two_sprites) {
5404 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5406 memcpy(dst, src_h[0][0], width);
5409 if (ysub[0] && ysub[1]) {
5410 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5411 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5412 } else if (ysub[0]) {
5413 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5414 src_h[1][0], alpha, width);
5415 } else if (ysub[1]) {
5416 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5417 src_h[0][0], (1<<16)-1-alpha, width);
5419 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5425 for (i = 0; i < 2; i++) {
5435 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5438 MpegEncContext *s = &v->s;
5439 AVCodecContext *avctx = s->avctx;
5442 vc1_parse_sprites(v, gb, &sd);
5444 if (!s->current_picture.f.data[0]) {
5445 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5449 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5450 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5454 av_frame_unref(&v->sprite_output_frame);
5455 if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
5458 vc1_draw_sprites(v, &sd);
5463 static void vc1_sprite_flush(AVCodecContext *avctx)
5465 VC1Context *v = avctx->priv_data;
5466 MpegEncContext *s = &v->s;
5467 AVFrame *f = &s->current_picture.f;
5470 /* Windows Media Image codecs have a convergence interval of two keyframes.
5471 Since we can't enforce it, clear to black the missing sprite. This is
5472 wrong but it looks better than doing nothing. */
5475 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5476 for (i = 0; i < v->sprite_height>>!!plane; i++)
5477 memset(f->data[plane] + i * f->linesize[plane],
5478 plane ? 128 : 0, f->linesize[plane]);
5483 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5485 MpegEncContext *s = &v->s;
5488 /* Allocate mb bitplanes */
5489 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5490 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5491 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5492 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5493 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5494 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5496 v->n_allocated_blks = s->mb_width + 2;
5497 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5498 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5499 v->cbp = v->cbp_base + s->mb_stride;
5500 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5501 v->ttblk = v->ttblk_base + s->mb_stride;
5502 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5503 v->is_intra = v->is_intra_base + s->mb_stride;
5504 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5505 v->luma_mv = v->luma_mv_base + s->mb_stride;
5507 /* allocate block type info in that way so it could be used with s->block_index[] */
5508 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5509 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5510 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5511 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5513 /* allocate memory to store block level MV info */
5514 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5515 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5516 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5517 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5518 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5519 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5520 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5521 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);
5523 /* Init coded blocks info */
5524 if (v->profile == PROFILE_ADVANCED) {
5525 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5527 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5531 ff_intrax8_common_init(&v->x8,s);
5533 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5534 for (i = 0; i < 4; i++)
5535 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5538 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5539 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5546 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5549 for (i = 0; i < 64; i++) {
5550 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5551 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5552 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5553 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5554 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5555 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5561 /** Initialize a VC1/WMV3 decoder
5562 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5563 * @todo TODO: Decypher remaining bits in extra_data
5565 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5567 VC1Context *v = avctx->priv_data;
5568 MpegEncContext *s = &v->s;
5571 /* save the container output size for WMImage */
5572 v->output_width = avctx->width;
5573 v->output_height = avctx->height;
5575 if (!avctx->extradata_size || !avctx->extradata)
5577 if (!(avctx->flags & CODEC_FLAG_GRAY))
5578 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5580 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5581 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5583 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5584 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5586 if (ff_vc1_init_common(v) < 0)
5588 // ensure static VLC tables are initialized
5589 if (ff_msmpeg4_decode_init(avctx) < 0)
5591 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5593 // Hack to ensure the above functions will be called
5594 // again once we know all necessary settings.
5595 // That this is necessary might indicate a bug.
5596 ff_vc1_decode_end(avctx);
5598 ff_h264chroma_init(&v->h264chroma, 8);
5599 ff_vc1dsp_init(&v->vc1dsp);
5601 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5604 // looks like WMV3 has a sequence header stored in the extradata
5605 // advanced sequence header may be before the first frame
5606 // the last byte of the extradata is a version number, 1 for the
5607 // samples we can decode
5609 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5611 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5614 count = avctx->extradata_size*8 - get_bits_count(&gb);
5616 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5617 count, get_bits(&gb, count));
5618 } else if (count < 0) {
5619 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5621 } else { // VC1/WVC1/WVP2
5622 const uint8_t *start = avctx->extradata;
5623 uint8_t *end = avctx->extradata + avctx->extradata_size;
5624 const uint8_t *next;
5625 int size, buf2_size;
5626 uint8_t *buf2 = NULL;
5627 int seq_initialized = 0, ep_initialized = 0;
5629 if (avctx->extradata_size < 16) {
5630 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5634 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5635 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5637 for (; next < end; start = next) {
5638 next = find_next_marker(start + 4, end);
5639 size = next - start - 4;
5642 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5643 init_get_bits(&gb, buf2, buf2_size * 8);
5644 switch (AV_RB32(start)) {
5645 case VC1_CODE_SEQHDR:
5646 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5650 seq_initialized = 1;
5652 case VC1_CODE_ENTRYPOINT:
5653 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5662 if (!seq_initialized || !ep_initialized) {
5663 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5666 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5669 avctx->profile = v->profile;
5670 if (v->profile == PROFILE_ADVANCED)
5671 avctx->level = v->level;
5673 avctx->has_b_frames = !!avctx->max_b_frames;
5675 s->mb_width = (avctx->coded_width + 15) >> 4;
5676 s->mb_height = (avctx->coded_height + 15) >> 4;
5678 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5679 ff_vc1_init_transposed_scantables(v);
5681 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5686 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5687 v->sprite_width = avctx->coded_width;
5688 v->sprite_height = avctx->coded_height;
5690 avctx->coded_width = avctx->width = v->output_width;
5691 avctx->coded_height = avctx->height = v->output_height;
5693 // prevent 16.16 overflows
5694 if (v->sprite_width > 1 << 14 ||
5695 v->sprite_height > 1 << 14 ||
5696 v->output_width > 1 << 14 ||
5697 v->output_height > 1 << 14) return -1;
5699 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5700 avpriv_request_sample(avctx, "odd sprites support");
5701 return AVERROR_PATCHWELCOME;
5707 /** Close a VC1/WMV3 decoder
5708 * @warning Initial try at using MpegEncContext stuff
5710 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5712 VC1Context *v = avctx->priv_data;
5715 av_frame_unref(&v->sprite_output_frame);
5717 for (i = 0; i < 4; i++)
5718 av_freep(&v->sr_rows[i >> 1][i & 1]);
5719 av_freep(&v->hrd_rate);
5720 av_freep(&v->hrd_buffer);
5721 ff_MPV_common_end(&v->s);
5722 av_freep(&v->mv_type_mb_plane);
5723 av_freep(&v->direct_mb_plane);
5724 av_freep(&v->forward_mb_plane);
5725 av_freep(&v->fieldtx_plane);
5726 av_freep(&v->acpred_plane);
5727 av_freep(&v->over_flags_plane);
5728 av_freep(&v->mb_type_base);
5729 av_freep(&v->blk_mv_type_base);
5730 av_freep(&v->mv_f_base);
5731 av_freep(&v->mv_f_next_base);
5732 av_freep(&v->block);
5733 av_freep(&v->cbp_base);
5734 av_freep(&v->ttblk_base);
5735 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5736 av_freep(&v->luma_mv_base);
5737 ff_intrax8_common_end(&v->x8);
5742 /** Decode a VC1/WMV3 frame
5743 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5745 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5746 int *got_frame, AVPacket *avpkt)
5748 const uint8_t *buf = avpkt->data;
5749 int buf_size = avpkt->size, n_slices = 0, i, ret;
5750 VC1Context *v = avctx->priv_data;
5751 MpegEncContext *s = &v->s;
5752 AVFrame *pict = data;
5753 uint8_t *buf2 = NULL;
5754 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5755 int mb_height, n_slices1=-1;
5760 } *slices = NULL, *tmp;
5762 v->second_field = 0;
5764 if(s->flags & CODEC_FLAG_LOW_DELAY)
5767 /* no supplementary picture */
5768 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5769 /* special case for last picture */
5770 if (s->low_delay == 0 && s->next_picture_ptr) {
5771 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
5773 s->next_picture_ptr = NULL;
5781 //for advanced profile we may need to parse and unescape data
5782 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5784 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5786 return AVERROR(ENOMEM);
5788 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5789 const uint8_t *start, *end, *next;
5793 for (start = buf, end = buf + buf_size; next < end; start = next) {
5794 next = find_next_marker(start + 4, end);
5795 size = next - start - 4;
5796 if (size <= 0) continue;
5797 switch (AV_RB32(start)) {
5798 case VC1_CODE_FRAME:
5801 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5803 case VC1_CODE_FIELD: {
5805 if (avctx->hwaccel ||
5806 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5807 buf_start_second_field = start;
5808 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5812 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5813 if (!slices[n_slices].buf)
5815 buf_size3 = vc1_unescape_buffer(start + 4, size,
5816 slices[n_slices].buf);
5817 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5819 /* assuming that the field marker is at the exact middle,
5820 hope it's correct */
5821 slices[n_slices].mby_start = s->mb_height >> 1;
5822 n_slices1 = n_slices - 1; // index of the last slice of the first field
5826 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5827 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5828 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5829 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5831 case VC1_CODE_SLICE: {
5833 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5837 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5838 if (!slices[n_slices].buf)
5840 buf_size3 = vc1_unescape_buffer(start + 4, size,
5841 slices[n_slices].buf);
5842 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5844 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5850 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5851 const uint8_t *divider;
5854 divider = find_next_marker(buf, buf + buf_size);
5855 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5856 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5858 } else { // found field marker, unescape second field
5859 if (avctx->hwaccel ||
5860 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5861 buf_start_second_field = divider;
5862 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5866 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5867 if (!slices[n_slices].buf)
5869 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5870 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5872 slices[n_slices].mby_start = s->mb_height >> 1;
5873 n_slices1 = n_slices - 1;
5876 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5878 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5880 init_get_bits(&s->gb, buf2, buf_size2*8);
5882 init_get_bits(&s->gb, buf, buf_size*8);
5884 if (v->res_sprite) {
5885 v->new_sprite = !get_bits1(&s->gb);
5886 v->two_sprites = get_bits1(&s->gb);
5887 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5888 we're using the sprite compositor. These are intentionally kept separate
5889 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5890 the vc1 one for WVP2 */
5891 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5892 if (v->new_sprite) {
5893 // switch AVCodecContext parameters to those of the sprites
5894 avctx->width = avctx->coded_width = v->sprite_width;
5895 avctx->height = avctx->coded_height = v->sprite_height;
5902 if (s->context_initialized &&
5903 (s->width != avctx->coded_width ||
5904 s->height != avctx->coded_height)) {
5905 ff_vc1_decode_end(avctx);
5908 if (!s->context_initialized) {
5909 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5912 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5914 if (v->profile == PROFILE_ADVANCED) {
5915 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5917 s->h_edge_pos = avctx->coded_width;
5918 s->v_edge_pos = avctx->coded_height;
5922 /* We need to set current_picture_ptr before reading the header,
5923 * otherwise we cannot store anything in there. */
5924 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5925 int i = ff_find_unused_picture(s, 0);
5928 s->current_picture_ptr = &s->picture[i];
5931 // do parse frame header
5932 v->pic_header_flag = 0;
5933 v->first_pic_header_flag = 1;
5934 if (v->profile < PROFILE_ADVANCED) {
5935 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5939 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5943 v->first_pic_header_flag = 0;
5945 if (avctx->debug & FF_DEBUG_PICT_INFO)
5946 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5948 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5949 && s->pict_type != AV_PICTURE_TYPE_I) {
5950 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5954 if ((s->mb_height >> v->field_mode) == 0) {
5955 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5959 // process pulldown flags
5960 s->current_picture_ptr->f.repeat_pict = 0;
5961 // Pulldown flags are only valid when 'broadcast' has been set.
5962 // So ticks_per_frame will be 2
5965 s->current_picture_ptr->f.repeat_pict = 1;
5966 } else if (v->rptfrm) {
5968 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5971 // for skipping the frame
5972 s->current_picture.f.pict_type = s->pict_type;
5973 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5975 /* skip B-frames if we don't have reference frames */
5976 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5979 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5980 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5981 avctx->skip_frame >= AVDISCARD_ALL) {
5985 if (s->next_p_frame_damaged) {
5986 if (s->pict_type == AV_PICTURE_TYPE_B)
5989 s->next_p_frame_damaged = 0;
5992 if (ff_MPV_frame_start(s, avctx) < 0) {
5996 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5997 v->s.current_picture_ptr->f.top_field_first = v->tff;
5999 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
6000 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
6002 if (avctx->hwaccel) {
6003 if (v->field_mode && buf_start_second_field) {
6004 // decode first field
6005 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6006 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6008 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6010 if (avctx->hwaccel->end_frame(avctx) < 0)
6013 // decode second field
6014 s->gb = slices[n_slices1 + 1].gb;
6015 s->picture_structure = PICT_TOP_FIELD + v->tff;
6016 v->second_field = 1;
6017 v->pic_header_flag = 0;
6018 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6019 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6022 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
6024 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6026 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6028 if (avctx->hwaccel->end_frame(avctx) < 0)
6031 s->picture_structure = PICT_FRAME;
6032 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6034 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6036 if (avctx->hwaccel->end_frame(avctx) < 0)
6043 ff_mpeg_er_frame_start(s);
6045 v->bits = buf_size * 8;
6046 v->end_mb_x = s->mb_width;
6047 if (v->field_mode) {
6048 s->current_picture.f.linesize[0] <<= 1;
6049 s->current_picture.f.linesize[1] <<= 1;
6050 s->current_picture.f.linesize[2] <<= 1;
6052 s->uvlinesize <<= 1;
6054 mb_height = s->mb_height >> v->field_mode;
6056 av_assert0 (mb_height > 0);
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_WMV3IMAGE_DECODER
6236 AVCodec ff_wmv3image_decoder = {
6237 .name = "wmv3image",
6238 .type = AVMEDIA_TYPE_VIDEO,
6239 .id = AV_CODEC_ID_WMV3IMAGE,
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,
6245 .flush = vc1_sprite_flush,
6246 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6247 .pix_fmts = ff_pixfmt_list_420
6251 #if CONFIG_VC1IMAGE_DECODER
6252 AVCodec ff_vc1image_decoder = {
6254 .type = AVMEDIA_TYPE_VIDEO,
6255 .id = AV_CODEC_ID_VC1IMAGE,
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,
6261 .flush = vc1_sprite_flush,
6262 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6263 .pix_fmts = ff_pixfmt_list_420