2 * VC-1 and WMV3 decoder
3 * Copyright (c) 2011 Mashiat Sarker Shakkhar
4 * Copyright (c) 2006-2007 Konstantin Shishkov
5 * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
7 * This file is part of FFmpeg.
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
26 * VC-1 and WMV3 decoder
31 #include "error_resilience.h"
32 #include "mpegvideo.h"
34 #include "h264chroma.h"
37 #include "vc1acdata.h"
38 #include "msmpeg4data.h"
41 #include "vdpau_internal.h"
42 #include "libavutil/avassert.h"
47 #define MB_INTRA_VLC_BITS 9
51 // offset tables for interlaced picture MVDATA decoding
52 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
53 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
55 /***********************************************************************/
57 * @name VC-1 Bitplane decoding
75 /** @} */ //imode defines
77 static void init_block_index(VC1Context *v)
79 MpegEncContext *s = &v->s;
80 ff_init_block_index(s);
81 if (v->field_mode && !(v->second_field ^ v->tff)) {
82 s->dest[0] += s->current_picture_ptr->f.linesize[0];
83 s->dest[1] += s->current_picture_ptr->f.linesize[1];
84 s->dest[2] += s->current_picture_ptr->f.linesize[2];
89 /** @} */ //Bitplane group
91 static void vc1_put_signed_blocks_clamped(VC1Context *v)
93 MpegEncContext *s = &v->s;
94 int topleft_mb_pos, top_mb_pos;
95 int stride_y, fieldtx = 0;
98 /* The put pixels loop is always one MB row behind the decoding loop,
99 * because we can only put pixels when overlap filtering is done, and
100 * for filtering of the bottom edge of a MB, we need the next MB row
102 * Within the row, the put pixels loop is also one MB col behind the
103 * decoding loop. The reason for this is again, because for filtering
104 * of the right MB edge, we need the next MB present. */
105 if (!s->first_slice_line) {
107 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
108 if (v->fcm == ILACE_FRAME)
109 fieldtx = v->fieldtx_plane[topleft_mb_pos];
110 stride_y = s->linesize << fieldtx;
111 v_dist = (16 - fieldtx) >> (fieldtx == 0);
112 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
113 s->dest[0] - 16 * s->linesize - 16,
115 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
116 s->dest[0] - 16 * s->linesize - 8,
118 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
119 s->dest[0] - v_dist * s->linesize - 16,
121 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
122 s->dest[0] - v_dist * s->linesize - 8,
124 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
125 s->dest[1] - 8 * s->uvlinesize - 8,
127 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
128 s->dest[2] - 8 * s->uvlinesize - 8,
131 if (s->mb_x == s->mb_width - 1) {
132 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
133 if (v->fcm == ILACE_FRAME)
134 fieldtx = v->fieldtx_plane[top_mb_pos];
135 stride_y = s->linesize << fieldtx;
136 v_dist = fieldtx ? 15 : 8;
137 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
138 s->dest[0] - 16 * s->linesize,
140 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
141 s->dest[0] - 16 * s->linesize + 8,
143 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
144 s->dest[0] - v_dist * s->linesize,
146 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
147 s->dest[0] - v_dist * s->linesize + 8,
149 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
150 s->dest[1] - 8 * s->uvlinesize,
152 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
153 s->dest[2] - 8 * s->uvlinesize,
158 #define inc_blk_idx(idx) do { \
160 if (idx >= v->n_allocated_blks) \
164 inc_blk_idx(v->topleft_blk_idx);
165 inc_blk_idx(v->top_blk_idx);
166 inc_blk_idx(v->left_blk_idx);
167 inc_blk_idx(v->cur_blk_idx);
170 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
172 MpegEncContext *s = &v->s;
174 if (!s->first_slice_line) {
175 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
177 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
178 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
179 for (j = 0; j < 2; j++) {
180 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
182 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
185 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
187 if (s->mb_y == s->end_mb_y - 1) {
189 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
190 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
191 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
193 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
197 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
199 MpegEncContext *s = &v->s;
202 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
203 * means it runs two rows/cols behind the decoding loop. */
204 if (!s->first_slice_line) {
206 if (s->mb_y >= s->start_mb_y + 2) {
207 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
210 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
211 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
212 for (j = 0; j < 2; j++) {
213 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
215 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
219 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
222 if (s->mb_x == s->mb_width - 1) {
223 if (s->mb_y >= s->start_mb_y + 2) {
224 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
227 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
228 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
229 for (j = 0; j < 2; j++) {
230 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
232 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
236 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
239 if (s->mb_y == s->end_mb_y) {
242 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
243 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
245 for (j = 0; j < 2; j++) {
246 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
251 if (s->mb_x == s->mb_width - 1) {
253 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
254 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
256 for (j = 0; j < 2; j++) {
257 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
265 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
267 MpegEncContext *s = &v->s;
270 if (v->condover == CONDOVER_NONE)
273 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
275 /* Within a MB, the horizontal overlap always runs before the vertical.
276 * To accomplish that, we run the H on left and internal borders of the
277 * currently decoded MB. Then, we wait for the next overlap iteration
278 * to do H overlap on the right edge of this MB, before moving over and
279 * running the V overlap. Therefore, the V overlap makes us trail by one
280 * MB col and the H overlap filter makes us trail by one MB row. This
281 * is reflected in the time at which we run the put_pixels loop. */
282 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
283 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
284 v->over_flags_plane[mb_pos - 1])) {
285 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
286 v->block[v->cur_blk_idx][0]);
287 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
288 v->block[v->cur_blk_idx][2]);
289 if (!(s->flags & CODEC_FLAG_GRAY)) {
290 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
291 v->block[v->cur_blk_idx][4]);
292 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
293 v->block[v->cur_blk_idx][5]);
296 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
297 v->block[v->cur_blk_idx][1]);
298 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
299 v->block[v->cur_blk_idx][3]);
301 if (s->mb_x == s->mb_width - 1) {
302 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
303 v->over_flags_plane[mb_pos - s->mb_stride])) {
304 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
305 v->block[v->cur_blk_idx][0]);
306 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
307 v->block[v->cur_blk_idx][1]);
308 if (!(s->flags & CODEC_FLAG_GRAY)) {
309 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
310 v->block[v->cur_blk_idx][4]);
311 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
312 v->block[v->cur_blk_idx][5]);
315 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
316 v->block[v->cur_blk_idx][2]);
317 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
318 v->block[v->cur_blk_idx][3]);
321 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
322 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
323 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
324 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
325 v->block[v->left_blk_idx][0]);
326 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
327 v->block[v->left_blk_idx][1]);
328 if (!(s->flags & CODEC_FLAG_GRAY)) {
329 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
330 v->block[v->left_blk_idx][4]);
331 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
332 v->block[v->left_blk_idx][5]);
335 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
336 v->block[v->left_blk_idx][2]);
337 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
338 v->block[v->left_blk_idx][3]);
342 /** Do motion compensation over 1 macroblock
343 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
345 static void vc1_mc_1mv(VC1Context *v, int dir)
347 MpegEncContext *s = &v->s;
348 H264ChromaContext *h264chroma = &v->h264chroma;
349 uint8_t *srcY, *srcU, *srcV;
350 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
352 int v_edge_pos = s->v_edge_pos >> v->field_mode;
354 const uint8_t *luty, *lutuv;
357 if ((!v->field_mode ||
358 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
359 !v->s.last_picture.f.data[0])
362 mx = s->mv[dir][0][0];
363 my = s->mv[dir][0][1];
365 // store motion vectors for further use in B frames
366 if (s->pict_type == AV_PICTURE_TYPE_P) {
367 for (i = 0; i < 4; i++) {
368 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][0] = mx;
369 s->current_picture.motion_val[1][s->block_index[i] + v->blocks_off][1] = my;
373 uvmx = (mx + ((mx & 3) == 3)) >> 1;
374 uvmy = (my + ((my & 3) == 3)) >> 1;
375 v->luma_mv[s->mb_x][0] = uvmx;
376 v->luma_mv[s->mb_x][1] = uvmy;
379 v->cur_field_type != v->ref_field_type[dir]) {
380 my = my - 2 + 4 * v->cur_field_type;
381 uvmy = uvmy - 2 + 4 * v->cur_field_type;
384 // fastuvmc shall be ignored for interlaced frame picture
385 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
386 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
387 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
390 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
391 srcY = s->current_picture.f.data[0];
392 srcU = s->current_picture.f.data[1];
393 srcV = s->current_picture.f.data[2];
394 luty = v->curr_luty [v->ref_field_type[dir]];
395 lutuv= v->curr_lutuv[v->ref_field_type[dir]];
396 use_ic=v->curr_use_ic;
398 srcY = s->last_picture.f.data[0];
399 srcU = s->last_picture.f.data[1];
400 srcV = s->last_picture.f.data[2];
401 luty = v->last_luty [v->ref_field_type[dir]];
402 lutuv= v->last_lutuv[v->ref_field_type[dir]];
403 use_ic=v->last_use_ic;
406 srcY = s->next_picture.f.data[0];
407 srcU = s->next_picture.f.data[1];
408 srcV = s->next_picture.f.data[2];
409 luty = v->next_luty [v->ref_field_type[dir]];
410 lutuv= v->next_lutuv[v->ref_field_type[dir]];
411 use_ic=v->next_use_ic;
417 src_x = s->mb_x * 16 + (mx >> 2);
418 src_y = s->mb_y * 16 + (my >> 2);
419 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
420 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
422 if (v->profile != PROFILE_ADVANCED) {
423 src_x = av_clip( src_x, -16, s->mb_width * 16);
424 src_y = av_clip( src_y, -16, s->mb_height * 16);
425 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
426 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
428 src_x = av_clip( src_x, -17, s->avctx->coded_width);
429 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
430 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
431 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
434 srcY += src_y * s->linesize + src_x;
435 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
436 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
438 if (v->field_mode && v->ref_field_type[dir]) {
439 srcY += s->current_picture_ptr->f.linesize[0];
440 srcU += s->current_picture_ptr->f.linesize[1];
441 srcV += s->current_picture_ptr->f.linesize[2];
444 /* for grayscale we should not try to read from unknown area */
445 if (s->flags & CODEC_FLAG_GRAY) {
446 srcU = s->edge_emu_buffer + 18 * s->linesize;
447 srcV = s->edge_emu_buffer + 18 * s->linesize;
450 if (v->rangeredfrm || use_ic
451 || s->h_edge_pos < 22 || v_edge_pos < 22
452 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
453 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
454 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
456 srcY -= s->mspel * (1 + s->linesize);
457 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
458 17 + s->mspel * 2, 17 + s->mspel * 2,
459 src_x - s->mspel, src_y - s->mspel,
460 s->h_edge_pos, v_edge_pos);
461 srcY = s->edge_emu_buffer;
462 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
463 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
464 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
465 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
468 /* if we deal with range reduction we need to scale source blocks */
469 if (v->rangeredfrm) {
474 for (j = 0; j < 17 + s->mspel * 2; j++) {
475 for (i = 0; i < 17 + s->mspel * 2; i++)
476 src[i] = ((src[i] - 128) >> 1) + 128;
481 for (j = 0; j < 9; j++) {
482 for (i = 0; i < 9; i++) {
483 src[i] = ((src[i] - 128) >> 1) + 128;
484 src2[i] = ((src2[i] - 128) >> 1) + 128;
486 src += s->uvlinesize;
487 src2 += s->uvlinesize;
490 /* if we deal with intensity compensation we need to scale source blocks */
496 for (j = 0; j < 17 + s->mspel * 2; j++) {
497 for (i = 0; i < 17 + s->mspel * 2; i++)
498 src[i] = luty[src[i]];
503 for (j = 0; j < 9; j++) {
504 for (i = 0; i < 9; i++) {
505 src[i] = lutuv[src[i]];
506 src2[i] = lutuv[src2[i]];
508 src += s->uvlinesize;
509 src2 += s->uvlinesize;
512 srcY += s->mspel * (1 + s->linesize);
518 dxy = ((my & 3) << 2) | (mx & 3);
519 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
520 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
521 srcY += s->linesize * 8;
522 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
523 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
524 } else { // hpel mc - always used for luma
525 dxy = (my & 2) | ((mx & 2) >> 1);
527 s->hdsp.put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
529 s->hdsp.put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
532 if (s->flags & CODEC_FLAG_GRAY) return;
533 /* Chroma MC always uses qpel bilinear */
534 uvmx = (uvmx & 3) << 1;
535 uvmy = (uvmy & 3) << 1;
537 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
538 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
540 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
541 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
545 static inline int median4(int a, int b, int c, int d)
548 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
549 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
551 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
552 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
556 /** Do motion compensation for 4-MV macroblock - luminance block
558 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir, int avg)
560 MpegEncContext *s = &v->s;
562 int dxy, mx, my, src_x, src_y;
564 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
565 int v_edge_pos = s->v_edge_pos >> v->field_mode;
569 if ((!v->field_mode ||
570 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
571 !v->s.last_picture.f.data[0])
574 mx = s->mv[dir][n][0];
575 my = s->mv[dir][n][1];
578 if (v->field_mode && (v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
579 srcY = s->current_picture.f.data[0];
580 luty = v->curr_luty[v->ref_field_type[dir]];
581 use_ic=v->curr_use_ic;
583 srcY = s->last_picture.f.data[0];
584 luty = v->last_luty[v->ref_field_type[dir]];
585 use_ic=v->last_use_ic;
588 srcY = s->next_picture.f.data[0];
589 luty = v->next_luty[v->ref_field_type[dir]];
590 use_ic=v->next_use_ic;
597 if (v->cur_field_type != v->ref_field_type[dir])
598 my = my - 2 + 4 * v->cur_field_type;
601 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
602 int same_count = 0, opp_count = 0, k;
603 int chosen_mv[2][4][2], f;
605 for (k = 0; k < 4; k++) {
606 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
607 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
608 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
612 f = opp_count > same_count;
613 switch (f ? opp_count : same_count) {
615 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
616 chosen_mv[f][2][0], chosen_mv[f][3][0]);
617 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
618 chosen_mv[f][2][1], chosen_mv[f][3][1]);
621 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
622 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
625 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
626 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
631 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
632 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
633 for (k = 0; k < 4; k++)
634 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
637 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
639 int width = s->avctx->coded_width;
640 int height = s->avctx->coded_height >> 1;
641 if (s->pict_type == AV_PICTURE_TYPE_P) {
642 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][0] = mx;
643 s->current_picture.motion_val[1][s->block_index[n] + v->blocks_off][1] = my;
645 qx = (s->mb_x * 16) + (mx >> 2);
646 qy = (s->mb_y * 8) + (my >> 3);
651 mx -= 4 * (qx - width);
654 else if (qy > height + 1)
655 my -= 8 * (qy - height - 1);
658 if ((v->fcm == ILACE_FRAME) && fieldmv)
659 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
661 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
663 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
665 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
667 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
669 if (v->profile != PROFILE_ADVANCED) {
670 src_x = av_clip(src_x, -16, s->mb_width * 16);
671 src_y = av_clip(src_y, -16, s->mb_height * 16);
673 src_x = av_clip(src_x, -17, s->avctx->coded_width);
674 if (v->fcm == ILACE_FRAME) {
676 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
678 src_y = av_clip(src_y, -18, s->avctx->coded_height);
680 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
684 srcY += src_y * s->linesize + src_x;
685 if (v->field_mode && v->ref_field_type[dir])
686 srcY += s->current_picture_ptr->f.linesize[0];
688 if (fieldmv && !(src_y & 1))
690 if (fieldmv && (src_y & 1) && src_y < 4)
692 if (v->rangeredfrm || use_ic
693 || s->h_edge_pos < 13 || v_edge_pos < 23
694 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
695 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
696 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
697 /* check emulate edge stride and offset */
698 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
699 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
700 src_x - s->mspel, src_y - (s->mspel << fieldmv),
701 s->h_edge_pos, v_edge_pos);
702 srcY = s->edge_emu_buffer;
703 /* if we deal with range reduction we need to scale source blocks */
704 if (v->rangeredfrm) {
709 for (j = 0; j < 9 + s->mspel * 2; j++) {
710 for (i = 0; i < 9 + s->mspel * 2; i++)
711 src[i] = ((src[i] - 128) >> 1) + 128;
712 src += s->linesize << fieldmv;
715 /* if we deal with intensity compensation we need to scale source blocks */
721 for (j = 0; j < 9 + s->mspel * 2; j++) {
722 for (i = 0; i < 9 + s->mspel * 2; i++)
723 src[i] = luty[src[i]];
724 src += s->linesize << fieldmv;
727 srcY += s->mspel * (1 + (s->linesize << fieldmv));
731 dxy = ((my & 3) << 2) | (mx & 3);
733 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
735 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
736 } else { // hpel mc - always used for luma
737 dxy = (my & 2) | ((mx & 2) >> 1);
739 s->hdsp.put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
741 s->hdsp.put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
745 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
748 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
750 idx = ((a[3] != flag) << 3)
751 | ((a[2] != flag) << 2)
752 | ((a[1] != flag) << 1)
755 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
756 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
758 } else if (count[idx] == 1) {
761 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
762 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
765 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
766 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
769 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
770 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
773 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
774 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
777 } else if (count[idx] == 2) {
779 for (i = 0; i < 3; i++)
784 for (i = t1 + 1; i < 4; i++)
789 *tx = (mvx[t1] + mvx[t2]) / 2;
790 *ty = (mvy[t1] + mvy[t2]) / 2;
798 /** Do motion compensation for 4-MV macroblock - both chroma blocks
800 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
802 MpegEncContext *s = &v->s;
803 H264ChromaContext *h264chroma = &v->h264chroma;
804 uint8_t *srcU, *srcV;
805 int uvmx, uvmy, uvsrc_x, uvsrc_y;
806 int k, tx = 0, ty = 0;
807 int mvx[4], mvy[4], intra[4], mv_f[4];
809 int chroma_ref_type = v->cur_field_type, off = 0;
810 int v_edge_pos = s->v_edge_pos >> v->field_mode;
811 const uint8_t *lutuv;
814 if (!v->field_mode && !v->s.last_picture.f.data[0])
816 if (s->flags & CODEC_FLAG_GRAY)
819 for (k = 0; k < 4; k++) {
820 mvx[k] = s->mv[dir][k][0];
821 mvy[k] = s->mv[dir][k][1];
822 intra[k] = v->mb_type[0][s->block_index[k]];
824 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
827 /* calculate chroma MV vector from four luma MVs */
828 if (!v->field_mode || (v->field_mode && !v->numref)) {
829 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
830 chroma_ref_type = v->reffield;
832 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
833 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
834 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
835 return; //no need to do MC for intra blocks
839 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
841 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
843 chroma_ref_type = !v->cur_field_type;
845 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
847 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
848 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
849 uvmx = (tx + ((tx & 3) == 3)) >> 1;
850 uvmy = (ty + ((ty & 3) == 3)) >> 1;
852 v->luma_mv[s->mb_x][0] = uvmx;
853 v->luma_mv[s->mb_x][1] = uvmy;
856 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
857 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
859 // Field conversion bias
860 if (v->cur_field_type != chroma_ref_type)
861 uvmy += 2 - 4 * chroma_ref_type;
863 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
864 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
866 if (v->profile != PROFILE_ADVANCED) {
867 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
868 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
870 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
871 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
875 if (v->field_mode && (v->cur_field_type != chroma_ref_type) && v->second_field) {
876 srcU = s->current_picture.f.data[1];
877 srcV = s->current_picture.f.data[2];
878 lutuv= v->curr_lutuv[chroma_ref_type];
879 use_ic=v->curr_use_ic;
881 srcU = s->last_picture.f.data[1];
882 srcV = s->last_picture.f.data[2];
883 lutuv= v->last_lutuv[chroma_ref_type];
884 use_ic=v->last_use_ic;
887 srcU = s->next_picture.f.data[1];
888 srcV = s->next_picture.f.data[2];
889 lutuv= v->next_lutuv[chroma_ref_type];
890 use_ic=v->next_use_ic;
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];
907 if (v->rangeredfrm || use_ic
908 || s->h_edge_pos < 18 || v_edge_pos < 18
909 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
910 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
911 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, 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, s->uvlinesize,
915 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
916 s->h_edge_pos >> 1, v_edge_pos >> 1);
917 srcU = s->edge_emu_buffer;
918 srcV = s->edge_emu_buffer + 16;
920 /* if we deal with range reduction we need to scale source blocks */
921 if (v->rangeredfrm) {
927 for (j = 0; j < 9; j++) {
928 for (i = 0; i < 9; i++) {
929 src[i] = ((src[i] - 128) >> 1) + 128;
930 src2[i] = ((src2[i] - 128) >> 1) + 128;
932 src += s->uvlinesize;
933 src2 += s->uvlinesize;
936 /* if we deal with intensity compensation we need to scale source blocks */
943 for (j = 0; j < 9; j++) {
944 for (i = 0; i < 9; i++) {
945 src[i] = lutuv[src[i]];
946 src2[i] = lutuv[src2[i]];
948 src += s->uvlinesize;
949 src2 += s->uvlinesize;
954 /* Chroma MC always uses qpel bilinear */
955 uvmx = (uvmx & 3) << 1;
956 uvmy = (uvmy & 3) << 1;
958 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
959 h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
961 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
962 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
966 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
968 static void vc1_mc_4mv_chroma4(VC1Context *v)
970 MpegEncContext *s = &v->s;
971 H264ChromaContext *h264chroma = &v->h264chroma;
972 uint8_t *srcU, *srcV;
973 int uvsrc_x, uvsrc_y;
974 int uvmx_field[4], uvmy_field[4];
976 int fieldmv = v->blk_mv_type[s->block_index[0]];
977 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
978 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
979 int v_edge_pos = s->v_edge_pos >> 1;
980 int use_ic = v->last_use_ic;
982 if (!v->s.last_picture.f.data[0])
984 if (s->flags & CODEC_FLAG_GRAY)
987 for (i = 0; i < 4; i++) {
989 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
992 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
994 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
997 for (i = 0; i < 4; i++) {
998 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
999 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
1000 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
1001 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
1002 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1003 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1004 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
1005 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
1006 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
1007 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
1009 if (fieldmv && !(uvsrc_y & 1))
1010 v_edge_pos = (s->v_edge_pos >> 1) - 1;
1012 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
1015 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
1016 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
1017 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
1018 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
1019 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1020 s->h_edge_pos >> 1, v_edge_pos);
1021 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
1022 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
1023 s->h_edge_pos >> 1, v_edge_pos);
1024 srcU = s->edge_emu_buffer;
1025 srcV = s->edge_emu_buffer + 16;
1027 /* if we deal with intensity compensation we need to scale source blocks */
1030 uint8_t *src, *src2;
1031 const uint8_t *lutuv = v->last_lutuv[v->ref_field_type[0]];
1035 for (j = 0; j < 5; j++) {
1036 for (i = 0; i < 5; i++) {
1037 src[i] = lutuv[src[i]];
1038 src2[i] = lutuv[src2[i]];
1040 src += s->uvlinesize << 1;
1041 src2 += s->uvlinesize << 1;
1046 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1047 h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1049 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]);
1050 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]);
1055 /***********************************************************************/
1057 * @name VC-1 Block-level functions
1058 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1064 * @brief Get macroblock-level quantizer scale
1066 #define GET_MQUANT() \
1067 if (v->dquantfrm) { \
1069 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1070 if (v->dqbilevel) { \
1071 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1073 mqdiff = get_bits(gb, 3); \
1075 mquant = v->pq + mqdiff; \
1077 mquant = get_bits(gb, 5); \
1080 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1081 edges = 1 << v->dqsbedge; \
1082 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1083 edges = (3 << v->dqsbedge) % 15; \
1084 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1086 if ((edges&1) && !s->mb_x) \
1087 mquant = v->altpq; \
1088 if ((edges&2) && s->first_slice_line) \
1089 mquant = v->altpq; \
1090 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1091 mquant = v->altpq; \
1092 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1093 mquant = v->altpq; \
1094 if (!mquant || mquant > 31) { \
1095 av_log(v->s.avctx, AV_LOG_ERROR, \
1096 "Overriding invalid mquant %d\n", mquant); \
1102 * @def GET_MVDATA(_dmv_x, _dmv_y)
1103 * @brief Get MV differentials
1104 * @see MVDATA decoding from 8.3.5.2, p(1)20
1105 * @param _dmv_x Horizontal differential for decoded MV
1106 * @param _dmv_y Vertical differential for decoded MV
1108 #define GET_MVDATA(_dmv_x, _dmv_y) \
1109 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1110 VC1_MV_DIFF_VLC_BITS, 2); \
1112 mb_has_coeffs = 1; \
1115 mb_has_coeffs = 0; \
1118 _dmv_x = _dmv_y = 0; \
1119 } else if (index == 35) { \
1120 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1121 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1122 } else if (index == 36) { \
1127 index1 = index % 6; \
1128 if (!s->quarter_sample && index1 == 5) val = 1; \
1130 if (size_table[index1] - val > 0) \
1131 val = get_bits(gb, size_table[index1] - val); \
1133 sign = 0 - (val&1); \
1134 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1136 index1 = index / 6; \
1137 if (!s->quarter_sample && index1 == 5) val = 1; \
1139 if (size_table[index1] - val > 0) \
1140 val = get_bits(gb, size_table[index1] - val); \
1142 sign = 0 - (val & 1); \
1143 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1146 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1147 int *dmv_y, int *pred_flag)
1150 int extend_x = 0, extend_y = 0;
1151 GetBitContext *gb = &v->s.gb;
1154 const int* offs_tab;
1157 bits = VC1_2REF_MVDATA_VLC_BITS;
1160 bits = VC1_1REF_MVDATA_VLC_BITS;
1163 switch (v->dmvrange) {
1171 extend_x = extend_y = 1;
1174 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1176 *dmv_x = get_bits(gb, v->k_x);
1177 *dmv_y = get_bits(gb, v->k_y);
1180 *pred_flag = *dmv_y & 1;
1181 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1183 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1188 av_assert0(index < esc);
1190 offs_tab = offset_table2;
1192 offs_tab = offset_table1;
1193 index1 = (index + 1) % 9;
1195 val = get_bits(gb, index1 + extend_x);
1196 sign = 0 -(val & 1);
1197 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1201 offs_tab = offset_table2;
1203 offs_tab = offset_table1;
1204 index1 = (index + 1) / 9;
1205 if (index1 > v->numref) {
1206 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1207 sign = 0 - (val & 1);
1208 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1211 if (v->numref && pred_flag)
1212 *pred_flag = index1 & 1;
1216 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1218 int scaledvalue, refdist;
1219 int scalesame1, scalesame2;
1220 int scalezone1_x, zone1offset_x;
1221 int table_index = dir ^ v->second_field;
1223 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1224 refdist = v->refdist;
1226 refdist = dir ? v->brfd : v->frfd;
1229 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1230 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1231 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1232 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1237 if (FFABS(n) < scalezone1_x)
1238 scaledvalue = (n * scalesame1) >> 8;
1241 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1243 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1246 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1249 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1251 int scaledvalue, refdist;
1252 int scalesame1, scalesame2;
1253 int scalezone1_y, zone1offset_y;
1254 int table_index = dir ^ v->second_field;
1256 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1257 refdist = v->refdist;
1259 refdist = dir ? v->brfd : v->frfd;
1262 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1263 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1264 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1265 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1270 if (FFABS(n) < scalezone1_y)
1271 scaledvalue = (n * scalesame1) >> 8;
1274 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1276 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1280 if (v->cur_field_type && !v->ref_field_type[dir])
1281 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1283 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1286 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1288 int scalezone1_x, zone1offset_x;
1289 int scaleopp1, scaleopp2, brfd;
1292 brfd = FFMIN(v->brfd, 3);
1293 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1294 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1295 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1296 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1301 if (FFABS(n) < scalezone1_x)
1302 scaledvalue = (n * scaleopp1) >> 8;
1305 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1307 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1310 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1313 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1315 int scalezone1_y, zone1offset_y;
1316 int scaleopp1, scaleopp2, brfd;
1319 brfd = FFMIN(v->brfd, 3);
1320 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1321 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][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_y)
1329 scaledvalue = (n * scaleopp1) >> 8;
1332 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1334 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1337 if (v->cur_field_type && !v->ref_field_type[dir]) {
1338 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1340 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1344 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1347 int brfd, scalesame;
1348 int hpel = 1 - v->s.quarter_sample;
1351 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1353 n = scaleforsame_y(v, i, n, dir) << hpel;
1355 n = scaleforsame_x(v, n, dir) << hpel;
1358 brfd = FFMIN(v->brfd, 3);
1359 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1361 n = (n * scalesame >> 8) << hpel;
1365 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1368 int refdist, scaleopp;
1369 int hpel = 1 - v->s.quarter_sample;
1372 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1374 n = scaleforopp_y(v, n, dir) << hpel;
1376 n = scaleforopp_x(v, n) << hpel;
1379 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1380 refdist = FFMIN(v->refdist, 3);
1382 refdist = dir ? v->brfd : v->frfd;
1383 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1385 n = (n * scaleopp >> 8) << hpel;
1389 /** Predict and set motion vector
1391 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1392 int mv1, int r_x, int r_y, uint8_t* is_intra,
1393 int pred_flag, int dir)
1395 MpegEncContext *s = &v->s;
1396 int xy, wrap, off = 0;
1400 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1401 int opposite, a_f, b_f, c_f;
1402 int16_t field_predA[2];
1403 int16_t field_predB[2];
1404 int16_t field_predC[2];
1405 int a_valid, b_valid, c_valid;
1406 int hybridmv_thresh, y_bias = 0;
1408 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1409 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1413 /* scale MV difference to be quad-pel */
1414 dmv_x <<= 1 - s->quarter_sample;
1415 dmv_y <<= 1 - s->quarter_sample;
1417 wrap = s->b8_stride;
1418 xy = s->block_index[n];
1421 s->mv[0][n][0] = s->current_picture.motion_val[0][xy + v->blocks_off][0] = 0;
1422 s->mv[0][n][1] = s->current_picture.motion_val[0][xy + v->blocks_off][1] = 0;
1423 s->current_picture.motion_val[1][xy + v->blocks_off][0] = 0;
1424 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
1425 if (mv1) { /* duplicate motion data for 1-MV block */
1426 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1427 s->current_picture.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1428 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1429 s->current_picture.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1430 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1431 s->current_picture.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1432 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1433 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1434 s->current_picture.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1435 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1436 s->current_picture.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1437 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1438 s->current_picture.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1443 C = s->current_picture.motion_val[dir][xy - 1 + v->blocks_off];
1444 A = s->current_picture.motion_val[dir][xy - wrap + v->blocks_off];
1446 if (v->field_mode && mixedmv_pic)
1447 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1449 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1451 //in 4-MV mode different blocks have different B predictor position
1454 off = (s->mb_x > 0) ? -1 : 1;
1457 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1466 B = s->current_picture.motion_val[dir][xy - wrap + off + v->blocks_off];
1468 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1469 b_valid = a_valid && (s->mb_width > 1);
1470 c_valid = s->mb_x || (n == 1 || n == 3);
1471 if (v->field_mode) {
1472 a_valid = a_valid && !is_intra[xy - wrap];
1473 b_valid = b_valid && !is_intra[xy - wrap + off];
1474 c_valid = c_valid && !is_intra[xy - 1];
1478 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1479 num_oppfield += a_f;
1480 num_samefield += 1 - a_f;
1481 field_predA[0] = A[0];
1482 field_predA[1] = A[1];
1484 field_predA[0] = field_predA[1] = 0;
1488 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1489 num_oppfield += b_f;
1490 num_samefield += 1 - b_f;
1491 field_predB[0] = B[0];
1492 field_predB[1] = B[1];
1494 field_predB[0] = field_predB[1] = 0;
1498 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1499 num_oppfield += c_f;
1500 num_samefield += 1 - c_f;
1501 field_predC[0] = C[0];
1502 field_predC[1] = C[1];
1504 field_predC[0] = field_predC[1] = 0;
1508 if (v->field_mode) {
1510 // REFFIELD determines if the last field or the second-last field is
1511 // to be used as reference
1512 opposite = 1 - v->reffield;
1514 if (num_samefield <= num_oppfield)
1515 opposite = 1 - pred_flag;
1517 opposite = pred_flag;
1522 if (a_valid && !a_f) {
1523 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1524 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1526 if (b_valid && !b_f) {
1527 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1528 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1530 if (c_valid && !c_f) {
1531 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1532 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1534 v->mv_f[dir][xy + v->blocks_off] = 1;
1535 v->ref_field_type[dir] = !v->cur_field_type;
1537 if (a_valid && a_f) {
1538 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1539 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1541 if (b_valid && b_f) {
1542 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1543 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1545 if (c_valid && c_f) {
1546 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1547 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1549 v->mv_f[dir][xy + v->blocks_off] = 0;
1550 v->ref_field_type[dir] = v->cur_field_type;
1554 px = field_predA[0];
1555 py = field_predA[1];
1556 } else if (c_valid) {
1557 px = field_predC[0];
1558 py = field_predC[1];
1559 } else if (b_valid) {
1560 px = field_predB[0];
1561 py = field_predB[1];
1567 if (num_samefield + num_oppfield > 1) {
1568 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1569 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1572 /* Pullback MV as specified in 8.3.5.3.4 */
1573 if (!v->field_mode) {
1575 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1576 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1577 X = (s->mb_width << 6) - 4;
1578 Y = (s->mb_height << 6) - 4;
1580 if (qx + px < -60) px = -60 - qx;
1581 if (qy + py < -60) py = -60 - qy;
1583 if (qx + px < -28) px = -28 - qx;
1584 if (qy + py < -28) py = -28 - qy;
1586 if (qx + px > X) px = X - qx;
1587 if (qy + py > Y) py = Y - qy;
1590 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1591 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1592 hybridmv_thresh = 32;
1593 if (a_valid && c_valid) {
1594 if (is_intra[xy - wrap])
1595 sum = FFABS(px) + FFABS(py);
1597 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1598 if (sum > hybridmv_thresh) {
1599 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1600 px = field_predA[0];
1601 py = field_predA[1];
1603 px = field_predC[0];
1604 py = field_predC[1];
1607 if (is_intra[xy - 1])
1608 sum = FFABS(px) + FFABS(py);
1610 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1611 if (sum > hybridmv_thresh) {
1612 if (get_bits1(&s->gb)) {
1613 px = field_predA[0];
1614 py = field_predA[1];
1616 px = field_predC[0];
1617 py = field_predC[1];
1624 if (v->field_mode && v->numref)
1626 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1628 /* store MV using signed modulus of MV range defined in 4.11 */
1629 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;
1630 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;
1631 if (mv1) { /* duplicate motion data for 1-MV block */
1632 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1633 s->current_picture.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1634 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1635 s->current_picture.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1636 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.motion_val[dir][xy + v->blocks_off][0];
1637 s->current_picture.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.motion_val[dir][xy + v->blocks_off][1];
1638 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1639 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];
1643 /** Predict and set motion vector for interlaced frame picture MBs
1645 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1646 int mvn, int r_x, int r_y, uint8_t* is_intra, int dir)
1648 MpegEncContext *s = &v->s;
1649 int xy, wrap, off = 0;
1650 int A[2], B[2], C[2];
1652 int a_valid = 0, b_valid = 0, c_valid = 0;
1653 int field_a, field_b, field_c; // 0: same, 1: opposit
1654 int total_valid, num_samefield, num_oppfield;
1655 int pos_c, pos_b, n_adj;
1657 wrap = s->b8_stride;
1658 xy = s->block_index[n];
1661 s->mv[0][n][0] = s->current_picture.motion_val[0][xy][0] = 0;
1662 s->mv[0][n][1] = s->current_picture.motion_val[0][xy][1] = 0;
1663 s->current_picture.motion_val[1][xy][0] = 0;
1664 s->current_picture.motion_val[1][xy][1] = 0;
1665 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1666 s->current_picture.motion_val[0][xy + 1][0] = 0;
1667 s->current_picture.motion_val[0][xy + 1][1] = 0;
1668 s->current_picture.motion_val[0][xy + wrap][0] = 0;
1669 s->current_picture.motion_val[0][xy + wrap][1] = 0;
1670 s->current_picture.motion_val[0][xy + wrap + 1][0] = 0;
1671 s->current_picture.motion_val[0][xy + wrap + 1][1] = 0;
1672 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1673 s->current_picture.motion_val[1][xy + 1][0] = 0;
1674 s->current_picture.motion_val[1][xy + 1][1] = 0;
1675 s->current_picture.motion_val[1][xy + wrap][0] = 0;
1676 s->current_picture.motion_val[1][xy + wrap][1] = 0;
1677 s->current_picture.motion_val[1][xy + wrap + 1][0] = 0;
1678 s->current_picture.motion_val[1][xy + wrap + 1][1] = 0;
1683 off = ((n == 0) || (n == 1)) ? 1 : -1;
1685 if (s->mb_x || (n == 1) || (n == 3)) {
1686 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1687 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1688 A[0] = s->current_picture.motion_val[dir][xy - 1][0];
1689 A[1] = s->current_picture.motion_val[dir][xy - 1][1];
1691 } else { // current block has frame mv and cand. has field MV (so average)
1692 A[0] = (s->current_picture.motion_val[dir][xy - 1][0]
1693 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][0] + 1) >> 1;
1694 A[1] = (s->current_picture.motion_val[dir][xy - 1][1]
1695 + s->current_picture.motion_val[dir][xy - 1 + off * wrap][1] + 1) >> 1;
1698 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1704 /* Predict B and C */
1705 B[0] = B[1] = C[0] = C[1] = 0;
1706 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1707 if (!s->first_slice_line) {
1708 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1711 pos_b = s->block_index[n_adj] - 2 * wrap;
1712 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1713 n_adj = (n & 2) | (n & 1);
1715 B[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][0];
1716 B[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap][1];
1717 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1718 B[0] = (B[0] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1719 B[1] = (B[1] + s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1722 if (s->mb_width > 1) {
1723 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1726 pos_c = s->block_index[2] - 2 * wrap + 2;
1727 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1730 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][0];
1731 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap + 2][1];
1732 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1733 C[0] = (1 + C[0] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1734 C[1] = (1 + C[1] + (s->current_picture.motion_val[dir][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1736 if (s->mb_x == s->mb_width - 1) {
1737 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1740 pos_c = s->block_index[3] - 2 * wrap - 2;
1741 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1744 C[0] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][0];
1745 C[1] = s->current_picture.motion_val[dir][s->block_index[n_adj] - 2 * wrap - 2][1];
1746 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1747 C[0] = (1 + C[0] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1748 C[1] = (1 + C[1] + s->current_picture.motion_val[dir][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1757 pos_b = s->block_index[1];
1759 B[0] = s->current_picture.motion_val[dir][pos_b][0];
1760 B[1] = s->current_picture.motion_val[dir][pos_b][1];
1761 pos_c = s->block_index[0];
1763 C[0] = s->current_picture.motion_val[dir][pos_c][0];
1764 C[1] = s->current_picture.motion_val[dir][pos_c][1];
1767 total_valid = a_valid + b_valid + c_valid;
1768 // check if predictor A is out of bounds
1769 if (!s->mb_x && !(n == 1 || n == 3)) {
1772 // check if predictor B is out of bounds
1773 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1774 B[0] = B[1] = C[0] = C[1] = 0;
1776 if (!v->blk_mv_type[xy]) {
1777 if (s->mb_width == 1) {
1781 if (total_valid >= 2) {
1782 px = mid_pred(A[0], B[0], C[0]);
1783 py = mid_pred(A[1], B[1], C[1]);
1784 } else if (total_valid) {
1785 if (a_valid) { px = A[0]; py = A[1]; }
1786 else if (b_valid) { px = B[0]; py = B[1]; }
1787 else if (c_valid) { px = C[0]; py = C[1]; }
1794 field_a = (A[1] & 4) ? 1 : 0;
1798 field_b = (B[1] & 4) ? 1 : 0;
1802 field_c = (C[1] & 4) ? 1 : 0;
1806 num_oppfield = field_a + field_b + field_c;
1807 num_samefield = total_valid - num_oppfield;
1808 if (total_valid == 3) {
1809 if ((num_samefield == 3) || (num_oppfield == 3)) {
1810 px = mid_pred(A[0], B[0], C[0]);
1811 py = mid_pred(A[1], B[1], C[1]);
1812 } else if (num_samefield >= num_oppfield) {
1813 /* take one MV from same field set depending on priority
1814 the check for B may not be necessary */
1815 px = !field_a ? A[0] : B[0];
1816 py = !field_a ? A[1] : B[1];
1818 px = field_a ? A[0] : B[0];
1819 py = field_a ? A[1] : B[1];
1821 } else if (total_valid == 2) {
1822 if (num_samefield >= num_oppfield) {
1823 if (!field_a && a_valid) {
1826 } else if (!field_b && b_valid) {
1829 } else if (c_valid) {
1834 if (field_a && a_valid) {
1837 } else if (field_b && b_valid) {
1840 } else if (c_valid) {
1845 } else if (total_valid == 1) {
1846 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1847 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1852 /* store MV using signed modulus of MV range defined in 4.11 */
1853 s->mv[dir][n][0] = s->current_picture.motion_val[dir][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1854 s->mv[dir][n][1] = s->current_picture.motion_val[dir][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1855 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1856 s->current_picture.motion_val[dir][xy + 1 ][0] = s->current_picture.motion_val[dir][xy][0];
1857 s->current_picture.motion_val[dir][xy + 1 ][1] = s->current_picture.motion_val[dir][xy][1];
1858 s->current_picture.motion_val[dir][xy + wrap ][0] = s->current_picture.motion_val[dir][xy][0];
1859 s->current_picture.motion_val[dir][xy + wrap ][1] = s->current_picture.motion_val[dir][xy][1];
1860 s->current_picture.motion_val[dir][xy + wrap + 1][0] = s->current_picture.motion_val[dir][xy][0];
1861 s->current_picture.motion_val[dir][xy + wrap + 1][1] = s->current_picture.motion_val[dir][xy][1];
1862 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1863 s->current_picture.motion_val[dir][xy + 1][0] = s->current_picture.motion_val[dir][xy][0];
1864 s->current_picture.motion_val[dir][xy + 1][1] = s->current_picture.motion_val[dir][xy][1];
1865 s->mv[dir][n + 1][0] = s->mv[dir][n][0];
1866 s->mv[dir][n + 1][1] = s->mv[dir][n][1];
1870 /** Motion compensation for direct or interpolated blocks in B-frames
1872 static void vc1_interp_mc(VC1Context *v)
1874 MpegEncContext *s = &v->s;
1875 H264ChromaContext *h264chroma = &v->h264chroma;
1876 uint8_t *srcY, *srcU, *srcV;
1877 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1879 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1880 int use_ic = v->next_use_ic;
1882 if (!v->field_mode && !v->s.next_picture.f.data[0])
1885 mx = s->mv[1][0][0];
1886 my = s->mv[1][0][1];
1887 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1888 uvmy = (my + ((my & 3) == 3)) >> 1;
1889 if (v->field_mode) {
1890 if (v->cur_field_type != v->ref_field_type[1])
1891 my = my - 2 + 4 * v->cur_field_type;
1892 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1895 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1896 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1898 srcY = s->next_picture.f.data[0];
1899 srcU = s->next_picture.f.data[1];
1900 srcV = s->next_picture.f.data[2];
1902 src_x = s->mb_x * 16 + (mx >> 2);
1903 src_y = s->mb_y * 16 + (my >> 2);
1904 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1905 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1907 if (v->profile != PROFILE_ADVANCED) {
1908 src_x = av_clip( src_x, -16, s->mb_width * 16);
1909 src_y = av_clip( src_y, -16, s->mb_height * 16);
1910 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1911 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1913 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1914 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1915 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1916 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1919 srcY += src_y * s->linesize + src_x;
1920 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1921 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1923 if (v->field_mode && v->ref_field_type[1]) {
1924 srcY += s->current_picture_ptr->f.linesize[0];
1925 srcU += s->current_picture_ptr->f.linesize[1];
1926 srcV += s->current_picture_ptr->f.linesize[2];
1929 /* for grayscale we should not try to read from unknown area */
1930 if (s->flags & CODEC_FLAG_GRAY) {
1931 srcU = s->edge_emu_buffer + 18 * s->linesize;
1932 srcV = s->edge_emu_buffer + 18 * s->linesize;
1935 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22 || use_ic
1936 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1937 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1938 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1940 srcY -= s->mspel * (1 + s->linesize);
1941 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1942 17 + s->mspel * 2, 17 + s->mspel * 2,
1943 src_x - s->mspel, src_y - s->mspel,
1944 s->h_edge_pos, v_edge_pos);
1945 srcY = s->edge_emu_buffer;
1946 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1947 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1948 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1949 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1952 /* if we deal with range reduction we need to scale source blocks */
1953 if (v->rangeredfrm) {
1955 uint8_t *src, *src2;
1958 for (j = 0; j < 17 + s->mspel * 2; j++) {
1959 for (i = 0; i < 17 + s->mspel * 2; i++)
1960 src[i] = ((src[i] - 128) >> 1) + 128;
1965 for (j = 0; j < 9; j++) {
1966 for (i = 0; i < 9; i++) {
1967 src[i] = ((src[i] - 128) >> 1) + 128;
1968 src2[i] = ((src2[i] - 128) >> 1) + 128;
1970 src += s->uvlinesize;
1971 src2 += s->uvlinesize;
1976 const uint8_t *luty = v->next_luty [v->ref_field_type[1]];
1977 const uint8_t *lutuv= v->next_lutuv[v->ref_field_type[1]];
1979 uint8_t *src, *src2;
1982 for (j = 0; j < 17 + s->mspel * 2; j++) {
1983 for (i = 0; i < 17 + s->mspel * 2; i++)
1984 src[i] = luty[src[i]];
1989 for (j = 0; j < 9; j++) {
1990 for (i = 0; i < 9; i++) {
1991 src[i] = lutuv[src[i]];
1992 src2[i] = lutuv[src2[i]];
1994 src += s->uvlinesize;
1995 src2 += s->uvlinesize;
1998 srcY += s->mspel * (1 + s->linesize);
2005 dxy = ((my & 3) << 2) | (mx & 3);
2006 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
2007 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
2008 srcY += s->linesize * 8;
2009 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
2010 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
2012 dxy = (my & 2) | ((mx & 2) >> 1);
2015 s->hdsp.avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
2017 s->hdsp.avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
2020 if (s->flags & CODEC_FLAG_GRAY) return;
2021 /* Chroma MC always uses qpel blilinear */
2022 uvmx = (uvmx & 3) << 1;
2023 uvmy = (uvmy & 3) << 1;
2025 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2026 h264chroma->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2028 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
2029 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
2033 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
2037 #if B_FRACTION_DEN==256
2041 return 2 * ((value * n + 255) >> 9);
2042 return (value * n + 128) >> 8;
2045 n -= B_FRACTION_DEN;
2047 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2048 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2052 /** Reconstruct motion vector for B-frame and do motion compensation
2054 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2055 int direct, int mode)
2062 if (mode == BMV_TYPE_INTERPOLATED) {
2068 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2071 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2072 int direct, int mvtype)
2074 MpegEncContext *s = &v->s;
2075 int xy, wrap, off = 0;
2080 const uint8_t *is_intra = v->mb_type[0];
2084 /* scale MV difference to be quad-pel */
2085 dmv_x[0] <<= 1 - s->quarter_sample;
2086 dmv_y[0] <<= 1 - s->quarter_sample;
2087 dmv_x[1] <<= 1 - s->quarter_sample;
2088 dmv_y[1] <<= 1 - s->quarter_sample;
2090 wrap = s->b8_stride;
2091 xy = s->block_index[0];
2094 s->current_picture.motion_val[0][xy + v->blocks_off][0] =
2095 s->current_picture.motion_val[0][xy + v->blocks_off][1] =
2096 s->current_picture.motion_val[1][xy + v->blocks_off][0] =
2097 s->current_picture.motion_val[1][xy + v->blocks_off][1] = 0;
2100 if (!v->field_mode) {
2101 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2102 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2103 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2104 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2106 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2107 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));
2108 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));
2109 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));
2110 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));
2113 s->current_picture.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2114 s->current_picture.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2115 s->current_picture.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2116 s->current_picture.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2120 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2121 C = s->current_picture.motion_val[0][xy - 2];
2122 A = s->current_picture.motion_val[0][xy - wrap * 2];
2123 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2124 B = s->current_picture.motion_val[0][xy - wrap * 2 + off];
2126 if (!s->mb_x) C[0] = C[1] = 0;
2127 if (!s->first_slice_line) { // predictor A is not out of bounds
2128 if (s->mb_width == 1) {
2132 px = mid_pred(A[0], B[0], C[0]);
2133 py = mid_pred(A[1], B[1], C[1]);
2135 } else if (s->mb_x) { // predictor C is not out of bounds
2141 /* Pullback MV as specified in 8.3.5.3.4 */
2144 if (v->profile < PROFILE_ADVANCED) {
2145 qx = (s->mb_x << 5);
2146 qy = (s->mb_y << 5);
2147 X = (s->mb_width << 5) - 4;
2148 Y = (s->mb_height << 5) - 4;
2149 if (qx + px < -28) px = -28 - qx;
2150 if (qy + py < -28) py = -28 - qy;
2151 if (qx + px > X) px = X - qx;
2152 if (qy + py > Y) py = Y - qy;
2154 qx = (s->mb_x << 6);
2155 qy = (s->mb_y << 6);
2156 X = (s->mb_width << 6) - 4;
2157 Y = (s->mb_height << 6) - 4;
2158 if (qx + px < -60) px = -60 - qx;
2159 if (qy + py < -60) py = -60 - qy;
2160 if (qx + px > X) px = X - qx;
2161 if (qy + py > Y) py = Y - qy;
2164 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2165 if (0 && !s->first_slice_line && s->mb_x) {
2166 if (is_intra[xy - wrap])
2167 sum = FFABS(px) + FFABS(py);
2169 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2171 if (get_bits1(&s->gb)) {
2179 if (is_intra[xy - 2])
2180 sum = FFABS(px) + FFABS(py);
2182 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2184 if (get_bits1(&s->gb)) {
2194 /* store MV using signed modulus of MV range defined in 4.11 */
2195 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2196 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2198 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2199 C = s->current_picture.motion_val[1][xy - 2];
2200 A = s->current_picture.motion_val[1][xy - wrap * 2];
2201 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2202 B = s->current_picture.motion_val[1][xy - wrap * 2 + off];
2206 if (!s->first_slice_line) { // predictor A is not out of bounds
2207 if (s->mb_width == 1) {
2211 px = mid_pred(A[0], B[0], C[0]);
2212 py = mid_pred(A[1], B[1], C[1]);
2214 } else if (s->mb_x) { // predictor C is not out of bounds
2220 /* Pullback MV as specified in 8.3.5.3.4 */
2223 if (v->profile < PROFILE_ADVANCED) {
2224 qx = (s->mb_x << 5);
2225 qy = (s->mb_y << 5);
2226 X = (s->mb_width << 5) - 4;
2227 Y = (s->mb_height << 5) - 4;
2228 if (qx + px < -28) px = -28 - qx;
2229 if (qy + py < -28) py = -28 - qy;
2230 if (qx + px > X) px = X - qx;
2231 if (qy + py > Y) py = Y - qy;
2233 qx = (s->mb_x << 6);
2234 qy = (s->mb_y << 6);
2235 X = (s->mb_width << 6) - 4;
2236 Y = (s->mb_height << 6) - 4;
2237 if (qx + px < -60) px = -60 - qx;
2238 if (qy + py < -60) py = -60 - qy;
2239 if (qx + px > X) px = X - qx;
2240 if (qy + py > Y) py = Y - qy;
2243 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2244 if (0 && !s->first_slice_line && s->mb_x) {
2245 if (is_intra[xy - wrap])
2246 sum = FFABS(px) + FFABS(py);
2248 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2250 if (get_bits1(&s->gb)) {
2258 if (is_intra[xy - 2])
2259 sum = FFABS(px) + FFABS(py);
2261 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2263 if (get_bits1(&s->gb)) {
2273 /* store MV using signed modulus of MV range defined in 4.11 */
2275 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2276 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2278 s->current_picture.motion_val[0][xy][0] = s->mv[0][0][0];
2279 s->current_picture.motion_val[0][xy][1] = s->mv[0][0][1];
2280 s->current_picture.motion_val[1][xy][0] = s->mv[1][0][0];
2281 s->current_picture.motion_val[1][xy][1] = s->mv[1][0][1];
2284 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2286 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2287 MpegEncContext *s = &v->s;
2288 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2290 if (v->bmvtype == BMV_TYPE_DIRECT) {
2291 int total_opp, k, f;
2292 if (s->next_picture.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2293 s->mv[0][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2294 v->bfraction, 0, s->quarter_sample);
2295 s->mv[0][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2296 v->bfraction, 0, s->quarter_sample);
2297 s->mv[1][0][0] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][0],
2298 v->bfraction, 1, s->quarter_sample);
2299 s->mv[1][0][1] = scale_mv(s->next_picture.motion_val[1][s->block_index[0] + v->blocks_off][1],
2300 v->bfraction, 1, s->quarter_sample);
2302 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2303 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2304 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2305 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2306 f = (total_opp > 2) ? 1 : 0;
2308 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2309 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2312 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2313 for (k = 0; k < 4; k++) {
2314 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2315 s->current_picture.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2316 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2317 s->current_picture.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2318 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2319 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2323 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2324 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);
2325 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);
2328 if (dir) { // backward
2329 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);
2330 if (n == 3 || mv1) {
2331 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2334 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);
2335 if (n == 3 || mv1) {
2336 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2341 /** Get predicted DC value for I-frames only
2342 * prediction dir: left=0, top=1
2343 * @param s MpegEncContext
2344 * @param overlap flag indicating that overlap filtering is used
2345 * @param pq integer part of picture quantizer
2346 * @param[in] n block index in the current MB
2347 * @param dc_val_ptr Pointer to DC predictor
2348 * @param dir_ptr Prediction direction for use in AC prediction
2350 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2351 int16_t **dc_val_ptr, int *dir_ptr)
2353 int a, b, c, wrap, pred, scale;
2355 static const uint16_t dcpred[32] = {
2356 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2357 114, 102, 93, 85, 79, 73, 68, 64,
2358 60, 57, 54, 51, 49, 47, 45, 43,
2359 41, 39, 38, 37, 35, 34, 33
2362 /* find prediction - wmv3_dc_scale always used here in fact */
2363 if (n < 4) scale = s->y_dc_scale;
2364 else scale = s->c_dc_scale;
2366 wrap = s->block_wrap[n];
2367 dc_val = s->dc_val[0] + s->block_index[n];
2373 b = dc_val[ - 1 - wrap];
2374 a = dc_val[ - wrap];
2376 if (pq < 9 || !overlap) {
2377 /* Set outer values */
2378 if (s->first_slice_line && (n != 2 && n != 3))
2379 b = a = dcpred[scale];
2380 if (s->mb_x == 0 && (n != 1 && n != 3))
2381 b = c = dcpred[scale];
2383 /* Set outer values */
2384 if (s->first_slice_line && (n != 2 && n != 3))
2386 if (s->mb_x == 0 && (n != 1 && n != 3))
2390 if (abs(a - b) <= abs(b - c)) {
2392 *dir_ptr = 1; // left
2395 *dir_ptr = 0; // top
2398 /* update predictor */
2399 *dc_val_ptr = &dc_val[0];
2404 /** Get predicted DC value
2405 * prediction dir: left=0, top=1
2406 * @param s MpegEncContext
2407 * @param overlap flag indicating that overlap filtering is used
2408 * @param pq integer part of picture quantizer
2409 * @param[in] n block index in the current MB
2410 * @param a_avail flag indicating top block availability
2411 * @param c_avail flag indicating left block availability
2412 * @param dc_val_ptr Pointer to DC predictor
2413 * @param dir_ptr Prediction direction for use in AC prediction
2415 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2416 int a_avail, int c_avail,
2417 int16_t **dc_val_ptr, int *dir_ptr)
2419 int a, b, c, wrap, pred;
2421 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2425 wrap = s->block_wrap[n];
2426 dc_val = s->dc_val[0] + s->block_index[n];
2432 b = dc_val[ - 1 - wrap];
2433 a = dc_val[ - wrap];
2434 /* scale predictors if needed */
2435 q1 = s->current_picture.qscale_table[mb_pos];
2436 dqscale_index = s->y_dc_scale_table[q1] - 1;
2437 if (dqscale_index < 0)
2439 if (c_avail && (n != 1 && n != 3)) {
2440 q2 = s->current_picture.qscale_table[mb_pos - 1];
2442 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2444 if (a_avail && (n != 2 && n != 3)) {
2445 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2447 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2449 if (a_avail && c_avail && (n != 3)) {
2454 off -= s->mb_stride;
2455 q2 = s->current_picture.qscale_table[off];
2457 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2460 if (a_avail && c_avail) {
2461 if (abs(a - b) <= abs(b - c)) {
2463 *dir_ptr = 1; // left
2466 *dir_ptr = 0; // top
2468 } else if (a_avail) {
2470 *dir_ptr = 0; // top
2471 } else if (c_avail) {
2473 *dir_ptr = 1; // left
2476 *dir_ptr = 1; // left
2479 /* update predictor */
2480 *dc_val_ptr = &dc_val[0];
2484 /** @} */ // Block group
2487 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2488 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2492 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2493 uint8_t **coded_block_ptr)
2495 int xy, wrap, pred, a, b, c;
2497 xy = s->block_index[n];
2498 wrap = s->b8_stride;
2503 a = s->coded_block[xy - 1 ];
2504 b = s->coded_block[xy - 1 - wrap];
2505 c = s->coded_block[xy - wrap];
2514 *coded_block_ptr = &s->coded_block[xy];
2520 * Decode one AC coefficient
2521 * @param v The VC1 context
2522 * @param last Last coefficient
2523 * @param skip How much zero coefficients to skip
2524 * @param value Decoded AC coefficient value
2525 * @param codingset set of VLC to decode data
2528 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2529 int *value, int codingset)
2531 GetBitContext *gb = &v->s.gb;
2532 int index, escape, run = 0, level = 0, lst = 0;
2534 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2535 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2536 run = vc1_index_decode_table[codingset][index][0];
2537 level = vc1_index_decode_table[codingset][index][1];
2538 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2542 escape = decode210(gb);
2544 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2545 run = vc1_index_decode_table[codingset][index][0];
2546 level = vc1_index_decode_table[codingset][index][1];
2547 lst = index >= vc1_last_decode_table[codingset];
2550 level += vc1_last_delta_level_table[codingset][run];
2552 level += vc1_delta_level_table[codingset][run];
2555 run += vc1_last_delta_run_table[codingset][level] + 1;
2557 run += vc1_delta_run_table[codingset][level] + 1;
2563 lst = get_bits1(gb);
2564 if (v->s.esc3_level_length == 0) {
2565 if (v->pq < 8 || v->dquantfrm) { // table 59
2566 v->s.esc3_level_length = get_bits(gb, 3);
2567 if (!v->s.esc3_level_length)
2568 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2569 } else { // table 60
2570 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2572 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2574 run = get_bits(gb, v->s.esc3_run_length);
2575 sign = get_bits1(gb);
2576 level = get_bits(gb, v->s.esc3_level_length);
2587 /** Decode intra block in intra frames - should be faster than decode_intra_block
2588 * @param v VC1Context
2589 * @param block block to decode
2590 * @param[in] n subblock index
2591 * @param coded are AC coeffs present or not
2592 * @param codingset set of VLC to decode data
2594 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2595 int coded, int codingset)
2597 GetBitContext *gb = &v->s.gb;
2598 MpegEncContext *s = &v->s;
2599 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2602 int16_t *ac_val, *ac_val2;
2605 /* Get DC differential */
2607 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2609 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2612 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2616 if (dcdiff == 119 /* ESC index value */) {
2617 /* TODO: Optimize */
2618 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2619 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2620 else dcdiff = get_bits(gb, 8);
2623 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2624 else if (v->pq == 2)
2625 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2632 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2635 /* Store the quantized DC coeff, used for prediction */
2637 block[0] = dcdiff * s->y_dc_scale;
2639 block[0] = dcdiff * s->c_dc_scale;
2650 int last = 0, skip, value;
2651 const uint8_t *zz_table;
2655 scale = v->pq * 2 + v->halfpq;
2659 zz_table = v->zz_8x8[2];
2661 zz_table = v->zz_8x8[3];
2663 zz_table = v->zz_8x8[1];
2665 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2667 if (dc_pred_dir) // left
2670 ac_val -= 16 * s->block_wrap[n];
2673 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2677 block[zz_table[i++]] = value;
2680 /* apply AC prediction if needed */
2682 if (dc_pred_dir) { // left
2683 for (k = 1; k < 8; k++)
2684 block[k << v->left_blk_sh] += ac_val[k];
2686 for (k = 1; k < 8; k++)
2687 block[k << v->top_blk_sh] += ac_val[k + 8];
2690 /* save AC coeffs for further prediction */
2691 for (k = 1; k < 8; k++) {
2692 ac_val2[k] = block[k << v->left_blk_sh];
2693 ac_val2[k + 8] = block[k << v->top_blk_sh];
2696 /* scale AC coeffs */
2697 for (k = 1; k < 64; k++)
2701 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2704 if (s->ac_pred) i = 63;
2710 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2714 scale = v->pq * 2 + v->halfpq;
2715 memset(ac_val2, 0, 16 * 2);
2716 if (dc_pred_dir) { // left
2719 memcpy(ac_val2, ac_val, 8 * 2);
2721 ac_val -= 16 * s->block_wrap[n];
2723 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2726 /* apply AC prediction if needed */
2728 if (dc_pred_dir) { //left
2729 for (k = 1; k < 8; k++) {
2730 block[k << v->left_blk_sh] = ac_val[k] * scale;
2731 if (!v->pquantizer && block[k << v->left_blk_sh])
2732 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2735 for (k = 1; k < 8; k++) {
2736 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2737 if (!v->pquantizer && block[k << v->top_blk_sh])
2738 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2744 s->block_last_index[n] = i;
2749 /** Decode intra block in intra frames - should be faster than decode_intra_block
2750 * @param v VC1Context
2751 * @param block block to decode
2752 * @param[in] n subblock number
2753 * @param coded are AC coeffs present or not
2754 * @param codingset set of VLC to decode data
2755 * @param mquant quantizer value for this macroblock
2757 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2758 int coded, int codingset, int mquant)
2760 GetBitContext *gb = &v->s.gb;
2761 MpegEncContext *s = &v->s;
2762 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2764 int16_t *dc_val = NULL;
2765 int16_t *ac_val, *ac_val2;
2767 int a_avail = v->a_avail, c_avail = v->c_avail;
2768 int use_pred = s->ac_pred;
2771 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2773 /* Get DC differential */
2775 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2777 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2780 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2784 if (dcdiff == 119 /* ESC index value */) {
2785 /* TODO: Optimize */
2786 if (mquant == 1) dcdiff = get_bits(gb, 10);
2787 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2788 else dcdiff = get_bits(gb, 8);
2791 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2792 else if (mquant == 2)
2793 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2800 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2803 /* Store the quantized DC coeff, used for prediction */
2805 block[0] = dcdiff * s->y_dc_scale;
2807 block[0] = dcdiff * s->c_dc_scale;
2813 /* check if AC is needed at all */
2814 if (!a_avail && !c_avail)
2816 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2819 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2821 if (dc_pred_dir) // left
2824 ac_val -= 16 * s->block_wrap[n];
2826 q1 = s->current_picture.qscale_table[mb_pos];
2827 if ( dc_pred_dir && c_avail && mb_pos)
2828 q2 = s->current_picture.qscale_table[mb_pos - 1];
2829 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2830 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
2831 if ( dc_pred_dir && n == 1)
2833 if (!dc_pred_dir && n == 2)
2839 int last = 0, skip, value;
2840 const uint8_t *zz_table;
2844 if (!use_pred && v->fcm == ILACE_FRAME) {
2845 zz_table = v->zzi_8x8;
2847 if (!dc_pred_dir) // top
2848 zz_table = v->zz_8x8[2];
2850 zz_table = v->zz_8x8[3];
2853 if (v->fcm != ILACE_FRAME)
2854 zz_table = v->zz_8x8[1];
2856 zz_table = v->zzi_8x8;
2860 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2864 block[zz_table[i++]] = value;
2867 /* apply AC prediction if needed */
2869 /* scale predictors if needed*/
2870 if (q2 && q1 != q2) {
2871 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2872 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2875 return AVERROR_INVALIDDATA;
2876 if (dc_pred_dir) { // left
2877 for (k = 1; k < 8; k++)
2878 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2880 for (k = 1; k < 8; k++)
2881 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2884 if (dc_pred_dir) { //left
2885 for (k = 1; k < 8; k++)
2886 block[k << v->left_blk_sh] += ac_val[k];
2888 for (k = 1; k < 8; k++)
2889 block[k << v->top_blk_sh] += ac_val[k + 8];
2893 /* save AC coeffs for further prediction */
2894 for (k = 1; k < 8; k++) {
2895 ac_val2[k ] = block[k << v->left_blk_sh];
2896 ac_val2[k + 8] = block[k << v->top_blk_sh];
2899 /* scale AC coeffs */
2900 for (k = 1; k < 64; k++)
2904 block[k] += (block[k] < 0) ? -mquant : mquant;
2907 if (use_pred) i = 63;
2908 } else { // no AC coeffs
2911 memset(ac_val2, 0, 16 * 2);
2912 if (dc_pred_dir) { // left
2914 memcpy(ac_val2, ac_val, 8 * 2);
2915 if (q2 && q1 != q2) {
2916 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2917 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2919 return AVERROR_INVALIDDATA;
2920 for (k = 1; k < 8; k++)
2921 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2926 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2927 if (q2 && q1 != q2) {
2928 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2929 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2931 return AVERROR_INVALIDDATA;
2932 for (k = 1; k < 8; k++)
2933 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2938 /* apply AC prediction if needed */
2940 if (dc_pred_dir) { // left
2941 for (k = 1; k < 8; k++) {
2942 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2943 if (!v->pquantizer && block[k << v->left_blk_sh])
2944 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2947 for (k = 1; k < 8; k++) {
2948 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2949 if (!v->pquantizer && block[k << v->top_blk_sh])
2950 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2956 s->block_last_index[n] = i;
2961 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2962 * @param v VC1Context
2963 * @param block block to decode
2964 * @param[in] n subblock index
2965 * @param coded are AC coeffs present or not
2966 * @param mquant block quantizer
2967 * @param codingset set of VLC to decode data
2969 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2970 int coded, int mquant, int codingset)
2972 GetBitContext *gb = &v->s.gb;
2973 MpegEncContext *s = &v->s;
2974 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2976 int16_t *dc_val = NULL;
2977 int16_t *ac_val, *ac_val2;
2979 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2980 int a_avail = v->a_avail, c_avail = v->c_avail;
2981 int use_pred = s->ac_pred;
2985 s->dsp.clear_block(block);
2987 /* XXX: Guard against dumb values of mquant */
2988 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2990 /* Set DC scale - y and c use the same */
2991 s->y_dc_scale = s->y_dc_scale_table[mquant];
2992 s->c_dc_scale = s->c_dc_scale_table[mquant];
2994 /* Get DC differential */
2996 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2998 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
3001 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
3005 if (dcdiff == 119 /* ESC index value */) {
3006 /* TODO: Optimize */
3007 if (mquant == 1) dcdiff = get_bits(gb, 10);
3008 else if (mquant == 2) dcdiff = get_bits(gb, 9);
3009 else dcdiff = get_bits(gb, 8);
3012 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
3013 else if (mquant == 2)
3014 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
3021 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
3024 /* Store the quantized DC coeff, used for prediction */
3027 block[0] = dcdiff * s->y_dc_scale;
3029 block[0] = dcdiff * s->c_dc_scale;
3035 /* check if AC is needed at all and adjust direction if needed */
3036 if (!a_avail) dc_pred_dir = 1;
3037 if (!c_avail) dc_pred_dir = 0;
3038 if (!a_avail && !c_avail) use_pred = 0;
3039 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3042 scale = mquant * 2 + v->halfpq;
3044 if (dc_pred_dir) //left
3047 ac_val -= 16 * s->block_wrap[n];
3049 q1 = s->current_picture.qscale_table[mb_pos];
3050 if (dc_pred_dir && c_avail && mb_pos)
3051 q2 = s->current_picture.qscale_table[mb_pos - 1];
3052 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3053 q2 = s->current_picture.qscale_table[mb_pos - s->mb_stride];
3054 if ( dc_pred_dir && n == 1)
3056 if (!dc_pred_dir && n == 2)
3058 if (n == 3) q2 = q1;
3061 int last = 0, skip, value;
3065 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3069 if (v->fcm == PROGRESSIVE)
3070 block[v->zz_8x8[0][i++]] = value;
3072 if (use_pred && (v->fcm == ILACE_FRAME)) {
3073 if (!dc_pred_dir) // top
3074 block[v->zz_8x8[2][i++]] = value;
3076 block[v->zz_8x8[3][i++]] = value;
3078 block[v->zzi_8x8[i++]] = value;
3083 /* apply AC prediction if needed */
3085 /* scale predictors if needed*/
3086 if (q2 && q1 != q2) {
3087 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3088 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3091 return AVERROR_INVALIDDATA;
3092 if (dc_pred_dir) { // left
3093 for (k = 1; k < 8; k++)
3094 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3096 for (k = 1; k < 8; k++)
3097 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3100 if (dc_pred_dir) { // left
3101 for (k = 1; k < 8; k++)
3102 block[k << v->left_blk_sh] += ac_val[k];
3104 for (k = 1; k < 8; k++)
3105 block[k << v->top_blk_sh] += ac_val[k + 8];
3109 /* save AC coeffs for further prediction */
3110 for (k = 1; k < 8; k++) {
3111 ac_val2[k ] = block[k << v->left_blk_sh];
3112 ac_val2[k + 8] = block[k << v->top_blk_sh];
3115 /* scale AC coeffs */
3116 for (k = 1; k < 64; k++)
3120 block[k] += (block[k] < 0) ? -mquant : mquant;
3123 if (use_pred) i = 63;
3124 } else { // no AC coeffs
3127 memset(ac_val2, 0, 16 * 2);
3128 if (dc_pred_dir) { // left
3130 memcpy(ac_val2, ac_val, 8 * 2);
3131 if (q2 && q1 != q2) {
3132 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3133 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3135 return AVERROR_INVALIDDATA;
3136 for (k = 1; k < 8; k++)
3137 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3142 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3143 if (q2 && q1 != q2) {
3144 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3145 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3147 return AVERROR_INVALIDDATA;
3148 for (k = 1; k < 8; k++)
3149 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3154 /* apply AC prediction if needed */
3156 if (dc_pred_dir) { // left
3157 for (k = 1; k < 8; k++) {
3158 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3159 if (!v->pquantizer && block[k << v->left_blk_sh])
3160 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3163 for (k = 1; k < 8; k++) {
3164 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3165 if (!v->pquantizer && block[k << v->top_blk_sh])
3166 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3172 s->block_last_index[n] = i;
3179 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3180 int mquant, int ttmb, int first_block,
3181 uint8_t *dst, int linesize, int skip_block,
3184 MpegEncContext *s = &v->s;
3185 GetBitContext *gb = &s->gb;
3188 int scale, off, idx, last, skip, value;
3189 int ttblk = ttmb & 7;
3192 s->dsp.clear_block(block);
3195 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)];
3197 if (ttblk == TT_4X4) {
3198 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3200 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3201 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3202 || (!v->res_rtm_flag && !first_block))) {
3203 subblkpat = decode012(gb);
3205 subblkpat ^= 3; // swap decoded pattern bits
3206 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3208 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3211 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3213 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3214 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3215 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3218 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3219 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3228 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3233 idx = v->zz_8x8[0][i++];
3235 idx = v->zzi_8x8[i++];
3236 block[idx] = value * scale;
3238 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3242 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3244 v->vc1dsp.vc1_inv_trans_8x8(block);
3245 s->dsp.add_pixels_clamped(block, dst, linesize);
3250 pat = ~subblkpat & 0xF;
3251 for (j = 0; j < 4; j++) {
3252 last = subblkpat & (1 << (3 - j));
3254 off = (j & 1) * 4 + (j & 2) * 16;
3256 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3261 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3263 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3264 block[idx + off] = value * scale;
3266 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3268 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3270 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3272 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3277 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3278 for (j = 0; j < 2; j++) {
3279 last = subblkpat & (1 << (1 - j));
3283 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3288 idx = v->zz_8x4[i++] + off;
3290 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3291 block[idx] = value * scale;
3293 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3295 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3297 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3299 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3304 pat = ~(subblkpat * 5) & 0xF;
3305 for (j = 0; j < 2; j++) {
3306 last = subblkpat & (1 << (1 - j));
3310 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3315 idx = v->zz_4x8[i++] + off;
3317 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3318 block[idx] = value * scale;
3320 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3322 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3324 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3326 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3332 *ttmb_out |= ttblk << (n * 4);
3336 /** @} */ // Macroblock group
3338 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3339 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3341 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3343 MpegEncContext *s = &v->s;
3344 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3345 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3346 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3347 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3348 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3351 if (block_num > 3) {
3352 dst = s->dest[block_num - 3];
3354 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3356 if (s->mb_y != s->end_mb_y || block_num < 2) {
3360 if (block_num > 3) {
3361 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3362 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3363 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3364 mv_stride = s->mb_stride;
3366 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3367 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3368 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3369 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3370 mv_stride = s->b8_stride;
3371 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3374 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3375 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3376 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3378 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3380 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3383 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3385 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3390 dst -= 4 * linesize;
3391 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3392 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3393 idx = (block_cbp | (block_cbp >> 2)) & 3;
3395 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3398 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3400 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3405 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3407 MpegEncContext *s = &v->s;
3408 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3409 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3410 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3411 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3412 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3415 if (block_num > 3) {
3416 dst = s->dest[block_num - 3] - 8 * linesize;
3418 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3421 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3424 if (block_num > 3) {
3425 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3426 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3427 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3429 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3430 : (mb_cbp >> ((block_num + 1) * 4));
3431 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3432 : (mb_is_intra >> ((block_num + 1) * 4));
3433 mv = &s->current_picture.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3435 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3436 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3438 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3440 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3443 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3445 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3451 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3452 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3453 idx = (block_cbp | (block_cbp >> 1)) & 5;
3455 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3458 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3460 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3465 static void vc1_apply_p_loop_filter(VC1Context *v)
3467 MpegEncContext *s = &v->s;
3470 for (i = 0; i < 6; i++) {
3471 vc1_apply_p_v_loop_filter(v, i);
3474 /* V always precedes H, therefore we run H one MB before V;
3475 * at the end of a row, we catch up to complete the row */
3477 for (i = 0; i < 6; i++) {
3478 vc1_apply_p_h_loop_filter(v, i);
3480 if (s->mb_x == s->mb_width - 1) {
3482 ff_update_block_index(s);
3483 for (i = 0; i < 6; i++) {
3484 vc1_apply_p_h_loop_filter(v, i);
3490 /** Decode one P-frame MB
3492 static int vc1_decode_p_mb(VC1Context *v)
3494 MpegEncContext *s = &v->s;
3495 GetBitContext *gb = &s->gb;
3497 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3498 int cbp; /* cbp decoding stuff */
3499 int mqdiff, mquant; /* MB quantization */
3500 int ttmb = v->ttfrm; /* MB Transform type */
3502 int mb_has_coeffs = 1; /* last_flag */
3503 int dmv_x, dmv_y; /* Differential MV components */
3504 int index, index1; /* LUT indexes */
3505 int val, sign; /* temp values */
3506 int first_block = 1;
3508 int skipped, fourmv;
3509 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3511 mquant = v->pq; /* lossy initialization */
3513 if (v->mv_type_is_raw)
3514 fourmv = get_bits1(gb);
3516 fourmv = v->mv_type_mb_plane[mb_pos];
3518 skipped = get_bits1(gb);
3520 skipped = v->s.mbskip_table[mb_pos];
3522 if (!fourmv) { /* 1MV mode */
3524 GET_MVDATA(dmv_x, dmv_y);
3527 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
3528 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
3530 s->current_picture.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3531 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3533 /* FIXME Set DC val for inter block ? */
3534 if (s->mb_intra && !mb_has_coeffs) {
3536 s->ac_pred = get_bits1(gb);
3538 } else if (mb_has_coeffs) {
3540 s->ac_pred = get_bits1(gb);
3541 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3547 s->current_picture.qscale_table[mb_pos] = mquant;
3549 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3550 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3551 VC1_TTMB_VLC_BITS, 2);
3552 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3554 for (i = 0; i < 6; i++) {
3555 s->dc_val[0][s->block_index[i]] = 0;
3557 val = ((cbp >> (5 - i)) & 1);
3558 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3559 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3561 /* check if prediction blocks A and C are available */
3562 v->a_avail = v->c_avail = 0;
3563 if (i == 2 || i == 3 || !s->first_slice_line)
3564 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3565 if (i == 1 || i == 3 || s->mb_x)
3566 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3568 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3569 (i & 4) ? v->codingset2 : v->codingset);
3570 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3572 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3574 for (j = 0; j < 64; j++)
3575 s->block[i][j] <<= 1;
3576 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3577 if (v->pq >= 9 && v->overlap) {
3579 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3581 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3583 block_cbp |= 0xF << (i << 2);
3584 block_intra |= 1 << i;
3586 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3587 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3588 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3589 block_cbp |= pat << (i << 2);
3590 if (!v->ttmbf && ttmb < 8)
3597 for (i = 0; i < 6; i++) {
3598 v->mb_type[0][s->block_index[i]] = 0;
3599 s->dc_val[0][s->block_index[i]] = 0;
3601 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3602 s->current_picture.qscale_table[mb_pos] = 0;
3603 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3606 } else { // 4MV mode
3607 if (!skipped /* unskipped MB */) {
3608 int intra_count = 0, coded_inter = 0;
3609 int is_intra[6], is_coded[6];
3611 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3612 for (i = 0; i < 6; i++) {
3613 val = ((cbp >> (5 - i)) & 1);
3614 s->dc_val[0][s->block_index[i]] = 0;
3621 GET_MVDATA(dmv_x, dmv_y);
3623 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3625 vc1_mc_4mv_luma(v, i, 0, 0);
3626 intra_count += s->mb_intra;
3627 is_intra[i] = s->mb_intra;
3628 is_coded[i] = mb_has_coeffs;
3631 is_intra[i] = (intra_count >= 3);
3635 vc1_mc_4mv_chroma(v, 0);
3636 v->mb_type[0][s->block_index[i]] = is_intra[i];
3638 coded_inter = !is_intra[i] & is_coded[i];
3640 // if there are no coded blocks then don't do anything more
3642 if (!intra_count && !coded_inter)
3645 s->current_picture.qscale_table[mb_pos] = mquant;
3646 /* test if block is intra and has pred */
3649 for (i = 0; i < 6; i++)
3651 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3652 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3658 s->ac_pred = get_bits1(gb);
3662 if (!v->ttmbf && coded_inter)
3663 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3664 for (i = 0; i < 6; i++) {
3666 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3667 s->mb_intra = is_intra[i];
3669 /* check if prediction blocks A and C are available */
3670 v->a_avail = v->c_avail = 0;
3671 if (i == 2 || i == 3 || !s->first_slice_line)
3672 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3673 if (i == 1 || i == 3 || s->mb_x)
3674 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3676 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3677 (i & 4) ? v->codingset2 : v->codingset);
3678 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3680 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3682 for (j = 0; j < 64; j++)
3683 s->block[i][j] <<= 1;
3684 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3685 (i & 4) ? s->uvlinesize : s->linesize);
3686 if (v->pq >= 9 && v->overlap) {
3688 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3690 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3692 block_cbp |= 0xF << (i << 2);
3693 block_intra |= 1 << i;
3694 } else if (is_coded[i]) {
3695 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3696 first_block, s->dest[dst_idx] + off,
3697 (i & 4) ? s->uvlinesize : s->linesize,
3698 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3700 block_cbp |= pat << (i << 2);
3701 if (!v->ttmbf && ttmb < 8)
3706 } else { // skipped MB
3708 s->current_picture.qscale_table[mb_pos] = 0;
3709 for (i = 0; i < 6; i++) {
3710 v->mb_type[0][s->block_index[i]] = 0;
3711 s->dc_val[0][s->block_index[i]] = 0;
3713 for (i = 0; i < 4; i++) {
3714 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3715 vc1_mc_4mv_luma(v, i, 0, 0);
3717 vc1_mc_4mv_chroma(v, 0);
3718 s->current_picture.qscale_table[mb_pos] = 0;
3722 v->cbp[s->mb_x] = block_cbp;
3723 v->ttblk[s->mb_x] = block_tt;
3724 v->is_intra[s->mb_x] = block_intra;
3729 /* Decode one macroblock in an interlaced frame p picture */
3731 static int vc1_decode_p_mb_intfr(VC1Context *v)
3733 MpegEncContext *s = &v->s;
3734 GetBitContext *gb = &s->gb;
3736 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3737 int cbp = 0; /* cbp decoding stuff */
3738 int mqdiff, mquant; /* MB quantization */
3739 int ttmb = v->ttfrm; /* MB Transform type */
3741 int mb_has_coeffs = 1; /* last_flag */
3742 int dmv_x, dmv_y; /* Differential MV components */
3743 int val; /* temp value */
3744 int first_block = 1;
3746 int skipped, fourmv = 0, twomv = 0;
3747 int block_cbp = 0, pat, block_tt = 0;
3748 int idx_mbmode = 0, mvbp;
3749 int stride_y, fieldtx;
3751 mquant = v->pq; /* Lossy initialization */
3754 skipped = get_bits1(gb);
3756 skipped = v->s.mbskip_table[mb_pos];
3758 if (v->fourmvswitch)
3759 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3761 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3762 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3763 /* store the motion vector type in a flag (useful later) */
3764 case MV_PMODE_INTFR_4MV:
3766 v->blk_mv_type[s->block_index[0]] = 0;
3767 v->blk_mv_type[s->block_index[1]] = 0;
3768 v->blk_mv_type[s->block_index[2]] = 0;
3769 v->blk_mv_type[s->block_index[3]] = 0;
3771 case MV_PMODE_INTFR_4MV_FIELD:
3773 v->blk_mv_type[s->block_index[0]] = 1;
3774 v->blk_mv_type[s->block_index[1]] = 1;
3775 v->blk_mv_type[s->block_index[2]] = 1;
3776 v->blk_mv_type[s->block_index[3]] = 1;
3778 case MV_PMODE_INTFR_2MV_FIELD:
3780 v->blk_mv_type[s->block_index[0]] = 1;
3781 v->blk_mv_type[s->block_index[1]] = 1;
3782 v->blk_mv_type[s->block_index[2]] = 1;
3783 v->blk_mv_type[s->block_index[3]] = 1;
3785 case MV_PMODE_INTFR_1MV:
3786 v->blk_mv_type[s->block_index[0]] = 0;
3787 v->blk_mv_type[s->block_index[1]] = 0;
3788 v->blk_mv_type[s->block_index[2]] = 0;
3789 v->blk_mv_type[s->block_index[3]] = 0;
3792 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3793 for (i = 0; i < 4; i++) {
3794 s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
3795 s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
3797 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
3798 s->mb_intra = v->is_intra[s->mb_x] = 1;
3799 for (i = 0; i < 6; i++)
3800 v->mb_type[0][s->block_index[i]] = 1;
3801 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3802 mb_has_coeffs = get_bits1(gb);
3804 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3805 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3807 s->current_picture.qscale_table[mb_pos] = mquant;
3808 /* Set DC scale - y and c use the same (not sure if necessary here) */
3809 s->y_dc_scale = s->y_dc_scale_table[mquant];
3810 s->c_dc_scale = s->c_dc_scale_table[mquant];
3812 for (i = 0; i < 6; i++) {
3813 s->dc_val[0][s->block_index[i]] = 0;
3815 val = ((cbp >> (5 - i)) & 1);
3816 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3817 v->a_avail = v->c_avail = 0;
3818 if (i == 2 || i == 3 || !s->first_slice_line)
3819 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3820 if (i == 1 || i == 3 || s->mb_x)
3821 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3823 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3824 (i & 4) ? v->codingset2 : v->codingset);
3825 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3826 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3828 stride_y = s->linesize << fieldtx;
3829 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3831 stride_y = s->uvlinesize;
3834 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3838 } else { // inter MB
3839 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3841 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3842 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3843 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3845 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3846 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3847 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3850 s->mb_intra = v->is_intra[s->mb_x] = 0;
3851 for (i = 0; i < 6; i++)
3852 v->mb_type[0][s->block_index[i]] = 0;
3853 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3854 /* for all motion vector read MVDATA and motion compensate each block */
3858 for (i = 0; i < 6; i++) {
3861 val = ((mvbp >> (3 - i)) & 1);
3863 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3865 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0);
3866 vc1_mc_4mv_luma(v, i, 0, 0);
3867 } else if (i == 4) {
3868 vc1_mc_4mv_chroma4(v);
3875 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3877 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3878 vc1_mc_4mv_luma(v, 0, 0, 0);
3879 vc1_mc_4mv_luma(v, 1, 0, 0);
3882 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3884 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], 0);
3885 vc1_mc_4mv_luma(v, 2, 0, 0);
3886 vc1_mc_4mv_luma(v, 3, 0, 0);
3887 vc1_mc_4mv_chroma4(v);
3889 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3892 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3894 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3898 GET_MQUANT(); // p. 227
3899 s->current_picture.qscale_table[mb_pos] = mquant;
3900 if (!v->ttmbf && cbp)
3901 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3902 for (i = 0; i < 6; i++) {
3903 s->dc_val[0][s->block_index[i]] = 0;
3905 val = ((cbp >> (5 - i)) & 1);
3907 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3909 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3911 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3912 first_block, s->dest[dst_idx] + off,
3913 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3914 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3915 block_cbp |= pat << (i << 2);
3916 if (!v->ttmbf && ttmb < 8)
3923 s->mb_intra = v->is_intra[s->mb_x] = 0;
3924 for (i = 0; i < 6; i++) {
3925 v->mb_type[0][s->block_index[i]] = 0;
3926 s->dc_val[0][s->block_index[i]] = 0;
3928 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
3929 s->current_picture.qscale_table[mb_pos] = 0;
3930 v->blk_mv_type[s->block_index[0]] = 0;
3931 v->blk_mv_type[s->block_index[1]] = 0;
3932 v->blk_mv_type[s->block_index[2]] = 0;
3933 v->blk_mv_type[s->block_index[3]] = 0;
3934 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
3937 if (s->mb_x == s->mb_width - 1)
3938 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3942 static int vc1_decode_p_mb_intfi(VC1Context *v)
3944 MpegEncContext *s = &v->s;
3945 GetBitContext *gb = &s->gb;
3947 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3948 int cbp = 0; /* cbp decoding stuff */
3949 int mqdiff, mquant; /* MB quantization */
3950 int ttmb = v->ttfrm; /* MB Transform type */
3952 int mb_has_coeffs = 1; /* last_flag */
3953 int dmv_x, dmv_y; /* Differential MV components */
3954 int val; /* temp values */
3955 int first_block = 1;
3958 int block_cbp = 0, pat, block_tt = 0;
3961 mquant = v->pq; /* Lossy initialization */
3963 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3964 if (idx_mbmode <= 1) { // intra MB
3965 s->mb_intra = v->is_intra[s->mb_x] = 1;
3966 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3967 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3968 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3970 s->current_picture.qscale_table[mb_pos] = mquant;
3971 /* Set DC scale - y and c use the same (not sure if necessary here) */
3972 s->y_dc_scale = s->y_dc_scale_table[mquant];
3973 s->c_dc_scale = s->c_dc_scale_table[mquant];
3974 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3975 mb_has_coeffs = idx_mbmode & 1;
3977 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3979 for (i = 0; i < 6; i++) {
3980 s->dc_val[0][s->block_index[i]] = 0;
3981 v->mb_type[0][s->block_index[i]] = 1;
3983 val = ((cbp >> (5 - i)) & 1);
3984 v->a_avail = v->c_avail = 0;
3985 if (i == 2 || i == 3 || !s->first_slice_line)
3986 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3987 if (i == 1 || i == 3 || s->mb_x)
3988 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3990 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3991 (i & 4) ? v->codingset2 : v->codingset);
3992 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3994 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3995 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3996 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3997 // TODO: loop filter
4000 s->mb_intra = v->is_intra[s->mb_x] = 0;
4001 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4002 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4003 if (idx_mbmode <= 5) { // 1-MV
4004 dmv_x = dmv_y = pred_flag = 0;
4005 if (idx_mbmode & 1) {
4006 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4008 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4010 mb_has_coeffs = !(idx_mbmode & 2);
4012 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4013 for (i = 0; i < 6; i++) {
4015 dmv_x = dmv_y = pred_flag = 0;
4016 val = ((v->fourmvbp >> (3 - i)) & 1);
4018 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
4020 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
4021 vc1_mc_4mv_luma(v, i, 0, 0);
4023 vc1_mc_4mv_chroma(v, 0);
4025 mb_has_coeffs = idx_mbmode & 1;
4028 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4032 s->current_picture.qscale_table[mb_pos] = mquant;
4033 if (!v->ttmbf && cbp) {
4034 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4037 for (i = 0; i < 6; i++) {
4038 s->dc_val[0][s->block_index[i]] = 0;
4040 val = ((cbp >> (5 - i)) & 1);
4041 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4043 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4044 first_block, s->dest[dst_idx] + off,
4045 (i & 4) ? s->uvlinesize : s->linesize,
4046 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4048 block_cbp |= pat << (i << 2);
4049 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4054 if (s->mb_x == s->mb_width - 1)
4055 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4059 /** Decode one B-frame MB (in Main profile)
4061 static void vc1_decode_b_mb(VC1Context *v)
4063 MpegEncContext *s = &v->s;
4064 GetBitContext *gb = &s->gb;
4066 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4067 int cbp = 0; /* cbp decoding stuff */
4068 int mqdiff, mquant; /* MB quantization */
4069 int ttmb = v->ttfrm; /* MB Transform type */
4070 int mb_has_coeffs = 0; /* last_flag */
4071 int index, index1; /* LUT indexes */
4072 int val, sign; /* temp values */
4073 int first_block = 1;
4075 int skipped, direct;
4076 int dmv_x[2], dmv_y[2];
4077 int bmvtype = BMV_TYPE_BACKWARD;
4079 mquant = v->pq; /* lossy initialization */
4083 direct = get_bits1(gb);
4085 direct = v->direct_mb_plane[mb_pos];
4087 skipped = get_bits1(gb);
4089 skipped = v->s.mbskip_table[mb_pos];
4091 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4092 for (i = 0; i < 6; i++) {
4093 v->mb_type[0][s->block_index[i]] = 0;
4094 s->dc_val[0][s->block_index[i]] = 0;
4096 s->current_picture.qscale_table[mb_pos] = 0;
4100 GET_MVDATA(dmv_x[0], dmv_y[0]);
4101 dmv_x[1] = dmv_x[0];
4102 dmv_y[1] = dmv_y[0];
4104 if (skipped || !s->mb_intra) {
4105 bmvtype = decode012(gb);
4108 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4111 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4114 bmvtype = BMV_TYPE_INTERPOLATED;
4115 dmv_x[0] = dmv_y[0] = 0;
4119 for (i = 0; i < 6; i++)
4120 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4124 bmvtype = BMV_TYPE_INTERPOLATED;
4125 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4126 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4130 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4133 s->current_picture.qscale_table[mb_pos] = mquant;
4135 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4136 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4137 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4138 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4140 if (!mb_has_coeffs && !s->mb_intra) {
4141 /* no coded blocks - effectively skipped */
4142 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4143 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4146 if (s->mb_intra && !mb_has_coeffs) {
4148 s->current_picture.qscale_table[mb_pos] = mquant;
4149 s->ac_pred = get_bits1(gb);
4151 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4153 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4154 GET_MVDATA(dmv_x[0], dmv_y[0]);
4155 if (!mb_has_coeffs) {
4156 /* interpolated skipped block */
4157 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4158 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4162 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4164 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4167 s->ac_pred = get_bits1(gb);
4168 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4170 s->current_picture.qscale_table[mb_pos] = mquant;
4171 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4172 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4176 for (i = 0; i < 6; i++) {
4177 s->dc_val[0][s->block_index[i]] = 0;
4179 val = ((cbp >> (5 - i)) & 1);
4180 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4181 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4183 /* check if prediction blocks A and C are available */
4184 v->a_avail = v->c_avail = 0;
4185 if (i == 2 || i == 3 || !s->first_slice_line)
4186 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4187 if (i == 1 || i == 3 || s->mb_x)
4188 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4190 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4191 (i & 4) ? v->codingset2 : v->codingset);
4192 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4194 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4196 for (j = 0; j < 64; j++)
4197 s->block[i][j] <<= 1;
4198 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4200 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4201 first_block, s->dest[dst_idx] + off,
4202 (i & 4) ? s->uvlinesize : s->linesize,
4203 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4204 if (!v->ttmbf && ttmb < 8)
4211 /** Decode one B-frame MB (in interlaced field B picture)
4213 static void vc1_decode_b_mb_intfi(VC1Context *v)
4215 MpegEncContext *s = &v->s;
4216 GetBitContext *gb = &s->gb;
4218 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4219 int cbp = 0; /* cbp decoding stuff */
4220 int mqdiff, mquant; /* MB quantization */
4221 int ttmb = v->ttfrm; /* MB Transform type */
4222 int mb_has_coeffs = 0; /* last_flag */
4223 int val; /* temp value */
4224 int first_block = 1;
4227 int dmv_x[2], dmv_y[2], pred_flag[2];
4228 int bmvtype = BMV_TYPE_BACKWARD;
4229 int idx_mbmode, interpmvp;
4231 mquant = v->pq; /* Lossy initialization */
4234 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4235 if (idx_mbmode <= 1) { // intra MB
4236 s->mb_intra = v->is_intra[s->mb_x] = 1;
4237 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4238 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4239 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4241 s->current_picture.qscale_table[mb_pos] = mquant;
4242 /* Set DC scale - y and c use the same (not sure if necessary here) */
4243 s->y_dc_scale = s->y_dc_scale_table[mquant];
4244 s->c_dc_scale = s->c_dc_scale_table[mquant];
4245 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4246 mb_has_coeffs = idx_mbmode & 1;
4248 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4250 for (i = 0; i < 6; i++) {
4251 s->dc_val[0][s->block_index[i]] = 0;
4253 val = ((cbp >> (5 - i)) & 1);
4254 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4255 v->a_avail = v->c_avail = 0;
4256 if (i == 2 || i == 3 || !s->first_slice_line)
4257 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4258 if (i == 1 || i == 3 || s->mb_x)
4259 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4261 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4262 (i & 4) ? v->codingset2 : v->codingset);
4263 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4265 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4267 for (j = 0; j < 64; j++)
4268 s->block[i][j] <<= 1;
4269 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4270 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4271 // TODO: yet to perform loop filter
4274 s->mb_intra = v->is_intra[s->mb_x] = 0;
4275 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4276 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4278 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4280 fwd = v->forward_mb_plane[mb_pos];
4281 if (idx_mbmode <= 5) { // 1-MV
4282 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4283 pred_flag[0] = pred_flag[1] = 0;
4285 bmvtype = BMV_TYPE_FORWARD;
4287 bmvtype = decode012(gb);
4290 bmvtype = BMV_TYPE_BACKWARD;
4293 bmvtype = BMV_TYPE_DIRECT;
4296 bmvtype = BMV_TYPE_INTERPOLATED;
4297 interpmvp = get_bits1(gb);
4300 v->bmvtype = bmvtype;
4301 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4302 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4304 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4305 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4307 if (bmvtype == BMV_TYPE_DIRECT) {
4308 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4309 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4311 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4312 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4313 mb_has_coeffs = !(idx_mbmode & 2);
4316 bmvtype = BMV_TYPE_FORWARD;
4317 v->bmvtype = bmvtype;
4318 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4319 for (i = 0; i < 6; i++) {
4321 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4322 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4323 val = ((v->fourmvbp >> (3 - i)) & 1);
4325 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4326 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4327 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4329 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4330 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD, 0);
4332 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4334 mb_has_coeffs = idx_mbmode & 1;
4337 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4341 s->current_picture.qscale_table[mb_pos] = mquant;
4342 if (!v->ttmbf && cbp) {
4343 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4346 for (i = 0; i < 6; i++) {
4347 s->dc_val[0][s->block_index[i]] = 0;
4349 val = ((cbp >> (5 - i)) & 1);
4350 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4352 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4353 first_block, s->dest[dst_idx] + off,
4354 (i & 4) ? s->uvlinesize : s->linesize,
4355 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4356 if (!v->ttmbf && ttmb < 8)
4364 /** Decode one B-frame MB (in interlaced frame B picture)
4366 static int vc1_decode_b_mb_intfr(VC1Context *v)
4368 MpegEncContext *s = &v->s;
4369 GetBitContext *gb = &s->gb;
4371 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4372 int cbp = 0; /* cbp decoding stuff */
4373 int mqdiff, mquant; /* MB quantization */
4374 int ttmb = v->ttfrm; /* MB Transform type */
4375 int mvsw = 0; /* motion vector switch */
4376 int mb_has_coeffs = 1; /* last_flag */
4377 int dmv_x, dmv_y; /* Differential MV components */
4378 int val; /* temp value */
4379 int first_block = 1;
4381 int skipped, direct, twomv = 0;
4382 int block_cbp = 0, pat, block_tt = 0;
4383 int idx_mbmode = 0, mvbp;
4384 int stride_y, fieldtx;
4385 int bmvtype = BMV_TYPE_BACKWARD;
4388 mquant = v->pq; /* Lossy initialization */
4391 skipped = get_bits1(gb);
4393 skipped = v->s.mbskip_table[mb_pos];
4396 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2);
4397 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD)
4400 v->blk_mv_type[s->block_index[0]] = 1;
4401 v->blk_mv_type[s->block_index[1]] = 1;
4402 v->blk_mv_type[s->block_index[2]] = 1;
4403 v->blk_mv_type[s->block_index[3]] = 1;
4405 v->blk_mv_type[s->block_index[0]] = 0;
4406 v->blk_mv_type[s->block_index[1]] = 0;
4407 v->blk_mv_type[s->block_index[2]] = 0;
4408 v->blk_mv_type[s->block_index[3]] = 0;
4413 direct = get_bits1(gb);
4415 direct = v->direct_mb_plane[mb_pos];
4418 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);
4419 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);
4420 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);
4421 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);
4424 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);
4425 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);
4426 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);
4427 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);
4429 for (i = 1; i < 4; i+=2) {
4430 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][i-1][0];
4431 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][i-1][1];
4432 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][i-1][0];
4433 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][i-1][1];
4436 for (i = 1; i < 4; i++) {
4437 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = s->mv[0][0][0];
4438 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = s->mv[0][0][1];
4439 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = s->mv[1][0][0];
4440 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = s->mv[1][0][1];
4445 if (ff_vc1_mbmode_intfrp[0][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
4446 for (i = 0; i < 4; i++) {
4447 s->mv[0][i][0] = s->current_picture.motion_val[0][s->block_index[i]][0] = 0;
4448 s->mv[0][i][1] = s->current_picture.motion_val[0][s->block_index[i]][1] = 0;
4449 s->mv[1][i][0] = s->current_picture.motion_val[1][s->block_index[i]][0] = 0;
4450 s->mv[1][i][1] = s->current_picture.motion_val[1][s->block_index[i]][1] = 0;
4452 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4453 s->mb_intra = v->is_intra[s->mb_x] = 1;
4454 for (i = 0; i < 6; i++)
4455 v->mb_type[0][s->block_index[i]] = 1;
4456 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
4457 mb_has_coeffs = get_bits1(gb);
4459 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4460 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4462 s->current_picture.qscale_table[mb_pos] = mquant;
4463 /* Set DC scale - y and c use the same (not sure if necessary here) */
4464 s->y_dc_scale = s->y_dc_scale_table[mquant];
4465 s->c_dc_scale = s->c_dc_scale_table[mquant];
4467 for (i = 0; i < 6; i++) {
4468 s->dc_val[0][s->block_index[i]] = 0;
4470 val = ((cbp >> (5 - i)) & 1);
4471 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4472 v->a_avail = v->c_avail = 0;
4473 if (i == 2 || i == 3 || !s->first_slice_line)
4474 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4475 if (i == 1 || i == 3 || s->mb_x)
4476 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4478 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4479 (i & 4) ? v->codingset2 : v->codingset);
4480 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
4481 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4483 stride_y = s->linesize << fieldtx;
4484 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
4486 stride_y = s->uvlinesize;
4489 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
4492 s->mb_intra = v->is_intra[s->mb_x] = 0;
4494 if (skipped || !s->mb_intra) {
4495 bmvtype = decode012(gb);
4498 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4501 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4504 bmvtype = BMV_TYPE_INTERPOLATED;
4508 if (twomv && bmvtype != BMV_TYPE_INTERPOLATED)
4509 mvsw = get_bits1(gb);
4512 if (!skipped) { // inter MB
4513 mb_has_coeffs = ff_vc1_mbmode_intfrp[0][idx_mbmode][3];
4515 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4517 if (bmvtype == BMV_TYPE_INTERPOLATED & twomv) {
4518 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4520 else if (bmvtype == BMV_TYPE_INTERPOLATED | twomv) {
4521 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
4525 for (i = 0; i < 6; i++)
4526 v->mb_type[0][s->block_index[i]] = 0;
4527 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[0][idx_mbmode][1];
4528 /* for all motion vector read MVDATA and motion compensate each block */
4532 for (i = 0; i < 4; i++) {
4533 vc1_mc_4mv_luma(v, i, 0, 0);
4534 vc1_mc_4mv_luma(v, i, 1, 1);
4536 vc1_mc_4mv_chroma4(v);
4541 } else if (twomv && bmvtype == BMV_TYPE_INTERPOLATED) {
4543 for (i = 0; i < 4; i++) {
4546 val = ((mvbp >> (3 - i)) & 1);
4548 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4551 vc1_pred_mv_intfr(v, j, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4552 vc1_mc_4mv_luma(v, j, dir, dir);
4553 vc1_mc_4mv_luma(v, j+1, dir, dir);
4556 vc1_mc_4mv_chroma4(v);
4557 } else if (bmvtype == BMV_TYPE_INTERPOLATED) {
4561 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4564 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4569 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4572 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4575 dir = bmvtype == BMV_TYPE_BACKWARD;
4582 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4584 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir);
4588 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4590 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0], dir2);
4593 for (i = 0; i<2; i++) {
4594 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];
4595 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];
4596 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];
4597 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];
4600 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4601 vc1_pred_mv_intfr(v, 2, 0, 0, 2, v->range_x, v->range_y, v->mb_type[0], !dir);
4604 vc1_mc_4mv_luma(v, 0, dir, 0);
4605 vc1_mc_4mv_luma(v, 1, dir, 0);
4606 vc1_mc_4mv_luma(v, 2, dir2, 0);
4607 vc1_mc_4mv_luma(v, 3, dir2, 0);
4608 vc1_mc_4mv_chroma4(v);
4610 dir = bmvtype == BMV_TYPE_BACKWARD;
4612 mvbp = ff_vc1_mbmode_intfrp[0][idx_mbmode][2];
4615 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
4618 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4619 v->blk_mv_type[s->block_index[0]] = 1;
4620 v->blk_mv_type[s->block_index[1]] = 1;
4621 v->blk_mv_type[s->block_index[2]] = 1;
4622 v->blk_mv_type[s->block_index[3]] = 1;
4623 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4624 for (i = 0; i<2; i++) {
4625 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];
4626 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];
4632 GET_MQUANT(); // p. 227
4633 s->current_picture.qscale_table[mb_pos] = mquant;
4634 if (!v->ttmbf && cbp)
4635 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4636 for (i = 0; i < 6; i++) {
4637 s->dc_val[0][s->block_index[i]] = 0;
4639 val = ((cbp >> (5 - i)) & 1);
4641 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4643 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
4645 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4646 first_block, s->dest[dst_idx] + off,
4647 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
4648 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
4649 block_cbp |= pat << (i << 2);
4650 if (!v->ttmbf && ttmb < 8)
4658 for (i = 0; i < 6; i++) {
4659 v->mb_type[0][s->block_index[i]] = 0;
4660 s->dc_val[0][s->block_index[i]] = 0;
4662 s->current_picture.mb_type[mb_pos] = MB_TYPE_SKIP;
4663 s->current_picture.qscale_table[mb_pos] = 0;
4664 v->blk_mv_type[s->block_index[0]] = 0;
4665 v->blk_mv_type[s->block_index[1]] = 0;
4666 v->blk_mv_type[s->block_index[2]] = 0;
4667 v->blk_mv_type[s->block_index[3]] = 0;
4670 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4671 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0);
4672 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 1);
4674 dir = bmvtype == BMV_TYPE_BACKWARD;
4675 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], dir);
4680 for (i = 0; i<2; i++) {
4681 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];
4682 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];
4683 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];
4684 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];
4687 v->blk_mv_type[s->block_index[0]] = 1;
4688 v->blk_mv_type[s->block_index[1]] = 1;
4689 v->blk_mv_type[s->block_index[2]] = 1;
4690 v->blk_mv_type[s->block_index[3]] = 1;
4691 vc1_pred_mv_intfr(v, 0, 0, 0, 2, v->range_x, v->range_y, 0, !dir);
4692 for (i = 0; i<2; i++) {
4693 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];
4694 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];
4701 if (direct || bmvtype == BMV_TYPE_INTERPOLATED) {
4706 if (s->mb_x == s->mb_width - 1)
4707 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
4708 v->cbp[s->mb_x] = block_cbp;
4709 v->ttblk[s->mb_x] = block_tt;
4713 /** Decode blocks of I-frame
4715 static void vc1_decode_i_blocks(VC1Context *v)
4718 MpegEncContext *s = &v->s;
4723 /* select codingmode used for VLC tables selection */
4724 switch (v->y_ac_table_index) {
4726 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4729 v->codingset = CS_HIGH_MOT_INTRA;
4732 v->codingset = CS_MID_RATE_INTRA;
4736 switch (v->c_ac_table_index) {
4738 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4741 v->codingset2 = CS_HIGH_MOT_INTER;
4744 v->codingset2 = CS_MID_RATE_INTER;
4748 /* Set DC scale - y and c use the same */
4749 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4750 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4753 s->mb_x = s->mb_y = 0;
4755 s->first_slice_line = 1;
4756 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4758 init_block_index(v);
4759 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4761 ff_update_block_index(s);
4762 dst[0] = s->dest[0];
4763 dst[1] = dst[0] + 8;
4764 dst[2] = s->dest[0] + s->linesize * 8;
4765 dst[3] = dst[2] + 8;
4766 dst[4] = s->dest[1];
4767 dst[5] = s->dest[2];
4768 s->dsp.clear_blocks(s->block[0]);
4769 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4770 s->current_picture.mb_type[mb_pos] = MB_TYPE_INTRA;
4771 s->current_picture.qscale_table[mb_pos] = v->pq;
4772 s->current_picture.motion_val[1][s->block_index[0]][0] = 0;
4773 s->current_picture.motion_val[1][s->block_index[0]][1] = 0;
4775 // do actual MB decoding and displaying
4776 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4777 v->s.ac_pred = get_bits1(&v->s.gb);
4779 for (k = 0; k < 6; k++) {
4780 val = ((cbp >> (5 - k)) & 1);
4783 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4787 cbp |= val << (5 - k);
4789 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4791 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4793 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4794 if (v->pq >= 9 && v->overlap) {
4796 for (j = 0; j < 64; j++)
4797 s->block[k][j] <<= 1;
4798 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4801 for (j = 0; j < 64; j++)
4802 s->block[k][j] = (s->block[k][j] - 64) << 1;
4803 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4807 if (v->pq >= 9 && v->overlap) {
4809 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4810 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4811 if (!(s->flags & CODEC_FLAG_GRAY)) {
4812 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4813 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4816 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4817 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4818 if (!s->first_slice_line) {
4819 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4820 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4821 if (!(s->flags & CODEC_FLAG_GRAY)) {
4822 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4823 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4826 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4827 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4829 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4831 if (get_bits_count(&s->gb) > v->bits) {
4832 ff_er_add_slice(&s->er, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4833 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4834 get_bits_count(&s->gb), v->bits);
4838 if (!v->s.loop_filter)
4839 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4841 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4843 s->first_slice_line = 0;
4845 if (v->s.loop_filter)
4846 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4848 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4849 * profile, these only differ are when decoding MSS2 rectangles. */
4850 ff_er_add_slice(&s->er, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4853 /** Decode blocks of I-frame for advanced profile
4855 static void vc1_decode_i_blocks_adv(VC1Context *v)
4858 MpegEncContext *s = &v->s;
4864 GetBitContext *gb = &s->gb;
4866 /* select codingmode used for VLC tables selection */
4867 switch (v->y_ac_table_index) {
4869 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4872 v->codingset = CS_HIGH_MOT_INTRA;
4875 v->codingset = CS_MID_RATE_INTRA;
4879 switch (v->c_ac_table_index) {
4881 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4884 v->codingset2 = CS_HIGH_MOT_INTER;
4887 v->codingset2 = CS_MID_RATE_INTER;
4892 s->mb_x = s->mb_y = 0;
4894 s->first_slice_line = 1;
4895 s->mb_y = s->start_mb_y;
4896 if (s->start_mb_y) {
4898 init_block_index(v);
4899 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4900 (1 + s->b8_stride) * sizeof(*s->coded_block));
4902 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4904 init_block_index(v);
4905 for (;s->mb_x < s->mb_width; s->mb_x++) {
4906 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4907 ff_update_block_index(s);
4908 s->dsp.clear_blocks(block[0]);
4909 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4910 s->current_picture.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4911 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4912 s->current_picture.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4914 // do actual MB decoding and displaying
4915 if (v->fieldtx_is_raw)
4916 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4917 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4918 if ( v->acpred_is_raw)
4919 v->s.ac_pred = get_bits1(&v->s.gb);
4921 v->s.ac_pred = v->acpred_plane[mb_pos];
4923 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4924 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4928 s->current_picture.qscale_table[mb_pos] = mquant;
4929 /* Set DC scale - y and c use the same */
4930 s->y_dc_scale = s->y_dc_scale_table[mquant];
4931 s->c_dc_scale = s->c_dc_scale_table[mquant];
4933 for (k = 0; k < 6; k++) {
4934 val = ((cbp >> (5 - k)) & 1);
4937 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4941 cbp |= val << (5 - k);
4943 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4944 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4946 vc1_decode_i_block_adv(v, block[k], k, val,
4947 (k < 4) ? v->codingset : v->codingset2, mquant);
4949 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4951 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4954 vc1_smooth_overlap_filter_iblk(v);
4955 vc1_put_signed_blocks_clamped(v);
4956 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4958 if (get_bits_count(&s->gb) > v->bits) {
4959 // TODO: may need modification to handle slice coding
4960 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4961 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4962 get_bits_count(&s->gb), v->bits);
4966 if (!v->s.loop_filter)
4967 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
4969 ff_mpeg_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4970 s->first_slice_line = 0;
4973 /* raw bottom MB row */
4975 init_block_index(v);
4977 for (;s->mb_x < s->mb_width; s->mb_x++) {
4978 ff_update_block_index(s);
4979 vc1_put_signed_blocks_clamped(v);
4980 if (v->s.loop_filter)
4981 vc1_loop_filter_iblk_delayed(v, v->pq);
4983 if (v->s.loop_filter)
4984 ff_mpeg_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4985 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4986 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4989 static void vc1_decode_p_blocks(VC1Context *v)
4991 MpegEncContext *s = &v->s;
4992 int apply_loop_filter;
4994 /* select codingmode used for VLC tables selection */
4995 switch (v->c_ac_table_index) {
4997 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5000 v->codingset = CS_HIGH_MOT_INTRA;
5003 v->codingset = CS_MID_RATE_INTRA;
5007 switch (v->c_ac_table_index) {
5009 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5012 v->codingset2 = CS_HIGH_MOT_INTER;
5015 v->codingset2 = CS_MID_RATE_INTER;
5019 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
5020 s->first_slice_line = 1;
5021 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
5022 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5024 init_block_index(v);
5025 for (; s->mb_x < s->mb_width; s->mb_x++) {
5026 ff_update_block_index(s);
5028 if (v->fcm == ILACE_FIELD)
5029 vc1_decode_p_mb_intfi(v);
5030 else if (v->fcm == ILACE_FRAME)
5031 vc1_decode_p_mb_intfr(v);
5032 else vc1_decode_p_mb(v);
5033 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
5034 vc1_apply_p_loop_filter(v);
5035 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5036 // TODO: may need modification to handle slice coding
5037 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5038 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5039 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5043 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
5044 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
5045 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
5046 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
5047 if (s->mb_y != s->start_mb_y) ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5048 s->first_slice_line = 0;
5050 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
5052 init_block_index(v);
5053 for (; s->mb_x < s->mb_width; s->mb_x++) {
5054 ff_update_block_index(s);
5055 vc1_apply_p_loop_filter(v);
5058 if (s->end_mb_y >= s->start_mb_y)
5059 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5060 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5061 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5064 static void vc1_decode_b_blocks(VC1Context *v)
5066 MpegEncContext *s = &v->s;
5068 /* select codingmode used for VLC tables selection */
5069 switch (v->c_ac_table_index) {
5071 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
5074 v->codingset = CS_HIGH_MOT_INTRA;
5077 v->codingset = CS_MID_RATE_INTRA;
5081 switch (v->c_ac_table_index) {
5083 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
5086 v->codingset2 = CS_HIGH_MOT_INTER;
5089 v->codingset2 = CS_MID_RATE_INTER;
5093 s->first_slice_line = 1;
5094 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5096 init_block_index(v);
5097 for (; s->mb_x < s->mb_width; s->mb_x++) {
5098 ff_update_block_index(s);
5100 if (v->fcm == ILACE_FIELD)
5101 vc1_decode_b_mb_intfi(v);
5102 else if (v->fcm == ILACE_FRAME)
5103 vc1_decode_b_mb_intfr(v);
5106 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
5107 // TODO: may need modification to handle slice coding
5108 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
5109 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
5110 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
5113 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
5115 if (!v->s.loop_filter)
5116 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5118 ff_mpeg_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
5119 s->first_slice_line = 0;
5121 if (v->s.loop_filter)
5122 ff_mpeg_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
5123 ff_er_add_slice(&s->er, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
5124 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
5127 static void vc1_decode_skip_blocks(VC1Context *v)
5129 MpegEncContext *s = &v->s;
5131 ff_er_add_slice(&s->er, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
5132 s->first_slice_line = 1;
5133 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
5135 init_block_index(v);
5136 ff_update_block_index(s);
5137 if (s->last_picture.f.data[0]) {
5138 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
5139 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5140 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
5142 ff_mpeg_draw_horiz_band(s, s->mb_y * 16, 16);
5143 s->first_slice_line = 0;
5145 s->pict_type = AV_PICTURE_TYPE_P;
5148 void ff_vc1_decode_blocks(VC1Context *v)
5151 v->s.esc3_level_length = 0;
5153 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
5156 v->left_blk_idx = -1;
5157 v->topleft_blk_idx = 1;
5159 switch (v->s.pict_type) {
5160 case AV_PICTURE_TYPE_I:
5161 if (v->profile == PROFILE_ADVANCED)
5162 vc1_decode_i_blocks_adv(v);
5164 vc1_decode_i_blocks(v);
5166 case AV_PICTURE_TYPE_P:
5167 if (v->p_frame_skipped)
5168 vc1_decode_skip_blocks(v);
5170 vc1_decode_p_blocks(v);
5172 case AV_PICTURE_TYPE_B:
5174 if (v->profile == PROFILE_ADVANCED)
5175 vc1_decode_i_blocks_adv(v);
5177 vc1_decode_i_blocks(v);
5179 vc1_decode_b_blocks(v);
5185 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5189 * Transform coefficients for both sprites in 16.16 fixed point format,
5190 * in the order they appear in the bitstream:
5192 * rotation 1 (unused)
5194 * rotation 2 (unused)
5201 int effect_type, effect_flag;
5202 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
5203 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
5206 static inline int get_fp_val(GetBitContext* gb)
5208 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
5211 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
5215 switch (get_bits(gb, 2)) {
5218 c[2] = get_fp_val(gb);
5222 c[0] = c[4] = get_fp_val(gb);
5223 c[2] = get_fp_val(gb);
5226 c[0] = get_fp_val(gb);
5227 c[2] = get_fp_val(gb);
5228 c[4] = get_fp_val(gb);
5231 c[0] = get_fp_val(gb);
5232 c[1] = get_fp_val(gb);
5233 c[2] = get_fp_val(gb);
5234 c[3] = get_fp_val(gb);
5235 c[4] = get_fp_val(gb);
5238 c[5] = get_fp_val(gb);
5240 c[6] = get_fp_val(gb);
5245 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
5247 AVCodecContext *avctx = v->s.avctx;
5250 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5251 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
5252 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
5253 avpriv_request_sample(avctx, "Non-zero rotation coefficients");
5254 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
5255 for (i = 0; i < 7; i++)
5256 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
5257 sd->coefs[sprite][i] / (1<<16),
5258 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
5259 av_log(avctx, AV_LOG_DEBUG, "\n");
5263 if (sd->effect_type = get_bits_long(gb, 30)) {
5264 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
5266 vc1_sprite_parse_transform(gb, sd->effect_params1);
5269 vc1_sprite_parse_transform(gb, sd->effect_params1);
5270 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
5273 for (i = 0; i < sd->effect_pcount1; i++)
5274 sd->effect_params1[i] = get_fp_val(gb);
5276 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
5277 // effect 13 is simple alpha blending and matches the opacity above
5278 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
5279 for (i = 0; i < sd->effect_pcount1; i++)
5280 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5281 sd->effect_params1[i] / (1 << 16),
5282 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
5283 av_log(avctx, AV_LOG_DEBUG, "\n");
5286 sd->effect_pcount2 = get_bits(gb, 16);
5287 if (sd->effect_pcount2 > 10) {
5288 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
5290 } else if (sd->effect_pcount2) {
5292 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
5293 while (++i < sd->effect_pcount2) {
5294 sd->effect_params2[i] = get_fp_val(gb);
5295 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
5296 sd->effect_params2[i] / (1 << 16),
5297 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
5299 av_log(avctx, AV_LOG_DEBUG, "\n");
5302 if (sd->effect_flag = get_bits1(gb))
5303 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
5305 if (get_bits_count(gb) >= gb->size_in_bits +
5306 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
5307 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
5308 if (get_bits_count(gb) < gb->size_in_bits - 8)
5309 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
5312 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
5314 int i, plane, row, sprite;
5315 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
5316 uint8_t* src_h[2][2];
5317 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
5319 MpegEncContext *s = &v->s;
5321 for (i = 0; i < 2; i++) {
5322 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
5323 xadv[i] = sd->coefs[i][0];
5324 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
5325 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
5327 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
5328 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
5330 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
5332 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
5333 int width = v->output_width>>!!plane;
5335 for (row = 0; row < v->output_height>>!!plane; row++) {
5336 uint8_t *dst = v->sprite_output_frame.data[plane] +
5337 v->sprite_output_frame.linesize[plane] * row;
5339 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
5340 uint8_t *iplane = s->current_picture.f.data[plane];
5341 int iline = s->current_picture.f.linesize[plane];
5342 int ycoord = yoff[sprite] + yadv[sprite] * row;
5343 int yline = ycoord >> 16;
5345 ysub[sprite] = ycoord & 0xFFFF;
5347 iplane = s->last_picture.f.data[plane];
5348 iline = s->last_picture.f.linesize[plane];
5350 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
5351 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
5352 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
5354 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
5356 if (sr_cache[sprite][0] != yline) {
5357 if (sr_cache[sprite][1] == yline) {
5358 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
5359 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
5361 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
5362 sr_cache[sprite][0] = yline;
5365 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
5366 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
5367 iplane + next_line, xoff[sprite],
5368 xadv[sprite], width);
5369 sr_cache[sprite][1] = yline + 1;
5371 src_h[sprite][0] = v->sr_rows[sprite][0];
5372 src_h[sprite][1] = v->sr_rows[sprite][1];
5376 if (!v->two_sprites) {
5378 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
5380 memcpy(dst, src_h[0][0], width);
5383 if (ysub[0] && ysub[1]) {
5384 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5385 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5386 } else if (ysub[0]) {
5387 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5388 src_h[1][0], alpha, width);
5389 } else if (ysub[1]) {
5390 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5391 src_h[0][0], (1<<16)-1-alpha, width);
5393 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5399 for (i = 0; i < 2; i++) {
5409 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5412 MpegEncContext *s = &v->s;
5413 AVCodecContext *avctx = s->avctx;
5416 vc1_parse_sprites(v, gb, &sd);
5418 if (!s->current_picture.f.data[0]) {
5419 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5423 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5424 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5428 av_frame_unref(&v->sprite_output_frame);
5429 if ((ret = ff_get_buffer(avctx, &v->sprite_output_frame, 0)) < 0)
5432 vc1_draw_sprites(v, &sd);
5437 static void vc1_sprite_flush(AVCodecContext *avctx)
5439 VC1Context *v = avctx->priv_data;
5440 MpegEncContext *s = &v->s;
5441 AVFrame *f = &s->current_picture.f;
5444 /* Windows Media Image codecs have a convergence interval of two keyframes.
5445 Since we can't enforce it, clear to black the missing sprite. This is
5446 wrong but it looks better than doing nothing. */
5449 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5450 for (i = 0; i < v->sprite_height>>!!plane; i++)
5451 memset(f->data[plane] + i * f->linesize[plane],
5452 plane ? 128 : 0, f->linesize[plane]);
5457 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5459 MpegEncContext *s = &v->s;
5462 /* Allocate mb bitplanes */
5463 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5464 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5465 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5466 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5467 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5468 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5470 v->n_allocated_blks = s->mb_width + 2;
5471 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5472 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5473 v->cbp = v->cbp_base + s->mb_stride;
5474 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5475 v->ttblk = v->ttblk_base + s->mb_stride;
5476 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5477 v->is_intra = v->is_intra_base + s->mb_stride;
5478 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5479 v->luma_mv = v->luma_mv_base + s->mb_stride;
5481 /* allocate block type info in that way so it could be used with s->block_index[] */
5482 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5483 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5484 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5485 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5487 /* allocate memory to store block level MV info */
5488 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5489 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5490 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5491 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5492 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5493 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5494 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5495 v->mv_f_next[1] = v->mv_f_next[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5497 /* Init coded blocks info */
5498 if (v->profile == PROFILE_ADVANCED) {
5499 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5501 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5505 ff_intrax8_common_init(&v->x8,s);
5507 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5508 for (i = 0; i < 4; i++)
5509 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5512 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5513 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5520 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5523 for (i = 0; i < 64; i++) {
5524 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5525 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5526 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5527 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5528 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5529 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5535 /** Initialize a VC1/WMV3 decoder
5536 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5537 * @todo TODO: Decypher remaining bits in extra_data
5539 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5541 VC1Context *v = avctx->priv_data;
5542 MpegEncContext *s = &v->s;
5545 /* save the container output size for WMImage */
5546 v->output_width = avctx->width;
5547 v->output_height = avctx->height;
5549 if (!avctx->extradata_size || !avctx->extradata)
5551 if (!(avctx->flags & CODEC_FLAG_GRAY))
5552 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5554 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5555 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5557 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5558 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5560 if (ff_vc1_init_common(v) < 0)
5562 // ensure static VLC tables are initialized
5563 if (ff_msmpeg4_decode_init(avctx) < 0)
5565 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5567 // Hack to ensure the above functions will be called
5568 // again once we know all necessary settings.
5569 // That this is necessary might indicate a bug.
5570 ff_vc1_decode_end(avctx);
5572 ff_h264chroma_init(&v->h264chroma, 8);
5573 ff_vc1dsp_init(&v->vc1dsp);
5575 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5578 // looks like WMV3 has a sequence header stored in the extradata
5579 // advanced sequence header may be before the first frame
5580 // the last byte of the extradata is a version number, 1 for the
5581 // samples we can decode
5583 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5585 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5588 count = avctx->extradata_size*8 - get_bits_count(&gb);
5590 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5591 count, get_bits(&gb, count));
5592 } else if (count < 0) {
5593 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5595 } else { // VC1/WVC1/WVP2
5596 const uint8_t *start = avctx->extradata;
5597 uint8_t *end = avctx->extradata + avctx->extradata_size;
5598 const uint8_t *next;
5599 int size, buf2_size;
5600 uint8_t *buf2 = NULL;
5601 int seq_initialized = 0, ep_initialized = 0;
5603 if (avctx->extradata_size < 16) {
5604 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5608 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5609 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5611 for (; next < end; start = next) {
5612 next = find_next_marker(start + 4, end);
5613 size = next - start - 4;
5616 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5617 init_get_bits(&gb, buf2, buf2_size * 8);
5618 switch (AV_RB32(start)) {
5619 case VC1_CODE_SEQHDR:
5620 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5624 seq_initialized = 1;
5626 case VC1_CODE_ENTRYPOINT:
5627 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5636 if (!seq_initialized || !ep_initialized) {
5637 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5640 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5643 avctx->profile = v->profile;
5644 if (v->profile == PROFILE_ADVANCED)
5645 avctx->level = v->level;
5647 avctx->has_b_frames = !!avctx->max_b_frames;
5649 s->mb_width = (avctx->coded_width + 15) >> 4;
5650 s->mb_height = (avctx->coded_height + 15) >> 4;
5652 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5653 ff_vc1_init_transposed_scantables(v);
5655 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5660 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5661 v->sprite_width = avctx->coded_width;
5662 v->sprite_height = avctx->coded_height;
5664 avctx->coded_width = avctx->width = v->output_width;
5665 avctx->coded_height = avctx->height = v->output_height;
5667 // prevent 16.16 overflows
5668 if (v->sprite_width > 1 << 14 ||
5669 v->sprite_height > 1 << 14 ||
5670 v->output_width > 1 << 14 ||
5671 v->output_height > 1 << 14) return -1;
5673 if ((v->sprite_width&1) || (v->sprite_height&1)) {
5674 avpriv_request_sample(avctx, "odd sprites support");
5675 return AVERROR_PATCHWELCOME;
5681 /** Close a VC1/WMV3 decoder
5682 * @warning Initial try at using MpegEncContext stuff
5684 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5686 VC1Context *v = avctx->priv_data;
5689 av_frame_unref(&v->sprite_output_frame);
5691 for (i = 0; i < 4; i++)
5692 av_freep(&v->sr_rows[i >> 1][i & 1]);
5693 av_freep(&v->hrd_rate);
5694 av_freep(&v->hrd_buffer);
5695 ff_MPV_common_end(&v->s);
5696 av_freep(&v->mv_type_mb_plane);
5697 av_freep(&v->direct_mb_plane);
5698 av_freep(&v->forward_mb_plane);
5699 av_freep(&v->fieldtx_plane);
5700 av_freep(&v->acpred_plane);
5701 av_freep(&v->over_flags_plane);
5702 av_freep(&v->mb_type_base);
5703 av_freep(&v->blk_mv_type_base);
5704 av_freep(&v->mv_f_base);
5705 av_freep(&v->mv_f_next_base);
5706 av_freep(&v->block);
5707 av_freep(&v->cbp_base);
5708 av_freep(&v->ttblk_base);
5709 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5710 av_freep(&v->luma_mv_base);
5711 ff_intrax8_common_end(&v->x8);
5716 /** Decode a VC1/WMV3 frame
5717 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5719 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5720 int *got_frame, AVPacket *avpkt)
5722 const uint8_t *buf = avpkt->data;
5723 int buf_size = avpkt->size, n_slices = 0, i, ret;
5724 VC1Context *v = avctx->priv_data;
5725 MpegEncContext *s = &v->s;
5726 AVFrame *pict = data;
5727 uint8_t *buf2 = NULL;
5728 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5729 int mb_height, n_slices1=-1;
5734 } *slices = NULL, *tmp;
5736 v->second_field = 0;
5738 if(s->flags & CODEC_FLAG_LOW_DELAY)
5741 /* no supplementary picture */
5742 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5743 /* special case for last picture */
5744 if (s->low_delay == 0 && s->next_picture_ptr) {
5745 if ((ret = av_frame_ref(pict, &s->next_picture_ptr->f)) < 0)
5747 s->next_picture_ptr = NULL;
5755 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5756 if (v->profile < PROFILE_ADVANCED)
5757 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5759 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5762 //for advanced profile we may need to parse and unescape data
5763 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5765 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5767 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5768 const uint8_t *start, *end, *next;
5772 for (start = buf, end = buf + buf_size; next < end; start = next) {
5773 next = find_next_marker(start + 4, end);
5774 size = next - start - 4;
5775 if (size <= 0) continue;
5776 switch (AV_RB32(start)) {
5777 case VC1_CODE_FRAME:
5778 if (avctx->hwaccel ||
5779 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5781 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5783 case VC1_CODE_FIELD: {
5785 if (avctx->hwaccel ||
5786 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5787 buf_start_second_field = start;
5788 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5792 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5793 if (!slices[n_slices].buf)
5795 buf_size3 = vc1_unescape_buffer(start + 4, size,
5796 slices[n_slices].buf);
5797 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5799 /* assuming that the field marker is at the exact middle,
5800 hope it's correct */
5801 slices[n_slices].mby_start = s->mb_height >> 1;
5802 n_slices1 = n_slices - 1; // index of the last slice of the first field
5806 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5807 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5808 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5809 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5811 case VC1_CODE_SLICE: {
5813 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5817 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5818 if (!slices[n_slices].buf)
5820 buf_size3 = vc1_unescape_buffer(start + 4, size,
5821 slices[n_slices].buf);
5822 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5824 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5830 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5831 const uint8_t *divider;
5834 divider = find_next_marker(buf, buf + buf_size);
5835 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5836 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5838 } else { // found field marker, unescape second field
5839 if (avctx->hwaccel ||
5840 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5841 buf_start_second_field = divider;
5842 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5846 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5847 if (!slices[n_slices].buf)
5849 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5850 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5852 slices[n_slices].mby_start = s->mb_height >> 1;
5853 n_slices1 = n_slices - 1;
5856 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5858 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5860 init_get_bits(&s->gb, buf2, buf_size2*8);
5862 init_get_bits(&s->gb, buf, buf_size*8);
5864 if (v->res_sprite) {
5865 v->new_sprite = !get_bits1(&s->gb);
5866 v->two_sprites = get_bits1(&s->gb);
5867 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5868 we're using the sprite compositor. These are intentionally kept separate
5869 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5870 the vc1 one for WVP2 */
5871 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5872 if (v->new_sprite) {
5873 // switch AVCodecContext parameters to those of the sprites
5874 avctx->width = avctx->coded_width = v->sprite_width;
5875 avctx->height = avctx->coded_height = v->sprite_height;
5882 if (s->context_initialized &&
5883 (s->width != avctx->coded_width ||
5884 s->height != avctx->coded_height)) {
5885 ff_vc1_decode_end(avctx);
5888 if (!s->context_initialized) {
5889 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5892 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5894 if (v->profile == PROFILE_ADVANCED) {
5895 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5897 s->h_edge_pos = avctx->coded_width;
5898 s->v_edge_pos = avctx->coded_height;
5902 /* We need to set current_picture_ptr before reading the header,
5903 * otherwise we cannot store anything in there. */
5904 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5905 int i = ff_find_unused_picture(s, 0);
5908 s->current_picture_ptr = &s->picture[i];
5911 // do parse frame header
5912 v->pic_header_flag = 0;
5913 v->first_pic_header_flag = 1;
5914 if (v->profile < PROFILE_ADVANCED) {
5915 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5919 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5923 v->first_pic_header_flag = 0;
5925 if (avctx->debug & FF_DEBUG_PICT_INFO)
5926 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5928 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5929 && s->pict_type != AV_PICTURE_TYPE_I) {
5930 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5934 if ((s->mb_height >> v->field_mode) == 0) {
5935 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5939 // process pulldown flags
5940 s->current_picture_ptr->f.repeat_pict = 0;
5941 // Pulldown flags are only valid when 'broadcast' has been set.
5942 // So ticks_per_frame will be 2
5945 s->current_picture_ptr->f.repeat_pict = 1;
5946 } else if (v->rptfrm) {
5948 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5951 // for skipping the frame
5952 s->current_picture.f.pict_type = s->pict_type;
5953 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5955 /* skip B-frames if we don't have reference frames */
5956 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5959 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5960 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5961 avctx->skip_frame >= AVDISCARD_ALL) {
5965 if (s->next_p_frame_damaged) {
5966 if (s->pict_type == AV_PICTURE_TYPE_B)
5969 s->next_p_frame_damaged = 0;
5972 if (ff_MPV_frame_start(s, avctx) < 0) {
5976 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5977 v->s.current_picture_ptr->f.top_field_first = v->tff;
5979 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5980 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5982 if ((CONFIG_VC1_VDPAU_DECODER)
5983 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5984 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5985 else if (avctx->hwaccel) {
5986 if (v->field_mode && buf_start_second_field) {
5987 // decode first field
5988 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5989 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5991 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5993 if (avctx->hwaccel->end_frame(avctx) < 0)
5996 // decode second field
5997 s->gb = slices[n_slices1 + 1].gb;
5998 s->picture_structure = PICT_TOP_FIELD + v->tff;
5999 v->second_field = 1;
6000 v->pic_header_flag = 0;
6001 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
6002 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
6005 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
6007 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6009 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
6011 if (avctx->hwaccel->end_frame(avctx) < 0)
6014 s->picture_structure = PICT_FRAME;
6015 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6017 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
6019 if (avctx->hwaccel->end_frame(avctx) < 0)
6026 ff_mpeg_er_frame_start(s);
6028 v->bits = buf_size * 8;
6029 v->end_mb_x = s->mb_width;
6030 if (v->field_mode) {
6031 s->current_picture.f.linesize[0] <<= 1;
6032 s->current_picture.f.linesize[1] <<= 1;
6033 s->current_picture.f.linesize[2] <<= 1;
6035 s->uvlinesize <<= 1;
6037 mb_height = s->mb_height >> v->field_mode;
6038 for (i = 0; i <= n_slices; i++) {
6039 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
6040 if (v->field_mode <= 0) {
6041 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
6042 "picture boundary (%d >= %d)\n", i,
6043 slices[i - 1].mby_start, mb_height);
6046 v->second_field = 1;
6047 v->blocks_off = s->b8_stride * (s->mb_height&~1);
6048 v->mb_off = s->mb_stride * s->mb_height >> 1;
6050 v->second_field = 0;
6055 v->pic_header_flag = 0;
6056 if (v->field_mode && i == n_slices1 + 2) {
6057 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6058 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
6061 } else if (get_bits1(&s->gb)) {
6062 v->pic_header_flag = 1;
6063 if ((header_ret = ff_vc1_parse_frame_header_adv(v, &s->gb)) < 0) {
6064 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
6071 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
6072 if (!v->field_mode || v->second_field)
6073 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6075 if (i >= n_slices) {
6076 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
6079 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
6081 if (s->end_mb_y <= s->start_mb_y) {
6082 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
6085 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
6086 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
6089 ff_vc1_decode_blocks(v);
6091 s->gb = slices[i].gb;
6093 if (v->field_mode) {
6094 v->second_field = 0;
6095 s->current_picture.f.linesize[0] >>= 1;
6096 s->current_picture.f.linesize[1] >>= 1;
6097 s->current_picture.f.linesize[2] >>= 1;
6099 s->uvlinesize >>= 1;
6100 if (v->s.pict_type != AV_PICTURE_TYPE_BI && v->s.pict_type != AV_PICTURE_TYPE_B) {
6101 FFSWAP(uint8_t *, v->mv_f_next[0], v->mv_f[0]);
6102 FFSWAP(uint8_t *, v->mv_f_next[1], v->mv_f[1]);
6105 av_dlog(s->avctx, "Consumed %i/%i bits\n",
6106 get_bits_count(&s->gb), s->gb.size_in_bits);
6107 // if (get_bits_count(&s->gb) > buf_size * 8)
6109 if(s->er.error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
6112 ff_er_frame_end(&s->er);
6115 ff_MPV_frame_end(s);
6117 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
6119 avctx->width = avctx->coded_width = v->output_width;
6120 avctx->height = avctx->coded_height = v->output_height;
6121 if (avctx->skip_frame >= AVDISCARD_NONREF)
6123 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
6124 if (vc1_decode_sprites(v, &s->gb))
6127 if ((ret = av_frame_ref(pict, &v->sprite_output_frame)) < 0)
6131 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
6132 if ((ret = av_frame_ref(pict, &s->current_picture_ptr->f)) < 0)
6134 ff_print_debug_info(s, s->current_picture_ptr, pict);
6135 } else if (s->last_picture_ptr != NULL) {
6136 if ((ret = av_frame_ref(pict, &s->last_picture_ptr->f)) < 0)
6138 ff_print_debug_info(s, s->last_picture_ptr, pict);
6140 if (s->last_picture_ptr || s->low_delay) {
6147 for (i = 0; i < n_slices; i++)
6148 av_free(slices[i].buf);
6154 for (i = 0; i < n_slices; i++)
6155 av_free(slices[i].buf);
6161 static const AVProfile profiles[] = {
6162 { FF_PROFILE_VC1_SIMPLE, "Simple" },
6163 { FF_PROFILE_VC1_MAIN, "Main" },
6164 { FF_PROFILE_VC1_COMPLEX, "Complex" },
6165 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
6166 { FF_PROFILE_UNKNOWN },
6169 static const enum AVPixelFormat vc1_hwaccel_pixfmt_list_420[] = {
6171 AV_PIX_FMT_DXVA2_VLD,
6174 AV_PIX_FMT_VAAPI_VLD,
6183 AVCodec ff_vc1_decoder = {
6185 .type = AVMEDIA_TYPE_VIDEO,
6186 .id = AV_CODEC_ID_VC1,
6187 .priv_data_size = sizeof(VC1Context),
6188 .init = vc1_decode_init,
6189 .close = ff_vc1_decode_end,
6190 .decode = vc1_decode_frame,
6191 .flush = ff_mpeg_flush,
6192 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6193 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
6194 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6195 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6198 #if CONFIG_WMV3_DECODER
6199 AVCodec ff_wmv3_decoder = {
6201 .type = AVMEDIA_TYPE_VIDEO,
6202 .id = AV_CODEC_ID_WMV3,
6203 .priv_data_size = sizeof(VC1Context),
6204 .init = vc1_decode_init,
6205 .close = ff_vc1_decode_end,
6206 .decode = vc1_decode_frame,
6207 .flush = ff_mpeg_flush,
6208 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
6209 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
6210 .pix_fmts = vc1_hwaccel_pixfmt_list_420,
6211 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6215 #if CONFIG_WMV3_VDPAU_DECODER
6216 AVCodec ff_wmv3_vdpau_decoder = {
6217 .name = "wmv3_vdpau",
6218 .type = AVMEDIA_TYPE_VIDEO,
6219 .id = AV_CODEC_ID_WMV3,
6220 .priv_data_size = sizeof(VC1Context),
6221 .init = vc1_decode_init,
6222 .close = ff_vc1_decode_end,
6223 .decode = vc1_decode_frame,
6224 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6225 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
6226 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
6227 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6231 #if CONFIG_VC1_VDPAU_DECODER
6232 AVCodec ff_vc1_vdpau_decoder = {
6233 .name = "vc1_vdpau",
6234 .type = AVMEDIA_TYPE_VIDEO,
6235 .id = AV_CODEC_ID_VC1,
6236 .priv_data_size = sizeof(VC1Context),
6237 .init = vc1_decode_init,
6238 .close = ff_vc1_decode_end,
6239 .decode = vc1_decode_frame,
6240 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
6241 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
6242 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
6243 .profiles = NULL_IF_CONFIG_SMALL(profiles)
6247 #if CONFIG_WMV3IMAGE_DECODER
6248 AVCodec ff_wmv3image_decoder = {
6249 .name = "wmv3image",
6250 .type = AVMEDIA_TYPE_VIDEO,
6251 .id = AV_CODEC_ID_WMV3IMAGE,
6252 .priv_data_size = sizeof(VC1Context),
6253 .init = vc1_decode_init,
6254 .close = ff_vc1_decode_end,
6255 .decode = vc1_decode_frame,
6256 .capabilities = CODEC_CAP_DR1,
6257 .flush = vc1_sprite_flush,
6258 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
6259 .pix_fmts = ff_pixfmt_list_420
6263 #if CONFIG_VC1IMAGE_DECODER
6264 AVCodec ff_vc1image_decoder = {
6266 .type = AVMEDIA_TYPE_VIDEO,
6267 .id = AV_CODEC_ID_VC1IMAGE,
6268 .priv_data_size = sizeof(VC1Context),
6269 .init = vc1_decode_init,
6270 .close = ff_vc1_decode_end,
6271 .decode = vc1_decode_frame,
6272 .capabilities = CODEC_CAP_DR1,
6273 .flush = vc1_sprite_flush,
6274 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
6275 .pix_fmts = ff_pixfmt_list_420