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"
33 #include "mpegutils.h"
34 #include "mpegvideo.h"
36 #include "h264chroma.h"
40 #include "vc1acdata.h"
41 #include "msmpeg4data.h"
44 #include "vdpau_internal.h"
45 #include "libavutil/avassert.h"
50 #define MB_INTRA_VLC_BITS 9
54 // offset tables for interlaced picture MVDATA decoding
55 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
56 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
58 /***********************************************************************/
60 * @name VC-1 Bitplane decoding
66 static void init_block_index(VC1Context *v)
68 MpegEncContext *s = &v->s;
69 ff_init_block_index(s);
70 if (v->field_mode && !(v->second_field ^ v->tff)) {
71 s->dest[0] += s->current_picture_ptr->f->linesize[0];
72 s->dest[1] += s->current_picture_ptr->f->linesize[1];
73 s->dest[2] += s->current_picture_ptr->f->linesize[2];
77 /** @} */ //Bitplane group
79 static void vc1_put_signed_blocks_clamped(VC1Context *v)
81 MpegEncContext *s = &v->s;
82 int topleft_mb_pos, top_mb_pos;
83 int stride_y, fieldtx = 0;
86 /* The put pixels loop is always one MB row behind the decoding loop,
87 * because we can only put pixels when overlap filtering is done, and
88 * for filtering of the bottom edge of a MB, we need the next MB row
90 * Within the row, the put pixels loop is also one MB col behind the
91 * decoding loop. The reason for this is again, because for filtering
92 * of the right MB edge, we need the next MB present. */
93 if (!s->first_slice_line) {
95 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
96 if (v->fcm == ILACE_FRAME)
97 fieldtx = v->fieldtx_plane[topleft_mb_pos];
98 stride_y = s->linesize << fieldtx;
99 v_dist = (16 - fieldtx) >> (fieldtx == 0);
100 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
101 s->dest[0] - 16 * s->linesize - 16,
103 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
104 s->dest[0] - 16 * s->linesize - 8,
106 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
107 s->dest[0] - v_dist * s->linesize - 16,
109 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
110 s->dest[0] - v_dist * s->linesize - 8,
112 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
113 s->dest[1] - 8 * s->uvlinesize - 8,
115 s->idsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
116 s->dest[2] - 8 * s->uvlinesize - 8,
119 if (s->mb_x == s->mb_width - 1) {
120 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
121 if (v->fcm == ILACE_FRAME)
122 fieldtx = v->fieldtx_plane[top_mb_pos];
123 stride_y = s->linesize << fieldtx;
124 v_dist = fieldtx ? 15 : 8;
125 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
126 s->dest[0] - 16 * s->linesize,
128 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
129 s->dest[0] - 16 * s->linesize + 8,
131 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
132 s->dest[0] - v_dist * s->linesize,
134 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
135 s->dest[0] - v_dist * s->linesize + 8,
137 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
138 s->dest[1] - 8 * s->uvlinesize,
140 s->idsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
141 s->dest[2] - 8 * s->uvlinesize,
146 #define inc_blk_idx(idx) do { \
148 if (idx >= v->n_allocated_blks) \
152 inc_blk_idx(v->topleft_blk_idx);
153 inc_blk_idx(v->top_blk_idx);
154 inc_blk_idx(v->left_blk_idx);
155 inc_blk_idx(v->cur_blk_idx);
158 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
160 MpegEncContext *s = &v->s;
162 if (!s->first_slice_line) {
163 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
165 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
166 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
167 for (j = 0; j < 2; j++) {
168 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
170 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
173 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
175 if (s->mb_y == s->end_mb_y - 1) {
177 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
178 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
179 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
181 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
185 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
187 MpegEncContext *s = &v->s;
190 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
191 * means it runs two rows/cols behind the decoding loop. */
192 if (!s->first_slice_line) {
194 if (s->mb_y >= s->start_mb_y + 2) {
195 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
198 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
199 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
200 for (j = 0; j < 2; j++) {
201 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
203 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
207 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
210 if (s->mb_x == s->mb_width - 1) {
211 if (s->mb_y >= s->start_mb_y + 2) {
212 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
215 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
216 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
217 for (j = 0; j < 2; j++) {
218 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
220 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
224 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
227 if (s->mb_y == s->end_mb_y) {
230 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
231 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
233 for (j = 0; j < 2; j++) {
234 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
239 if (s->mb_x == s->mb_width - 1) {
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
242 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
244 for (j = 0; j < 2; j++) {
245 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
253 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
255 MpegEncContext *s = &v->s;
258 if (v->condover == CONDOVER_NONE)
261 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
263 /* Within a MB, the horizontal overlap always runs before the vertical.
264 * To accomplish that, we run the H on left and internal borders of the
265 * currently decoded MB. Then, we wait for the next overlap iteration
266 * to do H overlap on the right edge of this MB, before moving over and
267 * running the V overlap. Therefore, the V overlap makes us trail by one
268 * MB col and the H overlap filter makes us trail by one MB row. This
269 * is reflected in the time at which we run the put_pixels loop. */
270 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
271 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
272 v->over_flags_plane[mb_pos - 1])) {
273 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
274 v->block[v->cur_blk_idx][0]);
275 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
276 v->block[v->cur_blk_idx][2]);
277 if (!(s->flags & CODEC_FLAG_GRAY)) {
278 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
279 v->block[v->cur_blk_idx][4]);
280 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
281 v->block[v->cur_blk_idx][5]);
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
285 v->block[v->cur_blk_idx][1]);
286 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
287 v->block[v->cur_blk_idx][3]);
289 if (s->mb_x == s->mb_width - 1) {
290 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
291 v->over_flags_plane[mb_pos - s->mb_stride])) {
292 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
293 v->block[v->cur_blk_idx][0]);
294 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
295 v->block[v->cur_blk_idx][1]);
296 if (!(s->flags & CODEC_FLAG_GRAY)) {
297 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
298 v->block[v->cur_blk_idx][4]);
299 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
300 v->block[v->cur_blk_idx][5]);
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
304 v->block[v->cur_blk_idx][2]);
305 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
306 v->block[v->cur_blk_idx][3]);
309 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
310 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
311 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
312 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
313 v->block[v->left_blk_idx][0]);
314 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
315 v->block[v->left_blk_idx][1]);
316 if (!(s->flags & CODEC_FLAG_GRAY)) {
317 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
318 v->block[v->left_blk_idx][4]);
319 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
320 v->block[v->left_blk_idx][5]);
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
324 v->block[v->left_blk_idx][2]);
325 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
326 v->block[v->left_blk_idx][3]);
330 /** Do motion compensation over 1 macroblock
331 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
333 static void vc1_mc_1mv(VC1Context *v, int dir)
335 MpegEncContext *s = &v->s;
336 H264ChromaContext *h264chroma = &v->h264chroma;
337 uint8_t *srcY, *srcU, *srcV;
338 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
339 int v_edge_pos = s->v_edge_pos >> v->field_mode;
341 uint8_t (*luty)[256], (*lutuv)[256];
344 if ((!v->field_mode ||
345 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
346 !v->s.last_picture.f->data[0])
349 mx = s->mv[dir][0][0];
350 my = s->mv[dir][0][1];
352 // store motion vectors for further use in B frames
353 if (s->pict_type == AV_PICTURE_TYPE_P) {
354 for (i = 0; i < 4; i++) {
355 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
356 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
360 uvmx = (mx + ((mx & 3) == 3)) >> 1;
361 uvmy = (my + ((my & 3) == 3)) >> 1;
362 v->luma_mv[s->mb_x][0] = uvmx;
363 v->luma_mv[s->mb_x][1] = uvmy;
366 v->cur_field_type != v->ref_field_type[dir]) {
367 my = my - 2 + 4 * v->cur_field_type;
368 uvmy = uvmy - 2 + 4 * v->cur_field_type;
371 // fastuvmc shall be ignored for interlaced frame picture
372 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
373 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
374 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
377 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
378 srcY = s->current_picture.f->data[0];
379 srcU = s->current_picture.f->data[1];
380 srcV = s->current_picture.f->data[2];
382 lutuv = v->curr_lutuv;
383 use_ic = *v->curr_use_ic;
385 srcY = s->last_picture.f->data[0];
386 srcU = s->last_picture.f->data[1];
387 srcV = s->last_picture.f->data[2];
389 lutuv = v->last_lutuv;
390 use_ic = v->last_use_ic;
393 srcY = s->next_picture.f->data[0];
394 srcU = s->next_picture.f->data[1];
395 srcV = s->next_picture.f->data[2];
397 lutuv = v->next_lutuv;
398 use_ic = v->next_use_ic;
401 if (!srcY || !srcU) {
402 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
406 src_x = s->mb_x * 16 + (mx >> 2);
407 src_y = s->mb_y * 16 + (my >> 2);
408 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
409 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
411 if (v->profile != PROFILE_ADVANCED) {
412 src_x = av_clip( src_x, -16, s->mb_width * 16);
413 src_y = av_clip( src_y, -16, s->mb_height * 16);
414 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
415 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
417 src_x = av_clip( src_x, -17, s->avctx->coded_width);
418 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
419 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
420 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
423 srcY += src_y * s->linesize + src_x;
424 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
425 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
427 if (v->field_mode && v->ref_field_type[dir]) {
428 srcY += s->current_picture_ptr->f->linesize[0];
429 srcU += s->current_picture_ptr->f->linesize[1];
430 srcV += s->current_picture_ptr->f->linesize[2];
433 /* for grayscale we should not try to read from unknown area */
434 if (s->flags & CODEC_FLAG_GRAY) {
435 srcU = s->edge_emu_buffer + 18 * s->linesize;
436 srcV = s->edge_emu_buffer + 18 * s->linesize;
439 if (v->rangeredfrm || use_ic
440 || s->h_edge_pos < 22 || v_edge_pos < 22
441 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
442 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
443 uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
444 uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
446 srcY -= s->mspel * (1 + s->linesize);
447 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
448 s->linesize, s->linesize,
449 17 + s->mspel * 2, 17 + s->mspel * 2,
450 src_x - s->mspel, src_y - s->mspel,
451 s->h_edge_pos, v_edge_pos);
452 srcY = s->edge_emu_buffer;
453 s->vdsp.emulated_edge_mc(ubuf, srcU,
454 s->uvlinesize, s->uvlinesize,
457 s->h_edge_pos >> 1, v_edge_pos >> 1);
458 s->vdsp.emulated_edge_mc(vbuf, srcV,
459 s->uvlinesize, s->uvlinesize,
462 s->h_edge_pos >> 1, v_edge_pos >> 1);
465 /* if we deal with range reduction we need to scale source blocks */
466 if (v->rangeredfrm) {
471 for (j = 0; j < 17 + s->mspel * 2; j++) {
472 for (i = 0; i < 17 + s->mspel * 2; i++)
473 src[i] = ((src[i] - 128) >> 1) + 128;
478 for (j = 0; j < 9; j++) {
479 for (i = 0; i < 9; i++) {
480 src[i] = ((src[i] - 128) >> 1) + 128;
481 src2[i] = ((src2[i] - 128) >> 1) + 128;
483 src += s->uvlinesize;
484 src2 += s->uvlinesize;
487 /* if we deal with intensity compensation we need to scale source blocks */
493 for (j = 0; j < 17 + s->mspel * 2; j++) {
494 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
495 for (i = 0; i < 17 + s->mspel * 2; i++)
496 src[i] = luty[f][src[i]];
501 for (j = 0; j < 9; j++) {
502 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
503 for (i = 0; i < 9; i++) {
504 src[i] = lutuv[f][src[i]];
505 src2[i] = lutuv[f][src2[i]];
507 src += s->uvlinesize;
508 src2 += s->uvlinesize;
511 srcY += s->mspel * (1 + s->linesize);
515 dxy = ((my & 3) << 2) | (mx & 3);
516 v->vc1dsp.put_vc1_mspel_pixels_tab[0][dxy](s->dest[0] , srcY , s->linesize, v->rnd);
517 } else { // hpel mc - always used for luma
518 dxy = (my & 2) | ((mx & 2) >> 1);
520 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
522 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
525 if (s->flags & CODEC_FLAG_GRAY) return;
526 /* Chroma MC always uses qpel bilinear */
527 uvmx = (uvmx & 3) << 1;
528 uvmy = (uvmy & 3) << 1;
530 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
531 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
533 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
534 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
538 static inline int median4(int a, int b, int c, int d)
541 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
542 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
544 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
545 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
549 /** Do motion compensation for 4-MV macroblock - luminance block
551 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
553 MpegEncContext *s = &v->s;
555 int dxy, mx, my, src_x, src_y;
557 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
558 int v_edge_pos = s->v_edge_pos >> v->field_mode;
559 uint8_t (*luty)[256];
562 if ((!v->field_mode ||
563 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
564 !v->s.last_picture.f->data[0])
567 mx = s->mv[dir][n][0];
568 my = s->mv[dir][n][1];
571 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
572 srcY = s->current_picture.f->data[0];
574 use_ic = *v->curr_use_ic;
576 srcY = s->last_picture.f->data[0];
578 use_ic = v->last_use_ic;
581 srcY = s->next_picture.f->data[0];
583 use_ic = v->next_use_ic;
587 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
592 if (v->cur_field_type != v->ref_field_type[dir])
593 my = my - 2 + 4 * v->cur_field_type;
596 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
597 int same_count = 0, opp_count = 0, k;
598 int chosen_mv[2][4][2], f;
600 for (k = 0; k < 4; k++) {
601 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
602 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
603 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
607 f = opp_count > same_count;
608 switch (f ? opp_count : same_count) {
610 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
611 chosen_mv[f][2][0], chosen_mv[f][3][0]);
612 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
613 chosen_mv[f][2][1], chosen_mv[f][3][1]);
616 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
617 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
620 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
621 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
626 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
627 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
628 for (k = 0; k < 4; k++)
629 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
632 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
634 int width = s->avctx->coded_width;
635 int height = s->avctx->coded_height >> 1;
636 if (s->pict_type == AV_PICTURE_TYPE_P) {
637 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
638 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
640 qx = (s->mb_x * 16) + (mx >> 2);
641 qy = (s->mb_y * 8) + (my >> 3);
646 mx -= 4 * (qx - width);
649 else if (qy > height + 1)
650 my -= 8 * (qy - height - 1);
653 if ((v->fcm == ILACE_FRAME) && fieldmv)
654 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
656 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
658 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
660 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
662 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
664 if (v->profile != PROFILE_ADVANCED) {
665 src_x = av_clip(src_x, -16, s->mb_width * 16);
666 src_y = av_clip(src_y, -16, s->mb_height * 16);
668 src_x = av_clip(src_x, -17, s->avctx->coded_width);
669 if (v->fcm == ILACE_FRAME) {
671 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
673 src_y = av_clip(src_y, -18, s->avctx->coded_height);
675 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
679 srcY += src_y * s->linesize + src_x;
680 if (v->field_mode && v->ref_field_type[dir])
681 srcY += s->current_picture_ptr->f->linesize[0];
683 if (fieldmv && !(src_y & 1))
685 if (fieldmv && (src_y & 1) && src_y < 4)
687 if (v->rangeredfrm || use_ic
688 || s->h_edge_pos < 13 || v_edge_pos < 23
689 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
690 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
691 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
692 /* check emulate edge stride and offset */
693 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
694 s->linesize, s->linesize,
695 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
696 src_x - s->mspel, src_y - (s->mspel << fieldmv),
697 s->h_edge_pos, v_edge_pos);
698 srcY = s->edge_emu_buffer;
699 /* if we deal with range reduction we need to scale source blocks */
700 if (v->rangeredfrm) {
705 for (j = 0; j < 9 + s->mspel * 2; j++) {
706 for (i = 0; i < 9 + s->mspel * 2; i++)
707 src[i] = ((src[i] - 128) >> 1) + 128;
708 src += s->linesize << fieldmv;
711 /* if we deal with intensity compensation we need to scale source blocks */
717 for (j = 0; j < 9 + s->mspel * 2; j++) {
718 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
719 for (i = 0; i < 9 + s->mspel * 2; i++)
720 src[i] = luty[f][src[i]];
721 src += s->linesize << fieldmv;
724 srcY += s->mspel * (1 + (s->linesize << fieldmv));
728 dxy = ((my & 3) << 2) | (mx & 3);
730 v->vc1dsp.avg_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
732 v->vc1dsp.put_vc1_mspel_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
733 } else { // hpel mc - always used for luma
734 dxy = (my & 2) | ((mx & 2) >> 1);
736 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
738 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
742 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
745 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
747 idx = ((a[3] != flag) << 3)
748 | ((a[2] != flag) << 2)
749 | ((a[1] != flag) << 1)
752 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
753 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
755 } else if (count[idx] == 1) {
758 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
759 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
762 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
763 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
766 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
767 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
770 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
771 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
774 } else if (count[idx] == 2) {
776 for (i = 0; i < 3; i++)
781 for (i = t1 + 1; i < 4; i++)
786 *tx = (mvx[t1] + mvx[t2]) / 2;
787 *ty = (mvy[t1] + mvy[t2]) / 2;
795 /** Do motion compensation for 4-MV macroblock - both chroma blocks
797 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
799 MpegEncContext *s = &v->s;
800 H264ChromaContext *h264chroma = &v->h264chroma;
801 uint8_t *srcU, *srcV;
802 int uvmx, uvmy, uvsrc_x, uvsrc_y;
803 int k, tx = 0, ty = 0;
804 int mvx[4], mvy[4], intra[4], mv_f[4];
806 int chroma_ref_type = v->cur_field_type;
807 int v_edge_pos = s->v_edge_pos >> v->field_mode;
808 uint8_t (*lutuv)[256];
811 if (!v->field_mode && !v->s.last_picture.f->data[0])
813 if (s->flags & CODEC_FLAG_GRAY)
816 for (k = 0; k < 4; k++) {
817 mvx[k] = s->mv[dir][k][0];
818 mvy[k] = s->mv[dir][k][1];
819 intra[k] = v->mb_type[0][s->block_index[k]];
821 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
824 /* calculate chroma MV vector from four luma MVs */
825 if (!v->field_mode || (v->field_mode && !v->numref)) {
826 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
827 chroma_ref_type = v->reffield;
829 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
830 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
831 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
832 return; //no need to do MC for intra blocks
836 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
838 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
840 chroma_ref_type = !v->cur_field_type;
842 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
844 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
845 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
846 uvmx = (tx + ((tx & 3) == 3)) >> 1;
847 uvmy = (ty + ((ty & 3) == 3)) >> 1;
849 v->luma_mv[s->mb_x][0] = uvmx;
850 v->luma_mv[s->mb_x][1] = uvmy;
853 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
854 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
856 // Field conversion bias
857 if (v->cur_field_type != chroma_ref_type)
858 uvmy += 2 - 4 * chroma_ref_type;
860 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
861 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
863 if (v->profile != PROFILE_ADVANCED) {
864 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
865 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
867 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
868 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
872 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
873 srcU = s->current_picture.f->data[1];
874 srcV = s->current_picture.f->data[2];
875 lutuv = v->curr_lutuv;
876 use_ic = *v->curr_use_ic;
878 srcU = s->last_picture.f->data[1];
879 srcV = s->last_picture.f->data[2];
880 lutuv = v->last_lutuv;
881 use_ic = v->last_use_ic;
884 srcU = s->next_picture.f->data[1];
885 srcV = s->next_picture.f->data[2];
886 lutuv = v->next_lutuv;
887 use_ic = v->next_use_ic;
891 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
895 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
896 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
899 if (chroma_ref_type) {
900 srcU += s->current_picture_ptr->f->linesize[1];
901 srcV += s->current_picture_ptr->f->linesize[2];
905 if (v->rangeredfrm || use_ic
906 || s->h_edge_pos < 18 || v_edge_pos < 18
907 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
908 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
909 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
910 s->uvlinesize, s->uvlinesize,
911 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
912 s->h_edge_pos >> 1, v_edge_pos >> 1);
913 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
914 s->uvlinesize, s->uvlinesize,
915 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
916 s->h_edge_pos >> 1, v_edge_pos >> 1);
917 srcU = s->edge_emu_buffer;
918 srcV = s->edge_emu_buffer + 16;
920 /* if we deal with range reduction we need to scale source blocks */
921 if (v->rangeredfrm) {
927 for (j = 0; j < 9; j++) {
928 for (i = 0; i < 9; i++) {
929 src[i] = ((src[i] - 128) >> 1) + 128;
930 src2[i] = ((src2[i] - 128) >> 1) + 128;
932 src += s->uvlinesize;
933 src2 += s->uvlinesize;
936 /* if we deal with intensity compensation we need to scale source blocks */
943 for (j = 0; j < 9; j++) {
944 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
945 for (i = 0; i < 9; i++) {
946 src[i] = lutuv[f][src[i]];
947 src2[i] = lutuv[f][src2[i]];
949 src += s->uvlinesize;
950 src2 += s->uvlinesize;
955 /* Chroma MC always uses qpel bilinear */
956 uvmx = (uvmx & 3) << 1;
957 uvmy = (uvmy & 3) << 1;
959 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
960 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
962 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
963 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
967 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
969 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
971 MpegEncContext *s = &v->s;
972 H264ChromaContext *h264chroma = &v->h264chroma;
973 uint8_t *srcU, *srcV;
974 int uvsrc_x, uvsrc_y;
975 int uvmx_field[4], uvmy_field[4];
977 int fieldmv = v->blk_mv_type[s->block_index[0]];
978 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
979 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
980 int v_edge_pos = s->v_edge_pos >> 1;
982 uint8_t (*lutuv)[256];
984 if (s->flags & CODEC_FLAG_GRAY)
987 for (i = 0; i < 4; i++) {
988 int d = i < 2 ? dir: dir2;
990 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
993 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
995 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
998 for (i = 0; i < 4; i++) {
999 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
1000 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1001 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1002 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1003 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1004 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1005 if (i < 2 ? dir : dir2) {
1006 srcU = s->next_picture.f->data[1];
1007 srcV = s->next_picture.f->data[2];
1008 lutuv = v->next_lutuv;
1009 use_ic = v->next_use_ic;
1011 srcU = s->last_picture.f->data[1];
1012 srcV = s->last_picture.f->data[2];
1013 lutuv = v->last_lutuv;
1014 use_ic = v->last_use_ic;
1018 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1019 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1020 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1021 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1023 if (fieldmv && !(uvsrc_y & 1))
1024 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1026 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1029 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1030 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1031 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1032 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
1033 s->uvlinesize, s->uvlinesize,
1034 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1035 s->h_edge_pos >> 1, v_edge_pos);
1036 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
1037 s->uvlinesize, s->uvlinesize,
1038 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1039 s->h_edge_pos >> 1, v_edge_pos);
1040 srcU = s->edge_emu_buffer;
1041 srcV = s->edge_emu_buffer + 16;
1043 /* if we deal with intensity compensation we need to scale source blocks */
1046 uint8_t *src, *src2;
1050 for (j = 0; j < 5; j++) {
1051 int f = (uvsrc_y + (j << fieldmv)) & 1;
1052 for (i = 0; i < 5; i++) {
1053 src[i] = lutuv[f][src[i]];
1054 src2[i] = lutuv[f][src2[i]];
1056 src += s->uvlinesize << fieldmv;
1057 src2 += s->uvlinesize << fieldmv;
1063 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1064 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1066 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]);
1067 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]);
1071 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1072 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1074 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]);
1075 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]);
1081 /***********************************************************************/
1083 * @name VC-1 Block-level functions
1084 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1090 * @brief Get macroblock-level quantizer scale
1092 #define GET_MQUANT() \
1093 if (v->dquantfrm) { \
1095 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1096 if (v->dqbilevel) { \
1097 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1099 mqdiff = get_bits(gb, 3); \
1101 mquant = v->pq + mqdiff; \
1103 mquant = get_bits(gb, 5); \
1106 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1107 edges = 1 << v->dqsbedge; \
1108 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1109 edges = (3 << v->dqsbedge) % 15; \
1110 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1112 if ((edges&1) && !s->mb_x) \
1113 mquant = v->altpq; \
1114 if ((edges&2) && s->first_slice_line) \
1115 mquant = v->altpq; \
1116 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1117 mquant = v->altpq; \
1118 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1119 mquant = v->altpq; \
1120 if (!mquant || mquant > 31) { \
1121 av_log(v->s.avctx, AV_LOG_ERROR, \
1122 "Overriding invalid mquant %d\n", mquant); \
1128 * @def GET_MVDATA(_dmv_x, _dmv_y)
1129 * @brief Get MV differentials
1130 * @see MVDATA decoding from 8.3.5.2, p(1)20
1131 * @param _dmv_x Horizontal differential for decoded MV
1132 * @param _dmv_y Vertical differential for decoded MV
1134 #define GET_MVDATA(_dmv_x, _dmv_y) \
1135 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1136 VC1_MV_DIFF_VLC_BITS, 2); \
1138 mb_has_coeffs = 1; \
1141 mb_has_coeffs = 0; \
1144 _dmv_x = _dmv_y = 0; \
1145 } else if (index == 35) { \
1146 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1147 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1148 } else if (index == 36) { \
1153 index1 = index % 6; \
1154 if (!s->quarter_sample && index1 == 5) val = 1; \
1156 if (size_table[index1] - val > 0) \
1157 val = get_bits(gb, size_table[index1] - val); \
1159 sign = 0 - (val&1); \
1160 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1162 index1 = index / 6; \
1163 if (!s->quarter_sample && index1 == 5) val = 1; \
1165 if (size_table[index1] - val > 0) \
1166 val = get_bits(gb, size_table[index1] - val); \
1168 sign = 0 - (val & 1); \
1169 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1172 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1173 int *dmv_y, int *pred_flag)
1176 int extend_x = 0, extend_y = 0;
1177 GetBitContext *gb = &v->s.gb;
1180 const int* offs_tab;
1183 bits = VC1_2REF_MVDATA_VLC_BITS;
1186 bits = VC1_1REF_MVDATA_VLC_BITS;
1189 switch (v->dmvrange) {
1197 extend_x = extend_y = 1;
1200 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1202 *dmv_x = get_bits(gb, v->k_x);
1203 *dmv_y = get_bits(gb, v->k_y);
1206 *pred_flag = *dmv_y & 1;
1207 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1209 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1214 av_assert0(index < esc);
1216 offs_tab = offset_table2;
1218 offs_tab = offset_table1;
1219 index1 = (index + 1) % 9;
1221 val = get_bits(gb, index1 + extend_x);
1222 sign = 0 -(val & 1);
1223 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1227 offs_tab = offset_table2;
1229 offs_tab = offset_table1;
1230 index1 = (index + 1) / 9;
1231 if (index1 > v->numref) {
1232 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1233 sign = 0 - (val & 1);
1234 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1237 if (v->numref && pred_flag)
1238 *pred_flag = index1 & 1;
1242 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1244 int scaledvalue, refdist;
1245 int scalesame1, scalesame2;
1246 int scalezone1_x, zone1offset_x;
1247 int table_index = dir ^ v->second_field;
1249 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1250 refdist = v->refdist;
1252 refdist = dir ? v->brfd : v->frfd;
1255 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1256 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1257 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1258 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1263 if (FFABS(n) < scalezone1_x)
1264 scaledvalue = (n * scalesame1) >> 8;
1267 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1269 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1272 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1275 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1277 int scaledvalue, refdist;
1278 int scalesame1, scalesame2;
1279 int scalezone1_y, zone1offset_y;
1280 int table_index = dir ^ v->second_field;
1282 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1283 refdist = v->refdist;
1285 refdist = dir ? v->brfd : v->frfd;
1288 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1289 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1290 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1291 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1296 if (FFABS(n) < scalezone1_y)
1297 scaledvalue = (n * scalesame1) >> 8;
1300 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1302 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1306 if (v->cur_field_type && !v->ref_field_type[dir])
1307 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1309 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1312 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1314 int scalezone1_x, zone1offset_x;
1315 int scaleopp1, scaleopp2, brfd;
1318 brfd = FFMIN(v->brfd, 3);
1319 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1320 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1321 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1322 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1327 if (FFABS(n) < scalezone1_x)
1328 scaledvalue = (n * scaleopp1) >> 8;
1331 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1333 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1336 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1339 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1341 int scalezone1_y, zone1offset_y;
1342 int scaleopp1, scaleopp2, brfd;
1345 brfd = FFMIN(v->brfd, 3);
1346 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1347 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1348 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1349 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1354 if (FFABS(n) < scalezone1_y)
1355 scaledvalue = (n * scaleopp1) >> 8;
1358 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1360 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1363 if (v->cur_field_type && !v->ref_field_type[dir]) {
1364 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1366 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1370 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1373 int brfd, scalesame;
1374 int hpel = 1 - v->s.quarter_sample;
1377 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1379 n = scaleforsame_y(v, i, n, dir) << hpel;
1381 n = scaleforsame_x(v, n, dir) << hpel;
1384 brfd = FFMIN(v->brfd, 3);
1385 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1387 n = (n * scalesame >> 8) << hpel;
1391 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1394 int refdist, scaleopp;
1395 int hpel = 1 - v->s.quarter_sample;
1398 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1400 n = scaleforopp_y(v, n, dir) << hpel;
1402 n = scaleforopp_x(v, n) << hpel;
1405 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1406 refdist = FFMIN(v->refdist, 3);
1408 refdist = dir ? v->brfd : v->frfd;
1409 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1411 n = (n * scaleopp >> 8) << hpel;
1415 /** Predict and set motion vector
1417 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1418 int mv1, int r_x, int r_y, uint8_t* is_intra,
1419 int pred_flag, int dir)
1421 MpegEncContext *s = &v->s;
1422 int xy, wrap, off = 0;
1426 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1427 int opposite, a_f, b_f, c_f;
1428 int16_t field_predA[2];
1429 int16_t field_predB[2];
1430 int16_t field_predC[2];
1431 int a_valid, b_valid, c_valid;
1432 int hybridmv_thresh, y_bias = 0;
1434 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1435 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1439 /* scale MV difference to be quad-pel */
1440 dmv_x <<= 1 - s->quarter_sample;
1441 dmv_y <<= 1 - s->quarter_sample;
1443 wrap = s->b8_stride;
1444 xy = s->block_index[n];
1447 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1448 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1449 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1450 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1451 if (mv1) { /* duplicate motion data for 1-MV block */
1452 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1453 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1454 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1455 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1456 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1457 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1458 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1459 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1460 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1461 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1462 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1463 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1464 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1469 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1470 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1472 if (v->field_mode && mixedmv_pic)
1473 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1475 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1477 //in 4-MV mode different blocks have different B predictor position
1480 off = (s->mb_x > 0) ? -1 : 1;
1483 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1492 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1494 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1495 b_valid = a_valid && (s->mb_width > 1);
1496 c_valid = s->mb_x || (n == 1 || n == 3);
1497 if (v->field_mode) {
1498 a_valid = a_valid && !is_intra[xy - wrap];
1499 b_valid = b_valid && !is_intra[xy - wrap + off];
1500 c_valid = c_valid && !is_intra[xy - 1];
1504 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1505 num_oppfield += a_f;
1506 num_samefield += 1 - a_f;
1507 field_predA[0] = A[0];
1508 field_predA[1] = A[1];
1510 field_predA[0] = field_predA[1] = 0;
1514 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1515 num_oppfield += b_f;
1516 num_samefield += 1 - b_f;
1517 field_predB[0] = B[0];
1518 field_predB[1] = B[1];
1520 field_predB[0] = field_predB[1] = 0;
1524 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1525 num_oppfield += c_f;
1526 num_samefield += 1 - c_f;
1527 field_predC[0] = C[0];
1528 field_predC[1] = C[1];
1530 field_predC[0] = field_predC[1] = 0;
1534 if (v->field_mode) {
1536 // REFFIELD determines if the last field or the second-last field is
1537 // to be used as reference
1538 opposite = 1 - v->reffield;
1540 if (num_samefield <= num_oppfield)
1541 opposite = 1 - pred_flag;
1543 opposite = pred_flag;
1548 if (a_valid && !a_f) {
1549 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1550 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1552 if (b_valid && !b_f) {
1553 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1554 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1556 if (c_valid && !c_f) {
1557 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1558 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1560 v->mv_f[dir][xy + v->blocks_off] = 1;
1561 v->ref_field_type[dir] = !v->cur_field_type;
1563 if (a_valid && a_f) {
1564 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1565 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1567 if (b_valid && b_f) {
1568 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1569 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1571 if (c_valid && c_f) {
1572 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1573 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1575 v->mv_f[dir][xy + v->blocks_off] = 0;
1576 v->ref_field_type[dir] = v->cur_field_type;
1580 px = field_predA[0];
1581 py = field_predA[1];
1582 } else if (c_valid) {
1583 px = field_predC[0];
1584 py = field_predC[1];
1585 } else if (b_valid) {
1586 px = field_predB[0];
1587 py = field_predB[1];
1593 if (num_samefield + num_oppfield > 1) {
1594 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1595 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1598 /* Pullback MV as specified in 8.3.5.3.4 */
1599 if (!v->field_mode) {
1601 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1602 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1603 X = (s->mb_width << 6) - 4;
1604 Y = (s->mb_height << 6) - 4;
1606 if (qx + px < -60) px = -60 - qx;
1607 if (qy + py < -60) py = -60 - qy;
1609 if (qx + px < -28) px = -28 - qx;
1610 if (qy + py < -28) py = -28 - qy;
1612 if (qx + px > X) px = X - qx;
1613 if (qy + py > Y) py = Y - qy;
1616 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1617 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1618 hybridmv_thresh = 32;
1619 if (a_valid && c_valid) {
1620 if (is_intra[xy - wrap])
1621 sum = FFABS(px) + FFABS(py);
1623 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1624 if (sum > hybridmv_thresh) {
1625 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1626 px = field_predA[0];
1627 py = field_predA[1];
1629 px = field_predC[0];
1630 py = field_predC[1];
1633 if (is_intra[xy - 1])
1634 sum = FFABS(px) + FFABS(py);
1636 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1637 if (sum > hybridmv_thresh) {
1638 if (get_bits1(&s->gb)) {
1639 px = field_predA[0];
1640 py = field_predA[1];
1642 px = field_predC[0];
1643 py = field_predC[1];
1650 if (v->field_mode && v->numref)
1652 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1654 /* store MV using signed modulus of MV range defined in 4.11 */
1655 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;
1656 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;
1657 if (mv1) { /* duplicate motion data for 1-MV block */
1658 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1659 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1660 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1661 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1662 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1663 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1664 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1665 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];
1669 /** Predict and set motion vector for interlaced frame picture MBs
1671 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1672 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1674 MpegEncContext *s = &v->s;
1675 int xy, wrap, off = 0;
1676 int A[2], B[2], C[2];
1678 int a_valid = 0, b_valid = 0, c_valid = 0;
1679 int field_a, field_b, field_c; // 0: same, 1: opposit
1680 int total_valid, num_samefield, num_oppfield;
1681 int pos_c, pos_b, n_adj;
1683 wrap = s->b8_stride;
1684 xy = s->block_index[n];
1687 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1688 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1689 s->current_picture.motion_val[1][xy][0] = 0;
1690 s->current_picture.motion_val[1][xy][1] = 0;
1691 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1692 s->current_picture.motion_val[0][xy + 1][0] = 0;
1693 s->current_picture.motion_val[0][xy + 1][1] = 0;
1694 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1695 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1696 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1697 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1698 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1699 s->current_picture.motion_val[1][xy + 1][0] = 0;
1700 s->current_picture.motion_val[1][xy + 1][1] = 0;
1701 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1702 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1703 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1704 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1709 off = ((n == 0) || (n == 1)) ? 1 : -1;
1711 if (s->mb_x || (n == 1) || (n == 3)) {
1712 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1713 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1714 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1715 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1717 } else { // current block has frame mv and cand. has field MV (so average)
1718 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1719 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1720 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1721 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1724 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1730 /* Predict B and C */
1731 B[0] = B[1] = C[0] = C[1] = 0;
1732 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1733 if (!s->first_slice_line) {
1734 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1737 pos_b = s->block_index[n_adj] - 2 * wrap;
1738 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1739 n_adj = (n & 2) | (n & 1);
1741 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1742 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1743 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1744 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1745 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1748 if (s->mb_width > 1) {
1749 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1752 pos_c = s->block_index[2] - 2 * wrap + 2;
1753 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1756 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1757 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1758 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1759 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1760 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1762 if (s->mb_x == s->mb_width - 1) {
1763 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1766 pos_c = s->block_index[3] - 2 * wrap - 2;
1767 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1770 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1771 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1772 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1773 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1774 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1783 pos_b = s->block_index[1];
1785 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1786 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1787 pos_c = s->block_index[0];
1789 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1790 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1793 total_valid = a_valid + b_valid + c_valid;
1794 // check if predictor A is out of bounds
1795 if (!s->mb_x && !(n == 1 || n == 3)) {
1798 // check if predictor B is out of bounds
1799 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1800 B[0] = B[1] = C[0] = C[1] = 0;
1802 if (!v->blk_mv_type[xy]) {
1803 if (s->mb_width == 1) {
1807 if (total_valid >= 2) {
1808 px = mid_pred(A[0], B[0], C[0]);
1809 py = mid_pred(A[1], B[1], C[1]);
1810 } else if (total_valid) {
1811 if (a_valid) { px = A[0]; py = A[1]; }
1812 else if (b_valid) { px = B[0]; py = B[1]; }
1813 else { px = C[0]; py = C[1]; }
1818 field_a = (A[1] & 4) ? 1 : 0;
1822 field_b = (B[1] & 4) ? 1 : 0;
1826 field_c = (C[1] & 4) ? 1 : 0;
1830 num_oppfield = field_a + field_b + field_c;
1831 num_samefield = total_valid - num_oppfield;
1832 if (total_valid == 3) {
1833 if ((num_samefield == 3) || (num_oppfield == 3)) {
1834 px = mid_pred(A[0], B[0], C[0]);
1835 py = mid_pred(A[1], B[1], C[1]);
1836 } else if (num_samefield >= num_oppfield) {
1837 /* take one MV from same field set depending on priority
1838 the check for B may not be necessary */
1839 px = !field_a ? A[0] : B[0];
1840 py = !field_a ? A[1] : B[1];
1842 px = field_a ? A[0] : B[0];
1843 py = field_a ? A[1] : B[1];
1845 } else if (total_valid == 2) {
1846 if (num_samefield >= num_oppfield) {
1847 if (!field_a && a_valid) {
1850 } else if (!field_b && b_valid) {
1853 } else /*if (c_valid)*/ {
1854 av_assert1(c_valid);
1857 } /*else px = py = 0;*/
1859 if (field_a && a_valid) {
1862 } else /*if (field_b && b_valid)*/ {
1863 av_assert1(field_b && b_valid);
1866 } /*else if (c_valid) {
1871 } else if (total_valid == 1) {
1872 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1873 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1877 /* store MV using signed modulus of MV range defined in 4.11 */
1878 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1879 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1880 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1881 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1882 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1883 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1884 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1885 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1886 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1887 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1888 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1889 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1890 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1891 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1895 /** Motion compensation for direct or interpolated blocks in B-frames
1897 static void vc1_interp_mc(VC1Context *v)
1899 MpegEncContext *s = &v->s;
1900 H264ChromaContext *h264chroma = &v->h264chroma;
1901 uint8_t *srcY, *srcU, *srcV;
1902 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1904 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1905 int use_ic = v->next_use_ic;
1907 if (!v->field_mode && !v->s.next_picture.f->data[0])
1910 mx = s->mv[1][0][0];
1911 my = s->mv[1][0][1];
1912 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1913 uvmy = (my + ((my & 3) == 3)) >> 1;
1914 if (v->field_mode) {
1915 if (v->cur_field_type != v->ref_field_type[1]) {
1916 my = my - 2 + 4 * v->cur_field_type;
1917 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1921 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1922 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1924 srcY = s->next_picture.f->data[0];
1925 srcU = s->next_picture.f->data[1];
1926 srcV = s->next_picture.f->data[2];
1928 src_x = s->mb_x * 16 + (mx >> 2);
1929 src_y = s->mb_y * 16 + (my >> 2);
1930 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1931 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1933 if (v->profile != PROFILE_ADVANCED) {
1934 src_x = av_clip( src_x, -16, s->mb_width * 16);
1935 src_y = av_clip( src_y, -16, s->mb_height * 16);
1936 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1937 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1939 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1940 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1941 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1942 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1945 srcY += src_y * s->linesize + src_x;
1946 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1947 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1949 if (v->field_mode && v->ref_field_type[1]) {
1950 srcY += s->current_picture_ptr->f->linesize[0];
1951 srcU += s->current_picture_ptr->f->linesize[1];
1952 srcV += s->current_picture_ptr->f->linesize[2];
1955 /* for grayscale we should not try to read from unknown area */
1956 if (s->flags & CODEC_FLAG_GRAY) {
1957 srcU = s->edge_emu_buffer + 18 * s->linesize;
1958 srcV = s->edge_emu_buffer + 18 * s->linesize;
1961 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1962 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1963 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1964 uint8_t *ubuf = s->edge_emu_buffer + 19 * s->linesize;
1965 uint8_t *vbuf = ubuf + 9 * s->uvlinesize;
1967 srcY -= s->mspel * (1 + s->linesize);
1968 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
1969 s->linesize, s->linesize,
1970 17 + s->mspel * 2, 17 + s->mspel * 2,
1971 src_x - s->mspel, src_y - s->mspel,
1972 s->h_edge_pos, v_edge_pos);
1973 srcY = s->edge_emu_buffer;
1974 s->vdsp.emulated_edge_mc(ubuf, srcU,
1975 s->uvlinesize, s->uvlinesize,
1978 s->h_edge_pos >> 1, v_edge_pos >> 1);
1979 s->vdsp.emulated_edge_mc(vbuf, srcV,
1980 s->uvlinesize, s->uvlinesize,
1983 s->h_edge_pos >> 1, v_edge_pos >> 1);
1986 /* if we deal with range reduction we need to scale source blocks */
1987 if (v->rangeredfrm) {
1989 uint8_t *src, *src2;
1992 for (j = 0; j < 17 + s->mspel * 2; j++) {
1993 for (i = 0; i < 17 + s->mspel * 2; i++)
1994 src[i] = ((src[i] - 128) >> 1) + 128;
1999 for (j = 0; j < 9; j++) {
2000 for (i = 0; i < 9; i++) {
2001 src[i] = ((src[i] - 128) >> 1) + 128;
2002 src2[i] = ((src2[i] - 128) >> 1) + 128;
2004 src += s->uvlinesize;
2005 src2 += s->uvlinesize;
2010 uint8_t (*luty )[256] = v->next_luty;
2011 uint8_t (*lutuv)[256] = v->next_lutuv;
2013 uint8_t *src, *src2;
2016 for (j = 0; j < 17 + s->mspel * 2; j++) {
2017 int f = v->field_mode ? v->ref_field_type[1] : ((j+src_y - s->mspel) & 1);
2018 for (i = 0; i < 17 + s->mspel * 2; i++)
2019 src[i] = luty[f][src[i]];
2024 for (j = 0; j < 9; j++) {
2025 int f = v->field_mode ? v->ref_field_type[1] : ((j+uvsrc_y) & 1);
2026 for (i = 0; i < 9; i++) {
2027 src[i] = lutuv[f][src[i]];
2028 src2[i] = lutuv[f][src2[i]];
2030 src += s->uvlinesize;
2031 src2 += s->uvlinesize;
2034 srcY += s->mspel * (1 + s->linesize);
2041 dxy = ((my & 3) << 2) | (mx & 3);
2042 v->vc1dsp.avg_vc1_mspel_pixels_tab[0][dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2044 dxy = (my & 2) | ((mx & 2) >> 1);
2047 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2049 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2052 if (s->flags & CODEC_FLAG_GRAY) return;
2053 /* Chroma MC always uses qpel blilinear */
2054 uvmx = (uvmx & 3) << 1;
2055 uvmy = (uvmy & 3) << 1;
2057 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2058 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2060 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2061 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2065 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2069 #if B_FRACTION_DEN==256
2073 return 2 * ((value * n + 255) >> 9);
2074 return (value * n + 128) >> 8;
2077 n -= B_FRACTION_DEN;
2079 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2080 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2084 /** Reconstruct motion vector for B-frame and do motion compensation
2086 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2087 int direct, int mode)
2094 if (mode == BMV_TYPE_INTERPOLATED) {
2100 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2103 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2104 int direct, int mvtype)
2106 MpegEncContext *s = &v->s;
2107 int xy, wrap, off = 0;
2112 const uint8_t *is_intra = v->mb_type[0];
2114 av_assert0(!v->field_mode);
2118 /* scale MV difference to be quad-pel */
2119 dmv_x[0] <<= 1 - s->quarter_sample;
2120 dmv_y[0] <<= 1 - s->quarter_sample;
2121 dmv_x[1] <<= 1 - s->quarter_sample;
2122 dmv_y[1] <<= 1 - s->quarter_sample;
2124 wrap = s->b8_stride;
2125 xy = s->block_index[0];
2128 s->current_picture.motion_val[0][xy][0] =
2129 s->current_picture.motion_val[0][xy][1] =
2130 s->current_picture.motion_val[1][xy][0] =
2131 s->current_picture.motion_val[1][xy][1] = 0;
2134 if (direct && s->next_picture_ptr->field_picture)
2135 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2137 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2138 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2139 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2140 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2142 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2143 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));
2144 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));
2145 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));
2146 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));
2148 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2149 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2150 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2151 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2155 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2156 C = s->current_picture.motion_val[0][xy - 2];
2157 A = s->current_picture.motion_val[0][xy - wrap * 2];
2158 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2159 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2161 if (!s->mb_x) C[0] = C[1] = 0;
2162 if (!s->first_slice_line) { // predictor A is not out of bounds
2163 if (s->mb_width == 1) {
2167 px = mid_pred(A[0], B[0], C[0]);
2168 py = mid_pred(A[1], B[1], C[1]);
2170 } else if (s->mb_x) { // predictor C is not out of bounds
2176 /* Pullback MV as specified in 8.3.5.3.4 */
2179 if (v->profile < PROFILE_ADVANCED) {
2180 qx = (s->mb_x << 5);
2181 qy = (s->mb_y << 5);
2182 X = (s->mb_width << 5) - 4;
2183 Y = (s->mb_height << 5) - 4;
2184 if (qx + px < -28) px = -28 - qx;
2185 if (qy + py < -28) py = -28 - qy;
2186 if (qx + px > X) px = X - qx;
2187 if (qy + py > Y) py = Y - qy;
2189 qx = (s->mb_x << 6);
2190 qy = (s->mb_y << 6);
2191 X = (s->mb_width << 6) - 4;
2192 Y = (s->mb_height << 6) - 4;
2193 if (qx + px < -60) px = -60 - qx;
2194 if (qy + py < -60) py = -60 - qy;
2195 if (qx + px > X) px = X - qx;
2196 if (qy + py > Y) py = Y - qy;
2199 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2200 if (0 && !s->first_slice_line && s->mb_x) {
2201 if (is_intra[xy - wrap])
2202 sum = FFABS(px) + FFABS(py);
2204 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2206 if (get_bits1(&s->gb)) {
2214 if (is_intra[xy - 2])
2215 sum = FFABS(px) + FFABS(py);
2217 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2219 if (get_bits1(&s->gb)) {
2229 /* store MV using signed modulus of MV range defined in 4.11 */
2230 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2231 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2233 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2234 C = s->current_picture.motion_val[1][xy - 2];
2235 A = s->current_picture.motion_val[1][xy - wrap * 2];
2236 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2237 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2241 if (!s->first_slice_line) { // predictor A is not out of bounds
2242 if (s->mb_width == 1) {
2246 px = mid_pred(A[0], B[0], C[0]);
2247 py = mid_pred(A[1], B[1], C[1]);
2249 } else if (s->mb_x) { // predictor C is not out of bounds
2255 /* Pullback MV as specified in 8.3.5.3.4 */
2258 if (v->profile < PROFILE_ADVANCED) {
2259 qx = (s->mb_x << 5);
2260 qy = (s->mb_y << 5);
2261 X = (s->mb_width << 5) - 4;
2262 Y = (s->mb_height << 5) - 4;
2263 if (qx + px < -28) px = -28 - qx;
2264 if (qy + py < -28) py = -28 - qy;
2265 if (qx + px > X) px = X - qx;
2266 if (qy + py > Y) py = Y - qy;
2268 qx = (s->mb_x << 6);
2269 qy = (s->mb_y << 6);
2270 X = (s->mb_width << 6) - 4;
2271 Y = (s->mb_height << 6) - 4;
2272 if (qx + px < -60) px = -60 - qx;
2273 if (qy + py < -60) py = -60 - qy;
2274 if (qx + px > X) px = X - qx;
2275 if (qy + py > Y) py = Y - qy;
2278 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2279 if (0 && !s->first_slice_line && s->mb_x) {
2280 if (is_intra[xy - wrap])
2281 sum = FFABS(px) + FFABS(py);
2283 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2285 if (get_bits1(&s->gb)) {
2293 if (is_intra[xy - 2])
2294 sum = FFABS(px) + FFABS(py);
2296 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2298 if (get_bits1(&s->gb)) {
2308 /* store MV using signed modulus of MV range defined in 4.11 */
2310 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2311 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2313 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2314 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2315 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2316 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2319 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2321 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2322 MpegEncContext *s = &v->s;
2323 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2325 if (v->bmvtype == BMV_TYPE_DIRECT) {
2326 int total_opp, k, f;
2327 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2328 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2329 v->bfraction, 0, s->quarter_sample);
2330 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2331 v->bfraction, 0, s->quarter_sample);
2332 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2333 v->bfraction, 1, s->quarter_sample);
2334 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2335 v->bfraction, 1, s->quarter_sample);
2337 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2338 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2339 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2340 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2341 f = (total_opp > 2) ? 1 : 0;
2343 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2344 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2347 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2348 for (k = 0; k < 4; k++) {
2349 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2350 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2351 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2352 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2353 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2354 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2358 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2359 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);
2360 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);
2363 if (dir) { // backward
2364 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);
2365 if (n == 3 || mv1) {
2366 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2369 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);
2370 if (n == 3 || mv1) {
2371 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2376 /** Get predicted DC value for I-frames only
2377 * prediction dir: left=0, top=1
2378 * @param s MpegEncContext
2379 * @param overlap flag indicating that overlap filtering is used
2380 * @param pq integer part of picture quantizer
2381 * @param[in] n block index in the current MB
2382 * @param dc_val_ptr Pointer to DC predictor
2383 * @param dir_ptr Prediction direction for use in AC prediction
2385 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2386 int16_t **dc_val_ptr, int *dir_ptr)
2388 int a, b, c, wrap, pred, scale;
2390 static const uint16_t dcpred[32] = {
2391 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2392 114, 102, 93, 85, 79, 73, 68, 64,
2393 60, 57, 54, 51, 49, 47, 45, 43,
2394 41, 39, 38, 37, 35, 34, 33
2397 /* find prediction - wmv3_dc_scale always used here in fact */
2398 if (n < 4) scale = s->y_dc_scale;
2399 else scale = s->c_dc_scale;
2401 wrap = s->block_wrap[n];
2402 dc_val = s->dc_val[0] + s->block_index[n];
2408 b = dc_val[ - 1 - wrap];
2409 a = dc_val[ - wrap];
2411 if (pq < 9 || !overlap) {
2412 /* Set outer values */
2413 if (s->first_slice_line && (n != 2 && n != 3))
2414 b = a = dcpred[scale];
2415 if (s->mb_x == 0 && (n != 1 && n != 3))
2416 b = c = dcpred[scale];
2418 /* Set outer values */
2419 if (s->first_slice_line && (n != 2 && n != 3))
2421 if (s->mb_x == 0 && (n != 1 && n != 3))
2425 if (abs(a - b) <= abs(b - c)) {
2427 *dir_ptr = 1; // left
2430 *dir_ptr = 0; // top
2433 /* update predictor */
2434 *dc_val_ptr = &dc_val[0];
2439 /** Get predicted DC value
2440 * prediction dir: left=0, top=1
2441 * @param s MpegEncContext
2442 * @param overlap flag indicating that overlap filtering is used
2443 * @param pq integer part of picture quantizer
2444 * @param[in] n block index in the current MB
2445 * @param a_avail flag indicating top block availability
2446 * @param c_avail flag indicating left block availability
2447 * @param dc_val_ptr Pointer to DC predictor
2448 * @param dir_ptr Prediction direction for use in AC prediction
2450 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2451 int a_avail, int c_avail,
2452 int16_t **dc_val_ptr, int *dir_ptr)
2454 int a, b, c, wrap, pred;
2456 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2460 wrap = s->block_wrap[n];
2461 dc_val = s->dc_val[0] + s->block_index[n];
2467 b = dc_val[ - 1 - wrap];
2468 a = dc_val[ - wrap];
2469 /* scale predictors if needed */
2470 q1 = s->current_picture.qscale_table[mb_pos];
2471 dqscale_index = s->y_dc_scale_table[q1] - 1;
2472 if (dqscale_index < 0)
2474 if (c_avail && (n != 1 && n != 3)) {
2475 q2 = s->current_picture.qscale_table[mb_pos - 1];
2477 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2479 if (a_avail && (n != 2 && n != 3)) {
2480 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2482 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2484 if (a_avail && c_avail && (n != 3)) {
2489 off -= s->mb_stride;
2490 q2 = s->current_picture.qscale_table[off];
2492 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2495 if (a_avail && c_avail) {
2496 if (abs(a - b) <= abs(b - c)) {
2498 *dir_ptr = 1; // left
2501 *dir_ptr = 0; // top
2503 } else if (a_avail) {
2505 *dir_ptr = 0; // top
2506 } else if (c_avail) {
2508 *dir_ptr = 1; // left
2511 *dir_ptr = 1; // left
2514 /* update predictor */
2515 *dc_val_ptr = &dc_val[0];
2519 /** @} */ // Block group
2522 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2523 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2527 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2528 uint8_t **coded_block_ptr)
2530 int xy, wrap, pred, a, b, c;
2532 xy = s->block_index[n];
2533 wrap = s->b8_stride;
2538 a = s->coded_block[xy - 1 ];
2539 b = s->coded_block[xy - 1 - wrap];
2540 c = s->coded_block[xy - wrap];
2549 *coded_block_ptr = &s->coded_block[xy];
2555 * Decode one AC coefficient
2556 * @param v The VC1 context
2557 * @param last Last coefficient
2558 * @param skip How much zero coefficients to skip
2559 * @param value Decoded AC coefficient value
2560 * @param codingset set of VLC to decode data
2563 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2564 int *value, int codingset)
2566 GetBitContext *gb = &v->s.gb;
2567 int index, escape, run = 0, level = 0, lst = 0;
2569 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2570 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2571 run = vc1_index_decode_table[codingset][index][0];
2572 level = vc1_index_decode_table[codingset][index][1];
2573 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2577 escape = decode210(gb);
2579 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2580 run = vc1_index_decode_table[codingset][index][0];
2581 level = vc1_index_decode_table[codingset][index][1];
2582 lst = index >= vc1_last_decode_table[codingset];
2585 level += vc1_last_delta_level_table[codingset][run];
2587 level += vc1_delta_level_table[codingset][run];
2590 run += vc1_last_delta_run_table[codingset][level] + 1;
2592 run += vc1_delta_run_table[codingset][level] + 1;
2598 lst = get_bits1(gb);
2599 if (v->s.esc3_level_length == 0) {
2600 if (v->pq < 8 || v->dquantfrm) { // table 59
2601 v->s.esc3_level_length = get_bits(gb, 3);
2602 if (!v->s.esc3_level_length)
2603 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2604 } else { // table 60
2605 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2607 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2609 run = get_bits(gb, v->s.esc3_run_length);
2610 sign = get_bits1(gb);
2611 level = get_bits(gb, v->s.esc3_level_length);
2622 /** Decode intra block in intra frames - should be faster than decode_intra_block
2623 * @param v VC1Context
2624 * @param block block to decode
2625 * @param[in] n subblock index
2626 * @param coded are AC coeffs present or not
2627 * @param codingset set of VLC to decode data
2629 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2630 int coded, int codingset)
2632 GetBitContext *gb = &v->s.gb;
2633 MpegEncContext *s = &v->s;
2634 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2637 int16_t *ac_val, *ac_val2;
2640 /* Get DC differential */
2642 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2644 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2647 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2651 if (dcdiff == 119 /* ESC index value */) {
2652 /* TODO: Optimize */
2653 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2654 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2655 else dcdiff = get_bits(gb, 8);
2658 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2659 else if (v->pq == 2)
2660 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2667 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2670 /* Store the quantized DC coeff, used for prediction */
2672 block[0] = dcdiff * s->y_dc_scale;
2674 block[0] = dcdiff * s->c_dc_scale;
2685 int last = 0, skip, value;
2686 const uint8_t *zz_table;
2690 scale = v->pq * 2 + v->halfpq;
2694 zz_table = v->zz_8x8[2];
2696 zz_table = v->zz_8x8[3];
2698 zz_table = v->zz_8x8[1];
2700 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2702 if (dc_pred_dir) // left
2705 ac_val -= 16 * s->block_wrap[n];
2708 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2712 block[zz_table[i++]] = value;
2715 /* apply AC prediction if needed */
2717 if (dc_pred_dir) { // left
2718 for (k = 1; k < 8; k++)
2719 block[k << v->left_blk_sh] += ac_val[k];
2721 for (k = 1; k < 8; k++)
2722 block[k << v->top_blk_sh] += ac_val[k + 8];
2725 /* save AC coeffs for further prediction */
2726 for (k = 1; k < 8; k++) {
2727 ac_val2[k] = block[k << v->left_blk_sh];
2728 ac_val2[k + 8] = block[k << v->top_blk_sh];
2731 /* scale AC coeffs */
2732 for (k = 1; k < 64; k++)
2736 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2739 if (s->ac_pred) i = 63;
2745 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2749 scale = v->pq * 2 + v->halfpq;
2750 memset(ac_val2, 0, 16 * 2);
2751 if (dc_pred_dir) { // left
2754 memcpy(ac_val2, ac_val, 8 * 2);
2756 ac_val -= 16 * s->block_wrap[n];
2758 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2761 /* apply AC prediction if needed */
2763 if (dc_pred_dir) { //left
2764 for (k = 1; k < 8; k++) {
2765 block[k << v->left_blk_sh] = ac_val[k] * scale;
2766 if (!v->pquantizer && block[k << v->left_blk_sh])
2767 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2770 for (k = 1; k < 8; k++) {
2771 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2772 if (!v->pquantizer && block[k << v->top_blk_sh])
2773 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2779 s->block_last_index[n] = i;
2784 /** Decode intra block in intra frames - should be faster than decode_intra_block
2785 * @param v VC1Context
2786 * @param block block to decode
2787 * @param[in] n subblock number
2788 * @param coded are AC coeffs present or not
2789 * @param codingset set of VLC to decode data
2790 * @param mquant quantizer value for this macroblock
2792 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2793 int coded, int codingset, int mquant)
2795 GetBitContext *gb = &v->s.gb;
2796 MpegEncContext *s = &v->s;
2797 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2799 int16_t *dc_val = NULL;
2800 int16_t *ac_val, *ac_val2;
2802 int a_avail = v->a_avail, c_avail = v->c_avail;
2803 int use_pred = s->ac_pred;
2806 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2808 /* Get DC differential */
2810 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2812 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2815 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2819 if (dcdiff == 119 /* ESC index value */) {
2820 /* TODO: Optimize */
2821 if (mquant == 1) dcdiff = get_bits(gb, 10);
2822 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2823 else dcdiff = get_bits(gb, 8);
2826 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2827 else if (mquant == 2)
2828 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2835 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2838 /* Store the quantized DC coeff, used for prediction */
2840 block[0] = dcdiff * s->y_dc_scale;
2842 block[0] = dcdiff * s->c_dc_scale;
2848 /* check if AC is needed at all */
2849 if (!a_avail && !c_avail)
2851 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2854 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2856 if (dc_pred_dir) // left
2859 ac_val -= 16 * s->block_wrap[n];
2861 q1 = s->current_picture.qscale_table[mb_pos];
2862 if ( dc_pred_dir && c_avail && mb_pos)
2863 q2 = s->current_picture.qscale_table[mb_pos - 1];
2864 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2865 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2866 if ( dc_pred_dir && n == 1)
2868 if (!dc_pred_dir && n == 2)
2874 int last = 0, skip, value;
2875 const uint8_t *zz_table;
2879 if (!use_pred && v->fcm == ILACE_FRAME) {
2880 zz_table = v->zzi_8x8;
2882 if (!dc_pred_dir) // top
2883 zz_table = v->zz_8x8[2];
2885 zz_table = v->zz_8x8[3];
2888 if (v->fcm != ILACE_FRAME)
2889 zz_table = v->zz_8x8[1];
2891 zz_table = v->zzi_8x8;
2895 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2899 block[zz_table[i++]] = value;
2902 /* apply AC prediction if needed */
2904 /* scale predictors if needed*/
2905 if (q2 && q1 != q2) {
2906 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2907 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2910 return AVERROR_INVALIDDATA;
2911 if (dc_pred_dir) { // left
2912 for (k = 1; k < 8; k++)
2913 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2915 for (k = 1; k < 8; k++)
2916 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2919 if (dc_pred_dir) { //left
2920 for (k = 1; k < 8; k++)
2921 block[k << v->left_blk_sh] += ac_val[k];
2923 for (k = 1; k < 8; k++)
2924 block[k << v->top_blk_sh] += ac_val[k + 8];
2928 /* save AC coeffs for further prediction */
2929 for (k = 1; k < 8; k++) {
2930 ac_val2[k ] = block[k << v->left_blk_sh];
2931 ac_val2[k + 8] = block[k << v->top_blk_sh];
2934 /* scale AC coeffs */
2935 for (k = 1; k < 64; k++)
2939 block[k] += (block[k] < 0) ? -mquant : mquant;
2942 if (use_pred) i = 63;
2943 } else { // no AC coeffs
2946 memset(ac_val2, 0, 16 * 2);
2947 if (dc_pred_dir) { // left
2949 memcpy(ac_val2, ac_val, 8 * 2);
2950 if (q2 && q1 != q2) {
2951 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2952 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2954 return AVERROR_INVALIDDATA;
2955 for (k = 1; k < 8; k++)
2956 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2961 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2962 if (q2 && q1 != q2) {
2963 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2964 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2966 return AVERROR_INVALIDDATA;
2967 for (k = 1; k < 8; k++)
2968 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2973 /* apply AC prediction if needed */
2975 if (dc_pred_dir) { // left
2976 for (k = 1; k < 8; k++) {
2977 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2978 if (!v->pquantizer && block[k << v->left_blk_sh])
2979 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2982 for (k = 1; k < 8; k++) {
2983 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2984 if (!v->pquantizer && block[k << v->top_blk_sh])
2985 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2991 s->block_last_index[n] = i;
2996 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2997 * @param v VC1Context
2998 * @param block block to decode
2999 * @param[in] n subblock index
3000 * @param coded are AC coeffs present or not
3001 * @param mquant block quantizer
3002 * @param codingset set of VLC to decode data
3004 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
3005 int coded, int mquant, int codingset)
3007 GetBitContext *gb = &v->s.gb;
3008 MpegEncContext *s = &v->s;
3009 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3011 int16_t *dc_val = NULL;
3012 int16_t *ac_val, *ac_val2;
3014 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3015 int a_avail = v->a_avail, c_avail = v->c_avail;
3016 int use_pred = s->ac_pred;
3020 s->bdsp.clear_block(block);
3022 /* XXX: Guard against dumb values of mquant */
3023 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3025 /* Set DC scale - y and c use the same */
3026 s->y_dc_scale = s->y_dc_scale_table[mquant];
3027 s->c_dc_scale = s->c_dc_scale_table[mquant];
3029 /* Get DC differential */
3031 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3033 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3036 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3040 if (dcdiff == 119 /* ESC index value */) {
3041 /* TODO: Optimize */
3042 if (mquant == 1) dcdiff = get_bits(gb, 10);
3043 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3044 else dcdiff = get_bits(gb, 8);
3047 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3048 else if (mquant == 2)
3049 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3056 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3059 /* Store the quantized DC coeff, used for prediction */
3062 block[0] = dcdiff * s->y_dc_scale;
3064 block[0] = dcdiff * s->c_dc_scale;
3070 /* check if AC is needed at all and adjust direction if needed */
3071 if (!a_avail) dc_pred_dir = 1;
3072 if (!c_avail) dc_pred_dir = 0;
3073 if (!a_avail && !c_avail) use_pred = 0;
3074 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3077 scale = mquant * 2 + v->halfpq;
3079 if (dc_pred_dir) //left
3082 ac_val -= 16 * s->block_wrap[n];
3084 q1 = s->current_picture.qscale_table[mb_pos];
3085 if (dc_pred_dir && c_avail && mb_pos)
3086 q2 = s->current_picture.qscale_table[mb_pos - 1];
3087 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3088 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3089 if ( dc_pred_dir && n == 1)
3091 if (!dc_pred_dir && n == 2)
3093 if (n == 3) q2 = q1;
3096 int last = 0, skip, value;
3100 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3104 if (v->fcm == PROGRESSIVE)
3105 block[v->zz_8x8[0][i++]] = value;
3107 if (use_pred && (v->fcm == ILACE_FRAME)) {
3108 if (!dc_pred_dir) // top
3109 block[v->zz_8x8[2][i++]] = value;
3111 block[v->zz_8x8[3][i++]] = value;
3113 block[v->zzi_8x8[i++]] = value;
3118 /* apply AC prediction if needed */
3120 /* scale predictors if needed*/
3121 if (q2 && q1 != q2) {
3122 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3123 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3126 return AVERROR_INVALIDDATA;
3127 if (dc_pred_dir) { // left
3128 for (k = 1; k < 8; k++)
3129 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3131 for (k = 1; k < 8; k++)
3132 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3135 if (dc_pred_dir) { // left
3136 for (k = 1; k < 8; k++)
3137 block[k << v->left_blk_sh] += ac_val[k];
3139 for (k = 1; k < 8; k++)
3140 block[k << v->top_blk_sh] += ac_val[k + 8];
3144 /* save AC coeffs for further prediction */
3145 for (k = 1; k < 8; k++) {
3146 ac_val2[k ] = block[k << v->left_blk_sh];
3147 ac_val2[k + 8] = block[k << v->top_blk_sh];
3150 /* scale AC coeffs */
3151 for (k = 1; k < 64; k++)
3155 block[k] += (block[k] < 0) ? -mquant : mquant;
3158 if (use_pred) i = 63;
3159 } else { // no AC coeffs
3162 memset(ac_val2, 0, 16 * 2);
3163 if (dc_pred_dir) { // left
3165 memcpy(ac_val2, ac_val, 8 * 2);
3166 if (q2 && q1 != q2) {
3167 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3168 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3170 return AVERROR_INVALIDDATA;
3171 for (k = 1; k < 8; k++)
3172 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3177 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3178 if (q2 && q1 != q2) {
3179 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3180 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3182 return AVERROR_INVALIDDATA;
3183 for (k = 1; k < 8; k++)
3184 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3189 /* apply AC prediction if needed */
3191 if (dc_pred_dir) { // left
3192 for (k = 1; k < 8; k++) {
3193 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3194 if (!v->pquantizer && block[k << v->left_blk_sh])
3195 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3198 for (k = 1; k < 8; k++) {
3199 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3200 if (!v->pquantizer && block[k << v->top_blk_sh])
3201 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3207 s->block_last_index[n] = i;
3214 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3215 int mquant, int ttmb, int first_block,
3216 uint8_t *dst, int linesize, int skip_block,
3219 MpegEncContext *s = &v->s;
3220 GetBitContext *gb = &s->gb;
3223 int scale, off, idx, last, skip, value;
3224 int ttblk = ttmb & 7;
3227 s->bdsp.clear_block(block);
3230 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)];
3232 if (ttblk == TT_4X4) {
3233 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3235 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3236 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3237 || (!v->res_rtm_flag && !first_block))) {
3238 subblkpat = decode012(gb);
3240 subblkpat ^= 3; // swap decoded pattern bits
3241 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3243 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3246 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3248 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3249 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3250 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3253 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3254 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3263 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3268 idx = v->zz_8x8[0][i++];
3270 idx = v->zzi_8x8[i++];
3271 block[idx] = value * scale;
3273 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3277 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3279 v->vc1dsp.vc1_inv_trans_8x8(block);
3280 s->idsp.add_pixels_clamped(block, dst, linesize);
3285 pat = ~subblkpat & 0xF;
3286 for (j = 0; j < 4; j++) {
3287 last = subblkpat & (1 << (3 - j));
3289 off = (j & 1) * 4 + (j & 2) * 16;
3291 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3296 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3298 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3299 block[idx + off] = value * scale;
3301 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3303 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3305 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3307 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3312 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3313 for (j = 0; j < 2; j++) {
3314 last = subblkpat & (1 << (1 - j));
3318 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3323 idx = v->zz_8x4[i++] + off;
3325 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3326 block[idx] = value * scale;
3328 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3330 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3332 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3334 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3339 pat = ~(subblkpat * 5) & 0xF;
3340 for (j = 0; j < 2; j++) {
3341 last = subblkpat & (1 << (1 - j));
3345 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3350 idx = v->zz_4x8[i++] + off;
3352 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3353 block[idx] = value * scale;
3355 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3357 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3359 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3361 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3367 *ttmb_out |= ttblk << (n * 4);
3371 /** @} */ // Macroblock group
3373 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3374 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3376 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3378 MpegEncContext *s = &v->s;
3379 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3380 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3381 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3382 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3383 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3386 if (block_num > 3) {
3387 dst = s->dest[block_num - 3];
3389 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3391 if (s->mb_y != s->end_mb_y || block_num < 2) {
3395 if (block_num > 3) {
3396 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3397 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3398 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3399 mv_stride = s->mb_stride;
3401 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3402 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3403 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3404 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3405 mv_stride = s->b8_stride;
3406 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3409 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3410 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3411 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3413 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3415 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3418 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3420 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3425 dst -= 4 * linesize;
3426 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3427 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3428 idx = (block_cbp | (block_cbp >> 2)) & 3;
3430 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3433 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3435 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3440 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3442 MpegEncContext *s = &v->s;
3443 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3444 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3445 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3446 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3447 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3450 if (block_num > 3) {
3451 dst = s->dest[block_num - 3] - 8 * linesize;
3453 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3456 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3459 if (block_num > 3) {
3460 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3461 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3462 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3464 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3465 : (mb_cbp >> ((block_num + 1) * 4));
3466 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3467 : (mb_is_intra >> ((block_num + 1) * 4));
3468 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3470 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3471 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3473 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3475 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3478 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3480 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3486 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3487 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3488 idx = (block_cbp | (block_cbp >> 1)) & 5;
3490 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3493 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3495 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3500 static void vc1_apply_p_loop_filter(VC1Context *v)
3502 MpegEncContext *s = &v->s;
3505 for (i = 0; i < 6; i++) {
3506 vc1_apply_p_v_loop_filter(v, i);
3509 /* V always precedes H, therefore we run H one MB before V;
3510 * at the end of a row, we catch up to complete the row */
3512 for (i = 0; i < 6; i++) {
3513 vc1_apply_p_h_loop_filter(v, i);
3515 if (s->mb_x == s->mb_width - 1) {
3517 ff_update_block_index(s);
3518 for (i = 0; i < 6; i++) {
3519 vc1_apply_p_h_loop_filter(v, i);
3525 /** Decode one P-frame MB
3527 static int vc1_decode_p_mb(VC1Context *v)
3529 MpegEncContext *s = &v->s;
3530 GetBitContext *gb = &s->gb;
3532 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3533 int cbp; /* cbp decoding stuff */
3534 int mqdiff, mquant; /* MB quantization */
3535 int ttmb = v->ttfrm; /* MB Transform type */
3537 int mb_has_coeffs = 1; /* last_flag */
3538 int dmv_x, dmv_y; /* Differential MV components */
3539 int index, index1; /* LUT indexes */
3540 int val, sign; /* temp values */
3541 int first_block = 1;
3543 int skipped, fourmv;
3544 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3546 mquant = v->pq; /* lossy initialization */
3548 if (v->mv_type_is_raw)
3549 fourmv = get_bits1(gb);
3551 fourmv = v->mv_type_mb_plane[mb_pos];
3553 skipped = get_bits1(gb);
3555 skipped = v->s.mbskip_table[mb_pos];
3557 if (!fourmv) { /* 1MV mode */
3559 GET_MVDATA(dmv_x, dmv_y);
3562 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3563 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3565 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3566 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3568 /* FIXME Set DC val for inter block ? */
3569 if (s->mb_intra && !mb_has_coeffs) {
3571 s->ac_pred = get_bits1(gb);
3573 } else if (mb_has_coeffs) {
3575 s->ac_pred = get_bits1(gb);
3576 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3582 s->current_picture.qscale_table[mb_pos] = mquant;
3584 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3585 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3586 VC1_TTMB_VLC_BITS, 2);
3587 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3589 for (i = 0; i < 6; i++) {
3590 s->dc_val[0][s->block_index[i]] = 0;
3592 val = ((cbp >> (5 - i)) & 1);
3593 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3594 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3596 /* check if prediction blocks A and C are available */
3597 v->a_avail = v->c_avail = 0;
3598 if (i == 2 || i == 3 || !s->first_slice_line)
3599 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3600 if (i == 1 || i == 3 || s->mb_x)
3601 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3603 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3604 (i & 4) ? v->codingset2 : v->codingset);
3605 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3607 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3609 for (j = 0; j < 64; j++)
3610 s->block[i][j] <<= 1;
3611 s->idsp.put_signed_pixels_clamped(s->block[i],
3612 s->dest[dst_idx] + off,
3613 i & 4 ? s->uvlinesize
3615 if (v->pq >= 9 && v->overlap) {
3617 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3619 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3621 block_cbp |= 0xF << (i << 2);
3622 block_intra |= 1 << i;
3624 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3625 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3626 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3627 block_cbp |= pat << (i << 2);
3628 if (!v->ttmbf && ttmb < 8)
3635 for (i = 0; i < 6; i++) {
3636 v->mb_type[0][s->block_index[i]] = 0;
3637 s->dc_val[0][s->block_index[i]] = 0;
3639 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3640 s->current_picture.qscale_table[mb_pos] = 0;
3641 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3644 } else { // 4MV mode
3645 if (!skipped /* unskipped MB */) {
3646 int intra_count = 0, coded_inter = 0;
3647 int is_intra[6], is_coded[6];
3649 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3650 for (i = 0; i < 6; i++) {
3651 val = ((cbp >> (5 - i)) & 1);
3652 s->dc_val[0][s->block_index[i]] = 0;
3659 GET_MVDATA(dmv_x, dmv_y);
3661 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3663 vc1_mc_4mv_luma(v, i, 0, 0);
3664 intra_count += s->mb_intra;
3665 is_intra[i] = s->mb_intra;
3666 is_coded[i] = mb_has_coeffs;
3669 is_intra[i] = (intra_count >= 3);
3673 vc1_mc_4mv_chroma(v, 0);
3674 v->mb_type[0][s->block_index[i]] = is_intra[i];
3676 coded_inter = !is_intra[i] && is_coded[i];
3678 // if there are no coded blocks then don't do anything more
3680 if (!intra_count && !coded_inter)
3683 s->current_picture.qscale_table[mb_pos] = mquant;
3684 /* test if block is intra and has pred */
3687 for (i = 0; i < 6; i++)
3689 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3690 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3696 s->ac_pred = get_bits1(gb);
3700 if (!v->ttmbf && coded_inter)
3701 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3702 for (i = 0; i < 6; i++) {
3704 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3705 s->mb_intra = is_intra[i];
3707 /* check if prediction blocks A and C are available */
3708 v->a_avail = v->c_avail = 0;
3709 if (i == 2 || i == 3 || !s->first_slice_line)
3710 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3711 if (i == 1 || i == 3 || s->mb_x)
3712 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3714 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3715 (i & 4) ? v->codingset2 : v->codingset);
3716 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3718 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3720 for (j = 0; j < 64; j++)
3721 s->block[i][j] <<= 1;
3722 s->idsp.put_signed_pixels_clamped(s->block[i],
3723 s->dest[dst_idx] + off,
3724 (i & 4) ? s->uvlinesize
3726 if (v->pq >= 9 && v->overlap) {
3728 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3730 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3732 block_cbp |= 0xF << (i << 2);
3733 block_intra |= 1 << i;
3734 } else if (is_coded[i]) {
3735 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3736 first_block, s->dest[dst_idx] + off,
3737 (i & 4) ? s->uvlinesize : s->linesize,
3738 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3740 block_cbp |= pat << (i << 2);
3741 if (!v->ttmbf && ttmb < 8)
3746 } else { // skipped MB
3748 s->current_picture.qscale_table[mb_pos] = 0;
3749 for (i = 0; i < 6; i++) {
3750 v->mb_type[0][s->block_index[i]] = 0;
3751 s->dc_val[0][s->block_index[i]] = 0;
3753 for (i = 0; i < 4; i++) {
3754 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3755 vc1_mc_4mv_luma(v, i, 0, 0);
3757 vc1_mc_4mv_chroma(v, 0);
3758 s->current_picture.qscale_table[mb_pos] = 0;
3762 v->cbp[s->mb_x] = block_cbp;
3763 v->ttblk[s->mb_x] = block_tt;
3764 v->is_intra[s->mb_x] = block_intra;
3769 /* Decode one macroblock in an interlaced frame p picture */
3771 static int vc1_decode_p_mb_intfr(VC1Context *v)
3773 MpegEncContext *s = &v->s;
3774 GetBitContext *gb = &s->gb;
3776 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3777 int cbp = 0; /* cbp decoding stuff */
3778 int mqdiff, mquant; /* MB quantization */
3779 int ttmb = v->ttfrm; /* MB Transform type */
3781 int mb_has_coeffs = 1; /* last_flag */
3782 int dmv_x, dmv_y; /* Differential MV components */
3783 int val; /* temp value */
3784 int first_block = 1;
3786 int skipped, fourmv = 0, twomv = 0;
3787 int block_cbp = 0, pat, block_tt = 0;
3788 int idx_mbmode = 0, mvbp;
3789 int stride_y, fieldtx;
3791 mquant = v->pq; /* Lossy initialization */
3794 skipped = get_bits1(gb);
3796 skipped = v->s.mbskip_table[mb_pos];
3798 if (v->fourmvswitch)
3799 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3801 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3802 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3803 /* store the motion vector type in a flag (useful later) */
3804 case MV_PMODE_INTFR_4MV:
3806 v->blk_mv_type[s->block_index[0]] = 0;
3807 v->blk_mv_type[s->block_index[1]] = 0;
3808 v->blk_mv_type[s->block_index[2]] = 0;
3809 v->blk_mv_type[s->block_index[3]] = 0;
3811 case MV_PMODE_INTFR_4MV_FIELD:
3813 v->blk_mv_type[s->block_index[0]] = 1;
3814 v->blk_mv_type[s->block_index[1]] = 1;
3815 v->blk_mv_type[s->block_index[2]] = 1;
3816 v->blk_mv_type[s->block_index[3]] = 1;
3818 case MV_PMODE_INTFR_2MV_FIELD:
3820 v->blk_mv_type[s->block_index[0]] = 1;
3821 v->blk_mv_type[s->block_index[1]] = 1;
3822 v->blk_mv_type[s->block_index[2]] = 1;
3823 v->blk_mv_type[s->block_index[3]] = 1;
3825 case MV_PMODE_INTFR_1MV:
3826 v->blk_mv_type[s->block_index[0]] = 0;
3827 v->blk_mv_type[s->block_index[1]] = 0;
3828 v->blk_mv_type[s->block_index[2]] = 0;
3829 v->blk_mv_type[s->block_index[3]] = 0;
3832 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3833 for (i = 0; i < 4; i++) {
3834 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3835 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3837 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3838 s->mb_intra = v->is_intra[s->mb_x] = 1;
3839 for (i = 0; i < 6; i++)
3840 v->mb_type[0][s->block_index[i]] = 1;
3841 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3842 mb_has_coeffs = get_bits1(gb);
3844 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3845 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3847 s->current_picture.qscale_table[mb_pos] = mquant;
3848 /* Set DC scale - y and c use the same (not sure if necessary here) */
3849 s->y_dc_scale = s->y_dc_scale_table[mquant];
3850 s->c_dc_scale = s->c_dc_scale_table[mquant];
3852 for (i = 0; i < 6; i++) {
3853 s->dc_val[0][s->block_index[i]] = 0;
3855 val = ((cbp >> (5 - i)) & 1);
3856 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3857 v->a_avail = v->c_avail = 0;
3858 if (i == 2 || i == 3 || !s->first_slice_line)
3859 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3860 if (i == 1 || i == 3 || s->mb_x)
3861 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3863 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3864 (i & 4) ? v->codingset2 : v->codingset);
3865 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3866 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3868 stride_y = s->linesize << fieldtx;
3869 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3871 stride_y = s->uvlinesize;
3874 s->idsp.put_signed_pixels_clamped(s->block[i],
3875 s->dest[dst_idx] + off,
3880 } else { // inter MB
3881 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3883 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3884 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3885 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3887 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3888 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3889 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3892 s->mb_intra = v->is_intra[s->mb_x] = 0;
3893 for (i = 0; i < 6; i++)
3894 v->mb_type[0][s->block_index[i]] = 0;
3895 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3896 /* for all motion vector read MVDATA and motion compensate each block */
3900 for (i = 0; i < 6; i++) {
3903 val = ((mvbp >> (3 - i)) & 1);
3905 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3907 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3908 vc1_mc_4mv_luma(v, i, 0, 0);
3909 } else if (i == 4) {
3910 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3917 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3919 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3920 vc1_mc_4mv_luma(v, 0, 0, 0);
3921 vc1_mc_4mv_luma(v, 1, 0, 0);
3924 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3926 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3927 vc1_mc_4mv_luma(v, 2, 0, 0);
3928 vc1_mc_4mv_luma(v, 3, 0, 0);
3929 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3931 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3934 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3936 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3940 GET_MQUANT(); // p. 227
3941 s->current_picture.qscale_table[mb_pos] = mquant;
3942 if (!v->ttmbf && cbp)
3943 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3944 for (i = 0; i < 6; i++) {
3945 s->dc_val[0][s->block_index[i]] = 0;
3947 val = ((cbp >> (5 - i)) & 1);
3949 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3951 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3953 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3954 first_block, s->dest[dst_idx] + off,
3955 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3956 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3957 block_cbp |= pat << (i << 2);
3958 if (!v->ttmbf && ttmb < 8)
3965 s->mb_intra = v->is_intra[s->mb_x] = 0;
3966 for (i = 0; i < 6; i++) {
3967 v->mb_type[0][s->block_index[i]] = 0;
3968 s->dc_val[0][s->block_index[i]] = 0;
3970 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3971 s->current_picture.qscale_table[mb_pos] = 0;
3972 v->blk_mv_type[s->block_index[0]] = 0;
3973 v->blk_mv_type[s->block_index[1]] = 0;
3974 v->blk_mv_type[s->block_index[2]] = 0;
3975 v->blk_mv_type[s->block_index[3]] = 0;
3976 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3979 if (s->mb_x == s->mb_width - 1)
3980 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3984 static int vc1_decode_p_mb_intfi(VC1Context *v)
3986 MpegEncContext *s = &v->s;
3987 GetBitContext *gb = &s->gb;
3989 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3990 int cbp = 0; /* cbp decoding stuff */
3991 int mqdiff, mquant; /* MB quantization */
3992 int ttmb = v->ttfrm; /* MB Transform type */
3994 int mb_has_coeffs = 1; /* last_flag */
3995 int dmv_x, dmv_y; /* Differential MV components */
3996 int val; /* temp values */
3997 int first_block = 1;
4000 int block_cbp = 0, pat, block_tt = 0;
4003 mquant = v->pq; /* Lossy initialization */
4005 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4006 if (idx_mbmode <= 1) { // intra MB
4007 s->mb_intra = v->is_intra[s->mb_x] = 1;
4008 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4009 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4010 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4012 s->current_picture.qscale_table[mb_pos] = mquant;
4013 /* Set DC scale - y and c use the same (not sure if necessary here) */
4014 s->y_dc_scale = s->y_dc_scale_table[mquant];
4015 s->c_dc_scale = s->c_dc_scale_table[mquant];
4016 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4017 mb_has_coeffs = idx_mbmode & 1;
4019 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4021 for (i = 0; i < 6; i++) {
4022 s->dc_val[0][s->block_index[i]] = 0;
4023 v->mb_type[0][s->block_index[i]] = 1;
4025 val = ((cbp >> (5 - i)) & 1);
4026 v->a_avail = v->c_avail = 0;
4027 if (i == 2 || i == 3 || !s->first_slice_line)
4028 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4029 if (i == 1 || i == 3 || s->mb_x)
4030 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4032 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4033 (i & 4) ? v->codingset2 : v->codingset);
4034 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4036 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4037 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4038 s->idsp.put_signed_pixels_clamped(s->block[i],
4039 s->dest[dst_idx] + off,
4040 (i & 4) ? s->uvlinesize
4042 // TODO: loop filter
4045 s->mb_intra = v->is_intra[s->mb_x] = 0;
4046 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4047 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4048 if (idx_mbmode <= 5) { // 1-MV
4049 dmv_x = dmv_y = pred_flag = 0;
4050 if (idx_mbmode & 1) {
4051 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4053 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4055 mb_has_coeffs = !(idx_mbmode & 2);
4057 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4058 for (i = 0; i < 6; i++) {
4060 dmv_x = dmv_y = pred_flag = 0;
4061 val = ((v->fourmvbp >> (3 - i)) & 1);
4063 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4065 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4066 vc1_mc_4mv_luma(v, i, 0, 0);
4068 vc1_mc_4mv_chroma(v, 0);
4070 mb_has_coeffs = idx_mbmode & 1;
4073 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4077 s->current_picture.qscale_table[mb_pos] = mquant;
4078 if (!v->ttmbf && cbp) {
4079 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4082 for (i = 0; i < 6; i++) {
4083 s->dc_val[0][s->block_index[i]] = 0;
4085 val = ((cbp >> (5 - i)) & 1);
4086 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4088 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4089 first_block, s->dest[dst_idx] + off,
4090 (i & 4) ? s->uvlinesize : s->linesize,
4091 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4093 block_cbp |= pat << (i << 2);
4094 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4099 if (s->mb_x == s->mb_width - 1)
4100 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4104 /** Decode one B-frame MB (in Main profile)
4106 static void vc1_decode_b_mb(VC1Context *v)
4108 MpegEncContext *s = &v->s;
4109 GetBitContext *gb = &s->gb;
4111 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4112 int cbp = 0; /* cbp decoding stuff */
4113 int mqdiff, mquant; /* MB quantization */
4114 int ttmb = v->ttfrm; /* MB Transform type */
4115 int mb_has_coeffs = 0; /* last_flag */
4116 int index, index1; /* LUT indexes */
4117 int val, sign; /* temp values */
4118 int first_block = 1;
4120 int skipped, direct;
4121 int dmv_x[2], dmv_y[2];
4122 int bmvtype = BMV_TYPE_BACKWARD;
4124 mquant = v->pq; /* lossy initialization */
4128 direct = get_bits1(gb);
4130 direct = v->direct_mb_plane[mb_pos];
4132 skipped = get_bits1(gb);
4134 skipped = v->s.mbskip_table[mb_pos];
4136 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4137 for (i = 0; i < 6; i++) {
4138 v->mb_type[0][s->block_index[i]] = 0;
4139 s->dc_val[0][s->block_index[i]] = 0;
4141 s->current_picture.qscale_table[mb_pos] = 0;
4145 GET_MVDATA(dmv_x[0], dmv_y[0]);
4146 dmv_x[1] = dmv_x[0];
4147 dmv_y[1] = dmv_y[0];
4149 if (skipped || !s->mb_intra) {
4150 bmvtype = decode012(gb);
4153 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4156 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4159 bmvtype = BMV_TYPE_INTERPOLATED;
4160 dmv_x[0] = dmv_y[0] = 0;
4164 for (i = 0; i < 6; i++)
4165 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4169 bmvtype = BMV_TYPE_INTERPOLATED;
4170 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4171 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4175 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4178 s->current_picture.qscale_table[mb_pos] = mquant;
4180 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4181 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4182 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4183 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4185 if (!mb_has_coeffs && !s->mb_intra) {
4186 /* no coded blocks - effectively skipped */
4187 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4188 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4191 if (s->mb_intra && !mb_has_coeffs) {
4193 s->current_picture.qscale_table[mb_pos] = mquant;
4194 s->ac_pred = get_bits1(gb);
4196 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4198 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4199 GET_MVDATA(dmv_x[0], dmv_y[0]);
4200 if (!mb_has_coeffs) {
4201 /* interpolated skipped block */
4202 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4203 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4207 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4209 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4212 s->ac_pred = get_bits1(gb);
4213 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4215 s->current_picture.qscale_table[mb_pos] = mquant;
4216 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4217 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4221 for (i = 0; i < 6; i++) {
4222 s->dc_val[0][s->block_index[i]] = 0;
4224 val = ((cbp >> (5 - i)) & 1);
4225 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4226 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4228 /* check if prediction blocks A and C are available */
4229 v->a_avail = v->c_avail = 0;
4230 if (i == 2 || i == 3 || !s->first_slice_line)
4231 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4232 if (i == 1 || i == 3 || s->mb_x)
4233 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4235 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4236 (i & 4) ? v->codingset2 : v->codingset);
4237 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4239 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4241 for (j = 0; j < 64; j++)
4242 s->block[i][j] <<= 1;
4243 s->idsp.put_signed_pixels_clamped(s->block[i],
4244 s->dest[dst_idx] + off,
4245 i & 4 ? s->uvlinesize
4248 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4249 first_block, s->dest[dst_idx] + off,
4250 (i & 4) ? s->uvlinesize : s->linesize,
4251 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4252 if (!v->ttmbf && ttmb < 8)
4259 /** Decode one B-frame MB (in interlaced field B picture)
4261 static void vc1_decode_b_mb_intfi(VC1Context *v)
4263 MpegEncContext *s = &v->s;
4264 GetBitContext *gb = &s->gb;
4266 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4267 int cbp = 0; /* cbp decoding stuff */
4268 int mqdiff, mquant; /* MB quantization */
4269 int ttmb = v->ttfrm; /* MB Transform type */
4270 int mb_has_coeffs = 0; /* last_flag */
4271 int val; /* temp value */
4272 int first_block = 1;
4275 int dmv_x[2], dmv_y[2], pred_flag[2];
4276 int bmvtype = BMV_TYPE_BACKWARD;
4279 mquant = v->pq; /* Lossy initialization */
4282 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4283 if (idx_mbmode <= 1) { // intra MB
4284 s->mb_intra = v->is_intra[s->mb_x] = 1;
4285 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4286 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4287 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4289 s->current_picture.qscale_table[mb_pos] = mquant;
4290 /* Set DC scale - y and c use the same (not sure if necessary here) */
4291 s->y_dc_scale = s->y_dc_scale_table[mquant];
4292 s->c_dc_scale = s->c_dc_scale_table[mquant];
4293 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4294 mb_has_coeffs = idx_mbmode & 1;
4296 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4298 for (i = 0; i < 6; i++) {
4299 s->dc_val[0][s->block_index[i]] = 0;
4301 val = ((cbp >> (5 - i)) & 1);
4302 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4303 v->a_avail = v->c_avail = 0;
4304 if (i == 2 || i == 3 || !s->first_slice_line)
4305 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4306 if (i == 1 || i == 3 || s->mb_x)
4307 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4309 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4310 (i & 4) ? v->codingset2 : v->codingset);
4311 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4313 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4315 for (j = 0; j < 64; j++)
4316 s->block[i][j] <<= 1;
4317 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4318 s->idsp.put_signed_pixels_clamped(s->block[i],
4319 s->dest[dst_idx] + off,
4320 (i & 4) ? s->uvlinesize
4322 // TODO: yet to perform loop filter
4325 s->mb_intra = v->is_intra[s->mb_x] = 0;
4326 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4327 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4329 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4331 fwd = v->forward_mb_plane[mb_pos];
4332 if (idx_mbmode <= 5) { // 1-MV
4334 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4335 pred_flag[0] = pred_flag[1] = 0;
4337 bmvtype = BMV_TYPE_FORWARD;
4339 bmvtype = decode012(gb);
4342 bmvtype = BMV_TYPE_BACKWARD;
4345 bmvtype = BMV_TYPE_DIRECT;
4348 bmvtype = BMV_TYPE_INTERPOLATED;
4349 interpmvp = get_bits1(gb);
4352 v->bmvtype = bmvtype;
4353 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4354 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4357 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4359 if (bmvtype == BMV_TYPE_DIRECT) {
4360 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4361 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4362 if (!s->next_picture_ptr->field_picture) {
4363 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
4367 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4368 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4369 mb_has_coeffs = !(idx_mbmode & 2);
4372 bmvtype = BMV_TYPE_FORWARD;
4373 v->bmvtype = bmvtype;
4374 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4375 for (i = 0; i < 6; i++) {
4377 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4378 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4379 val = ((v->fourmvbp >> (3 - i)) & 1);
4381 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4382 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4383 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4385 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4386 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4388 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4390 mb_has_coeffs = idx_mbmode & 1;
4393 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4397 s->current_picture.qscale_table[mb_pos] = mquant;
4398 if (!v->ttmbf && cbp) {
4399 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4402 for (i = 0; i < 6; i++) {
4403 s->dc_val[0][s->block_index[i]] = 0;
4405 val = ((cbp >> (5 - i)) & 1);
4406 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4408 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4409 first_block, s->dest[dst_idx] + off,
4410 (i & 4) ? s->uvlinesize : s->linesize,
4411 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4412 if (!v->ttmbf && ttmb < 8)
4420 /** Decode one B-frame MB (in interlaced frame B picture)
4422 static int vc1_decode_b_mb_intfr(VC1Context *v)
4424 MpegEncContext *s = &v->s;
4425 GetBitContext *gb = &s->gb;
4427 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4428 int cbp = 0; /* cbp decoding stuff */
4429 int mqdiff, mquant; /* MB quantization */
4430 int ttmb = v->ttfrm; /* MB Transform type */
4431 int mvsw = 0; /* motion vector switch */
4432 int mb_has_coeffs = 1; /* last_flag */
4433 int dmv_x, dmv_y; /* Differential MV components */
4434 int val; /* temp value */
4435 int first_block = 1;
4437 int skipped, direct, twomv = 0;
4438 int block_cbp = 0, pat, block_tt = 0;
4439 int idx_mbmode = 0, mvbp;
4440 int stride_y, fieldtx;
4441 int bmvtype = BMV_TYPE_BACKWARD;
4444 mquant = v->pq; /* Lossy initialization */
4447 skipped = get_bits1(gb);
4449 skipped = v->s.mbskip_table[mb_pos];
4452 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4453 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4455 v->blk_mv_type[s->block_index[0]] = 1;
4456 v->blk_mv_type[s->block_index[1]] = 1;
4457 v->blk_mv_type[s->block_index[2]] = 1;
4458 v->blk_mv_type[s->block_index[3]] = 1;
4460 v->blk_mv_type[s->block_index[0]] = 0;
4461 v->blk_mv_type[s->block_index[1]] = 0;
4462 v->blk_mv_type[s->block_index[2]] = 0;
4463 v->blk_mv_type[s->block_index[3]] = 0;
4468 direct = get_bits1(gb);
4470 direct = v->direct_mb_plane[mb_pos];
4473 if (s->next_picture_ptr->field_picture)
4474 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
4475 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);
4476 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);
4477 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);
4478 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);
4481 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);
4482 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);
4483 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);
4484 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);
4486 for (i = 1; i < 4; i += 2) {
4487 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4488 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4489 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4490 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4493 for (i = 1; i < 4; i++) {
4494 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4495 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4496 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4497 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4502 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4503 for (i = 0; i < 4; i++) {
4504 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4505 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4506 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4507 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4509 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4510 s->mb_intra = v->is_intra[s->mb_x] = 1;
4511 for (i = 0; i < 6; i++)
4512 v->mb_type[0][s->block_index[i]] = 1;
4513 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4514 mb_has_coeffs = get_bits1(gb);
4516 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4517 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4519 s->current_picture.qscale_table[mb_pos] = mquant;
4520 /* Set DC scale - y and c use the same (not sure if necessary here) */
4521 s->y_dc_scale = s->y_dc_scale_table[mquant];
4522 s->c_dc_scale = s->c_dc_scale_table[mquant];
4524 for (i = 0; i < 6; i++) {
4525 s->dc_val[0][s->block_index[i]] = 0;
4527 val = ((cbp >> (5 - i)) & 1);
4528 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4529 v->a_avail = v->c_avail = 0;
4530 if (i == 2 || i == 3 || !s->first_slice_line)
4531 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4532 if (i == 1 || i == 3 || s->mb_x)
4533 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4535 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4536 (i & 4) ? v->codingset2 : v->codingset);
4537 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4539 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4541 stride_y = s->linesize << fieldtx;
4542 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4544 stride_y = s->uvlinesize;
4547 s->idsp.put_signed_pixels_clamped(s->block[i],
4548 s->dest[dst_idx] + off,
4552 s->mb_intra = v->is_intra[s->mb_x] = 0;
4554 if (skipped || !s->mb_intra) {
4555 bmvtype = decode012(gb);
4558 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4561 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4564 bmvtype = BMV_TYPE_INTERPOLATED;
4568 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4569 mvsw = get_bits1(gb);
4572 if (!skipped) { // inter MB
4573 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4575 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4577 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
4578 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4579 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
4580 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4584 for (i = 0; i < 6; i++)
4585 v->mb_type[0][s->block_index[i]] = 0;
4586 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4587 /* for all motion vector read MVDATA and motion compensate each block */
4591 for (i = 0; i < 4; i++) {
4592 vc1_mc_4mv_luma(v, i, 0, 0);
4593 vc1_mc_4mv_luma(v, i, 1, 1);
4595 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4596 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4601 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4603 for (i = 0; i < 4; i++) {
4606 val = ((mvbp >> (3 - i)) & 1);
4608 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4610 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4611 vc1_mc_4mv_luma(v, j, dir, dir);
4612 vc1_mc_4mv_luma(v, j+1, dir, dir);
4615 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4616 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4617 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4621 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4623 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4628 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4630 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4633 dir = bmvtype == BMV_TYPE_BACKWARD;
4640 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4641 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4645 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4646 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
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];
4652 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];
4653 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];
4656 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4657 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4660 vc1_mc_4mv_luma(v, 0, dir, 0);
4661 vc1_mc_4mv_luma(v, 1, dir, 0);
4662 vc1_mc_4mv_luma(v, 2, dir2, 0);
4663 vc1_mc_4mv_luma(v, 3, dir2, 0);
4664 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4666 dir = bmvtype == BMV_TYPE_BACKWARD;
4668 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4671 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4673 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4674 v->blk_mv_type[s->block_index[0]] = 1;
4675 v->blk_mv_type[s->block_index[1]] = 1;
4676 v->blk_mv_type[s->block_index[2]] = 1;
4677 v->blk_mv_type[s->block_index[3]] = 1;
4678 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4679 for (i = 0; i < 2; i++) {
4680 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];
4681 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];
4687 GET_MQUANT(); // p. 227
4688 s->current_picture.qscale_table[mb_pos] = mquant;
4689 if (!v->ttmbf && cbp)
4690 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4691 for (i = 0; i < 6; i++) {
4692 s->dc_val[0][s->block_index[i]] = 0;
4694 val = ((cbp >> (5 - i)) & 1);
4696 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4698 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4700 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4701 first_block, s->dest[dst_idx] + off,
4702 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4703 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4704 block_cbp |= pat << (i << 2);
4705 if (!v->ttmbf && ttmb < 8)
4713 for (i = 0; i < 6; i++) {
4714 v->mb_type[0][s->block_index[i]] = 0;
4715 s->dc_val[0][s->block_index[i]] = 0;
4717 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4718 s->current_picture.qscale_table[mb_pos] = 0;
4719 v->blk_mv_type[s->block_index[0]] = 0;
4720 v->blk_mv_type[s->block_index[1]] = 0;
4721 v->blk_mv_type[s->block_index[2]] = 0;
4722 v->blk_mv_type[s->block_index[3]] = 0;
4725 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4726 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4727 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4729 dir = bmvtype == BMV_TYPE_BACKWARD;
4730 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4735 for (i = 0; i < 2; i++) {
4736 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];
4737 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];
4738 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];
4739 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];
4742 v->blk_mv_type[s->block_index[0]] = 1;
4743 v->blk_mv_type[s->block_index[1]] = 1;
4744 v->blk_mv_type[s->block_index[2]] = 1;
4745 v->blk_mv_type[s->block_index[3]] = 1;
4746 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4747 for (i = 0; i < 2; i++) {
4748 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];
4749 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];
4756 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4761 if (s->mb_x == s->mb_width - 1)
4762 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4763 v->cbp[s->mb_x] = block_cbp;
4764 v->ttblk[s->mb_x] = block_tt;
4768 /** Decode blocks of I-frame
4770 static void vc1_decode_i_blocks(VC1Context *v)
4773 MpegEncContext *s = &v->s;
4778 /* select codingmode used for VLC tables selection */
4779 switch (v->y_ac_table_index) {
4781 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4784 v->codingset = CS_HIGH_MOT_INTRA;
4787 v->codingset = CS_MID_RATE_INTRA;
4791 switch (v->c_ac_table_index) {
4793 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4796 v->codingset2 = CS_HIGH_MOT_INTER;
4799 v->codingset2 = CS_MID_RATE_INTER;
4803 /* Set DC scale - y and c use the same */
4804 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4805 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4808 s->mb_x = s->mb_y = 0;
4810 s->first_slice_line = 1;
4811 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4813 init_block_index(v);
4814 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4816 ff_update_block_index(s);
4817 dst[0] = s->dest[0];
4818 dst[1] = dst[0] + 8;
4819 dst[2] = s->dest[0] + s->linesize * 8;
4820 dst[3] = dst[2] + 8;
4821 dst[4] = s->dest[1];
4822 dst[5] = s->dest[2];
4823 s->bdsp.clear_blocks(s->block[0]);
4824 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4825 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4826 s->current_picture.qscale_table[mb_pos] = v->pq;
4827 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4828 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4830 // do actual MB decoding and displaying
4831 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4832 v->s.ac_pred = get_bits1(&v->s.gb);
4834 for (k = 0; k < 6; k++) {
4835 val = ((cbp >> (5 - k)) & 1);
4838 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4842 cbp |= val << (5 - k);
4844 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4846 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4848 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4849 if (v->pq >= 9 && v->overlap) {
4851 for (j = 0; j < 64; j++)
4852 s->block[k][j] <<= 1;
4853 s->idsp.put_signed_pixels_clamped(s->block[k], dst[k],
4854 k & 4 ? s->uvlinesize
4858 for (j = 0; j < 64; j++)
4859 s->block[k][j] = (s->block[k][j] - 64) << 1;
4860 s->idsp.put_pixels_clamped(s->block[k], dst[k],
4861 k & 4 ? s->uvlinesize
4866 if (v->pq >= 9 && v->overlap) {
4868 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4869 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4870 if (!(s->flags & CODEC_FLAG_GRAY)) {
4871 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4872 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4875 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4876 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4877 if (!s->first_slice_line) {
4878 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4879 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4880 if (!(s->flags & CODEC_FLAG_GRAY)) {
4881 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4882 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4885 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4886 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4888 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4890 if (get_bits_count(&s->gb) > v->bits) {
4891 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4892 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4893 get_bits_count(&s->gb), v->bits);
4897 if (!v->s.loop_filter)
4898 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4900 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4902 s->first_slice_line = 0;
4904 if (v->s.loop_filter)
4905 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4907 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4908 * profile, these only differ are when decoding MSS2 rectangles. */
4909 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4912 /** Decode blocks of I-frame for advanced profile
4914 static void vc1_decode_i_blocks_adv(VC1Context *v)
4917 MpegEncContext *s = &v->s;
4923 GetBitContext *gb = &s->gb;
4925 /* select codingmode used for VLC tables selection */
4926 switch (v->y_ac_table_index) {
4928 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4931 v->codingset = CS_HIGH_MOT_INTRA;
4934 v->codingset = CS_MID_RATE_INTRA;
4938 switch (v->c_ac_table_index) {
4940 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4943 v->codingset2 = CS_HIGH_MOT_INTER;
4946 v->codingset2 = CS_MID_RATE_INTER;
4951 s->mb_x = s->mb_y = 0;
4953 s->first_slice_line = 1;
4954 s->mb_y = s->start_mb_y;
4955 if (s->start_mb_y) {
4957 init_block_index(v);
4958 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4959 (1 + s->b8_stride) * sizeof(*s->coded_block));
4961 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4963 init_block_index(v);
4964 for (;s->mb_x < s->mb_width; s->mb_x++) {
4965 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4966 ff_update_block_index(s);
4967 s->bdsp.clear_blocks(block[0]);
4968 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4969 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4970 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4971 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4973 // do actual MB decoding and displaying
4974 if (v->fieldtx_is_raw)
4975 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4976 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4977 if ( v->acpred_is_raw)
4978 v->s.ac_pred = get_bits1(&v->s.gb);
4980 v->s.ac_pred = v->acpred_plane[mb_pos];
4982 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4983 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4987 s->current_picture.qscale_table[mb_pos] = mquant;
4988 /* Set DC scale - y and c use the same */
4989 s->y_dc_scale = s->y_dc_scale_table[mquant];
4990 s->c_dc_scale = s->c_dc_scale_table[mquant];
4992 for (k = 0; k < 6; k++) {
4993 val = ((cbp >> (5 - k)) & 1);
4996 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
5000 cbp |= val << (5 - k);
5002 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
5003 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
5005 vc1_decode_i_block_adv(v, block[k], k, val,
5006 (k < 4) ? v->codingset : v->codingset2, mquant);
5008 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
5010 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
5013 vc1_smooth_overlap_filter_iblk(v);
5014 vc1_put_signed_blocks_clamped(v);
5015 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
5017 if (get_bits_count(&s->gb) > v->bits) {
5018 // TODO: may need modification to handle slice coding
5019 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5020 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
5021 get_bits_count(&s->gb), v->bits);
5025 if (!v->s.loop_filter)
5026 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5028 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
5029 s->first_slice_line = 0;
5032 /* raw bottom MB row */
5034 init_block_index(v);
5036 for (;s->mb_x < s->mb_width; s->mb_x++) {
5037 ff_update_block_index(s);
5038 vc1_put_signed_blocks_clamped(v);
5039 if (v->s.loop_filter)
5040 vc1_loop_filter_iblk_delayed(v, v->pq);
5042 if (v->s.loop_filter)
5043 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5044 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5045 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5048 static void vc1_decode_p_blocks(VC1Context *v)
5050 MpegEncContext *s = &v->s;
5051 int apply_loop_filter;
5053 /* select codingmode used for VLC tables selection */
5054 switch (v->c_ac_table_index) {
5056 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5059 v->codingset = CS_HIGH_MOT_INTRA;
5062 v->codingset = CS_MID_RATE_INTRA;
5066 switch (v->c_ac_table_index) {
5068 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5071 v->codingset2 = CS_HIGH_MOT_INTER;
5074 v->codingset2 = CS_MID_RATE_INTER;
5078 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5079 v->fcm == PROGRESSIVE;
5080 s->first_slice_line = 1;
5081 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5082 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5084 init_block_index(v);
5085 for (; s->mb_x < s->mb_width; s->mb_x++) {
5086 ff_update_block_index(s);
5088 if (v->fcm == ILACE_FIELD)
5089 vc1_decode_p_mb_intfi(v);
5090 else if (v->fcm == ILACE_FRAME)
5091 vc1_decode_p_mb_intfr(v);
5092 else vc1_decode_p_mb(v);
5093 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5094 vc1_apply_p_loop_filter(v);
5095 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5096 // TODO: may need modification to handle slice coding
5097 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5098 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5099 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5103 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5104 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5105 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5106 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5107 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5108 s->first_slice_line = 0;
5110 if (apply_loop_filter) {
5112 init_block_index(v);
5113 for (; s->mb_x < s->mb_width; s->mb_x++) {
5114 ff_update_block_index(s);
5115 vc1_apply_p_loop_filter(v);
5118 if (s->end_mb_y >= s->start_mb_y)
5119 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5120 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5121 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5124 static void vc1_decode_b_blocks(VC1Context *v)
5126 MpegEncContext *s = &v->s;
5128 /* select codingmode used for VLC tables selection */
5129 switch (v->c_ac_table_index) {
5131 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5134 v->codingset = CS_HIGH_MOT_INTRA;
5137 v->codingset = CS_MID_RATE_INTRA;
5141 switch (v->c_ac_table_index) {
5143 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5146 v->codingset2 = CS_HIGH_MOT_INTER;
5149 v->codingset2 = CS_MID_RATE_INTER;
5153 s->first_slice_line = 1;
5154 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5156 init_block_index(v);
5157 for (; s->mb_x < s->mb_width; s->mb_x++) {
5158 ff_update_block_index(s);
5160 if (v->fcm == ILACE_FIELD)
5161 vc1_decode_b_mb_intfi(v);
5162 else if (v->fcm == ILACE_FRAME)
5163 vc1_decode_b_mb_intfr(v);
5166 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5167 // TODO: may need modification to handle slice coding
5168 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5169 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5170 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5173 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5175 if (!v->s.loop_filter)
5176 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5178 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5179 s->first_slice_line = 0;
5181 if (v->s.loop_filter)
5182 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5183 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5184 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5187 static void vc1_decode_skip_blocks(VC1Context *v)
5189 MpegEncContext *s = &v->s;
5191 if (!v->s.last_picture.f->data[0])
5194 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5195 s->first_slice_line = 1;
5196 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5198 init_block_index(v);
5199 ff_update_block_index(s);
5200 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5201 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5202 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5203 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5204 s->first_slice_line = 0;
5206 s->pict_type = AV_PICTURE_TYPE_P;
5209 void ff_vc1_decode_blocks(VC1Context *v)
5212 v->s.esc3_level_length = 0;
5214 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5217 v->left_blk_idx = -1;
5218 v->topleft_blk_idx = 1;
5220 switch (v->s.pict_type) {
5221 case AV_PICTURE_TYPE_I:
5222 if (v->profile == PROFILE_ADVANCED)
5223 vc1_decode_i_blocks_adv(v);
5225 vc1_decode_i_blocks(v);
5227 case AV_PICTURE_TYPE_P:
5228 if (v->p_frame_skipped)
5229 vc1_decode_skip_blocks(v);
5231 vc1_decode_p_blocks(v);
5233 case AV_PICTURE_TYPE_B:
5235 if (v->profile == PROFILE_ADVANCED)
5236 vc1_decode_i_blocks_adv(v);
5238 vc1_decode_i_blocks(v);
5240 vc1_decode_b_blocks(v);
5246 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5250 * Transform coefficients for both sprites in 16.16 fixed point format,
5251 * in the order they appear in the bitstream:
5253 * rotation 1 (unused)
5255 * rotation 2 (unused)
5262 int effect_type, effect_flag;
5263 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5264 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5267 static inline int get_fp_val(GetBitContext* gb)
5269 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5272 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5276 switch (get_bits(gb, 2)) {
5279 c[2] = get_fp_val(gb);
5283 c[0] = c[4] = get_fp_val(gb);
5284 c[2] = get_fp_val(gb);
5287 c[0] = get_fp_val(gb);
5288 c[2] = get_fp_val(gb);
5289 c[4] = get_fp_val(gb);
5292 c[0] = get_fp_val(gb);
5293 c[1] = get_fp_val(gb);
5294 c[2] = get_fp_val(gb);
5295 c[3] = get_fp_val(gb);
5296 c[4] = get_fp_val(gb);
5299 c[5] = get_fp_val(gb);
5301 c[6] = get_fp_val(gb);
5306 static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5308 AVCodecContext *avctx = v->s.avctx;
5311 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5312 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5313 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5314 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5315 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5316 for (i = 0; i < 7; i++)
5317 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5318 sd->coefs[sprite][i] / (1<<16),
5319 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5320 av_log(avctx, AV_LOG_DEBUG, "\n");
5324 if (sd->effect_type = get_bits_long(gb, 30)) {
5325 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5327 vc1_sprite_parse_transform(gb, sd->effect_params1);
5330 vc1_sprite_parse_transform(gb, sd->effect_params1);
5331 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5334 for (i = 0; i < sd->effect_pcount1; i++)
5335 sd->effect_params1[i] = get_fp_val(gb);
5337 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5338 // effect 13 is simple alpha blending and matches the opacity above
5339 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5340 for (i = 0; i < sd->effect_pcount1; i++)
5341 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5342 sd->effect_params1[i] / (1 << 16),
5343 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5344 av_log(avctx, AV_LOG_DEBUG, "\n");
5347 sd->effect_pcount2 = get_bits(gb, 16);
5348 if (sd->effect_pcount2 > 10) {
5349 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5350 return AVERROR_INVALIDDATA;
5351 } else if (sd->effect_pcount2) {
5353 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5354 while (++i < sd->effect_pcount2) {
5355 sd->effect_params2[i] = get_fp_val(gb);
5356 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5357 sd->effect_params2[i] / (1 << 16),
5358 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5360 av_log(avctx, AV_LOG_DEBUG, "\n");
5363 if (sd->effect_flag = get_bits1(gb))
5364 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5366 if (get_bits_count(gb) >= gb->size_in_bits +
5367 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
5368 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5369 return AVERROR_INVALIDDATA;
5371 if (get_bits_count(gb) < gb->size_in_bits - 8)
5372 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5377 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5379 int i, plane, row, sprite;
5380 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5381 uint8_t* src_h[2][2];
5382 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5384 MpegEncContext *s = &v->s;
5386 for (i = 0; i <= v->two_sprites; i++) {
5387 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5388 xadv[i] = sd->coefs[i][0];
5389 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5390 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5392 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5393 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5395 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5397 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5398 int width = v->output_width>>!!plane;
5400 for (row = 0; row < v->output_height>>!!plane; row++) {
5401 uint8_t *dst = v->sprite_output_frame->data[plane] +
5402 v->sprite_output_frame->linesize[plane] * row;
5404 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5405 uint8_t *iplane = s->current_picture.f->data[plane];
5406 int iline = s->current_picture.f->linesize[plane];
5407 int ycoord = yoff[sprite] + yadv[sprite] * row;
5408 int yline = ycoord >> 16;
5410 ysub[sprite] = ycoord & 0xFFFF;
5412 iplane = s->last_picture.f->data[plane];
5413 iline = s->last_picture.f->linesize[plane];
5415 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5416 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5417 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5419 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5421 if (sr_cache[sprite][0] != yline) {
5422 if (sr_cache[sprite][1] == yline) {
5423 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5424 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5426 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5427 sr_cache[sprite][0] = yline;
5430 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5431 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5432 iplane + next_line, xoff[sprite],
5433 xadv[sprite], width);
5434 sr_cache[sprite][1] = yline + 1;
5436 src_h[sprite][0] = v->sr_rows[sprite][0];
5437 src_h[sprite][1] = v->sr_rows[sprite][1];
5441 if (!v->two_sprites) {
5443 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5445 memcpy(dst, src_h[0][0], width);
5448 if (ysub[0] && ysub[1]) {
5449 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5450 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5451 } else if (ysub[0]) {
5452 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5453 src_h[1][0], alpha, width);
5454 } else if (ysub[1]) {
5455 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5456 src_h[0][0], (1<<16)-1-alpha, width);
5458 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5464 for (i = 0; i <= v->two_sprites; i++) {
5474 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5477 MpegEncContext *s = &v->s;
5478 AVCodecContext *avctx = s->avctx;
5481 memset(&sd, 0, sizeof(sd));
5483 ret = vc1_parse_sprites(v, gb, &sd);
5487 if (!s->current_picture.f || !s->current_picture.f->data[0]) {
5488 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5492 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
5493 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5497 av_frame_unref(v->sprite_output_frame);
5498 if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
5501 vc1_draw_sprites(v, &sd);
5506 static void vc1_sprite_flush(AVCodecContext *avctx)
5508 VC1Context *v = avctx->priv_data;
5509 MpegEncContext *s = &v->s;
5510 AVFrame *f = s->current_picture.f;
5513 /* Windows Media Image codecs have a convergence interval of two keyframes.
5514 Since we can't enforce it, clear to black the missing sprite. This is
5515 wrong but it looks better than doing nothing. */
5517 if (f && f->data[0])
5518 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5519 for (i = 0; i < v->sprite_height>>!!plane; i++)
5520 memset(f->data[plane] + i * f->linesize[plane],
5521 plane ? 128 : 0, f->linesize[plane]);
5526 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5528 MpegEncContext *s = &v->s;
5530 int mb_height = FFALIGN(s->mb_height, 2);
5532 /* Allocate mb bitplanes */
5533 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
5534 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
5535 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
5536 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
5537 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
5538 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
5540 v->n_allocated_blks = s->mb_width + 2;
5541 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5542 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5543 v->cbp = v->cbp_base + s->mb_stride;
5544 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5545 v->ttblk = v->ttblk_base + s->mb_stride;
5546 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5547 v->is_intra = v->is_intra_base + s->mb_stride;
5548 v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5549 v->luma_mv = v->luma_mv_base + s->mb_stride;
5551 /* allocate block type info in that way so it could be used with s->block_index[] */
5552 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5553 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5554 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
5555 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
5557 /* allocate memory to store block level MV info */
5558 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5559 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5560 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5561 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5562 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5563 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5564 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5565 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5567 /* Init coded blocks info */
5568 if (v->profile == PROFILE_ADVANCED) {
5569 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5571 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5575 ff_intrax8_common_init(&v->x8,s);
5577 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5578 for (i = 0; i < 4; i++)
5579 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
5580 return AVERROR(ENOMEM);
5583 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5584 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5586 av_freep(&v->mv_type_mb_plane);
5587 av_freep(&v->direct_mb_plane);
5588 av_freep(&v->acpred_plane);
5589 av_freep(&v->over_flags_plane);
5590 av_freep(&v->block);
5591 av_freep(&v->cbp_base);
5592 av_freep(&v->ttblk_base);
5593 av_freep(&v->is_intra_base);
5594 av_freep(&v->luma_mv_base);
5595 av_freep(&v->mb_type_base);
5596 return AVERROR(ENOMEM);
5602 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5605 for (i = 0; i < 64; i++) {
5606 #define transpose(x) (((x) >> 3) | (((x) & 7) << 3))
5607 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5608 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5609 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5610 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5611 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5617 /** Initialize a VC1/WMV3 decoder
5618 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5619 * @todo TODO: Decypher remaining bits in extra_data
5621 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5623 VC1Context *v = avctx->priv_data;
5624 MpegEncContext *s = &v->s;
5628 /* save the container output size for WMImage */
5629 v->output_width = avctx->width;
5630 v->output_height = avctx->height;
5632 if (!avctx->extradata_size || !avctx->extradata)
5634 if (!(avctx->flags & CODEC_FLAG_GRAY))
5635 avctx->pix_fmt = ff_get_format(avctx, avctx->codec->pix_fmts);
5637 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5640 if ((ret = ff_vc1_init_common(v)) < 0)
5642 // ensure static VLC tables are initialized
5643 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
5645 if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
5647 // Hack to ensure the above functions will be called
5648 // again once we know all necessary settings.
5649 // That this is necessary might indicate a bug.
5650 ff_vc1_decode_end(avctx);
5652 ff_blockdsp_init(&s->bdsp, avctx);
5653 ff_h264chroma_init(&v->h264chroma, 8);
5654 ff_qpeldsp_init(&s->qdsp);
5656 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5659 // looks like WMV3 has a sequence header stored in the extradata
5660 // advanced sequence header may be before the first frame
5661 // the last byte of the extradata is a version number, 1 for the
5662 // samples we can decode
5664 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5666 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
5669 count = avctx->extradata_size*8 - get_bits_count(&gb);
5671 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5672 count, get_bits(&gb, count));
5673 } else if (count < 0) {
5674 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5676 } else { // VC1/WVC1/WVP2
5677 const uint8_t *start = avctx->extradata;
5678 uint8_t *end = avctx->extradata + avctx->extradata_size;
5679 const uint8_t *next;
5680 int size, buf2_size;
5681 uint8_t *buf2 = NULL;
5682 int seq_initialized = 0, ep_initialized = 0;
5684 if (avctx->extradata_size < 16) {
5685 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5689 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5690 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5692 for (; next < end; start = next) {
5693 next = find_next_marker(start + 4, end);
5694 size = next - start - 4;
5697 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5698 init_get_bits(&gb, buf2, buf2_size * 8);
5699 switch (AV_RB32(start)) {
5700 case VC1_CODE_SEQHDR:
5701 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
5705 seq_initialized = 1;
5707 case VC1_CODE_ENTRYPOINT:
5708 if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
5717 if (!seq_initialized || !ep_initialized) {
5718 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5721 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5724 v->sprite_output_frame = av_frame_alloc();
5725 if (!v->sprite_output_frame)
5726 return AVERROR(ENOMEM);
5728 avctx->profile = v->profile;
5729 if (v->profile == PROFILE_ADVANCED)
5730 avctx->level = v->level;
5732 avctx->has_b_frames = !!avctx->max_b_frames;
5734 s->mb_width = (avctx->coded_width + 15) >> 4;
5735 s->mb_height = (avctx->coded_height + 15) >> 4;
5737 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5738 ff_vc1_init_transposed_scantables(v);
5740 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5745 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5746 v->sprite_width = avctx->coded_width;
5747 v->sprite_height = avctx->coded_height;
5749 avctx->coded_width = avctx->width = v->output_width;
5750 avctx->coded_height = avctx->height = v->output_height;
5752 // prevent 16.16 overflows
5753 if (v->sprite_width > 1 << 14 ||
5754 v->sprite_height > 1 << 14 ||
5755 v->output_width > 1 << 14 ||
5756 v->output_height > 1 << 14) return -1;
5758 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5759 avpriv_request_sample(avctx, "odd sprites support");
5760 return AVERROR_PATCHWELCOME;
5766 /** Close a VC1/WMV3 decoder
5767 * @warning Initial try at using MpegEncContext stuff
5769 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5771 VC1Context *v = avctx->priv_data;
5774 av_frame_free(&v->sprite_output_frame);
5776 for (i = 0; i < 4; i++)
5777 av_freep(&v->sr_rows[i >> 1][i & 1]);
5778 av_freep(&v->hrd_rate);
5779 av_freep(&v->hrd_buffer);
5780 ff_mpv_common_end(&v->s);
5781 av_freep(&v->mv_type_mb_plane);
5782 av_freep(&v->direct_mb_plane);
5783 av_freep(&v->forward_mb_plane);
5784 av_freep(&v->fieldtx_plane);
5785 av_freep(&v->acpred_plane);
5786 av_freep(&v->over_flags_plane);
5787 av_freep(&v->mb_type_base);
5788 av_freep(&v->blk_mv_type_base);
5789 av_freep(&v->mv_f_base);
5790 av_freep(&v->mv_f_next_base);
5791 av_freep(&v->block);
5792 av_freep(&v->cbp_base);
5793 av_freep(&v->ttblk_base);
5794 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5795 av_freep(&v->luma_mv_base);
5796 ff_intrax8_common_end(&v->x8);
5801 /** Decode a VC1/WMV3 frame
5802 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5804 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5805 int *got_frame, AVPacket *avpkt)
5807 const uint8_t *buf = avpkt->data;
5808 int buf_size = avpkt->size, n_slices = 0, i, ret;
5809 VC1Context *v = avctx->priv_data;
5810 MpegEncContext *s = &v->s;
5811 AVFrame *pict = data;
5812 uint8_t *buf2 = NULL;
5813 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5814 int mb_height, n_slices1=-1;
5819 } *slices = NULL, *tmp;
5821 v->second_field = 0;
5823 if(s->flags & CODEC_FLAG_LOW_DELAY)
5826 /* no supplementary picture */
5827 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5828 /* special case for last picture */
5829 if (s->low_delay == 0 && s->next_picture_ptr) {
5830 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
5832 s->next_picture_ptr = NULL;
5840 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5841 if (v->profile < PROFILE_ADVANCED)
5842 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5844 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5847 //for advanced profile we may need to parse and unescape data
5848 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5850 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5852 return AVERROR(ENOMEM);
5854 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5855 const uint8_t *start, *end, *next;
5859 for (start = buf, end = buf + buf_size; next < end; start = next) {
5860 next = find_next_marker(start + 4, end);
5861 size = next - start - 4;
5862 if (size <= 0) continue;
5863 switch (AV_RB32(start)) {
5864 case VC1_CODE_FRAME:
5865 if (avctx->hwaccel ||
5866 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5868 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5870 case VC1_CODE_FIELD: {
5872 if (avctx->hwaccel ||
5873 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5874 buf_start_second_field = start;
5875 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5879 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5880 if (!slices[n_slices].buf)
5882 buf_size3 = vc1_unescape_buffer(start + 4, size,
5883 slices[n_slices].buf);
5884 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5886 /* assuming that the field marker is at the exact middle,
5887 hope it's correct */
5888 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5889 n_slices1 = n_slices - 1; // index of the last slice of the first field
5893 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5894 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5895 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5896 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5898 case VC1_CODE_SLICE: {
5900 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5904 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5905 if (!slices[n_slices].buf)
5907 buf_size3 = vc1_unescape_buffer(start + 4, size,
5908 slices[n_slices].buf);
5909 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5911 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5917 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5918 const uint8_t *divider;
5921 divider = find_next_marker(buf, buf + buf_size);
5922 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5923 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5925 } else { // found field marker, unescape second field
5926 if (avctx->hwaccel ||
5927 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5928 buf_start_second_field = divider;
5929 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5933 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5934 if (!slices[n_slices].buf)
5936 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5937 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5939 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5940 n_slices1 = n_slices - 1;
5943 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5945 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5947 init_get_bits(&s->gb, buf2, buf_size2*8);
5949 init_get_bits(&s->gb, buf, buf_size*8);
5951 if (v->res_sprite) {
5952 v->new_sprite = !get_bits1(&s->gb);
5953 v->two_sprites = get_bits1(&s->gb);
5954 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5955 we're using the sprite compositor. These are intentionally kept separate
5956 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5957 the vc1 one for WVP2 */
5958 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5959 if (v->new_sprite) {
5960 // switch AVCodecContext parameters to those of the sprites
5961 avctx->width = avctx->coded_width = v->sprite_width;
5962 avctx->height = avctx->coded_height = v->sprite_height;
5969 if (s->context_initialized &&
5970 (s->width != avctx->coded_width ||
5971 s->height != avctx->coded_height)) {
5972 ff_vc1_decode_end(avctx);
5975 if (!s->context_initialized) {
5976 if (ff_msmpeg4_decode_init(avctx) < 0)
5978 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
5979 ff_mpv_common_end(s);
5983 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5985 if (v->profile == PROFILE_ADVANCED) {
5986 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5988 s->h_edge_pos = avctx->coded_width;
5989 s->v_edge_pos = avctx->coded_height;
5993 // do parse frame header
5994 v->pic_header_flag = 0;
5995 v->first_pic_header_flag = 1;
5996 if (v->profile < PROFILE_ADVANCED) {
5997 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
6001 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6005 v->first_pic_header_flag = 0;
6007 if (avctx->debug & FF_DEBUG_PICT_INFO)
6008 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
6010 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
6011 && s->pict_type != AV_PICTURE_TYPE_I) {
6012 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
6016 if ((s->mb_height >> v->field_mode) == 0) {
6017 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
6021 // for skipping the frame
6022 s->current_picture.f->pict_type = s->pict_type;
6023 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
6025 /* skip B-frames if we don't have reference frames */
6026 if (!s->last_picture_ptr && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
6027 av_log(v->s.avctx, AV_LOG_DEBUG, "Skipping B frame without reference frames\n");
6030 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
6031 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
6032 avctx->skip_frame >= AVDISCARD_ALL) {
6036 if (s->next_p_frame_damaged) {
6037 if (s->pict_type == AV_PICTURE_TYPE_B)
6040 s->next_p_frame_damaged = 0;
6043 if (ff_mpv_frame_start(s, avctx) < 0) {
6047 v->s.current_picture_ptr->field_picture = v->field_mode;
6048 v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
6049 v->s.current_picture_ptr->f->top_field_first = v->tff;
6051 // process pulldown flags
6052 s->current_picture_ptr->f->repeat_pict = 0;
6053 // Pulldown flags are only valid when 'broadcast' has been set.
6054 // So ticks_per_frame will be 2
6057 s->current_picture_ptr->f->repeat_pict = 1;
6058 } else if (v->rptfrm) {
6060 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
6063 s->me.qpel_put = s->qdsp.put_qpel_pixels_tab;
6064 s->me.qpel_avg = s->qdsp.avg_qpel_pixels_tab;
6066 if ((CONFIG_VC1_VDPAU_DECODER)
6067 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6068 if (v->field_mode && buf_start_second_field) {
6069 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6070 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6072 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6074 } else if (avctx->hwaccel) {
6075 if (v->field_mode && buf_start_second_field) {
6076 // decode first field
6077 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6078 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6080 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6082 if (avctx->hwaccel->end_frame(avctx) < 0)
6085 // decode second field
6086 s->gb = slices[n_slices1 + 1].gb;
6087 s->picture_structure = PICT_TOP_FIELD + v->tff;
6088 v->second_field = 1;
6089 v->pic_header_flag = 0;
6090 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6091 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6094 v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
6096 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6098 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6100 if (avctx->hwaccel->end_frame(avctx) < 0)
6103 s->picture_structure = PICT_FRAME;
6104 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6106 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6108 if (avctx->hwaccel->end_frame(avctx) < 0)
6114 ff_mpeg_er_frame_start(s);
6116 v->bits = buf_size * 8;
6117 v->end_mb_x = s->mb_width;
6118 if (v->field_mode) {
6119 s->current_picture.f->linesize[0] <<= 1;
6120 s->current_picture.f->linesize[1] <<= 1;
6121 s->current_picture.f->linesize[2] <<= 1;
6123 s->uvlinesize <<= 1;
6125 mb_height = s->mb_height >> v->field_mode;
6127 av_assert0 (mb_height > 0);
6129 for (i = 0; i <= n_slices; i++) {
6130 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6131 if (v->field_mode <= 0) {
6132 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6133 "picture boundary (%d >= %d)\n", i,
6134 slices[i - 1].mby_start, mb_height);
6137 v->second_field = 1;
6138 av_assert0((s->mb_height & 1) == 0);
6139 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6140 v->mb_off = s->mb_stride * s->mb_height >> 1;
6142 v->second_field = 0;
6147 v->pic_header_flag = 0;
6148 if (v->field_mode && i == n_slices1 + 2) {
6149 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6150 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6151 if (avctx->err_recognition & AV_EF_EXPLODE)
6155 } else if (get_bits1(&s->gb)) {
6156 v->pic_header_flag = 1;
6157 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6158 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6159 if (avctx->err_recognition & AV_EF_EXPLODE)
6167 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6168 if (!v->field_mode || v->second_field)
6169 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6171 if (i >= n_slices) {
6172 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6175 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6177 if (s->end_mb_y <= s->start_mb_y) {
6178 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6181 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6182 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6185 ff_vc1_decode_blocks(v);
6187 s->gb = slices[i].gb;
6189 if (v->field_mode) {
6190 v->second_field = 0;
6191 s->current_picture.f->linesize[0] >>= 1;
6192 s->current_picture.f->linesize[1] >>= 1;
6193 s->current_picture.f->linesize[2] >>= 1;
6195 s->uvlinesize >>= 1;
6196 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6197 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6198 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6201 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6202 get_bits_count(&s->gb), s->gb.size_in_bits);
6203 // if (get_bits_count(&s->gb) > buf_size * 8)
6205 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6208 ff_er_frame_end(&s->er);
6211 ff_mpv_frame_end(s);
6213 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6215 avctx->width = avctx->coded_width = v->output_width;
6216 avctx->height = avctx->coded_height = v->output_height;
6217 if (avctx->skip_frame >= AVDISCARD_NONREF)
6219 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6220 if (vc1_decode_sprites(v, &s->gb))
6223 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
6227 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6228 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
6230 ff_print_debug_info(s, s->current_picture_ptr, pict);
6232 } else if (s->last_picture_ptr) {
6233 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
6235 ff_print_debug_info(s, s->last_picture_ptr, pict);
6242 for (i = 0; i < n_slices; i++)
6243 av_free(slices[i].buf);
6249 for (i = 0; i < n_slices; i++)
6250 av_free(slices[i].buf);
6256 static const AVProfile profiles[] = {
6257 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6258 { FF_PROFILE_VC1_MAIN, "Main" },
6259 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6260 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6261 { FF_PROFILE_UNKNOWN },
6264 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6265 #if CONFIG_VC1_DXVA2_HWACCEL
6266 AV_PIX_FMT_DXVA2_VLD,
6268 #if CONFIG_VC1_VAAPI_HWACCEL
6269 AV_PIX_FMT_VAAPI_VLD,
6271 #if CONFIG_VC1_VDPAU_HWACCEL
6278 AVCodec ff_vc1_decoder = {
6280 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6281 .type = AVMEDIA_TYPE_VIDEO,
6282 .id = AV_CODEC_ID_VC1,
6283 .priv_data_size = sizeof(VC1Context),
6284 .init = vc1_decode_init,
6285 .close = ff_vc1_decode_end,
6286 .decode = vc1_decode_frame,
6287 .flush = ff_mpeg_flush,
6288 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6289 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6290 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6293 #if CONFIG_WMV3_DECODER
6294 AVCodec ff_wmv3_decoder = {
6296 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6297 .type = AVMEDIA_TYPE_VIDEO,
6298 .id = AV_CODEC_ID_WMV3,
6299 .priv_data_size = sizeof(VC1Context),
6300 .init = vc1_decode_init,
6301 .close = ff_vc1_decode_end,
6302 .decode = vc1_decode_frame,
6303 .flush = ff_mpeg_flush,
6304 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6305 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6306 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6310 #if CONFIG_WMV3_VDPAU_DECODER
6311 AVCodec ff_wmv3_vdpau_decoder = {
6312 .name = "wmv3_vdpau",
6313 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6314 .type = AVMEDIA_TYPE_VIDEO,
6315 .id = AV_CODEC_ID_WMV3,
6316 .priv_data_size = sizeof(VC1Context),
6317 .init = vc1_decode_init,
6318 .close = ff_vc1_decode_end,
6319 .decode = vc1_decode_frame,
6320 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6321 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6322 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6326 #if CONFIG_VC1_VDPAU_DECODER
6327 AVCodec ff_vc1_vdpau_decoder = {
6328 .name = "vc1_vdpau",
6329 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6330 .type = AVMEDIA_TYPE_VIDEO,
6331 .id = AV_CODEC_ID_VC1,
6332 .priv_data_size = sizeof(VC1Context),
6333 .init = vc1_decode_init,
6334 .close = ff_vc1_decode_end,
6335 .decode = vc1_decode_frame,
6336 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6337 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6338 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6342 #if CONFIG_WMV3IMAGE_DECODER
6343 AVCodec ff_wmv3image_decoder = {
6344 .name = "wmv3image",
6345 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6346 .type = AVMEDIA_TYPE_VIDEO,
6347 .id = AV_CODEC_ID_WMV3IMAGE,
6348 .priv_data_size = sizeof(VC1Context),
6349 .init = vc1_decode_init,
6350 .close = ff_vc1_decode_end,
6351 .decode = vc1_decode_frame,
6352 .capabilities = CODEC_CAP_DR1,
6353 .flush = vc1_sprite_flush,
6354 .pix_fmts = (const enum AVPixelFormat[]) {
6361 #if CONFIG_VC1IMAGE_DECODER
6362 AVCodec ff_vc1image_decoder = {
6364 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6365 .type = AVMEDIA_TYPE_VIDEO,
6366 .id = AV_CODEC_ID_VC1IMAGE,
6367 .priv_data_size = sizeof(VC1Context),
6368 .init = vc1_decode_init,
6369 .close = ff_vc1_decode_end,
6370 .decode = vc1_decode_frame,
6371 .capabilities = CODEC_CAP_DR1,
6372 .flush = vc1_sprite_flush,
6373 .pix_fmts = (const enum AVPixelFormat[]) {