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
32 #include "mpegvideo.h"
36 #include "vc1acdata.h"
37 #include "msmpeg4data.h"
40 #include "vdpau_internal.h"
41 #include "libavutil/avassert.h"
46 #define MB_INTRA_VLC_BITS 9
50 // offset tables for interlaced picture MVDATA decoding
51 static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
52 static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
54 /***********************************************************************/
56 * @name VC-1 Bitplane decoding
74 /** @} */ //imode defines
77 /** @} */ //Bitplane group
79 static void vc1_put_signed_blocks_clamped(VC1Context *v)
81 MpegEncContext *s = &v->s;
82 int topleft_mb_pos, top_mb_pos;
83 int stride_y, fieldtx = 0;
86 /* The put pixels loop is always one MB row behind the decoding loop,
87 * because we can only put pixels when overlap filtering is done, and
88 * for filtering of the bottom edge of a MB, we need the next MB row
90 * Within the row, the put pixels loop is also one MB col behind the
91 * decoding loop. The reason for this is again, because for filtering
92 * of the right MB edge, we need the next MB present. */
93 if (!s->first_slice_line) {
95 topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
96 if (v->fcm == ILACE_FRAME)
97 fieldtx = v->fieldtx_plane[topleft_mb_pos];
98 stride_y = s->linesize << fieldtx;
99 v_dist = (16 - fieldtx) >> (fieldtx == 0);
100 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
101 s->dest[0] - 16 * s->linesize - 16,
103 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
104 s->dest[0] - 16 * s->linesize - 8,
106 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
107 s->dest[0] - v_dist * s->linesize - 16,
109 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
110 s->dest[0] - v_dist * s->linesize - 8,
112 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
113 s->dest[1] - 8 * s->uvlinesize - 8,
115 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
116 s->dest[2] - 8 * s->uvlinesize - 8,
119 if (s->mb_x == s->mb_width - 1) {
120 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
121 if (v->fcm == ILACE_FRAME)
122 fieldtx = v->fieldtx_plane[top_mb_pos];
123 stride_y = s->linesize << fieldtx;
124 v_dist = fieldtx ? 15 : 8;
125 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
126 s->dest[0] - 16 * s->linesize,
128 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
129 s->dest[0] - 16 * s->linesize + 8,
131 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
132 s->dest[0] - v_dist * s->linesize,
134 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
135 s->dest[0] - v_dist * s->linesize + 8,
137 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
138 s->dest[1] - 8 * s->uvlinesize,
140 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
141 s->dest[2] - 8 * s->uvlinesize,
146 #define inc_blk_idx(idx) do { \
148 if (idx >= v->n_allocated_blks) \
152 inc_blk_idx(v->topleft_blk_idx);
153 inc_blk_idx(v->top_blk_idx);
154 inc_blk_idx(v->left_blk_idx);
155 inc_blk_idx(v->cur_blk_idx);
158 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
160 MpegEncContext *s = &v->s;
162 if (!s->first_slice_line) {
163 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
165 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
166 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
167 for (j = 0; j < 2; j++) {
168 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
170 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
173 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
175 if (s->mb_y == s->end_mb_y - 1) {
177 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
178 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
179 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
181 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
185 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
187 MpegEncContext *s = &v->s;
190 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
191 * means it runs two rows/cols behind the decoding loop. */
192 if (!s->first_slice_line) {
194 if (s->mb_y >= s->start_mb_y + 2) {
195 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
198 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
199 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
200 for (j = 0; j < 2; j++) {
201 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
203 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
207 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
210 if (s->mb_x == s->mb_width - 1) {
211 if (s->mb_y >= s->start_mb_y + 2) {
212 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
215 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
216 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
217 for (j = 0; j < 2; j++) {
218 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
220 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
224 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
227 if (s->mb_y == s->end_mb_y) {
230 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
231 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
233 for (j = 0; j < 2; j++) {
234 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
239 if (s->mb_x == s->mb_width - 1) {
241 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
242 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
244 for (j = 0; j < 2; j++) {
245 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
253 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
255 MpegEncContext *s = &v->s;
258 if (v->condover == CONDOVER_NONE)
261 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
263 /* Within a MB, the horizontal overlap always runs before the vertical.
264 * To accomplish that, we run the H on left and internal borders of the
265 * currently decoded MB. Then, we wait for the next overlap iteration
266 * to do H overlap on the right edge of this MB, before moving over and
267 * running the V overlap. Therefore, the V overlap makes us trail by one
268 * MB col and the H overlap filter makes us trail by one MB row. This
269 * is reflected in the time at which we run the put_pixels loop. */
270 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
271 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
272 v->over_flags_plane[mb_pos - 1])) {
273 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
274 v->block[v->cur_blk_idx][0]);
275 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
276 v->block[v->cur_blk_idx][2]);
277 if (!(s->flags & CODEC_FLAG_GRAY)) {
278 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
279 v->block[v->cur_blk_idx][4]);
280 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
281 v->block[v->cur_blk_idx][5]);
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
285 v->block[v->cur_blk_idx][1]);
286 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
287 v->block[v->cur_blk_idx][3]);
289 if (s->mb_x == s->mb_width - 1) {
290 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
291 v->over_flags_plane[mb_pos - s->mb_stride])) {
292 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
293 v->block[v->cur_blk_idx][0]);
294 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
295 v->block[v->cur_blk_idx][1]);
296 if (!(s->flags & CODEC_FLAG_GRAY)) {
297 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
298 v->block[v->cur_blk_idx][4]);
299 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
300 v->block[v->cur_blk_idx][5]);
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
304 v->block[v->cur_blk_idx][2]);
305 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
306 v->block[v->cur_blk_idx][3]);
309 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
310 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
311 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
312 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
313 v->block[v->left_blk_idx][0]);
314 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
315 v->block[v->left_blk_idx][1]);
316 if (!(s->flags & CODEC_FLAG_GRAY)) {
317 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
318 v->block[v->left_blk_idx][4]);
319 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
320 v->block[v->left_blk_idx][5]);
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
324 v->block[v->left_blk_idx][2]);
325 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
326 v->block[v->left_blk_idx][3]);
330 /** Do motion compensation over 1 macroblock
331 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
333 static void vc1_mc_1mv(VC1Context *v, int dir)
335 MpegEncContext *s = &v->s;
336 DSPContext *dsp = &v->s.dsp;
337 uint8_t *srcY, *srcU, *srcV;
338 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
340 int v_edge_pos = s->v_edge_pos >> v->field_mode;
342 if ((!v->field_mode ||
343 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
344 !v->s.last_picture.f.data[0])
347 mx = s->mv[dir][0][0];
348 my = s->mv[dir][0][1];
350 // store motion vectors for further use in B frames
351 if (s->pict_type == AV_PICTURE_TYPE_P) {
352 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
353 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
356 uvmx = (mx + ((mx & 3) == 3)) >> 1;
357 uvmy = (my + ((my & 3) == 3)) >> 1;
358 v->luma_mv[s->mb_x][0] = uvmx;
359 v->luma_mv[s->mb_x][1] = uvmy;
362 v->cur_field_type != v->ref_field_type[dir]) {
363 my = my - 2 + 4 * v->cur_field_type;
364 uvmy = uvmy - 2 + 4 * v->cur_field_type;
367 // fastuvmc shall be ignored for interlaced frame picture
368 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
369 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
370 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
372 if (v->field_mode) { // interlaced field picture
374 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
375 srcY = s->current_picture.f.data[0];
376 srcU = s->current_picture.f.data[1];
377 srcV = s->current_picture.f.data[2];
379 srcY = s->last_picture.f.data[0];
380 srcU = s->last_picture.f.data[1];
381 srcV = s->last_picture.f.data[2];
384 srcY = s->next_picture.f.data[0];
385 srcU = s->next_picture.f.data[1];
386 srcV = s->next_picture.f.data[2];
390 srcY = s->last_picture.f.data[0];
391 srcU = s->last_picture.f.data[1];
392 srcV = s->last_picture.f.data[2];
394 srcY = s->next_picture.f.data[0];
395 srcU = s->next_picture.f.data[1];
396 srcV = s->next_picture.f.data[2];
403 src_x = s->mb_x * 16 + (mx >> 2);
404 src_y = s->mb_y * 16 + (my >> 2);
405 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
406 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
408 if (v->profile != PROFILE_ADVANCED) {
409 src_x = av_clip( src_x, -16, s->mb_width * 16);
410 src_y = av_clip( src_y, -16, s->mb_height * 16);
411 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
412 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
414 src_x = av_clip( src_x, -17, s->avctx->coded_width);
415 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
416 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
417 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
420 srcY += src_y * s->linesize + src_x;
421 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
422 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
424 if (v->field_mode && v->ref_field_type[dir]) {
425 srcY += s->current_picture_ptr->f.linesize[0];
426 srcU += s->current_picture_ptr->f.linesize[1];
427 srcV += s->current_picture_ptr->f.linesize[2];
430 /* for grayscale we should not try to read from unknown area */
431 if (s->flags & CODEC_FLAG_GRAY) {
432 srcU = s->edge_emu_buffer + 18 * s->linesize;
433 srcV = s->edge_emu_buffer + 18 * s->linesize;
436 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
437 || s->h_edge_pos < 22 || v_edge_pos < 22
438 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
439 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
440 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
442 srcY -= s->mspel * (1 + s->linesize);
443 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
444 17 + s->mspel * 2, 17 + s->mspel * 2,
445 src_x - s->mspel, src_y - s->mspel,
446 s->h_edge_pos, v_edge_pos);
447 srcY = s->edge_emu_buffer;
448 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
449 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
450 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
451 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
454 /* if we deal with range reduction we need to scale source blocks */
455 if (v->rangeredfrm) {
460 for (j = 0; j < 17 + s->mspel * 2; j++) {
461 for (i = 0; i < 17 + s->mspel * 2; i++)
462 src[i] = ((src[i] - 128) >> 1) + 128;
467 for (j = 0; j < 9; j++) {
468 for (i = 0; i < 9; i++) {
469 src[i] = ((src[i] - 128) >> 1) + 128;
470 src2[i] = ((src2[i] - 128) >> 1) + 128;
472 src += s->uvlinesize;
473 src2 += s->uvlinesize;
476 /* if we deal with intensity compensation we need to scale source blocks */
477 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
482 for (j = 0; j < 17 + s->mspel * 2; j++) {
483 for (i = 0; i < 17 + s->mspel * 2; i++)
484 src[i] = v->luty[src[i]];
489 for (j = 0; j < 9; j++) {
490 for (i = 0; i < 9; i++) {
491 src[i] = v->lutuv[src[i]];
492 src2[i] = v->lutuv[src2[i]];
494 src += s->uvlinesize;
495 src2 += s->uvlinesize;
498 srcY += s->mspel * (1 + s->linesize);
501 if (v->field_mode && v->second_field) {
502 off = s->current_picture_ptr->f.linesize[0];
503 off_uv = s->current_picture_ptr->f.linesize[1];
509 dxy = ((my & 3) << 2) | (mx & 3);
510 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
511 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
512 srcY += s->linesize * 8;
513 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
514 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
515 } else { // hpel mc - always used for luma
516 dxy = (my & 2) | ((mx & 2) >> 1);
518 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
520 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
523 if (s->flags & CODEC_FLAG_GRAY) return;
524 /* Chroma MC always uses qpel bilinear */
525 uvmx = (uvmx & 3) << 1;
526 uvmy = (uvmy & 3) << 1;
528 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
529 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
531 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
532 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
536 static inline int median4(int a, int b, int c, int d)
539 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
540 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
542 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
543 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
547 /** Do motion compensation for 4-MV macroblock - luminance block
549 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
551 MpegEncContext *s = &v->s;
552 DSPContext *dsp = &v->s.dsp;
554 int dxy, mx, my, src_x, src_y;
556 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
557 int v_edge_pos = s->v_edge_pos >> v->field_mode;
559 if ((!v->field_mode ||
560 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
561 !v->s.last_picture.f.data[0])
564 mx = s->mv[dir][n][0];
565 my = s->mv[dir][n][1];
569 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
570 srcY = s->current_picture.f.data[0];
572 srcY = s->last_picture.f.data[0];
574 srcY = s->last_picture.f.data[0];
576 srcY = s->next_picture.f.data[0];
582 if (v->cur_field_type != v->ref_field_type[dir])
583 my = my - 2 + 4 * v->cur_field_type;
586 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
587 int same_count = 0, opp_count = 0, k;
588 int chosen_mv[2][4][2], f;
590 for (k = 0; k < 4; k++) {
591 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
592 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
593 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
597 f = opp_count > same_count;
598 switch (f ? opp_count : same_count) {
600 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
601 chosen_mv[f][2][0], chosen_mv[f][3][0]);
602 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
603 chosen_mv[f][2][1], chosen_mv[f][3][1]);
606 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
607 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
610 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
611 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
616 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
617 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
618 for (k = 0; k < 4; k++)
619 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
622 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
624 int width = s->avctx->coded_width;
625 int height = s->avctx->coded_height >> 1;
626 qx = (s->mb_x * 16) + (mx >> 2);
627 qy = (s->mb_y * 8) + (my >> 3);
632 mx -= 4 * (qx - width);
635 else if (qy > height + 1)
636 my -= 8 * (qy - height - 1);
639 if ((v->fcm == ILACE_FRAME) && fieldmv)
640 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
642 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
643 if (v->field_mode && v->second_field)
644 off += s->current_picture_ptr->f.linesize[0];
646 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
648 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
650 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
652 if (v->profile != PROFILE_ADVANCED) {
653 src_x = av_clip(src_x, -16, s->mb_width * 16);
654 src_y = av_clip(src_y, -16, s->mb_height * 16);
656 src_x = av_clip(src_x, -17, s->avctx->coded_width);
657 if (v->fcm == ILACE_FRAME) {
659 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
661 src_y = av_clip(src_y, -18, s->avctx->coded_height);
663 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
667 srcY += src_y * s->linesize + src_x;
668 if (v->field_mode && v->ref_field_type[dir])
669 srcY += s->current_picture_ptr->f.linesize[0];
671 if (fieldmv && !(src_y & 1))
673 if (fieldmv && (src_y & 1) && src_y < 4)
675 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
676 || s->h_edge_pos < 13 || v_edge_pos < 23
677 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
678 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
679 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
680 /* check emulate edge stride and offset */
681 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
682 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
683 src_x - s->mspel, src_y - (s->mspel << fieldmv),
684 s->h_edge_pos, v_edge_pos);
685 srcY = s->edge_emu_buffer;
686 /* if we deal with range reduction we need to scale source blocks */
687 if (v->rangeredfrm) {
692 for (j = 0; j < 9 + s->mspel * 2; j++) {
693 for (i = 0; i < 9 + s->mspel * 2; i++)
694 src[i] = ((src[i] - 128) >> 1) + 128;
695 src += s->linesize << fieldmv;
698 /* if we deal with intensity compensation we need to scale source blocks */
699 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
704 for (j = 0; j < 9 + s->mspel * 2; j++) {
705 for (i = 0; i < 9 + s->mspel * 2; i++)
706 src[i] = v->luty[src[i]];
707 src += s->linesize << fieldmv;
710 srcY += s->mspel * (1 + (s->linesize << fieldmv));
714 dxy = ((my & 3) << 2) | (mx & 3);
715 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
716 } else { // hpel mc - always used for luma
717 dxy = (my & 2) | ((mx & 2) >> 1);
719 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
721 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
725 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
728 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
730 idx = ((a[3] != flag) << 3)
731 | ((a[2] != flag) << 2)
732 | ((a[1] != flag) << 1)
735 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
736 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
738 } else if (count[idx] == 1) {
741 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
742 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
745 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
746 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
749 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
750 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
753 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
754 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
757 } else if (count[idx] == 2) {
759 for (i = 0; i < 3; i++)
764 for (i = t1 + 1; i < 4; i++)
769 *tx = (mvx[t1] + mvx[t2]) / 2;
770 *ty = (mvy[t1] + mvy[t2]) / 2;
778 /** Do motion compensation for 4-MV macroblock - both chroma blocks
780 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
782 MpegEncContext *s = &v->s;
783 DSPContext *dsp = &v->s.dsp;
784 uint8_t *srcU, *srcV;
785 int uvmx, uvmy, uvsrc_x, uvsrc_y;
786 int k, tx = 0, ty = 0;
787 int mvx[4], mvy[4], intra[4], mv_f[4];
789 int chroma_ref_type = v->cur_field_type, off = 0;
790 int v_edge_pos = s->v_edge_pos >> v->field_mode;
792 if (!v->field_mode && !v->s.last_picture.f.data[0])
794 if (s->flags & CODEC_FLAG_GRAY)
797 for (k = 0; k < 4; k++) {
798 mvx[k] = s->mv[dir][k][0];
799 mvy[k] = s->mv[dir][k][1];
800 intra[k] = v->mb_type[0][s->block_index[k]];
802 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
805 /* calculate chroma MV vector from four luma MVs */
806 if (!v->field_mode || (v->field_mode && !v->numref)) {
807 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
808 chroma_ref_type = v->reffield;
810 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
811 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
812 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
813 return; //no need to do MC for intra blocks
817 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
819 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
821 chroma_ref_type = !v->cur_field_type;
823 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
825 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
826 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
827 uvmx = (tx + ((tx & 3) == 3)) >> 1;
828 uvmy = (ty + ((ty & 3) == 3)) >> 1;
830 v->luma_mv[s->mb_x][0] = uvmx;
831 v->luma_mv[s->mb_x][1] = uvmy;
834 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
835 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
837 // Field conversion bias
838 if (v->cur_field_type != chroma_ref_type)
839 uvmy += 2 - 4 * chroma_ref_type;
841 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
842 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
844 if (v->profile != PROFILE_ADVANCED) {
845 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
846 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
848 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
849 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
854 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
855 srcU = s->current_picture.f.data[1];
856 srcV = s->current_picture.f.data[2];
858 srcU = s->last_picture.f.data[1];
859 srcV = s->last_picture.f.data[2];
862 srcU = s->last_picture.f.data[1];
863 srcV = s->last_picture.f.data[2];
866 srcU = s->next_picture.f.data[1];
867 srcV = s->next_picture.f.data[2];
873 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
874 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
877 if (chroma_ref_type) {
878 srcU += s->current_picture_ptr->f.linesize[1];
879 srcV += s->current_picture_ptr->f.linesize[2];
881 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
884 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
885 || s->h_edge_pos < 18 || v_edge_pos < 18
886 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
887 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
888 s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
889 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
890 s->h_edge_pos >> 1, v_edge_pos >> 1);
891 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
892 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
893 s->h_edge_pos >> 1, v_edge_pos >> 1);
894 srcU = s->edge_emu_buffer;
895 srcV = s->edge_emu_buffer + 16;
897 /* if we deal with range reduction we need to scale source blocks */
898 if (v->rangeredfrm) {
904 for (j = 0; j < 9; j++) {
905 for (i = 0; i < 9; i++) {
906 src[i] = ((src[i] - 128) >> 1) + 128;
907 src2[i] = ((src2[i] - 128) >> 1) + 128;
909 src += s->uvlinesize;
910 src2 += s->uvlinesize;
913 /* if we deal with intensity compensation we need to scale source blocks */
914 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
920 for (j = 0; j < 9; j++) {
921 for (i = 0; i < 9; i++) {
922 src[i] = v->lutuv[src[i]];
923 src2[i] = v->lutuv[src2[i]];
925 src += s->uvlinesize;
926 src2 += s->uvlinesize;
931 /* Chroma MC always uses qpel bilinear */
932 uvmx = (uvmx & 3) << 1;
933 uvmy = (uvmy & 3) << 1;
935 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
936 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
938 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
939 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
943 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
945 static void vc1_mc_4mv_chroma4(VC1Context *v)
947 MpegEncContext *s = &v->s;
948 DSPContext *dsp = &v->s.dsp;
949 uint8_t *srcU, *srcV;
950 int uvsrc_x, uvsrc_y;
951 int uvmx_field[4], uvmy_field[4];
953 int fieldmv = v->blk_mv_type[s->block_index[0]];
954 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
955 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
956 int v_edge_pos = s->v_edge_pos >> 1;
958 if (!v->s.last_picture.f.data[0])
960 if (s->flags & CODEC_FLAG_GRAY)
963 for (i = 0; i < 4; i++) {
965 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
968 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
970 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
973 for (i = 0; i < 4; i++) {
974 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
975 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
976 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
977 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
978 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
979 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
980 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
981 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
982 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
983 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
985 if (fieldmv && !(uvsrc_y & 1))
986 v_edge_pos = (s->v_edge_pos >> 1) - 1;
988 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
990 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
991 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
992 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
993 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
994 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
995 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
996 s->h_edge_pos >> 1, v_edge_pos);
997 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
998 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
999 s->h_edge_pos >> 1, v_edge_pos);
1000 srcU = s->edge_emu_buffer;
1001 srcV = s->edge_emu_buffer + 16;
1003 /* if we deal with intensity compensation we need to scale source blocks */
1004 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1006 uint8_t *src, *src2;
1010 for (j = 0; j < 5; j++) {
1011 for (i = 0; i < 5; i++) {
1012 src[i] = v->lutuv[src[i]];
1013 src2[i] = v->lutuv[src2[i]];
1015 src += s->uvlinesize << 1;
1016 src2 += s->uvlinesize << 1;
1021 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1022 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1024 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]);
1025 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]);
1030 /***********************************************************************/
1032 * @name VC-1 Block-level functions
1033 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1039 * @brief Get macroblock-level quantizer scale
1041 #define GET_MQUANT() \
1042 if (v->dquantfrm) { \
1044 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1045 if (v->dqbilevel) { \
1046 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1048 mqdiff = get_bits(gb, 3); \
1050 mquant = v->pq + mqdiff; \
1052 mquant = get_bits(gb, 5); \
1055 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1056 edges = 1 << v->dqsbedge; \
1057 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1058 edges = (3 << v->dqsbedge) % 15; \
1059 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1061 if ((edges&1) && !s->mb_x) \
1062 mquant = v->altpq; \
1063 if ((edges&2) && s->first_slice_line) \
1064 mquant = v->altpq; \
1065 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1066 mquant = v->altpq; \
1067 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1068 mquant = v->altpq; \
1069 if (!mquant || mquant > 31) { \
1070 av_log(v->s.avctx, AV_LOG_ERROR, \
1071 "Overriding invalid mquant %d\n", mquant); \
1077 * @def GET_MVDATA(_dmv_x, _dmv_y)
1078 * @brief Get MV differentials
1079 * @see MVDATA decoding from 8.3.5.2, p(1)20
1080 * @param _dmv_x Horizontal differential for decoded MV
1081 * @param _dmv_y Vertical differential for decoded MV
1083 #define GET_MVDATA(_dmv_x, _dmv_y) \
1084 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1085 VC1_MV_DIFF_VLC_BITS, 2); \
1087 mb_has_coeffs = 1; \
1090 mb_has_coeffs = 0; \
1093 _dmv_x = _dmv_y = 0; \
1094 } else if (index == 35) { \
1095 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1096 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1097 } else if (index == 36) { \
1102 index1 = index % 6; \
1103 if (!s->quarter_sample && index1 == 5) val = 1; \
1105 if (size_table[index1] - val > 0) \
1106 val = get_bits(gb, size_table[index1] - val); \
1108 sign = 0 - (val&1); \
1109 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1111 index1 = index / 6; \
1112 if (!s->quarter_sample && index1 == 5) val = 1; \
1114 if (size_table[index1] - val > 0) \
1115 val = get_bits(gb, size_table[index1] - val); \
1117 sign = 0 - (val & 1); \
1118 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1121 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1122 int *dmv_y, int *pred_flag)
1125 int extend_x = 0, extend_y = 0;
1126 GetBitContext *gb = &v->s.gb;
1129 const int* offs_tab;
1132 bits = VC1_2REF_MVDATA_VLC_BITS;
1135 bits = VC1_1REF_MVDATA_VLC_BITS;
1138 switch (v->dmvrange) {
1146 extend_x = extend_y = 1;
1149 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1151 *dmv_x = get_bits(gb, v->k_x);
1152 *dmv_y = get_bits(gb, v->k_y);
1155 *pred_flag = *dmv_y & 1;
1156 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1158 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1163 av_assert0(index < esc);
1165 offs_tab = offset_table2;
1167 offs_tab = offset_table1;
1168 index1 = (index + 1) % 9;
1170 val = get_bits(gb, index1 + extend_x);
1171 sign = 0 -(val & 1);
1172 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1176 offs_tab = offset_table2;
1178 offs_tab = offset_table1;
1179 index1 = (index + 1) / 9;
1180 if (index1 > v->numref) {
1181 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1182 sign = 0 - (val & 1);
1183 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1186 if (v->numref && pred_flag)
1187 *pred_flag = index1 & 1;
1191 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1193 int scaledvalue, refdist;
1194 int scalesame1, scalesame2;
1195 int scalezone1_x, zone1offset_x;
1196 int table_index = dir ^ v->second_field;
1198 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1199 refdist = v->refdist;
1201 refdist = dir ? v->brfd : v->frfd;
1204 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1205 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1206 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1207 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1212 if (FFABS(n) < scalezone1_x)
1213 scaledvalue = (n * scalesame1) >> 8;
1216 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1218 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1221 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1224 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1226 int scaledvalue, refdist;
1227 int scalesame1, scalesame2;
1228 int scalezone1_y, zone1offset_y;
1229 int table_index = dir ^ v->second_field;
1231 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1232 refdist = v->refdist;
1234 refdist = dir ? v->brfd : v->frfd;
1237 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1238 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1239 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1240 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1245 if (FFABS(n) < scalezone1_y)
1246 scaledvalue = (n * scalesame1) >> 8;
1249 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1251 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1255 if (v->cur_field_type && !v->ref_field_type[dir])
1256 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1258 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1261 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1263 int scalezone1_x, zone1offset_x;
1264 int scaleopp1, scaleopp2, brfd;
1267 brfd = FFMIN(v->brfd, 3);
1268 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1269 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1270 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1271 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1276 if (FFABS(n) < scalezone1_x)
1277 scaledvalue = (n * scaleopp1) >> 8;
1280 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1282 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1285 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1288 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1290 int scalezone1_y, zone1offset_y;
1291 int scaleopp1, scaleopp2, brfd;
1294 brfd = FFMIN(v->brfd, 3);
1295 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1296 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1297 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1298 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1303 if (FFABS(n) < scalezone1_y)
1304 scaledvalue = (n * scaleopp1) >> 8;
1307 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1309 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1312 if (v->cur_field_type && !v->ref_field_type[dir]) {
1313 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1315 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1319 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1322 int brfd, scalesame;
1323 int hpel = 1 - v->s.quarter_sample;
1326 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1328 n = scaleforsame_y(v, i, n, dir) << hpel;
1330 n = scaleforsame_x(v, n, dir) << hpel;
1333 brfd = FFMIN(v->brfd, 3);
1334 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1336 n = (n * scalesame >> 8) << hpel;
1340 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1343 int refdist, scaleopp;
1344 int hpel = 1 - v->s.quarter_sample;
1347 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1349 n = scaleforopp_y(v, n, dir) << hpel;
1351 n = scaleforopp_x(v, n) << hpel;
1354 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1355 refdist = FFMIN(v->refdist, 3);
1357 refdist = dir ? v->brfd : v->frfd;
1358 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1360 n = (n * scaleopp >> 8) << hpel;
1364 /** Predict and set motion vector
1366 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1367 int mv1, int r_x, int r_y, uint8_t* is_intra,
1368 int pred_flag, int dir)
1370 MpegEncContext *s = &v->s;
1371 int xy, wrap, off = 0;
1375 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1376 int opposite, a_f, b_f, c_f;
1377 int16_t field_predA[2];
1378 int16_t field_predB[2];
1379 int16_t field_predC[2];
1380 int a_valid, b_valid, c_valid;
1381 int hybridmv_thresh, y_bias = 0;
1383 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1384 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1388 /* scale MV difference to be quad-pel */
1389 dmv_x <<= 1 - s->quarter_sample;
1390 dmv_y <<= 1 - s->quarter_sample;
1392 wrap = s->b8_stride;
1393 xy = s->block_index[n];
1396 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1397 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1398 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1399 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1400 if (mv1) { /* duplicate motion data for 1-MV block */
1401 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1402 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1403 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1404 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1405 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1406 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1407 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1408 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1409 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1410 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1411 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1412 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1413 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1418 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1419 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1421 if (v->field_mode && mixedmv_pic)
1422 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1424 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1426 //in 4-MV mode different blocks have different B predictor position
1429 off = (s->mb_x > 0) ? -1 : 1;
1432 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1441 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1443 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1444 b_valid = a_valid && (s->mb_width > 1);
1445 c_valid = s->mb_x || (n == 1 || n == 3);
1446 if (v->field_mode) {
1447 a_valid = a_valid && !is_intra[xy - wrap];
1448 b_valid = b_valid && !is_intra[xy - wrap + off];
1449 c_valid = c_valid && !is_intra[xy - 1];
1453 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1454 num_oppfield += a_f;
1455 num_samefield += 1 - a_f;
1456 field_predA[0] = A[0];
1457 field_predA[1] = A[1];
1459 field_predA[0] = field_predA[1] = 0;
1463 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1464 num_oppfield += b_f;
1465 num_samefield += 1 - b_f;
1466 field_predB[0] = B[0];
1467 field_predB[1] = B[1];
1469 field_predB[0] = field_predB[1] = 0;
1473 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1474 num_oppfield += c_f;
1475 num_samefield += 1 - c_f;
1476 field_predC[0] = C[0];
1477 field_predC[1] = C[1];
1479 field_predC[0] = field_predC[1] = 0;
1483 if (v->field_mode) {
1485 // REFFIELD determines if the last field or the second-last field is
1486 // to be used as reference
1487 opposite = 1 - v->reffield;
1489 if (num_samefield <= num_oppfield)
1490 opposite = 1 - pred_flag;
1492 opposite = pred_flag;
1497 if (a_valid && !a_f) {
1498 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1499 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1501 if (b_valid && !b_f) {
1502 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1503 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1505 if (c_valid && !c_f) {
1506 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1507 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1509 v->mv_f[dir][xy + v->blocks_off] = 1;
1510 v->ref_field_type[dir] = !v->cur_field_type;
1512 if (a_valid && a_f) {
1513 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1514 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1516 if (b_valid && b_f) {
1517 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1518 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1520 if (c_valid && c_f) {
1521 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1522 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1524 v->mv_f[dir][xy + v->blocks_off] = 0;
1525 v->ref_field_type[dir] = v->cur_field_type;
1529 px = field_predA[0];
1530 py = field_predA[1];
1531 } else if (c_valid) {
1532 px = field_predC[0];
1533 py = field_predC[1];
1534 } else if (b_valid) {
1535 px = field_predB[0];
1536 py = field_predB[1];
1542 if (num_samefield + num_oppfield > 1) {
1543 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1544 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1547 /* Pullback MV as specified in 8.3.5.3.4 */
1548 if (!v->field_mode) {
1550 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1551 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1552 X = (s->mb_width << 6) - 4;
1553 Y = (s->mb_height << 6) - 4;
1555 if (qx + px < -60) px = -60 - qx;
1556 if (qy + py < -60) py = -60 - qy;
1558 if (qx + px < -28) px = -28 - qx;
1559 if (qy + py < -28) py = -28 - qy;
1561 if (qx + px > X) px = X - qx;
1562 if (qy + py > Y) py = Y - qy;
1565 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1566 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1567 hybridmv_thresh = 32;
1568 if (a_valid && c_valid) {
1569 if (is_intra[xy - wrap])
1570 sum = FFABS(px) + FFABS(py);
1572 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1573 if (sum > hybridmv_thresh) {
1574 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1575 px = field_predA[0];
1576 py = field_predA[1];
1578 px = field_predC[0];
1579 py = field_predC[1];
1582 if (is_intra[xy - 1])
1583 sum = FFABS(px) + FFABS(py);
1585 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1586 if (sum > hybridmv_thresh) {
1587 if (get_bits1(&s->gb)) {
1588 px = field_predA[0];
1589 py = field_predA[1];
1591 px = field_predC[0];
1592 py = field_predC[1];
1599 if (v->field_mode && v->numref)
1601 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1603 /* store MV using signed modulus of MV range defined in 4.11 */
1604 s->mv[dir][n][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1605 s->mv[dir][n][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1] = ((py + dmv_y + r_y - y_bias) & ((r_y << 1) - 1)) - r_y + y_bias;
1606 if (mv1) { /* duplicate motion data for 1-MV block */
1607 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1608 s->current_picture.f.motion_val[dir][xy + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1609 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1610 s->current_picture.f.motion_val[dir][xy + wrap + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1611 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][0] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][0];
1612 s->current_picture.f.motion_val[dir][xy + wrap + 1 + v->blocks_off][1] = s->current_picture.f.motion_val[dir][xy + v->blocks_off][1];
1613 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1614 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];
1618 /** Predict and set motion vector for interlaced frame picture MBs
1620 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1621 int mvn, int r_x, int r_y, uint8_t* is_intra)
1623 MpegEncContext *s = &v->s;
1624 int xy, wrap, off = 0;
1625 int A[2], B[2], C[2];
1627 int a_valid = 0, b_valid = 0, c_valid = 0;
1628 int field_a, field_b, field_c; // 0: same, 1: opposit
1629 int total_valid, num_samefield, num_oppfield;
1630 int pos_c, pos_b, n_adj;
1632 wrap = s->b8_stride;
1633 xy = s->block_index[n];
1636 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1637 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1638 s->current_picture.f.motion_val[1][xy][0] = 0;
1639 s->current_picture.f.motion_val[1][xy][1] = 0;
1640 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1641 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1642 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1643 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1644 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1645 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1646 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1647 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1648 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1649 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1650 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1651 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1652 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1653 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1658 off = ((n == 0) || (n == 1)) ? 1 : -1;
1660 if (s->mb_x || (n == 1) || (n == 3)) {
1661 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1662 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1663 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1664 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1666 } else { // current block has frame mv and cand. has field MV (so average)
1667 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1668 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1669 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1670 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1673 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1679 /* Predict B and C */
1680 B[0] = B[1] = C[0] = C[1] = 0;
1681 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1682 if (!s->first_slice_line) {
1683 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1686 pos_b = s->block_index[n_adj] - 2 * wrap;
1687 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1688 n_adj = (n & 2) | (n & 1);
1690 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1691 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1692 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1693 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1694 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1697 if (s->mb_width > 1) {
1698 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1701 pos_c = s->block_index[2] - 2 * wrap + 2;
1702 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1705 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1706 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1707 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1708 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1709 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1711 if (s->mb_x == s->mb_width - 1) {
1712 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1715 pos_c = s->block_index[3] - 2 * wrap - 2;
1716 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1719 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1720 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1721 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1722 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1723 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1732 pos_b = s->block_index[1];
1734 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1735 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1736 pos_c = s->block_index[0];
1738 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1739 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1742 total_valid = a_valid + b_valid + c_valid;
1743 // check if predictor A is out of bounds
1744 if (!s->mb_x && !(n == 1 || n == 3)) {
1747 // check if predictor B is out of bounds
1748 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1749 B[0] = B[1] = C[0] = C[1] = 0;
1751 if (!v->blk_mv_type[xy]) {
1752 if (s->mb_width == 1) {
1756 if (total_valid >= 2) {
1757 px = mid_pred(A[0], B[0], C[0]);
1758 py = mid_pred(A[1], B[1], C[1]);
1759 } else if (total_valid) {
1760 if (a_valid) { px = A[0]; py = A[1]; }
1761 else if (b_valid) { px = B[0]; py = B[1]; }
1762 else if (c_valid) { px = C[0]; py = C[1]; }
1769 field_a = (A[1] & 4) ? 1 : 0;
1773 field_b = (B[1] & 4) ? 1 : 0;
1777 field_c = (C[1] & 4) ? 1 : 0;
1781 num_oppfield = field_a + field_b + field_c;
1782 num_samefield = total_valid - num_oppfield;
1783 if (total_valid == 3) {
1784 if ((num_samefield == 3) || (num_oppfield == 3)) {
1785 px = mid_pred(A[0], B[0], C[0]);
1786 py = mid_pred(A[1], B[1], C[1]);
1787 } else if (num_samefield >= num_oppfield) {
1788 /* take one MV from same field set depending on priority
1789 the check for B may not be necessary */
1790 px = !field_a ? A[0] : B[0];
1791 py = !field_a ? A[1] : B[1];
1793 px = field_a ? A[0] : B[0];
1794 py = field_a ? A[1] : B[1];
1796 } else if (total_valid == 2) {
1797 if (num_samefield >= num_oppfield) {
1798 if (!field_a && a_valid) {
1801 } else if (!field_b && b_valid) {
1804 } else if (c_valid) {
1809 if (field_a && a_valid) {
1812 } else if (field_b && b_valid) {
1815 } else if (c_valid) {
1820 } else if (total_valid == 1) {
1821 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1822 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1827 /* store MV using signed modulus of MV range defined in 4.11 */
1828 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = ((px + dmv_x + r_x) & ((r_x << 1) - 1)) - r_x;
1829 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = ((py + dmv_y + r_y) & ((r_y << 1) - 1)) - r_y;
1830 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1831 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1832 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1833 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1834 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1835 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1836 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1837 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1838 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1839 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1840 s->mv[0][n + 1][0] = s->mv[0][n][0];
1841 s->mv[0][n + 1][1] = s->mv[0][n][1];
1845 /** Motion compensation for direct or interpolated blocks in B-frames
1847 static void vc1_interp_mc(VC1Context *v)
1849 MpegEncContext *s = &v->s;
1850 DSPContext *dsp = &v->s.dsp;
1851 uint8_t *srcY, *srcU, *srcV;
1852 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1854 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1856 if (!v->field_mode && !v->s.next_picture.f.data[0])
1859 mx = s->mv[1][0][0];
1860 my = s->mv[1][0][1];
1861 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1862 uvmy = (my + ((my & 3) == 3)) >> 1;
1863 if (v->field_mode) {
1864 if (v->cur_field_type != v->ref_field_type[1])
1865 my = my - 2 + 4 * v->cur_field_type;
1866 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1869 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1870 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1872 srcY = s->next_picture.f.data[0];
1873 srcU = s->next_picture.f.data[1];
1874 srcV = s->next_picture.f.data[2];
1876 src_x = s->mb_x * 16 + (mx >> 2);
1877 src_y = s->mb_y * 16 + (my >> 2);
1878 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1879 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1881 if (v->profile != PROFILE_ADVANCED) {
1882 src_x = av_clip( src_x, -16, s->mb_width * 16);
1883 src_y = av_clip( src_y, -16, s->mb_height * 16);
1884 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1885 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1887 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1888 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1889 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1890 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1893 srcY += src_y * s->linesize + src_x;
1894 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1895 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1897 if (v->field_mode && v->ref_field_type[1]) {
1898 srcY += s->current_picture_ptr->f.linesize[0];
1899 srcU += s->current_picture_ptr->f.linesize[1];
1900 srcV += s->current_picture_ptr->f.linesize[2];
1903 /* for grayscale we should not try to read from unknown area */
1904 if (s->flags & CODEC_FLAG_GRAY) {
1905 srcU = s->edge_emu_buffer + 18 * s->linesize;
1906 srcV = s->edge_emu_buffer + 18 * s->linesize;
1909 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1910 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1911 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1912 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1914 srcY -= s->mspel * (1 + s->linesize);
1915 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1916 17 + s->mspel * 2, 17 + s->mspel * 2,
1917 src_x - s->mspel, src_y - s->mspel,
1918 s->h_edge_pos, v_edge_pos);
1919 srcY = s->edge_emu_buffer;
1920 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1921 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1922 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1923 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1926 /* if we deal with range reduction we need to scale source blocks */
1927 if (v->rangeredfrm) {
1929 uint8_t *src, *src2;
1932 for (j = 0; j < 17 + s->mspel * 2; j++) {
1933 for (i = 0; i < 17 + s->mspel * 2; i++)
1934 src[i] = ((src[i] - 128) >> 1) + 128;
1939 for (j = 0; j < 9; j++) {
1940 for (i = 0; i < 9; i++) {
1941 src[i] = ((src[i] - 128) >> 1) + 128;
1942 src2[i] = ((src2[i] - 128) >> 1) + 128;
1944 src += s->uvlinesize;
1945 src2 += s->uvlinesize;
1948 srcY += s->mspel * (1 + s->linesize);
1951 if (v->field_mode && v->second_field) {
1952 off = s->current_picture_ptr->f.linesize[0];
1953 off_uv = s->current_picture_ptr->f.linesize[1];
1960 dxy = ((my & 3) << 2) | (mx & 3);
1961 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1962 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1963 srcY += s->linesize * 8;
1964 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1965 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1967 dxy = (my & 2) | ((mx & 2) >> 1);
1970 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1972 dsp->avg_no_rnd_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize, 16);
1975 if (s->flags & CODEC_FLAG_GRAY) return;
1976 /* Chroma MC always uses qpel blilinear */
1977 uvmx = (uvmx & 3) << 1;
1978 uvmy = (uvmy & 3) << 1;
1980 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1981 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1983 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1984 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1988 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1992 #if B_FRACTION_DEN==256
1996 return 2 * ((value * n + 255) >> 9);
1997 return (value * n + 128) >> 8;
2000 n -= B_FRACTION_DEN;
2002 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2003 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2007 /** Reconstruct motion vector for B-frame and do motion compensation
2009 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2010 int direct, int mode)
2013 v->mv_mode2 = v->mv_mode;
2014 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2020 v->mv_mode = v->mv_mode2;
2023 if (mode == BMV_TYPE_INTERPOLATED) {
2027 v->mv_mode = v->mv_mode2;
2031 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2032 v->mv_mode = v->mv_mode2;
2033 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2035 v->mv_mode = v->mv_mode2;
2038 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2039 int direct, int mvtype)
2041 MpegEncContext *s = &v->s;
2042 int xy, wrap, off = 0;
2047 const uint8_t *is_intra = v->mb_type[0];
2051 /* scale MV difference to be quad-pel */
2052 dmv_x[0] <<= 1 - s->quarter_sample;
2053 dmv_y[0] <<= 1 - s->quarter_sample;
2054 dmv_x[1] <<= 1 - s->quarter_sample;
2055 dmv_y[1] <<= 1 - s->quarter_sample;
2057 wrap = s->b8_stride;
2058 xy = s->block_index[0];
2061 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2062 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2063 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2064 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2067 if (!v->field_mode) {
2068 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2069 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2070 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2071 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2073 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2074 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));
2075 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));
2076 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));
2077 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));
2080 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2081 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2082 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2083 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2087 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2088 C = s->current_picture.f.motion_val[0][xy - 2];
2089 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2090 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2091 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2093 if (!s->mb_x) C[0] = C[1] = 0;
2094 if (!s->first_slice_line) { // predictor A is not out of bounds
2095 if (s->mb_width == 1) {
2099 px = mid_pred(A[0], B[0], C[0]);
2100 py = mid_pred(A[1], B[1], C[1]);
2102 } else if (s->mb_x) { // predictor C is not out of bounds
2108 /* Pullback MV as specified in 8.3.5.3.4 */
2111 if (v->profile < PROFILE_ADVANCED) {
2112 qx = (s->mb_x << 5);
2113 qy = (s->mb_y << 5);
2114 X = (s->mb_width << 5) - 4;
2115 Y = (s->mb_height << 5) - 4;
2116 if (qx + px < -28) px = -28 - qx;
2117 if (qy + py < -28) py = -28 - qy;
2118 if (qx + px > X) px = X - qx;
2119 if (qy + py > Y) py = Y - qy;
2121 qx = (s->mb_x << 6);
2122 qy = (s->mb_y << 6);
2123 X = (s->mb_width << 6) - 4;
2124 Y = (s->mb_height << 6) - 4;
2125 if (qx + px < -60) px = -60 - qx;
2126 if (qy + py < -60) py = -60 - qy;
2127 if (qx + px > X) px = X - qx;
2128 if (qy + py > Y) py = Y - qy;
2131 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2132 if (0 && !s->first_slice_line && s->mb_x) {
2133 if (is_intra[xy - wrap])
2134 sum = FFABS(px) + FFABS(py);
2136 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2138 if (get_bits1(&s->gb)) {
2146 if (is_intra[xy - 2])
2147 sum = FFABS(px) + FFABS(py);
2149 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2151 if (get_bits1(&s->gb)) {
2161 /* store MV using signed modulus of MV range defined in 4.11 */
2162 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2163 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2165 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2166 C = s->current_picture.f.motion_val[1][xy - 2];
2167 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2168 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2169 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2173 if (!s->first_slice_line) { // predictor A is not out of bounds
2174 if (s->mb_width == 1) {
2178 px = mid_pred(A[0], B[0], C[0]);
2179 py = mid_pred(A[1], B[1], C[1]);
2181 } else if (s->mb_x) { // predictor C is not out of bounds
2187 /* Pullback MV as specified in 8.3.5.3.4 */
2190 if (v->profile < PROFILE_ADVANCED) {
2191 qx = (s->mb_x << 5);
2192 qy = (s->mb_y << 5);
2193 X = (s->mb_width << 5) - 4;
2194 Y = (s->mb_height << 5) - 4;
2195 if (qx + px < -28) px = -28 - qx;
2196 if (qy + py < -28) py = -28 - qy;
2197 if (qx + px > X) px = X - qx;
2198 if (qy + py > Y) py = Y - qy;
2200 qx = (s->mb_x << 6);
2201 qy = (s->mb_y << 6);
2202 X = (s->mb_width << 6) - 4;
2203 Y = (s->mb_height << 6) - 4;
2204 if (qx + px < -60) px = -60 - qx;
2205 if (qy + py < -60) py = -60 - qy;
2206 if (qx + px > X) px = X - qx;
2207 if (qy + py > Y) py = Y - qy;
2210 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2211 if (0 && !s->first_slice_line && s->mb_x) {
2212 if (is_intra[xy - wrap])
2213 sum = FFABS(px) + FFABS(py);
2215 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2217 if (get_bits1(&s->gb)) {
2225 if (is_intra[xy - 2])
2226 sum = FFABS(px) + FFABS(py);
2228 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2230 if (get_bits1(&s->gb)) {
2240 /* store MV using signed modulus of MV range defined in 4.11 */
2242 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2243 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2245 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2246 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2247 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2248 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2251 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2253 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2254 MpegEncContext *s = &v->s;
2255 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2257 if (v->bmvtype == BMV_TYPE_DIRECT) {
2258 int total_opp, k, f;
2259 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2260 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2261 v->bfraction, 0, s->quarter_sample);
2262 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2263 v->bfraction, 0, s->quarter_sample);
2264 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2265 v->bfraction, 1, s->quarter_sample);
2266 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2267 v->bfraction, 1, s->quarter_sample);
2269 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2270 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2271 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2272 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2273 f = (total_opp > 2) ? 1 : 0;
2275 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2276 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2279 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2280 for (k = 0; k < 4; k++) {
2281 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2282 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2283 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2284 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2285 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2286 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2290 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2291 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);
2292 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);
2295 if (dir) { // backward
2296 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);
2297 if (n == 3 || mv1) {
2298 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2301 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);
2302 if (n == 3 || mv1) {
2303 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2308 /** Get predicted DC value for I-frames only
2309 * prediction dir: left=0, top=1
2310 * @param s MpegEncContext
2311 * @param overlap flag indicating that overlap filtering is used
2312 * @param pq integer part of picture quantizer
2313 * @param[in] n block index in the current MB
2314 * @param dc_val_ptr Pointer to DC predictor
2315 * @param dir_ptr Prediction direction for use in AC prediction
2317 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2318 int16_t **dc_val_ptr, int *dir_ptr)
2320 int a, b, c, wrap, pred, scale;
2322 static const uint16_t dcpred[32] = {
2323 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2324 114, 102, 93, 85, 79, 73, 68, 64,
2325 60, 57, 54, 51, 49, 47, 45, 43,
2326 41, 39, 38, 37, 35, 34, 33
2329 /* find prediction - wmv3_dc_scale always used here in fact */
2330 if (n < 4) scale = s->y_dc_scale;
2331 else scale = s->c_dc_scale;
2333 wrap = s->block_wrap[n];
2334 dc_val = s->dc_val[0] + s->block_index[n];
2340 b = dc_val[ - 1 - wrap];
2341 a = dc_val[ - wrap];
2343 if (pq < 9 || !overlap) {
2344 /* Set outer values */
2345 if (s->first_slice_line && (n != 2 && n != 3))
2346 b = a = dcpred[scale];
2347 if (s->mb_x == 0 && (n != 1 && n != 3))
2348 b = c = dcpred[scale];
2350 /* Set outer values */
2351 if (s->first_slice_line && (n != 2 && n != 3))
2353 if (s->mb_x == 0 && (n != 1 && n != 3))
2357 if (abs(a - b) <= abs(b - c)) {
2359 *dir_ptr = 1; // left
2362 *dir_ptr = 0; // top
2365 /* update predictor */
2366 *dc_val_ptr = &dc_val[0];
2371 /** Get predicted DC value
2372 * prediction dir: left=0, top=1
2373 * @param s MpegEncContext
2374 * @param overlap flag indicating that overlap filtering is used
2375 * @param pq integer part of picture quantizer
2376 * @param[in] n block index in the current MB
2377 * @param a_avail flag indicating top block availability
2378 * @param c_avail flag indicating left block availability
2379 * @param dc_val_ptr Pointer to DC predictor
2380 * @param dir_ptr Prediction direction for use in AC prediction
2382 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2383 int a_avail, int c_avail,
2384 int16_t **dc_val_ptr, int *dir_ptr)
2386 int a, b, c, wrap, pred;
2388 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2392 wrap = s->block_wrap[n];
2393 dc_val = s->dc_val[0] + s->block_index[n];
2399 b = dc_val[ - 1 - wrap];
2400 a = dc_val[ - wrap];
2401 /* scale predictors if needed */
2402 q1 = s->current_picture.f.qscale_table[mb_pos];
2403 dqscale_index = s->y_dc_scale_table[q1] - 1;
2404 if (dqscale_index < 0)
2406 if (c_avail && (n != 1 && n != 3)) {
2407 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2409 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2411 if (a_avail && (n != 2 && n != 3)) {
2412 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2414 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2416 if (a_avail && c_avail && (n != 3)) {
2421 off -= s->mb_stride;
2422 q2 = s->current_picture.f.qscale_table[off];
2424 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2427 if (a_avail && c_avail) {
2428 if (abs(a - b) <= abs(b - c)) {
2430 *dir_ptr = 1; // left
2433 *dir_ptr = 0; // top
2435 } else if (a_avail) {
2437 *dir_ptr = 0; // top
2438 } else if (c_avail) {
2440 *dir_ptr = 1; // left
2443 *dir_ptr = 1; // left
2446 /* update predictor */
2447 *dc_val_ptr = &dc_val[0];
2451 /** @} */ // Block group
2454 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2455 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2459 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2460 uint8_t **coded_block_ptr)
2462 int xy, wrap, pred, a, b, c;
2464 xy = s->block_index[n];
2465 wrap = s->b8_stride;
2470 a = s->coded_block[xy - 1 ];
2471 b = s->coded_block[xy - 1 - wrap];
2472 c = s->coded_block[xy - wrap];
2481 *coded_block_ptr = &s->coded_block[xy];
2487 * Decode one AC coefficient
2488 * @param v The VC1 context
2489 * @param last Last coefficient
2490 * @param skip How much zero coefficients to skip
2491 * @param value Decoded AC coefficient value
2492 * @param codingset set of VLC to decode data
2495 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2496 int *value, int codingset)
2498 GetBitContext *gb = &v->s.gb;
2499 int index, escape, run = 0, level = 0, lst = 0;
2501 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2502 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2503 run = vc1_index_decode_table[codingset][index][0];
2504 level = vc1_index_decode_table[codingset][index][1];
2505 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2509 escape = decode210(gb);
2511 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2512 run = vc1_index_decode_table[codingset][index][0];
2513 level = vc1_index_decode_table[codingset][index][1];
2514 lst = index >= vc1_last_decode_table[codingset];
2517 level += vc1_last_delta_level_table[codingset][run];
2519 level += vc1_delta_level_table[codingset][run];
2522 run += vc1_last_delta_run_table[codingset][level] + 1;
2524 run += vc1_delta_run_table[codingset][level] + 1;
2530 lst = get_bits1(gb);
2531 if (v->s.esc3_level_length == 0) {
2532 if (v->pq < 8 || v->dquantfrm) { // table 59
2533 v->s.esc3_level_length = get_bits(gb, 3);
2534 if (!v->s.esc3_level_length)
2535 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2536 } else { // table 60
2537 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2539 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2541 run = get_bits(gb, v->s.esc3_run_length);
2542 sign = get_bits1(gb);
2543 level = get_bits(gb, v->s.esc3_level_length);
2554 /** Decode intra block in intra frames - should be faster than decode_intra_block
2555 * @param v VC1Context
2556 * @param block block to decode
2557 * @param[in] n subblock index
2558 * @param coded are AC coeffs present or not
2559 * @param codingset set of VLC to decode data
2561 static int vc1_decode_i_block(VC1Context *v, int16_t block[64], int n,
2562 int coded, int codingset)
2564 GetBitContext *gb = &v->s.gb;
2565 MpegEncContext *s = &v->s;
2566 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2569 int16_t *ac_val, *ac_val2;
2572 /* Get DC differential */
2574 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2576 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2579 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2583 if (dcdiff == 119 /* ESC index value */) {
2584 /* TODO: Optimize */
2585 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2586 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2587 else dcdiff = get_bits(gb, 8);
2590 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2591 else if (v->pq == 2)
2592 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2599 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2602 /* Store the quantized DC coeff, used for prediction */
2604 block[0] = dcdiff * s->y_dc_scale;
2606 block[0] = dcdiff * s->c_dc_scale;
2617 int last = 0, skip, value;
2618 const uint8_t *zz_table;
2622 scale = v->pq * 2 + v->halfpq;
2626 zz_table = v->zz_8x8[2];
2628 zz_table = v->zz_8x8[3];
2630 zz_table = v->zz_8x8[1];
2632 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2634 if (dc_pred_dir) // left
2637 ac_val -= 16 * s->block_wrap[n];
2640 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2644 block[zz_table[i++]] = value;
2647 /* apply AC prediction if needed */
2649 if (dc_pred_dir) { // left
2650 for (k = 1; k < 8; k++)
2651 block[k << v->left_blk_sh] += ac_val[k];
2653 for (k = 1; k < 8; k++)
2654 block[k << v->top_blk_sh] += ac_val[k + 8];
2657 /* save AC coeffs for further prediction */
2658 for (k = 1; k < 8; k++) {
2659 ac_val2[k] = block[k << v->left_blk_sh];
2660 ac_val2[k + 8] = block[k << v->top_blk_sh];
2663 /* scale AC coeffs */
2664 for (k = 1; k < 64; k++)
2668 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2671 if (s->ac_pred) i = 63;
2677 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2681 scale = v->pq * 2 + v->halfpq;
2682 memset(ac_val2, 0, 16 * 2);
2683 if (dc_pred_dir) { // left
2686 memcpy(ac_val2, ac_val, 8 * 2);
2688 ac_val -= 16 * s->block_wrap[n];
2690 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2693 /* apply AC prediction if needed */
2695 if (dc_pred_dir) { //left
2696 for (k = 1; k < 8; k++) {
2697 block[k << v->left_blk_sh] = ac_val[k] * scale;
2698 if (!v->pquantizer && block[k << v->left_blk_sh])
2699 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2702 for (k = 1; k < 8; k++) {
2703 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2704 if (!v->pquantizer && block[k << v->top_blk_sh])
2705 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2711 s->block_last_index[n] = i;
2716 /** Decode intra block in intra frames - should be faster than decode_intra_block
2717 * @param v VC1Context
2718 * @param block block to decode
2719 * @param[in] n subblock number
2720 * @param coded are AC coeffs present or not
2721 * @param codingset set of VLC to decode data
2722 * @param mquant quantizer value for this macroblock
2724 static int vc1_decode_i_block_adv(VC1Context *v, int16_t block[64], int n,
2725 int coded, int codingset, int mquant)
2727 GetBitContext *gb = &v->s.gb;
2728 MpegEncContext *s = &v->s;
2729 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2731 int16_t *dc_val = NULL;
2732 int16_t *ac_val, *ac_val2;
2734 int a_avail = v->a_avail, c_avail = v->c_avail;
2735 int use_pred = s->ac_pred;
2738 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2740 /* Get DC differential */
2742 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2744 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2747 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2751 if (dcdiff == 119 /* ESC index value */) {
2752 /* TODO: Optimize */
2753 if (mquant == 1) dcdiff = get_bits(gb, 10);
2754 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2755 else dcdiff = get_bits(gb, 8);
2758 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2759 else if (mquant == 2)
2760 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2767 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2770 /* Store the quantized DC coeff, used for prediction */
2772 block[0] = dcdiff * s->y_dc_scale;
2774 block[0] = dcdiff * s->c_dc_scale;
2780 /* check if AC is needed at all */
2781 if (!a_avail && !c_avail)
2783 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2786 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2788 if (dc_pred_dir) // left
2791 ac_val -= 16 * s->block_wrap[n];
2793 q1 = s->current_picture.f.qscale_table[mb_pos];
2794 if ( dc_pred_dir && c_avail && mb_pos)
2795 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2796 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2797 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2798 if ( dc_pred_dir && n == 1)
2800 if (!dc_pred_dir && n == 2)
2806 int last = 0, skip, value;
2807 const uint8_t *zz_table;
2811 if (!use_pred && v->fcm == ILACE_FRAME) {
2812 zz_table = v->zzi_8x8;
2814 if (!dc_pred_dir) // top
2815 zz_table = v->zz_8x8[2];
2817 zz_table = v->zz_8x8[3];
2820 if (v->fcm != ILACE_FRAME)
2821 zz_table = v->zz_8x8[1];
2823 zz_table = v->zzi_8x8;
2827 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2831 block[zz_table[i++]] = value;
2834 /* apply AC prediction if needed */
2836 /* scale predictors if needed*/
2837 if (q2 && q1 != q2) {
2838 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2839 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2842 return AVERROR_INVALIDDATA;
2843 if (dc_pred_dir) { // left
2844 for (k = 1; k < 8; k++)
2845 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2847 for (k = 1; k < 8; k++)
2848 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2851 if (dc_pred_dir) { //left
2852 for (k = 1; k < 8; k++)
2853 block[k << v->left_blk_sh] += ac_val[k];
2855 for (k = 1; k < 8; k++)
2856 block[k << v->top_blk_sh] += ac_val[k + 8];
2860 /* save AC coeffs for further prediction */
2861 for (k = 1; k < 8; k++) {
2862 ac_val2[k ] = block[k << v->left_blk_sh];
2863 ac_val2[k + 8] = block[k << v->top_blk_sh];
2866 /* scale AC coeffs */
2867 for (k = 1; k < 64; k++)
2871 block[k] += (block[k] < 0) ? -mquant : mquant;
2874 if (use_pred) i = 63;
2875 } else { // no AC coeffs
2878 memset(ac_val2, 0, 16 * 2);
2879 if (dc_pred_dir) { // left
2881 memcpy(ac_val2, ac_val, 8 * 2);
2882 if (q2 && q1 != q2) {
2883 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2884 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2886 return AVERROR_INVALIDDATA;
2887 for (k = 1; k < 8; k++)
2888 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2893 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2894 if (q2 && q1 != q2) {
2895 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2896 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2898 return AVERROR_INVALIDDATA;
2899 for (k = 1; k < 8; k++)
2900 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2905 /* apply AC prediction if needed */
2907 if (dc_pred_dir) { // left
2908 for (k = 1; k < 8; k++) {
2909 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2910 if (!v->pquantizer && block[k << v->left_blk_sh])
2911 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2914 for (k = 1; k < 8; k++) {
2915 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2916 if (!v->pquantizer && block[k << v->top_blk_sh])
2917 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2923 s->block_last_index[n] = i;
2928 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2929 * @param v VC1Context
2930 * @param block block to decode
2931 * @param[in] n subblock index
2932 * @param coded are AC coeffs present or not
2933 * @param mquant block quantizer
2934 * @param codingset set of VLC to decode data
2936 static int vc1_decode_intra_block(VC1Context *v, int16_t block[64], int n,
2937 int coded, int mquant, int codingset)
2939 GetBitContext *gb = &v->s.gb;
2940 MpegEncContext *s = &v->s;
2941 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2943 int16_t *dc_val = NULL;
2944 int16_t *ac_val, *ac_val2;
2946 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2947 int a_avail = v->a_avail, c_avail = v->c_avail;
2948 int use_pred = s->ac_pred;
2952 s->dsp.clear_block(block);
2954 /* XXX: Guard against dumb values of mquant */
2955 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2957 /* Set DC scale - y and c use the same */
2958 s->y_dc_scale = s->y_dc_scale_table[mquant];
2959 s->c_dc_scale = s->c_dc_scale_table[mquant];
2961 /* Get DC differential */
2963 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2965 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2968 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2972 if (dcdiff == 119 /* ESC index value */) {
2973 /* TODO: Optimize */
2974 if (mquant == 1) dcdiff = get_bits(gb, 10);
2975 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2976 else dcdiff = get_bits(gb, 8);
2979 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2980 else if (mquant == 2)
2981 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2988 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2991 /* Store the quantized DC coeff, used for prediction */
2994 block[0] = dcdiff * s->y_dc_scale;
2996 block[0] = dcdiff * s->c_dc_scale;
3002 /* check if AC is needed at all and adjust direction if needed */
3003 if (!a_avail) dc_pred_dir = 1;
3004 if (!c_avail) dc_pred_dir = 0;
3005 if (!a_avail && !c_avail) use_pred = 0;
3006 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3009 scale = mquant * 2 + v->halfpq;
3011 if (dc_pred_dir) //left
3014 ac_val -= 16 * s->block_wrap[n];
3016 q1 = s->current_picture.f.qscale_table[mb_pos];
3017 if (dc_pred_dir && c_avail && mb_pos)
3018 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3019 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3020 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3021 if ( dc_pred_dir && n == 1)
3023 if (!dc_pred_dir && n == 2)
3025 if (n == 3) q2 = q1;
3028 int last = 0, skip, value;
3032 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3036 if (v->fcm == PROGRESSIVE)
3037 block[v->zz_8x8[0][i++]] = value;
3039 if (use_pred && (v->fcm == ILACE_FRAME)) {
3040 if (!dc_pred_dir) // top
3041 block[v->zz_8x8[2][i++]] = value;
3043 block[v->zz_8x8[3][i++]] = value;
3045 block[v->zzi_8x8[i++]] = value;
3050 /* apply AC prediction if needed */
3052 /* scale predictors if needed*/
3053 if (q2 && q1 != q2) {
3054 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3055 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3058 return AVERROR_INVALIDDATA;
3059 if (dc_pred_dir) { // left
3060 for (k = 1; k < 8; k++)
3061 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3063 for (k = 1; k < 8; k++)
3064 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3067 if (dc_pred_dir) { // left
3068 for (k = 1; k < 8; k++)
3069 block[k << v->left_blk_sh] += ac_val[k];
3071 for (k = 1; k < 8; k++)
3072 block[k << v->top_blk_sh] += ac_val[k + 8];
3076 /* save AC coeffs for further prediction */
3077 for (k = 1; k < 8; k++) {
3078 ac_val2[k ] = block[k << v->left_blk_sh];
3079 ac_val2[k + 8] = block[k << v->top_blk_sh];
3082 /* scale AC coeffs */
3083 for (k = 1; k < 64; k++)
3087 block[k] += (block[k] < 0) ? -mquant : mquant;
3090 if (use_pred) i = 63;
3091 } else { // no AC coeffs
3094 memset(ac_val2, 0, 16 * 2);
3095 if (dc_pred_dir) { // left
3097 memcpy(ac_val2, ac_val, 8 * 2);
3098 if (q2 && q1 != q2) {
3099 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3100 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3102 return AVERROR_INVALIDDATA;
3103 for (k = 1; k < 8; k++)
3104 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3109 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3110 if (q2 && q1 != q2) {
3111 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3112 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3114 return AVERROR_INVALIDDATA;
3115 for (k = 1; k < 8; k++)
3116 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3121 /* apply AC prediction if needed */
3123 if (dc_pred_dir) { // left
3124 for (k = 1; k < 8; k++) {
3125 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3126 if (!v->pquantizer && block[k << v->left_blk_sh])
3127 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3130 for (k = 1; k < 8; k++) {
3131 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3132 if (!v->pquantizer && block[k << v->top_blk_sh])
3133 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3139 s->block_last_index[n] = i;
3146 static int vc1_decode_p_block(VC1Context *v, int16_t block[64], int n,
3147 int mquant, int ttmb, int first_block,
3148 uint8_t *dst, int linesize, int skip_block,
3151 MpegEncContext *s = &v->s;
3152 GetBitContext *gb = &s->gb;
3155 int scale, off, idx, last, skip, value;
3156 int ttblk = ttmb & 7;
3159 s->dsp.clear_block(block);
3162 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)];
3164 if (ttblk == TT_4X4) {
3165 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3167 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3168 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3169 || (!v->res_rtm_flag && !first_block))) {
3170 subblkpat = decode012(gb);
3172 subblkpat ^= 3; // swap decoded pattern bits
3173 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3175 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3178 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3180 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3181 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3182 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3185 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3186 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3195 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3200 idx = v->zz_8x8[0][i++];
3202 idx = v->zzi_8x8[i++];
3203 block[idx] = value * scale;
3205 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3209 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3211 v->vc1dsp.vc1_inv_trans_8x8(block);
3212 s->dsp.add_pixels_clamped(block, dst, linesize);
3217 pat = ~subblkpat & 0xF;
3218 for (j = 0; j < 4; j++) {
3219 last = subblkpat & (1 << (3 - j));
3221 off = (j & 1) * 4 + (j & 2) * 16;
3223 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3228 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3230 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3231 block[idx + off] = value * scale;
3233 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3235 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3237 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3239 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3244 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3245 for (j = 0; j < 2; j++) {
3246 last = subblkpat & (1 << (1 - j));
3250 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3255 idx = v->zz_8x4[i++] + off;
3257 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3258 block[idx] = value * scale;
3260 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3262 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3264 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3266 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3271 pat = ~(subblkpat * 5) & 0xF;
3272 for (j = 0; j < 2; j++) {
3273 last = subblkpat & (1 << (1 - j));
3277 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3282 idx = v->zz_4x8[i++] + off;
3284 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3285 block[idx] = value * scale;
3287 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3289 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3291 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3293 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3299 *ttmb_out |= ttblk << (n * 4);
3303 /** @} */ // Macroblock group
3305 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3306 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3308 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3310 MpegEncContext *s = &v->s;
3311 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3312 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3313 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3314 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3315 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3318 if (block_num > 3) {
3319 dst = s->dest[block_num - 3];
3321 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3323 if (s->mb_y != s->end_mb_y || block_num < 2) {
3327 if (block_num > 3) {
3328 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3329 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3330 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3331 mv_stride = s->mb_stride;
3333 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3334 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3335 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3336 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3337 mv_stride = s->b8_stride;
3338 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3341 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3342 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3343 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3345 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3347 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3350 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3352 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3357 dst -= 4 * linesize;
3358 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3359 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3360 idx = (block_cbp | (block_cbp >> 2)) & 3;
3362 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3365 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3367 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3372 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3374 MpegEncContext *s = &v->s;
3375 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3376 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3377 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3378 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3379 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3382 if (block_num > 3) {
3383 dst = s->dest[block_num - 3] - 8 * linesize;
3385 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3388 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3391 if (block_num > 3) {
3392 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3393 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3394 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3396 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3397 : (mb_cbp >> ((block_num + 1) * 4));
3398 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3399 : (mb_is_intra >> ((block_num + 1) * 4));
3400 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3402 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3403 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3405 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3407 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3410 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3412 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3418 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3419 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3420 idx = (block_cbp | (block_cbp >> 1)) & 5;
3422 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3425 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3427 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3432 static void vc1_apply_p_loop_filter(VC1Context *v)
3434 MpegEncContext *s = &v->s;
3437 for (i = 0; i < 6; i++) {
3438 vc1_apply_p_v_loop_filter(v, i);
3441 /* V always precedes H, therefore we run H one MB before V;
3442 * at the end of a row, we catch up to complete the row */
3444 for (i = 0; i < 6; i++) {
3445 vc1_apply_p_h_loop_filter(v, i);
3447 if (s->mb_x == s->mb_width - 1) {
3449 ff_update_block_index(s);
3450 for (i = 0; i < 6; i++) {
3451 vc1_apply_p_h_loop_filter(v, i);
3457 /** Decode one P-frame MB
3459 static int vc1_decode_p_mb(VC1Context *v)
3461 MpegEncContext *s = &v->s;
3462 GetBitContext *gb = &s->gb;
3464 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3465 int cbp; /* cbp decoding stuff */
3466 int mqdiff, mquant; /* MB quantization */
3467 int ttmb = v->ttfrm; /* MB Transform type */
3469 int mb_has_coeffs = 1; /* last_flag */
3470 int dmv_x, dmv_y; /* Differential MV components */
3471 int index, index1; /* LUT indexes */
3472 int val, sign; /* temp values */
3473 int first_block = 1;
3475 int skipped, fourmv;
3476 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3478 mquant = v->pq; /* lossy initialization */
3480 if (v->mv_type_is_raw)
3481 fourmv = get_bits1(gb);
3483 fourmv = v->mv_type_mb_plane[mb_pos];
3485 skipped = get_bits1(gb);
3487 skipped = v->s.mbskip_table[mb_pos];
3489 if (!fourmv) { /* 1MV mode */
3491 GET_MVDATA(dmv_x, dmv_y);
3494 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3495 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3497 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3498 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3500 /* FIXME Set DC val for inter block ? */
3501 if (s->mb_intra && !mb_has_coeffs) {
3503 s->ac_pred = get_bits1(gb);
3505 } else if (mb_has_coeffs) {
3507 s->ac_pred = get_bits1(gb);
3508 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3514 s->current_picture.f.qscale_table[mb_pos] = mquant;
3516 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3517 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3518 VC1_TTMB_VLC_BITS, 2);
3519 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3521 for (i = 0; i < 6; i++) {
3522 s->dc_val[0][s->block_index[i]] = 0;
3524 val = ((cbp >> (5 - i)) & 1);
3525 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3526 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3528 /* check if prediction blocks A and C are available */
3529 v->a_avail = v->c_avail = 0;
3530 if (i == 2 || i == 3 || !s->first_slice_line)
3531 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3532 if (i == 1 || i == 3 || s->mb_x)
3533 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3535 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3536 (i & 4) ? v->codingset2 : v->codingset);
3537 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3539 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3541 for (j = 0; j < 64; j++)
3542 s->block[i][j] <<= 1;
3543 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3544 if (v->pq >= 9 && v->overlap) {
3546 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3548 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3550 block_cbp |= 0xF << (i << 2);
3551 block_intra |= 1 << i;
3553 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3554 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3555 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3556 block_cbp |= pat << (i << 2);
3557 if (!v->ttmbf && ttmb < 8)
3564 for (i = 0; i < 6; i++) {
3565 v->mb_type[0][s->block_index[i]] = 0;
3566 s->dc_val[0][s->block_index[i]] = 0;
3568 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3569 s->current_picture.f.qscale_table[mb_pos] = 0;
3570 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3573 } else { // 4MV mode
3574 if (!skipped /* unskipped MB */) {
3575 int intra_count = 0, coded_inter = 0;
3576 int is_intra[6], is_coded[6];
3578 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3579 for (i = 0; i < 6; i++) {
3580 val = ((cbp >> (5 - i)) & 1);
3581 s->dc_val[0][s->block_index[i]] = 0;
3588 GET_MVDATA(dmv_x, dmv_y);
3590 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3592 vc1_mc_4mv_luma(v, i, 0);
3593 intra_count += s->mb_intra;
3594 is_intra[i] = s->mb_intra;
3595 is_coded[i] = mb_has_coeffs;
3598 is_intra[i] = (intra_count >= 3);
3602 vc1_mc_4mv_chroma(v, 0);
3603 v->mb_type[0][s->block_index[i]] = is_intra[i];
3605 coded_inter = !is_intra[i] & is_coded[i];
3607 // if there are no coded blocks then don't do anything more
3609 if (!intra_count && !coded_inter)
3612 s->current_picture.f.qscale_table[mb_pos] = mquant;
3613 /* test if block is intra and has pred */
3616 for (i = 0; i < 6; i++)
3618 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3619 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3625 s->ac_pred = get_bits1(gb);
3629 if (!v->ttmbf && coded_inter)
3630 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3631 for (i = 0; i < 6; i++) {
3633 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3634 s->mb_intra = is_intra[i];
3636 /* check if prediction blocks A and C are available */
3637 v->a_avail = v->c_avail = 0;
3638 if (i == 2 || i == 3 || !s->first_slice_line)
3639 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3640 if (i == 1 || i == 3 || s->mb_x)
3641 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3643 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3644 (i & 4) ? v->codingset2 : v->codingset);
3645 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3647 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3649 for (j = 0; j < 64; j++)
3650 s->block[i][j] <<= 1;
3651 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3652 (i & 4) ? s->uvlinesize : s->linesize);
3653 if (v->pq >= 9 && v->overlap) {
3655 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3657 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3659 block_cbp |= 0xF << (i << 2);
3660 block_intra |= 1 << i;
3661 } else if (is_coded[i]) {
3662 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3663 first_block, s->dest[dst_idx] + off,
3664 (i & 4) ? s->uvlinesize : s->linesize,
3665 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3667 block_cbp |= pat << (i << 2);
3668 if (!v->ttmbf && ttmb < 8)
3673 } else { // skipped MB
3675 s->current_picture.f.qscale_table[mb_pos] = 0;
3676 for (i = 0; i < 6; i++) {
3677 v->mb_type[0][s->block_index[i]] = 0;
3678 s->dc_val[0][s->block_index[i]] = 0;
3680 for (i = 0; i < 4; i++) {
3681 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3682 vc1_mc_4mv_luma(v, i, 0);
3684 vc1_mc_4mv_chroma(v, 0);
3685 s->current_picture.f.qscale_table[mb_pos] = 0;
3689 v->cbp[s->mb_x] = block_cbp;
3690 v->ttblk[s->mb_x] = block_tt;
3691 v->is_intra[s->mb_x] = block_intra;
3696 /* Decode one macroblock in an interlaced frame p picture */
3698 static int vc1_decode_p_mb_intfr(VC1Context *v)
3700 MpegEncContext *s = &v->s;
3701 GetBitContext *gb = &s->gb;
3703 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3704 int cbp = 0; /* cbp decoding stuff */
3705 int mqdiff, mquant; /* MB quantization */
3706 int ttmb = v->ttfrm; /* MB Transform type */
3708 int mb_has_coeffs = 1; /* last_flag */
3709 int dmv_x, dmv_y; /* Differential MV components */
3710 int val; /* temp value */
3711 int first_block = 1;
3713 int skipped, fourmv = 0, twomv = 0;
3714 int block_cbp = 0, pat, block_tt = 0;
3715 int idx_mbmode = 0, mvbp;
3716 int stride_y, fieldtx;
3718 mquant = v->pq; /* Lossy initialization */
3721 skipped = get_bits1(gb);
3723 skipped = v->s.mbskip_table[mb_pos];
3725 if (v->fourmvswitch)
3726 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3728 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3729 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3730 /* store the motion vector type in a flag (useful later) */
3731 case MV_PMODE_INTFR_4MV:
3733 v->blk_mv_type[s->block_index[0]] = 0;
3734 v->blk_mv_type[s->block_index[1]] = 0;
3735 v->blk_mv_type[s->block_index[2]] = 0;
3736 v->blk_mv_type[s->block_index[3]] = 0;
3738 case MV_PMODE_INTFR_4MV_FIELD:
3740 v->blk_mv_type[s->block_index[0]] = 1;
3741 v->blk_mv_type[s->block_index[1]] = 1;
3742 v->blk_mv_type[s->block_index[2]] = 1;
3743 v->blk_mv_type[s->block_index[3]] = 1;
3745 case MV_PMODE_INTFR_2MV_FIELD:
3747 v->blk_mv_type[s->block_index[0]] = 1;
3748 v->blk_mv_type[s->block_index[1]] = 1;
3749 v->blk_mv_type[s->block_index[2]] = 1;
3750 v->blk_mv_type[s->block_index[3]] = 1;
3752 case MV_PMODE_INTFR_1MV:
3753 v->blk_mv_type[s->block_index[0]] = 0;
3754 v->blk_mv_type[s->block_index[1]] = 0;
3755 v->blk_mv_type[s->block_index[2]] = 0;
3756 v->blk_mv_type[s->block_index[3]] = 0;
3759 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3760 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3761 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3762 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3763 s->mb_intra = v->is_intra[s->mb_x] = 1;
3764 for (i = 0; i < 6; i++)
3765 v->mb_type[0][s->block_index[i]] = 1;
3766 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3767 mb_has_coeffs = get_bits1(gb);
3769 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3770 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3772 s->current_picture.f.qscale_table[mb_pos] = mquant;
3773 /* Set DC scale - y and c use the same (not sure if necessary here) */
3774 s->y_dc_scale = s->y_dc_scale_table[mquant];
3775 s->c_dc_scale = s->c_dc_scale_table[mquant];
3777 for (i = 0; i < 6; i++) {
3778 s->dc_val[0][s->block_index[i]] = 0;
3780 val = ((cbp >> (5 - i)) & 1);
3781 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3782 v->a_avail = v->c_avail = 0;
3783 if (i == 2 || i == 3 || !s->first_slice_line)
3784 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3785 if (i == 1 || i == 3 || s->mb_x)
3786 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3788 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3789 (i & 4) ? v->codingset2 : v->codingset);
3790 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3791 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3793 stride_y = s->linesize << fieldtx;
3794 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3796 stride_y = s->uvlinesize;
3799 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3803 } else { // inter MB
3804 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3806 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3807 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3808 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3810 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3811 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3812 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3815 s->mb_intra = v->is_intra[s->mb_x] = 0;
3816 for (i = 0; i < 6; i++)
3817 v->mb_type[0][s->block_index[i]] = 0;
3818 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3819 /* for all motion vector read MVDATA and motion compensate each block */
3823 for (i = 0; i < 6; i++) {
3826 val = ((mvbp >> (3 - i)) & 1);
3828 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3830 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3831 vc1_mc_4mv_luma(v, i, 0);
3832 } else if (i == 4) {
3833 vc1_mc_4mv_chroma4(v);
3840 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3842 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3843 vc1_mc_4mv_luma(v, 0, 0);
3844 vc1_mc_4mv_luma(v, 1, 0);
3847 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3849 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3850 vc1_mc_4mv_luma(v, 2, 0);
3851 vc1_mc_4mv_luma(v, 3, 0);
3852 vc1_mc_4mv_chroma4(v);
3854 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3857 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3859 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3863 GET_MQUANT(); // p. 227
3864 s->current_picture.f.qscale_table[mb_pos] = mquant;
3865 if (!v->ttmbf && cbp)
3866 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3867 for (i = 0; i < 6; i++) {
3868 s->dc_val[0][s->block_index[i]] = 0;
3870 val = ((cbp >> (5 - i)) & 1);
3872 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3874 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3876 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3877 first_block, s->dest[dst_idx] + off,
3878 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3879 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3880 block_cbp |= pat << (i << 2);
3881 if (!v->ttmbf && ttmb < 8)
3888 s->mb_intra = v->is_intra[s->mb_x] = 0;
3889 for (i = 0; i < 6; i++) {
3890 v->mb_type[0][s->block_index[i]] = 0;
3891 s->dc_val[0][s->block_index[i]] = 0;
3893 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3894 s->current_picture.f.qscale_table[mb_pos] = 0;
3895 v->blk_mv_type[s->block_index[0]] = 0;
3896 v->blk_mv_type[s->block_index[1]] = 0;
3897 v->blk_mv_type[s->block_index[2]] = 0;
3898 v->blk_mv_type[s->block_index[3]] = 0;
3899 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3902 if (s->mb_x == s->mb_width - 1)
3903 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3907 static int vc1_decode_p_mb_intfi(VC1Context *v)
3909 MpegEncContext *s = &v->s;
3910 GetBitContext *gb = &s->gb;
3912 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3913 int cbp = 0; /* cbp decoding stuff */
3914 int mqdiff, mquant; /* MB quantization */
3915 int ttmb = v->ttfrm; /* MB Transform type */
3917 int mb_has_coeffs = 1; /* last_flag */
3918 int dmv_x, dmv_y; /* Differential MV components */
3919 int val; /* temp values */
3920 int first_block = 1;
3923 int block_cbp = 0, pat, block_tt = 0;
3926 mquant = v->pq; /* Lossy initialization */
3928 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3929 if (idx_mbmode <= 1) { // intra MB
3930 s->mb_intra = v->is_intra[s->mb_x] = 1;
3931 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3932 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3933 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3935 s->current_picture.f.qscale_table[mb_pos] = mquant;
3936 /* Set DC scale - y and c use the same (not sure if necessary here) */
3937 s->y_dc_scale = s->y_dc_scale_table[mquant];
3938 s->c_dc_scale = s->c_dc_scale_table[mquant];
3939 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3940 mb_has_coeffs = idx_mbmode & 1;
3942 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3944 for (i = 0; i < 6; i++) {
3945 s->dc_val[0][s->block_index[i]] = 0;
3946 v->mb_type[0][s->block_index[i]] = 1;
3948 val = ((cbp >> (5 - i)) & 1);
3949 v->a_avail = v->c_avail = 0;
3950 if (i == 2 || i == 3 || !s->first_slice_line)
3951 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3952 if (i == 1 || i == 3 || s->mb_x)
3953 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3955 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3956 (i & 4) ? v->codingset2 : v->codingset);
3957 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3959 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3960 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3961 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3962 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3963 // TODO: loop filter
3966 s->mb_intra = v->is_intra[s->mb_x] = 0;
3967 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3968 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3969 if (idx_mbmode <= 5) { // 1-MV
3970 dmv_x = dmv_y = pred_flag = 0;
3971 if (idx_mbmode & 1) {
3972 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3974 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3976 mb_has_coeffs = !(idx_mbmode & 2);
3978 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3979 for (i = 0; i < 6; i++) {
3981 dmv_x = dmv_y = pred_flag = 0;
3982 val = ((v->fourmvbp >> (3 - i)) & 1);
3984 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3986 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3987 vc1_mc_4mv_luma(v, i, 0);
3989 vc1_mc_4mv_chroma(v, 0);
3991 mb_has_coeffs = idx_mbmode & 1;
3994 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3998 s->current_picture.f.qscale_table[mb_pos] = mquant;
3999 if (!v->ttmbf && cbp) {
4000 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4003 for (i = 0; i < 6; i++) {
4004 s->dc_val[0][s->block_index[i]] = 0;
4006 val = ((cbp >> (5 - i)) & 1);
4007 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4008 if (v->second_field)
4009 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4011 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4012 first_block, s->dest[dst_idx] + off,
4013 (i & 4) ? s->uvlinesize : s->linesize,
4014 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4016 block_cbp |= pat << (i << 2);
4017 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4022 if (s->mb_x == s->mb_width - 1)
4023 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4027 /** Decode one B-frame MB (in Main profile)
4029 static void vc1_decode_b_mb(VC1Context *v)
4031 MpegEncContext *s = &v->s;
4032 GetBitContext *gb = &s->gb;
4034 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4035 int cbp = 0; /* cbp decoding stuff */
4036 int mqdiff, mquant; /* MB quantization */
4037 int ttmb = v->ttfrm; /* MB Transform type */
4038 int mb_has_coeffs = 0; /* last_flag */
4039 int index, index1; /* LUT indexes */
4040 int val, sign; /* temp values */
4041 int first_block = 1;
4043 int skipped, direct;
4044 int dmv_x[2], dmv_y[2];
4045 int bmvtype = BMV_TYPE_BACKWARD;
4047 mquant = v->pq; /* lossy initialization */
4051 direct = get_bits1(gb);
4053 direct = v->direct_mb_plane[mb_pos];
4055 skipped = get_bits1(gb);
4057 skipped = v->s.mbskip_table[mb_pos];
4059 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4060 for (i = 0; i < 6; i++) {
4061 v->mb_type[0][s->block_index[i]] = 0;
4062 s->dc_val[0][s->block_index[i]] = 0;
4064 s->current_picture.f.qscale_table[mb_pos] = 0;
4068 GET_MVDATA(dmv_x[0], dmv_y[0]);
4069 dmv_x[1] = dmv_x[0];
4070 dmv_y[1] = dmv_y[0];
4072 if (skipped || !s->mb_intra) {
4073 bmvtype = decode012(gb);
4076 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4079 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4082 bmvtype = BMV_TYPE_INTERPOLATED;
4083 dmv_x[0] = dmv_y[0] = 0;
4087 for (i = 0; i < 6; i++)
4088 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4092 bmvtype = BMV_TYPE_INTERPOLATED;
4093 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4094 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4098 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4101 s->current_picture.f.qscale_table[mb_pos] = mquant;
4103 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4104 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4105 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4106 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4108 if (!mb_has_coeffs && !s->mb_intra) {
4109 /* no coded blocks - effectively skipped */
4110 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4111 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4114 if (s->mb_intra && !mb_has_coeffs) {
4116 s->current_picture.f.qscale_table[mb_pos] = mquant;
4117 s->ac_pred = get_bits1(gb);
4119 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4121 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4122 GET_MVDATA(dmv_x[0], dmv_y[0]);
4123 if (!mb_has_coeffs) {
4124 /* interpolated skipped block */
4125 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4126 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4130 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4132 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4135 s->ac_pred = get_bits1(gb);
4136 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4138 s->current_picture.f.qscale_table[mb_pos] = mquant;
4139 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4140 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4144 for (i = 0; i < 6; i++) {
4145 s->dc_val[0][s->block_index[i]] = 0;
4147 val = ((cbp >> (5 - i)) & 1);
4148 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4149 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4151 /* check if prediction blocks A and C are available */
4152 v->a_avail = v->c_avail = 0;
4153 if (i == 2 || i == 3 || !s->first_slice_line)
4154 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4155 if (i == 1 || i == 3 || s->mb_x)
4156 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4158 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4159 (i & 4) ? v->codingset2 : v->codingset);
4160 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4162 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4164 for (j = 0; j < 64; j++)
4165 s->block[i][j] <<= 1;
4166 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4168 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4169 first_block, s->dest[dst_idx] + off,
4170 (i & 4) ? s->uvlinesize : s->linesize,
4171 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4172 if (!v->ttmbf && ttmb < 8)
4179 /** Decode one B-frame MB (in interlaced field B picture)
4181 static void vc1_decode_b_mb_intfi(VC1Context *v)
4183 MpegEncContext *s = &v->s;
4184 GetBitContext *gb = &s->gb;
4186 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4187 int cbp = 0; /* cbp decoding stuff */
4188 int mqdiff, mquant; /* MB quantization */
4189 int ttmb = v->ttfrm; /* MB Transform type */
4190 int mb_has_coeffs = 0; /* last_flag */
4191 int val; /* temp value */
4192 int first_block = 1;
4195 int dmv_x[2], dmv_y[2], pred_flag[2];
4196 int bmvtype = BMV_TYPE_BACKWARD;
4197 int idx_mbmode, interpmvp;
4199 mquant = v->pq; /* Lossy initialization */
4202 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4203 if (idx_mbmode <= 1) { // intra MB
4204 s->mb_intra = v->is_intra[s->mb_x] = 1;
4205 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4206 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4207 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4209 s->current_picture.f.qscale_table[mb_pos] = mquant;
4210 /* Set DC scale - y and c use the same (not sure if necessary here) */
4211 s->y_dc_scale = s->y_dc_scale_table[mquant];
4212 s->c_dc_scale = s->c_dc_scale_table[mquant];
4213 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4214 mb_has_coeffs = idx_mbmode & 1;
4216 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4218 for (i = 0; i < 6; i++) {
4219 s->dc_val[0][s->block_index[i]] = 0;
4221 val = ((cbp >> (5 - i)) & 1);
4222 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4223 v->a_avail = v->c_avail = 0;
4224 if (i == 2 || i == 3 || !s->first_slice_line)
4225 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4226 if (i == 1 || i == 3 || s->mb_x)
4227 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4229 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4230 (i & 4) ? v->codingset2 : v->codingset);
4231 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4233 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4235 for (j = 0; j < 64; j++)
4236 s->block[i][j] <<= 1;
4237 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4238 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4239 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4240 // TODO: yet to perform loop filter
4243 s->mb_intra = v->is_intra[s->mb_x] = 0;
4244 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4245 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4247 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4249 fwd = v->forward_mb_plane[mb_pos];
4250 if (idx_mbmode <= 5) { // 1-MV
4251 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4252 pred_flag[0] = pred_flag[1] = 0;
4254 bmvtype = BMV_TYPE_FORWARD;
4256 bmvtype = decode012(gb);
4259 bmvtype = BMV_TYPE_BACKWARD;
4262 bmvtype = BMV_TYPE_DIRECT;
4265 bmvtype = BMV_TYPE_INTERPOLATED;
4266 interpmvp = get_bits1(gb);
4269 v->bmvtype = bmvtype;
4270 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4271 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4273 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4274 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4276 if (bmvtype == BMV_TYPE_DIRECT) {
4277 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4278 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4280 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4281 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4282 mb_has_coeffs = !(idx_mbmode & 2);
4285 bmvtype = BMV_TYPE_FORWARD;
4286 v->bmvtype = bmvtype;
4287 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4288 for (i = 0; i < 6; i++) {
4290 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4291 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4292 val = ((v->fourmvbp >> (3 - i)) & 1);
4294 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4295 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4296 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4298 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4299 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4301 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4303 mb_has_coeffs = idx_mbmode & 1;
4306 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4310 s->current_picture.f.qscale_table[mb_pos] = mquant;
4311 if (!v->ttmbf && cbp) {
4312 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4315 for (i = 0; i < 6; i++) {
4316 s->dc_val[0][s->block_index[i]] = 0;
4318 val = ((cbp >> (5 - i)) & 1);
4319 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4320 if (v->second_field)
4321 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4323 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4324 first_block, s->dest[dst_idx] + off,
4325 (i & 4) ? s->uvlinesize : s->linesize,
4326 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4327 if (!v->ttmbf && ttmb < 8)
4335 /** Decode blocks of I-frame
4337 static void vc1_decode_i_blocks(VC1Context *v)
4340 MpegEncContext *s = &v->s;
4345 /* select codingmode used for VLC tables selection */
4346 switch (v->y_ac_table_index) {
4348 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4351 v->codingset = CS_HIGH_MOT_INTRA;
4354 v->codingset = CS_MID_RATE_INTRA;
4358 switch (v->c_ac_table_index) {
4360 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4363 v->codingset2 = CS_HIGH_MOT_INTER;
4366 v->codingset2 = CS_MID_RATE_INTER;
4370 /* Set DC scale - y and c use the same */
4371 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4372 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4375 s->mb_x = s->mb_y = 0;
4377 s->first_slice_line = 1;
4378 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4380 ff_init_block_index(s);
4381 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4383 ff_update_block_index(s);
4384 dst[0] = s->dest[0];
4385 dst[1] = dst[0] + 8;
4386 dst[2] = s->dest[0] + s->linesize * 8;
4387 dst[3] = dst[2] + 8;
4388 dst[4] = s->dest[1];
4389 dst[5] = s->dest[2];
4390 s->dsp.clear_blocks(s->block[0]);
4391 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4392 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4393 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4394 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4395 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4397 // do actual MB decoding and displaying
4398 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4399 v->s.ac_pred = get_bits1(&v->s.gb);
4401 for (k = 0; k < 6; k++) {
4402 val = ((cbp >> (5 - k)) & 1);
4405 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4409 cbp |= val << (5 - k);
4411 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4413 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4415 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4416 if (v->pq >= 9 && v->overlap) {
4418 for (j = 0; j < 64; j++)
4419 s->block[k][j] <<= 1;
4420 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4423 for (j = 0; j < 64; j++)
4424 s->block[k][j] = (s->block[k][j] - 64) << 1;
4425 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4429 if (v->pq >= 9 && v->overlap) {
4431 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4432 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4433 if (!(s->flags & CODEC_FLAG_GRAY)) {
4434 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4435 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4438 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4439 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4440 if (!s->first_slice_line) {
4441 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4442 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4443 if (!(s->flags & CODEC_FLAG_GRAY)) {
4444 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4445 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4448 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4449 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4451 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4453 if (get_bits_count(&s->gb) > v->bits) {
4454 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4455 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4456 get_bits_count(&s->gb), v->bits);
4460 if (!v->s.loop_filter)
4461 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4463 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4465 s->first_slice_line = 0;
4467 if (v->s.loop_filter)
4468 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4470 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4471 * profile, these only differ are when decoding MSS2 rectangles. */
4472 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4475 /** Decode blocks of I-frame for advanced profile
4477 static void vc1_decode_i_blocks_adv(VC1Context *v)
4480 MpegEncContext *s = &v->s;
4486 GetBitContext *gb = &s->gb;
4488 /* select codingmode used for VLC tables selection */
4489 switch (v->y_ac_table_index) {
4491 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4494 v->codingset = CS_HIGH_MOT_INTRA;
4497 v->codingset = CS_MID_RATE_INTRA;
4501 switch (v->c_ac_table_index) {
4503 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4506 v->codingset2 = CS_HIGH_MOT_INTER;
4509 v->codingset2 = CS_MID_RATE_INTER;
4514 s->mb_x = s->mb_y = 0;
4516 s->first_slice_line = 1;
4517 s->mb_y = s->start_mb_y;
4518 if (s->start_mb_y) {
4520 ff_init_block_index(s);
4521 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4522 (1 + s->b8_stride) * sizeof(*s->coded_block));
4524 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4526 ff_init_block_index(s);
4527 for (;s->mb_x < s->mb_width; s->mb_x++) {
4528 int16_t (*block)[64] = v->block[v->cur_blk_idx];
4529 ff_update_block_index(s);
4530 s->dsp.clear_blocks(block[0]);
4531 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4532 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4533 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4534 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4536 // do actual MB decoding and displaying
4537 if (v->fieldtx_is_raw)
4538 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4539 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4540 if ( v->acpred_is_raw)
4541 v->s.ac_pred = get_bits1(&v->s.gb);
4543 v->s.ac_pred = v->acpred_plane[mb_pos];
4545 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4546 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4550 s->current_picture.f.qscale_table[mb_pos] = mquant;
4551 /* Set DC scale - y and c use the same */
4552 s->y_dc_scale = s->y_dc_scale_table[mquant];
4553 s->c_dc_scale = s->c_dc_scale_table[mquant];
4555 for (k = 0; k < 6; k++) {
4556 val = ((cbp >> (5 - k)) & 1);
4559 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4563 cbp |= val << (5 - k);
4565 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4566 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4568 vc1_decode_i_block_adv(v, block[k], k, val,
4569 (k < 4) ? v->codingset : v->codingset2, mquant);
4571 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4573 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4576 vc1_smooth_overlap_filter_iblk(v);
4577 vc1_put_signed_blocks_clamped(v);
4578 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4580 if (get_bits_count(&s->gb) > v->bits) {
4581 // TODO: may need modification to handle slice coding
4582 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4583 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4584 get_bits_count(&s->gb), v->bits);
4588 if (!v->s.loop_filter)
4589 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4591 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4592 s->first_slice_line = 0;
4595 /* raw bottom MB row */
4597 ff_init_block_index(s);
4598 for (;s->mb_x < s->mb_width; s->mb_x++) {
4599 ff_update_block_index(s);
4600 vc1_put_signed_blocks_clamped(v);
4601 if (v->s.loop_filter)
4602 vc1_loop_filter_iblk_delayed(v, v->pq);
4604 if (v->s.loop_filter)
4605 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4606 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4607 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4610 static void vc1_decode_p_blocks(VC1Context *v)
4612 MpegEncContext *s = &v->s;
4613 int apply_loop_filter;
4615 /* select codingmode used for VLC tables selection */
4616 switch (v->c_ac_table_index) {
4618 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4621 v->codingset = CS_HIGH_MOT_INTRA;
4624 v->codingset = CS_MID_RATE_INTRA;
4628 switch (v->c_ac_table_index) {
4630 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4633 v->codingset2 = CS_HIGH_MOT_INTER;
4636 v->codingset2 = CS_MID_RATE_INTER;
4640 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4641 s->first_slice_line = 1;
4642 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4643 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4645 ff_init_block_index(s);
4646 for (; s->mb_x < s->mb_width; s->mb_x++) {
4647 ff_update_block_index(s);
4649 if (v->fcm == ILACE_FIELD)
4650 vc1_decode_p_mb_intfi(v);
4651 else if (v->fcm == ILACE_FRAME)
4652 vc1_decode_p_mb_intfr(v);
4653 else vc1_decode_p_mb(v);
4654 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4655 vc1_apply_p_loop_filter(v);
4656 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4657 // TODO: may need modification to handle slice coding
4658 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4659 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4660 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4664 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4665 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4666 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4667 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4668 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4669 s->first_slice_line = 0;
4671 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4673 ff_init_block_index(s);
4674 for (; s->mb_x < s->mb_width; s->mb_x++) {
4675 ff_update_block_index(s);
4676 vc1_apply_p_loop_filter(v);
4679 if (s->end_mb_y >= s->start_mb_y)
4680 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4681 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4682 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4685 static void vc1_decode_b_blocks(VC1Context *v)
4687 MpegEncContext *s = &v->s;
4689 /* select codingmode used for VLC tables selection */
4690 switch (v->c_ac_table_index) {
4692 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4695 v->codingset = CS_HIGH_MOT_INTRA;
4698 v->codingset = CS_MID_RATE_INTRA;
4702 switch (v->c_ac_table_index) {
4704 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4707 v->codingset2 = CS_HIGH_MOT_INTER;
4710 v->codingset2 = CS_MID_RATE_INTER;
4714 s->first_slice_line = 1;
4715 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4717 ff_init_block_index(s);
4718 for (; s->mb_x < s->mb_width; s->mb_x++) {
4719 ff_update_block_index(s);
4721 if (v->fcm == ILACE_FIELD)
4722 vc1_decode_b_mb_intfi(v);
4725 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4726 // TODO: may need modification to handle slice coding
4727 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4728 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4729 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4732 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4734 if (!v->s.loop_filter)
4735 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4737 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4738 s->first_slice_line = 0;
4740 if (v->s.loop_filter)
4741 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4742 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4743 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4746 static void vc1_decode_skip_blocks(VC1Context *v)
4748 MpegEncContext *s = &v->s;
4750 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4751 s->first_slice_line = 1;
4752 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4754 ff_init_block_index(s);
4755 ff_update_block_index(s);
4756 if (s->last_picture.f.data[0]) {
4757 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4758 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4759 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4761 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4762 s->first_slice_line = 0;
4764 s->pict_type = AV_PICTURE_TYPE_P;
4767 void ff_vc1_decode_blocks(VC1Context *v)
4770 v->s.esc3_level_length = 0;
4772 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4775 v->left_blk_idx = -1;
4776 v->topleft_blk_idx = 1;
4778 switch (v->s.pict_type) {
4779 case AV_PICTURE_TYPE_I:
4780 if (v->profile == PROFILE_ADVANCED)
4781 vc1_decode_i_blocks_adv(v);
4783 vc1_decode_i_blocks(v);
4785 case AV_PICTURE_TYPE_P:
4786 if (v->p_frame_skipped)
4787 vc1_decode_skip_blocks(v);
4789 vc1_decode_p_blocks(v);
4791 case AV_PICTURE_TYPE_B:
4793 if (v->profile == PROFILE_ADVANCED)
4794 vc1_decode_i_blocks_adv(v);
4796 vc1_decode_i_blocks(v);
4798 vc1_decode_b_blocks(v);
4804 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4808 * Transform coefficients for both sprites in 16.16 fixed point format,
4809 * in the order they appear in the bitstream:
4811 * rotation 1 (unused)
4813 * rotation 2 (unused)
4820 int effect_type, effect_flag;
4821 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4822 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4825 static inline int get_fp_val(GetBitContext* gb)
4827 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4830 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4834 switch (get_bits(gb, 2)) {
4837 c[2] = get_fp_val(gb);
4841 c[0] = c[4] = get_fp_val(gb);
4842 c[2] = get_fp_val(gb);
4845 c[0] = get_fp_val(gb);
4846 c[2] = get_fp_val(gb);
4847 c[4] = get_fp_val(gb);
4850 c[0] = get_fp_val(gb);
4851 c[1] = get_fp_val(gb);
4852 c[2] = get_fp_val(gb);
4853 c[3] = get_fp_val(gb);
4854 c[4] = get_fp_val(gb);
4857 c[5] = get_fp_val(gb);
4859 c[6] = get_fp_val(gb);
4864 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4866 AVCodecContext *avctx = v->s.avctx;
4869 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4870 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4871 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4872 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4873 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4874 for (i = 0; i < 7; i++)
4875 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4876 sd->coefs[sprite][i] / (1<<16),
4877 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4878 av_log(avctx, AV_LOG_DEBUG, "\n");
4882 if (sd->effect_type = get_bits_long(gb, 30)) {
4883 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4885 vc1_sprite_parse_transform(gb, sd->effect_params1);
4888 vc1_sprite_parse_transform(gb, sd->effect_params1);
4889 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4892 for (i = 0; i < sd->effect_pcount1; i++)
4893 sd->effect_params1[i] = get_fp_val(gb);
4895 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4896 // effect 13 is simple alpha blending and matches the opacity above
4897 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4898 for (i = 0; i < sd->effect_pcount1; i++)
4899 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4900 sd->effect_params1[i] / (1 << 16),
4901 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4902 av_log(avctx, AV_LOG_DEBUG, "\n");
4905 sd->effect_pcount2 = get_bits(gb, 16);
4906 if (sd->effect_pcount2 > 10) {
4907 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4909 } else if (sd->effect_pcount2) {
4911 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4912 while (++i < sd->effect_pcount2) {
4913 sd->effect_params2[i] = get_fp_val(gb);
4914 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4915 sd->effect_params2[i] / (1 << 16),
4916 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4918 av_log(avctx, AV_LOG_DEBUG, "\n");
4921 if (sd->effect_flag = get_bits1(gb))
4922 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4924 if (get_bits_count(gb) >= gb->size_in_bits +
4925 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4926 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4927 if (get_bits_count(gb) < gb->size_in_bits - 8)
4928 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4931 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4933 int i, plane, row, sprite;
4934 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4935 uint8_t* src_h[2][2];
4936 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4938 MpegEncContext *s = &v->s;
4940 for (i = 0; i < 2; i++) {
4941 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4942 xadv[i] = sd->coefs[i][0];
4943 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4944 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4946 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4947 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4949 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4951 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4952 int width = v->output_width>>!!plane;
4954 for (row = 0; row < v->output_height>>!!plane; row++) {
4955 uint8_t *dst = v->sprite_output_frame.data[plane] +
4956 v->sprite_output_frame.linesize[plane] * row;
4958 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4959 uint8_t *iplane = s->current_picture.f.data[plane];
4960 int iline = s->current_picture.f.linesize[plane];
4961 int ycoord = yoff[sprite] + yadv[sprite] * row;
4962 int yline = ycoord >> 16;
4964 ysub[sprite] = ycoord & 0xFFFF;
4966 iplane = s->last_picture.f.data[plane];
4967 iline = s->last_picture.f.linesize[plane];
4969 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4970 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4971 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4973 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4975 if (sr_cache[sprite][0] != yline) {
4976 if (sr_cache[sprite][1] == yline) {
4977 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4978 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4980 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4981 sr_cache[sprite][0] = yline;
4984 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4985 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4986 iplane + next_line, xoff[sprite],
4987 xadv[sprite], width);
4988 sr_cache[sprite][1] = yline + 1;
4990 src_h[sprite][0] = v->sr_rows[sprite][0];
4991 src_h[sprite][1] = v->sr_rows[sprite][1];
4995 if (!v->two_sprites) {
4997 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4999 memcpy(dst, src_h[0][0], width);
5002 if (ysub[0] && ysub[1]) {
5003 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5004 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5005 } else if (ysub[0]) {
5006 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5007 src_h[1][0], alpha, width);
5008 } else if (ysub[1]) {
5009 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5010 src_h[0][0], (1<<16)-1-alpha, width);
5012 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5018 for (i = 0; i < 2; i++) {
5028 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5030 MpegEncContext *s = &v->s;
5031 AVCodecContext *avctx = s->avctx;
5034 vc1_parse_sprites(v, gb, &sd);
5036 if (!s->current_picture.f.data[0]) {
5037 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5041 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5042 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5046 if (v->sprite_output_frame.data[0])
5047 avctx->release_buffer(avctx, &v->sprite_output_frame);
5049 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5050 v->sprite_output_frame.reference = 0;
5051 if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
5052 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5056 vc1_draw_sprites(v, &sd);
5061 static void vc1_sprite_flush(AVCodecContext *avctx)
5063 VC1Context *v = avctx->priv_data;
5064 MpegEncContext *s = &v->s;
5065 AVFrame *f = &s->current_picture.f;
5068 /* Windows Media Image codecs have a convergence interval of two keyframes.
5069 Since we can't enforce it, clear to black the missing sprite. This is
5070 wrong but it looks better than doing nothing. */
5073 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5074 for (i = 0; i < v->sprite_height>>!!plane; i++)
5075 memset(f->data[plane] + i * f->linesize[plane],
5076 plane ? 128 : 0, f->linesize[plane]);
5081 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5083 MpegEncContext *s = &v->s;
5086 /* Allocate mb bitplanes */
5087 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5088 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5089 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5090 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5091 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5092 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5094 v->n_allocated_blks = s->mb_width + 2;
5095 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5096 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5097 v->cbp = v->cbp_base + s->mb_stride;
5098 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5099 v->ttblk = v->ttblk_base + s->mb_stride;
5100 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5101 v->is_intra = v->is_intra_base + s->mb_stride;
5102 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5103 v->luma_mv = v->luma_mv_base + s->mb_stride;
5105 /* allocate block type info in that way so it could be used with s->block_index[] */
5106 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5107 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5108 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5109 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5111 /* allocate memory to store block level MV info */
5112 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5113 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5114 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5115 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5116 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5117 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5118 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5119 v->mv_f_last[1] = v->mv_f_last[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5120 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5121 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5122 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);
5124 /* Init coded blocks info */
5125 if (v->profile == PROFILE_ADVANCED) {
5126 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5128 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5132 ff_intrax8_common_init(&v->x8,s);
5134 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5135 for (i = 0; i < 4; i++)
5136 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5139 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5140 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5147 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5150 for (i = 0; i < 64; i++) {
5151 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5152 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5153 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5154 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5155 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5156 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5162 /** Initialize a VC1/WMV3 decoder
5163 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5164 * @todo TODO: Decypher remaining bits in extra_data
5166 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5168 VC1Context *v = avctx->priv_data;
5169 MpegEncContext *s = &v->s;
5172 /* save the container output size for WMImage */
5173 v->output_width = avctx->width;
5174 v->output_height = avctx->height;
5176 if (!avctx->extradata_size || !avctx->extradata)
5178 if (!(avctx->flags & CODEC_FLAG_GRAY))
5179 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5181 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5182 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5184 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5185 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5187 if (ff_vc1_init_common(v) < 0)
5189 // ensure static VLC tables are initialized
5190 if (ff_msmpeg4_decode_init(avctx) < 0)
5192 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5194 // Hack to ensure the above functions will be called
5195 // again once we know all necessary settings.
5196 // That this is necessary might indicate a bug.
5197 ff_vc1_decode_end(avctx);
5198 ff_vc1dsp_init(&v->vc1dsp);
5200 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5203 // looks like WMV3 has a sequence header stored in the extradata
5204 // advanced sequence header may be before the first frame
5205 // the last byte of the extradata is a version number, 1 for the
5206 // samples we can decode
5208 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5210 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5213 count = avctx->extradata_size*8 - get_bits_count(&gb);
5215 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5216 count, get_bits(&gb, count));
5217 } else if (count < 0) {
5218 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5220 } else { // VC1/WVC1/WVP2
5221 const uint8_t *start = avctx->extradata;
5222 uint8_t *end = avctx->extradata + avctx->extradata_size;
5223 const uint8_t *next;
5224 int size, buf2_size;
5225 uint8_t *buf2 = NULL;
5226 int seq_initialized = 0, ep_initialized = 0;
5228 if (avctx->extradata_size < 16) {
5229 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5233 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5234 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5236 for (; next < end; start = next) {
5237 next = find_next_marker(start + 4, end);
5238 size = next - start - 4;
5241 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5242 init_get_bits(&gb, buf2, buf2_size * 8);
5243 switch (AV_RB32(start)) {
5244 case VC1_CODE_SEQHDR:
5245 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5249 seq_initialized = 1;
5251 case VC1_CODE_ENTRYPOINT:
5252 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5261 if (!seq_initialized || !ep_initialized) {
5262 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5265 v->res_sprite = (avctx->codec_id == AV_CODEC_ID_VC1IMAGE);
5268 avctx->profile = v->profile;
5269 if (v->profile == PROFILE_ADVANCED)
5270 avctx->level = v->level;
5272 avctx->has_b_frames = !!avctx->max_b_frames;
5274 s->mb_width = (avctx->coded_width + 15) >> 4;
5275 s->mb_height = (avctx->coded_height + 15) >> 4;
5277 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5278 ff_vc1_init_transposed_scantables(v);
5280 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5285 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5286 v->sprite_width = avctx->coded_width;
5287 v->sprite_height = avctx->coded_height;
5289 avctx->coded_width = avctx->width = v->output_width;
5290 avctx->coded_height = avctx->height = v->output_height;
5292 // prevent 16.16 overflows
5293 if (v->sprite_width > 1 << 14 ||
5294 v->sprite_height > 1 << 14 ||
5295 v->output_width > 1 << 14 ||
5296 v->output_height > 1 << 14) return -1;
5301 /** Close a VC1/WMV3 decoder
5302 * @warning Initial try at using MpegEncContext stuff
5304 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5306 VC1Context *v = avctx->priv_data;
5309 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5310 && v->sprite_output_frame.data[0])
5311 avctx->release_buffer(avctx, &v->sprite_output_frame);
5312 for (i = 0; i < 4; i++)
5313 av_freep(&v->sr_rows[i >> 1][i & 1]);
5314 av_freep(&v->hrd_rate);
5315 av_freep(&v->hrd_buffer);
5316 ff_MPV_common_end(&v->s);
5317 av_freep(&v->mv_type_mb_plane);
5318 av_freep(&v->direct_mb_plane);
5319 av_freep(&v->forward_mb_plane);
5320 av_freep(&v->fieldtx_plane);
5321 av_freep(&v->acpred_plane);
5322 av_freep(&v->over_flags_plane);
5323 av_freep(&v->mb_type_base);
5324 av_freep(&v->blk_mv_type_base);
5325 av_freep(&v->mv_f_base);
5326 av_freep(&v->mv_f_last_base);
5327 av_freep(&v->mv_f_next_base);
5328 av_freep(&v->block);
5329 av_freep(&v->cbp_base);
5330 av_freep(&v->ttblk_base);
5331 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5332 av_freep(&v->luma_mv_base);
5333 ff_intrax8_common_end(&v->x8);
5338 /** Decode a VC1/WMV3 frame
5339 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5341 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5342 int *got_frame, AVPacket *avpkt)
5344 const uint8_t *buf = avpkt->data;
5345 int buf_size = avpkt->size, n_slices = 0, i;
5346 VC1Context *v = avctx->priv_data;
5347 MpegEncContext *s = &v->s;
5348 AVFrame *pict = data;
5349 uint8_t *buf2 = NULL;
5350 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5351 int mb_height, n_slices1=-1;
5356 } *slices = NULL, *tmp;
5358 v->second_field = 0;
5360 if(s->flags & CODEC_FLAG_LOW_DELAY)
5363 /* no supplementary picture */
5364 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5365 /* special case for last picture */
5366 if (s->low_delay == 0 && s->next_picture_ptr) {
5367 *pict = s->next_picture_ptr->f;
5368 s->next_picture_ptr = NULL;
5376 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5377 if (v->profile < PROFILE_ADVANCED)
5378 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5380 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5383 //for advanced profile we may need to parse and unescape data
5384 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5386 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5388 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5389 const uint8_t *start, *end, *next;
5393 for (start = buf, end = buf + buf_size; next < end; start = next) {
5394 next = find_next_marker(start + 4, end);
5395 size = next - start - 4;
5396 if (size <= 0) continue;
5397 switch (AV_RB32(start)) {
5398 case VC1_CODE_FRAME:
5399 if (avctx->hwaccel ||
5400 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5402 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5404 case VC1_CODE_FIELD: {
5406 if (avctx->hwaccel ||
5407 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5408 buf_start_second_field = start;
5409 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5413 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5414 if (!slices[n_slices].buf)
5416 buf_size3 = vc1_unescape_buffer(start + 4, size,
5417 slices[n_slices].buf);
5418 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5420 /* assuming that the field marker is at the exact middle,
5421 hope it's correct */
5422 slices[n_slices].mby_start = s->mb_height >> 1;
5423 n_slices1 = n_slices - 1; // index of the last slice of the first field
5427 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5428 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5429 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5430 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5432 case VC1_CODE_SLICE: {
5434 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5438 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5439 if (!slices[n_slices].buf)
5441 buf_size3 = vc1_unescape_buffer(start + 4, size,
5442 slices[n_slices].buf);
5443 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5445 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5451 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5452 const uint8_t *divider;
5455 divider = find_next_marker(buf, buf + buf_size);
5456 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5457 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5459 } else { // found field marker, unescape second field
5460 if (avctx->hwaccel ||
5461 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5462 buf_start_second_field = divider;
5463 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5467 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5468 if (!slices[n_slices].buf)
5470 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5471 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5473 slices[n_slices].mby_start = s->mb_height >> 1;
5474 n_slices1 = n_slices - 1;
5477 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5479 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5481 init_get_bits(&s->gb, buf2, buf_size2*8);
5483 init_get_bits(&s->gb, buf, buf_size*8);
5485 if (v->res_sprite) {
5486 v->new_sprite = !get_bits1(&s->gb);
5487 v->two_sprites = get_bits1(&s->gb);
5488 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5489 we're using the sprite compositor. These are intentionally kept separate
5490 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5491 the vc1 one for WVP2 */
5492 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5493 if (v->new_sprite) {
5494 // switch AVCodecContext parameters to those of the sprites
5495 avctx->width = avctx->coded_width = v->sprite_width;
5496 avctx->height = avctx->coded_height = v->sprite_height;
5503 if (s->context_initialized &&
5504 (s->width != avctx->coded_width ||
5505 s->height != avctx->coded_height)) {
5506 ff_vc1_decode_end(avctx);
5509 if (!s->context_initialized) {
5510 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5513 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5515 if (v->profile == PROFILE_ADVANCED) {
5516 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5518 s->h_edge_pos = avctx->coded_width;
5519 s->v_edge_pos = avctx->coded_height;
5523 /* We need to set current_picture_ptr before reading the header,
5524 * otherwise we cannot store anything in there. */
5525 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5526 int i = ff_find_unused_picture(s, 0);
5529 s->current_picture_ptr = &s->picture[i];
5532 // do parse frame header
5533 v->pic_header_flag = 0;
5534 v->first_pic_header_flag = 1;
5535 if (v->profile < PROFILE_ADVANCED) {
5536 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5540 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5544 v->first_pic_header_flag = 0;
5546 if (avctx->debug & FF_DEBUG_PICT_INFO)
5547 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5549 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5550 && s->pict_type != AV_PICTURE_TYPE_I) {
5551 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5555 if ((s->mb_height >> v->field_mode) == 0) {
5556 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5560 // process pulldown flags
5561 s->current_picture_ptr->f.repeat_pict = 0;
5562 // Pulldown flags are only valid when 'broadcast' has been set.
5563 // So ticks_per_frame will be 2
5566 s->current_picture_ptr->f.repeat_pict = 1;
5567 } else if (v->rptfrm) {
5569 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5572 // for skipping the frame
5573 s->current_picture.f.pict_type = s->pict_type;
5574 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5576 /* skip B-frames if we don't have reference frames */
5577 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5580 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5581 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5582 avctx->skip_frame >= AVDISCARD_ALL) {
5586 if (s->next_p_frame_damaged) {
5587 if (s->pict_type == AV_PICTURE_TYPE_B)
5590 s->next_p_frame_damaged = 0;
5593 if (ff_MPV_frame_start(s, avctx) < 0) {
5597 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5598 v->s.current_picture_ptr->f.top_field_first = v->tff;
5600 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5601 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5603 if ((CONFIG_VC1_VDPAU_DECODER)
5604 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5605 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5606 else if (avctx->hwaccel) {
5607 if (v->field_mode && buf_start_second_field) {
5608 // decode first field
5609 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5610 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5612 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5614 if (avctx->hwaccel->end_frame(avctx) < 0)
5617 // decode second field
5618 s->gb = slices[n_slices1 + 1].gb;
5619 s->picture_structure = PICT_TOP_FIELD + v->tff;
5620 v->second_field = 1;
5621 v->pic_header_flag = 0;
5622 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5623 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5626 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
5628 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5630 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5632 if (avctx->hwaccel->end_frame(avctx) < 0)
5635 s->picture_structure = PICT_FRAME;
5636 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5638 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5640 if (avctx->hwaccel->end_frame(avctx) < 0)
5644 if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5645 goto err; // This codepath is still incomplete thus it is disabled
5647 ff_er_frame_start(s);
5649 v->bits = buf_size * 8;
5650 v->end_mb_x = s->mb_width;
5651 if (v->field_mode) {
5653 s->current_picture.f.linesize[0] <<= 1;
5654 s->current_picture.f.linesize[1] <<= 1;
5655 s->current_picture.f.linesize[2] <<= 1;
5657 s->uvlinesize <<= 1;
5658 tmp[0] = v->mv_f_last[0];
5659 tmp[1] = v->mv_f_last[1];
5660 v->mv_f_last[0] = v->mv_f_next[0];
5661 v->mv_f_last[1] = v->mv_f_next[1];
5662 v->mv_f_next[0] = v->mv_f[0];
5663 v->mv_f_next[1] = v->mv_f[1];
5664 v->mv_f[0] = tmp[0];
5665 v->mv_f[1] = tmp[1];
5667 mb_height = s->mb_height >> v->field_mode;
5668 for (i = 0; i <= n_slices; i++) {
5669 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5670 if (v->field_mode <= 0) {
5671 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5672 "picture boundary (%d >= %d)\n", i,
5673 slices[i - 1].mby_start, mb_height);
5676 v->second_field = 1;
5677 v->blocks_off = s->b8_stride * (s->mb_height&~1);
5678 v->mb_off = s->mb_stride * s->mb_height >> 1;
5680 v->second_field = 0;
5685 v->pic_header_flag = 0;
5686 if (v->field_mode && i == n_slices1 + 2) {
5687 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5688 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5691 } else if (get_bits1(&s->gb)) {
5692 v->pic_header_flag = 1;
5693 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5694 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5699 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5700 if (!v->field_mode || v->second_field)
5701 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5703 if (i >= n_slices) {
5704 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5707 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5709 if (s->end_mb_y <= s->start_mb_y) {
5710 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5713 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
5714 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
5717 ff_vc1_decode_blocks(v);
5719 s->gb = slices[i].gb;
5721 if (v->field_mode) {
5722 v->second_field = 0;
5723 if (s->pict_type == AV_PICTURE_TYPE_B) {
5724 memcpy(v->mv_f_base, v->mv_f_next_base,
5725 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5727 s->current_picture.f.linesize[0] >>= 1;
5728 s->current_picture.f.linesize[1] >>= 1;
5729 s->current_picture.f.linesize[2] >>= 1;
5731 s->uvlinesize >>= 1;
5733 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5734 get_bits_count(&s->gb), s->gb.size_in_bits);
5735 // if (get_bits_count(&s->gb) > buf_size * 8)
5737 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5743 ff_MPV_frame_end(s);
5745 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5747 avctx->width = avctx->coded_width = v->output_width;
5748 avctx->height = avctx->coded_height = v->output_height;
5749 if (avctx->skip_frame >= AVDISCARD_NONREF)
5751 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5752 if (vc1_decode_sprites(v, &s->gb))
5755 *pict = v->sprite_output_frame;
5758 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5759 *pict = s->current_picture_ptr->f;
5760 } else if (s->last_picture_ptr != NULL) {
5761 *pict = s->last_picture_ptr->f;
5763 if (s->last_picture_ptr || s->low_delay) {
5765 ff_print_debug_info(s, pict);
5771 for (i = 0; i < n_slices; i++)
5772 av_free(slices[i].buf);
5778 for (i = 0; i < n_slices; i++)
5779 av_free(slices[i].buf);
5785 static const AVProfile profiles[] = {
5786 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5787 { FF_PROFILE_VC1_MAIN, "Main" },
5788 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5789 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5790 { FF_PROFILE_UNKNOWN },
5793 AVCodec ff_vc1_decoder = {
5795 .type = AVMEDIA_TYPE_VIDEO,
5796 .id = AV_CODEC_ID_VC1,
5797 .priv_data_size = sizeof(VC1Context),
5798 .init = vc1_decode_init,
5799 .close = ff_vc1_decode_end,
5800 .decode = vc1_decode_frame,
5801 .flush = ff_mpeg_flush,
5802 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5803 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5804 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5805 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5808 #if CONFIG_WMV3_DECODER
5809 AVCodec ff_wmv3_decoder = {
5811 .type = AVMEDIA_TYPE_VIDEO,
5812 .id = AV_CODEC_ID_WMV3,
5813 .priv_data_size = sizeof(VC1Context),
5814 .init = vc1_decode_init,
5815 .close = ff_vc1_decode_end,
5816 .decode = vc1_decode_frame,
5817 .flush = ff_mpeg_flush,
5818 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5819 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5820 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5821 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5825 #if CONFIG_WMV3_VDPAU_DECODER
5826 AVCodec ff_wmv3_vdpau_decoder = {
5827 .name = "wmv3_vdpau",
5828 .type = AVMEDIA_TYPE_VIDEO,
5829 .id = AV_CODEC_ID_WMV3,
5830 .priv_data_size = sizeof(VC1Context),
5831 .init = vc1_decode_init,
5832 .close = ff_vc1_decode_end,
5833 .decode = vc1_decode_frame,
5834 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5835 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5836 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5837 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5841 #if CONFIG_VC1_VDPAU_DECODER
5842 AVCodec ff_vc1_vdpau_decoder = {
5843 .name = "vc1_vdpau",
5844 .type = AVMEDIA_TYPE_VIDEO,
5845 .id = AV_CODEC_ID_VC1,
5846 .priv_data_size = sizeof(VC1Context),
5847 .init = vc1_decode_init,
5848 .close = ff_vc1_decode_end,
5849 .decode = vc1_decode_frame,
5850 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5851 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5852 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5853 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5857 #if CONFIG_WMV3IMAGE_DECODER
5858 AVCodec ff_wmv3image_decoder = {
5859 .name = "wmv3image",
5860 .type = AVMEDIA_TYPE_VIDEO,
5861 .id = AV_CODEC_ID_WMV3IMAGE,
5862 .priv_data_size = sizeof(VC1Context),
5863 .init = vc1_decode_init,
5864 .close = ff_vc1_decode_end,
5865 .decode = vc1_decode_frame,
5866 .capabilities = CODEC_CAP_DR1,
5867 .flush = vc1_sprite_flush,
5868 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5869 .pix_fmts = ff_pixfmt_list_420
5873 #if CONFIG_VC1IMAGE_DECODER
5874 AVCodec ff_vc1image_decoder = {
5876 .type = AVMEDIA_TYPE_VIDEO,
5877 .id = AV_CODEC_ID_VC1IMAGE,
5878 .priv_data_size = sizeof(VC1Context),
5879 .init = vc1_decode_init,
5880 .close = ff_vc1_decode_end,
5881 .decode = vc1_decode_frame,
5882 .capabilities = CODEC_CAP_DR1,
5883 .flush = vc1_sprite_flush,
5884 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5885 .pix_fmts = ff_pixfmt_list_420