2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
31 #include "error_resilience.h"
32 #include "mpegutils.h"
33 #include "mpegvideo.h"
35 #include "h264chroma.h"
38 #include "vc1acdata.h"
39 #include "msmpeg4data.h"
42 #include "vdpau_internal.h"
43 #include "libavutil/avassert.h"
48 #define MB_INTRA_VLC_BITS 9
52 // offset tables for interlaced picture MVDATA decoding
53 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
54 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
56 /***********************************************************************/
58 * @name VC-1 Bitplane decoding
64 static void init_block_index(VC1Context *v)
66 MpegEncContext *s = &v->s;
67 ff_init_block_index(s);
68 if (v->field_mode && !(v->second_field ^ v->tff)) {
69 s->dest[0] += s->current_picture_ptr->f->linesize[0];
70 s->dest[1] += s->current_picture_ptr->f->linesize[1];
71 s->dest[2] += s->current_picture_ptr->f->linesize[2];
75 /** @} */ //Bitplane group
77 static void vc1_put_signed_blocks_clamped(VC1Context *v)
79 MpegEncContext *s = &v->s;
80 int topleft_mb_pos, top_mb_pos;
81 int stride_y, fieldtx = 0;
84 /* The put pixels loop is always one MB row behind the decoding loop,
85 * because we can only put pixels when overlap filtering is done, and
86 * for filtering of the bottom edge of a MB, we need the next MB row
88 * Within the row, the put pixels loop is also one MB col behind the
89 * decoding loop. The reason for this is again, because for filtering
90 * of the right MB edge, we need the next MB present. */
91 if (!s->first_slice_line) {
93 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
94 if (v->fcm == ILACE_FRAME)
95 fieldtx = v->fieldtx_plane[topleft_mb_pos];
96 stride_y = s->linesize << fieldtx;
97 v_dist = (16 - fieldtx) >> (fieldtx == 0);
98 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
99 s->dest[0] - 16 * s->linesize - 16,
101 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
102 s->dest[0] - 16 * s->linesize - 8,
104 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
105 s->dest[0] - v_dist * s->linesize - 16,
107 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
108 s->dest[0] - v_dist * s->linesize - 8,
110 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
111 s->dest[1] - 8 * s->uvlinesize - 8,
113 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
114 s->dest[2] - 8 * s->uvlinesize - 8,
117 if (s->mb_x == s->mb_width - 1) {
118 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
119 if (v->fcm == ILACE_FRAME)
120 fieldtx = v->fieldtx_plane[top_mb_pos];
121 stride_y = s->linesize << fieldtx;
122 v_dist = fieldtx ? 15 : 8;
123 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
124 s->dest[0] - 16 * s->linesize,
126 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
127 s->dest[0] - 16 * s->linesize + 8,
129 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
130 s->dest[0] - v_dist * s->linesize,
132 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
133 s->dest[0] - v_dist * s->linesize + 8,
135 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
136 s->dest[1] - 8 * s->uvlinesize,
138 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
139 s->dest[2] - 8 * s->uvlinesize,
144 #define inc_blk_idx(idx) do { \
146 if (idx >= v->n_allocated_blks) \
150 inc_blk_idx(v->topleft_blk_idx);
151 inc_blk_idx(v->top_blk_idx);
152 inc_blk_idx(v->left_blk_idx);
153 inc_blk_idx(v->cur_blk_idx);
156 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
158 MpegEncContext *s = &v->s;
160 if (!s->first_slice_line) {
161 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
163 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
164 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
165 for (j = 0; j < 2; j++) {
166 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
168 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
171 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
173 if (s->mb_y == s->end_mb_y - 1) {
175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
176 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
177 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
179 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
183 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
185 MpegEncContext *s = &v->s;
188 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
189 * means it runs two rows/cols behind the decoding loop. */
190 if (!s->first_slice_line) {
192 if (s->mb_y >= s->start_mb_y + 2) {
193 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
196 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
197 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
198 for (j = 0; j < 2; j++) {
199 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
201 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
205 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
208 if (s->mb_x == s->mb_width - 1) {
209 if (s->mb_y >= s->start_mb_y + 2) {
210 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
213 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
214 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
215 for (j = 0; j < 2; j++) {
216 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
218 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
222 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
225 if (s->mb_y == s->end_mb_y) {
228 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
229 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
231 for (j = 0; j < 2; j++) {
232 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
237 if (s->mb_x == s->mb_width - 1) {
239 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
242 for (j = 0; j < 2; j++) {
243 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
251 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
253 MpegEncContext *s = &v->s;
256 if (v->condover == CONDOVER_NONE)
259 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
261 /* Within a MB, the horizontal overlap always runs before the vertical.
262 * To accomplish that, we run the H on left and internal borders of the
263 * currently decoded MB. Then, we wait for the next overlap iteration
264 * to do H overlap on the right edge of this MB, before moving over and
265 * running the V overlap. Therefore, the V overlap makes us trail by one
266 * MB col and the H overlap filter makes us trail by one MB row. This
267 * is reflected in the time at which we run the put_pixels loop. */
268 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
269 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
270 v->over_flags_plane[mb_pos - 1])) {
271 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
272 v->block[v->cur_blk_idx][0]);
273 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
274 v->block[v->cur_blk_idx][2]);
275 if (!(s->flags & CODEC_FLAG_GRAY)) {
276 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
277 v->block[v->cur_blk_idx][4]);
278 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
279 v->block[v->cur_blk_idx][5]);
282 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
283 v->block[v->cur_blk_idx][1]);
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
285 v->block[v->cur_blk_idx][3]);
287 if (s->mb_x == s->mb_width - 1) {
288 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
289 v->over_flags_plane[mb_pos - s->mb_stride])) {
290 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
291 v->block[v->cur_blk_idx][0]);
292 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
293 v->block[v->cur_blk_idx][1]);
294 if (!(s->flags & CODEC_FLAG_GRAY)) {
295 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
296 v->block[v->cur_blk_idx][4]);
297 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
298 v->block[v->cur_blk_idx][5]);
301 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
302 v->block[v->cur_blk_idx][2]);
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
304 v->block[v->cur_blk_idx][3]);
307 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
308 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
309 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
310 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
311 v->block[v->left_blk_idx][0]);
312 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
313 v->block[v->left_blk_idx][1]);
314 if (!(s->flags & CODEC_FLAG_GRAY)) {
315 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
316 v->block[v->left_blk_idx][4]);
317 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
318 v->block[v->left_blk_idx][5]);
321 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
322 v->block[v->left_blk_idx][2]);
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
324 v->block[v->left_blk_idx][3]);
328 /** Do motion compensation over 1 macroblock
329 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
331 static void vc1_mc_1mv(VC1Context *v, int dir)
333 MpegEncContext *s = &v->s;
334 H264ChromaContext *h264chroma = &v->h264chroma;
335 uint8_t *srcY, *srcU, *srcV;
336 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
337 int v_edge_pos = s->v_edge_pos >> v->field_mode;
339 uint8_t (*luty)[256], (*lutuv)[256];
342 if ((!v->field_mode ||
343 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
344 !v->s.last_picture.f->data[0])
347 mx = s->mv[dir][0][0];
348 my = s->mv[dir][0][1];
350 // store motion vectors for further use in B frames
351 if (s->pict_type == AV_PICTURE_TYPE_P) {
352 for (i = 0; i < 4; i++) {
353 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
354 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
358 uvmx = (mx + ((mx & 3) == 3)) >> 1;
359 uvmy = (my + ((my & 3) == 3)) >> 1;
360 v->luma_mv[s->mb_x][0] = uvmx;
361 v->luma_mv[s->mb_x][1] = uvmy;
364 v->cur_field_type != v->ref_field_type[dir]) {
365 my = my - 2 + 4 * v->cur_field_type;
366 uvmy = uvmy - 2 + 4 * v->cur_field_type;
369 // fastuvmc shall be ignored for interlaced frame picture
370 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
371 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
372 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
375 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
376 srcY = s->current_picture.f->data[0];
377 srcU = s->current_picture.f->data[1];
378 srcV = s->current_picture.f->data[2];
380 lutuv = v->curr_lutuv;
381 use_ic = *v->curr_use_ic;
383 srcY = s->last_picture.f->data[0];
384 srcU = s->last_picture.f->data[1];
385 srcV = s->last_picture.f->data[2];
387 lutuv = v->last_lutuv;
388 use_ic = v->last_use_ic;
391 srcY = s->next_picture.f->data[0];
392 srcU = s->next_picture.f->data[1];
393 srcV = s->next_picture.f->data[2];
395 lutuv = v->next_lutuv;
396 use_ic = v->next_use_ic;
399 if (!srcY || !srcU) {
400 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
404 src_x = s->mb_x * 16 + (mx >> 2);
405 src_y = s->mb_y * 16 + (my >> 2);
406 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
407 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
409 if (v->profile != PROFILE_ADVANCED) {
410 src_x = av_clip( src_x, -16, s->mb_width * 16);
411 src_y = av_clip( src_y, -16, s->mb_height * 16);
412 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
413 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
415 src_x = av_clip( src_x, -17, s->avctx->coded_width);
416 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
417 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
418 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
421 srcY += src_y * s->linesize + src_x;
422 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
423 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
425 if (v->field_mode && v->ref_field_type[dir]) {
426 srcY += s->current_picture_ptr->f->linesize[0];
427 srcU += s->current_picture_ptr->f->linesize[1];
428 srcV += s->current_picture_ptr->f->linesize[2];
431 /* for grayscale we should not try to read from unknown area */
432 if (s->flags & CODEC_FLAG_GRAY) {
433 srcU = s->edge_emu_buffer + 18 * s->linesize;
434 srcV = s->edge_emu_buffer + 18 * s->linesize;
437 if (v->rangeredfrm || use_ic
438 || s->h_edge_pos < 22 || v_edge_pos < 22
439 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
440 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
441 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
443 srcY -= s->mspel * (1 + s->linesize);
444 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
445 s->linesize, s->linesize,
446 17 + s->mspel * 2, 17 + s->mspel * 2,
447 src_x - s->mspel, src_y - s->mspel,
448 s->h_edge_pos, v_edge_pos);
449 srcY = s->edge_emu_buffer;
450 s->vdsp.emulated_edge_mc(uvbuf, srcU,
451 s->uvlinesize, s->uvlinesize,
454 s->h_edge_pos >> 1, v_edge_pos >> 1);
455 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV,
456 s->uvlinesize, s->uvlinesize,
459 s->h_edge_pos >> 1, v_edge_pos >> 1);
462 /* if we deal with range reduction we need to scale source blocks */
463 if (v->rangeredfrm) {
468 for (j = 0; j < 17 + s->mspel * 2; j++) {
469 for (i = 0; i < 17 + s->mspel * 2; i++)
470 src[i] = ((src[i] - 128) >> 1) + 128;
475 for (j = 0; j < 9; j++) {
476 for (i = 0; i < 9; i++) {
477 src[i] = ((src[i] - 128) >> 1) + 128;
478 src2[i] = ((src2[i] - 128) >> 1) + 128;
480 src += s->uvlinesize;
481 src2 += s->uvlinesize;
484 /* if we deal with intensity compensation we need to scale source blocks */
490 for (j = 0; j < 17 + s->mspel * 2; j++) {
491 int f = v->field_mode ? v->ref_field_type[dir] : ((j + src_y - s->mspel) & 1) ;
492 for (i = 0; i < 17 + s->mspel * 2; i++)
493 src[i] = luty[f][src[i]];
498 for (j = 0; j < 9; j++) {
499 int f = v->field_mode ? v->ref_field_type[dir] : ((j + uvsrc_y) & 1);
500 for (i = 0; i < 9; i++) {
501 src[i] = lutuv[f][src[i]];
502 src2[i] = lutuv[f][src2[i]];
504 src += s->uvlinesize;
505 src2 += s->uvlinesize;
508 srcY += s->mspel * (1 + s->linesize);
512 dxy = ((my & 3) << 2) | (mx & 3);
513 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] , srcY , s->linesize, v->rnd);
514 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8, srcY + 8, s->linesize, v->rnd);
515 srcY += s->linesize * 8;
516 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize , srcY , s->linesize, v->rnd);
517 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
518 } else { // hpel mc - always used for luma
519 dxy = (my & 2) | ((mx & 2) >> 1);
521 s->hdsp.put_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
523 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0], srcY, s->linesize, 16);
526 if (s->flags & CODEC_FLAG_GRAY) return;
527 /* Chroma MC always uses qpel bilinear */
528 uvmx = (uvmx & 3) << 1;
529 uvmy = (uvmy & 3) << 1;
531 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
532 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
534 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
535 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
539 static inline int median4(int a, int b, int c, int d)
542 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
543 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
545 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
546 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
550 /** Do motion compensation for 4-MV macroblock - luminance block
552 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
554 MpegEncContext *s = &v->s;
556 int dxy, mx, my, src_x, src_y;
558 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
559 int v_edge_pos = s->v_edge_pos >> v->field_mode;
560 uint8_t (*luty)[256];
563 if ((!v->field_mode ||
564 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
565 !v->s.last_picture.f->data[0])
568 mx = s->mv[dir][n][0];
569 my = s->mv[dir][n][1];
572 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
573 srcY = s->current_picture.f->data[0];
575 use_ic = *v->curr_use_ic;
577 srcY = s->last_picture.f->data[0];
579 use_ic = v->last_use_ic;
582 srcY = s->next_picture.f->data[0];
584 use_ic = v->next_use_ic;
588 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
593 if (v->cur_field_type != v->ref_field_type[dir])
594 my = my - 2 + 4 * v->cur_field_type;
597 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
598 int same_count = 0, opp_count = 0, k;
599 int chosen_mv[2][4][2], f;
601 for (k = 0; k < 4; k++) {
602 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
603 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
604 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
608 f = opp_count > same_count;
609 switch (f ? opp_count : same_count) {
611 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
612 chosen_mv[f][2][0], chosen_mv[f][3][0]);
613 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
614 chosen_mv[f][2][1], chosen_mv[f][3][1]);
617 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
618 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
621 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
622 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
627 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
628 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
629 for (k = 0; k < 4; k++)
630 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
633 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
635 int width = s->avctx->coded_width;
636 int height = s->avctx->coded_height >> 1;
637 if (s->pict_type == AV_PICTURE_TYPE_P) {
638 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
639 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
641 qx = (s->mb_x * 16) + (mx >> 2);
642 qy = (s->mb_y * 8) + (my >> 3);
647 mx -= 4 * (qx - width);
650 else if (qy > height + 1)
651 my -= 8 * (qy - height - 1);
654 if ((v->fcm == ILACE_FRAME) && fieldmv)
655 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
657 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
659 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
661 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
663 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
665 if (v->profile != PROFILE_ADVANCED) {
666 src_x = av_clip(src_x, -16, s->mb_width * 16);
667 src_y = av_clip(src_y, -16, s->mb_height * 16);
669 src_x = av_clip(src_x, -17, s->avctx->coded_width);
670 if (v->fcm == ILACE_FRAME) {
672 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
674 src_y = av_clip(src_y, -18, s->avctx->coded_height);
676 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
680 srcY += src_y * s->linesize + src_x;
681 if (v->field_mode && v->ref_field_type[dir])
682 srcY += s->current_picture_ptr->f->linesize[0];
684 if (fieldmv && !(src_y & 1))
686 if (fieldmv && (src_y & 1) && src_y < 4)
688 if (v->rangeredfrm || use_ic
689 || s->h_edge_pos < 13 || v_edge_pos < 23
690 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
691 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
692 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
693 /* check emulate edge stride and offset */
694 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
695 s->linesize, s->linesize,
696 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
697 src_x - s->mspel, src_y - (s->mspel << fieldmv),
698 s->h_edge_pos, v_edge_pos);
699 srcY = s->edge_emu_buffer;
700 /* if we deal with range reduction we need to scale source blocks */
701 if (v->rangeredfrm) {
706 for (j = 0; j < 9 + s->mspel * 2; j++) {
707 for (i = 0; i < 9 + s->mspel * 2; i++)
708 src[i] = ((src[i] - 128) >> 1) + 128;
709 src += s->linesize << fieldmv;
712 /* if we deal with intensity compensation we need to scale source blocks */
718 for (j = 0; j < 9 + s->mspel * 2; j++) {
719 int f = v->field_mode ? v->ref_field_type[dir] : (((j<<fieldmv)+src_y - (s->mspel << fieldmv)) & 1);
720 for (i = 0; i < 9 + s->mspel * 2; i++)
721 src[i] = luty[f][src[i]];
722 src += s->linesize << fieldmv;
725 srcY += s->mspel * (1 + (s->linesize << fieldmv));
729 dxy = ((my & 3) << 2) | (mx & 3);
731 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
733 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
734 } else { // hpel mc - always used for luma
735 dxy = (my & 2) | ((mx & 2) >> 1);
737 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
739 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
743 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
746 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
748 idx = ((a[3] != flag) << 3)
749 | ((a[2] != flag) << 2)
750 | ((a[1] != flag) << 1)
753 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
754 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
756 } else if (count[idx] == 1) {
759 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
760 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
763 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
764 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
767 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
768 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
771 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
772 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
775 } else if (count[idx] == 2) {
777 for (i = 0; i < 3; i++)
782 for (i = t1 + 1; i < 4; i++)
787 *tx = (mvx[t1] + mvx[t2]) / 2;
788 *ty = (mvy[t1] + mvy[t2]) / 2;
796 /** Do motion compensation for 4-MV macroblock - both chroma blocks
798 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
800 MpegEncContext *s = &v->s;
801 H264ChromaContext *h264chroma = &v->h264chroma;
802 uint8_t *srcU, *srcV;
803 int uvmx, uvmy, uvsrc_x, uvsrc_y;
804 int k, tx = 0, ty = 0;
805 int mvx[4], mvy[4], intra[4], mv_f[4];
807 int chroma_ref_type = v->cur_field_type;
808 int v_edge_pos = s->v_edge_pos >> v->field_mode;
809 uint8_t (*lutuv)[256];
812 if (!v->field_mode && !v->s.last_picture.f->data[0])
814 if (s->flags & CODEC_FLAG_GRAY)
817 for (k = 0; k < 4; k++) {
818 mvx[k] = s->mv[dir][k][0];
819 mvy[k] = s->mv[dir][k][1];
820 intra[k] = v->mb_type[0][s->block_index[k]];
822 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
825 /* calculate chroma MV vector from four luma MVs */
826 if (!v->field_mode || (v->field_mode && !v->numref)) {
827 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
828 chroma_ref_type = v->reffield;
830 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
831 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
832 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
833 return; //no need to do MC for intra blocks
837 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
839 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
841 chroma_ref_type = !v->cur_field_type;
843 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f->data[0])
845 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
846 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
847 uvmx = (tx + ((tx & 3) == 3)) >> 1;
848 uvmy = (ty + ((ty & 3) == 3)) >> 1;
850 v->luma_mv[s->mb_x][0] = uvmx;
851 v->luma_mv[s->mb_x][1] = uvmy;
854 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
855 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
857 // Field conversion bias
858 if (v->cur_field_type != chroma_ref_type)
859 uvmy += 2 - 4 * chroma_ref_type;
861 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
862 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
864 if (v->profile != PROFILE_ADVANCED) {
865 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
866 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
868 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
869 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
873 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
874 srcU = s->current_picture.f->data[1];
875 srcV = s->current_picture.f->data[2];
876 lutuv = v->curr_lutuv;
877 use_ic = *v->curr_use_ic;
879 srcU = s->last_picture.f->data[1];
880 srcV = s->last_picture.f->data[2];
881 lutuv = v->last_lutuv;
882 use_ic = v->last_use_ic;
885 srcU = s->next_picture.f->data[1];
886 srcV = s->next_picture.f->data[2];
887 lutuv = v->next_lutuv;
888 use_ic = v->next_use_ic;
892 av_log(v->s.avctx, AV_LOG_ERROR, "Referenced frame missing.\n");
896 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
897 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
900 if (chroma_ref_type) {
901 srcU += s->current_picture_ptr->f->linesize[1];
902 srcV += s->current_picture_ptr->f->linesize[2];
906 if (v->rangeredfrm || use_ic
907 || s->h_edge_pos < 18 || v_edge_pos < 18
908 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
909 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
910 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
911 s->uvlinesize, s->uvlinesize,
912 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
913 s->h_edge_pos >> 1, v_edge_pos >> 1);
914 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
915 s->uvlinesize, s->uvlinesize,
916 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
917 s->h_edge_pos >> 1, v_edge_pos >> 1);
918 srcU = s->edge_emu_buffer;
919 srcV = s->edge_emu_buffer + 16;
921 /* if we deal with range reduction we need to scale source blocks */
922 if (v->rangeredfrm) {
928 for (j = 0; j < 9; j++) {
929 for (i = 0; i < 9; i++) {
930 src[i] = ((src[i] - 128) >> 1) + 128;
931 src2[i] = ((src2[i] - 128) >> 1) + 128;
933 src += s->uvlinesize;
934 src2 += s->uvlinesize;
937 /* if we deal with intensity compensation we need to scale source blocks */
944 for (j = 0; j < 9; j++) {
945 int f = v->field_mode ? chroma_ref_type : ((j + uvsrc_y) & 1);
946 for (i = 0; i < 9; i++) {
947 src[i] = lutuv[f][src[i]];
948 src2[i] = lutuv[f][src2[i]];
950 src += s->uvlinesize;
951 src2 += s->uvlinesize;
956 /* Chroma MC always uses qpel bilinear */
957 uvmx = (uvmx & 3) << 1;
958 uvmy = (uvmy & 3) << 1;
960 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
961 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
963 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1], srcU, s->uvlinesize, 8, uvmx, uvmy);
964 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2], srcV, s->uvlinesize, 8, uvmx, uvmy);
968 /** Do motion compensation for 4-MV interlaced frame chroma macroblock (both U and V)
970 static void vc1_mc_4mv_chroma4(VC1Context *v, int dir, int dir2, int avg)
972 MpegEncContext *s = &v->s;
973 H264ChromaContext *h264chroma = &v->h264chroma;
974 uint8_t *srcU, *srcV;
975 int uvsrc_x, uvsrc_y;
976 int uvmx_field[4], uvmy_field[4];
978 int fieldmv = v->blk_mv_type[s->block_index[0]];
979 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
980 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
981 int v_edge_pos = s->v_edge_pos >> 1;
983 uint8_t (*lutuv)[256];
985 if (s->flags & CODEC_FLAG_GRAY)
988 for (i = 0; i < 4; i++) {
989 int d = i < 2 ? dir: dir2;
991 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
994 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
996 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
999 for (i = 0; i < 4; i++) {
1000 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
1001 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1002 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1003 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1004 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1005 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1006 if (i < 2 ? dir : dir2) {
1007 srcU = s->next_picture.f->data[1];
1008 srcV = s->next_picture.f->data[2];
1009 lutuv = v->next_lutuv;
1010 use_ic = v->next_use_ic;
1012 srcU = s->last_picture.f->data[1];
1013 srcV = s->last_picture.f->data[2];
1014 lutuv = v->last_lutuv;
1015 use_ic = v->last_use_ic;
1019 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1020 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1021 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1022 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1024 if (fieldmv && !(uvsrc_y & 1))
1025 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1027 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1030 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1031 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1032 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1033 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU,
1034 s->uvlinesize, s->uvlinesize,
1035 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1036 s->h_edge_pos >> 1, v_edge_pos);
1037 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV,
1038 s->uvlinesize, s->uvlinesize,
1039 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1040 s->h_edge_pos >> 1, v_edge_pos);
1041 srcU = s->edge_emu_buffer;
1042 srcV = s->edge_emu_buffer + 16;
1044 /* if we deal with intensity compensation we need to scale source blocks */
1047 uint8_t *src, *src2;
1051 for (j = 0; j < 5; j++) {
1052 int f = (uvsrc_y + (j << fieldmv)) & 1;
1053 for (i = 0; i < 5; i++) {
1054 src[i] = lutuv[f][src[i]];
1055 src2[i] = lutuv[f][src2[i]];
1057 src += s->uvlinesize << fieldmv;
1058 src2 += s->uvlinesize << fieldmv;
1064 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1065 h264chroma->avg_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1067 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1068 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1072 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1073 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1075 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1076 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1082 /***********************************************************************/
1084 * @name VC-1 Block-level functions
1085 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1091 * @brief Get macroblock-level quantizer scale
1093 #define GET_MQUANT() \
1094 if (v->dquantfrm) { \
1096 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1097 if (v->dqbilevel) { \
1098 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1100 mqdiff = get_bits(gb, 3); \
1102 mquant = v->pq + mqdiff; \
1104 mquant = get_bits(gb, 5); \
1107 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1108 edges = 1 << v->dqsbedge; \
1109 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1110 edges = (3 << v->dqsbedge) % 15; \
1111 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1113 if ((edges&1) && !s->mb_x) \
1114 mquant = v->altpq; \
1115 if ((edges&2) && s->first_slice_line) \
1116 mquant = v->altpq; \
1117 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1118 mquant = v->altpq; \
1119 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1120 mquant = v->altpq; \
1121 if (!mquant || mquant > 31) { \
1122 av_log(v->s.avctx, AV_LOG_ERROR, \
1123 "Overriding invalid mquant %d\n", mquant); \
1129 * @def GET_MVDATA(_dmv_x, _dmv_y)
1130 * @brief Get MV differentials
1131 * @see MVDATA decoding from 8.3.5.2, p(1)20
1132 * @param _dmv_x Horizontal differential for decoded MV
1133 * @param _dmv_y Vertical differential for decoded MV
1135 #define GET_MVDATA(_dmv_x, _dmv_y) \
1136 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1137 VC1_MV_DIFF_VLC_BITS, 2); \
1139 mb_has_coeffs = 1; \
1142 mb_has_coeffs = 0; \
1145 _dmv_x = _dmv_y = 0; \
1146 } else if (index == 35) { \
1147 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1148 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1149 } else if (index == 36) { \
1154 index1 = index % 6; \
1155 if (!s->quarter_sample && index1 == 5) val = 1; \
1157 if (size_table[index1] - val > 0) \
1158 val = get_bits(gb, size_table[index1] - val); \
1160 sign = 0 - (val&1); \
1161 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1163 index1 = index / 6; \
1164 if (!s->quarter_sample && index1 == 5) val = 1; \
1166 if (size_table[index1] - val > 0) \
1167 val = get_bits(gb, size_table[index1] - val); \
1169 sign = 0 - (val & 1); \
1170 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1173 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1174 int *dmv_y, int *pred_flag)
1177 int extend_x = 0, extend_y = 0;
1178 GetBitContext *gb = &v->s.gb;
1181 const int* offs_tab;
1184 bits = VC1_2REF_MVDATA_VLC_BITS;
1187 bits = VC1_1REF_MVDATA_VLC_BITS;
1190 switch (v->dmvrange) {
1198 extend_x = extend_y = 1;
1201 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1203 *dmv_x = get_bits(gb, v->k_x);
1204 *dmv_y = get_bits(gb, v->k_y);
1207 *pred_flag = *dmv_y & 1;
1208 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1210 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1215 av_assert0(index < esc);
1217 offs_tab = offset_table2;
1219 offs_tab = offset_table1;
1220 index1 = (index + 1) % 9;
1222 val = get_bits(gb, index1 + extend_x);
1223 sign = 0 -(val & 1);
1224 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1228 offs_tab = offset_table2;
1230 offs_tab = offset_table1;
1231 index1 = (index + 1) / 9;
1232 if (index1 > v->numref) {
1233 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1234 sign = 0 - (val & 1);
1235 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1238 if (v->numref && pred_flag)
1239 *pred_flag = index1 & 1;
1243 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1245 int scaledvalue, refdist;
1246 int scalesame1, scalesame2;
1247 int scalezone1_x, zone1offset_x;
1248 int table_index = dir ^ v->second_field;
1250 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1251 refdist = v->refdist;
1253 refdist = dir ? v->brfd : v->frfd;
1256 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1257 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1258 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1259 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1264 if (FFABS(n) < scalezone1_x)
1265 scaledvalue = (n * scalesame1) >> 8;
1268 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1270 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1273 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1276 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1278 int scaledvalue, refdist;
1279 int scalesame1, scalesame2;
1280 int scalezone1_y, zone1offset_y;
1281 int table_index = dir ^ v->second_field;
1283 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1284 refdist = v->refdist;
1286 refdist = dir ? v->brfd : v->frfd;
1289 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1290 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1291 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1292 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1297 if (FFABS(n) < scalezone1_y)
1298 scaledvalue = (n * scalesame1) >> 8;
1301 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1303 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1307 if (v->cur_field_type && !v->ref_field_type[dir])
1308 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1310 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1313 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1315 int scalezone1_x, zone1offset_x;
1316 int scaleopp1, scaleopp2, brfd;
1319 brfd = FFMIN(v->brfd, 3);
1320 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1321 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1322 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1323 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1328 if (FFABS(n) < scalezone1_x)
1329 scaledvalue = (n * scaleopp1) >> 8;
1332 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1334 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1337 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1340 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1342 int scalezone1_y, zone1offset_y;
1343 int scaleopp1, scaleopp2, brfd;
1346 brfd = FFMIN(v->brfd, 3);
1347 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1348 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1349 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1350 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1355 if (FFABS(n) < scalezone1_y)
1356 scaledvalue = (n * scaleopp1) >> 8;
1359 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1361 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1364 if (v->cur_field_type && !v->ref_field_type[dir]) {
1365 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1367 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1371 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1374 int brfd, scalesame;
1375 int hpel = 1 - v->s.quarter_sample;
1378 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1380 n = scaleforsame_y(v, i, n, dir) << hpel;
1382 n = scaleforsame_x(v, n, dir) << hpel;
1385 brfd = FFMIN(v->brfd, 3);
1386 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1388 n = (n * scalesame >> 8) << hpel;
1392 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1395 int refdist, scaleopp;
1396 int hpel = 1 - v->s.quarter_sample;
1399 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1401 n = scaleforopp_y(v, n, dir) << hpel;
1403 n = scaleforopp_x(v, n) << hpel;
1406 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1407 refdist = FFMIN(v->refdist, 3);
1409 refdist = dir ? v->brfd : v->frfd;
1410 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1412 n = (n * scaleopp >> 8) << hpel;
1416 /** Predict and set motion vector
1418 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1419 int mv1, int r_x, int r_y, uint8_t* is_intra,
1420 int pred_flag, int dir)
1422 MpegEncContext *s = &v->s;
1423 int xy, wrap, off = 0;
1427 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1428 int opposite, a_f, b_f, c_f;
1429 int16_t field_predA[2];
1430 int16_t field_predB[2];
1431 int16_t field_predC[2];
1432 int a_valid, b_valid, c_valid;
1433 int hybridmv_thresh, y_bias = 0;
1435 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1436 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1440 /* scale MV difference to be quad-pel */
1441 dmv_x <<= 1 - s->quarter_sample;
1442 dmv_y <<= 1 - s->quarter_sample;
1444 wrap = s->b8_stride;
1445 xy = s->block_index[n];
1448 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1449 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1450 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1451 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1452 if (mv1) { /* duplicate motion data for 1-MV block */
1453 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1454 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1455 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1456 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1457 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1458 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1459 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1460 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1461 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1462 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1463 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1464 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1465 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1470 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1471 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1473 if (v->field_mode && mixedmv_pic)
1474 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1476 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1478 //in 4-MV mode different blocks have different B predictor position
1481 off = (s->mb_x > 0) ? -1 : 1;
1484 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1493 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1495 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1496 b_valid = a_valid && (s->mb_width > 1);
1497 c_valid = s->mb_x || (n == 1 || n == 3);
1498 if (v->field_mode) {
1499 a_valid = a_valid && !is_intra[xy - wrap];
1500 b_valid = b_valid && !is_intra[xy - wrap + off];
1501 c_valid = c_valid && !is_intra[xy - 1];
1505 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1506 num_oppfield += a_f;
1507 num_samefield += 1 - a_f;
1508 field_predA[0] = A[0];
1509 field_predA[1] = A[1];
1511 field_predA[0] = field_predA[1] = 0;
1515 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1516 num_oppfield += b_f;
1517 num_samefield += 1 - b_f;
1518 field_predB[0] = B[0];
1519 field_predB[1] = B[1];
1521 field_predB[0] = field_predB[1] = 0;
1525 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1526 num_oppfield += c_f;
1527 num_samefield += 1 - c_f;
1528 field_predC[0] = C[0];
1529 field_predC[1] = C[1];
1531 field_predC[0] = field_predC[1] = 0;
1535 if (v->field_mode) {
1537 // REFFIELD determines if the last field or the second-last field is
1538 // to be used as reference
1539 opposite = 1 - v->reffield;
1541 if (num_samefield <= num_oppfield)
1542 opposite = 1 - pred_flag;
1544 opposite = pred_flag;
1549 if (a_valid && !a_f) {
1550 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1551 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1553 if (b_valid && !b_f) {
1554 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1555 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1557 if (c_valid && !c_f) {
1558 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1559 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1561 v->mv_f[dir][xy + v->blocks_off] = 1;
1562 v->ref_field_type[dir] = !v->cur_field_type;
1564 if (a_valid && a_f) {
1565 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1566 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1568 if (b_valid && b_f) {
1569 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1570 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1572 if (c_valid && c_f) {
1573 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1574 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1576 v->mv_f[dir][xy + v->blocks_off] = 0;
1577 v->ref_field_type[dir] = v->cur_field_type;
1581 px = field_predA[0];
1582 py = field_predA[1];
1583 } else if (c_valid) {
1584 px = field_predC[0];
1585 py = field_predC[1];
1586 } else if (b_valid) {
1587 px = field_predB[0];
1588 py = field_predB[1];
1594 if (num_samefield + num_oppfield > 1) {
1595 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1596 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1599 /* Pullback MV as specified in 8.3.5.3.4 */
1600 if (!v->field_mode) {
1602 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1603 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1604 X = (s->mb_width << 6) - 4;
1605 Y = (s->mb_height << 6) - 4;
1607 if (qx + px < -60) px = -60 - qx;
1608 if (qy + py < -60) py = -60 - qy;
1610 if (qx + px < -28) px = -28 - qx;
1611 if (qy + py < -28) py = -28 - qy;
1613 if (qx + px > X) px = X - qx;
1614 if (qy + py > Y) py = Y - qy;
1617 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1618 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1619 hybridmv_thresh = 32;
1620 if (a_valid && c_valid) {
1621 if (is_intra[xy - wrap])
1622 sum = FFABS(px) + FFABS(py);
1624 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1625 if (sum > hybridmv_thresh) {
1626 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1627 px = field_predA[0];
1628 py = field_predA[1];
1630 px = field_predC[0];
1631 py = field_predC[1];
1634 if (is_intra[xy - 1])
1635 sum = FFABS(px) + FFABS(py);
1637 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1638 if (sum > hybridmv_thresh) {
1639 if (get_bits1(&s->gb)) {
1640 px = field_predA[0];
1641 py = field_predA[1];
1643 px = field_predC[0];
1644 py = field_predC[1];
1651 if (v->field_mode && v->numref)
1653 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1655 /* store MV using signed modulus of MV range defined in 4.11 */
1656 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1657 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1658 if (mv1) { /* duplicate motion data for 1-MV block */
1659 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1660 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1661 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1662 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1663 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1664 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1665 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1666 v->mv_f[dir][xy + wrap + v->blocks_off] = v->mv_f[dir][xy + wrap + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1670 /** Predict and set motion vector for interlaced frame picture MBs
1672 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1673 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1675 MpegEncContext *s = &v->s;
1676 int xy, wrap, off = 0;
1677 int A[2], B[2], C[2];
1679 int a_valid = 0, b_valid = 0, c_valid = 0;
1680 int field_a, field_b, field_c; // 0: same, 1: opposit
1681 int total_valid, num_samefield, num_oppfield;
1682 int pos_c, pos_b, n_adj;
1684 wrap = s->b8_stride;
1685 xy = s->block_index[n];
1688 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1689 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1690 s->current_picture.motion_val[1][xy][0] = 0;
1691 s->current_picture.motion_val[1][xy][1] = 0;
1692 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1693 s->current_picture.motion_val[0][xy + 1][0] = 0;
1694 s->current_picture.motion_val[0][xy + 1][1] = 0;
1695 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1696 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1697 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1698 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1699 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1700 s->current_picture.motion_val[1][xy + 1][0] = 0;
1701 s->current_picture.motion_val[1][xy + 1][1] = 0;
1702 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1703 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1704 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1705 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1710 off = ((n == 0) || (n == 1)) ? 1 : -1;
1712 if (s->mb_x || (n == 1) || (n == 3)) {
1713 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1714 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1715 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1716 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1718 } else { // current block has frame mv and cand. has field MV (so average)
1719 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1720 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1721 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1722 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1725 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1731 /* Predict B and C */
1732 B[0] = B[1] = C[0] = C[1] = 0;
1733 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1734 if (!s->first_slice_line) {
1735 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1738 pos_b = s->block_index[n_adj] - 2 * wrap;
1739 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1740 n_adj = (n & 2) | (n & 1);
1742 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1743 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1744 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1745 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1746 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1749 if (s->mb_width > 1) {
1750 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1753 pos_c = s->block_index[2] - 2 * wrap + 2;
1754 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1757 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1758 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1759 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1760 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1761 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1763 if (s->mb_x == s->mb_width - 1) {
1764 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1767 pos_c = s->block_index[3] - 2 * wrap - 2;
1768 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1771 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1772 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1773 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1774 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1775 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1784 pos_b = s->block_index[1];
1786 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1787 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1788 pos_c = s->block_index[0];
1790 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1791 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1794 total_valid = a_valid + b_valid + c_valid;
1795 // check if predictor A is out of bounds
1796 if (!s->mb_x && !(n == 1 || n == 3)) {
1799 // check if predictor B is out of bounds
1800 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1801 B[0] = B[1] = C[0] = C[1] = 0;
1803 if (!v->blk_mv_type[xy]) {
1804 if (s->mb_width == 1) {
1808 if (total_valid >= 2) {
1809 px = mid_pred(A[0], B[0], C[0]);
1810 py = mid_pred(A[1], B[1], C[1]);
1811 } else if (total_valid) {
1812 if (a_valid) { px = A[0]; py = A[1]; }
1813 else if (b_valid) { px = B[0]; py = B[1]; }
1814 else { px = C[0]; py = C[1]; }
1819 field_a = (A[1] & 4) ? 1 : 0;
1823 field_b = (B[1] & 4) ? 1 : 0;
1827 field_c = (C[1] & 4) ? 1 : 0;
1831 num_oppfield = field_a + field_b + field_c;
1832 num_samefield = total_valid - num_oppfield;
1833 if (total_valid == 3) {
1834 if ((num_samefield == 3) || (num_oppfield == 3)) {
1835 px = mid_pred(A[0], B[0], C[0]);
1836 py = mid_pred(A[1], B[1], C[1]);
1837 } else if (num_samefield >= num_oppfield) {
1838 /* take one MV from same field set depending on priority
1839 the check for B may not be necessary */
1840 px = !field_a ? A[0] : B[0];
1841 py = !field_a ? A[1] : B[1];
1843 px = field_a ? A[0] : B[0];
1844 py = field_a ? A[1] : B[1];
1846 } else if (total_valid == 2) {
1847 if (num_samefield >= num_oppfield) {
1848 if (!field_a && a_valid) {
1851 } else if (!field_b && b_valid) {
1854 } else /*if (c_valid)*/ {
1855 av_assert1(c_valid);
1858 } /*else px = py = 0;*/
1860 if (field_a && a_valid) {
1863 } else /*if (field_b && b_valid)*/ {
1864 av_assert1(field_b && b_valid);
1867 } /*else if (c_valid) {
1872 } else if (total_valid == 1) {
1873 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1874 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1878 /* store MV using signed modulus of MV range defined in 4.11 */
1879 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1880 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1881 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1882 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1883 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1884 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1885 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1886 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1887 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1888 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1889 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1890 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1891 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1892 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1896 /** Motion compensation for direct or interpolated blocks in B-frames
1898 static void vc1_interp_mc(VC1Context *v)
1900 MpegEncContext *s = &v->s;
1901 H264ChromaContext *h264chroma = &v->h264chroma;
1902 uint8_t *srcY, *srcU, *srcV;
1903 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1905 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1906 int use_ic = v->next_use_ic;
1908 if (!v->field_mode && !v->s.next_picture.f->data[0])
1911 mx = s->mv[1][0][0];
1912 my = s->mv[1][0][1];
1913 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1914 uvmy = (my + ((my & 3) == 3)) >> 1;
1915 if (v->field_mode) {
1916 if (v->cur_field_type != v->ref_field_type[1]) {
1917 my = my - 2 + 4 * v->cur_field_type;
1918 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1922 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1923 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1925 srcY = s->next_picture.f->data[0];
1926 srcU = s->next_picture.f->data[1];
1927 srcV = s->next_picture.f->data[2];
1929 src_x = s->mb_x * 16 + (mx >> 2);
1930 src_y = s->mb_y * 16 + (my >> 2);
1931 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1932 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1934 if (v->profile != PROFILE_ADVANCED) {
1935 src_x = av_clip( src_x, -16, s->mb_width * 16);
1936 src_y = av_clip( src_y, -16, s->mb_height * 16);
1937 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1938 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1940 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1941 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1942 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1943 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1946 srcY += src_y * s->linesize + src_x;
1947 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1948 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1950 if (v->field_mode && v->ref_field_type[1]) {
1951 srcY += s->current_picture_ptr->f->linesize[0];
1952 srcU += s->current_picture_ptr->f->linesize[1];
1953 srcV += s->current_picture_ptr->f->linesize[2];
1956 /* for grayscale we should not try to read from unknown area */
1957 if (s->flags & CODEC_FLAG_GRAY) {
1958 srcU = s->edge_emu_buffer + 18 * s->linesize;
1959 srcV = s->edge_emu_buffer + 18 * s->linesize;
1962 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1963 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1964 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1965 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1967 srcY -= s->mspel * (1 + s->linesize);
1968 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY,
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(uvbuf, srcU,
1975 s->uvlinesize, s->uvlinesize,
1978 s->h_edge_pos >> 1, v_edge_pos >> 1);
1979 s->vdsp.emulated_edge_mc(uvbuf + 16, 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[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2043 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2044 srcY += s->linesize * 8;
2045 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2046 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2048 dxy = (my & 2) | ((mx & 2) >> 1);
2051 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2053 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2056 if (s->flags & CODEC_FLAG_GRAY) return;
2057 /* Chroma MC always uses qpel blilinear */
2058 uvmx = (uvmx & 3) << 1;
2059 uvmy = (uvmy & 3) << 1;
2061 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2062 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2064 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2065 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2069 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2073 #if B_FRACTION_DEN==256
2077 return 2 * ((value * n + 255) >> 9);
2078 return (value * n + 128) >> 8;
2081 n -= B_FRACTION_DEN;
2083 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2084 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2088 /** Reconstruct motion vector for B-frame and do motion compensation
2090 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2091 int direct, int mode)
2098 if (mode == BMV_TYPE_INTERPOLATED) {
2104 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2107 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2108 int direct, int mvtype)
2110 MpegEncContext *s = &v->s;
2111 int xy, wrap, off = 0;
2116 const uint8_t *is_intra = v->mb_type[0];
2118 av_assert0(!v->field_mode);
2122 /* scale MV difference to be quad-pel */
2123 dmv_x[0] <<= 1 - s->quarter_sample;
2124 dmv_y[0] <<= 1 - s->quarter_sample;
2125 dmv_x[1] <<= 1 - s->quarter_sample;
2126 dmv_y[1] <<= 1 - s->quarter_sample;
2128 wrap = s->b8_stride;
2129 xy = s->block_index[0];
2132 s->current_picture.motion_val[0][xy][0] =
2133 s->current_picture.motion_val[0][xy][1] =
2134 s->current_picture.motion_val[1][xy][0] =
2135 s->current_picture.motion_val[1][xy][1] = 0;
2138 if (direct && s->next_picture_ptr->field_picture)
2139 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
2141 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2142 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2143 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2144 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2146 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2147 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));
2148 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));
2149 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));
2150 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));
2152 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2153 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2154 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2155 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2159 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2160 C = s->current_picture.motion_val[0][xy - 2];
2161 A = s->current_picture.motion_val[0][xy - wrap * 2];
2162 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2163 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2165 if (!s->mb_x) C[0] = C[1] = 0;
2166 if (!s->first_slice_line) { // predictor A is not out of bounds
2167 if (s->mb_width == 1) {
2171 px = mid_pred(A[0], B[0], C[0]);
2172 py = mid_pred(A[1], B[1], C[1]);
2174 } else if (s->mb_x) { // predictor C is not out of bounds
2180 /* Pullback MV as specified in 8.3.5.3.4 */
2183 if (v->profile < PROFILE_ADVANCED) {
2184 qx = (s->mb_x << 5);
2185 qy = (s->mb_y << 5);
2186 X = (s->mb_width << 5) - 4;
2187 Y = (s->mb_height << 5) - 4;
2188 if (qx + px < -28) px = -28 - qx;
2189 if (qy + py < -28) py = -28 - qy;
2190 if (qx + px > X) px = X - qx;
2191 if (qy + py > Y) py = Y - qy;
2193 qx = (s->mb_x << 6);
2194 qy = (s->mb_y << 6);
2195 X = (s->mb_width << 6) - 4;
2196 Y = (s->mb_height << 6) - 4;
2197 if (qx + px < -60) px = -60 - qx;
2198 if (qy + py < -60) py = -60 - qy;
2199 if (qx + px > X) px = X - qx;
2200 if (qy + py > Y) py = Y - qy;
2203 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2204 if (0 && !s->first_slice_line && s->mb_x) {
2205 if (is_intra[xy - wrap])
2206 sum = FFABS(px) + FFABS(py);
2208 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2210 if (get_bits1(&s->gb)) {
2218 if (is_intra[xy - 2])
2219 sum = FFABS(px) + FFABS(py);
2221 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2223 if (get_bits1(&s->gb)) {
2233 /* store MV using signed modulus of MV range defined in 4.11 */
2234 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2235 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2237 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2238 C = s->current_picture.motion_val[1][xy - 2];
2239 A = s->current_picture.motion_val[1][xy - wrap * 2];
2240 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2241 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2245 if (!s->first_slice_line) { // predictor A is not out of bounds
2246 if (s->mb_width == 1) {
2250 px = mid_pred(A[0], B[0], C[0]);
2251 py = mid_pred(A[1], B[1], C[1]);
2253 } else if (s->mb_x) { // predictor C is not out of bounds
2259 /* Pullback MV as specified in 8.3.5.3.4 */
2262 if (v->profile < PROFILE_ADVANCED) {
2263 qx = (s->mb_x << 5);
2264 qy = (s->mb_y << 5);
2265 X = (s->mb_width << 5) - 4;
2266 Y = (s->mb_height << 5) - 4;
2267 if (qx + px < -28) px = -28 - qx;
2268 if (qy + py < -28) py = -28 - qy;
2269 if (qx + px > X) px = X - qx;
2270 if (qy + py > Y) py = Y - qy;
2272 qx = (s->mb_x << 6);
2273 qy = (s->mb_y << 6);
2274 X = (s->mb_width << 6) - 4;
2275 Y = (s->mb_height << 6) - 4;
2276 if (qx + px < -60) px = -60 - qx;
2277 if (qy + py < -60) py = -60 - qy;
2278 if (qx + px > X) px = X - qx;
2279 if (qy + py > Y) py = Y - qy;
2282 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2283 if (0 && !s->first_slice_line && s->mb_x) {
2284 if (is_intra[xy - wrap])
2285 sum = FFABS(px) + FFABS(py);
2287 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2289 if (get_bits1(&s->gb)) {
2297 if (is_intra[xy - 2])
2298 sum = FFABS(px) + FFABS(py);
2300 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2302 if (get_bits1(&s->gb)) {
2312 /* store MV using signed modulus of MV range defined in 4.11 */
2314 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2315 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2317 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2318 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2319 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2320 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2323 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2325 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2326 MpegEncContext *s = &v->s;
2327 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2329 if (v->bmvtype == BMV_TYPE_DIRECT) {
2330 int total_opp, k, f;
2331 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2332 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2333 v->bfraction, 0, s->quarter_sample);
2334 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2335 v->bfraction, 0, s->quarter_sample);
2336 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2337 v->bfraction, 1, s->quarter_sample);
2338 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2339 v->bfraction, 1, s->quarter_sample);
2341 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2342 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2343 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2344 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2345 f = (total_opp > 2) ? 1 : 0;
2347 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2348 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2351 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2352 for (k = 0; k < 4; k++) {
2353 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2354 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2355 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2356 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2357 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2358 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2362 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2363 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);
2364 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);
2367 if (dir) { // backward
2368 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);
2369 if (n == 3 || mv1) {
2370 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2373 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);
2374 if (n == 3 || mv1) {
2375 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2380 /** Get predicted DC value for I-frames only
2381 * prediction dir: left=0, top=1
2382 * @param s MpegEncContext
2383 * @param overlap flag indicating that overlap filtering is used
2384 * @param pq integer part of picture quantizer
2385 * @param[in] n block index in the current MB
2386 * @param dc_val_ptr Pointer to DC predictor
2387 * @param dir_ptr Prediction direction for use in AC prediction
2389 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2390 int16_t **dc_val_ptr, int *dir_ptr)
2392 int a, b, c, wrap, pred, scale;
2394 static const uint16_t dcpred[32] = {
2395 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2396 114, 102, 93, 85, 79, 73, 68, 64,
2397 60, 57, 54, 51, 49, 47, 45, 43,
2398 41, 39, 38, 37, 35, 34, 33
2401 /* find prediction - wmv3_dc_scale always used here in fact */
2402 if (n < 4) scale = s->y_dc_scale;
2403 else scale = s->c_dc_scale;
2405 wrap = s->block_wrap[n];
2406 dc_val = s->dc_val[0] + s->block_index[n];
2412 b = dc_val[ - 1 - wrap];
2413 a = dc_val[ - wrap];
2415 if (pq < 9 || !overlap) {
2416 /* Set outer values */
2417 if (s->first_slice_line && (n != 2 && n != 3))
2418 b = a = dcpred[scale];
2419 if (s->mb_x == 0 && (n != 1 && n != 3))
2420 b = c = dcpred[scale];
2422 /* Set outer values */
2423 if (s->first_slice_line && (n != 2 && n != 3))
2425 if (s->mb_x == 0 && (n != 1 && n != 3))
2429 if (abs(a - b) <= abs(b - c)) {
2431 *dir_ptr = 1; // left
2434 *dir_ptr = 0; // top
2437 /* update predictor */
2438 *dc_val_ptr = &dc_val[0];
2443 /** Get predicted DC value
2444 * prediction dir: left=0, top=1
2445 * @param s MpegEncContext
2446 * @param overlap flag indicating that overlap filtering is used
2447 * @param pq integer part of picture quantizer
2448 * @param[in] n block index in the current MB
2449 * @param a_avail flag indicating top block availability
2450 * @param c_avail flag indicating left block availability
2451 * @param dc_val_ptr Pointer to DC predictor
2452 * @param dir_ptr Prediction direction for use in AC prediction
2454 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2455 int a_avail, int c_avail,
2456 int16_t **dc_val_ptr, int *dir_ptr)
2458 int a, b, c, wrap, pred;
2460 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2464 wrap = s->block_wrap[n];
2465 dc_val = s->dc_val[0] + s->block_index[n];
2471 b = dc_val[ - 1 - wrap];
2472 a = dc_val[ - wrap];
2473 /* scale predictors if needed */
2474 q1 = s->current_picture.qscale_table[mb_pos];
2475 dqscale_index = s->y_dc_scale_table[q1] - 1;
2476 if (dqscale_index < 0)
2478 if (c_avail && (n != 1 && n != 3)) {
2479 q2 = s->current_picture.qscale_table[mb_pos - 1];
2481 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2483 if (a_avail && (n != 2 && n != 3)) {
2484 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2486 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2488 if (a_avail && c_avail && (n != 3)) {
2493 off -= s->mb_stride;
2494 q2 = s->current_picture.qscale_table[off];
2496 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2499 if (a_avail && c_avail) {
2500 if (abs(a - b) <= abs(b - c)) {
2502 *dir_ptr = 1; // left
2505 *dir_ptr = 0; // top
2507 } else if (a_avail) {
2509 *dir_ptr = 0; // top
2510 } else if (c_avail) {
2512 *dir_ptr = 1; // left
2515 *dir_ptr = 1; // left
2518 /* update predictor */
2519 *dc_val_ptr = &dc_val[0];
2523 /** @} */ // Block group
2526 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2527 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2531 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2532 uint8_t **coded_block_ptr)
2534 int xy, wrap, pred, a, b, c;
2536 xy = s->block_index[n];
2537 wrap = s->b8_stride;
2542 a = s->coded_block[xy - 1 ];
2543 b = s->coded_block[xy - 1 - wrap];
2544 c = s->coded_block[xy - wrap];
2553 *coded_block_ptr = &s->coded_block[xy];
2559 * Decode one AC coefficient
2560 * @param v The VC1 context
2561 * @param last Last coefficient
2562 * @param skip How much zero coefficients to skip
2563 * @param value Decoded AC coefficient value
2564 * @param codingset set of VLC to decode data
2567 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2568 int *value, int codingset)
2570 GetBitContext *gb = &v->s.gb;
2571 int index, escape, run = 0, level = 0, lst = 0;
2573 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2574 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2575 run = vc1_index_decode_table[codingset][index][0];
2576 level = vc1_index_decode_table[codingset][index][1];
2577 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2581 escape = decode210(gb);
2583 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2584 run = vc1_index_decode_table[codingset][index][0];
2585 level = vc1_index_decode_table[codingset][index][1];
2586 lst = index >= vc1_last_decode_table[codingset];
2589 level += vc1_last_delta_level_table[codingset][run];
2591 level += vc1_delta_level_table[codingset][run];
2594 run += vc1_last_delta_run_table[codingset][level] + 1;
2596 run += vc1_delta_run_table[codingset][level] + 1;
2602 lst = get_bits1(gb);
2603 if (v->s.esc3_level_length == 0) {
2604 if (v->pq < 8 || v->dquantfrm) { // table 59
2605 v->s.esc3_level_length = get_bits(gb, 3);
2606 if (!v->s.esc3_level_length)
2607 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2608 } else { // table 60
2609 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2611 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2613 run = get_bits(gb, v->s.esc3_run_length);
2614 sign = get_bits1(gb);
2615 level = get_bits(gb, v->s.esc3_level_length);
2626 /** Decode intra block in intra frames - should be faster than decode_intra_block
2627 * @param v VC1Context
2628 * @param block block to decode
2629 * @param[in] n subblock index
2630 * @param coded are AC coeffs present or not
2631 * @param codingset set of VLC to decode data
2633 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2634 int coded, int codingset)
2636 GetBitContext *gb = &v->s.gb;
2637 MpegEncContext *s = &v->s;
2638 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2641 int16_t *ac_val, *ac_val2;
2644 /* Get DC differential */
2646 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2648 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2651 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2655 if (dcdiff == 119 /* ESC index value */) {
2656 /* TODO: Optimize */
2657 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2658 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2659 else dcdiff = get_bits(gb, 8);
2662 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2663 else if (v->pq == 2)
2664 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2671 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2674 /* Store the quantized DC coeff, used for prediction */
2676 block[0] = dcdiff * s->y_dc_scale;
2678 block[0] = dcdiff * s->c_dc_scale;
2689 int last = 0, skip, value;
2690 const uint8_t *zz_table;
2694 scale = v->pq * 2 + v->halfpq;
2698 zz_table = v->zz_8x8[2];
2700 zz_table = v->zz_8x8[3];
2702 zz_table = v->zz_8x8[1];
2704 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2706 if (dc_pred_dir) // left
2709 ac_val -= 16 * s->block_wrap[n];
2712 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2716 block[zz_table[i++]] = value;
2719 /* apply AC prediction if needed */
2721 if (dc_pred_dir) { // left
2722 for (k = 1; k < 8; k++)
2723 block[k << v->left_blk_sh] += ac_val[k];
2725 for (k = 1; k < 8; k++)
2726 block[k << v->top_blk_sh] += ac_val[k + 8];
2729 /* save AC coeffs for further prediction */
2730 for (k = 1; k < 8; k++) {
2731 ac_val2[k] = block[k << v->left_blk_sh];
2732 ac_val2[k + 8] = block[k << v->top_blk_sh];
2735 /* scale AC coeffs */
2736 for (k = 1; k < 64; k++)
2740 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2743 if (s->ac_pred) i = 63;
2749 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2753 scale = v->pq * 2 + v->halfpq;
2754 memset(ac_val2, 0, 16 * 2);
2755 if (dc_pred_dir) { // left
2758 memcpy(ac_val2, ac_val, 8 * 2);
2760 ac_val -= 16 * s->block_wrap[n];
2762 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2765 /* apply AC prediction if needed */
2767 if (dc_pred_dir) { //left
2768 for (k = 1; k < 8; k++) {
2769 block[k << v->left_blk_sh] = ac_val[k] * scale;
2770 if (!v->pquantizer && block[k << v->left_blk_sh])
2771 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2774 for (k = 1; k < 8; k++) {
2775 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2776 if (!v->pquantizer && block[k << v->top_blk_sh])
2777 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2783 s->block_last_index[n] = i;
2788 /** Decode intra block in intra frames - should be faster than decode_intra_block
2789 * @param v VC1Context
2790 * @param block block to decode
2791 * @param[in] n subblock number
2792 * @param coded are AC coeffs present or not
2793 * @param codingset set of VLC to decode data
2794 * @param mquant quantizer value for this macroblock
2796 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2797 int coded, int codingset, int mquant)
2799 GetBitContext *gb = &v->s.gb;
2800 MpegEncContext *s = &v->s;
2801 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2803 int16_t *dc_val = NULL;
2804 int16_t *ac_val, *ac_val2;
2806 int a_avail = v->a_avail, c_avail = v->c_avail;
2807 int use_pred = s->ac_pred;
2810 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2812 /* Get DC differential */
2814 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2816 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2819 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2823 if (dcdiff == 119 /* ESC index value */) {
2824 /* TODO: Optimize */
2825 if (mquant == 1) dcdiff = get_bits(gb, 10);
2826 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2827 else dcdiff = get_bits(gb, 8);
2830 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2831 else if (mquant == 2)
2832 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2839 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2842 /* Store the quantized DC coeff, used for prediction */
2844 block[0] = dcdiff * s->y_dc_scale;
2846 block[0] = dcdiff * s->c_dc_scale;
2852 /* check if AC is needed at all */
2853 if (!a_avail && !c_avail)
2855 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2858 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2860 if (dc_pred_dir) // left
2863 ac_val -= 16 * s->block_wrap[n];
2865 q1 = s->current_picture.qscale_table[mb_pos];
2866 if ( dc_pred_dir && c_avail && mb_pos)
2867 q2 = s->current_picture.qscale_table[mb_pos - 1];
2868 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2869 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2870 if ( dc_pred_dir && n == 1)
2872 if (!dc_pred_dir && n == 2)
2878 int last = 0, skip, value;
2879 const uint8_t *zz_table;
2883 if (!use_pred && v->fcm == ILACE_FRAME) {
2884 zz_table = v->zzi_8x8;
2886 if (!dc_pred_dir) // top
2887 zz_table = v->zz_8x8[2];
2889 zz_table = v->zz_8x8[3];
2892 if (v->fcm != ILACE_FRAME)
2893 zz_table = v->zz_8x8[1];
2895 zz_table = v->zzi_8x8;
2899 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2903 block[zz_table[i++]] = value;
2906 /* apply AC prediction if needed */
2908 /* scale predictors if needed*/
2909 if (q2 && q1 != q2) {
2910 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2911 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2914 return AVERROR_INVALIDDATA;
2915 if (dc_pred_dir) { // left
2916 for (k = 1; k < 8; k++)
2917 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2919 for (k = 1; k < 8; k++)
2920 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2923 if (dc_pred_dir) { //left
2924 for (k = 1; k < 8; k++)
2925 block[k << v->left_blk_sh] += ac_val[k];
2927 for (k = 1; k < 8; k++)
2928 block[k << v->top_blk_sh] += ac_val[k + 8];
2932 /* save AC coeffs for further prediction */
2933 for (k = 1; k < 8; k++) {
2934 ac_val2[k ] = block[k << v->left_blk_sh];
2935 ac_val2[k + 8] = block[k << v->top_blk_sh];
2938 /* scale AC coeffs */
2939 for (k = 1; k < 64; k++)
2943 block[k] += (block[k] < 0) ? -mquant : mquant;
2946 if (use_pred) i = 63;
2947 } else { // no AC coeffs
2950 memset(ac_val2, 0, 16 * 2);
2951 if (dc_pred_dir) { // left
2953 memcpy(ac_val2, ac_val, 8 * 2);
2954 if (q2 && q1 != q2) {
2955 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2956 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2958 return AVERROR_INVALIDDATA;
2959 for (k = 1; k < 8; k++)
2960 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2965 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2966 if (q2 && q1 != q2) {
2967 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2968 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2970 return AVERROR_INVALIDDATA;
2971 for (k = 1; k < 8; k++)
2972 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2977 /* apply AC prediction if needed */
2979 if (dc_pred_dir) { // left
2980 for (k = 1; k < 8; k++) {
2981 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2982 if (!v->pquantizer && block[k << v->left_blk_sh])
2983 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2986 for (k = 1; k < 8; k++) {
2987 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2988 if (!v->pquantizer && block[k << v->top_blk_sh])
2989 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2995 s->block_last_index[n] = i;
3000 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
3001 * @param v VC1Context
3002 * @param block block to decode
3003 * @param[in] n subblock index
3004 * @param coded are AC coeffs present or not
3005 * @param mquant block quantizer
3006 * @param codingset set of VLC to decode data
3008 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
3009 int coded, int mquant, int codingset)
3011 GetBitContext *gb = &v->s.gb;
3012 MpegEncContext *s = &v->s;
3013 int dc_pred_dir = 0; /* Direction of the DC prediction used */
3015 int16_t *dc_val = NULL;
3016 int16_t *ac_val, *ac_val2;
3018 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3019 int a_avail = v->a_avail, c_avail = v->c_avail;
3020 int use_pred = s->ac_pred;
3024 s->dsp.clear_block(block);
3026 /* XXX: Guard against dumb values of mquant */
3027 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
3029 /* Set DC scale - y and c use the same */
3030 s->y_dc_scale = s->y_dc_scale_table[mquant];
3031 s->c_dc_scale = s->c_dc_scale_table[mquant];
3033 /* Get DC differential */
3035 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3037 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3040 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3044 if (dcdiff == 119 /* ESC index value */) {
3045 /* TODO: Optimize */
3046 if (mquant == 1) dcdiff = get_bits(gb, 10);
3047 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3048 else dcdiff = get_bits(gb, 8);
3051 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3052 else if (mquant == 2)
3053 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3060 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3063 /* Store the quantized DC coeff, used for prediction */
3066 block[0] = dcdiff * s->y_dc_scale;
3068 block[0] = dcdiff * s->c_dc_scale;
3074 /* check if AC is needed at all and adjust direction if needed */
3075 if (!a_avail) dc_pred_dir = 1;
3076 if (!c_avail) dc_pred_dir = 0;
3077 if (!a_avail && !c_avail) use_pred = 0;
3078 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3081 scale = mquant * 2 + v->halfpq;
3083 if (dc_pred_dir) //left
3086 ac_val -= 16 * s->block_wrap[n];
3088 q1 = s->current_picture.qscale_table[mb_pos];
3089 if (dc_pred_dir && c_avail && mb_pos)
3090 q2 = s->current_picture.qscale_table[mb_pos - 1];
3091 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3092 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3093 if ( dc_pred_dir && n == 1)
3095 if (!dc_pred_dir && n == 2)
3097 if (n == 3) q2 = q1;
3100 int last = 0, skip, value;
3104 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3108 if (v->fcm == PROGRESSIVE)
3109 block[v->zz_8x8[0][i++]] = value;
3111 if (use_pred && (v->fcm == ILACE_FRAME)) {
3112 if (!dc_pred_dir) // top
3113 block[v->zz_8x8[2][i++]] = value;
3115 block[v->zz_8x8[3][i++]] = value;
3117 block[v->zzi_8x8[i++]] = value;
3122 /* apply AC prediction if needed */
3124 /* scale predictors if needed*/
3125 if (q2 && q1 != q2) {
3126 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3127 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3130 return AVERROR_INVALIDDATA;
3131 if (dc_pred_dir) { // left
3132 for (k = 1; k < 8; k++)
3133 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3135 for (k = 1; k < 8; k++)
3136 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3139 if (dc_pred_dir) { // left
3140 for (k = 1; k < 8; k++)
3141 block[k << v->left_blk_sh] += ac_val[k];
3143 for (k = 1; k < 8; k++)
3144 block[k << v->top_blk_sh] += ac_val[k + 8];
3148 /* save AC coeffs for further prediction */
3149 for (k = 1; k < 8; k++) {
3150 ac_val2[k ] = block[k << v->left_blk_sh];
3151 ac_val2[k + 8] = block[k << v->top_blk_sh];
3154 /* scale AC coeffs */
3155 for (k = 1; k < 64; k++)
3159 block[k] += (block[k] < 0) ? -mquant : mquant;
3162 if (use_pred) i = 63;
3163 } else { // no AC coeffs
3166 memset(ac_val2, 0, 16 * 2);
3167 if (dc_pred_dir) { // left
3169 memcpy(ac_val2, ac_val, 8 * 2);
3170 if (q2 && q1 != q2) {
3171 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3172 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3174 return AVERROR_INVALIDDATA;
3175 for (k = 1; k < 8; k++)
3176 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3181 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3182 if (q2 && q1 != q2) {
3183 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3184 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3186 return AVERROR_INVALIDDATA;
3187 for (k = 1; k < 8; k++)
3188 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3193 /* apply AC prediction if needed */
3195 if (dc_pred_dir) { // left
3196 for (k = 1; k < 8; k++) {
3197 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3198 if (!v->pquantizer && block[k << v->left_blk_sh])
3199 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3202 for (k = 1; k < 8; k++) {
3203 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3204 if (!v->pquantizer && block[k << v->top_blk_sh])
3205 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3211 s->block_last_index[n] = i;
3218 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3219 int mquant, int ttmb, int first_block,
3220 uint8_t *dst, int linesize, int skip_block,
3223 MpegEncContext *s = &v->s;
3224 GetBitContext *gb = &s->gb;
3227 int scale, off, idx, last, skip, value;
3228 int ttblk = ttmb & 7;
3231 s->dsp.clear_block(block);
3234 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)];
3236 if (ttblk == TT_4X4) {
3237 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3239 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3240 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3241 || (!v->res_rtm_flag && !first_block))) {
3242 subblkpat = decode012(gb);
3244 subblkpat ^= 3; // swap decoded pattern bits
3245 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3247 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3250 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3252 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3253 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3254 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3257 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3258 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3267 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3272 idx = v->zz_8x8[0][i++];
3274 idx = v->zzi_8x8[i++];
3275 block[idx] = value * scale;
3277 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3281 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3283 v->vc1dsp.vc1_inv_trans_8x8(block);
3284 s->dsp.add_pixels_clamped(block, dst, linesize);
3289 pat = ~subblkpat & 0xF;
3290 for (j = 0; j < 4; j++) {
3291 last = subblkpat & (1 << (3 - j));
3293 off = (j & 1) * 4 + (j & 2) * 16;
3295 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3300 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3302 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3303 block[idx + off] = value * scale;
3305 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3307 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3309 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3311 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3316 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3317 for (j = 0; j < 2; j++) {
3318 last = subblkpat & (1 << (1 - j));
3322 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3327 idx = v->zz_8x4[i++] + off;
3329 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3330 block[idx] = value * scale;
3332 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3334 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3336 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3338 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3343 pat = ~(subblkpat * 5) & 0xF;
3344 for (j = 0; j < 2; j++) {
3345 last = subblkpat & (1 << (1 - j));
3349 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3354 idx = v->zz_4x8[i++] + off;
3356 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3357 block[idx] = value * scale;
3359 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3361 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3363 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3365 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3371 *ttmb_out |= ttblk << (n * 4);
3375 /** @} */ // Macroblock group
3377 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3378 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3380 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3382 MpegEncContext *s = &v->s;
3383 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3384 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3385 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3386 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3387 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3390 if (block_num > 3) {
3391 dst = s->dest[block_num - 3];
3393 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3395 if (s->mb_y != s->end_mb_y || block_num < 2) {
3399 if (block_num > 3) {
3400 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3401 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3402 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3403 mv_stride = s->mb_stride;
3405 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3406 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3407 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3408 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3409 mv_stride = s->b8_stride;
3410 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3413 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3414 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3415 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3417 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3419 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3422 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3424 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3429 dst -= 4 * linesize;
3430 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3431 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3432 idx = (block_cbp | (block_cbp >> 2)) & 3;
3434 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3437 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3439 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3444 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3446 MpegEncContext *s = &v->s;
3447 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3448 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3449 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3450 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3451 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3454 if (block_num > 3) {
3455 dst = s->dest[block_num - 3] - 8 * linesize;
3457 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3460 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3463 if (block_num > 3) {
3464 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3465 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3466 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3468 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3469 : (mb_cbp >> ((block_num + 1) * 4));
3470 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3471 : (mb_is_intra >> ((block_num + 1) * 4));
3472 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3474 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3475 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3477 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3479 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3482 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3484 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3490 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3491 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3492 idx = (block_cbp | (block_cbp >> 1)) & 5;
3494 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3497 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3499 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3504 static void vc1_apply_p_loop_filter(VC1Context *v)
3506 MpegEncContext *s = &v->s;
3509 for (i = 0; i < 6; i++) {
3510 vc1_apply_p_v_loop_filter(v, i);
3513 /* V always precedes H, therefore we run H one MB before V;
3514 * at the end of a row, we catch up to complete the row */
3516 for (i = 0; i < 6; i++) {
3517 vc1_apply_p_h_loop_filter(v, i);
3519 if (s->mb_x == s->mb_width - 1) {
3521 ff_update_block_index(s);
3522 for (i = 0; i < 6; i++) {
3523 vc1_apply_p_h_loop_filter(v, i);
3529 /** Decode one P-frame MB
3531 static int vc1_decode_p_mb(VC1Context *v)
3533 MpegEncContext *s = &v->s;
3534 GetBitContext *gb = &s->gb;
3536 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3537 int cbp; /* cbp decoding stuff */
3538 int mqdiff, mquant; /* MB quantization */
3539 int ttmb = v->ttfrm; /* MB Transform type */
3541 int mb_has_coeffs = 1; /* last_flag */
3542 int dmv_x, dmv_y; /* Differential MV components */
3543 int index, index1; /* LUT indexes */
3544 int val, sign; /* temp values */
3545 int first_block = 1;
3547 int skipped, fourmv;
3548 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3550 mquant = v->pq; /* lossy initialization */
3552 if (v->mv_type_is_raw)
3553 fourmv = get_bits1(gb);
3555 fourmv = v->mv_type_mb_plane[mb_pos];
3557 skipped = get_bits1(gb);
3559 skipped = v->s.mbskip_table[mb_pos];
3561 if (!fourmv) { /* 1MV mode */
3563 GET_MVDATA(dmv_x, dmv_y);
3566 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3567 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3569 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3570 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3572 /* FIXME Set DC val for inter block ? */
3573 if (s->mb_intra && !mb_has_coeffs) {
3575 s->ac_pred = get_bits1(gb);
3577 } else if (mb_has_coeffs) {
3579 s->ac_pred = get_bits1(gb);
3580 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3586 s->current_picture.qscale_table[mb_pos] = mquant;
3588 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3589 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3590 VC1_TTMB_VLC_BITS, 2);
3591 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3593 for (i = 0; i < 6; i++) {
3594 s->dc_val[0][s->block_index[i]] = 0;
3596 val = ((cbp >> (5 - i)) & 1);
3597 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3598 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3600 /* check if prediction blocks A and C are available */
3601 v->a_avail = v->c_avail = 0;
3602 if (i == 2 || i == 3 || !s->first_slice_line)
3603 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3604 if (i == 1 || i == 3 || s->mb_x)
3605 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3607 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3608 (i & 4) ? v->codingset2 : v->codingset);
3609 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3611 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3613 for (j = 0; j < 64; j++)
3614 s->block[i][j] <<= 1;
3615 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3616 if (v->pq >= 9 && v->overlap) {
3618 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3620 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3622 block_cbp |= 0xF << (i << 2);
3623 block_intra |= 1 << i;
3625 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3626 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3627 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3628 block_cbp |= pat << (i << 2);
3629 if (!v->ttmbf && ttmb < 8)
3636 for (i = 0; i < 6; i++) {
3637 v->mb_type[0][s->block_index[i]] = 0;
3638 s->dc_val[0][s->block_index[i]] = 0;
3640 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3641 s->current_picture.qscale_table[mb_pos] = 0;
3642 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3645 } else { // 4MV mode
3646 if (!skipped /* unskipped MB */) {
3647 int intra_count = 0, coded_inter = 0;
3648 int is_intra[6], is_coded[6];
3650 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3651 for (i = 0; i < 6; i++) {
3652 val = ((cbp >> (5 - i)) & 1);
3653 s->dc_val[0][s->block_index[i]] = 0;
3660 GET_MVDATA(dmv_x, dmv_y);
3662 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3664 vc1_mc_4mv_luma(v, i, 0, 0);
3665 intra_count += s->mb_intra;
3666 is_intra[i] = s->mb_intra;
3667 is_coded[i] = mb_has_coeffs;
3670 is_intra[i] = (intra_count >= 3);
3674 vc1_mc_4mv_chroma(v, 0);
3675 v->mb_type[0][s->block_index[i]] = is_intra[i];
3677 coded_inter = !is_intra[i] & is_coded[i];
3679 // if there are no coded blocks then don't do anything more
3681 if (!intra_count && !coded_inter)
3684 s->current_picture.qscale_table[mb_pos] = mquant;
3685 /* test if block is intra and has pred */
3688 for (i = 0; i < 6; i++)
3690 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3691 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3697 s->ac_pred = get_bits1(gb);
3701 if (!v->ttmbf && coded_inter)
3702 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3703 for (i = 0; i < 6; i++) {
3705 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3706 s->mb_intra = is_intra[i];
3708 /* check if prediction blocks A and C are available */
3709 v->a_avail = v->c_avail = 0;
3710 if (i == 2 || i == 3 || !s->first_slice_line)
3711 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3712 if (i == 1 || i == 3 || s->mb_x)
3713 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3715 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3716 (i & 4) ? v->codingset2 : v->codingset);
3717 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3719 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3721 for (j = 0; j < 64; j++)
3722 s->block[i][j] <<= 1;
3723 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3724 (i & 4) ? s->uvlinesize : s->linesize);
3725 if (v->pq >= 9 && v->overlap) {
3727 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3729 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3731 block_cbp |= 0xF << (i << 2);
3732 block_intra |= 1 << i;
3733 } else if (is_coded[i]) {
3734 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3735 first_block, s->dest[dst_idx] + off,
3736 (i & 4) ? s->uvlinesize : s->linesize,
3737 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3739 block_cbp |= pat << (i << 2);
3740 if (!v->ttmbf && ttmb < 8)
3745 } else { // skipped MB
3747 s->current_picture.qscale_table[mb_pos] = 0;
3748 for (i = 0; i < 6; i++) {
3749 v->mb_type[0][s->block_index[i]] = 0;
3750 s->dc_val[0][s->block_index[i]] = 0;
3752 for (i = 0; i < 4; i++) {
3753 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3754 vc1_mc_4mv_luma(v, i, 0, 0);
3756 vc1_mc_4mv_chroma(v, 0);
3757 s->current_picture.qscale_table[mb_pos] = 0;
3761 v->cbp[s->mb_x] = block_cbp;
3762 v->ttblk[s->mb_x] = block_tt;
3763 v->is_intra[s->mb_x] = block_intra;
3768 /* Decode one macroblock in an interlaced frame p picture */
3770 static int vc1_decode_p_mb_intfr(VC1Context *v)
3772 MpegEncContext *s = &v->s;
3773 GetBitContext *gb = &s->gb;
3775 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3776 int cbp = 0; /* cbp decoding stuff */
3777 int mqdiff, mquant; /* MB quantization */
3778 int ttmb = v->ttfrm; /* MB Transform type */
3780 int mb_has_coeffs = 1; /* last_flag */
3781 int dmv_x, dmv_y; /* Differential MV components */
3782 int val; /* temp value */
3783 int first_block = 1;
3785 int skipped, fourmv = 0, twomv = 0;
3786 int block_cbp = 0, pat, block_tt = 0;
3787 int idx_mbmode = 0, mvbp;
3788 int stride_y, fieldtx;
3790 mquant = v->pq; /* Lossy initialization */
3793 skipped = get_bits1(gb);
3795 skipped = v->s.mbskip_table[mb_pos];
3797 if (v->fourmvswitch)
3798 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3800 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3801 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3802 /* store the motion vector type in a flag (useful later) */
3803 case MV_PMODE_INTFR_4MV:
3805 v->blk_mv_type[s->block_index[0]] = 0;
3806 v->blk_mv_type[s->block_index[1]] = 0;
3807 v->blk_mv_type[s->block_index[2]] = 0;
3808 v->blk_mv_type[s->block_index[3]] = 0;
3810 case MV_PMODE_INTFR_4MV_FIELD:
3812 v->blk_mv_type[s->block_index[0]] = 1;
3813 v->blk_mv_type[s->block_index[1]] = 1;
3814 v->blk_mv_type[s->block_index[2]] = 1;
3815 v->blk_mv_type[s->block_index[3]] = 1;
3817 case MV_PMODE_INTFR_2MV_FIELD:
3819 v->blk_mv_type[s->block_index[0]] = 1;
3820 v->blk_mv_type[s->block_index[1]] = 1;
3821 v->blk_mv_type[s->block_index[2]] = 1;
3822 v->blk_mv_type[s->block_index[3]] = 1;
3824 case MV_PMODE_INTFR_1MV:
3825 v->blk_mv_type[s->block_index[0]] = 0;
3826 v->blk_mv_type[s->block_index[1]] = 0;
3827 v->blk_mv_type[s->block_index[2]] = 0;
3828 v->blk_mv_type[s->block_index[3]] = 0;
3831 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3832 for (i = 0; i < 4; i++) {
3833 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3834 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3836 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3837 s->mb_intra = v->is_intra[s->mb_x] = 1;
3838 for (i = 0; i < 6; i++)
3839 v->mb_type[0][s->block_index[i]] = 1;
3840 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3841 mb_has_coeffs = get_bits1(gb);
3843 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3844 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3846 s->current_picture.qscale_table[mb_pos] = mquant;
3847 /* Set DC scale - y and c use the same (not sure if necessary here) */
3848 s->y_dc_scale = s->y_dc_scale_table[mquant];
3849 s->c_dc_scale = s->c_dc_scale_table[mquant];
3851 for (i = 0; i < 6; i++) {
3852 s->dc_val[0][s->block_index[i]] = 0;
3854 val = ((cbp >> (5 - i)) & 1);
3855 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3856 v->a_avail = v->c_avail = 0;
3857 if (i == 2 || i == 3 || !s->first_slice_line)
3858 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3859 if (i == 1 || i == 3 || s->mb_x)
3860 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3862 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3863 (i & 4) ? v->codingset2 : v->codingset);
3864 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3865 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3867 stride_y = s->linesize << fieldtx;
3868 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3870 stride_y = s->uvlinesize;
3873 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3877 } else { // inter MB
3878 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3880 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3881 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3882 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3884 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3885 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3886 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3889 s->mb_intra = v->is_intra[s->mb_x] = 0;
3890 for (i = 0; i < 6; i++)
3891 v->mb_type[0][s->block_index[i]] = 0;
3892 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3893 /* for all motion vector read MVDATA and motion compensate each block */
3897 for (i = 0; i < 6; i++) {
3900 val = ((mvbp >> (3 - i)) & 1);
3902 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3904 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3905 vc1_mc_4mv_luma(v, i, 0, 0);
3906 } else if (i == 4) {
3907 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3914 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3916 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3917 vc1_mc_4mv_luma(v, 0, 0, 0);
3918 vc1_mc_4mv_luma(v, 1, 0, 0);
3921 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3923 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3924 vc1_mc_4mv_luma(v, 2, 0, 0);
3925 vc1_mc_4mv_luma(v, 3, 0, 0);
3926 vc1_mc_4mv_chroma4(v, 0, 0, 0);
3928 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3931 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3933 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3937 GET_MQUANT(); // p. 227
3938 s->current_picture.qscale_table[mb_pos] = mquant;
3939 if (!v->ttmbf && cbp)
3940 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3941 for (i = 0; i < 6; i++) {
3942 s->dc_val[0][s->block_index[i]] = 0;
3944 val = ((cbp >> (5 - i)) & 1);
3946 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3948 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3950 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3951 first_block, s->dest[dst_idx] + off,
3952 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3953 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3954 block_cbp |= pat << (i << 2);
3955 if (!v->ttmbf && ttmb < 8)
3962 s->mb_intra = v->is_intra[s->mb_x] = 0;
3963 for (i = 0; i < 6; i++) {
3964 v->mb_type[0][s->block_index[i]] = 0;
3965 s->dc_val[0][s->block_index[i]] = 0;
3967 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3968 s->current_picture.qscale_table[mb_pos] = 0;
3969 v->blk_mv_type[s->block_index[0]] = 0;
3970 v->blk_mv_type[s->block_index[1]] = 0;
3971 v->blk_mv_type[s->block_index[2]] = 0;
3972 v->blk_mv_type[s->block_index[3]] = 0;
3973 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3976 if (s->mb_x == s->mb_width - 1)
3977 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3981 static int vc1_decode_p_mb_intfi(VC1Context *v)
3983 MpegEncContext *s = &v->s;
3984 GetBitContext *gb = &s->gb;
3986 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3987 int cbp = 0; /* cbp decoding stuff */
3988 int mqdiff, mquant; /* MB quantization */
3989 int ttmb = v->ttfrm; /* MB Transform type */
3991 int mb_has_coeffs = 1; /* last_flag */
3992 int dmv_x, dmv_y; /* Differential MV components */
3993 int val; /* temp values */
3994 int first_block = 1;
3997 int block_cbp = 0, pat, block_tt = 0;
4000 mquant = v->pq; /* Lossy initialization */
4002 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4003 if (idx_mbmode <= 1) { // intra MB
4004 s->mb_intra = v->is_intra[s->mb_x] = 1;
4005 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4006 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4007 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4009 s->current_picture.qscale_table[mb_pos] = mquant;
4010 /* Set DC scale - y and c use the same (not sure if necessary here) */
4011 s->y_dc_scale = s->y_dc_scale_table[mquant];
4012 s->c_dc_scale = s->c_dc_scale_table[mquant];
4013 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4014 mb_has_coeffs = idx_mbmode & 1;
4016 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4018 for (i = 0; i < 6; i++) {
4019 s->dc_val[0][s->block_index[i]] = 0;
4020 v->mb_type[0][s->block_index[i]] = 1;
4022 val = ((cbp >> (5 - i)) & 1);
4023 v->a_avail = v->c_avail = 0;
4024 if (i == 2 || i == 3 || !s->first_slice_line)
4025 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4026 if (i == 1 || i == 3 || s->mb_x)
4027 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4029 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4030 (i & 4) ? v->codingset2 : v->codingset);
4031 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4033 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4034 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4035 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4036 // TODO: loop filter
4039 s->mb_intra = v->is_intra[s->mb_x] = 0;
4040 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4041 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4042 if (idx_mbmode <= 5) { // 1-MV
4043 dmv_x = dmv_y = pred_flag = 0;
4044 if (idx_mbmode & 1) {
4045 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4047 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4049 mb_has_coeffs = !(idx_mbmode & 2);
4051 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4052 for (i = 0; i < 6; i++) {
4054 dmv_x = dmv_y = pred_flag = 0;
4055 val = ((v->fourmvbp >> (3 - i)) & 1);
4057 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4059 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4060 vc1_mc_4mv_luma(v, i, 0, 0);
4062 vc1_mc_4mv_chroma(v, 0);
4064 mb_has_coeffs = idx_mbmode & 1;
4067 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4071 s->current_picture.qscale_table[mb_pos] = mquant;
4072 if (!v->ttmbf && cbp) {
4073 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4076 for (i = 0; i < 6; i++) {
4077 s->dc_val[0][s->block_index[i]] = 0;
4079 val = ((cbp >> (5 - i)) & 1);
4080 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4082 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4083 first_block, s->dest[dst_idx] + off,
4084 (i & 4) ? s->uvlinesize : s->linesize,
4085 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4087 block_cbp |= pat << (i << 2);
4088 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4093 if (s->mb_x == s->mb_width - 1)
4094 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4098 /** Decode one B-frame MB (in Main profile)
4100 static void vc1_decode_b_mb(VC1Context *v)
4102 MpegEncContext *s = &v->s;
4103 GetBitContext *gb = &s->gb;
4105 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4106 int cbp = 0; /* cbp decoding stuff */
4107 int mqdiff, mquant; /* MB quantization */
4108 int ttmb = v->ttfrm; /* MB Transform type */
4109 int mb_has_coeffs = 0; /* last_flag */
4110 int index, index1; /* LUT indexes */
4111 int val, sign; /* temp values */
4112 int first_block = 1;
4114 int skipped, direct;
4115 int dmv_x[2], dmv_y[2];
4116 int bmvtype = BMV_TYPE_BACKWARD;
4118 mquant = v->pq; /* lossy initialization */
4122 direct = get_bits1(gb);
4124 direct = v->direct_mb_plane[mb_pos];
4126 skipped = get_bits1(gb);
4128 skipped = v->s.mbskip_table[mb_pos];
4130 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4131 for (i = 0; i < 6; i++) {
4132 v->mb_type[0][s->block_index[i]] = 0;
4133 s->dc_val[0][s->block_index[i]] = 0;
4135 s->current_picture.qscale_table[mb_pos] = 0;
4139 GET_MVDATA(dmv_x[0], dmv_y[0]);
4140 dmv_x[1] = dmv_x[0];
4141 dmv_y[1] = dmv_y[0];
4143 if (skipped || !s->mb_intra) {
4144 bmvtype = decode012(gb);
4147 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4150 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4153 bmvtype = BMV_TYPE_INTERPOLATED;
4154 dmv_x[0] = dmv_y[0] = 0;
4158 for (i = 0; i < 6; i++)
4159 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4163 bmvtype = BMV_TYPE_INTERPOLATED;
4164 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4165 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4169 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4172 s->current_picture.qscale_table[mb_pos] = mquant;
4174 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4175 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4176 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4177 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4179 if (!mb_has_coeffs && !s->mb_intra) {
4180 /* no coded blocks - effectively skipped */
4181 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4182 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4185 if (s->mb_intra && !mb_has_coeffs) {
4187 s->current_picture.qscale_table[mb_pos] = mquant;
4188 s->ac_pred = get_bits1(gb);
4190 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4192 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4193 GET_MVDATA(dmv_x[0], dmv_y[0]);
4194 if (!mb_has_coeffs) {
4195 /* interpolated skipped block */
4196 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4197 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4201 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4203 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4206 s->ac_pred = get_bits1(gb);
4207 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4209 s->current_picture.qscale_table[mb_pos] = mquant;
4210 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4211 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4215 for (i = 0; i < 6; i++) {
4216 s->dc_val[0][s->block_index[i]] = 0;
4218 val = ((cbp >> (5 - i)) & 1);
4219 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4220 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4222 /* check if prediction blocks A and C are available */
4223 v->a_avail = v->c_avail = 0;
4224 if (i == 2 || i == 3 || !s->first_slice_line)
4225 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4226 if (i == 1 || i == 3 || s->mb_x)
4227 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4229 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4230 (i & 4) ? v->codingset2 : v->codingset);
4231 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4233 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4235 for (j = 0; j < 64; j++)
4236 s->block[i][j] <<= 1;
4237 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4239 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4240 first_block, s->dest[dst_idx] + off,
4241 (i & 4) ? s->uvlinesize : s->linesize,
4242 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4243 if (!v->ttmbf && ttmb < 8)
4250 /** Decode one B-frame MB (in interlaced field B picture)
4252 static void vc1_decode_b_mb_intfi(VC1Context *v)
4254 MpegEncContext *s = &v->s;
4255 GetBitContext *gb = &s->gb;
4257 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4258 int cbp = 0; /* cbp decoding stuff */
4259 int mqdiff, mquant; /* MB quantization */
4260 int ttmb = v->ttfrm; /* MB Transform type */
4261 int mb_has_coeffs = 0; /* last_flag */
4262 int val; /* temp value */
4263 int first_block = 1;
4266 int dmv_x[2], dmv_y[2], pred_flag[2];
4267 int bmvtype = BMV_TYPE_BACKWARD;
4270 mquant = v->pq; /* Lossy initialization */
4273 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4274 if (idx_mbmode <= 1) { // intra MB
4275 s->mb_intra = v->is_intra[s->mb_x] = 1;
4276 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4277 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4278 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4280 s->current_picture.qscale_table[mb_pos] = mquant;
4281 /* Set DC scale - y and c use the same (not sure if necessary here) */
4282 s->y_dc_scale = s->y_dc_scale_table[mquant];
4283 s->c_dc_scale = s->c_dc_scale_table[mquant];
4284 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4285 mb_has_coeffs = idx_mbmode & 1;
4287 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4289 for (i = 0; i < 6; i++) {
4290 s->dc_val[0][s->block_index[i]] = 0;
4292 val = ((cbp >> (5 - i)) & 1);
4293 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4294 v->a_avail = v->c_avail = 0;
4295 if (i == 2 || i == 3 || !s->first_slice_line)
4296 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4297 if (i == 1 || i == 3 || s->mb_x)
4298 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4300 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4301 (i & 4) ? v->codingset2 : v->codingset);
4302 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4304 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4306 for (j = 0; j < 64; j++)
4307 s->block[i][j] <<= 1;
4308 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4309 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4310 // TODO: yet to perform loop filter
4313 s->mb_intra = v->is_intra[s->mb_x] = 0;
4314 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4315 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4317 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4319 fwd = v->forward_mb_plane[mb_pos];
4320 if (idx_mbmode <= 5) { // 1-MV
4322 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4323 pred_flag[0] = pred_flag[1] = 0;
4325 bmvtype = BMV_TYPE_FORWARD;
4327 bmvtype = decode012(gb);
4330 bmvtype = BMV_TYPE_BACKWARD;
4333 bmvtype = BMV_TYPE_DIRECT;
4336 bmvtype = BMV_TYPE_INTERPOLATED;
4337 interpmvp = get_bits1(gb);
4340 v->bmvtype = bmvtype;
4341 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4342 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4345 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4347 if (bmvtype == BMV_TYPE_DIRECT) {
4348 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4349 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4350 if (!s->next_picture_ptr->field_picture) {
4351 av_log(s->avctx, AV_LOG_ERROR, "Mixed field/frame direct mode not supported\n");
4355 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4356 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4357 mb_has_coeffs = !(idx_mbmode & 2);
4360 bmvtype = BMV_TYPE_FORWARD;
4361 v->bmvtype = bmvtype;
4362 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4363 for (i = 0; i < 6; i++) {
4365 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4366 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4367 val = ((v->fourmvbp >> (3 - i)) & 1);
4369 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4370 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4371 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4373 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4374 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4376 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4378 mb_has_coeffs = idx_mbmode & 1;
4381 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4385 s->current_picture.qscale_table[mb_pos] = mquant;
4386 if (!v->ttmbf && cbp) {
4387 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4390 for (i = 0; i < 6; i++) {
4391 s->dc_val[0][s->block_index[i]] = 0;
4393 val = ((cbp >> (5 - i)) & 1);
4394 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4396 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4397 first_block, s->dest[dst_idx] + off,
4398 (i & 4) ? s->uvlinesize : s->linesize,
4399 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4400 if (!v->ttmbf && ttmb < 8)
4408 /** Decode one B-frame MB (in interlaced frame B picture)
4410 static int vc1_decode_b_mb_intfr(VC1Context *v)
4412 MpegEncContext *s = &v->s;
4413 GetBitContext *gb = &s->gb;
4415 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4416 int cbp = 0; /* cbp decoding stuff */
4417 int mqdiff, mquant; /* MB quantization */
4418 int ttmb = v->ttfrm; /* MB Transform type */
4419 int mvsw = 0; /* motion vector switch */
4420 int mb_has_coeffs = 1; /* last_flag */
4421 int dmv_x, dmv_y; /* Differential MV components */
4422 int val; /* temp value */
4423 int first_block = 1;
4425 int skipped, direct, twomv = 0;
4426 int block_cbp = 0, pat, block_tt = 0;
4427 int idx_mbmode = 0, mvbp;
4428 int stride_y, fieldtx;
4429 int bmvtype = BMV_TYPE_BACKWARD;
4432 mquant = v->pq; /* Lossy initialization */
4435 skipped = get_bits1(gb);
4437 skipped = v->s.mbskip_table[mb_pos];
4440 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4441 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
4443 v->blk_mv_type[s->block_index[0]] = 1;
4444 v->blk_mv_type[s->block_index[1]] = 1;
4445 v->blk_mv_type[s->block_index[2]] = 1;
4446 v->blk_mv_type[s->block_index[3]] = 1;
4448 v->blk_mv_type[s->block_index[0]] = 0;
4449 v->blk_mv_type[s->block_index[1]] = 0;
4450 v->blk_mv_type[s->block_index[2]] = 0;
4451 v->blk_mv_type[s->block_index[3]] = 0;
4456 direct = get_bits1(gb);
4458 direct = v->direct_mb_plane[mb_pos];
4461 if (s->next_picture_ptr->field_picture)
4462 av_log(s->avctx, AV_LOG_WARNING, "Mixed frame/field direct mode not supported\n");
4463 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);
4464 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);
4465 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);
4466 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);
4469 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);
4470 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);
4471 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);
4472 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);
4474 for (i = 1; i < 4; i += 2) {
4475 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4476 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4477 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4478 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4481 for (i = 1; i < 4; i++) {
4482 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4483 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4484 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4485 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4490 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4491 for (i = 0; i < 4; i++) {
4492 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4493 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4494 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4495 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4497 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4498 s->mb_intra = v->is_intra[s->mb_x] = 1;
4499 for (i = 0; i < 6; i++)
4500 v->mb_type[0][s->block_index[i]] = 1;
4501 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4502 mb_has_coeffs = get_bits1(gb);
4504 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4505 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4507 s->current_picture.qscale_table[mb_pos] = mquant;
4508 /* Set DC scale - y and c use the same (not sure if necessary here) */
4509 s->y_dc_scale = s->y_dc_scale_table[mquant];
4510 s->c_dc_scale = s->c_dc_scale_table[mquant];
4512 for (i = 0; i < 6; i++) {
4513 s->dc_val[0][s->block_index[i]] = 0;
4515 val = ((cbp >> (5 - i)) & 1);
4516 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4517 v->a_avail = v->c_avail = 0;
4518 if (i == 2 || i == 3 || !s->first_slice_line)
4519 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4520 if (i == 1 || i == 3 || s->mb_x)
4521 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4523 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4524 (i & 4) ? v->codingset2 : v->codingset);
4525 if (i > 3 && (s->flags & CODEC_FLAG_GRAY))
4527 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4529 stride_y = s->linesize << fieldtx;
4530 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4532 stride_y = s->uvlinesize;
4535 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4538 s->mb_intra = v->is_intra[s->mb_x] = 0;
4540 if (skipped || !s->mb_intra) {
4541 bmvtype = decode012(gb);
4544 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4547 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4550 bmvtype = BMV_TYPE_INTERPOLATED;
4554 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4555 mvsw = get_bits1(gb);
4558 if (!skipped) { // inter MB
4559 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4561 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4563 if (bmvtype == BMV_TYPE_INTERPOLATED && twomv) {
4564 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4565 } else if (bmvtype == BMV_TYPE_INTERPOLATED || twomv) {
4566 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4570 for (i = 0; i < 6; i++)
4571 v->mb_type[0][s->block_index[i]] = 0;
4572 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4573 /* for all motion vector read MVDATA and motion compensate each block */
4577 for (i = 0; i < 4; i++) {
4578 vc1_mc_4mv_luma(v, i, 0, 0);
4579 vc1_mc_4mv_luma(v, i, 1, 1);
4581 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4582 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4587 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4589 for (i = 0; i < 4; i++) {
4592 val = ((mvbp >> (3 - i)) & 1);
4594 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4596 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4597 vc1_mc_4mv_luma(v, j, dir, dir);
4598 vc1_mc_4mv_luma(v, j+1, dir, dir);
4601 vc1_mc_4mv_chroma4(v, 0, 0, 0);
4602 vc1_mc_4mv_chroma4(v, 1, 1, 1);
4603 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4607 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4609 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4614 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4616 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4619 dir = bmvtype == BMV_TYPE_BACKWARD;
4626 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4627 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4631 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4632 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4635 for (i = 0; i < 2; i++) {
4636 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];
4637 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];
4638 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];
4639 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];
4642 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4643 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4646 vc1_mc_4mv_luma(v, 0, dir, 0);
4647 vc1_mc_4mv_luma(v, 1, dir, 0);
4648 vc1_mc_4mv_luma(v, 2, dir2, 0);
4649 vc1_mc_4mv_luma(v, 3, dir2, 0);
4650 vc1_mc_4mv_chroma4(v, dir, dir2, 0);
4652 dir = bmvtype == BMV_TYPE_BACKWARD;
4654 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4657 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4659 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4660 v->blk_mv_type[s->block_index[0]] = 1;
4661 v->blk_mv_type[s->block_index[1]] = 1;
4662 v->blk_mv_type[s->block_index[2]] = 1;
4663 v->blk_mv_type[s->block_index[3]] = 1;
4664 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4665 for (i = 0; i < 2; i++) {
4666 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];
4667 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];
4673 GET_MQUANT(); // p. 227
4674 s->current_picture.qscale_table[mb_pos] = mquant;
4675 if (!v->ttmbf && cbp)
4676 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4677 for (i = 0; i < 6; i++) {
4678 s->dc_val[0][s->block_index[i]] = 0;
4680 val = ((cbp >> (5 - i)) & 1);
4682 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4684 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4686 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4687 first_block, s->dest[dst_idx] + off,
4688 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4689 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4690 block_cbp |= pat << (i << 2);
4691 if (!v->ttmbf && ttmb < 8)
4699 for (i = 0; i < 6; i++) {
4700 v->mb_type[0][s->block_index[i]] = 0;
4701 s->dc_val[0][s->block_index[i]] = 0;
4703 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4704 s->current_picture.qscale_table[mb_pos] = 0;
4705 v->blk_mv_type[s->block_index[0]] = 0;
4706 v->blk_mv_type[s->block_index[1]] = 0;
4707 v->blk_mv_type[s->block_index[2]] = 0;
4708 v->blk_mv_type[s->block_index[3]] = 0;
4711 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4712 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4713 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4715 dir = bmvtype == BMV_TYPE_BACKWARD;
4716 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4721 for (i = 0; i < 2; i++) {
4722 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];
4723 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];
4724 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];
4725 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];
4728 v->blk_mv_type[s->block_index[0]] = 1;
4729 v->blk_mv_type[s->block_index[1]] = 1;
4730 v->blk_mv_type[s->block_index[2]] = 1;
4731 v->blk_mv_type[s->block_index[3]] = 1;
4732 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4733 for (i = 0; i < 2; i++) {
4734 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];
4735 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];
4742 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4747 if (s->mb_x == s->mb_width - 1)
4748 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4749 v->cbp[s->mb_x] = block_cbp;
4750 v->ttblk[s->mb_x] = block_tt;
4754 /** Decode blocks of I-frame
4756 static void vc1_decode_i_blocks(VC1Context *v)
4759 MpegEncContext *s = &v->s;
4764 /* select codingmode used for VLC tables selection */
4765 switch (v->y_ac_table_index) {
4767 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4770 v->codingset = CS_HIGH_MOT_INTRA;
4773 v->codingset = CS_MID_RATE_INTRA;
4777 switch (v->c_ac_table_index) {
4779 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4782 v->codingset2 = CS_HIGH_MOT_INTER;
4785 v->codingset2 = CS_MID_RATE_INTER;
4789 /* Set DC scale - y and c use the same */
4790 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4791 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4794 s->mb_x = s->mb_y = 0;
4796 s->first_slice_line = 1;
4797 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4799 init_block_index(v);
4800 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4802 ff_update_block_index(s);
4803 dst[0] = s->dest[0];
4804 dst[1] = dst[0] + 8;
4805 dst[2] = s->dest[0] + s->linesize * 8;
4806 dst[3] = dst[2] + 8;
4807 dst[4] = s->dest[1];
4808 dst[5] = s->dest[2];
4809 s->dsp.clear_blocks(s->block[0]);
4810 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4811 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4812 s->current_picture.qscale_table[mb_pos] = v->pq;
4813 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4814 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4816 // do actual MB decoding and displaying
4817 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4818 v->s.ac_pred = get_bits1(&v->s.gb);
4820 for (k = 0; k < 6; k++) {
4821 val = ((cbp >> (5 - k)) & 1);
4824 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4828 cbp |= val << (5 - k);
4830 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4832 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4834 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4835 if (v->pq >= 9 && v->overlap) {
4837 for (j = 0; j < 64; j++)
4838 s->block[k][j] <<= 1;
4839 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4842 for (j = 0; j < 64; j++)
4843 s->block[k][j] = (s->block[k][j] - 64) << 1;
4844 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4848 if (v->pq >= 9 && v->overlap) {
4850 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4851 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4852 if (!(s->flags & CODEC_FLAG_GRAY)) {
4853 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4854 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4857 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4858 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4859 if (!s->first_slice_line) {
4860 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4861 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4862 if (!(s->flags & CODEC_FLAG_GRAY)) {
4863 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4864 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4867 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4868 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4870 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4872 if (get_bits_count(&s->gb) > v->bits) {
4873 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4874 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4875 get_bits_count(&s->gb), v->bits);
4879 if (!v->s.loop_filter)
4880 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4882 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4884 s->first_slice_line = 0;
4886 if (v->s.loop_filter)
4887 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4889 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4890 * profile, these only differ are when decoding MSS2 rectangles. */
4891 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4894 /** Decode blocks of I-frame for advanced profile
4896 static void vc1_decode_i_blocks_adv(VC1Context *v)
4899 MpegEncContext *s = &v->s;
4905 GetBitContext *gb = &s->gb;
4907 /* select codingmode used for VLC tables selection */
4908 switch (v->y_ac_table_index) {
4910 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4913 v->codingset = CS_HIGH_MOT_INTRA;
4916 v->codingset = CS_MID_RATE_INTRA;
4920 switch (v->c_ac_table_index) {
4922 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4925 v->codingset2 = CS_HIGH_MOT_INTER;
4928 v->codingset2 = CS_MID_RATE_INTER;
4933 s->mb_x = s->mb_y = 0;
4935 s->first_slice_line = 1;
4936 s->mb_y = s->start_mb_y;
4937 if (s->start_mb_y) {
4939 init_block_index(v);
4940 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4941 (1 + s->b8_stride) * sizeof(*s->coded_block));
4943 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4945 init_block_index(v);
4946 for (;s->mb_x < s->mb_width; s->mb_x++) {
4947 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4948 ff_update_block_index(s);
4949 s->dsp.clear_blocks(block[0]);
4950 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4951 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4952 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4953 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4955 // do actual MB decoding and displaying
4956 if (v->fieldtx_is_raw)
4957 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4958 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4959 if ( v->acpred_is_raw)
4960 v->s.ac_pred = get_bits1(&v->s.gb);
4962 v->s.ac_pred = v->acpred_plane[mb_pos];
4964 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4965 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4969 s->current_picture.qscale_table[mb_pos] = mquant;
4970 /* Set DC scale - y and c use the same */
4971 s->y_dc_scale = s->y_dc_scale_table[mquant];
4972 s->c_dc_scale = s->c_dc_scale_table[mquant];
4974 for (k = 0; k < 6; k++) {
4975 val = ((cbp >> (5 - k)) & 1);
4978 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4982 cbp |= val << (5 - k);
4984 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4985 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4987 vc1_decode_i_block_adv(v, block[k], k, val,
4988 (k < 4) ? v->codingset : v->codingset2, mquant);
4990 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4992 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4995 vc1_smooth_overlap_filter_iblk(v);
4996 vc1_put_signed_blocks_clamped(v);
4997 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4999 if (get_bits_count(&s->gb) > v->bits) {
5000 // TODO: may need modification to handle slice coding
5001 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5002 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
5003 get_bits_count(&s->gb), v->bits);
5007 if (!v->s.loop_filter)
5008 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5010 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
5011 s->first_slice_line = 0;
5014 /* raw bottom MB row */
5016 init_block_index(v);
5018 for (;s->mb_x < s->mb_width; s->mb_x++) {
5019 ff_update_block_index(s);
5020 vc1_put_signed_blocks_clamped(v);
5021 if (v->s.loop_filter)
5022 vc1_loop_filter_iblk_delayed(v, v->pq);
5024 if (v->s.loop_filter)
5025 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
5026 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5027 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5030 static void vc1_decode_p_blocks(VC1Context *v)
5032 MpegEncContext *s = &v->s;
5033 int apply_loop_filter;
5035 /* select codingmode used for VLC tables selection */
5036 switch (v->c_ac_table_index) {
5038 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5041 v->codingset = CS_HIGH_MOT_INTRA;
5044 v->codingset = CS_MID_RATE_INTRA;
5048 switch (v->c_ac_table_index) {
5050 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5053 v->codingset2 = CS_HIGH_MOT_INTER;
5056 v->codingset2 = CS_MID_RATE_INTER;
5060 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY) &&
5061 v->fcm == PROGRESSIVE;
5062 s->first_slice_line = 1;
5063 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5064 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5066 init_block_index(v);
5067 for (; s->mb_x < s->mb_width; s->mb_x++) {
5068 ff_update_block_index(s);
5070 if (v->fcm == ILACE_FIELD)
5071 vc1_decode_p_mb_intfi(v);
5072 else if (v->fcm == ILACE_FRAME)
5073 vc1_decode_p_mb_intfr(v);
5074 else vc1_decode_p_mb(v);
5075 if (s->mb_y != s->start_mb_y && apply_loop_filter)
5076 vc1_apply_p_loop_filter(v);
5077 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5078 // TODO: may need modification to handle slice coding
5079 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5080 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5081 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5085 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5086 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5087 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5088 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5089 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5090 s->first_slice_line = 0;
5092 if (apply_loop_filter) {
5094 init_block_index(v);
5095 for (; s->mb_x < s->mb_width; s->mb_x++) {
5096 ff_update_block_index(s);
5097 vc1_apply_p_loop_filter(v);
5100 if (s->end_mb_y >= s->start_mb_y)
5101 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5102 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5103 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5106 static void vc1_decode_b_blocks(VC1Context *v)
5108 MpegEncContext *s = &v->s;
5110 /* select codingmode used for VLC tables selection */
5111 switch (v->c_ac_table_index) {
5113 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5116 v->codingset = CS_HIGH_MOT_INTRA;
5119 v->codingset = CS_MID_RATE_INTRA;
5123 switch (v->c_ac_table_index) {
5125 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5128 v->codingset2 = CS_HIGH_MOT_INTER;
5131 v->codingset2 = CS_MID_RATE_INTER;
5135 s->first_slice_line = 1;
5136 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5138 init_block_index(v);
5139 for (; s->mb_x < s->mb_width; s->mb_x++) {
5140 ff_update_block_index(s);
5142 if (v->fcm == ILACE_FIELD)
5143 vc1_decode_b_mb_intfi(v);
5144 else if (v->fcm == ILACE_FRAME)
5145 vc1_decode_b_mb_intfr(v);
5148 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5149 // TODO: may need modification to handle slice coding
5150 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5151 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5152 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5155 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5157 if (!v->s.loop_filter)
5158 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5160 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5161 s->first_slice_line = 0;
5163 if (v->s.loop_filter)
5164 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5165 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5166 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5169 static void vc1_decode_skip_blocks(VC1Context *v)
5171 MpegEncContext *s = &v->s;
5173 if (!v->s.last_picture.f->data[0])
5176 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5177 s->first_slice_line = 1;
5178 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5180 init_block_index(v);
5181 ff_update_block_index(s);
5182 memcpy(s->dest[0], s->last_picture.f->data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5183 memcpy(s->dest[1], s->last_picture.f->data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5184 memcpy(s->dest[2], s->last_picture.f->data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5185 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5186 s->first_slice_line = 0;
5188 s->pict_type = AV_PICTURE_TYPE_P;
5191 void ff_vc1_decode_blocks(VC1Context *v)
5194 v->s.esc3_level_length = 0;
5196 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5199 v->left_blk_idx = -1;
5200 v->topleft_blk_idx = 1;
5202 switch (v->s.pict_type) {
5203 case AV_PICTURE_TYPE_I:
5204 if (v->profile == PROFILE_ADVANCED)
5205 vc1_decode_i_blocks_adv(v);
5207 vc1_decode_i_blocks(v);
5209 case AV_PICTURE_TYPE_P:
5210 if (v->p_frame_skipped)
5211 vc1_decode_skip_blocks(v);
5213 vc1_decode_p_blocks(v);
5215 case AV_PICTURE_TYPE_B:
5217 if (v->profile == PROFILE_ADVANCED)
5218 vc1_decode_i_blocks_adv(v);
5220 vc1_decode_i_blocks(v);
5222 vc1_decode_b_blocks(v);
5228 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5232 * Transform coefficients for both sprites in 16.16 fixed point format,
5233 * in the order they appear in the bitstream:
5235 * rotation 1 (unused)
5237 * rotation 2 (unused)
5244 int effect_type, effect_flag;
5245 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5246 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5249 static inline int get_fp_val(GetBitContext* gb)
5251 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5254 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5258 switch (get_bits(gb, 2)) {
5261 c[2] = get_fp_val(gb);
5265 c[0] = c[4] = get_fp_val(gb);
5266 c[2] = get_fp_val(gb);
5269 c[0] = get_fp_val(gb);
5270 c[2] = get_fp_val(gb);
5271 c[4] = get_fp_val(gb);
5274 c[0] = get_fp_val(gb);
5275 c[1] = get_fp_val(gb);
5276 c[2] = get_fp_val(gb);
5277 c[3] = get_fp_val(gb);
5278 c[4] = get_fp_val(gb);
5281 c[5] = get_fp_val(gb);
5283 c[6] = get_fp_val(gb);
5288 static int vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5290 AVCodecContext *avctx = v->s.avctx;
5293 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5294 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5295 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5296 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5297 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5298 for (i = 0; i < 7; i++)
5299 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5300 sd->coefs[sprite][i] / (1<<16),
5301 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5302 av_log(avctx, AV_LOG_DEBUG, "\n");
5306 if (sd->effect_type = get_bits_long(gb, 30)) {
5307 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5309 vc1_sprite_parse_transform(gb, sd->effect_params1);
5312 vc1_sprite_parse_transform(gb, sd->effect_params1);
5313 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5316 for (i = 0; i < sd->effect_pcount1; i++)
5317 sd->effect_params1[i] = get_fp_val(gb);
5319 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5320 // effect 13 is simple alpha blending and matches the opacity above
5321 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5322 for (i = 0; i < sd->effect_pcount1; i++)
5323 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5324 sd->effect_params1[i] / (1 << 16),
5325 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5326 av_log(avctx, AV_LOG_DEBUG, "\n");
5329 sd->effect_pcount2 = get_bits(gb, 16);
5330 if (sd->effect_pcount2 > 10) {
5331 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5332 return AVERROR_INVALIDDATA;
5333 } else if (sd->effect_pcount2) {
5335 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5336 while (++i < sd->effect_pcount2) {
5337 sd->effect_params2[i] = get_fp_val(gb);
5338 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5339 sd->effect_params2[i] / (1 << 16),
5340 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5342 av_log(avctx, AV_LOG_DEBUG, "\n");
5345 if (sd->effect_flag = get_bits1(gb))
5346 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5348 if (get_bits_count(gb) >= gb->size_in_bits +
5349 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0)) {
5350 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5351 return AVERROR_INVALIDDATA;
5353 if (get_bits_count(gb) < gb->size_in_bits - 8)
5354 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5359 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5361 int i, plane, row, sprite;
5362 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5363 uint8_t* src_h[2][2];
5364 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5366 MpegEncContext *s = &v->s;
5368 for (i = 0; i <= v->two_sprites; i++) {
5369 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5370 xadv[i] = sd->coefs[i][0];
5371 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5372 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5374 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5375 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5377 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5379 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5380 int width = v->output_width>>!!plane;
5382 for (row = 0; row < v->output_height>>!!plane; row++) {
5383 uint8_t *dst = v->sprite_output_frame->data[plane] +
5384 v->sprite_output_frame->linesize[plane] * row;
5386 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5387 uint8_t *iplane = s->current_picture.f->data[plane];
5388 int iline = s->current_picture.f->linesize[plane];
5389 int ycoord = yoff[sprite] + yadv[sprite] * row;
5390 int yline = ycoord >> 16;
5392 ysub[sprite] = ycoord & 0xFFFF;
5394 iplane = s->last_picture.f->data[plane];
5395 iline = s->last_picture.f->linesize[plane];
5397 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5398 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5399 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5401 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5403 if (sr_cache[sprite][0] != yline) {
5404 if (sr_cache[sprite][1] == yline) {
5405 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5406 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5408 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5409 sr_cache[sprite][0] = yline;
5412 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5413 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5414 iplane + next_line, xoff[sprite],
5415 xadv[sprite], width);
5416 sr_cache[sprite][1] = yline + 1;
5418 src_h[sprite][0] = v->sr_rows[sprite][0];
5419 src_h[sprite][1] = v->sr_rows[sprite][1];
5423 if (!v->two_sprites) {
5425 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5427 memcpy(dst, src_h[0][0], width);
5430 if (ysub[0] && ysub[1]) {
5431 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5432 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5433 } else if (ysub[0]) {
5434 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5435 src_h[1][0], alpha, width);
5436 } else if (ysub[1]) {
5437 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5438 src_h[0][0], (1<<16)-1-alpha, width);
5440 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5446 for (i = 0; i <= v->two_sprites; i++) {
5456 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5459 MpegEncContext *s = &v->s;
5460 AVCodecContext *avctx = s->avctx;
5463 memset(&sd, 0, sizeof(sd));
5465 ret = vc1_parse_sprites(v, gb, &sd);
5469 if (!s->current_picture.f->data[0]) {
5470 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5474 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f->data[0])) {
5475 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5479 av_frame_unref(v->sprite_output_frame);
5480 if ((ret = ff_get_buffer(avctx, v->sprite_output_frame, 0)) < 0)
5483 vc1_draw_sprites(v, &sd);
5488 static void vc1_sprite_flush(AVCodecContext *avctx)
5490 VC1Context *v = avctx->priv_data;
5491 MpegEncContext *s = &v->s;
5492 AVFrame *f = s->current_picture.f;
5495 /* Windows Media Image codecs have a convergence interval of two keyframes.
5496 Since we can't enforce it, clear to black the missing sprite. This is
5497 wrong but it looks better than doing nothing. */
5500 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5501 for (i = 0; i < v->sprite_height>>!!plane; i++)
5502 memset(f->data[plane] + i * f->linesize[plane],
5503 plane ? 128 : 0, f->linesize[plane]);
5508 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5510 MpegEncContext *s = &v->s;
5512 int mb_height = FFALIGN(s->mb_height, 2);
5514 /* Allocate mb bitplanes */
5515 v->mv_type_mb_plane = av_malloc (s->mb_stride * mb_height);
5516 v->direct_mb_plane = av_malloc (s->mb_stride * mb_height);
5517 v->forward_mb_plane = av_malloc (s->mb_stride * mb_height);
5518 v->fieldtx_plane = av_mallocz(s->mb_stride * mb_height);
5519 v->acpred_plane = av_malloc (s->mb_stride * mb_height);
5520 v->over_flags_plane = av_malloc (s->mb_stride * mb_height);
5522 v->n_allocated_blks = s->mb_width + 2;
5523 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5524 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5525 v->cbp = v->cbp_base + s->mb_stride;
5526 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5527 v->ttblk = v->ttblk_base + s->mb_stride;
5528 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5529 v->is_intra = v->is_intra_base + s->mb_stride;
5530 v->luma_mv_base = av_mallocz(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5531 v->luma_mv = v->luma_mv_base + s->mb_stride;
5533 /* allocate block type info in that way so it could be used with s->block_index[] */
5534 v->mb_type_base = av_malloc(s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5535 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5536 v->mb_type[1] = v->mb_type_base + s->b8_stride * (mb_height * 2 + 1) + s->mb_stride + 1;
5537 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (mb_height + 1);
5539 /* allocate memory to store block level MV info */
5540 v->blk_mv_type_base = av_mallocz( s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5541 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5542 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5543 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5544 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5545 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2));
5546 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5547 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (mb_height * 2 + 1) + s->mb_stride * (mb_height + 1) * 2);
5549 /* Init coded blocks info */
5550 if (v->profile == PROFILE_ADVANCED) {
5551 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5553 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5557 ff_intrax8_common_init(&v->x8,s);
5559 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5560 for (i = 0; i < 4; i++)
5561 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width)))
5562 return AVERROR(ENOMEM);
5565 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5566 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5568 av_freep(&v->mv_type_mb_plane);
5569 av_freep(&v->direct_mb_plane);
5570 av_freep(&v->acpred_plane);
5571 av_freep(&v->over_flags_plane);
5572 av_freep(&v->block);
5573 av_freep(&v->cbp_base);
5574 av_freep(&v->ttblk_base);
5575 av_freep(&v->is_intra_base);
5576 av_freep(&v->luma_mv_base);
5577 av_freep(&v->mb_type_base);
5578 return AVERROR(ENOMEM);
5584 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5587 for (i = 0; i < 64; i++) {
5588 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5589 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5590 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5591 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5592 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5593 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5599 /** Initialize a VC1/WMV3 decoder
5600 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5601 * @todo TODO: Decypher remaining bits in extra_data
5603 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5605 VC1Context *v = avctx->priv_data;
5606 MpegEncContext *s = &v->s;
5610 /* save the container output size for WMImage */
5611 v->output_width = avctx->width;
5612 v->output_height = avctx->height;
5614 if (!avctx->extradata_size || !avctx->extradata)
5616 if (!(avctx->flags & CODEC_FLAG_GRAY))
5617 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5619 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5620 avctx->hwaccel = ff_find_hwaccel(avctx);
5623 if ((ret = ff_vc1_init_common(v)) < 0)
5625 // ensure static VLC tables are initialized
5626 if ((ret = ff_msmpeg4_decode_init(avctx)) < 0)
5628 if ((ret = ff_vc1_decode_init_alloc_tables(v)) < 0)
5630 // Hack to ensure the above functions will be called
5631 // again once we know all necessary settings.
5632 // That this is necessary might indicate a bug.
5633 ff_vc1_decode_end(avctx);
5635 ff_h264chroma_init(&v->h264chroma, 8);
5636 ff_vc1dsp_init(&v->vc1dsp);
5638 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5641 // looks like WMV3 has a sequence header stored in the extradata
5642 // advanced sequence header may be before the first frame
5643 // the last byte of the extradata is a version number, 1 for the
5644 // samples we can decode
5646 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5648 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0)
5651 count = avctx->extradata_size*8 - get_bits_count(&gb);
5653 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5654 count, get_bits(&gb, count));
5655 } else if (count < 0) {
5656 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5658 } else { // VC1/WVC1/WVP2
5659 const uint8_t *start = avctx->extradata;
5660 uint8_t *end = avctx->extradata + avctx->extradata_size;
5661 const uint8_t *next;
5662 int size, buf2_size;
5663 uint8_t *buf2 = NULL;
5664 int seq_initialized = 0, ep_initialized = 0;
5666 if (avctx->extradata_size < 16) {
5667 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5671 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5672 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5674 for (; next < end; start = next) {
5675 next = find_next_marker(start + 4, end);
5676 size = next - start - 4;
5679 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5680 init_get_bits(&gb, buf2, buf2_size * 8);
5681 switch (AV_RB32(start)) {
5682 case VC1_CODE_SEQHDR:
5683 if ((ret = ff_vc1_decode_sequence_header(avctx, v, &gb)) < 0) {
5687 seq_initialized = 1;
5689 case VC1_CODE_ENTRYPOINT:
5690 if ((ret = ff_vc1_decode_entry_point(avctx, v, &gb)) < 0) {
5699 if (!seq_initialized || !ep_initialized) {
5700 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5703 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5706 v->sprite_output_frame = av_frame_alloc();
5707 if (!v->sprite_output_frame)
5708 return AVERROR(ENOMEM);
5710 avctx->profile = v->profile;
5711 if (v->profile == PROFILE_ADVANCED)
5712 avctx->level = v->level;
5714 avctx->has_b_frames = !!avctx->max_b_frames;
5716 s->mb_width = (avctx->coded_width + 15) >> 4;
5717 s->mb_height = (avctx->coded_height + 15) >> 4;
5719 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5720 ff_vc1_init_transposed_scantables(v);
5722 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5727 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5728 v->sprite_width = avctx->coded_width;
5729 v->sprite_height = avctx->coded_height;
5731 avctx->coded_width = avctx->width = v->output_width;
5732 avctx->coded_height = avctx->height = v->output_height;
5734 // prevent 16.16 overflows
5735 if (v->sprite_width > 1 << 14 ||
5736 v->sprite_height > 1 << 14 ||
5737 v->output_width > 1 << 14 ||
5738 v->output_height > 1 << 14) return -1;
5740 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5741 avpriv_request_sample(avctx, "odd sprites support");
5742 return AVERROR_PATCHWELCOME;
5748 /** Close a VC1/WMV3 decoder
5749 * @warning Initial try at using MpegEncContext stuff
5751 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5753 VC1Context *v = avctx->priv_data;
5756 av_frame_free(&v->sprite_output_frame);
5758 for (i = 0; i < 4; i++)
5759 av_freep(&v->sr_rows[i >> 1][i & 1]);
5760 av_freep(&v->hrd_rate);
5761 av_freep(&v->hrd_buffer);
5762 ff_MPV_common_end(&v->s);
5763 av_freep(&v->mv_type_mb_plane);
5764 av_freep(&v->direct_mb_plane);
5765 av_freep(&v->forward_mb_plane);
5766 av_freep(&v->fieldtx_plane);
5767 av_freep(&v->acpred_plane);
5768 av_freep(&v->over_flags_plane);
5769 av_freep(&v->mb_type_base);
5770 av_freep(&v->blk_mv_type_base);
5771 av_freep(&v->mv_f_base);
5772 av_freep(&v->mv_f_next_base);
5773 av_freep(&v->block);
5774 av_freep(&v->cbp_base);
5775 av_freep(&v->ttblk_base);
5776 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5777 av_freep(&v->luma_mv_base);
5778 ff_intrax8_common_end(&v->x8);
5783 /** Decode a VC1/WMV3 frame
5784 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5786 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5787 int *got_frame, AVPacket *avpkt)
5789 const uint8_t *buf = avpkt->data;
5790 int buf_size = avpkt->size, n_slices = 0, i, ret;
5791 VC1Context *v = avctx->priv_data;
5792 MpegEncContext *s = &v->s;
5793 AVFrame *pict = data;
5794 uint8_t *buf2 = NULL;
5795 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5796 int mb_height, n_slices1=-1;
5801 } *slices = NULL, *tmp;
5803 v->second_field = 0;
5805 if(s->flags & CODEC_FLAG_LOW_DELAY)
5808 /* no supplementary picture */
5809 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5810 /* special case for last picture */
5811 if (s->low_delay == 0 && s->next_picture_ptr) {
5812 if ((ret = av_frame_ref(pict, s->next_picture_ptr->f)) < 0)
5814 s->next_picture_ptr = NULL;
5822 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5823 if (v->profile < PROFILE_ADVANCED)
5824 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5826 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5829 //for advanced profile we may need to parse and unescape data
5830 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5832 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5834 return AVERROR(ENOMEM);
5836 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5837 const uint8_t *start, *end, *next;
5841 for (start = buf, end = buf + buf_size; next < end; start = next) {
5842 next = find_next_marker(start + 4, end);
5843 size = next - start - 4;
5844 if (size <= 0) continue;
5845 switch (AV_RB32(start)) {
5846 case VC1_CODE_FRAME:
5847 if (avctx->hwaccel ||
5848 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5850 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5852 case VC1_CODE_FIELD: {
5854 if (avctx->hwaccel ||
5855 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5856 buf_start_second_field = start;
5857 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5861 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5862 if (!slices[n_slices].buf)
5864 buf_size3 = vc1_unescape_buffer(start + 4, size,
5865 slices[n_slices].buf);
5866 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5868 /* assuming that the field marker is at the exact middle,
5869 hope it's correct */
5870 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5871 n_slices1 = n_slices - 1; // index of the last slice of the first field
5875 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5876 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5877 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5878 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5880 case VC1_CODE_SLICE: {
5882 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5886 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5887 if (!slices[n_slices].buf)
5889 buf_size3 = vc1_unescape_buffer(start + 4, size,
5890 slices[n_slices].buf);
5891 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5893 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5899 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5900 const uint8_t *divider;
5903 divider = find_next_marker(buf, buf + buf_size);
5904 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5905 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5907 } else { // found field marker, unescape second field
5908 if (avctx->hwaccel ||
5909 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5910 buf_start_second_field = divider;
5911 tmp = av_realloc_array(slices, sizeof(*slices), (n_slices+1));
5915 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5916 if (!slices[n_slices].buf)
5918 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5919 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5921 slices[n_slices].mby_start = s->mb_height + 1 >> 1;
5922 n_slices1 = n_slices - 1;
5925 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5927 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5929 init_get_bits(&s->gb, buf2, buf_size2*8);
5931 init_get_bits(&s->gb, buf, buf_size*8);
5933 if (v->res_sprite) {
5934 v->new_sprite = !get_bits1(&s->gb);
5935 v->two_sprites = get_bits1(&s->gb);
5936 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5937 we're using the sprite compositor. These are intentionally kept separate
5938 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5939 the vc1 one for WVP2 */
5940 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5941 if (v->new_sprite) {
5942 // switch AVCodecContext parameters to those of the sprites
5943 avctx->width = avctx->coded_width = v->sprite_width;
5944 avctx->height = avctx->coded_height = v->sprite_height;
5951 if (s->context_initialized &&
5952 (s->width != avctx->coded_width ||
5953 s->height != avctx->coded_height)) {
5954 ff_vc1_decode_end(avctx);
5957 if (!s->context_initialized) {
5958 if (ff_msmpeg4_decode_init(avctx) < 0)
5960 if (ff_vc1_decode_init_alloc_tables(v) < 0) {
5961 ff_MPV_common_end(s);
5965 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5967 if (v->profile == PROFILE_ADVANCED) {
5968 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5970 s->h_edge_pos = avctx->coded_width;
5971 s->v_edge_pos = avctx->coded_height;
5975 // do parse frame header
5976 v->pic_header_flag = 0;
5977 v->first_pic_header_flag = 1;
5978 if (v->profile < PROFILE_ADVANCED) {
5979 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5983 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5987 v->first_pic_header_flag = 0;
5989 if (avctx->debug & FF_DEBUG_PICT_INFO)
5990 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5992 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5993 && s->pict_type != AV_PICTURE_TYPE_I) {
5994 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5998 if ((s->mb_height >> v->field_mode) == 0) {
5999 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
6003 // for skipping the frame
6004 s->current_picture.f->pict_type = s->pict_type;
6005 s->current_picture.f->key_frame = s->pict_type == AV_PICTURE_TYPE_I;
6007 /* skip B-frames if we don't have reference frames */
6008 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
6011 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
6012 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
6013 avctx->skip_frame >= AVDISCARD_ALL) {
6017 if (s->next_p_frame_damaged) {
6018 if (s->pict_type == AV_PICTURE_TYPE_B)
6021 s->next_p_frame_damaged = 0;
6024 if (ff_MPV_frame_start(s, avctx) < 0) {
6028 v->s.current_picture_ptr->field_picture = v->field_mode;
6029 v->s.current_picture_ptr->f->interlaced_frame = (v->fcm != PROGRESSIVE);
6030 v->s.current_picture_ptr->f->top_field_first = v->tff;
6032 // process pulldown flags
6033 s->current_picture_ptr->f->repeat_pict = 0;
6034 // Pulldown flags are only valid when 'broadcast' has been set.
6035 // So ticks_per_frame will be 2
6038 s->current_picture_ptr->f->repeat_pict = 1;
6039 } else if (v->rptfrm) {
6041 s->current_picture_ptr->f->repeat_pict = v->rptfrm * 2;
6044 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
6045 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
6047 if ((CONFIG_VC1_VDPAU_DECODER)
6048 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
6049 if (v->field_mode && buf_start_second_field) {
6050 ff_vdpau_vc1_decode_picture(s, buf_start, buf_start_second_field - buf_start);
6051 ff_vdpau_vc1_decode_picture(s, buf_start_second_field, (buf + buf_size) - buf_start_second_field);
6053 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
6055 } else if (avctx->hwaccel) {
6056 if (v->field_mode && buf_start_second_field) {
6057 // decode first field
6058 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
6059 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6061 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
6063 if (avctx->hwaccel->end_frame(avctx) < 0)
6066 // decode second field
6067 s->gb = slices[n_slices1 + 1].gb;
6068 s->picture_structure = PICT_TOP_FIELD + v->tff;
6069 v->second_field = 1;
6070 v->pic_header_flag = 0;
6071 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6072 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6075 v->s.current_picture_ptr->f->pict_type = v->s.pict_type;
6077 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6079 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6081 if (avctx->hwaccel->end_frame(avctx) < 0)
6084 s->picture_structure = PICT_FRAME;
6085 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6087 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6089 if (avctx->hwaccel->end_frame(avctx) < 0)
6095 ff_mpeg_er_frame_start(s);
6097 v->bits = buf_size * 8;
6098 v->end_mb_x = s->mb_width;
6099 if (v->field_mode) {
6100 s->current_picture.f->linesize[0] <<= 1;
6101 s->current_picture.f->linesize[1] <<= 1;
6102 s->current_picture.f->linesize[2] <<= 1;
6104 s->uvlinesize <<= 1;
6106 mb_height = s->mb_height >> v->field_mode;
6108 av_assert0 (mb_height > 0);
6110 for (i = 0; i <= n_slices; i++) {
6111 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6112 if (v->field_mode <= 0) {
6113 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6114 "picture boundary (%d >= %d)\n", i,
6115 slices[i - 1].mby_start, mb_height);
6118 v->second_field = 1;
6119 av_assert0((s->mb_height & 1) == 0);
6120 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6121 v->mb_off = s->mb_stride * s->mb_height >> 1;
6123 v->second_field = 0;
6128 v->pic_header_flag = 0;
6129 if (v->field_mode && i == n_slices1 + 2) {
6130 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6131 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6132 if (avctx->err_recognition & AV_EF_EXPLODE)
6136 } else if (get_bits1(&s->gb)) {
6137 v->pic_header_flag = 1;
6138 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6139 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6140 if (avctx->err_recognition & AV_EF_EXPLODE)
6148 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6149 if (!v->field_mode || v->second_field)
6150 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6152 if (i >= n_slices) {
6153 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6156 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6158 if (s->end_mb_y <= s->start_mb_y) {
6159 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6162 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6163 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6166 ff_vc1_decode_blocks(v);
6168 s->gb = slices[i].gb;
6170 if (v->field_mode) {
6171 v->second_field = 0;
6172 s->current_picture.f->linesize[0] >>= 1;
6173 s->current_picture.f->linesize[1] >>= 1;
6174 s->current_picture.f->linesize[2] >>= 1;
6176 s->uvlinesize >>= 1;
6177 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6178 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6179 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6182 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6183 get_bits_count(&s->gb), s->gb.size_in_bits);
6184 // if (get_bits_count(&s->gb) > buf_size * 8)
6186 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6189 ff_er_frame_end(&s->er);
6192 ff_MPV_frame_end(s);
6194 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6196 avctx->width = avctx->coded_width = v->output_width;
6197 avctx->height = avctx->coded_height = v->output_height;
6198 if (avctx->skip_frame >= AVDISCARD_NONREF)
6200 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6201 if (vc1_decode_sprites(v, &s->gb))
6204 if ((ret = av_frame_ref(pict, v->sprite_output_frame)) < 0)
6208 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6209 if ((ret = av_frame_ref(pict, s->current_picture_ptr->f)) < 0)
6211 ff_print_debug_info(s, s->current_picture_ptr, pict);
6213 } else if (s->last_picture_ptr != NULL) {
6214 if ((ret = av_frame_ref(pict, s->last_picture_ptr->f)) < 0)
6216 ff_print_debug_info(s, s->last_picture_ptr, pict);
6223 for (i = 0; i < n_slices; i++)
6224 av_free(slices[i].buf);
6230 for (i = 0; i < n_slices; i++)
6231 av_free(slices[i].buf);
6237 static const AVProfile profiles[] = {
6238 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6239 { FF_PROFILE_VC1_MAIN, "Main" },
6240 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6241 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6242 { FF_PROFILE_UNKNOWN },
6245 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6246 #if CONFIG_VC1_DXVA2_HWACCEL
6247 AV_PIX_FMT_DXVA2_VLD,
6249 #if CONFIG_VC1_VAAPI_HWACCEL
6250 AV_PIX_FMT_VAAPI_VLD,
6252 #if CONFIG_VC1_VDPAU_HWACCEL
6259 AVCodec ff_vc1_decoder = {
6261 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6262 .type = AVMEDIA_TYPE_VIDEO,
6263 .id = AV_CODEC_ID_VC1,
6264 .priv_data_size = sizeof(VC1Context),
6265 .init = vc1_decode_init,
6266 .close = ff_vc1_decode_end,
6267 .decode = vc1_decode_frame,
6268 .flush = ff_mpeg_flush,
6269 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6270 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6271 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6274 #if CONFIG_WMV3_DECODER
6275 AVCodec ff_wmv3_decoder = {
6277 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6278 .type = AVMEDIA_TYPE_VIDEO,
6279 .id = AV_CODEC_ID_WMV3,
6280 .priv_data_size = sizeof(VC1Context),
6281 .init = vc1_decode_init,
6282 .close = ff_vc1_decode_end,
6283 .decode = vc1_decode_frame,
6284 .flush = ff_mpeg_flush,
6285 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6286 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6287 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6291 #if CONFIG_WMV3_VDPAU_DECODER
6292 AVCodec ff_wmv3_vdpau_decoder = {
6293 .name = "wmv3_vdpau",
6294 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6295 .type = AVMEDIA_TYPE_VIDEO,
6296 .id = AV_CODEC_ID_WMV3,
6297 .priv_data_size = sizeof(VC1Context),
6298 .init = vc1_decode_init,
6299 .close = ff_vc1_decode_end,
6300 .decode = vc1_decode_frame,
6301 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6302 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6303 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6307 #if CONFIG_VC1_VDPAU_DECODER
6308 AVCodec ff_vc1_vdpau_decoder = {
6309 .name = "vc1_vdpau",
6310 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6311 .type = AVMEDIA_TYPE_VIDEO,
6312 .id = AV_CODEC_ID_VC1,
6313 .priv_data_size = sizeof(VC1Context),
6314 .init = vc1_decode_init,
6315 .close = ff_vc1_decode_end,
6316 .decode = vc1_decode_frame,
6317 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6318 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6319 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6323 #if CONFIG_WMV3IMAGE_DECODER
6324 AVCodec ff_wmv3image_decoder = {
6325 .name = "wmv3image",
6326 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6327 .type = AVMEDIA_TYPE_VIDEO,
6328 .id = AV_CODEC_ID_WMV3IMAGE,
6329 .priv_data_size = sizeof(VC1Context),
6330 .init = vc1_decode_init,
6331 .close = ff_vc1_decode_end,
6332 .decode = vc1_decode_frame,
6333 .capabilities = CODEC_CAP_DR1,
6334 .flush = vc1_sprite_flush,
6335 .pix_fmts = (const enum AVPixelFormat[]) {
6342 #if CONFIG_VC1IMAGE_DECODER
6343 AVCodec ff_vc1image_decoder = {
6345 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6346 .type = AVMEDIA_TYPE_VIDEO,
6347 .id = AV_CODEC_ID_VC1IMAGE,
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[]) {