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;
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 fieldtx = v->fieldtx_plane[topleft_mb_pos];
97 stride_y = s->linesize << fieldtx;
98 v_dist = (16 - fieldtx) >> (fieldtx == 0);
99 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
100 s->dest[0] - 16 * s->linesize - 16,
102 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
103 s->dest[0] - 16 * s->linesize - 8,
105 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
106 s->dest[0] - v_dist * s->linesize - 16,
108 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
109 s->dest[0] - v_dist * s->linesize - 8,
111 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
112 s->dest[1] - 8 * s->uvlinesize - 8,
114 s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
115 s->dest[2] - 8 * s->uvlinesize - 8,
118 if (s->mb_x == s->mb_width - 1) {
119 top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
120 fieldtx = v->fieldtx_plane[top_mb_pos];
121 stride_y = s->linesize << fieldtx;
122 v_dist = fieldtx ? 15 : 8;
123 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
124 s->dest[0] - 16 * s->linesize,
126 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
127 s->dest[0] - 16 * s->linesize + 8,
129 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
130 s->dest[0] - v_dist * s->linesize,
132 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
133 s->dest[0] - v_dist * s->linesize + 8,
135 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
136 s->dest[1] - 8 * s->uvlinesize,
138 s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
139 s->dest[2] - 8 * s->uvlinesize,
144 #define inc_blk_idx(idx) do { \
146 if (idx >= v->n_allocated_blks) \
150 inc_blk_idx(v->topleft_blk_idx);
151 inc_blk_idx(v->top_blk_idx);
152 inc_blk_idx(v->left_blk_idx);
153 inc_blk_idx(v->cur_blk_idx);
156 static void vc1_loop_filter_iblk(VC1Context *v, int pq)
158 MpegEncContext *s = &v->s;
160 if (!s->first_slice_line) {
161 v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
163 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
164 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
165 for (j = 0; j < 2; j++) {
166 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
168 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
171 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
173 if (s->mb_y == s->end_mb_y - 1) {
175 v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
176 v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
177 v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
179 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
183 static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
185 MpegEncContext *s = &v->s;
188 /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
189 * means it runs two rows/cols behind the decoding loop. */
190 if (!s->first_slice_line) {
192 if (s->mb_y >= s->start_mb_y + 2) {
193 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
196 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
197 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
198 for (j = 0; j < 2; j++) {
199 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
201 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
205 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
208 if (s->mb_x == s->mb_width - 1) {
209 if (s->mb_y >= s->start_mb_y + 2) {
210 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
213 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
214 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
215 for (j = 0; j < 2; j++) {
216 v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
218 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
222 v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
225 if (s->mb_y == s->end_mb_y) {
228 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
229 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
231 for (j = 0; j < 2; j++) {
232 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
237 if (s->mb_x == s->mb_width - 1) {
239 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
240 v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
242 for (j = 0; j < 2; j++) {
243 v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
251 static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
253 MpegEncContext *s = &v->s;
256 if (v->condover == CONDOVER_NONE)
259 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
261 /* Within a MB, the horizontal overlap always runs before the vertical.
262 * To accomplish that, we run the H on left and internal borders of the
263 * currently decoded MB. Then, we wait for the next overlap iteration
264 * to do H overlap on the right edge of this MB, before moving over and
265 * running the V overlap. Therefore, the V overlap makes us trail by one
266 * MB col and the H overlap filter makes us trail by one MB row. This
267 * is reflected in the time at which we run the put_pixels loop. */
268 if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
269 if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
270 v->over_flags_plane[mb_pos - 1])) {
271 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
272 v->block[v->cur_blk_idx][0]);
273 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
274 v->block[v->cur_blk_idx][2]);
275 if (!(s->flags & CODEC_FLAG_GRAY)) {
276 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
277 v->block[v->cur_blk_idx][4]);
278 v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
279 v->block[v->cur_blk_idx][5]);
282 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
283 v->block[v->cur_blk_idx][1]);
284 v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
285 v->block[v->cur_blk_idx][3]);
287 if (s->mb_x == s->mb_width - 1) {
288 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
289 v->over_flags_plane[mb_pos - s->mb_stride])) {
290 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
291 v->block[v->cur_blk_idx][0]);
292 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
293 v->block[v->cur_blk_idx][1]);
294 if (!(s->flags & CODEC_FLAG_GRAY)) {
295 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
296 v->block[v->cur_blk_idx][4]);
297 v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
298 v->block[v->cur_blk_idx][5]);
301 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
302 v->block[v->cur_blk_idx][2]);
303 v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
304 v->block[v->cur_blk_idx][3]);
307 if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
308 if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
309 v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
310 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
311 v->block[v->left_blk_idx][0]);
312 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
313 v->block[v->left_blk_idx][1]);
314 if (!(s->flags & CODEC_FLAG_GRAY)) {
315 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
316 v->block[v->left_blk_idx][4]);
317 v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
318 v->block[v->left_blk_idx][5]);
321 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
322 v->block[v->left_blk_idx][2]);
323 v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
324 v->block[v->left_blk_idx][3]);
328 /** Do motion compensation over 1 macroblock
329 * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
331 static void vc1_mc_1mv(VC1Context *v, int dir)
333 MpegEncContext *s = &v->s;
334 DSPContext *dsp = &v->s.dsp;
335 uint8_t *srcY, *srcU, *srcV;
336 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
338 int v_edge_pos = s->v_edge_pos >> v->field_mode;
340 if ((!v->field_mode ||
341 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
342 !v->s.last_picture.f.data[0])
345 mx = s->mv[dir][0][0];
346 my = s->mv[dir][0][1];
348 // store motion vectors for further use in B frames
349 if (s->pict_type == AV_PICTURE_TYPE_P) {
350 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
351 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
354 uvmx = (mx + ((mx & 3) == 3)) >> 1;
355 uvmy = (my + ((my & 3) == 3)) >> 1;
356 v->luma_mv[s->mb_x][0] = uvmx;
357 v->luma_mv[s->mb_x][1] = uvmy;
360 v->cur_field_type != v->ref_field_type[dir]) {
361 my = my - 2 + 4 * v->cur_field_type;
362 uvmy = uvmy - 2 + 4 * v->cur_field_type;
365 // fastuvmc shall be ignored for interlaced frame picture
366 if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
367 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
368 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
370 if (v->field_mode) { // interlaced field picture
372 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
373 srcY = s->current_picture.f.data[0];
374 srcU = s->current_picture.f.data[1];
375 srcV = s->current_picture.f.data[2];
377 srcY = s->last_picture.f.data[0];
378 srcU = s->last_picture.f.data[1];
379 srcV = s->last_picture.f.data[2];
382 srcY = s->next_picture.f.data[0];
383 srcU = s->next_picture.f.data[1];
384 srcV = s->next_picture.f.data[2];
388 srcY = s->last_picture.f.data[0];
389 srcU = s->last_picture.f.data[1];
390 srcV = s->last_picture.f.data[2];
392 srcY = s->next_picture.f.data[0];
393 srcU = s->next_picture.f.data[1];
394 srcV = s->next_picture.f.data[2];
401 src_x = s->mb_x * 16 + (mx >> 2);
402 src_y = s->mb_y * 16 + (my >> 2);
403 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
404 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
406 if (v->profile != PROFILE_ADVANCED) {
407 src_x = av_clip( src_x, -16, s->mb_width * 16);
408 src_y = av_clip( src_y, -16, s->mb_height * 16);
409 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
410 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
412 src_x = av_clip( src_x, -17, s->avctx->coded_width);
413 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
414 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
415 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
418 srcY += src_y * s->linesize + src_x;
419 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
420 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
422 if (v->field_mode && v->ref_field_type[dir]) {
423 srcY += s->current_picture_ptr->f.linesize[0];
424 srcU += s->current_picture_ptr->f.linesize[1];
425 srcV += s->current_picture_ptr->f.linesize[2];
428 /* for grayscale we should not try to read from unknown area */
429 if (s->flags & CODEC_FLAG_GRAY) {
430 srcU = s->edge_emu_buffer + 18 * s->linesize;
431 srcV = s->edge_emu_buffer + 18 * s->linesize;
434 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
435 || s->h_edge_pos < 22 || v_edge_pos < 22
436 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
437 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
438 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
440 srcY -= s->mspel * (1 + s->linesize);
441 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
442 17 + s->mspel * 2, 17 + s->mspel * 2,
443 src_x - s->mspel, src_y - s->mspel,
444 s->h_edge_pos, v_edge_pos);
445 srcY = s->edge_emu_buffer;
446 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
447 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
448 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
449 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
452 /* if we deal with range reduction we need to scale source blocks */
453 if (v->rangeredfrm) {
458 for (j = 0; j < 17 + s->mspel * 2; j++) {
459 for (i = 0; i < 17 + s->mspel * 2; i++)
460 src[i] = ((src[i] - 128) >> 1) + 128;
465 for (j = 0; j < 9; j++) {
466 for (i = 0; i < 9; i++) {
467 src[i] = ((src[i] - 128) >> 1) + 128;
468 src2[i] = ((src2[i] - 128) >> 1) + 128;
470 src += s->uvlinesize;
471 src2 += s->uvlinesize;
474 /* if we deal with intensity compensation we need to scale source blocks */
475 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
480 for (j = 0; j < 17 + s->mspel * 2; j++) {
481 for (i = 0; i < 17 + s->mspel * 2; i++)
482 src[i] = v->luty[src[i]];
487 for (j = 0; j < 9; j++) {
488 for (i = 0; i < 9; i++) {
489 src[i] = v->lutuv[src[i]];
490 src2[i] = v->lutuv[src2[i]];
492 src += s->uvlinesize;
493 src2 += s->uvlinesize;
496 srcY += s->mspel * (1 + s->linesize);
499 if (v->field_mode && v->second_field) {
500 off = s->current_picture_ptr->f.linesize[0];
501 off_uv = s->current_picture_ptr->f.linesize[1];
507 dxy = ((my & 3) << 2) | (mx & 3);
508 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
509 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
510 srcY += s->linesize * 8;
511 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
512 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
513 } else { // hpel mc - always used for luma
514 dxy = (my & 2) | ((mx & 2) >> 1);
516 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
518 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
521 if (s->flags & CODEC_FLAG_GRAY) return;
522 /* Chroma MC always uses qpel bilinear */
523 uvmx = (uvmx & 3) << 1;
524 uvmy = (uvmy & 3) << 1;
526 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
527 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
529 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
530 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
534 static inline int median4(int a, int b, int c, int d)
537 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
538 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
540 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
541 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
545 /** Do motion compensation for 4-MV macroblock - luminance block
547 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
549 MpegEncContext *s = &v->s;
550 DSPContext *dsp = &v->s.dsp;
552 int dxy, mx, my, src_x, src_y;
554 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
555 int v_edge_pos = s->v_edge_pos >> v->field_mode;
557 if ((!v->field_mode ||
558 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
559 !v->s.last_picture.f.data[0])
562 mx = s->mv[dir][n][0];
563 my = s->mv[dir][n][1];
567 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
568 srcY = s->current_picture.f.data[0];
570 srcY = s->last_picture.f.data[0];
572 srcY = s->last_picture.f.data[0];
574 srcY = s->next_picture.f.data[0];
580 if (v->cur_field_type != v->ref_field_type[dir])
581 my = my - 2 + 4 * v->cur_field_type;
584 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
585 int same_count = 0, opp_count = 0, k;
586 int chosen_mv[2][4][2], f;
588 for (k = 0; k < 4; k++) {
589 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
590 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
591 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
595 f = opp_count > same_count;
596 switch (f ? opp_count : same_count) {
598 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
599 chosen_mv[f][2][0], chosen_mv[f][3][0]);
600 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
601 chosen_mv[f][2][1], chosen_mv[f][3][1]);
604 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
605 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
608 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
609 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
614 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
615 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
616 for (k = 0; k < 4; k++)
617 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
620 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
622 int width = s->avctx->coded_width;
623 int height = s->avctx->coded_height >> 1;
624 qx = (s->mb_x * 16) + (mx >> 2);
625 qy = (s->mb_y * 8) + (my >> 3);
630 mx -= 4 * (qx - width);
633 else if (qy > height + 1)
634 my -= 8 * (qy - height - 1);
637 if ((v->fcm == ILACE_FRAME) && fieldmv)
638 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
640 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
641 if (v->field_mode && v->second_field)
642 off += s->current_picture_ptr->f.linesize[0];
644 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
646 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
648 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
650 if (v->profile != PROFILE_ADVANCED) {
651 src_x = av_clip(src_x, -16, s->mb_width * 16);
652 src_y = av_clip(src_y, -16, s->mb_height * 16);
654 src_x = av_clip(src_x, -17, s->avctx->coded_width);
655 if (v->fcm == ILACE_FRAME) {
657 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
659 src_y = av_clip(src_y, -18, s->avctx->coded_height);
661 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
665 srcY += src_y * s->linesize + src_x;
666 if (v->field_mode && v->ref_field_type[dir])
667 srcY += s->current_picture_ptr->f.linesize[0];
669 if (fieldmv && !(src_y & 1))
671 if (fieldmv && (src_y & 1) && src_y < 4)
673 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
674 || s->h_edge_pos < 13 || v_edge_pos < 23
675 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
676 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
677 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
678 /* check emulate edge stride and offset */
679 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
680 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
681 src_x - s->mspel, src_y - (s->mspel << fieldmv),
682 s->h_edge_pos, v_edge_pos);
683 srcY = s->edge_emu_buffer;
684 /* if we deal with range reduction we need to scale source blocks */
685 if (v->rangeredfrm) {
690 for (j = 0; j < 9 + s->mspel * 2; j++) {
691 for (i = 0; i < 9 + s->mspel * 2; i++)
692 src[i] = ((src[i] - 128) >> 1) + 128;
693 src += s->linesize << fieldmv;
696 /* if we deal with intensity compensation we need to scale source blocks */
697 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
702 for (j = 0; j < 9 + s->mspel * 2; j++) {
703 for (i = 0; i < 9 + s->mspel * 2; i++)
704 src[i] = v->luty[src[i]];
705 src += s->linesize << fieldmv;
708 srcY += s->mspel * (1 + (s->linesize << fieldmv));
712 dxy = ((my & 3) << 2) | (mx & 3);
713 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
714 } else { // hpel mc - always used for luma
715 dxy = (my & 2) | ((mx & 2) >> 1);
717 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
719 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
723 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
726 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
728 idx = ((a[3] != flag) << 3)
729 | ((a[2] != flag) << 2)
730 | ((a[1] != flag) << 1)
733 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
734 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
736 } else if (count[idx] == 1) {
739 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
740 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
743 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
744 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
747 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
748 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
751 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
752 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
755 } else if (count[idx] == 2) {
757 for (i = 0; i < 3; i++)
762 for (i = t1 + 1; i < 4; i++)
767 *tx = (mvx[t1] + mvx[t2]) / 2;
768 *ty = (mvy[t1] + mvy[t2]) / 2;
776 /** Do motion compensation for 4-MV macroblock - both chroma blocks
778 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
780 MpegEncContext *s = &v->s;
781 DSPContext *dsp = &v->s.dsp;
782 uint8_t *srcU, *srcV;
783 int uvmx, uvmy, uvsrc_x, uvsrc_y;
784 int k, tx = 0, ty = 0;
785 int mvx[4], mvy[4], intra[4], mv_f[4];
787 int chroma_ref_type = v->cur_field_type, off = 0;
788 int v_edge_pos = s->v_edge_pos >> v->field_mode;
790 if (!v->field_mode && !v->s.last_picture.f.data[0])
792 if (s->flags & CODEC_FLAG_GRAY)
795 for (k = 0; k < 4; k++) {
796 mvx[k] = s->mv[dir][k][0];
797 mvy[k] = s->mv[dir][k][1];
798 intra[k] = v->mb_type[0][s->block_index[k]];
800 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
803 /* calculate chroma MV vector from four luma MVs */
804 if (!v->field_mode || (v->field_mode && !v->numref)) {
805 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
806 chroma_ref_type = v->reffield;
808 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
809 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
810 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
811 return; //no need to do MC for intra blocks
815 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
817 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
819 chroma_ref_type = !v->cur_field_type;
821 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
823 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
824 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
825 uvmx = (tx + ((tx & 3) == 3)) >> 1;
826 uvmy = (ty + ((ty & 3) == 3)) >> 1;
828 v->luma_mv[s->mb_x][0] = uvmx;
829 v->luma_mv[s->mb_x][1] = uvmy;
832 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
833 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
835 // Field conversion bias
836 if (v->cur_field_type != chroma_ref_type)
837 uvmy += 2 - 4 * chroma_ref_type;
839 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
840 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
842 if (v->profile != PROFILE_ADVANCED) {
843 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
844 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
846 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
847 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
852 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
853 srcU = s->current_picture.f.data[1];
854 srcV = s->current_picture.f.data[2];
856 srcU = s->last_picture.f.data[1];
857 srcV = s->last_picture.f.data[2];
860 srcU = s->last_picture.f.data[1];
861 srcV = s->last_picture.f.data[2];
864 srcU = s->next_picture.f.data[1];
865 srcV = s->next_picture.f.data[2];
871 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
872 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
875 if (chroma_ref_type) {
876 srcU += s->current_picture_ptr->f.linesize[1];
877 srcV += s->current_picture_ptr->f.linesize[2];
879 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
882 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
883 || s->h_edge_pos < 18 || v_edge_pos < 18
884 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
885 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
886 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
887 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
888 s->h_edge_pos >> 1, v_edge_pos >> 1);
889 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
890 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
891 s->h_edge_pos >> 1, v_edge_pos >> 1);
892 srcU = s->edge_emu_buffer;
893 srcV = s->edge_emu_buffer + 16;
895 /* if we deal with range reduction we need to scale source blocks */
896 if (v->rangeredfrm) {
902 for (j = 0; j < 9; j++) {
903 for (i = 0; i < 9; i++) {
904 src[i] = ((src[i] - 128) >> 1) + 128;
905 src2[i] = ((src2[i] - 128) >> 1) + 128;
907 src += s->uvlinesize;
908 src2 += s->uvlinesize;
911 /* if we deal with intensity compensation we need to scale source blocks */
912 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
918 for (j = 0; j < 9; j++) {
919 for (i = 0; i < 9; i++) {
920 src[i] = v->lutuv[src[i]];
921 src2[i] = v->lutuv[src2[i]];
923 src += s->uvlinesize;
924 src2 += s->uvlinesize;
929 /* Chroma MC always uses qpel bilinear */
930 uvmx = (uvmx & 3) << 1;
931 uvmy = (uvmy & 3) << 1;
933 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
934 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
936 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
937 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
941 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
943 static void vc1_mc_4mv_chroma4(VC1Context *v)
945 MpegEncContext *s = &v->s;
946 DSPContext *dsp = &v->s.dsp;
947 uint8_t *srcU, *srcV;
948 int uvsrc_x, uvsrc_y;
949 int uvmx_field[4], uvmy_field[4];
951 int fieldmv = v->blk_mv_type[s->block_index[0]];
952 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
953 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
954 int v_edge_pos = s->v_edge_pos >> 1;
956 if (!v->s.last_picture.f.data[0])
958 if (s->flags & CODEC_FLAG_GRAY)
961 for (i = 0; i < 4; i++) {
963 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
966 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
968 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
971 for (i = 0; i < 4; i++) {
972 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
973 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
974 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
975 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
976 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
977 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
978 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
979 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
980 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
981 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
983 if (fieldmv && !(uvsrc_y & 1))
985 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
987 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
988 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
989 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
990 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
991 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
992 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
993 s->h_edge_pos >> 1, v_edge_pos);
994 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
995 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
996 s->h_edge_pos >> 1, v_edge_pos);
997 srcU = s->edge_emu_buffer;
998 srcV = s->edge_emu_buffer + 16;
1000 /* if we deal with intensity compensation we need to scale source blocks */
1001 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1003 uint8_t *src, *src2;
1007 for (j = 0; j < 5; j++) {
1008 for (i = 0; i < 5; i++) {
1009 src[i] = v->lutuv[src[i]];
1010 src2[i] = v->lutuv[src2[i]];
1012 src += s->uvlinesize << 1;
1013 src2 += s->uvlinesize << 1;
1018 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1019 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1021 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]);
1022 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]);
1027 /***********************************************************************/
1029 * @name VC-1 Block-level functions
1030 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1036 * @brief Get macroblock-level quantizer scale
1038 #define GET_MQUANT() \
1039 if (v->dquantfrm) { \
1041 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1042 if (v->dqbilevel) { \
1043 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1045 mqdiff = get_bits(gb, 3); \
1047 mquant = v->pq + mqdiff; \
1049 mquant = get_bits(gb, 5); \
1052 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1053 edges = 1 << v->dqsbedge; \
1054 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1055 edges = (3 << v->dqsbedge) % 15; \
1056 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1058 if ((edges&1) && !s->mb_x) \
1059 mquant = v->altpq; \
1060 if ((edges&2) && s->first_slice_line) \
1061 mquant = v->altpq; \
1062 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1063 mquant = v->altpq; \
1064 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1065 mquant = v->altpq; \
1066 if (!mquant || mquant > 31) { \
1067 av_log(v->s.avctx, AV_LOG_ERROR, \
1068 "Overriding invalid mquant %d\n", mquant); \
1074 * @def GET_MVDATA(_dmv_x, _dmv_y)
1075 * @brief Get MV differentials
1076 * @see MVDATA decoding from 8.3.5.2, p(1)20
1077 * @param _dmv_x Horizontal differential for decoded MV
1078 * @param _dmv_y Vertical differential for decoded MV
1080 #define GET_MVDATA(_dmv_x, _dmv_y) \
1081 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1082 VC1_MV_DIFF_VLC_BITS, 2); \
1084 mb_has_coeffs = 1; \
1087 mb_has_coeffs = 0; \
1090 _dmv_x = _dmv_y = 0; \
1091 } else if (index == 35) { \
1092 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1093 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1094 } else if (index == 36) { \
1099 index1 = index % 6; \
1100 if (!s->quarter_sample && index1 == 5) val = 1; \
1102 if (size_table[index1] - val > 0) \
1103 val = get_bits(gb, size_table[index1] - val); \
1105 sign = 0 - (val&1); \
1106 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1108 index1 = index / 6; \
1109 if (!s->quarter_sample && index1 == 5) val = 1; \
1111 if (size_table[index1] - val > 0) \
1112 val = get_bits(gb, size_table[index1] - val); \
1114 sign = 0 - (val & 1); \
1115 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1118 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1119 int *dmv_y, int *pred_flag)
1122 int extend_x = 0, extend_y = 0;
1123 GetBitContext *gb = &v->s.gb;
1126 const int* offs_tab;
1129 bits = VC1_2REF_MVDATA_VLC_BITS;
1132 bits = VC1_1REF_MVDATA_VLC_BITS;
1135 switch (v->dmvrange) {
1143 extend_x = extend_y = 1;
1146 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1148 *dmv_x = get_bits(gb, v->k_x);
1149 *dmv_y = get_bits(gb, v->k_y);
1151 *pred_flag = *dmv_y & 1;
1152 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1156 av_assert0(index < esc);
1158 offs_tab = offset_table2;
1160 offs_tab = offset_table1;
1161 index1 = (index + 1) % 9;
1163 val = get_bits(gb, index1 + extend_x);
1164 sign = 0 -(val & 1);
1165 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1169 offs_tab = offset_table2;
1171 offs_tab = offset_table1;
1172 index1 = (index + 1) / 9;
1173 if (index1 > v->numref) {
1174 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1175 sign = 0 - (val & 1);
1176 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1180 *pred_flag = index1 & 1;
1184 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1186 int scaledvalue, refdist;
1187 int scalesame1, scalesame2;
1188 int scalezone1_x, zone1offset_x;
1189 int table_index = dir ^ v->second_field;
1191 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1192 refdist = v->refdist;
1194 refdist = dir ? v->brfd : v->frfd;
1197 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1198 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1199 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1200 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1205 if (FFABS(n) < scalezone1_x)
1206 scaledvalue = (n * scalesame1) >> 8;
1209 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1211 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1214 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1217 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1219 int scaledvalue, refdist;
1220 int scalesame1, scalesame2;
1221 int scalezone1_y, zone1offset_y;
1222 int table_index = dir ^ v->second_field;
1224 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1225 refdist = v->refdist;
1227 refdist = dir ? v->brfd : v->frfd;
1230 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1231 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1232 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1233 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1238 if (FFABS(n) < scalezone1_y)
1239 scaledvalue = (n * scalesame1) >> 8;
1242 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1244 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1248 if (v->cur_field_type && !v->ref_field_type[dir])
1249 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1251 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1254 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1256 int scalezone1_x, zone1offset_x;
1257 int scaleopp1, scaleopp2, brfd;
1260 brfd = FFMIN(v->brfd, 3);
1261 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1262 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1263 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1264 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1269 if (FFABS(n) < scalezone1_x)
1270 scaledvalue = (n * scaleopp1) >> 8;
1273 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1275 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1278 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1281 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1283 int scalezone1_y, zone1offset_y;
1284 int scaleopp1, scaleopp2, brfd;
1287 brfd = FFMIN(v->brfd, 3);
1288 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1289 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1290 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1291 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1296 if (FFABS(n) < scalezone1_y)
1297 scaledvalue = (n * scaleopp1) >> 8;
1300 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1302 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1305 if (v->cur_field_type && !v->ref_field_type[dir]) {
1306 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1308 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1312 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1315 int brfd, scalesame;
1316 int hpel = 1 - v->s.quarter_sample;
1319 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1321 n = scaleforsame_y(v, i, n, dir) << hpel;
1323 n = scaleforsame_x(v, n, dir) << hpel;
1326 brfd = FFMIN(v->brfd, 3);
1327 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1329 n = (n * scalesame >> 8) << hpel;
1333 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1336 int refdist, scaleopp;
1337 int hpel = 1 - v->s.quarter_sample;
1340 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1342 n = scaleforopp_y(v, n, dir) << hpel;
1344 n = scaleforopp_x(v, n) << hpel;
1347 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1348 refdist = FFMIN(v->refdist, 3);
1350 refdist = dir ? v->brfd : v->frfd;
1351 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1353 n = (n * scaleopp >> 8) << hpel;
1357 /** Predict and set motion vector
1359 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1360 int mv1, int r_x, int r_y, uint8_t* is_intra,
1361 int pred_flag, int dir)
1363 MpegEncContext *s = &v->s;
1364 int xy, wrap, off = 0;
1368 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1369 int opposite, a_f, b_f, c_f;
1370 int16_t field_predA[2];
1371 int16_t field_predB[2];
1372 int16_t field_predC[2];
1373 int a_valid, b_valid, c_valid;
1374 int hybridmv_thresh, y_bias = 0;
1376 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1377 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1381 /* scale MV difference to be quad-pel */
1382 dmv_x <<= 1 - s->quarter_sample;
1383 dmv_y <<= 1 - s->quarter_sample;
1385 wrap = s->b8_stride;
1386 xy = s->block_index[n];
1389 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1390 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1391 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1392 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1393 if (mv1) { /* duplicate motion data for 1-MV block */
1394 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1395 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1396 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1397 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1398 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1399 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1400 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1401 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1402 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1403 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1404 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1405 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1406 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1411 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1412 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1414 if (v->field_mode && mixedmv_pic)
1415 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1417 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1419 //in 4-MV mode different blocks have different B predictor position
1422 off = (s->mb_x > 0) ? -1 : 1;
1425 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1434 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1436 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1437 b_valid = a_valid && (s->mb_width > 1);
1438 c_valid = s->mb_x || (n == 1 || n == 3);
1439 if (v->field_mode) {
1440 a_valid = a_valid && !is_intra[xy - wrap];
1441 b_valid = b_valid && !is_intra[xy - wrap + off];
1442 c_valid = c_valid && !is_intra[xy - 1];
1446 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1447 num_oppfield += a_f;
1448 num_samefield += 1 - a_f;
1449 field_predA[0] = A[0];
1450 field_predA[1] = A[1];
1452 field_predA[0] = field_predA[1] = 0;
1456 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1457 num_oppfield += b_f;
1458 num_samefield += 1 - b_f;
1459 field_predB[0] = B[0];
1460 field_predB[1] = B[1];
1462 field_predB[0] = field_predB[1] = 0;
1466 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1467 num_oppfield += c_f;
1468 num_samefield += 1 - c_f;
1469 field_predC[0] = C[0];
1470 field_predC[1] = C[1];
1472 field_predC[0] = field_predC[1] = 0;
1476 if (v->field_mode) {
1478 // REFFIELD determines if the last field or the second-last field is
1479 // to be used as reference
1480 opposite = 1 - v->reffield;
1482 if (num_samefield <= num_oppfield)
1483 opposite = 1 - pred_flag;
1485 opposite = pred_flag;
1490 if (a_valid && !a_f) {
1491 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1492 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1494 if (b_valid && !b_f) {
1495 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1496 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1498 if (c_valid && !c_f) {
1499 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1500 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1502 v->mv_f[dir][xy + v->blocks_off] = 1;
1503 v->ref_field_type[dir] = !v->cur_field_type;
1505 if (a_valid && a_f) {
1506 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1507 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1509 if (b_valid && b_f) {
1510 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1511 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1513 if (c_valid && c_f) {
1514 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1515 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1517 v->mv_f[dir][xy + v->blocks_off] = 0;
1518 v->ref_field_type[dir] = v->cur_field_type;
1522 px = field_predA[0];
1523 py = field_predA[1];
1524 } else if (c_valid) {
1525 px = field_predC[0];
1526 py = field_predC[1];
1527 } else if (b_valid) {
1528 px = field_predB[0];
1529 py = field_predB[1];
1535 if (num_samefield + num_oppfield > 1) {
1536 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1537 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1540 /* Pullback MV as specified in 8.3.5.3.4 */
1541 if (!v->field_mode) {
1543 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1544 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1545 X = (s->mb_width << 6) - 4;
1546 Y = (s->mb_height << 6) - 4;
1548 if (qx + px < -60) px = -60 - qx;
1549 if (qy + py < -60) py = -60 - qy;
1551 if (qx + px < -28) px = -28 - qx;
1552 if (qy + py < -28) py = -28 - qy;
1554 if (qx + px > X) px = X - qx;
1555 if (qy + py > Y) py = Y - qy;
1558 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1559 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1560 hybridmv_thresh = 32;
1561 if (a_valid && c_valid) {
1562 if (is_intra[xy - wrap])
1563 sum = FFABS(px) + FFABS(py);
1565 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1566 if (sum > hybridmv_thresh) {
1567 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1568 px = field_predA[0];
1569 py = field_predA[1];
1571 px = field_predC[0];
1572 py = field_predC[1];
1575 if (is_intra[xy - 1])
1576 sum = FFABS(px) + FFABS(py);
1578 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1579 if (sum > hybridmv_thresh) {
1580 if (get_bits1(&s->gb)) {
1581 px = field_predA[0];
1582 py = field_predA[1];
1584 px = field_predC[0];
1585 py = field_predC[1];
1592 if (v->field_mode && v->numref)
1594 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1596 /* store MV using signed modulus of MV range defined in 4.11 */
1597 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;
1598 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;
1599 if (mv1) { /* duplicate motion data for 1-MV block */
1600 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];
1601 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];
1602 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];
1603 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];
1604 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];
1605 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];
1606 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1607 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];
1611 /** Predict and set motion vector for interlaced frame picture MBs
1613 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1614 int mvn, int r_x, int r_y, uint8_t* is_intra)
1616 MpegEncContext *s = &v->s;
1617 int xy, wrap, off = 0;
1618 int A[2], B[2], C[2];
1620 int a_valid = 0, b_valid = 0, c_valid = 0;
1621 int field_a, field_b, field_c; // 0: same, 1: opposit
1622 int total_valid, num_samefield, num_oppfield;
1623 int pos_c, pos_b, n_adj;
1625 wrap = s->b8_stride;
1626 xy = s->block_index[n];
1629 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1630 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1631 s->current_picture.f.motion_val[1][xy][0] = 0;
1632 s->current_picture.f.motion_val[1][xy][1] = 0;
1633 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1634 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1635 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1636 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1637 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1638 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1639 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1640 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1641 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1642 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1643 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1644 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1645 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1646 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1651 off = ((n == 0) || (n == 1)) ? 1 : -1;
1653 if (s->mb_x || (n == 1) || (n == 3)) {
1654 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1655 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1656 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1657 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1659 } else { // current block has frame mv and cand. has field MV (so average)
1660 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1661 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1662 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1663 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1666 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1672 /* Predict B and C */
1673 B[0] = B[1] = C[0] = C[1] = 0;
1674 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1675 if (!s->first_slice_line) {
1676 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1679 pos_b = s->block_index[n_adj] - 2 * wrap;
1680 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1681 n_adj = (n & 2) | (n & 1);
1683 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1684 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1685 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1686 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1687 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1690 if (s->mb_width > 1) {
1691 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1694 pos_c = s->block_index[2] - 2 * wrap + 2;
1695 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1698 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1699 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1700 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1701 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1702 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1704 if (s->mb_x == s->mb_width - 1) {
1705 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1708 pos_c = s->block_index[3] - 2 * wrap - 2;
1709 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1712 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1713 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1714 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1715 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1716 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1725 pos_b = s->block_index[1];
1727 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1728 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1729 pos_c = s->block_index[0];
1731 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1732 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1735 total_valid = a_valid + b_valid + c_valid;
1736 // check if predictor A is out of bounds
1737 if (!s->mb_x && !(n == 1 || n == 3)) {
1740 // check if predictor B is out of bounds
1741 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1742 B[0] = B[1] = C[0] = C[1] = 0;
1744 if (!v->blk_mv_type[xy]) {
1745 if (s->mb_width == 1) {
1749 if (total_valid >= 2) {
1750 px = mid_pred(A[0], B[0], C[0]);
1751 py = mid_pred(A[1], B[1], C[1]);
1752 } else if (total_valid) {
1753 if (a_valid) { px = A[0]; py = A[1]; }
1754 if (b_valid) { px = B[0]; py = B[1]; }
1755 if (c_valid) { px = C[0]; py = C[1]; }
1761 field_a = (A[1] & 4) ? 1 : 0;
1765 field_b = (B[1] & 4) ? 1 : 0;
1769 field_c = (C[1] & 4) ? 1 : 0;
1773 num_oppfield = field_a + field_b + field_c;
1774 num_samefield = total_valid - num_oppfield;
1775 if (total_valid == 3) {
1776 if ((num_samefield == 3) || (num_oppfield == 3)) {
1777 px = mid_pred(A[0], B[0], C[0]);
1778 py = mid_pred(A[1], B[1], C[1]);
1779 } else if (num_samefield >= num_oppfield) {
1780 /* take one MV from same field set depending on priority
1781 the check for B may not be necessary */
1782 px = !field_a ? A[0] : B[0];
1783 py = !field_a ? A[1] : B[1];
1785 px = field_a ? A[0] : B[0];
1786 py = field_a ? A[1] : B[1];
1788 } else if (total_valid == 2) {
1789 if (num_samefield >= num_oppfield) {
1790 if (!field_a && a_valid) {
1793 } else if (!field_b && b_valid) {
1796 } else if (c_valid) {
1801 if (field_a && a_valid) {
1804 } else if (field_b && b_valid) {
1807 } else if (c_valid) {
1812 } else if (total_valid == 1) {
1813 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1814 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1819 /* store MV using signed modulus of MV range defined in 4.11 */
1820 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;
1821 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;
1822 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1823 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1824 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1825 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1826 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1827 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1828 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1829 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1830 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1831 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1832 s->mv[0][n + 1][0] = s->mv[0][n][0];
1833 s->mv[0][n + 1][1] = s->mv[0][n][1];
1837 /** Motion compensation for direct or interpolated blocks in B-frames
1839 static void vc1_interp_mc(VC1Context *v)
1841 MpegEncContext *s = &v->s;
1842 DSPContext *dsp = &v->s.dsp;
1843 uint8_t *srcY, *srcU, *srcV;
1844 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1846 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1848 if (!v->field_mode && !v->s.next_picture.f.data[0])
1851 mx = s->mv[1][0][0];
1852 my = s->mv[1][0][1];
1853 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1854 uvmy = (my + ((my & 3) == 3)) >> 1;
1855 if (v->field_mode) {
1856 if (v->cur_field_type != v->ref_field_type[1])
1857 my = my - 2 + 4 * v->cur_field_type;
1858 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1861 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1862 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1864 srcY = s->next_picture.f.data[0];
1865 srcU = s->next_picture.f.data[1];
1866 srcV = s->next_picture.f.data[2];
1868 src_x = s->mb_x * 16 + (mx >> 2);
1869 src_y = s->mb_y * 16 + (my >> 2);
1870 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1871 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1873 if (v->profile != PROFILE_ADVANCED) {
1874 src_x = av_clip( src_x, -16, s->mb_width * 16);
1875 src_y = av_clip( src_y, -16, s->mb_height * 16);
1876 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1877 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1879 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1880 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1881 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1882 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1885 srcY += src_y * s->linesize + src_x;
1886 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1887 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1889 if (v->field_mode && v->ref_field_type[1]) {
1890 srcY += s->current_picture_ptr->f.linesize[0];
1891 srcU += s->current_picture_ptr->f.linesize[1];
1892 srcV += s->current_picture_ptr->f.linesize[2];
1895 /* for grayscale we should not try to read from unknown area */
1896 if (s->flags & CODEC_FLAG_GRAY) {
1897 srcU = s->edge_emu_buffer + 18 * s->linesize;
1898 srcV = s->edge_emu_buffer + 18 * s->linesize;
1901 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1902 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1903 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1904 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1906 srcY -= s->mspel * (1 + s->linesize);
1907 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1908 17 + s->mspel * 2, 17 + s->mspel * 2,
1909 src_x - s->mspel, src_y - s->mspel,
1910 s->h_edge_pos, v_edge_pos);
1911 srcY = s->edge_emu_buffer;
1912 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1913 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1914 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1915 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1918 /* if we deal with range reduction we need to scale source blocks */
1919 if (v->rangeredfrm) {
1921 uint8_t *src, *src2;
1924 for (j = 0; j < 17 + s->mspel * 2; j++) {
1925 for (i = 0; i < 17 + s->mspel * 2; i++)
1926 src[i] = ((src[i] - 128) >> 1) + 128;
1931 for (j = 0; j < 9; j++) {
1932 for (i = 0; i < 9; i++) {
1933 src[i] = ((src[i] - 128) >> 1) + 128;
1934 src2[i] = ((src2[i] - 128) >> 1) + 128;
1936 src += s->uvlinesize;
1937 src2 += s->uvlinesize;
1940 srcY += s->mspel * (1 + s->linesize);
1943 if (v->field_mode && v->second_field) {
1944 off = s->current_picture_ptr->f.linesize[0];
1945 off_uv = s->current_picture_ptr->f.linesize[1];
1952 dxy = ((my & 3) << 2) | (mx & 3);
1953 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1954 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1955 srcY += s->linesize * 8;
1956 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1957 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1959 dxy = (my & 2) | ((mx & 2) >> 1);
1962 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1964 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1967 if (s->flags & CODEC_FLAG_GRAY) return;
1968 /* Chroma MC always uses qpel blilinear */
1969 uvmx = (uvmx & 3) << 1;
1970 uvmy = (uvmy & 3) << 1;
1972 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1973 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1975 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1976 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1980 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1984 #if B_FRACTION_DEN==256
1988 return 2 * ((value * n + 255) >> 9);
1989 return (value * n + 128) >> 8;
1992 n -= B_FRACTION_DEN;
1994 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1995 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1999 /** Reconstruct motion vector for B-frame and do motion compensation
2001 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2002 int direct, int mode)
2005 v->mv_mode2 = v->mv_mode;
2006 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2012 v->mv_mode = v->mv_mode2;
2015 if (mode == BMV_TYPE_INTERPOLATED) {
2019 v->mv_mode = v->mv_mode2;
2023 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2024 v->mv_mode = v->mv_mode2;
2025 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2027 v->mv_mode = v->mv_mode2;
2030 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2031 int direct, int mvtype)
2033 MpegEncContext *s = &v->s;
2034 int xy, wrap, off = 0;
2039 const uint8_t *is_intra = v->mb_type[0];
2043 /* scale MV difference to be quad-pel */
2044 dmv_x[0] <<= 1 - s->quarter_sample;
2045 dmv_y[0] <<= 1 - s->quarter_sample;
2046 dmv_x[1] <<= 1 - s->quarter_sample;
2047 dmv_y[1] <<= 1 - s->quarter_sample;
2049 wrap = s->b8_stride;
2050 xy = s->block_index[0];
2053 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2054 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2055 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2056 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2059 if (!v->field_mode) {
2060 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2061 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2062 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2063 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2065 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2066 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));
2067 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));
2068 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));
2069 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));
2072 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2073 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2074 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2075 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2079 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2080 C = s->current_picture.f.motion_val[0][xy - 2];
2081 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2082 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2083 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2085 if (!s->mb_x) C[0] = C[1] = 0;
2086 if (!s->first_slice_line) { // predictor A is not out of bounds
2087 if (s->mb_width == 1) {
2091 px = mid_pred(A[0], B[0], C[0]);
2092 py = mid_pred(A[1], B[1], C[1]);
2094 } else if (s->mb_x) { // predictor C is not out of bounds
2100 /* Pullback MV as specified in 8.3.5.3.4 */
2103 if (v->profile < PROFILE_ADVANCED) {
2104 qx = (s->mb_x << 5);
2105 qy = (s->mb_y << 5);
2106 X = (s->mb_width << 5) - 4;
2107 Y = (s->mb_height << 5) - 4;
2108 if (qx + px < -28) px = -28 - qx;
2109 if (qy + py < -28) py = -28 - qy;
2110 if (qx + px > X) px = X - qx;
2111 if (qy + py > Y) py = Y - qy;
2113 qx = (s->mb_x << 6);
2114 qy = (s->mb_y << 6);
2115 X = (s->mb_width << 6) - 4;
2116 Y = (s->mb_height << 6) - 4;
2117 if (qx + px < -60) px = -60 - qx;
2118 if (qy + py < -60) py = -60 - qy;
2119 if (qx + px > X) px = X - qx;
2120 if (qy + py > Y) py = Y - qy;
2123 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2124 if (0 && !s->first_slice_line && s->mb_x) {
2125 if (is_intra[xy - wrap])
2126 sum = FFABS(px) + FFABS(py);
2128 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2130 if (get_bits1(&s->gb)) {
2138 if (is_intra[xy - 2])
2139 sum = FFABS(px) + FFABS(py);
2141 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2143 if (get_bits1(&s->gb)) {
2153 /* store MV using signed modulus of MV range defined in 4.11 */
2154 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2155 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2157 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2158 C = s->current_picture.f.motion_val[1][xy - 2];
2159 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2160 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2161 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2165 if (!s->first_slice_line) { // predictor A is not out of bounds
2166 if (s->mb_width == 1) {
2170 px = mid_pred(A[0], B[0], C[0]);
2171 py = mid_pred(A[1], B[1], C[1]);
2173 } else if (s->mb_x) { // predictor C is not out of bounds
2179 /* Pullback MV as specified in 8.3.5.3.4 */
2182 if (v->profile < PROFILE_ADVANCED) {
2183 qx = (s->mb_x << 5);
2184 qy = (s->mb_y << 5);
2185 X = (s->mb_width << 5) - 4;
2186 Y = (s->mb_height << 5) - 4;
2187 if (qx + px < -28) px = -28 - qx;
2188 if (qy + py < -28) py = -28 - qy;
2189 if (qx + px > X) px = X - qx;
2190 if (qy + py > Y) py = Y - qy;
2192 qx = (s->mb_x << 6);
2193 qy = (s->mb_y << 6);
2194 X = (s->mb_width << 6) - 4;
2195 Y = (s->mb_height << 6) - 4;
2196 if (qx + px < -60) px = -60 - qx;
2197 if (qy + py < -60) py = -60 - qy;
2198 if (qx + px > X) px = X - qx;
2199 if (qy + py > Y) py = Y - qy;
2202 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2203 if (0 && !s->first_slice_line && s->mb_x) {
2204 if (is_intra[xy - wrap])
2205 sum = FFABS(px) + FFABS(py);
2207 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2209 if (get_bits1(&s->gb)) {
2217 if (is_intra[xy - 2])
2218 sum = FFABS(px) + FFABS(py);
2220 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2222 if (get_bits1(&s->gb)) {
2232 /* store MV using signed modulus of MV range defined in 4.11 */
2234 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2235 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2237 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2238 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2239 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2240 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2243 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2245 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2246 MpegEncContext *s = &v->s;
2247 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2249 if (v->bmvtype == BMV_TYPE_DIRECT) {
2250 int total_opp, k, f;
2251 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2252 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2253 v->bfraction, 0, s->quarter_sample);
2254 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2255 v->bfraction, 0, s->quarter_sample);
2256 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2257 v->bfraction, 1, s->quarter_sample);
2258 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2259 v->bfraction, 1, s->quarter_sample);
2261 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2262 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2263 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2264 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2265 f = (total_opp > 2) ? 1 : 0;
2267 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2268 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2271 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2272 for (k = 0; k < 4; k++) {
2273 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2274 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2275 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2276 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2277 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2278 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2282 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2283 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);
2284 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);
2287 if (dir) { // backward
2288 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);
2289 if (n == 3 || mv1) {
2290 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2293 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);
2294 if (n == 3 || mv1) {
2295 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2300 /** Get predicted DC value for I-frames only
2301 * prediction dir: left=0, top=1
2302 * @param s MpegEncContext
2303 * @param overlap flag indicating that overlap filtering is used
2304 * @param pq integer part of picture quantizer
2305 * @param[in] n block index in the current MB
2306 * @param dc_val_ptr Pointer to DC predictor
2307 * @param dir_ptr Prediction direction for use in AC prediction
2309 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2310 int16_t **dc_val_ptr, int *dir_ptr)
2312 int a, b, c, wrap, pred, scale;
2314 static const uint16_t dcpred[32] = {
2315 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2316 114, 102, 93, 85, 79, 73, 68, 64,
2317 60, 57, 54, 51, 49, 47, 45, 43,
2318 41, 39, 38, 37, 35, 34, 33
2321 /* find prediction - wmv3_dc_scale always used here in fact */
2322 if (n < 4) scale = s->y_dc_scale;
2323 else scale = s->c_dc_scale;
2325 wrap = s->block_wrap[n];
2326 dc_val = s->dc_val[0] + s->block_index[n];
2332 b = dc_val[ - 1 - wrap];
2333 a = dc_val[ - wrap];
2335 if (pq < 9 || !overlap) {
2336 /* Set outer values */
2337 if (s->first_slice_line && (n != 2 && n != 3))
2338 b = a = dcpred[scale];
2339 if (s->mb_x == 0 && (n != 1 && n != 3))
2340 b = c = dcpred[scale];
2342 /* Set outer values */
2343 if (s->first_slice_line && (n != 2 && n != 3))
2345 if (s->mb_x == 0 && (n != 1 && n != 3))
2349 if (abs(a - b) <= abs(b - c)) {
2351 *dir_ptr = 1; // left
2354 *dir_ptr = 0; // top
2357 /* update predictor */
2358 *dc_val_ptr = &dc_val[0];
2363 /** Get predicted DC value
2364 * prediction dir: left=0, top=1
2365 * @param s MpegEncContext
2366 * @param overlap flag indicating that overlap filtering is used
2367 * @param pq integer part of picture quantizer
2368 * @param[in] n block index in the current MB
2369 * @param a_avail flag indicating top block availability
2370 * @param c_avail flag indicating left block availability
2371 * @param dc_val_ptr Pointer to DC predictor
2372 * @param dir_ptr Prediction direction for use in AC prediction
2374 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2375 int a_avail, int c_avail,
2376 int16_t **dc_val_ptr, int *dir_ptr)
2378 int a, b, c, wrap, pred;
2380 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2384 wrap = s->block_wrap[n];
2385 dc_val = s->dc_val[0] + s->block_index[n];
2391 b = dc_val[ - 1 - wrap];
2392 a = dc_val[ - wrap];
2393 /* scale predictors if needed */
2394 q1 = s->current_picture.f.qscale_table[mb_pos];
2395 dqscale_index = s->y_dc_scale_table[q1] - 1;
2396 if (dqscale_index < 0)
2398 if (c_avail && (n != 1 && n != 3)) {
2399 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2401 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2403 if (a_avail && (n != 2 && n != 3)) {
2404 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2406 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2408 if (a_avail && c_avail && (n != 3)) {
2413 off -= s->mb_stride;
2414 q2 = s->current_picture.f.qscale_table[off];
2416 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2419 if (a_avail && c_avail) {
2420 if (abs(a - b) <= abs(b - c)) {
2422 *dir_ptr = 1; // left
2425 *dir_ptr = 0; // top
2427 } else if (a_avail) {
2429 *dir_ptr = 0; // top
2430 } else if (c_avail) {
2432 *dir_ptr = 1; // left
2435 *dir_ptr = 1; // left
2438 /* update predictor */
2439 *dc_val_ptr = &dc_val[0];
2443 /** @} */ // Block group
2446 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2447 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2451 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2452 uint8_t **coded_block_ptr)
2454 int xy, wrap, pred, a, b, c;
2456 xy = s->block_index[n];
2457 wrap = s->b8_stride;
2462 a = s->coded_block[xy - 1 ];
2463 b = s->coded_block[xy - 1 - wrap];
2464 c = s->coded_block[xy - wrap];
2473 *coded_block_ptr = &s->coded_block[xy];
2479 * Decode one AC coefficient
2480 * @param v The VC1 context
2481 * @param last Last coefficient
2482 * @param skip How much zero coefficients to skip
2483 * @param value Decoded AC coefficient value
2484 * @param codingset set of VLC to decode data
2487 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2488 int *value, int codingset)
2490 GetBitContext *gb = &v->s.gb;
2491 int index, escape, run = 0, level = 0, lst = 0;
2493 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2494 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2495 run = vc1_index_decode_table[codingset][index][0];
2496 level = vc1_index_decode_table[codingset][index][1];
2497 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2501 escape = decode210(gb);
2503 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2504 run = vc1_index_decode_table[codingset][index][0];
2505 level = vc1_index_decode_table[codingset][index][1];
2506 lst = index >= vc1_last_decode_table[codingset];
2509 level += vc1_last_delta_level_table[codingset][run];
2511 level += vc1_delta_level_table[codingset][run];
2514 run += vc1_last_delta_run_table[codingset][level] + 1;
2516 run += vc1_delta_run_table[codingset][level] + 1;
2522 lst = get_bits1(gb);
2523 if (v->s.esc3_level_length == 0) {
2524 if (v->pq < 8 || v->dquantfrm) { // table 59
2525 v->s.esc3_level_length = get_bits(gb, 3);
2526 if (!v->s.esc3_level_length)
2527 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2528 } else { // table 60
2529 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2531 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2533 run = get_bits(gb, v->s.esc3_run_length);
2534 sign = get_bits1(gb);
2535 level = get_bits(gb, v->s.esc3_level_length);
2546 /** Decode intra block in intra frames - should be faster than decode_intra_block
2547 * @param v VC1Context
2548 * @param block block to decode
2549 * @param[in] n subblock index
2550 * @param coded are AC coeffs present or not
2551 * @param codingset set of VLC to decode data
2553 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2554 int coded, int codingset)
2556 GetBitContext *gb = &v->s.gb;
2557 MpegEncContext *s = &v->s;
2558 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2561 int16_t *ac_val, *ac_val2;
2564 /* Get DC differential */
2566 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2568 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2571 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2575 if (dcdiff == 119 /* ESC index value */) {
2576 /* TODO: Optimize */
2577 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2578 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2579 else dcdiff = get_bits(gb, 8);
2582 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2583 else if (v->pq == 2)
2584 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2591 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2594 /* Store the quantized DC coeff, used for prediction */
2596 block[0] = dcdiff * s->y_dc_scale;
2598 block[0] = dcdiff * s->c_dc_scale;
2609 int last = 0, skip, value;
2610 const uint8_t *zz_table;
2614 scale = v->pq * 2 + v->halfpq;
2618 zz_table = v->zz_8x8[2];
2620 zz_table = v->zz_8x8[3];
2622 zz_table = v->zz_8x8[1];
2624 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2626 if (dc_pred_dir) // left
2629 ac_val -= 16 * s->block_wrap[n];
2632 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2636 block[zz_table[i++]] = value;
2639 /* apply AC prediction if needed */
2641 if (dc_pred_dir) { // left
2642 for (k = 1; k < 8; k++)
2643 block[k << v->left_blk_sh] += ac_val[k];
2645 for (k = 1; k < 8; k++)
2646 block[k << v->top_blk_sh] += ac_val[k + 8];
2649 /* save AC coeffs for further prediction */
2650 for (k = 1; k < 8; k++) {
2651 ac_val2[k] = block[k << v->left_blk_sh];
2652 ac_val2[k + 8] = block[k << v->top_blk_sh];
2655 /* scale AC coeffs */
2656 for (k = 1; k < 64; k++)
2660 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2663 if (s->ac_pred) i = 63;
2669 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2673 scale = v->pq * 2 + v->halfpq;
2674 memset(ac_val2, 0, 16 * 2);
2675 if (dc_pred_dir) { // left
2678 memcpy(ac_val2, ac_val, 8 * 2);
2680 ac_val -= 16 * s->block_wrap[n];
2682 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2685 /* apply AC prediction if needed */
2687 if (dc_pred_dir) { //left
2688 for (k = 1; k < 8; k++) {
2689 block[k << v->left_blk_sh] = ac_val[k] * scale;
2690 if (!v->pquantizer && block[k << v->left_blk_sh])
2691 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2694 for (k = 1; k < 8; k++) {
2695 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2696 if (!v->pquantizer && block[k << v->top_blk_sh])
2697 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2703 s->block_last_index[n] = i;
2708 /** Decode intra block in intra frames - should be faster than decode_intra_block
2709 * @param v VC1Context
2710 * @param block block to decode
2711 * @param[in] n subblock number
2712 * @param coded are AC coeffs present or not
2713 * @param codingset set of VLC to decode data
2714 * @param mquant quantizer value for this macroblock
2716 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2717 int coded, int codingset, int mquant)
2719 GetBitContext *gb = &v->s.gb;
2720 MpegEncContext *s = &v->s;
2721 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2724 int16_t *ac_val, *ac_val2;
2726 int a_avail = v->a_avail, c_avail = v->c_avail;
2727 int use_pred = s->ac_pred;
2730 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2732 /* Get DC differential */
2734 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2736 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2739 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2743 if (dcdiff == 119 /* ESC index value */) {
2744 /* TODO: Optimize */
2745 if (mquant == 1) dcdiff = get_bits(gb, 10);
2746 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2747 else dcdiff = get_bits(gb, 8);
2750 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2751 else if (mquant == 2)
2752 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2759 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2762 /* Store the quantized DC coeff, used for prediction */
2764 block[0] = dcdiff * s->y_dc_scale;
2766 block[0] = dcdiff * s->c_dc_scale;
2772 /* check if AC is needed at all */
2773 if (!a_avail && !c_avail)
2775 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2778 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2780 if (dc_pred_dir) // left
2783 ac_val -= 16 * s->block_wrap[n];
2785 q1 = s->current_picture.f.qscale_table[mb_pos];
2786 if ( dc_pred_dir && c_avail && mb_pos)
2787 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2788 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2789 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2790 if ( dc_pred_dir && n == 1)
2792 if (!dc_pred_dir && n == 2)
2798 int last = 0, skip, value;
2799 const uint8_t *zz_table;
2803 if (!use_pred && v->fcm == ILACE_FRAME) {
2804 zz_table = v->zzi_8x8;
2806 if (!dc_pred_dir) // top
2807 zz_table = v->zz_8x8[2];
2809 zz_table = v->zz_8x8[3];
2812 if (v->fcm != ILACE_FRAME)
2813 zz_table = v->zz_8x8[1];
2815 zz_table = v->zzi_8x8;
2819 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2823 block[zz_table[i++]] = value;
2826 /* apply AC prediction if needed */
2828 /* scale predictors if needed*/
2829 if (q2 && q1 != q2) {
2830 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2831 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2834 return AVERROR_INVALIDDATA;
2835 if (dc_pred_dir) { // left
2836 for (k = 1; k < 8; k++)
2837 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2839 for (k = 1; k < 8; k++)
2840 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2843 if (dc_pred_dir) { //left
2844 for (k = 1; k < 8; k++)
2845 block[k << v->left_blk_sh] += ac_val[k];
2847 for (k = 1; k < 8; k++)
2848 block[k << v->top_blk_sh] += ac_val[k + 8];
2852 /* save AC coeffs for further prediction */
2853 for (k = 1; k < 8; k++) {
2854 ac_val2[k ] = block[k << v->left_blk_sh];
2855 ac_val2[k + 8] = block[k << v->top_blk_sh];
2858 /* scale AC coeffs */
2859 for (k = 1; k < 64; k++)
2863 block[k] += (block[k] < 0) ? -mquant : mquant;
2866 if (use_pred) i = 63;
2867 } else { // no AC coeffs
2870 memset(ac_val2, 0, 16 * 2);
2871 if (dc_pred_dir) { // left
2873 memcpy(ac_val2, ac_val, 8 * 2);
2874 if (q2 && q1 != q2) {
2875 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2876 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2878 return AVERROR_INVALIDDATA;
2879 for (k = 1; k < 8; k++)
2880 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2885 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2886 if (q2 && q1 != q2) {
2887 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2888 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2890 return AVERROR_INVALIDDATA;
2891 for (k = 1; k < 8; k++)
2892 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2897 /* apply AC prediction if needed */
2899 if (dc_pred_dir) { // left
2900 for (k = 1; k < 8; k++) {
2901 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2902 if (!v->pquantizer && block[k << v->left_blk_sh])
2903 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2906 for (k = 1; k < 8; k++) {
2907 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2908 if (!v->pquantizer && block[k << v->top_blk_sh])
2909 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2915 s->block_last_index[n] = i;
2920 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2921 * @param v VC1Context
2922 * @param block block to decode
2923 * @param[in] n subblock index
2924 * @param coded are AC coeffs present or not
2925 * @param mquant block quantizer
2926 * @param codingset set of VLC to decode data
2928 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2929 int coded, int mquant, int codingset)
2931 GetBitContext *gb = &v->s.gb;
2932 MpegEncContext *s = &v->s;
2933 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2936 int16_t *ac_val, *ac_val2;
2938 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2939 int a_avail = v->a_avail, c_avail = v->c_avail;
2940 int use_pred = s->ac_pred;
2944 s->dsp.clear_block(block);
2946 /* XXX: Guard against dumb values of mquant */
2947 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2949 /* Set DC scale - y and c use the same */
2950 s->y_dc_scale = s->y_dc_scale_table[mquant];
2951 s->c_dc_scale = s->c_dc_scale_table[mquant];
2953 /* Get DC differential */
2955 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2957 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2960 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2964 if (dcdiff == 119 /* ESC index value */) {
2965 /* TODO: Optimize */
2966 if (mquant == 1) dcdiff = get_bits(gb, 10);
2967 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2968 else dcdiff = get_bits(gb, 8);
2971 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2972 else if (mquant == 2)
2973 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2980 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2983 /* Store the quantized DC coeff, used for prediction */
2986 block[0] = dcdiff * s->y_dc_scale;
2988 block[0] = dcdiff * s->c_dc_scale;
2994 /* check if AC is needed at all and adjust direction if needed */
2995 if (!a_avail) dc_pred_dir = 1;
2996 if (!c_avail) dc_pred_dir = 0;
2997 if (!a_avail && !c_avail) use_pred = 0;
2998 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3001 scale = mquant * 2 + v->halfpq;
3003 if (dc_pred_dir) //left
3006 ac_val -= 16 * s->block_wrap[n];
3008 q1 = s->current_picture.f.qscale_table[mb_pos];
3009 if (dc_pred_dir && c_avail && mb_pos)
3010 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3011 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3012 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3013 if ( dc_pred_dir && n == 1)
3015 if (!dc_pred_dir && n == 2)
3017 if (n == 3) q2 = q1;
3020 int last = 0, skip, value;
3024 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3028 if (v->fcm == PROGRESSIVE)
3029 block[v->zz_8x8[0][i++]] = value;
3031 if (use_pred && (v->fcm == ILACE_FRAME)) {
3032 if (!dc_pred_dir) // top
3033 block[v->zz_8x8[2][i++]] = value;
3035 block[v->zz_8x8[3][i++]] = value;
3037 block[v->zzi_8x8[i++]] = value;
3042 /* apply AC prediction if needed */
3044 /* scale predictors if needed*/
3045 if (q2 && q1 != q2) {
3046 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3047 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3050 return AVERROR_INVALIDDATA;
3051 if (dc_pred_dir) { // left
3052 for (k = 1; k < 8; k++)
3053 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3055 for (k = 1; k < 8; k++)
3056 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3059 if (dc_pred_dir) { // left
3060 for (k = 1; k < 8; k++)
3061 block[k << v->left_blk_sh] += ac_val[k];
3063 for (k = 1; k < 8; k++)
3064 block[k << v->top_blk_sh] += ac_val[k + 8];
3068 /* save AC coeffs for further prediction */
3069 for (k = 1; k < 8; k++) {
3070 ac_val2[k ] = block[k << v->left_blk_sh];
3071 ac_val2[k + 8] = block[k << v->top_blk_sh];
3074 /* scale AC coeffs */
3075 for (k = 1; k < 64; k++)
3079 block[k] += (block[k] < 0) ? -mquant : mquant;
3082 if (use_pred) i = 63;
3083 } else { // no AC coeffs
3086 memset(ac_val2, 0, 16 * 2);
3087 if (dc_pred_dir) { // left
3089 memcpy(ac_val2, ac_val, 8 * 2);
3090 if (q2 && q1 != q2) {
3091 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3092 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3094 return AVERROR_INVALIDDATA;
3095 for (k = 1; k < 8; k++)
3096 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3101 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3102 if (q2 && q1 != q2) {
3103 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3104 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3106 return AVERROR_INVALIDDATA;
3107 for (k = 1; k < 8; k++)
3108 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3113 /* apply AC prediction if needed */
3115 if (dc_pred_dir) { // left
3116 for (k = 1; k < 8; k++) {
3117 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3118 if (!v->pquantizer && block[k << v->left_blk_sh])
3119 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3122 for (k = 1; k < 8; k++) {
3123 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3124 if (!v->pquantizer && block[k << v->top_blk_sh])
3125 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3131 s->block_last_index[n] = i;
3138 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3139 int mquant, int ttmb, int first_block,
3140 uint8_t *dst, int linesize, int skip_block,
3143 MpegEncContext *s = &v->s;
3144 GetBitContext *gb = &s->gb;
3147 int scale, off, idx, last, skip, value;
3148 int ttblk = ttmb & 7;
3151 s->dsp.clear_block(block);
3154 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)];
3156 if (ttblk == TT_4X4) {
3157 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3159 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3160 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3161 || (!v->res_rtm_flag && !first_block))) {
3162 subblkpat = decode012(gb);
3164 subblkpat ^= 3; // swap decoded pattern bits
3165 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3167 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3170 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3172 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3173 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3174 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3177 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3178 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3187 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3192 idx = v->zz_8x8[0][i++];
3194 idx = v->zzi_8x8[i++];
3195 block[idx] = value * scale;
3197 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3201 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3203 v->vc1dsp.vc1_inv_trans_8x8(block);
3204 s->dsp.add_pixels_clamped(block, dst, linesize);
3209 pat = ~subblkpat & 0xF;
3210 for (j = 0; j < 4; j++) {
3211 last = subblkpat & (1 << (3 - j));
3213 off = (j & 1) * 4 + (j & 2) * 16;
3215 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3220 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3222 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3223 block[idx + off] = value * scale;
3225 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3227 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3229 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3231 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3236 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3237 for (j = 0; j < 2; j++) {
3238 last = subblkpat & (1 << (1 - j));
3242 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3247 idx = v->zz_8x4[i++] + off;
3249 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3250 block[idx] = value * scale;
3252 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3254 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3256 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3258 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3263 pat = ~(subblkpat * 5) & 0xF;
3264 for (j = 0; j < 2; j++) {
3265 last = subblkpat & (1 << (1 - j));
3269 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3274 idx = v->zz_4x8[i++] + off;
3276 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3277 block[idx] = value * scale;
3279 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3281 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3283 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3285 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3291 *ttmb_out |= ttblk << (n * 4);
3295 /** @} */ // Macroblock group
3297 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3298 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3300 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3302 MpegEncContext *s = &v->s;
3303 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3304 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3305 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3306 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3307 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3310 if (block_num > 3) {
3311 dst = s->dest[block_num - 3];
3313 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3315 if (s->mb_y != s->end_mb_y || block_num < 2) {
3319 if (block_num > 3) {
3320 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3321 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3322 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3323 mv_stride = s->mb_stride;
3325 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3326 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3327 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3328 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3329 mv_stride = s->b8_stride;
3330 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3333 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3334 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3335 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3337 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3339 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3342 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3344 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3349 dst -= 4 * linesize;
3350 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3351 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3352 idx = (block_cbp | (block_cbp >> 2)) & 3;
3354 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3357 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3359 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3364 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3366 MpegEncContext *s = &v->s;
3367 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3368 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3369 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3370 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3371 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3374 if (block_num > 3) {
3375 dst = s->dest[block_num - 3] - 8 * linesize;
3377 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3380 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3383 if (block_num > 3) {
3384 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3385 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3386 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3388 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3389 : (mb_cbp >> ((block_num + 1) * 4));
3390 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3391 : (mb_is_intra >> ((block_num + 1) * 4));
3392 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3394 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3395 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3397 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3399 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3402 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3404 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3410 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3411 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3412 idx = (block_cbp | (block_cbp >> 1)) & 5;
3414 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3417 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3419 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3424 static void vc1_apply_p_loop_filter(VC1Context *v)
3426 MpegEncContext *s = &v->s;
3429 for (i = 0; i < 6; i++) {
3430 vc1_apply_p_v_loop_filter(v, i);
3433 /* V always precedes H, therefore we run H one MB before V;
3434 * at the end of a row, we catch up to complete the row */
3436 for (i = 0; i < 6; i++) {
3437 vc1_apply_p_h_loop_filter(v, i);
3439 if (s->mb_x == s->mb_width - 1) {
3441 ff_update_block_index(s);
3442 for (i = 0; i < 6; i++) {
3443 vc1_apply_p_h_loop_filter(v, i);
3449 /** Decode one P-frame MB
3451 static int vc1_decode_p_mb(VC1Context *v)
3453 MpegEncContext *s = &v->s;
3454 GetBitContext *gb = &s->gb;
3456 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3457 int cbp; /* cbp decoding stuff */
3458 int mqdiff, mquant; /* MB quantization */
3459 int ttmb = v->ttfrm; /* MB Transform type */
3461 int mb_has_coeffs = 1; /* last_flag */
3462 int dmv_x, dmv_y; /* Differential MV components */
3463 int index, index1; /* LUT indexes */
3464 int val, sign; /* temp values */
3465 int first_block = 1;
3467 int skipped, fourmv;
3468 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3470 mquant = v->pq; /* lossy initialization */
3472 if (v->mv_type_is_raw)
3473 fourmv = get_bits1(gb);
3475 fourmv = v->mv_type_mb_plane[mb_pos];
3477 skipped = get_bits1(gb);
3479 skipped = v->s.mbskip_table[mb_pos];
3481 if (!fourmv) { /* 1MV mode */
3483 GET_MVDATA(dmv_x, dmv_y);
3486 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3487 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3489 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3490 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3492 /* FIXME Set DC val for inter block ? */
3493 if (s->mb_intra && !mb_has_coeffs) {
3495 s->ac_pred = get_bits1(gb);
3497 } else if (mb_has_coeffs) {
3499 s->ac_pred = get_bits1(gb);
3500 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3506 s->current_picture.f.qscale_table[mb_pos] = mquant;
3508 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3509 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3510 VC1_TTMB_VLC_BITS, 2);
3511 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3513 for (i = 0; i < 6; i++) {
3514 s->dc_val[0][s->block_index[i]] = 0;
3516 val = ((cbp >> (5 - i)) & 1);
3517 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3518 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3520 /* check if prediction blocks A and C are available */
3521 v->a_avail = v->c_avail = 0;
3522 if (i == 2 || i == 3 || !s->first_slice_line)
3523 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3524 if (i == 1 || i == 3 || s->mb_x)
3525 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3527 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3528 (i & 4) ? v->codingset2 : v->codingset);
3529 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3531 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3533 for (j = 0; j < 64; j++)
3534 s->block[i][j] <<= 1;
3535 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3536 if (v->pq >= 9 && v->overlap) {
3538 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3540 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3542 block_cbp |= 0xF << (i << 2);
3543 block_intra |= 1 << i;
3545 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3546 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3547 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3548 block_cbp |= pat << (i << 2);
3549 if (!v->ttmbf && ttmb < 8)
3556 for (i = 0; i < 6; i++) {
3557 v->mb_type[0][s->block_index[i]] = 0;
3558 s->dc_val[0][s->block_index[i]] = 0;
3560 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3561 s->current_picture.f.qscale_table[mb_pos] = 0;
3562 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3565 } else { // 4MV mode
3566 if (!skipped /* unskipped MB */) {
3567 int intra_count = 0, coded_inter = 0;
3568 int is_intra[6], is_coded[6];
3570 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3571 for (i = 0; i < 6; i++) {
3572 val = ((cbp >> (5 - i)) & 1);
3573 s->dc_val[0][s->block_index[i]] = 0;
3580 GET_MVDATA(dmv_x, dmv_y);
3582 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3584 vc1_mc_4mv_luma(v, i, 0);
3585 intra_count += s->mb_intra;
3586 is_intra[i] = s->mb_intra;
3587 is_coded[i] = mb_has_coeffs;
3590 is_intra[i] = (intra_count >= 3);
3594 vc1_mc_4mv_chroma(v, 0);
3595 v->mb_type[0][s->block_index[i]] = is_intra[i];
3597 coded_inter = !is_intra[i] & is_coded[i];
3599 // if there are no coded blocks then don't do anything more
3601 if (!intra_count && !coded_inter)
3604 s->current_picture.f.qscale_table[mb_pos] = mquant;
3605 /* test if block is intra and has pred */
3608 for (i = 0; i < 6; i++)
3610 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3611 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3617 s->ac_pred = get_bits1(gb);
3621 if (!v->ttmbf && coded_inter)
3622 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3623 for (i = 0; i < 6; i++) {
3625 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3626 s->mb_intra = is_intra[i];
3628 /* check if prediction blocks A and C are available */
3629 v->a_avail = v->c_avail = 0;
3630 if (i == 2 || i == 3 || !s->first_slice_line)
3631 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3632 if (i == 1 || i == 3 || s->mb_x)
3633 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3635 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3636 (i & 4) ? v->codingset2 : v->codingset);
3637 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3639 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3641 for (j = 0; j < 64; j++)
3642 s->block[i][j] <<= 1;
3643 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3644 (i & 4) ? s->uvlinesize : s->linesize);
3645 if (v->pq >= 9 && v->overlap) {
3647 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3649 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3651 block_cbp |= 0xF << (i << 2);
3652 block_intra |= 1 << i;
3653 } else if (is_coded[i]) {
3654 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3655 first_block, s->dest[dst_idx] + off,
3656 (i & 4) ? s->uvlinesize : s->linesize,
3657 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3659 block_cbp |= pat << (i << 2);
3660 if (!v->ttmbf && ttmb < 8)
3665 } else { // skipped MB
3667 s->current_picture.f.qscale_table[mb_pos] = 0;
3668 for (i = 0; i < 6; i++) {
3669 v->mb_type[0][s->block_index[i]] = 0;
3670 s->dc_val[0][s->block_index[i]] = 0;
3672 for (i = 0; i < 4; i++) {
3673 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3674 vc1_mc_4mv_luma(v, i, 0);
3676 vc1_mc_4mv_chroma(v, 0);
3677 s->current_picture.f.qscale_table[mb_pos] = 0;
3681 v->cbp[s->mb_x] = block_cbp;
3682 v->ttblk[s->mb_x] = block_tt;
3683 v->is_intra[s->mb_x] = block_intra;
3688 /* Decode one macroblock in an interlaced frame p picture */
3690 static int vc1_decode_p_mb_intfr(VC1Context *v)
3692 MpegEncContext *s = &v->s;
3693 GetBitContext *gb = &s->gb;
3695 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3696 int cbp = 0; /* cbp decoding stuff */
3697 int mqdiff, mquant; /* MB quantization */
3698 int ttmb = v->ttfrm; /* MB Transform type */
3700 int mb_has_coeffs = 1; /* last_flag */
3701 int dmv_x, dmv_y; /* Differential MV components */
3702 int val; /* temp value */
3703 int first_block = 1;
3705 int skipped, fourmv = 0, twomv = 0;
3706 int block_cbp = 0, pat, block_tt = 0;
3707 int idx_mbmode = 0, mvbp;
3708 int stride_y, fieldtx;
3710 mquant = v->pq; /* Lossy initialization */
3713 skipped = get_bits1(gb);
3715 skipped = v->s.mbskip_table[mb_pos];
3717 if (v->fourmvswitch)
3718 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3720 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3721 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3722 /* store the motion vector type in a flag (useful later) */
3723 case MV_PMODE_INTFR_4MV:
3725 v->blk_mv_type[s->block_index[0]] = 0;
3726 v->blk_mv_type[s->block_index[1]] = 0;
3727 v->blk_mv_type[s->block_index[2]] = 0;
3728 v->blk_mv_type[s->block_index[3]] = 0;
3730 case MV_PMODE_INTFR_4MV_FIELD:
3732 v->blk_mv_type[s->block_index[0]] = 1;
3733 v->blk_mv_type[s->block_index[1]] = 1;
3734 v->blk_mv_type[s->block_index[2]] = 1;
3735 v->blk_mv_type[s->block_index[3]] = 1;
3737 case MV_PMODE_INTFR_2MV_FIELD:
3739 v->blk_mv_type[s->block_index[0]] = 1;
3740 v->blk_mv_type[s->block_index[1]] = 1;
3741 v->blk_mv_type[s->block_index[2]] = 1;
3742 v->blk_mv_type[s->block_index[3]] = 1;
3744 case MV_PMODE_INTFR_1MV:
3745 v->blk_mv_type[s->block_index[0]] = 0;
3746 v->blk_mv_type[s->block_index[1]] = 0;
3747 v->blk_mv_type[s->block_index[2]] = 0;
3748 v->blk_mv_type[s->block_index[3]] = 0;
3751 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3752 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3753 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3754 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3755 s->mb_intra = v->is_intra[s->mb_x] = 1;
3756 for (i = 0; i < 6; i++)
3757 v->mb_type[0][s->block_index[i]] = 1;
3758 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3759 mb_has_coeffs = get_bits1(gb);
3761 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3762 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3764 s->current_picture.f.qscale_table[mb_pos] = mquant;
3765 /* Set DC scale - y and c use the same (not sure if necessary here) */
3766 s->y_dc_scale = s->y_dc_scale_table[mquant];
3767 s->c_dc_scale = s->c_dc_scale_table[mquant];
3769 for (i = 0; i < 6; i++) {
3770 s->dc_val[0][s->block_index[i]] = 0;
3772 val = ((cbp >> (5 - i)) & 1);
3773 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3774 v->a_avail = v->c_avail = 0;
3775 if (i == 2 || i == 3 || !s->first_slice_line)
3776 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3777 if (i == 1 || i == 3 || s->mb_x)
3778 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3780 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3781 (i & 4) ? v->codingset2 : v->codingset);
3782 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3783 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3785 stride_y = s->linesize << fieldtx;
3786 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3788 stride_y = s->uvlinesize;
3791 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3795 } else { // inter MB
3796 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3798 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3799 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3800 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3802 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3803 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3804 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3807 s->mb_intra = v->is_intra[s->mb_x] = 0;
3808 for (i = 0; i < 6; i++)
3809 v->mb_type[0][s->block_index[i]] = 0;
3810 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3811 /* for all motion vector read MVDATA and motion compensate each block */
3815 for (i = 0; i < 6; i++) {
3818 val = ((mvbp >> (3 - i)) & 1);
3820 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3822 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3823 vc1_mc_4mv_luma(v, i, 0);
3824 } else if (i == 4) {
3825 vc1_mc_4mv_chroma4(v);
3832 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3834 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3835 vc1_mc_4mv_luma(v, 0, 0);
3836 vc1_mc_4mv_luma(v, 1, 0);
3839 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3841 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3842 vc1_mc_4mv_luma(v, 2, 0);
3843 vc1_mc_4mv_luma(v, 3, 0);
3844 vc1_mc_4mv_chroma4(v);
3846 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3849 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3851 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3855 GET_MQUANT(); // p. 227
3856 s->current_picture.f.qscale_table[mb_pos] = mquant;
3857 if (!v->ttmbf && cbp)
3858 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3859 for (i = 0; i < 6; i++) {
3860 s->dc_val[0][s->block_index[i]] = 0;
3862 val = ((cbp >> (5 - i)) & 1);
3864 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3866 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3868 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3869 first_block, s->dest[dst_idx] + off,
3870 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3871 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3872 block_cbp |= pat << (i << 2);
3873 if (!v->ttmbf && ttmb < 8)
3880 s->mb_intra = v->is_intra[s->mb_x] = 0;
3881 for (i = 0; i < 6; i++) {
3882 v->mb_type[0][s->block_index[i]] = 0;
3883 s->dc_val[0][s->block_index[i]] = 0;
3885 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3886 s->current_picture.f.qscale_table[mb_pos] = 0;
3887 v->blk_mv_type[s->block_index[0]] = 0;
3888 v->blk_mv_type[s->block_index[1]] = 0;
3889 v->blk_mv_type[s->block_index[2]] = 0;
3890 v->blk_mv_type[s->block_index[3]] = 0;
3891 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3894 if (s->mb_x == s->mb_width - 1)
3895 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3899 static int vc1_decode_p_mb_intfi(VC1Context *v)
3901 MpegEncContext *s = &v->s;
3902 GetBitContext *gb = &s->gb;
3904 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3905 int cbp = 0; /* cbp decoding stuff */
3906 int mqdiff, mquant; /* MB quantization */
3907 int ttmb = v->ttfrm; /* MB Transform type */
3909 int mb_has_coeffs = 1; /* last_flag */
3910 int dmv_x, dmv_y; /* Differential MV components */
3911 int val; /* temp values */
3912 int first_block = 1;
3915 int block_cbp = 0, pat, block_tt = 0;
3918 mquant = v->pq; /* Lossy initialization */
3920 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3921 if (idx_mbmode <= 1) { // intra MB
3922 s->mb_intra = v->is_intra[s->mb_x] = 1;
3923 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3924 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3925 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3927 s->current_picture.f.qscale_table[mb_pos] = mquant;
3928 /* Set DC scale - y and c use the same (not sure if necessary here) */
3929 s->y_dc_scale = s->y_dc_scale_table[mquant];
3930 s->c_dc_scale = s->c_dc_scale_table[mquant];
3931 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3932 mb_has_coeffs = idx_mbmode & 1;
3934 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3936 for (i = 0; i < 6; i++) {
3937 s->dc_val[0][s->block_index[i]] = 0;
3938 v->mb_type[0][s->block_index[i]] = 1;
3940 val = ((cbp >> (5 - i)) & 1);
3941 v->a_avail = v->c_avail = 0;
3942 if (i == 2 || i == 3 || !s->first_slice_line)
3943 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3944 if (i == 1 || i == 3 || s->mb_x)
3945 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3947 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3948 (i & 4) ? v->codingset2 : v->codingset);
3949 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3951 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3952 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3953 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3954 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3955 // TODO: loop filter
3958 s->mb_intra = v->is_intra[s->mb_x] = 0;
3959 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3960 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3961 if (idx_mbmode <= 5) { // 1-MV
3962 dmv_x = dmv_y = pred_flag = 0;
3963 if (idx_mbmode & 1) {
3964 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3966 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3968 mb_has_coeffs = !(idx_mbmode & 2);
3970 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3971 for (i = 0; i < 6; i++) {
3973 dmv_x = dmv_y = pred_flag = 0;
3974 val = ((v->fourmvbp >> (3 - i)) & 1);
3976 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3978 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3979 vc1_mc_4mv_luma(v, i, 0);
3981 vc1_mc_4mv_chroma(v, 0);
3983 mb_has_coeffs = idx_mbmode & 1;
3986 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3990 s->current_picture.f.qscale_table[mb_pos] = mquant;
3991 if (!v->ttmbf && cbp) {
3992 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3995 for (i = 0; i < 6; i++) {
3996 s->dc_val[0][s->block_index[i]] = 0;
3998 val = ((cbp >> (5 - i)) & 1);
3999 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4000 if (v->second_field)
4001 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4003 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4004 first_block, s->dest[dst_idx] + off,
4005 (i & 4) ? s->uvlinesize : s->linesize,
4006 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4008 block_cbp |= pat << (i << 2);
4009 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4014 if (s->mb_x == s->mb_width - 1)
4015 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4019 /** Decode one B-frame MB (in Main profile)
4021 static void vc1_decode_b_mb(VC1Context *v)
4023 MpegEncContext *s = &v->s;
4024 GetBitContext *gb = &s->gb;
4026 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4027 int cbp = 0; /* cbp decoding stuff */
4028 int mqdiff, mquant; /* MB quantization */
4029 int ttmb = v->ttfrm; /* MB Transform type */
4030 int mb_has_coeffs = 0; /* last_flag */
4031 int index, index1; /* LUT indexes */
4032 int val, sign; /* temp values */
4033 int first_block = 1;
4035 int skipped, direct;
4036 int dmv_x[2], dmv_y[2];
4037 int bmvtype = BMV_TYPE_BACKWARD;
4039 mquant = v->pq; /* lossy initialization */
4043 direct = get_bits1(gb);
4045 direct = v->direct_mb_plane[mb_pos];
4047 skipped = get_bits1(gb);
4049 skipped = v->s.mbskip_table[mb_pos];
4051 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4052 for (i = 0; i < 6; i++) {
4053 v->mb_type[0][s->block_index[i]] = 0;
4054 s->dc_val[0][s->block_index[i]] = 0;
4056 s->current_picture.f.qscale_table[mb_pos] = 0;
4060 GET_MVDATA(dmv_x[0], dmv_y[0]);
4061 dmv_x[1] = dmv_x[0];
4062 dmv_y[1] = dmv_y[0];
4064 if (skipped || !s->mb_intra) {
4065 bmvtype = decode012(gb);
4068 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4071 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4074 bmvtype = BMV_TYPE_INTERPOLATED;
4075 dmv_x[0] = dmv_y[0] = 0;
4079 for (i = 0; i < 6; i++)
4080 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4084 bmvtype = BMV_TYPE_INTERPOLATED;
4085 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4086 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4090 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4093 s->current_picture.f.qscale_table[mb_pos] = mquant;
4095 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4096 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4097 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4098 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4100 if (!mb_has_coeffs && !s->mb_intra) {
4101 /* no coded blocks - effectively skipped */
4102 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4103 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4106 if (s->mb_intra && !mb_has_coeffs) {
4108 s->current_picture.f.qscale_table[mb_pos] = mquant;
4109 s->ac_pred = get_bits1(gb);
4111 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4113 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4114 GET_MVDATA(dmv_x[0], dmv_y[0]);
4115 if (!mb_has_coeffs) {
4116 /* interpolated skipped block */
4117 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4118 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4122 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4124 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4127 s->ac_pred = get_bits1(gb);
4128 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4130 s->current_picture.f.qscale_table[mb_pos] = mquant;
4131 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4132 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4136 for (i = 0; i < 6; i++) {
4137 s->dc_val[0][s->block_index[i]] = 0;
4139 val = ((cbp >> (5 - i)) & 1);
4140 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4141 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4143 /* check if prediction blocks A and C are available */
4144 v->a_avail = v->c_avail = 0;
4145 if (i == 2 || i == 3 || !s->first_slice_line)
4146 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4147 if (i == 1 || i == 3 || s->mb_x)
4148 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4150 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4151 (i & 4) ? v->codingset2 : v->codingset);
4152 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4154 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4156 for (j = 0; j < 64; j++)
4157 s->block[i][j] <<= 1;
4158 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4160 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4161 first_block, s->dest[dst_idx] + off,
4162 (i & 4) ? s->uvlinesize : s->linesize,
4163 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4164 if (!v->ttmbf && ttmb < 8)
4171 /** Decode one B-frame MB (in interlaced field B picture)
4173 static void vc1_decode_b_mb_intfi(VC1Context *v)
4175 MpegEncContext *s = &v->s;
4176 GetBitContext *gb = &s->gb;
4178 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4179 int cbp = 0; /* cbp decoding stuff */
4180 int mqdiff, mquant; /* MB quantization */
4181 int ttmb = v->ttfrm; /* MB Transform type */
4182 int mb_has_coeffs = 0; /* last_flag */
4183 int val; /* temp value */
4184 int first_block = 1;
4187 int dmv_x[2], dmv_y[2], pred_flag[2];
4188 int bmvtype = BMV_TYPE_BACKWARD;
4189 int idx_mbmode, interpmvp;
4191 mquant = v->pq; /* Lossy initialization */
4194 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4195 if (idx_mbmode <= 1) { // intra MB
4196 s->mb_intra = v->is_intra[s->mb_x] = 1;
4197 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4198 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4199 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4201 s->current_picture.f.qscale_table[mb_pos] = mquant;
4202 /* Set DC scale - y and c use the same (not sure if necessary here) */
4203 s->y_dc_scale = s->y_dc_scale_table[mquant];
4204 s->c_dc_scale = s->c_dc_scale_table[mquant];
4205 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4206 mb_has_coeffs = idx_mbmode & 1;
4208 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4210 for (i = 0; i < 6; i++) {
4211 s->dc_val[0][s->block_index[i]] = 0;
4213 val = ((cbp >> (5 - i)) & 1);
4214 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4215 v->a_avail = v->c_avail = 0;
4216 if (i == 2 || i == 3 || !s->first_slice_line)
4217 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4218 if (i == 1 || i == 3 || s->mb_x)
4219 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4221 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4222 (i & 4) ? v->codingset2 : v->codingset);
4223 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4225 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4227 for (j = 0; j < 64; j++)
4228 s->block[i][j] <<= 1;
4229 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4230 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4231 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4232 // TODO: yet to perform loop filter
4235 s->mb_intra = v->is_intra[s->mb_x] = 0;
4236 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4237 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4239 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4241 fwd = v->forward_mb_plane[mb_pos];
4242 if (idx_mbmode <= 5) { // 1-MV
4243 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4244 pred_flag[0] = pred_flag[1] = 0;
4246 bmvtype = BMV_TYPE_FORWARD;
4248 bmvtype = decode012(gb);
4251 bmvtype = BMV_TYPE_BACKWARD;
4254 bmvtype = BMV_TYPE_DIRECT;
4257 bmvtype = BMV_TYPE_INTERPOLATED;
4258 interpmvp = get_bits1(gb);
4261 v->bmvtype = bmvtype;
4262 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4263 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4265 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4266 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4268 if (bmvtype == BMV_TYPE_DIRECT) {
4269 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4270 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4272 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4273 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4274 mb_has_coeffs = !(idx_mbmode & 2);
4277 bmvtype = BMV_TYPE_FORWARD;
4278 v->bmvtype = bmvtype;
4279 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4280 for (i = 0; i < 6; i++) {
4282 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4283 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4284 val = ((v->fourmvbp >> (3 - i)) & 1);
4286 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4287 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4288 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4290 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4291 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4293 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4295 mb_has_coeffs = idx_mbmode & 1;
4298 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4302 s->current_picture.f.qscale_table[mb_pos] = mquant;
4303 if (!v->ttmbf && cbp) {
4304 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4307 for (i = 0; i < 6; i++) {
4308 s->dc_val[0][s->block_index[i]] = 0;
4310 val = ((cbp >> (5 - i)) & 1);
4311 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4312 if (v->second_field)
4313 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4315 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4316 first_block, s->dest[dst_idx] + off,
4317 (i & 4) ? s->uvlinesize : s->linesize,
4318 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4319 if (!v->ttmbf && ttmb < 8)
4327 /** Decode blocks of I-frame
4329 static void vc1_decode_i_blocks(VC1Context *v)
4332 MpegEncContext *s = &v->s;
4337 /* select codingmode used for VLC tables selection */
4338 switch (v->y_ac_table_index) {
4340 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4343 v->codingset = CS_HIGH_MOT_INTRA;
4346 v->codingset = CS_MID_RATE_INTRA;
4350 switch (v->c_ac_table_index) {
4352 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4355 v->codingset2 = CS_HIGH_MOT_INTER;
4358 v->codingset2 = CS_MID_RATE_INTER;
4362 /* Set DC scale - y and c use the same */
4363 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4364 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4367 s->mb_x = s->mb_y = 0;
4369 s->first_slice_line = 1;
4370 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4372 ff_init_block_index(s);
4373 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4375 ff_update_block_index(s);
4376 dst[0] = s->dest[0];
4377 dst[1] = dst[0] + 8;
4378 dst[2] = s->dest[0] + s->linesize * 8;
4379 dst[3] = dst[2] + 8;
4380 dst[4] = s->dest[1];
4381 dst[5] = s->dest[2];
4382 s->dsp.clear_blocks(s->block[0]);
4383 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4384 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4385 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4386 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4387 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4389 // do actual MB decoding and displaying
4390 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4391 v->s.ac_pred = get_bits1(&v->s.gb);
4393 for (k = 0; k < 6; k++) {
4394 val = ((cbp >> (5 - k)) & 1);
4397 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4401 cbp |= val << (5 - k);
4403 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4405 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4407 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4408 if (v->pq >= 9 && v->overlap) {
4410 for (j = 0; j < 64; j++)
4411 s->block[k][j] <<= 1;
4412 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4415 for (j = 0; j < 64; j++)
4416 s->block[k][j] = (s->block[k][j] - 64) << 1;
4417 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4421 if (v->pq >= 9 && v->overlap) {
4423 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4424 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4425 if (!(s->flags & CODEC_FLAG_GRAY)) {
4426 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4427 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4430 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4431 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4432 if (!s->first_slice_line) {
4433 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4434 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4435 if (!(s->flags & CODEC_FLAG_GRAY)) {
4436 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4437 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4440 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4441 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4443 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4445 if (get_bits_count(&s->gb) > v->bits) {
4446 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4447 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4448 get_bits_count(&s->gb), v->bits);
4452 if (!v->s.loop_filter)
4453 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4455 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4457 s->first_slice_line = 0;
4459 if (v->s.loop_filter)
4460 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4462 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4463 * profile, these only differ are when decoding MSS2 rectangles. */
4464 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4467 /** Decode blocks of I-frame for advanced profile
4469 static void vc1_decode_i_blocks_adv(VC1Context *v)
4472 MpegEncContext *s = &v->s;
4478 GetBitContext *gb = &s->gb;
4480 /* select codingmode used for VLC tables selection */
4481 switch (v->y_ac_table_index) {
4483 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4486 v->codingset = CS_HIGH_MOT_INTRA;
4489 v->codingset = CS_MID_RATE_INTRA;
4493 switch (v->c_ac_table_index) {
4495 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4498 v->codingset2 = CS_HIGH_MOT_INTER;
4501 v->codingset2 = CS_MID_RATE_INTER;
4506 s->mb_x = s->mb_y = 0;
4508 s->first_slice_line = 1;
4509 s->mb_y = s->start_mb_y;
4510 if (s->start_mb_y) {
4512 ff_init_block_index(s);
4513 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4514 (1 + s->b8_stride) * sizeof(*s->coded_block));
4516 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4518 ff_init_block_index(s);
4519 for (;s->mb_x < s->mb_width; s->mb_x++) {
4520 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4521 ff_update_block_index(s);
4522 s->dsp.clear_blocks(block[0]);
4523 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4524 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4525 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4526 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4528 // do actual MB decoding and displaying
4529 if (v->fieldtx_is_raw)
4530 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4531 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4532 if ( v->acpred_is_raw)
4533 v->s.ac_pred = get_bits1(&v->s.gb);
4535 v->s.ac_pred = v->acpred_plane[mb_pos];
4537 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4538 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4542 s->current_picture.f.qscale_table[mb_pos] = mquant;
4543 /* Set DC scale - y and c use the same */
4544 s->y_dc_scale = s->y_dc_scale_table[mquant];
4545 s->c_dc_scale = s->c_dc_scale_table[mquant];
4547 for (k = 0; k < 6; k++) {
4548 val = ((cbp >> (5 - k)) & 1);
4551 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4555 cbp |= val << (5 - k);
4557 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4558 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4560 vc1_decode_i_block_adv(v, block[k], k, val,
4561 (k < 4) ? v->codingset : v->codingset2, mquant);
4563 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4565 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4568 vc1_smooth_overlap_filter_iblk(v);
4569 vc1_put_signed_blocks_clamped(v);
4570 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4572 if (get_bits_count(&s->gb) > v->bits) {
4573 // TODO: may need modification to handle slice coding
4574 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4575 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4576 get_bits_count(&s->gb), v->bits);
4580 if (!v->s.loop_filter)
4581 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4583 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4584 s->first_slice_line = 0;
4587 /* raw bottom MB row */
4589 ff_init_block_index(s);
4590 for (;s->mb_x < s->mb_width; s->mb_x++) {
4591 ff_update_block_index(s);
4592 vc1_put_signed_blocks_clamped(v);
4593 if (v->s.loop_filter)
4594 vc1_loop_filter_iblk_delayed(v, v->pq);
4596 if (v->s.loop_filter)
4597 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4598 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4599 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4602 static void vc1_decode_p_blocks(VC1Context *v)
4604 MpegEncContext *s = &v->s;
4605 int apply_loop_filter;
4607 /* select codingmode used for VLC tables selection */
4608 switch (v->c_ac_table_index) {
4610 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4613 v->codingset = CS_HIGH_MOT_INTRA;
4616 v->codingset = CS_MID_RATE_INTRA;
4620 switch (v->c_ac_table_index) {
4622 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4625 v->codingset2 = CS_HIGH_MOT_INTER;
4628 v->codingset2 = CS_MID_RATE_INTER;
4632 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4633 s->first_slice_line = 1;
4634 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4635 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4637 ff_init_block_index(s);
4638 for (; s->mb_x < s->mb_width; s->mb_x++) {
4639 ff_update_block_index(s);
4641 if (v->fcm == ILACE_FIELD)
4642 vc1_decode_p_mb_intfi(v);
4643 else if (v->fcm == ILACE_FRAME)
4644 vc1_decode_p_mb_intfr(v);
4645 else vc1_decode_p_mb(v);
4646 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4647 vc1_apply_p_loop_filter(v);
4648 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4649 // TODO: may need modification to handle slice coding
4650 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4651 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4652 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4656 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4657 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4658 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4659 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4660 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4661 s->first_slice_line = 0;
4663 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4665 ff_init_block_index(s);
4666 for (; s->mb_x < s->mb_width; s->mb_x++) {
4667 ff_update_block_index(s);
4668 vc1_apply_p_loop_filter(v);
4671 if (s->end_mb_y >= s->start_mb_y)
4672 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4673 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4674 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4677 static void vc1_decode_b_blocks(VC1Context *v)
4679 MpegEncContext *s = &v->s;
4681 /* select codingmode used for VLC tables selection */
4682 switch (v->c_ac_table_index) {
4684 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4687 v->codingset = CS_HIGH_MOT_INTRA;
4690 v->codingset = CS_MID_RATE_INTRA;
4694 switch (v->c_ac_table_index) {
4696 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4699 v->codingset2 = CS_HIGH_MOT_INTER;
4702 v->codingset2 = CS_MID_RATE_INTER;
4706 s->first_slice_line = 1;
4707 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4709 ff_init_block_index(s);
4710 for (; s->mb_x < s->mb_width; s->mb_x++) {
4711 ff_update_block_index(s);
4713 if (v->fcm == ILACE_FIELD)
4714 vc1_decode_b_mb_intfi(v);
4717 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4718 // TODO: may need modification to handle slice coding
4719 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4720 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4721 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4724 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4726 if (!v->s.loop_filter)
4727 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4729 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4730 s->first_slice_line = 0;
4732 if (v->s.loop_filter)
4733 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4734 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4735 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4738 static void vc1_decode_skip_blocks(VC1Context *v)
4740 MpegEncContext *s = &v->s;
4742 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4743 s->first_slice_line = 1;
4744 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4746 ff_init_block_index(s);
4747 ff_update_block_index(s);
4748 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4749 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4750 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4751 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4752 s->first_slice_line = 0;
4754 s->pict_type = AV_PICTURE_TYPE_P;
4757 void ff_vc1_decode_blocks(VC1Context *v)
4760 v->s.esc3_level_length = 0;
4762 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4765 v->left_blk_idx = -1;
4766 v->topleft_blk_idx = 1;
4768 switch (v->s.pict_type) {
4769 case AV_PICTURE_TYPE_I:
4770 if (v->profile == PROFILE_ADVANCED)
4771 vc1_decode_i_blocks_adv(v);
4773 vc1_decode_i_blocks(v);
4775 case AV_PICTURE_TYPE_P:
4776 if (v->p_frame_skipped)
4777 vc1_decode_skip_blocks(v);
4779 vc1_decode_p_blocks(v);
4781 case AV_PICTURE_TYPE_B:
4783 if (v->profile == PROFILE_ADVANCED)
4784 vc1_decode_i_blocks_adv(v);
4786 vc1_decode_i_blocks(v);
4788 vc1_decode_b_blocks(v);
4794 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4798 * Transform coefficients for both sprites in 16.16 fixed point format,
4799 * in the order they appear in the bitstream:
4801 * rotation 1 (unused)
4803 * rotation 2 (unused)
4810 int effect_type, effect_flag;
4811 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4812 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4815 static inline int get_fp_val(GetBitContext* gb)
4817 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4820 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4824 switch (get_bits(gb, 2)) {
4827 c[2] = get_fp_val(gb);
4831 c[0] = c[4] = get_fp_val(gb);
4832 c[2] = get_fp_val(gb);
4835 c[0] = get_fp_val(gb);
4836 c[2] = get_fp_val(gb);
4837 c[4] = get_fp_val(gb);
4840 c[0] = get_fp_val(gb);
4841 c[1] = get_fp_val(gb);
4842 c[2] = get_fp_val(gb);
4843 c[3] = get_fp_val(gb);
4844 c[4] = get_fp_val(gb);
4847 c[5] = get_fp_val(gb);
4849 c[6] = get_fp_val(gb);
4854 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4856 AVCodecContext *avctx = v->s.avctx;
4859 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4860 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4861 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4862 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4863 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4864 for (i = 0; i < 7; i++)
4865 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4866 sd->coefs[sprite][i] / (1<<16),
4867 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4868 av_log(avctx, AV_LOG_DEBUG, "\n");
4872 if (sd->effect_type = get_bits_long(gb, 30)) {
4873 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4875 vc1_sprite_parse_transform(gb, sd->effect_params1);
4878 vc1_sprite_parse_transform(gb, sd->effect_params1);
4879 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4882 for (i = 0; i < sd->effect_pcount1; i++)
4883 sd->effect_params1[i] = get_fp_val(gb);
4885 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4886 // effect 13 is simple alpha blending and matches the opacity above
4887 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4888 for (i = 0; i < sd->effect_pcount1; i++)
4889 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4890 sd->effect_params1[i] / (1 << 16),
4891 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4892 av_log(avctx, AV_LOG_DEBUG, "\n");
4895 sd->effect_pcount2 = get_bits(gb, 16);
4896 if (sd->effect_pcount2 > 10) {
4897 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4899 } else if (sd->effect_pcount2) {
4901 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4902 while (++i < sd->effect_pcount2) {
4903 sd->effect_params2[i] = get_fp_val(gb);
4904 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4905 sd->effect_params2[i] / (1 << 16),
4906 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4908 av_log(avctx, AV_LOG_DEBUG, "\n");
4911 if (sd->effect_flag = get_bits1(gb))
4912 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4914 if (get_bits_count(gb) >= gb->size_in_bits +
4915 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4916 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4917 if (get_bits_count(gb) < gb->size_in_bits - 8)
4918 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4921 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4923 int i, plane, row, sprite;
4924 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4925 uint8_t* src_h[2][2];
4926 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4928 MpegEncContext *s = &v->s;
4930 for (i = 0; i < 2; i++) {
4931 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4932 xadv[i] = sd->coefs[i][0];
4933 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4934 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4936 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4937 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4939 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4941 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4942 int width = v->output_width>>!!plane;
4944 for (row = 0; row < v->output_height>>!!plane; row++) {
4945 uint8_t *dst = v->sprite_output_frame.data[plane] +
4946 v->sprite_output_frame.linesize[plane] * row;
4948 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4949 uint8_t *iplane = s->current_picture.f.data[plane];
4950 int iline = s->current_picture.f.linesize[plane];
4951 int ycoord = yoff[sprite] + yadv[sprite] * row;
4952 int yline = ycoord >> 16;
4954 ysub[sprite] = ycoord & 0xFFFF;
4956 iplane = s->last_picture.f.data[plane];
4957 iline = s->last_picture.f.linesize[plane];
4959 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4960 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4961 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4963 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4965 if (sr_cache[sprite][0] != yline) {
4966 if (sr_cache[sprite][1] == yline) {
4967 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4968 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4970 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4971 sr_cache[sprite][0] = yline;
4974 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4975 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4976 iplane + next_line, xoff[sprite],
4977 xadv[sprite], width);
4978 sr_cache[sprite][1] = yline + 1;
4980 src_h[sprite][0] = v->sr_rows[sprite][0];
4981 src_h[sprite][1] = v->sr_rows[sprite][1];
4985 if (!v->two_sprites) {
4987 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4989 memcpy(dst, src_h[0][0], width);
4992 if (ysub[0] && ysub[1]) {
4993 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4994 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4995 } else if (ysub[0]) {
4996 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
4997 src_h[1][0], alpha, width);
4998 } else if (ysub[1]) {
4999 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5000 src_h[0][0], (1<<16)-1-alpha, width);
5002 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5008 for (i = 0; i < 2; i++) {
5018 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5020 MpegEncContext *s = &v->s;
5021 AVCodecContext *avctx = s->avctx;
5024 vc1_parse_sprites(v, gb, &sd);
5026 if (!s->current_picture.f.data[0]) {
5027 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5031 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5032 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5036 if (v->sprite_output_frame.data[0])
5037 avctx->release_buffer(avctx, &v->sprite_output_frame);
5039 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5040 v->sprite_output_frame.reference = 0;
5041 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
5042 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5046 vc1_draw_sprites(v, &sd);
5051 static void vc1_sprite_flush(AVCodecContext *avctx)
5053 VC1Context *v = avctx->priv_data;
5054 MpegEncContext *s = &v->s;
5055 AVFrame *f = &s->current_picture.f;
5058 /* Windows Media Image codecs have a convergence interval of two keyframes.
5059 Since we can't enforce it, clear to black the missing sprite. This is
5060 wrong but it looks better than doing nothing. */
5063 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5064 for (i = 0; i < v->sprite_height>>!!plane; i++)
5065 memset(f->data[plane] + i * f->linesize[plane],
5066 plane ? 128 : 0, f->linesize[plane]);
5071 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5073 MpegEncContext *s = &v->s;
5076 /* Allocate mb bitplanes */
5077 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5078 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5079 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5080 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5081 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5082 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5084 v->n_allocated_blks = s->mb_width + 2;
5085 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5086 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5087 v->cbp = v->cbp_base + s->mb_stride;
5088 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5089 v->ttblk = v->ttblk_base + s->mb_stride;
5090 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5091 v->is_intra = v->is_intra_base + s->mb_stride;
5092 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5093 v->luma_mv = v->luma_mv_base + s->mb_stride;
5095 /* allocate block type info in that way so it could be used with s->block_index[] */
5096 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5097 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5098 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5099 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5101 /* allocate memory to store block level MV info */
5102 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5103 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5104 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5105 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5106 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5107 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5108 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5109 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);
5110 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5111 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5112 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);
5114 /* Init coded blocks info */
5115 if (v->profile == PROFILE_ADVANCED) {
5116 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5118 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5122 ff_intrax8_common_init(&v->x8,s);
5124 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5125 for (i = 0; i < 4; i++)
5126 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5129 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5130 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5137 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5140 for (i = 0; i < 64; i++) {
5141 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5142 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5143 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5144 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5145 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5146 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5152 /** Initialize a VC1/WMV3 decoder
5153 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5154 * @todo TODO: Decypher remaining bits in extra_data
5156 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5158 VC1Context *v = avctx->priv_data;
5159 MpegEncContext *s = &v->s;
5162 /* save the container output size for WMImage */
5163 v->output_width = avctx->width;
5164 v->output_height = avctx->height;
5166 if (!avctx->extradata_size || !avctx->extradata)
5168 if (!(avctx->flags & CODEC_FLAG_GRAY))
5169 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5171 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5172 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5174 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5175 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5177 if (avctx->idct_algo == FF_IDCT_AUTO) {
5178 avctx->idct_algo = FF_IDCT_WMV2;
5181 if (ff_vc1_init_common(v) < 0)
5183 // ensure static VLC tables are initialized
5184 if (ff_msmpeg4_decode_init(avctx) < 0)
5186 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5188 // Hack to ensure the above functions will be called
5189 // again once we know all necessary settings.
5190 // That this is necessary might indicate a bug.
5191 ff_vc1_decode_end(avctx);
5192 ff_vc1dsp_init(&v->vc1dsp);
5194 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5197 // looks like WMV3 has a sequence header stored in the extradata
5198 // advanced sequence header may be before the first frame
5199 // the last byte of the extradata is a version number, 1 for the
5200 // samples we can decode
5202 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5204 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5207 count = avctx->extradata_size*8 - get_bits_count(&gb);
5209 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5210 count, get_bits(&gb, count));
5211 } else if (count < 0) {
5212 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5214 } else { // VC1/WVC1/WVP2
5215 const uint8_t *start = avctx->extradata;
5216 uint8_t *end = avctx->extradata + avctx->extradata_size;
5217 const uint8_t *next;
5218 int size, buf2_size;
5219 uint8_t *buf2 = NULL;
5220 int seq_initialized = 0, ep_initialized = 0;
5222 if (avctx->extradata_size < 16) {
5223 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5227 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5228 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5230 for (; next < end; start = next) {
5231 next = find_next_marker(start + 4, end);
5232 size = next - start - 4;
5235 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5236 init_get_bits(&gb, buf2, buf2_size * 8);
5237 switch (AV_RB32(start)) {
5238 case VC1_CODE_SEQHDR:
5239 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5243 seq_initialized = 1;
5245 case VC1_CODE_ENTRYPOINT:
5246 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5255 if (!seq_initialized || !ep_initialized) {
5256 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5259 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5262 avctx->profile = v->profile;
5263 if (v->profile == PROFILE_ADVANCED)
5264 avctx->level = v->level;
5266 avctx->has_b_frames = !!avctx->max_b_frames;
5268 s->mb_width = (avctx->coded_width + 15) >> 4;
5269 s->mb_height = (avctx->coded_height + 15) >> 4;
5271 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5272 ff_vc1_init_transposed_scantables(v);
5274 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5279 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5280 v->sprite_width = avctx->coded_width;
5281 v->sprite_height = avctx->coded_height;
5283 avctx->coded_width = avctx->width = v->output_width;
5284 avctx->coded_height = avctx->height = v->output_height;
5286 // prevent 16.16 overflows
5287 if (v->sprite_width > 1 << 14 ||
5288 v->sprite_height > 1 << 14 ||
5289 v->output_width > 1 << 14 ||
5290 v->output_height > 1 << 14) return -1;
5295 /** Close a VC1/WMV3 decoder
5296 * @warning Initial try at using MpegEncContext stuff
5298 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5300 VC1Context *v = avctx->priv_data;
5303 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5304 && v->sprite_output_frame.data[0])
5305 avctx->release_buffer(avctx, &v->sprite_output_frame);
5306 for (i = 0; i < 4; i++)
5307 av_freep(&v->sr_rows[i >> 1][i & 1]);
5308 av_freep(&v->hrd_rate);
5309 av_freep(&v->hrd_buffer);
5310 ff_MPV_common_end(&v->s);
5311 av_freep(&v->mv_type_mb_plane);
5312 av_freep(&v->direct_mb_plane);
5313 av_freep(&v->forward_mb_plane);
5314 av_freep(&v->fieldtx_plane);
5315 av_freep(&v->acpred_plane);
5316 av_freep(&v->over_flags_plane);
5317 av_freep(&v->mb_type_base);
5318 av_freep(&v->blk_mv_type_base);
5319 av_freep(&v->mv_f_base);
5320 av_freep(&v->mv_f_last_base);
5321 av_freep(&v->mv_f_next_base);
5322 av_freep(&v->block);
5323 av_freep(&v->cbp_base);
5324 av_freep(&v->ttblk_base);
5325 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5326 av_freep(&v->luma_mv_base);
5327 ff_intrax8_common_end(&v->x8);
5332 /** Decode a VC1/WMV3 frame
5333 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5335 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5336 int *data_size, AVPacket *avpkt)
5338 const uint8_t *buf = avpkt->data;
5339 int buf_size = avpkt->size, n_slices = 0, i;
5340 VC1Context *v = avctx->priv_data;
5341 MpegEncContext *s = &v->s;
5342 AVFrame *pict = data;
5343 uint8_t *buf2 = NULL;
5344 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5345 int mb_height, n_slices1=-1;
5350 } *slices = NULL, *tmp;
5352 v->second_field = 0;
5354 if(s->flags & CODEC_FLAG_LOW_DELAY)
5357 /* no supplementary picture */
5358 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5359 /* special case for last picture */
5360 if (s->low_delay == 0 && s->next_picture_ptr) {
5361 *pict = s->next_picture_ptr->f;
5362 s->next_picture_ptr = NULL;
5364 *data_size = sizeof(AVFrame);
5370 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5371 if (v->profile < PROFILE_ADVANCED)
5372 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5374 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5377 //for advanced profile we may need to parse and unescape data
5378 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5380 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5382 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5383 const uint8_t *start, *end, *next;
5387 for (start = buf, end = buf + buf_size; next < end; start = next) {
5388 next = find_next_marker(start + 4, end);
5389 size = next - start - 4;
5390 if (size <= 0) continue;
5391 switch (AV_RB32(start)) {
5392 case VC1_CODE_FRAME:
5393 if (avctx->hwaccel ||
5394 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5396 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5398 case VC1_CODE_FIELD: {
5400 if (avctx->hwaccel ||
5401 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5402 buf_start_second_field = start;
5403 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5407 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5408 if (!slices[n_slices].buf)
5410 buf_size3 = vc1_unescape_buffer(start + 4, size,
5411 slices[n_slices].buf);
5412 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5414 /* assuming that the field marker is at the exact middle,
5415 hope it's correct */
5416 slices[n_slices].mby_start = s->mb_height >> 1;
5417 n_slices1 = n_slices - 1; // index of the last slice of the first field
5421 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5422 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5423 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5424 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5426 case VC1_CODE_SLICE: {
5428 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5432 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5433 if (!slices[n_slices].buf)
5435 buf_size3 = vc1_unescape_buffer(start + 4, size,
5436 slices[n_slices].buf);
5437 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5439 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5445 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5446 const uint8_t *divider;
5449 divider = find_next_marker(buf, buf + buf_size);
5450 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5451 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5453 } else { // found field marker, unescape second field
5454 if (avctx->hwaccel ||
5455 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5456 buf_start_second_field = divider;
5457 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5461 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5462 if (!slices[n_slices].buf)
5464 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5465 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5467 slices[n_slices].mby_start = s->mb_height >> 1;
5468 n_slices1 = n_slices - 1;
5471 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5473 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5475 init_get_bits(&s->gb, buf2, buf_size2*8);
5477 init_get_bits(&s->gb, buf, buf_size*8);
5479 if (v->res_sprite) {
5480 v->new_sprite = !get_bits1(&s->gb);
5481 v->two_sprites = get_bits1(&s->gb);
5482 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5483 we're using the sprite compositor. These are intentionally kept separate
5484 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5485 the vc1 one for WVP2 */
5486 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5487 if (v->new_sprite) {
5488 // switch AVCodecContext parameters to those of the sprites
5489 avctx->width = avctx->coded_width = v->sprite_width;
5490 avctx->height = avctx->coded_height = v->sprite_height;
5497 if (s->context_initialized &&
5498 (s->width != avctx->coded_width ||
5499 s->height != avctx->coded_height)) {
5500 ff_vc1_decode_end(avctx);
5503 if (!s->context_initialized) {
5504 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5507 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5509 if (v->profile == PROFILE_ADVANCED) {
5510 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5512 s->h_edge_pos = avctx->coded_width;
5513 s->v_edge_pos = avctx->coded_height;
5517 /* We need to set current_picture_ptr before reading the header,
5518 * otherwise we cannot store anything in there. */
5519 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5520 int i = ff_find_unused_picture(s, 0);
5523 s->current_picture_ptr = &s->picture[i];
5526 // do parse frame header
5527 v->pic_header_flag = 0;
5528 v->first_pic_header_flag = 1;
5529 if (v->profile < PROFILE_ADVANCED) {
5530 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5534 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5538 v->first_pic_header_flag = 0;
5540 if (avctx->debug & FF_DEBUG_PICT_INFO)
5541 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5543 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5544 && s->pict_type != AV_PICTURE_TYPE_I) {
5545 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5549 if ((s->mb_height >> v->field_mode) == 0) {
5550 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5554 // process pulldown flags
5555 s->current_picture_ptr->f.repeat_pict = 0;
5556 // Pulldown flags are only valid when 'broadcast' has been set.
5557 // So ticks_per_frame will be 2
5560 s->current_picture_ptr->f.repeat_pict = 1;
5561 } else if (v->rptfrm) {
5563 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5566 // for skipping the frame
5567 s->current_picture.f.pict_type = s->pict_type;
5568 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5570 /* skip B-frames if we don't have reference frames */
5571 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
5574 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5575 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5576 avctx->skip_frame >= AVDISCARD_ALL) {
5580 if (s->next_p_frame_damaged) {
5581 if (s->pict_type == AV_PICTURE_TYPE_B)
5584 s->next_p_frame_damaged = 0;
5587 if (ff_MPV_frame_start(s, avctx) < 0) {
5591 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5592 v->s.current_picture_ptr->f.top_field_first = v->tff;
5594 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5595 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5597 if ((CONFIG_VC1_VDPAU_DECODER)
5598 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5599 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5600 else if (avctx->hwaccel) {
5601 if (v->field_mode && buf_start_second_field) {
5602 // decode first field
5603 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5604 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5606 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5608 if (avctx->hwaccel->end_frame(avctx) < 0)
5611 // decode second field
5612 s->gb = slices[n_slices1 + 1].gb;
5613 s->picture_structure = PICT_TOP_FIELD + v->tff;
5614 v->second_field = 1;
5615 v->pic_header_flag = 0;
5616 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5617 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5620 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
5622 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5624 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5626 if (avctx->hwaccel->end_frame(avctx) < 0)
5629 s->picture_structure = PICT_FRAME;
5630 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5632 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5634 if (avctx->hwaccel->end_frame(avctx) < 0)
5638 if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5639 goto err; // This codepath is still incomplete thus it is disabled
5641 ff_er_frame_start(s);
5643 v->bits = buf_size * 8;
5644 v->end_mb_x = s->mb_width;
5645 if (v->field_mode) {
5647 s->current_picture.f.linesize[0] <<= 1;
5648 s->current_picture.f.linesize[1] <<= 1;
5649 s->current_picture.f.linesize[2] <<= 1;
5651 s->uvlinesize <<= 1;
5652 tmp[0] = v->mv_f_last[0];
5653 tmp[1] = v->mv_f_last[1];
5654 v->mv_f_last[0] = v->mv_f_next[0];
5655 v->mv_f_last[1] = v->mv_f_next[1];
5656 v->mv_f_next[0] = v->mv_f[0];
5657 v->mv_f_next[1] = v->mv_f[1];
5658 v->mv_f[0] = tmp[0];
5659 v->mv_f[1] = tmp[1];
5661 mb_height = s->mb_height >> v->field_mode;
5662 for (i = 0; i <= n_slices; i++) {
5663 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5664 if (v->field_mode <= 0) {
5665 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5666 "picture boundary (%d >= %d)\n", i,
5667 slices[i - 1].mby_start, mb_height);
5670 v->second_field = 1;
5671 v->blocks_off = s->mb_width * s->mb_height << 1;
5672 v->mb_off = s->mb_stride * s->mb_height >> 1;
5674 v->second_field = 0;
5679 v->pic_header_flag = 0;
5680 if (v->field_mode && i == n_slices1 + 2) {
5681 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5682 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5685 } else if (get_bits1(&s->gb)) {
5686 v->pic_header_flag = 1;
5687 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5688 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5693 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5694 if (!v->field_mode || v->second_field)
5695 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5697 if (i >= n_slices) {
5698 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5701 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5703 if (s->end_mb_y <= s->start_mb_y) {
5704 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5707 ff_vc1_decode_blocks(v);
5709 s->gb = slices[i].gb;
5711 if (v->field_mode) {
5712 v->second_field = 0;
5713 if (s->pict_type == AV_PICTURE_TYPE_B) {
5714 memcpy(v->mv_f_base, v->mv_f_next_base,
5715 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5717 s->current_picture.f.linesize[0] >>= 1;
5718 s->current_picture.f.linesize[1] >>= 1;
5719 s->current_picture.f.linesize[2] >>= 1;
5721 s->uvlinesize >>= 1;
5723 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5724 get_bits_count(&s->gb), s->gb.size_in_bits);
5725 // if (get_bits_count(&s->gb) > buf_size * 8)
5727 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5733 ff_MPV_frame_end(s);
5735 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5737 avctx->width = avctx->coded_width = v->output_width;
5738 avctx->height = avctx->coded_height = v->output_height;
5739 if (avctx->skip_frame >= AVDISCARD_NONREF)
5741 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5742 if (vc1_decode_sprites(v, &s->gb))
5745 *pict = v->sprite_output_frame;
5746 *data_size = sizeof(AVFrame);
5748 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5749 *pict = s->current_picture_ptr->f;
5750 } else if (s->last_picture_ptr != NULL) {
5751 *pict = s->last_picture_ptr->f;
5753 if (s->last_picture_ptr || s->low_delay) {
5754 *data_size = sizeof(AVFrame);
5755 ff_print_debug_info(s, pict);
5761 for (i = 0; i < n_slices; i++)
5762 av_free(slices[i].buf);
5768 for (i = 0; i < n_slices; i++)
5769 av_free(slices[i].buf);
5775 static const AVProfile profiles[] = {
5776 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5777 { FF_PROFILE_VC1_MAIN, "Main" },
5778 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5779 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5780 { FF_PROFILE_UNKNOWN },
5783 AVCodec ff_vc1_decoder = {
5785 .type = AVMEDIA_TYPE_VIDEO,
5786 .id = AV_CODEC_ID_VC1,
5787 .priv_data_size = sizeof(VC1Context),
5788 .init = vc1_decode_init,
5789 .close = ff_vc1_decode_end,
5790 .decode = vc1_decode_frame,
5791 .flush = ff_mpeg_flush,
5792 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5793 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5794 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5795 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5798 #if CONFIG_WMV3_DECODER
5799 AVCodec ff_wmv3_decoder = {
5801 .type = AVMEDIA_TYPE_VIDEO,
5802 .id = AV_CODEC_ID_WMV3,
5803 .priv_data_size = sizeof(VC1Context),
5804 .init = vc1_decode_init,
5805 .close = ff_vc1_decode_end,
5806 .decode = vc1_decode_frame,
5807 .flush = ff_mpeg_flush,
5808 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5809 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5810 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5811 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5815 #if CONFIG_WMV3_VDPAU_DECODER
5816 AVCodec ff_wmv3_vdpau_decoder = {
5817 .name = "wmv3_vdpau",
5818 .type = AVMEDIA_TYPE_VIDEO,
5819 .id = AV_CODEC_ID_WMV3,
5820 .priv_data_size = sizeof(VC1Context),
5821 .init = vc1_decode_init,
5822 .close = ff_vc1_decode_end,
5823 .decode = vc1_decode_frame,
5824 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5825 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5826 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5827 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5831 #if CONFIG_VC1_VDPAU_DECODER
5832 AVCodec ff_vc1_vdpau_decoder = {
5833 .name = "vc1_vdpau",
5834 .type = AVMEDIA_TYPE_VIDEO,
5835 .id = AV_CODEC_ID_VC1,
5836 .priv_data_size = sizeof(VC1Context),
5837 .init = vc1_decode_init,
5838 .close = ff_vc1_decode_end,
5839 .decode = vc1_decode_frame,
5840 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5841 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5842 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5843 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5847 #if CONFIG_WMV3IMAGE_DECODER
5848 AVCodec ff_wmv3image_decoder = {
5849 .name = "wmv3image",
5850 .type = AVMEDIA_TYPE_VIDEO,
5851 .id = AV_CODEC_ID_WMV3IMAGE,
5852 .priv_data_size = sizeof(VC1Context),
5853 .init = vc1_decode_init,
5854 .close = ff_vc1_decode_end,
5855 .decode = vc1_decode_frame,
5856 .capabilities = CODEC_CAP_DR1,
5857 .flush = vc1_sprite_flush,
5858 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5859 .pix_fmts = ff_pixfmt_list_420
5863 #if CONFIG_VC1IMAGE_DECODER
5864 AVCodec ff_vc1image_decoder = {
5866 .type = AVMEDIA_TYPE_VIDEO,
5867 .id = AV_CODEC_ID_VC1IMAGE,
5868 .priv_data_size = sizeof(VC1Context),
5869 .init = vc1_decode_init,
5870 .close = ff_vc1_decode_end,
5871 .decode = vc1_decode_frame,
5872 .capabilities = CODEC_CAP_DR1,
5873 .flush = vc1_sprite_flush,
5874 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5875 .pix_fmts = ff_pixfmt_list_420