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->vdsp.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->vdsp.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->vdsp.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->vdsp.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->vdsp.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->vdsp.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))
984 v_edge_pos = (s->v_edge_pos >> 1) - 1;
986 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
988 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
989 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
990 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
991 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
992 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
993 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
994 s->h_edge_pos >> 1, v_edge_pos);
995 s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
996 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
997 s->h_edge_pos >> 1, v_edge_pos);
998 srcU = s->edge_emu_buffer;
999 srcV = s->edge_emu_buffer + 16;
1001 /* if we deal with intensity compensation we need to scale source blocks */
1002 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
1004 uint8_t *src, *src2;
1008 for (j = 0; j < 5; j++) {
1009 for (i = 0; i < 5; i++) {
1010 src[i] = v->lutuv[src[i]];
1011 src2[i] = v->lutuv[src2[i]];
1013 src += s->uvlinesize << 1;
1014 src2 += s->uvlinesize << 1;
1019 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1020 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1022 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]);
1023 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]);
1028 /***********************************************************************/
1030 * @name VC-1 Block-level functions
1031 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1037 * @brief Get macroblock-level quantizer scale
1039 #define GET_MQUANT() \
1040 if (v->dquantfrm) { \
1042 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1043 if (v->dqbilevel) { \
1044 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1046 mqdiff = get_bits(gb, 3); \
1048 mquant = v->pq + mqdiff; \
1050 mquant = get_bits(gb, 5); \
1053 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1054 edges = 1 << v->dqsbedge; \
1055 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1056 edges = (3 << v->dqsbedge) % 15; \
1057 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1059 if ((edges&1) && !s->mb_x) \
1060 mquant = v->altpq; \
1061 if ((edges&2) && s->first_slice_line) \
1062 mquant = v->altpq; \
1063 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1064 mquant = v->altpq; \
1065 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1066 mquant = v->altpq; \
1067 if (!mquant || mquant > 31) { \
1068 av_log(v->s.avctx, AV_LOG_ERROR, \
1069 "Overriding invalid mquant %d\n", mquant); \
1075 * @def GET_MVDATA(_dmv_x, _dmv_y)
1076 * @brief Get MV differentials
1077 * @see MVDATA decoding from 8.3.5.2, p(1)20
1078 * @param _dmv_x Horizontal differential for decoded MV
1079 * @param _dmv_y Vertical differential for decoded MV
1081 #define GET_MVDATA(_dmv_x, _dmv_y) \
1082 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1083 VC1_MV_DIFF_VLC_BITS, 2); \
1085 mb_has_coeffs = 1; \
1088 mb_has_coeffs = 0; \
1091 _dmv_x = _dmv_y = 0; \
1092 } else if (index == 35) { \
1093 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1094 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1095 } else if (index == 36) { \
1100 index1 = index % 6; \
1101 if (!s->quarter_sample && index1 == 5) val = 1; \
1103 if (size_table[index1] - val > 0) \
1104 val = get_bits(gb, size_table[index1] - val); \
1106 sign = 0 - (val&1); \
1107 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1109 index1 = index / 6; \
1110 if (!s->quarter_sample && index1 == 5) val = 1; \
1112 if (size_table[index1] - val > 0) \
1113 val = get_bits(gb, size_table[index1] - val); \
1115 sign = 0 - (val & 1); \
1116 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1119 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1120 int *dmv_y, int *pred_flag)
1123 int extend_x = 0, extend_y = 0;
1124 GetBitContext *gb = &v->s.gb;
1127 const int* offs_tab;
1130 bits = VC1_2REF_MVDATA_VLC_BITS;
1133 bits = VC1_1REF_MVDATA_VLC_BITS;
1136 switch (v->dmvrange) {
1144 extend_x = extend_y = 1;
1147 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1149 *dmv_x = get_bits(gb, v->k_x);
1150 *dmv_y = get_bits(gb, v->k_y);
1152 *pred_flag = *dmv_y & 1;
1153 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1157 av_assert0(index < esc);
1159 offs_tab = offset_table2;
1161 offs_tab = offset_table1;
1162 index1 = (index + 1) % 9;
1164 val = get_bits(gb, index1 + extend_x);
1165 sign = 0 -(val & 1);
1166 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1170 offs_tab = offset_table2;
1172 offs_tab = offset_table1;
1173 index1 = (index + 1) / 9;
1174 if (index1 > v->numref) {
1175 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1176 sign = 0 - (val & 1);
1177 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1181 *pred_flag = index1 & 1;
1185 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1187 int scaledvalue, refdist;
1188 int scalesame1, scalesame2;
1189 int scalezone1_x, zone1offset_x;
1190 int table_index = dir ^ v->second_field;
1192 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1193 refdist = v->refdist;
1195 refdist = dir ? v->brfd : v->frfd;
1198 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1199 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1200 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1201 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1206 if (FFABS(n) < scalezone1_x)
1207 scaledvalue = (n * scalesame1) >> 8;
1210 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1212 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1215 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1218 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1220 int scaledvalue, refdist;
1221 int scalesame1, scalesame2;
1222 int scalezone1_y, zone1offset_y;
1223 int table_index = dir ^ v->second_field;
1225 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1226 refdist = v->refdist;
1228 refdist = dir ? v->brfd : v->frfd;
1231 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1232 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1233 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1234 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1239 if (FFABS(n) < scalezone1_y)
1240 scaledvalue = (n * scalesame1) >> 8;
1243 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1245 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1249 if (v->cur_field_type && !v->ref_field_type[dir])
1250 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1252 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1255 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1257 int scalezone1_x, zone1offset_x;
1258 int scaleopp1, scaleopp2, brfd;
1261 brfd = FFMIN(v->brfd, 3);
1262 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1263 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1264 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1265 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1270 if (FFABS(n) < scalezone1_x)
1271 scaledvalue = (n * scaleopp1) >> 8;
1274 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1276 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1279 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1282 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1284 int scalezone1_y, zone1offset_y;
1285 int scaleopp1, scaleopp2, brfd;
1288 brfd = FFMIN(v->brfd, 3);
1289 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1290 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1291 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1292 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1297 if (FFABS(n) < scalezone1_y)
1298 scaledvalue = (n * scaleopp1) >> 8;
1301 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1303 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1306 if (v->cur_field_type && !v->ref_field_type[dir]) {
1307 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1309 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1313 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1316 int brfd, scalesame;
1317 int hpel = 1 - v->s.quarter_sample;
1320 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1322 n = scaleforsame_y(v, i, n, dir) << hpel;
1324 n = scaleforsame_x(v, n, dir) << hpel;
1327 brfd = FFMIN(v->brfd, 3);
1328 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1330 n = (n * scalesame >> 8) << hpel;
1334 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1337 int refdist, scaleopp;
1338 int hpel = 1 - v->s.quarter_sample;
1341 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1343 n = scaleforopp_y(v, n, dir) << hpel;
1345 n = scaleforopp_x(v, n) << hpel;
1348 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1349 refdist = FFMIN(v->refdist, 3);
1351 refdist = dir ? v->brfd : v->frfd;
1352 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1354 n = (n * scaleopp >> 8) << hpel;
1358 /** Predict and set motion vector
1360 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1361 int mv1, int r_x, int r_y, uint8_t* is_intra,
1362 int pred_flag, int dir)
1364 MpegEncContext *s = &v->s;
1365 int xy, wrap, off = 0;
1369 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1370 int opposite, a_f, b_f, c_f;
1371 int16_t field_predA[2];
1372 int16_t field_predB[2];
1373 int16_t field_predC[2];
1374 int a_valid, b_valid, c_valid;
1375 int hybridmv_thresh, y_bias = 0;
1377 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1378 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1382 /* scale MV difference to be quad-pel */
1383 dmv_x <<= 1 - s->quarter_sample;
1384 dmv_y <<= 1 - s->quarter_sample;
1386 wrap = s->b8_stride;
1387 xy = s->block_index[n];
1390 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1391 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1392 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1393 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1394 if (mv1) { /* duplicate motion data for 1-MV block */
1395 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1396 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1397 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1398 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1399 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1400 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1401 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1402 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1403 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1404 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1405 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1406 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1407 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1412 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1413 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1415 if (v->field_mode && mixedmv_pic)
1416 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1418 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1420 //in 4-MV mode different blocks have different B predictor position
1423 off = (s->mb_x > 0) ? -1 : 1;
1426 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1435 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1437 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1438 b_valid = a_valid && (s->mb_width > 1);
1439 c_valid = s->mb_x || (n == 1 || n == 3);
1440 if (v->field_mode) {
1441 a_valid = a_valid && !is_intra[xy - wrap];
1442 b_valid = b_valid && !is_intra[xy - wrap + off];
1443 c_valid = c_valid && !is_intra[xy - 1];
1447 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1448 num_oppfield += a_f;
1449 num_samefield += 1 - a_f;
1450 field_predA[0] = A[0];
1451 field_predA[1] = A[1];
1453 field_predA[0] = field_predA[1] = 0;
1457 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1458 num_oppfield += b_f;
1459 num_samefield += 1 - b_f;
1460 field_predB[0] = B[0];
1461 field_predB[1] = B[1];
1463 field_predB[0] = field_predB[1] = 0;
1467 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1468 num_oppfield += c_f;
1469 num_samefield += 1 - c_f;
1470 field_predC[0] = C[0];
1471 field_predC[1] = C[1];
1473 field_predC[0] = field_predC[1] = 0;
1477 if (v->field_mode) {
1479 // REFFIELD determines if the last field or the second-last field is
1480 // to be used as reference
1481 opposite = 1 - v->reffield;
1483 if (num_samefield <= num_oppfield)
1484 opposite = 1 - pred_flag;
1486 opposite = pred_flag;
1491 if (a_valid && !a_f) {
1492 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1493 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1495 if (b_valid && !b_f) {
1496 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1497 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1499 if (c_valid && !c_f) {
1500 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1501 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1503 v->mv_f[dir][xy + v->blocks_off] = 1;
1504 v->ref_field_type[dir] = !v->cur_field_type;
1506 if (a_valid && a_f) {
1507 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1508 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1510 if (b_valid && b_f) {
1511 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1512 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1514 if (c_valid && c_f) {
1515 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1516 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1518 v->mv_f[dir][xy + v->blocks_off] = 0;
1519 v->ref_field_type[dir] = v->cur_field_type;
1523 px = field_predA[0];
1524 py = field_predA[1];
1525 } else if (c_valid) {
1526 px = field_predC[0];
1527 py = field_predC[1];
1528 } else if (b_valid) {
1529 px = field_predB[0];
1530 py = field_predB[1];
1536 if (num_samefield + num_oppfield > 1) {
1537 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1538 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1541 /* Pullback MV as specified in 8.3.5.3.4 */
1542 if (!v->field_mode) {
1544 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1545 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1546 X = (s->mb_width << 6) - 4;
1547 Y = (s->mb_height << 6) - 4;
1549 if (qx + px < -60) px = -60 - qx;
1550 if (qy + py < -60) py = -60 - qy;
1552 if (qx + px < -28) px = -28 - qx;
1553 if (qy + py < -28) py = -28 - qy;
1555 if (qx + px > X) px = X - qx;
1556 if (qy + py > Y) py = Y - qy;
1559 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1560 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1561 hybridmv_thresh = 32;
1562 if (a_valid && c_valid) {
1563 if (is_intra[xy - wrap])
1564 sum = FFABS(px) + FFABS(py);
1566 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1567 if (sum > hybridmv_thresh) {
1568 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1569 px = field_predA[0];
1570 py = field_predA[1];
1572 px = field_predC[0];
1573 py = field_predC[1];
1576 if (is_intra[xy - 1])
1577 sum = FFABS(px) + FFABS(py);
1579 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1580 if (sum > hybridmv_thresh) {
1581 if (get_bits1(&s->gb)) {
1582 px = field_predA[0];
1583 py = field_predA[1];
1585 px = field_predC[0];
1586 py = field_predC[1];
1593 if (v->field_mode && v->numref)
1595 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1597 /* store MV using signed modulus of MV range defined in 4.11 */
1598 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;
1599 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;
1600 if (mv1) { /* duplicate motion data for 1-MV block */
1601 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];
1602 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];
1603 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];
1604 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];
1605 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];
1606 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];
1607 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1608 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];
1612 /** Predict and set motion vector for interlaced frame picture MBs
1614 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1615 int mvn, int r_x, int r_y, uint8_t* is_intra)
1617 MpegEncContext *s = &v->s;
1618 int xy, wrap, off = 0;
1619 int A[2], B[2], C[2];
1621 int a_valid = 0, b_valid = 0, c_valid = 0;
1622 int field_a, field_b, field_c; // 0: same, 1: opposit
1623 int total_valid, num_samefield, num_oppfield;
1624 int pos_c, pos_b, n_adj;
1626 wrap = s->b8_stride;
1627 xy = s->block_index[n];
1630 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1631 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1632 s->current_picture.f.motion_val[1][xy][0] = 0;
1633 s->current_picture.f.motion_val[1][xy][1] = 0;
1634 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1635 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1636 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1637 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1638 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1639 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1640 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1641 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1642 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1643 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1644 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1645 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1646 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1647 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1652 off = ((n == 0) || (n == 1)) ? 1 : -1;
1654 if (s->mb_x || (n == 1) || (n == 3)) {
1655 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1656 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1657 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1658 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1660 } else { // current block has frame mv and cand. has field MV (so average)
1661 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1662 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1663 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1664 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1667 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1673 /* Predict B and C */
1674 B[0] = B[1] = C[0] = C[1] = 0;
1675 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1676 if (!s->first_slice_line) {
1677 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1680 pos_b = s->block_index[n_adj] - 2 * wrap;
1681 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1682 n_adj = (n & 2) | (n & 1);
1684 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1685 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1686 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1687 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1688 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1691 if (s->mb_width > 1) {
1692 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1695 pos_c = s->block_index[2] - 2 * wrap + 2;
1696 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1699 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1700 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1701 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1702 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1703 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1705 if (s->mb_x == s->mb_width - 1) {
1706 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1709 pos_c = s->block_index[3] - 2 * wrap - 2;
1710 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1713 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1714 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1715 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1716 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1717 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1726 pos_b = s->block_index[1];
1728 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1729 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1730 pos_c = s->block_index[0];
1732 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1733 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1736 total_valid = a_valid + b_valid + c_valid;
1737 // check if predictor A is out of bounds
1738 if (!s->mb_x && !(n == 1 || n == 3)) {
1741 // check if predictor B is out of bounds
1742 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1743 B[0] = B[1] = C[0] = C[1] = 0;
1745 if (!v->blk_mv_type[xy]) {
1746 if (s->mb_width == 1) {
1750 if (total_valid >= 2) {
1751 px = mid_pred(A[0], B[0], C[0]);
1752 py = mid_pred(A[1], B[1], C[1]);
1753 } else if (total_valid) {
1754 if (a_valid) { px = A[0]; py = A[1]; }
1755 if (b_valid) { px = B[0]; py = B[1]; }
1756 if (c_valid) { px = C[0]; py = C[1]; }
1762 field_a = (A[1] & 4) ? 1 : 0;
1766 field_b = (B[1] & 4) ? 1 : 0;
1770 field_c = (C[1] & 4) ? 1 : 0;
1774 num_oppfield = field_a + field_b + field_c;
1775 num_samefield = total_valid - num_oppfield;
1776 if (total_valid == 3) {
1777 if ((num_samefield == 3) || (num_oppfield == 3)) {
1778 px = mid_pred(A[0], B[0], C[0]);
1779 py = mid_pred(A[1], B[1], C[1]);
1780 } else if (num_samefield >= num_oppfield) {
1781 /* take one MV from same field set depending on priority
1782 the check for B may not be necessary */
1783 px = !field_a ? A[0] : B[0];
1784 py = !field_a ? A[1] : B[1];
1786 px = field_a ? A[0] : B[0];
1787 py = field_a ? A[1] : B[1];
1789 } else if (total_valid == 2) {
1790 if (num_samefield >= num_oppfield) {
1791 if (!field_a && a_valid) {
1794 } else if (!field_b && b_valid) {
1797 } else if (c_valid) {
1802 if (field_a && a_valid) {
1805 } else if (field_b && b_valid) {
1808 } else if (c_valid) {
1813 } else if (total_valid == 1) {
1814 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1815 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1820 /* store MV using signed modulus of MV range defined in 4.11 */
1821 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;
1822 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;
1823 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1824 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1825 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1826 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1827 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1828 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1829 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1830 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1831 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1832 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1833 s->mv[0][n + 1][0] = s->mv[0][n][0];
1834 s->mv[0][n + 1][1] = s->mv[0][n][1];
1838 /** Motion compensation for direct or interpolated blocks in B-frames
1840 static void vc1_interp_mc(VC1Context *v)
1842 MpegEncContext *s = &v->s;
1843 DSPContext *dsp = &v->s.dsp;
1844 uint8_t *srcY, *srcU, *srcV;
1845 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1847 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1849 if (!v->field_mode && !v->s.next_picture.f.data[0])
1852 mx = s->mv[1][0][0];
1853 my = s->mv[1][0][1];
1854 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1855 uvmy = (my + ((my & 3) == 3)) >> 1;
1856 if (v->field_mode) {
1857 if (v->cur_field_type != v->ref_field_type[1])
1858 my = my - 2 + 4 * v->cur_field_type;
1859 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1862 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1863 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1865 srcY = s->next_picture.f.data[0];
1866 srcU = s->next_picture.f.data[1];
1867 srcV = s->next_picture.f.data[2];
1869 src_x = s->mb_x * 16 + (mx >> 2);
1870 src_y = s->mb_y * 16 + (my >> 2);
1871 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1872 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1874 if (v->profile != PROFILE_ADVANCED) {
1875 src_x = av_clip( src_x, -16, s->mb_width * 16);
1876 src_y = av_clip( src_y, -16, s->mb_height * 16);
1877 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1878 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1880 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1881 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1882 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1883 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1886 srcY += src_y * s->linesize + src_x;
1887 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1888 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1890 if (v->field_mode && v->ref_field_type[1]) {
1891 srcY += s->current_picture_ptr->f.linesize[0];
1892 srcU += s->current_picture_ptr->f.linesize[1];
1893 srcV += s->current_picture_ptr->f.linesize[2];
1896 /* for grayscale we should not try to read from unknown area */
1897 if (s->flags & CODEC_FLAG_GRAY) {
1898 srcU = s->edge_emu_buffer + 18 * s->linesize;
1899 srcV = s->edge_emu_buffer + 18 * s->linesize;
1902 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1903 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1904 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1905 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1907 srcY -= s->mspel * (1 + s->linesize);
1908 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1909 17 + s->mspel * 2, 17 + s->mspel * 2,
1910 src_x - s->mspel, src_y - s->mspel,
1911 s->h_edge_pos, v_edge_pos);
1912 srcY = s->edge_emu_buffer;
1913 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1914 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1915 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1916 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1919 /* if we deal with range reduction we need to scale source blocks */
1920 if (v->rangeredfrm) {
1922 uint8_t *src, *src2;
1925 for (j = 0; j < 17 + s->mspel * 2; j++) {
1926 for (i = 0; i < 17 + s->mspel * 2; i++)
1927 src[i] = ((src[i] - 128) >> 1) + 128;
1932 for (j = 0; j < 9; j++) {
1933 for (i = 0; i < 9; i++) {
1934 src[i] = ((src[i] - 128) >> 1) + 128;
1935 src2[i] = ((src2[i] - 128) >> 1) + 128;
1937 src += s->uvlinesize;
1938 src2 += s->uvlinesize;
1941 srcY += s->mspel * (1 + s->linesize);
1944 if (v->field_mode && v->second_field) {
1945 off = s->current_picture_ptr->f.linesize[0];
1946 off_uv = s->current_picture_ptr->f.linesize[1];
1953 dxy = ((my & 3) << 2) | (mx & 3);
1954 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1955 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1956 srcY += s->linesize * 8;
1957 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1958 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1960 dxy = (my & 2) | ((mx & 2) >> 1);
1963 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1965 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1968 if (s->flags & CODEC_FLAG_GRAY) return;
1969 /* Chroma MC always uses qpel blilinear */
1970 uvmx = (uvmx & 3) << 1;
1971 uvmy = (uvmy & 3) << 1;
1973 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1974 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1976 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1977 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1981 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1985 #if B_FRACTION_DEN==256
1989 return 2 * ((value * n + 255) >> 9);
1990 return (value * n + 128) >> 8;
1993 n -= B_FRACTION_DEN;
1995 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1996 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2000 /** Reconstruct motion vector for B-frame and do motion compensation
2002 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2003 int direct, int mode)
2006 v->mv_mode2 = v->mv_mode;
2007 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2013 v->mv_mode = v->mv_mode2;
2016 if (mode == BMV_TYPE_INTERPOLATED) {
2020 v->mv_mode = v->mv_mode2;
2024 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2025 v->mv_mode = v->mv_mode2;
2026 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2028 v->mv_mode = v->mv_mode2;
2031 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2032 int direct, int mvtype)
2034 MpegEncContext *s = &v->s;
2035 int xy, wrap, off = 0;
2040 const uint8_t *is_intra = v->mb_type[0];
2044 /* scale MV difference to be quad-pel */
2045 dmv_x[0] <<= 1 - s->quarter_sample;
2046 dmv_y[0] <<= 1 - s->quarter_sample;
2047 dmv_x[1] <<= 1 - s->quarter_sample;
2048 dmv_y[1] <<= 1 - s->quarter_sample;
2050 wrap = s->b8_stride;
2051 xy = s->block_index[0];
2054 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2055 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2056 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2057 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2060 if (!v->field_mode) {
2061 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2062 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2063 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2064 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2066 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2067 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));
2068 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));
2069 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));
2070 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));
2073 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2074 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2075 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2076 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2080 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2081 C = s->current_picture.f.motion_val[0][xy - 2];
2082 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2083 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2084 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2086 if (!s->mb_x) C[0] = C[1] = 0;
2087 if (!s->first_slice_line) { // predictor A is not out of bounds
2088 if (s->mb_width == 1) {
2092 px = mid_pred(A[0], B[0], C[0]);
2093 py = mid_pred(A[1], B[1], C[1]);
2095 } else if (s->mb_x) { // predictor C is not out of bounds
2101 /* Pullback MV as specified in 8.3.5.3.4 */
2104 if (v->profile < PROFILE_ADVANCED) {
2105 qx = (s->mb_x << 5);
2106 qy = (s->mb_y << 5);
2107 X = (s->mb_width << 5) - 4;
2108 Y = (s->mb_height << 5) - 4;
2109 if (qx + px < -28) px = -28 - qx;
2110 if (qy + py < -28) py = -28 - qy;
2111 if (qx + px > X) px = X - qx;
2112 if (qy + py > Y) py = Y - qy;
2114 qx = (s->mb_x << 6);
2115 qy = (s->mb_y << 6);
2116 X = (s->mb_width << 6) - 4;
2117 Y = (s->mb_height << 6) - 4;
2118 if (qx + px < -60) px = -60 - qx;
2119 if (qy + py < -60) py = -60 - qy;
2120 if (qx + px > X) px = X - qx;
2121 if (qy + py > Y) py = Y - qy;
2124 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2125 if (0 && !s->first_slice_line && s->mb_x) {
2126 if (is_intra[xy - wrap])
2127 sum = FFABS(px) + FFABS(py);
2129 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2131 if (get_bits1(&s->gb)) {
2139 if (is_intra[xy - 2])
2140 sum = FFABS(px) + FFABS(py);
2142 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2144 if (get_bits1(&s->gb)) {
2154 /* store MV using signed modulus of MV range defined in 4.11 */
2155 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2156 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2158 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2159 C = s->current_picture.f.motion_val[1][xy - 2];
2160 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2161 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2162 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2166 if (!s->first_slice_line) { // predictor A is not out of bounds
2167 if (s->mb_width == 1) {
2171 px = mid_pred(A[0], B[0], C[0]);
2172 py = mid_pred(A[1], B[1], C[1]);
2174 } else if (s->mb_x) { // predictor C is not out of bounds
2180 /* Pullback MV as specified in 8.3.5.3.4 */
2183 if (v->profile < PROFILE_ADVANCED) {
2184 qx = (s->mb_x << 5);
2185 qy = (s->mb_y << 5);
2186 X = (s->mb_width << 5) - 4;
2187 Y = (s->mb_height << 5) - 4;
2188 if (qx + px < -28) px = -28 - qx;
2189 if (qy + py < -28) py = -28 - qy;
2190 if (qx + px > X) px = X - qx;
2191 if (qy + py > Y) py = Y - qy;
2193 qx = (s->mb_x << 6);
2194 qy = (s->mb_y << 6);
2195 X = (s->mb_width << 6) - 4;
2196 Y = (s->mb_height << 6) - 4;
2197 if (qx + px < -60) px = -60 - qx;
2198 if (qy + py < -60) py = -60 - qy;
2199 if (qx + px > X) px = X - qx;
2200 if (qy + py > Y) py = Y - qy;
2203 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2204 if (0 && !s->first_slice_line && s->mb_x) {
2205 if (is_intra[xy - wrap])
2206 sum = FFABS(px) + FFABS(py);
2208 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2210 if (get_bits1(&s->gb)) {
2218 if (is_intra[xy - 2])
2219 sum = FFABS(px) + FFABS(py);
2221 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2223 if (get_bits1(&s->gb)) {
2233 /* store MV using signed modulus of MV range defined in 4.11 */
2235 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2236 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2238 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2239 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2240 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2241 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2244 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2246 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2247 MpegEncContext *s = &v->s;
2248 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2250 if (v->bmvtype == BMV_TYPE_DIRECT) {
2251 int total_opp, k, f;
2252 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2253 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2254 v->bfraction, 0, s->quarter_sample);
2255 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2256 v->bfraction, 0, s->quarter_sample);
2257 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2258 v->bfraction, 1, s->quarter_sample);
2259 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2260 v->bfraction, 1, s->quarter_sample);
2262 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2263 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2264 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2265 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2266 f = (total_opp > 2) ? 1 : 0;
2268 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2269 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2272 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2273 for (k = 0; k < 4; k++) {
2274 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2275 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2276 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2277 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2278 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2279 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2283 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2284 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);
2285 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);
2288 if (dir) { // backward
2289 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);
2290 if (n == 3 || mv1) {
2291 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2294 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);
2295 if (n == 3 || mv1) {
2296 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2301 /** Get predicted DC value for I-frames only
2302 * prediction dir: left=0, top=1
2303 * @param s MpegEncContext
2304 * @param overlap flag indicating that overlap filtering is used
2305 * @param pq integer part of picture quantizer
2306 * @param[in] n block index in the current MB
2307 * @param dc_val_ptr Pointer to DC predictor
2308 * @param dir_ptr Prediction direction for use in AC prediction
2310 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2311 int16_t **dc_val_ptr, int *dir_ptr)
2313 int a, b, c, wrap, pred, scale;
2315 static const uint16_t dcpred[32] = {
2316 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2317 114, 102, 93, 85, 79, 73, 68, 64,
2318 60, 57, 54, 51, 49, 47, 45, 43,
2319 41, 39, 38, 37, 35, 34, 33
2322 /* find prediction - wmv3_dc_scale always used here in fact */
2323 if (n < 4) scale = s->y_dc_scale;
2324 else scale = s->c_dc_scale;
2326 wrap = s->block_wrap[n];
2327 dc_val = s->dc_val[0] + s->block_index[n];
2333 b = dc_val[ - 1 - wrap];
2334 a = dc_val[ - wrap];
2336 if (pq < 9 || !overlap) {
2337 /* Set outer values */
2338 if (s->first_slice_line && (n != 2 && n != 3))
2339 b = a = dcpred[scale];
2340 if (s->mb_x == 0 && (n != 1 && n != 3))
2341 b = c = dcpred[scale];
2343 /* Set outer values */
2344 if (s->first_slice_line && (n != 2 && n != 3))
2346 if (s->mb_x == 0 && (n != 1 && n != 3))
2350 if (abs(a - b) <= abs(b - c)) {
2352 *dir_ptr = 1; // left
2355 *dir_ptr = 0; // top
2358 /* update predictor */
2359 *dc_val_ptr = &dc_val[0];
2364 /** Get predicted DC value
2365 * prediction dir: left=0, top=1
2366 * @param s MpegEncContext
2367 * @param overlap flag indicating that overlap filtering is used
2368 * @param pq integer part of picture quantizer
2369 * @param[in] n block index in the current MB
2370 * @param a_avail flag indicating top block availability
2371 * @param c_avail flag indicating left block availability
2372 * @param dc_val_ptr Pointer to DC predictor
2373 * @param dir_ptr Prediction direction for use in AC prediction
2375 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2376 int a_avail, int c_avail,
2377 int16_t **dc_val_ptr, int *dir_ptr)
2379 int a, b, c, wrap, pred;
2381 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2385 wrap = s->block_wrap[n];
2386 dc_val = s->dc_val[0] + s->block_index[n];
2392 b = dc_val[ - 1 - wrap];
2393 a = dc_val[ - wrap];
2394 /* scale predictors if needed */
2395 q1 = s->current_picture.f.qscale_table[mb_pos];
2396 dqscale_index = s->y_dc_scale_table[q1] - 1;
2397 if (dqscale_index < 0)
2399 if (c_avail && (n != 1 && n != 3)) {
2400 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2402 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2404 if (a_avail && (n != 2 && n != 3)) {
2405 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2407 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2409 if (a_avail && c_avail && (n != 3)) {
2414 off -= s->mb_stride;
2415 q2 = s->current_picture.f.qscale_table[off];
2417 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2420 if (a_avail && c_avail) {
2421 if (abs(a - b) <= abs(b - c)) {
2423 *dir_ptr = 1; // left
2426 *dir_ptr = 0; // top
2428 } else if (a_avail) {
2430 *dir_ptr = 0; // top
2431 } else if (c_avail) {
2433 *dir_ptr = 1; // left
2436 *dir_ptr = 1; // left
2439 /* update predictor */
2440 *dc_val_ptr = &dc_val[0];
2444 /** @} */ // Block group
2447 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2448 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2452 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2453 uint8_t **coded_block_ptr)
2455 int xy, wrap, pred, a, b, c;
2457 xy = s->block_index[n];
2458 wrap = s->b8_stride;
2463 a = s->coded_block[xy - 1 ];
2464 b = s->coded_block[xy - 1 - wrap];
2465 c = s->coded_block[xy - wrap];
2474 *coded_block_ptr = &s->coded_block[xy];
2480 * Decode one AC coefficient
2481 * @param v The VC1 context
2482 * @param last Last coefficient
2483 * @param skip How much zero coefficients to skip
2484 * @param value Decoded AC coefficient value
2485 * @param codingset set of VLC to decode data
2488 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2489 int *value, int codingset)
2491 GetBitContext *gb = &v->s.gb;
2492 int index, escape, run = 0, level = 0, lst = 0;
2494 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2495 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2496 run = vc1_index_decode_table[codingset][index][0];
2497 level = vc1_index_decode_table[codingset][index][1];
2498 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2502 escape = decode210(gb);
2504 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2505 run = vc1_index_decode_table[codingset][index][0];
2506 level = vc1_index_decode_table[codingset][index][1];
2507 lst = index >= vc1_last_decode_table[codingset];
2510 level += vc1_last_delta_level_table[codingset][run];
2512 level += vc1_delta_level_table[codingset][run];
2515 run += vc1_last_delta_run_table[codingset][level] + 1;
2517 run += vc1_delta_run_table[codingset][level] + 1;
2523 lst = get_bits1(gb);
2524 if (v->s.esc3_level_length == 0) {
2525 if (v->pq < 8 || v->dquantfrm) { // table 59
2526 v->s.esc3_level_length = get_bits(gb, 3);
2527 if (!v->s.esc3_level_length)
2528 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2529 } else { // table 60
2530 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2532 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2534 run = get_bits(gb, v->s.esc3_run_length);
2535 sign = get_bits1(gb);
2536 level = get_bits(gb, v->s.esc3_level_length);
2547 /** Decode intra block in intra frames - should be faster than decode_intra_block
2548 * @param v VC1Context
2549 * @param block block to decode
2550 * @param[in] n subblock index
2551 * @param coded are AC coeffs present or not
2552 * @param codingset set of VLC to decode data
2554 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2555 int coded, int codingset)
2557 GetBitContext *gb = &v->s.gb;
2558 MpegEncContext *s = &v->s;
2559 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2562 int16_t *ac_val, *ac_val2;
2565 /* Get DC differential */
2567 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2569 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2572 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2576 if (dcdiff == 119 /* ESC index value */) {
2577 /* TODO: Optimize */
2578 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2579 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2580 else dcdiff = get_bits(gb, 8);
2583 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2584 else if (v->pq == 2)
2585 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2592 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2595 /* Store the quantized DC coeff, used for prediction */
2597 block[0] = dcdiff * s->y_dc_scale;
2599 block[0] = dcdiff * s->c_dc_scale;
2610 int last = 0, skip, value;
2611 const uint8_t *zz_table;
2615 scale = v->pq * 2 + v->halfpq;
2619 zz_table = v->zz_8x8[2];
2621 zz_table = v->zz_8x8[3];
2623 zz_table = v->zz_8x8[1];
2625 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2627 if (dc_pred_dir) // left
2630 ac_val -= 16 * s->block_wrap[n];
2633 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2637 block[zz_table[i++]] = value;
2640 /* apply AC prediction if needed */
2642 if (dc_pred_dir) { // left
2643 for (k = 1; k < 8; k++)
2644 block[k << v->left_blk_sh] += ac_val[k];
2646 for (k = 1; k < 8; k++)
2647 block[k << v->top_blk_sh] += ac_val[k + 8];
2650 /* save AC coeffs for further prediction */
2651 for (k = 1; k < 8; k++) {
2652 ac_val2[k] = block[k << v->left_blk_sh];
2653 ac_val2[k + 8] = block[k << v->top_blk_sh];
2656 /* scale AC coeffs */
2657 for (k = 1; k < 64; k++)
2661 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2664 if (s->ac_pred) i = 63;
2670 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2674 scale = v->pq * 2 + v->halfpq;
2675 memset(ac_val2, 0, 16 * 2);
2676 if (dc_pred_dir) { // left
2679 memcpy(ac_val2, ac_val, 8 * 2);
2681 ac_val -= 16 * s->block_wrap[n];
2683 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2686 /* apply AC prediction if needed */
2688 if (dc_pred_dir) { //left
2689 for (k = 1; k < 8; k++) {
2690 block[k << v->left_blk_sh] = ac_val[k] * scale;
2691 if (!v->pquantizer && block[k << v->left_blk_sh])
2692 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2695 for (k = 1; k < 8; k++) {
2696 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2697 if (!v->pquantizer && block[k << v->top_blk_sh])
2698 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2704 s->block_last_index[n] = i;
2709 /** Decode intra block in intra frames - should be faster than decode_intra_block
2710 * @param v VC1Context
2711 * @param block block to decode
2712 * @param[in] n subblock number
2713 * @param coded are AC coeffs present or not
2714 * @param codingset set of VLC to decode data
2715 * @param mquant quantizer value for this macroblock
2717 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2718 int coded, int codingset, int mquant)
2720 GetBitContext *gb = &v->s.gb;
2721 MpegEncContext *s = &v->s;
2722 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2725 int16_t *ac_val, *ac_val2;
2727 int a_avail = v->a_avail, c_avail = v->c_avail;
2728 int use_pred = s->ac_pred;
2731 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2733 /* Get DC differential */
2735 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2737 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2740 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2744 if (dcdiff == 119 /* ESC index value */) {
2745 /* TODO: Optimize */
2746 if (mquant == 1) dcdiff = get_bits(gb, 10);
2747 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2748 else dcdiff = get_bits(gb, 8);
2751 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2752 else if (mquant == 2)
2753 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2760 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2763 /* Store the quantized DC coeff, used for prediction */
2765 block[0] = dcdiff * s->y_dc_scale;
2767 block[0] = dcdiff * s->c_dc_scale;
2773 /* check if AC is needed at all */
2774 if (!a_avail && !c_avail)
2776 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2779 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2781 if (dc_pred_dir) // left
2784 ac_val -= 16 * s->block_wrap[n];
2786 q1 = s->current_picture.f.qscale_table[mb_pos];
2787 if ( dc_pred_dir && c_avail && mb_pos)
2788 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2789 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2790 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2791 if ( dc_pred_dir && n == 1)
2793 if (!dc_pred_dir && n == 2)
2799 int last = 0, skip, value;
2800 const uint8_t *zz_table;
2804 if (!use_pred && v->fcm == ILACE_FRAME) {
2805 zz_table = v->zzi_8x8;
2807 if (!dc_pred_dir) // top
2808 zz_table = v->zz_8x8[2];
2810 zz_table = v->zz_8x8[3];
2813 if (v->fcm != ILACE_FRAME)
2814 zz_table = v->zz_8x8[1];
2816 zz_table = v->zzi_8x8;
2820 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2824 block[zz_table[i++]] = value;
2827 /* apply AC prediction if needed */
2829 /* scale predictors if needed*/
2830 if (q2 && q1 != q2) {
2831 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2832 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2835 return AVERROR_INVALIDDATA;
2836 if (dc_pred_dir) { // left
2837 for (k = 1; k < 8; k++)
2838 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2840 for (k = 1; k < 8; k++)
2841 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2844 if (dc_pred_dir) { //left
2845 for (k = 1; k < 8; k++)
2846 block[k << v->left_blk_sh] += ac_val[k];
2848 for (k = 1; k < 8; k++)
2849 block[k << v->top_blk_sh] += ac_val[k + 8];
2853 /* save AC coeffs for further prediction */
2854 for (k = 1; k < 8; k++) {
2855 ac_val2[k ] = block[k << v->left_blk_sh];
2856 ac_val2[k + 8] = block[k << v->top_blk_sh];
2859 /* scale AC coeffs */
2860 for (k = 1; k < 64; k++)
2864 block[k] += (block[k] < 0) ? -mquant : mquant;
2867 if (use_pred) i = 63;
2868 } else { // no AC coeffs
2871 memset(ac_val2, 0, 16 * 2);
2872 if (dc_pred_dir) { // left
2874 memcpy(ac_val2, ac_val, 8 * 2);
2875 if (q2 && q1 != q2) {
2876 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2877 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2879 return AVERROR_INVALIDDATA;
2880 for (k = 1; k < 8; k++)
2881 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2886 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2887 if (q2 && q1 != q2) {
2888 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2889 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2891 return AVERROR_INVALIDDATA;
2892 for (k = 1; k < 8; k++)
2893 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2898 /* apply AC prediction if needed */
2900 if (dc_pred_dir) { // left
2901 for (k = 1; k < 8; k++) {
2902 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2903 if (!v->pquantizer && block[k << v->left_blk_sh])
2904 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2907 for (k = 1; k < 8; k++) {
2908 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2909 if (!v->pquantizer && block[k << v->top_blk_sh])
2910 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2916 s->block_last_index[n] = i;
2921 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2922 * @param v VC1Context
2923 * @param block block to decode
2924 * @param[in] n subblock index
2925 * @param coded are AC coeffs present or not
2926 * @param mquant block quantizer
2927 * @param codingset set of VLC to decode data
2929 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2930 int coded, int mquant, int codingset)
2932 GetBitContext *gb = &v->s.gb;
2933 MpegEncContext *s = &v->s;
2934 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2937 int16_t *ac_val, *ac_val2;
2939 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2940 int a_avail = v->a_avail, c_avail = v->c_avail;
2941 int use_pred = s->ac_pred;
2945 s->dsp.clear_block(block);
2947 /* XXX: Guard against dumb values of mquant */
2948 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2950 /* Set DC scale - y and c use the same */
2951 s->y_dc_scale = s->y_dc_scale_table[mquant];
2952 s->c_dc_scale = s->c_dc_scale_table[mquant];
2954 /* Get DC differential */
2956 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2958 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2961 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2965 if (dcdiff == 119 /* ESC index value */) {
2966 /* TODO: Optimize */
2967 if (mquant == 1) dcdiff = get_bits(gb, 10);
2968 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2969 else dcdiff = get_bits(gb, 8);
2972 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2973 else if (mquant == 2)
2974 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2981 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2984 /* Store the quantized DC coeff, used for prediction */
2987 block[0] = dcdiff * s->y_dc_scale;
2989 block[0] = dcdiff * s->c_dc_scale;
2995 /* check if AC is needed at all and adjust direction if needed */
2996 if (!a_avail) dc_pred_dir = 1;
2997 if (!c_avail) dc_pred_dir = 0;
2998 if (!a_avail && !c_avail) use_pred = 0;
2999 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3002 scale = mquant * 2 + v->halfpq;
3004 if (dc_pred_dir) //left
3007 ac_val -= 16 * s->block_wrap[n];
3009 q1 = s->current_picture.f.qscale_table[mb_pos];
3010 if (dc_pred_dir && c_avail && mb_pos)
3011 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3012 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3013 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3014 if ( dc_pred_dir && n == 1)
3016 if (!dc_pred_dir && n == 2)
3018 if (n == 3) q2 = q1;
3021 int last = 0, skip, value;
3025 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3029 if (v->fcm == PROGRESSIVE)
3030 block[v->zz_8x8[0][i++]] = value;
3032 if (use_pred && (v->fcm == ILACE_FRAME)) {
3033 if (!dc_pred_dir) // top
3034 block[v->zz_8x8[2][i++]] = value;
3036 block[v->zz_8x8[3][i++]] = value;
3038 block[v->zzi_8x8[i++]] = value;
3043 /* apply AC prediction if needed */
3045 /* scale predictors if needed*/
3046 if (q2 && q1 != q2) {
3047 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3048 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3051 return AVERROR_INVALIDDATA;
3052 if (dc_pred_dir) { // left
3053 for (k = 1; k < 8; k++)
3054 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3056 for (k = 1; k < 8; k++)
3057 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3060 if (dc_pred_dir) { // left
3061 for (k = 1; k < 8; k++)
3062 block[k << v->left_blk_sh] += ac_val[k];
3064 for (k = 1; k < 8; k++)
3065 block[k << v->top_blk_sh] += ac_val[k + 8];
3069 /* save AC coeffs for further prediction */
3070 for (k = 1; k < 8; k++) {
3071 ac_val2[k ] = block[k << v->left_blk_sh];
3072 ac_val2[k + 8] = block[k << v->top_blk_sh];
3075 /* scale AC coeffs */
3076 for (k = 1; k < 64; k++)
3080 block[k] += (block[k] < 0) ? -mquant : mquant;
3083 if (use_pred) i = 63;
3084 } else { // no AC coeffs
3087 memset(ac_val2, 0, 16 * 2);
3088 if (dc_pred_dir) { // left
3090 memcpy(ac_val2, ac_val, 8 * 2);
3091 if (q2 && q1 != q2) {
3092 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3093 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3095 return AVERROR_INVALIDDATA;
3096 for (k = 1; k < 8; k++)
3097 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3102 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3103 if (q2 && q1 != q2) {
3104 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3105 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3107 return AVERROR_INVALIDDATA;
3108 for (k = 1; k < 8; k++)
3109 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3114 /* apply AC prediction if needed */
3116 if (dc_pred_dir) { // left
3117 for (k = 1; k < 8; k++) {
3118 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3119 if (!v->pquantizer && block[k << v->left_blk_sh])
3120 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3123 for (k = 1; k < 8; k++) {
3124 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3125 if (!v->pquantizer && block[k << v->top_blk_sh])
3126 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3132 s->block_last_index[n] = i;
3139 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3140 int mquant, int ttmb, int first_block,
3141 uint8_t *dst, int linesize, int skip_block,
3144 MpegEncContext *s = &v->s;
3145 GetBitContext *gb = &s->gb;
3148 int scale, off, idx, last, skip, value;
3149 int ttblk = ttmb & 7;
3152 s->dsp.clear_block(block);
3155 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)];
3157 if (ttblk == TT_4X4) {
3158 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3160 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3161 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3162 || (!v->res_rtm_flag && !first_block))) {
3163 subblkpat = decode012(gb);
3165 subblkpat ^= 3; // swap decoded pattern bits
3166 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3168 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3171 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3173 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3174 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3175 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3178 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3179 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3188 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3193 idx = v->zz_8x8[0][i++];
3195 idx = v->zzi_8x8[i++];
3196 block[idx] = value * scale;
3198 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3202 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3204 v->vc1dsp.vc1_inv_trans_8x8(block);
3205 s->dsp.add_pixels_clamped(block, dst, linesize);
3210 pat = ~subblkpat & 0xF;
3211 for (j = 0; j < 4; j++) {
3212 last = subblkpat & (1 << (3 - j));
3214 off = (j & 1) * 4 + (j & 2) * 16;
3216 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3221 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3223 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3224 block[idx + off] = value * scale;
3226 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3228 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3230 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3232 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3237 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3238 for (j = 0; j < 2; j++) {
3239 last = subblkpat & (1 << (1 - j));
3243 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3248 idx = v->zz_8x4[i++] + off;
3250 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3251 block[idx] = value * scale;
3253 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3255 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3257 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3259 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3264 pat = ~(subblkpat * 5) & 0xF;
3265 for (j = 0; j < 2; j++) {
3266 last = subblkpat & (1 << (1 - j));
3270 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3275 idx = v->zz_4x8[i++] + off;
3277 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3278 block[idx] = value * scale;
3280 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3282 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3284 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3286 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3292 *ttmb_out |= ttblk << (n * 4);
3296 /** @} */ // Macroblock group
3298 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3299 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3301 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3303 MpegEncContext *s = &v->s;
3304 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3305 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3306 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3307 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3308 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3311 if (block_num > 3) {
3312 dst = s->dest[block_num - 3];
3314 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3316 if (s->mb_y != s->end_mb_y || block_num < 2) {
3320 if (block_num > 3) {
3321 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3322 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3323 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3324 mv_stride = s->mb_stride;
3326 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3327 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3328 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3329 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3330 mv_stride = s->b8_stride;
3331 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3334 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3335 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3336 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3338 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3340 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3343 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3345 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3350 dst -= 4 * linesize;
3351 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3352 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3353 idx = (block_cbp | (block_cbp >> 2)) & 3;
3355 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3358 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3360 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3365 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3367 MpegEncContext *s = &v->s;
3368 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3369 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3370 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3371 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3372 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3375 if (block_num > 3) {
3376 dst = s->dest[block_num - 3] - 8 * linesize;
3378 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3381 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3384 if (block_num > 3) {
3385 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3386 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3387 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3389 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3390 : (mb_cbp >> ((block_num + 1) * 4));
3391 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3392 : (mb_is_intra >> ((block_num + 1) * 4));
3393 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3395 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3396 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3398 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3400 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3403 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3405 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3411 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3412 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3413 idx = (block_cbp | (block_cbp >> 1)) & 5;
3415 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3418 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3420 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3425 static void vc1_apply_p_loop_filter(VC1Context *v)
3427 MpegEncContext *s = &v->s;
3430 for (i = 0; i < 6; i++) {
3431 vc1_apply_p_v_loop_filter(v, i);
3434 /* V always precedes H, therefore we run H one MB before V;
3435 * at the end of a row, we catch up to complete the row */
3437 for (i = 0; i < 6; i++) {
3438 vc1_apply_p_h_loop_filter(v, i);
3440 if (s->mb_x == s->mb_width - 1) {
3442 ff_update_block_index(s);
3443 for (i = 0; i < 6; i++) {
3444 vc1_apply_p_h_loop_filter(v, i);
3450 /** Decode one P-frame MB
3452 static int vc1_decode_p_mb(VC1Context *v)
3454 MpegEncContext *s = &v->s;
3455 GetBitContext *gb = &s->gb;
3457 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3458 int cbp; /* cbp decoding stuff */
3459 int mqdiff, mquant; /* MB quantization */
3460 int ttmb = v->ttfrm; /* MB Transform type */
3462 int mb_has_coeffs = 1; /* last_flag */
3463 int dmv_x, dmv_y; /* Differential MV components */
3464 int index, index1; /* LUT indexes */
3465 int val, sign; /* temp values */
3466 int first_block = 1;
3468 int skipped, fourmv;
3469 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3471 mquant = v->pq; /* lossy initialization */
3473 if (v->mv_type_is_raw)
3474 fourmv = get_bits1(gb);
3476 fourmv = v->mv_type_mb_plane[mb_pos];
3478 skipped = get_bits1(gb);
3480 skipped = v->s.mbskip_table[mb_pos];
3482 if (!fourmv) { /* 1MV mode */
3484 GET_MVDATA(dmv_x, dmv_y);
3487 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3488 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3490 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3491 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3493 /* FIXME Set DC val for inter block ? */
3494 if (s->mb_intra && !mb_has_coeffs) {
3496 s->ac_pred = get_bits1(gb);
3498 } else if (mb_has_coeffs) {
3500 s->ac_pred = get_bits1(gb);
3501 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3507 s->current_picture.f.qscale_table[mb_pos] = mquant;
3509 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3510 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3511 VC1_TTMB_VLC_BITS, 2);
3512 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3514 for (i = 0; i < 6; i++) {
3515 s->dc_val[0][s->block_index[i]] = 0;
3517 val = ((cbp >> (5 - i)) & 1);
3518 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3519 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3521 /* check if prediction blocks A and C are available */
3522 v->a_avail = v->c_avail = 0;
3523 if (i == 2 || i == 3 || !s->first_slice_line)
3524 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3525 if (i == 1 || i == 3 || s->mb_x)
3526 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3528 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3529 (i & 4) ? v->codingset2 : v->codingset);
3530 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3532 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3534 for (j = 0; j < 64; j++)
3535 s->block[i][j] <<= 1;
3536 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3537 if (v->pq >= 9 && v->overlap) {
3539 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3541 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3543 block_cbp |= 0xF << (i << 2);
3544 block_intra |= 1 << i;
3546 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3547 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3548 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3549 block_cbp |= pat << (i << 2);
3550 if (!v->ttmbf && ttmb < 8)
3557 for (i = 0; i < 6; i++) {
3558 v->mb_type[0][s->block_index[i]] = 0;
3559 s->dc_val[0][s->block_index[i]] = 0;
3561 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3562 s->current_picture.f.qscale_table[mb_pos] = 0;
3563 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3566 } else { // 4MV mode
3567 if (!skipped /* unskipped MB */) {
3568 int intra_count = 0, coded_inter = 0;
3569 int is_intra[6], is_coded[6];
3571 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3572 for (i = 0; i < 6; i++) {
3573 val = ((cbp >> (5 - i)) & 1);
3574 s->dc_val[0][s->block_index[i]] = 0;
3581 GET_MVDATA(dmv_x, dmv_y);
3583 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3585 vc1_mc_4mv_luma(v, i, 0);
3586 intra_count += s->mb_intra;
3587 is_intra[i] = s->mb_intra;
3588 is_coded[i] = mb_has_coeffs;
3591 is_intra[i] = (intra_count >= 3);
3595 vc1_mc_4mv_chroma(v, 0);
3596 v->mb_type[0][s->block_index[i]] = is_intra[i];
3598 coded_inter = !is_intra[i] & is_coded[i];
3600 // if there are no coded blocks then don't do anything more
3602 if (!intra_count && !coded_inter)
3605 s->current_picture.f.qscale_table[mb_pos] = mquant;
3606 /* test if block is intra and has pred */
3609 for (i = 0; i < 6; i++)
3611 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3612 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3618 s->ac_pred = get_bits1(gb);
3622 if (!v->ttmbf && coded_inter)
3623 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3624 for (i = 0; i < 6; i++) {
3626 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3627 s->mb_intra = is_intra[i];
3629 /* check if prediction blocks A and C are available */
3630 v->a_avail = v->c_avail = 0;
3631 if (i == 2 || i == 3 || !s->first_slice_line)
3632 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3633 if (i == 1 || i == 3 || s->mb_x)
3634 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3636 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3637 (i & 4) ? v->codingset2 : v->codingset);
3638 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3640 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3642 for (j = 0; j < 64; j++)
3643 s->block[i][j] <<= 1;
3644 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3645 (i & 4) ? s->uvlinesize : s->linesize);
3646 if (v->pq >= 9 && v->overlap) {
3648 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3650 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3652 block_cbp |= 0xF << (i << 2);
3653 block_intra |= 1 << i;
3654 } else if (is_coded[i]) {
3655 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3656 first_block, s->dest[dst_idx] + off,
3657 (i & 4) ? s->uvlinesize : s->linesize,
3658 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3660 block_cbp |= pat << (i << 2);
3661 if (!v->ttmbf && ttmb < 8)
3666 } else { // skipped MB
3668 s->current_picture.f.qscale_table[mb_pos] = 0;
3669 for (i = 0; i < 6; i++) {
3670 v->mb_type[0][s->block_index[i]] = 0;
3671 s->dc_val[0][s->block_index[i]] = 0;
3673 for (i = 0; i < 4; i++) {
3674 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3675 vc1_mc_4mv_luma(v, i, 0);
3677 vc1_mc_4mv_chroma(v, 0);
3678 s->current_picture.f.qscale_table[mb_pos] = 0;
3682 v->cbp[s->mb_x] = block_cbp;
3683 v->ttblk[s->mb_x] = block_tt;
3684 v->is_intra[s->mb_x] = block_intra;
3689 /* Decode one macroblock in an interlaced frame p picture */
3691 static int vc1_decode_p_mb_intfr(VC1Context *v)
3693 MpegEncContext *s = &v->s;
3694 GetBitContext *gb = &s->gb;
3696 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3697 int cbp = 0; /* cbp decoding stuff */
3698 int mqdiff, mquant; /* MB quantization */
3699 int ttmb = v->ttfrm; /* MB Transform type */
3701 int mb_has_coeffs = 1; /* last_flag */
3702 int dmv_x, dmv_y; /* Differential MV components */
3703 int val; /* temp value */
3704 int first_block = 1;
3706 int skipped, fourmv = 0, twomv = 0;
3707 int block_cbp = 0, pat, block_tt = 0;
3708 int idx_mbmode = 0, mvbp;
3709 int stride_y, fieldtx;
3711 mquant = v->pq; /* Lossy initialization */
3714 skipped = get_bits1(gb);
3716 skipped = v->s.mbskip_table[mb_pos];
3718 if (v->fourmvswitch)
3719 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3721 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3722 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3723 /* store the motion vector type in a flag (useful later) */
3724 case MV_PMODE_INTFR_4MV:
3726 v->blk_mv_type[s->block_index[0]] = 0;
3727 v->blk_mv_type[s->block_index[1]] = 0;
3728 v->blk_mv_type[s->block_index[2]] = 0;
3729 v->blk_mv_type[s->block_index[3]] = 0;
3731 case MV_PMODE_INTFR_4MV_FIELD:
3733 v->blk_mv_type[s->block_index[0]] = 1;
3734 v->blk_mv_type[s->block_index[1]] = 1;
3735 v->blk_mv_type[s->block_index[2]] = 1;
3736 v->blk_mv_type[s->block_index[3]] = 1;
3738 case MV_PMODE_INTFR_2MV_FIELD:
3740 v->blk_mv_type[s->block_index[0]] = 1;
3741 v->blk_mv_type[s->block_index[1]] = 1;
3742 v->blk_mv_type[s->block_index[2]] = 1;
3743 v->blk_mv_type[s->block_index[3]] = 1;
3745 case MV_PMODE_INTFR_1MV:
3746 v->blk_mv_type[s->block_index[0]] = 0;
3747 v->blk_mv_type[s->block_index[1]] = 0;
3748 v->blk_mv_type[s->block_index[2]] = 0;
3749 v->blk_mv_type[s->block_index[3]] = 0;
3752 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3753 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3754 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3755 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3756 s->mb_intra = v->is_intra[s->mb_x] = 1;
3757 for (i = 0; i < 6; i++)
3758 v->mb_type[0][s->block_index[i]] = 1;
3759 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3760 mb_has_coeffs = get_bits1(gb);
3762 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3763 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3765 s->current_picture.f.qscale_table[mb_pos] = mquant;
3766 /* Set DC scale - y and c use the same (not sure if necessary here) */
3767 s->y_dc_scale = s->y_dc_scale_table[mquant];
3768 s->c_dc_scale = s->c_dc_scale_table[mquant];
3770 for (i = 0; i < 6; i++) {
3771 s->dc_val[0][s->block_index[i]] = 0;
3773 val = ((cbp >> (5 - i)) & 1);
3774 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3775 v->a_avail = v->c_avail = 0;
3776 if (i == 2 || i == 3 || !s->first_slice_line)
3777 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3778 if (i == 1 || i == 3 || s->mb_x)
3779 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3781 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3782 (i & 4) ? v->codingset2 : v->codingset);
3783 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3784 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3786 stride_y = s->linesize << fieldtx;
3787 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3789 stride_y = s->uvlinesize;
3792 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3796 } else { // inter MB
3797 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3799 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3800 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3801 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3803 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3804 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3805 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3808 s->mb_intra = v->is_intra[s->mb_x] = 0;
3809 for (i = 0; i < 6; i++)
3810 v->mb_type[0][s->block_index[i]] = 0;
3811 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3812 /* for all motion vector read MVDATA and motion compensate each block */
3816 for (i = 0; i < 6; i++) {
3819 val = ((mvbp >> (3 - i)) & 1);
3821 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3823 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3824 vc1_mc_4mv_luma(v, i, 0);
3825 } else if (i == 4) {
3826 vc1_mc_4mv_chroma4(v);
3833 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3835 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3836 vc1_mc_4mv_luma(v, 0, 0);
3837 vc1_mc_4mv_luma(v, 1, 0);
3840 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3842 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3843 vc1_mc_4mv_luma(v, 2, 0);
3844 vc1_mc_4mv_luma(v, 3, 0);
3845 vc1_mc_4mv_chroma4(v);
3847 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3850 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3852 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3856 GET_MQUANT(); // p. 227
3857 s->current_picture.f.qscale_table[mb_pos] = mquant;
3858 if (!v->ttmbf && cbp)
3859 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3860 for (i = 0; i < 6; i++) {
3861 s->dc_val[0][s->block_index[i]] = 0;
3863 val = ((cbp >> (5 - i)) & 1);
3865 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3867 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3869 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3870 first_block, s->dest[dst_idx] + off,
3871 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3872 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3873 block_cbp |= pat << (i << 2);
3874 if (!v->ttmbf && ttmb < 8)
3881 s->mb_intra = v->is_intra[s->mb_x] = 0;
3882 for (i = 0; i < 6; i++) {
3883 v->mb_type[0][s->block_index[i]] = 0;
3884 s->dc_val[0][s->block_index[i]] = 0;
3886 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3887 s->current_picture.f.qscale_table[mb_pos] = 0;
3888 v->blk_mv_type[s->block_index[0]] = 0;
3889 v->blk_mv_type[s->block_index[1]] = 0;
3890 v->blk_mv_type[s->block_index[2]] = 0;
3891 v->blk_mv_type[s->block_index[3]] = 0;
3892 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3895 if (s->mb_x == s->mb_width - 1)
3896 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3900 static int vc1_decode_p_mb_intfi(VC1Context *v)
3902 MpegEncContext *s = &v->s;
3903 GetBitContext *gb = &s->gb;
3905 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3906 int cbp = 0; /* cbp decoding stuff */
3907 int mqdiff, mquant; /* MB quantization */
3908 int ttmb = v->ttfrm; /* MB Transform type */
3910 int mb_has_coeffs = 1; /* last_flag */
3911 int dmv_x, dmv_y; /* Differential MV components */
3912 int val; /* temp values */
3913 int first_block = 1;
3916 int block_cbp = 0, pat, block_tt = 0;
3919 mquant = v->pq; /* Lossy initialization */
3921 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3922 if (idx_mbmode <= 1) { // intra MB
3923 s->mb_intra = v->is_intra[s->mb_x] = 1;
3924 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3925 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3926 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3928 s->current_picture.f.qscale_table[mb_pos] = mquant;
3929 /* Set DC scale - y and c use the same (not sure if necessary here) */
3930 s->y_dc_scale = s->y_dc_scale_table[mquant];
3931 s->c_dc_scale = s->c_dc_scale_table[mquant];
3932 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3933 mb_has_coeffs = idx_mbmode & 1;
3935 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3937 for (i = 0; i < 6; i++) {
3938 s->dc_val[0][s->block_index[i]] = 0;
3939 v->mb_type[0][s->block_index[i]] = 1;
3941 val = ((cbp >> (5 - i)) & 1);
3942 v->a_avail = v->c_avail = 0;
3943 if (i == 2 || i == 3 || !s->first_slice_line)
3944 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3945 if (i == 1 || i == 3 || s->mb_x)
3946 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3948 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3949 (i & 4) ? v->codingset2 : v->codingset);
3950 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3952 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3953 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3954 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3955 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3956 // TODO: loop filter
3959 s->mb_intra = v->is_intra[s->mb_x] = 0;
3960 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3961 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3962 if (idx_mbmode <= 5) { // 1-MV
3963 dmv_x = dmv_y = pred_flag = 0;
3964 if (idx_mbmode & 1) {
3965 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3967 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3969 mb_has_coeffs = !(idx_mbmode & 2);
3971 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3972 for (i = 0; i < 6; i++) {
3974 dmv_x = dmv_y = pred_flag = 0;
3975 val = ((v->fourmvbp >> (3 - i)) & 1);
3977 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3979 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3980 vc1_mc_4mv_luma(v, i, 0);
3982 vc1_mc_4mv_chroma(v, 0);
3984 mb_has_coeffs = idx_mbmode & 1;
3987 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3991 s->current_picture.f.qscale_table[mb_pos] = mquant;
3992 if (!v->ttmbf && cbp) {
3993 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3996 for (i = 0; i < 6; i++) {
3997 s->dc_val[0][s->block_index[i]] = 0;
3999 val = ((cbp >> (5 - i)) & 1);
4000 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4001 if (v->second_field)
4002 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4004 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4005 first_block, s->dest[dst_idx] + off,
4006 (i & 4) ? s->uvlinesize : s->linesize,
4007 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4009 block_cbp |= pat << (i << 2);
4010 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4015 if (s->mb_x == s->mb_width - 1)
4016 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4020 /** Decode one B-frame MB (in Main profile)
4022 static void vc1_decode_b_mb(VC1Context *v)
4024 MpegEncContext *s = &v->s;
4025 GetBitContext *gb = &s->gb;
4027 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4028 int cbp = 0; /* cbp decoding stuff */
4029 int mqdiff, mquant; /* MB quantization */
4030 int ttmb = v->ttfrm; /* MB Transform type */
4031 int mb_has_coeffs = 0; /* last_flag */
4032 int index, index1; /* LUT indexes */
4033 int val, sign; /* temp values */
4034 int first_block = 1;
4036 int skipped, direct;
4037 int dmv_x[2], dmv_y[2];
4038 int bmvtype = BMV_TYPE_BACKWARD;
4040 mquant = v->pq; /* lossy initialization */
4044 direct = get_bits1(gb);
4046 direct = v->direct_mb_plane[mb_pos];
4048 skipped = get_bits1(gb);
4050 skipped = v->s.mbskip_table[mb_pos];
4052 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4053 for (i = 0; i < 6; i++) {
4054 v->mb_type[0][s->block_index[i]] = 0;
4055 s->dc_val[0][s->block_index[i]] = 0;
4057 s->current_picture.f.qscale_table[mb_pos] = 0;
4061 GET_MVDATA(dmv_x[0], dmv_y[0]);
4062 dmv_x[1] = dmv_x[0];
4063 dmv_y[1] = dmv_y[0];
4065 if (skipped || !s->mb_intra) {
4066 bmvtype = decode012(gb);
4069 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4072 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4075 bmvtype = BMV_TYPE_INTERPOLATED;
4076 dmv_x[0] = dmv_y[0] = 0;
4080 for (i = 0; i < 6; i++)
4081 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4085 bmvtype = BMV_TYPE_INTERPOLATED;
4086 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4087 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4091 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4094 s->current_picture.f.qscale_table[mb_pos] = mquant;
4096 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4097 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4098 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4099 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4101 if (!mb_has_coeffs && !s->mb_intra) {
4102 /* no coded blocks - effectively skipped */
4103 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4104 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4107 if (s->mb_intra && !mb_has_coeffs) {
4109 s->current_picture.f.qscale_table[mb_pos] = mquant;
4110 s->ac_pred = get_bits1(gb);
4112 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4114 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4115 GET_MVDATA(dmv_x[0], dmv_y[0]);
4116 if (!mb_has_coeffs) {
4117 /* interpolated skipped block */
4118 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4119 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4123 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4125 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4128 s->ac_pred = get_bits1(gb);
4129 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4131 s->current_picture.f.qscale_table[mb_pos] = mquant;
4132 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4133 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4137 for (i = 0; i < 6; i++) {
4138 s->dc_val[0][s->block_index[i]] = 0;
4140 val = ((cbp >> (5 - i)) & 1);
4141 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4142 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4144 /* check if prediction blocks A and C are available */
4145 v->a_avail = v->c_avail = 0;
4146 if (i == 2 || i == 3 || !s->first_slice_line)
4147 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4148 if (i == 1 || i == 3 || s->mb_x)
4149 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4151 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4152 (i & 4) ? v->codingset2 : v->codingset);
4153 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4155 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4157 for (j = 0; j < 64; j++)
4158 s->block[i][j] <<= 1;
4159 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4161 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4162 first_block, s->dest[dst_idx] + off,
4163 (i & 4) ? s->uvlinesize : s->linesize,
4164 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4165 if (!v->ttmbf && ttmb < 8)
4172 /** Decode one B-frame MB (in interlaced field B picture)
4174 static void vc1_decode_b_mb_intfi(VC1Context *v)
4176 MpegEncContext *s = &v->s;
4177 GetBitContext *gb = &s->gb;
4179 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4180 int cbp = 0; /* cbp decoding stuff */
4181 int mqdiff, mquant; /* MB quantization */
4182 int ttmb = v->ttfrm; /* MB Transform type */
4183 int mb_has_coeffs = 0; /* last_flag */
4184 int val; /* temp value */
4185 int first_block = 1;
4188 int dmv_x[2], dmv_y[2], pred_flag[2];
4189 int bmvtype = BMV_TYPE_BACKWARD;
4190 int idx_mbmode, interpmvp;
4192 mquant = v->pq; /* Lossy initialization */
4195 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4196 if (idx_mbmode <= 1) { // intra MB
4197 s->mb_intra = v->is_intra[s->mb_x] = 1;
4198 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4199 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4200 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4202 s->current_picture.f.qscale_table[mb_pos] = mquant;
4203 /* Set DC scale - y and c use the same (not sure if necessary here) */
4204 s->y_dc_scale = s->y_dc_scale_table[mquant];
4205 s->c_dc_scale = s->c_dc_scale_table[mquant];
4206 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4207 mb_has_coeffs = idx_mbmode & 1;
4209 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4211 for (i = 0; i < 6; i++) {
4212 s->dc_val[0][s->block_index[i]] = 0;
4214 val = ((cbp >> (5 - i)) & 1);
4215 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4216 v->a_avail = v->c_avail = 0;
4217 if (i == 2 || i == 3 || !s->first_slice_line)
4218 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4219 if (i == 1 || i == 3 || s->mb_x)
4220 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4222 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4223 (i & 4) ? v->codingset2 : v->codingset);
4224 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4226 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4228 for (j = 0; j < 64; j++)
4229 s->block[i][j] <<= 1;
4230 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4231 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4232 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4233 // TODO: yet to perform loop filter
4236 s->mb_intra = v->is_intra[s->mb_x] = 0;
4237 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4238 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4240 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4242 fwd = v->forward_mb_plane[mb_pos];
4243 if (idx_mbmode <= 5) { // 1-MV
4244 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4245 pred_flag[0] = pred_flag[1] = 0;
4247 bmvtype = BMV_TYPE_FORWARD;
4249 bmvtype = decode012(gb);
4252 bmvtype = BMV_TYPE_BACKWARD;
4255 bmvtype = BMV_TYPE_DIRECT;
4258 bmvtype = BMV_TYPE_INTERPOLATED;
4259 interpmvp = get_bits1(gb);
4262 v->bmvtype = bmvtype;
4263 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4264 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4266 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4267 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4269 if (bmvtype == BMV_TYPE_DIRECT) {
4270 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4271 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4273 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4274 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4275 mb_has_coeffs = !(idx_mbmode & 2);
4278 bmvtype = BMV_TYPE_FORWARD;
4279 v->bmvtype = bmvtype;
4280 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4281 for (i = 0; i < 6; i++) {
4283 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4284 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4285 val = ((v->fourmvbp >> (3 - i)) & 1);
4287 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4288 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4289 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4291 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4292 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4294 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4296 mb_has_coeffs = idx_mbmode & 1;
4299 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4303 s->current_picture.f.qscale_table[mb_pos] = mquant;
4304 if (!v->ttmbf && cbp) {
4305 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4308 for (i = 0; i < 6; i++) {
4309 s->dc_val[0][s->block_index[i]] = 0;
4311 val = ((cbp >> (5 - i)) & 1);
4312 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4313 if (v->second_field)
4314 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4316 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4317 first_block, s->dest[dst_idx] + off,
4318 (i & 4) ? s->uvlinesize : s->linesize,
4319 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4320 if (!v->ttmbf && ttmb < 8)
4328 /** Decode blocks of I-frame
4330 static void vc1_decode_i_blocks(VC1Context *v)
4333 MpegEncContext *s = &v->s;
4338 /* select codingmode used for VLC tables selection */
4339 switch (v->y_ac_table_index) {
4341 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4344 v->codingset = CS_HIGH_MOT_INTRA;
4347 v->codingset = CS_MID_RATE_INTRA;
4351 switch (v->c_ac_table_index) {
4353 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4356 v->codingset2 = CS_HIGH_MOT_INTER;
4359 v->codingset2 = CS_MID_RATE_INTER;
4363 /* Set DC scale - y and c use the same */
4364 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4365 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4368 s->mb_x = s->mb_y = 0;
4370 s->first_slice_line = 1;
4371 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4373 ff_init_block_index(s);
4374 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4376 ff_update_block_index(s);
4377 dst[0] = s->dest[0];
4378 dst[1] = dst[0] + 8;
4379 dst[2] = s->dest[0] + s->linesize * 8;
4380 dst[3] = dst[2] + 8;
4381 dst[4] = s->dest[1];
4382 dst[5] = s->dest[2];
4383 s->dsp.clear_blocks(s->block[0]);
4384 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4385 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4386 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4387 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4388 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4390 // do actual MB decoding and displaying
4391 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4392 v->s.ac_pred = get_bits1(&v->s.gb);
4394 for (k = 0; k < 6; k++) {
4395 val = ((cbp >> (5 - k)) & 1);
4398 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4402 cbp |= val << (5 - k);
4404 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4406 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4408 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4409 if (v->pq >= 9 && v->overlap) {
4411 for (j = 0; j < 64; j++)
4412 s->block[k][j] <<= 1;
4413 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4416 for (j = 0; j < 64; j++)
4417 s->block[k][j] = (s->block[k][j] - 64) << 1;
4418 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4422 if (v->pq >= 9 && v->overlap) {
4424 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4425 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4426 if (!(s->flags & CODEC_FLAG_GRAY)) {
4427 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4428 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4431 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4432 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4433 if (!s->first_slice_line) {
4434 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4435 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4436 if (!(s->flags & CODEC_FLAG_GRAY)) {
4437 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4438 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4441 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4442 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4444 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4446 if (get_bits_count(&s->gb) > v->bits) {
4447 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4448 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4449 get_bits_count(&s->gb), v->bits);
4453 if (!v->s.loop_filter)
4454 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4456 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4458 s->first_slice_line = 0;
4460 if (v->s.loop_filter)
4461 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4463 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4464 * profile, these only differ are when decoding MSS2 rectangles. */
4465 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4468 /** Decode blocks of I-frame for advanced profile
4470 static void vc1_decode_i_blocks_adv(VC1Context *v)
4473 MpegEncContext *s = &v->s;
4479 GetBitContext *gb = &s->gb;
4481 /* select codingmode used for VLC tables selection */
4482 switch (v->y_ac_table_index) {
4484 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4487 v->codingset = CS_HIGH_MOT_INTRA;
4490 v->codingset = CS_MID_RATE_INTRA;
4494 switch (v->c_ac_table_index) {
4496 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4499 v->codingset2 = CS_HIGH_MOT_INTER;
4502 v->codingset2 = CS_MID_RATE_INTER;
4507 s->mb_x = s->mb_y = 0;
4509 s->first_slice_line = 1;
4510 s->mb_y = s->start_mb_y;
4511 if (s->start_mb_y) {
4513 ff_init_block_index(s);
4514 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4515 (1 + s->b8_stride) * sizeof(*s->coded_block));
4517 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4519 ff_init_block_index(s);
4520 for (;s->mb_x < s->mb_width; s->mb_x++) {
4521 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4522 ff_update_block_index(s);
4523 s->dsp.clear_blocks(block[0]);
4524 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4525 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4526 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4527 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4529 // do actual MB decoding and displaying
4530 if (v->fieldtx_is_raw)
4531 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4532 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4533 if ( v->acpred_is_raw)
4534 v->s.ac_pred = get_bits1(&v->s.gb);
4536 v->s.ac_pred = v->acpred_plane[mb_pos];
4538 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4539 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4543 s->current_picture.f.qscale_table[mb_pos] = mquant;
4544 /* Set DC scale - y and c use the same */
4545 s->y_dc_scale = s->y_dc_scale_table[mquant];
4546 s->c_dc_scale = s->c_dc_scale_table[mquant];
4548 for (k = 0; k < 6; k++) {
4549 val = ((cbp >> (5 - k)) & 1);
4552 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4556 cbp |= val << (5 - k);
4558 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4559 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4561 vc1_decode_i_block_adv(v, block[k], k, val,
4562 (k < 4) ? v->codingset : v->codingset2, mquant);
4564 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4566 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4569 vc1_smooth_overlap_filter_iblk(v);
4570 vc1_put_signed_blocks_clamped(v);
4571 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4573 if (get_bits_count(&s->gb) > v->bits) {
4574 // TODO: may need modification to handle slice coding
4575 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4576 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4577 get_bits_count(&s->gb), v->bits);
4581 if (!v->s.loop_filter)
4582 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4584 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4585 s->first_slice_line = 0;
4588 /* raw bottom MB row */
4590 ff_init_block_index(s);
4591 for (;s->mb_x < s->mb_width; s->mb_x++) {
4592 ff_update_block_index(s);
4593 vc1_put_signed_blocks_clamped(v);
4594 if (v->s.loop_filter)
4595 vc1_loop_filter_iblk_delayed(v, v->pq);
4597 if (v->s.loop_filter)
4598 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4599 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4600 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4603 static void vc1_decode_p_blocks(VC1Context *v)
4605 MpegEncContext *s = &v->s;
4606 int apply_loop_filter;
4608 /* select codingmode used for VLC tables selection */
4609 switch (v->c_ac_table_index) {
4611 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4614 v->codingset = CS_HIGH_MOT_INTRA;
4617 v->codingset = CS_MID_RATE_INTRA;
4621 switch (v->c_ac_table_index) {
4623 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4626 v->codingset2 = CS_HIGH_MOT_INTER;
4629 v->codingset2 = CS_MID_RATE_INTER;
4633 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4634 s->first_slice_line = 1;
4635 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4636 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4638 ff_init_block_index(s);
4639 for (; s->mb_x < s->mb_width; s->mb_x++) {
4640 ff_update_block_index(s);
4642 if (v->fcm == ILACE_FIELD)
4643 vc1_decode_p_mb_intfi(v);
4644 else if (v->fcm == ILACE_FRAME)
4645 vc1_decode_p_mb_intfr(v);
4646 else vc1_decode_p_mb(v);
4647 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4648 vc1_apply_p_loop_filter(v);
4649 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4650 // TODO: may need modification to handle slice coding
4651 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4652 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4653 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4657 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4658 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4659 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4660 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4661 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4662 s->first_slice_line = 0;
4664 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4666 ff_init_block_index(s);
4667 for (; s->mb_x < s->mb_width; s->mb_x++) {
4668 ff_update_block_index(s);
4669 vc1_apply_p_loop_filter(v);
4672 if (s->end_mb_y >= s->start_mb_y)
4673 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4674 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4675 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4678 static void vc1_decode_b_blocks(VC1Context *v)
4680 MpegEncContext *s = &v->s;
4682 /* select codingmode used for VLC tables selection */
4683 switch (v->c_ac_table_index) {
4685 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4688 v->codingset = CS_HIGH_MOT_INTRA;
4691 v->codingset = CS_MID_RATE_INTRA;
4695 switch (v->c_ac_table_index) {
4697 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4700 v->codingset2 = CS_HIGH_MOT_INTER;
4703 v->codingset2 = CS_MID_RATE_INTER;
4707 s->first_slice_line = 1;
4708 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4710 ff_init_block_index(s);
4711 for (; s->mb_x < s->mb_width; s->mb_x++) {
4712 ff_update_block_index(s);
4714 if (v->fcm == ILACE_FIELD)
4715 vc1_decode_b_mb_intfi(v);
4718 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4719 // TODO: may need modification to handle slice coding
4720 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4721 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4722 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4725 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4727 if (!v->s.loop_filter)
4728 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4730 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4731 s->first_slice_line = 0;
4733 if (v->s.loop_filter)
4734 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4735 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4736 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4739 static void vc1_decode_skip_blocks(VC1Context *v)
4741 MpegEncContext *s = &v->s;
4743 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4744 s->first_slice_line = 1;
4745 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4747 ff_init_block_index(s);
4748 ff_update_block_index(s);
4749 if (s->last_picture.f.data[0]) {
4750 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4751 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4752 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4754 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4755 s->first_slice_line = 0;
4757 s->pict_type = AV_PICTURE_TYPE_P;
4760 void ff_vc1_decode_blocks(VC1Context *v)
4763 v->s.esc3_level_length = 0;
4765 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4768 v->left_blk_idx = -1;
4769 v->topleft_blk_idx = 1;
4771 switch (v->s.pict_type) {
4772 case AV_PICTURE_TYPE_I:
4773 if (v->profile == PROFILE_ADVANCED)
4774 vc1_decode_i_blocks_adv(v);
4776 vc1_decode_i_blocks(v);
4778 case AV_PICTURE_TYPE_P:
4779 if (v->p_frame_skipped)
4780 vc1_decode_skip_blocks(v);
4782 vc1_decode_p_blocks(v);
4784 case AV_PICTURE_TYPE_B:
4786 if (v->profile == PROFILE_ADVANCED)
4787 vc1_decode_i_blocks_adv(v);
4789 vc1_decode_i_blocks(v);
4791 vc1_decode_b_blocks(v);
4797 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4801 * Transform coefficients for both sprites in 16.16 fixed point format,
4802 * in the order they appear in the bitstream:
4804 * rotation 1 (unused)
4806 * rotation 2 (unused)
4813 int effect_type, effect_flag;
4814 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4815 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4818 static inline int get_fp_val(GetBitContext* gb)
4820 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4823 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4827 switch (get_bits(gb, 2)) {
4830 c[2] = get_fp_val(gb);
4834 c[0] = c[4] = get_fp_val(gb);
4835 c[2] = get_fp_val(gb);
4838 c[0] = get_fp_val(gb);
4839 c[2] = get_fp_val(gb);
4840 c[4] = get_fp_val(gb);
4843 c[0] = get_fp_val(gb);
4844 c[1] = get_fp_val(gb);
4845 c[2] = get_fp_val(gb);
4846 c[3] = get_fp_val(gb);
4847 c[4] = get_fp_val(gb);
4850 c[5] = get_fp_val(gb);
4852 c[6] = get_fp_val(gb);
4857 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4859 AVCodecContext *avctx = v->s.avctx;
4862 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4863 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4864 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4865 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4866 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4867 for (i = 0; i < 7; i++)
4868 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4869 sd->coefs[sprite][i] / (1<<16),
4870 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4871 av_log(avctx, AV_LOG_DEBUG, "\n");
4875 if (sd->effect_type = get_bits_long(gb, 30)) {
4876 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4878 vc1_sprite_parse_transform(gb, sd->effect_params1);
4881 vc1_sprite_parse_transform(gb, sd->effect_params1);
4882 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4885 for (i = 0; i < sd->effect_pcount1; i++)
4886 sd->effect_params1[i] = get_fp_val(gb);
4888 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4889 // effect 13 is simple alpha blending and matches the opacity above
4890 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4891 for (i = 0; i < sd->effect_pcount1; i++)
4892 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4893 sd->effect_params1[i] / (1 << 16),
4894 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4895 av_log(avctx, AV_LOG_DEBUG, "\n");
4898 sd->effect_pcount2 = get_bits(gb, 16);
4899 if (sd->effect_pcount2 > 10) {
4900 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4902 } else if (sd->effect_pcount2) {
4904 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4905 while (++i < sd->effect_pcount2) {
4906 sd->effect_params2[i] = get_fp_val(gb);
4907 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4908 sd->effect_params2[i] / (1 << 16),
4909 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4911 av_log(avctx, AV_LOG_DEBUG, "\n");
4914 if (sd->effect_flag = get_bits1(gb))
4915 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4917 if (get_bits_count(gb) >= gb->size_in_bits +
4918 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4919 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4920 if (get_bits_count(gb) < gb->size_in_bits - 8)
4921 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4924 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4926 int i, plane, row, sprite;
4927 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4928 uint8_t* src_h[2][2];
4929 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4931 MpegEncContext *s = &v->s;
4933 for (i = 0; i < 2; i++) {
4934 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4935 xadv[i] = sd->coefs[i][0];
4936 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4937 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4939 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4940 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4942 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4944 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4945 int width = v->output_width>>!!plane;
4947 for (row = 0; row < v->output_height>>!!plane; row++) {
4948 uint8_t *dst = v->sprite_output_frame.data[plane] +
4949 v->sprite_output_frame.linesize[plane] * row;
4951 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4952 uint8_t *iplane = s->current_picture.f.data[plane];
4953 int iline = s->current_picture.f.linesize[plane];
4954 int ycoord = yoff[sprite] + yadv[sprite] * row;
4955 int yline = ycoord >> 16;
4957 ysub[sprite] = ycoord & 0xFFFF;
4959 iplane = s->last_picture.f.data[plane];
4960 iline = s->last_picture.f.linesize[plane];
4962 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4963 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4964 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4966 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4968 if (sr_cache[sprite][0] != yline) {
4969 if (sr_cache[sprite][1] == yline) {
4970 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4971 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4973 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4974 sr_cache[sprite][0] = yline;
4977 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4978 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4979 iplane + next_line, xoff[sprite],
4980 xadv[sprite], width);
4981 sr_cache[sprite][1] = yline + 1;
4983 src_h[sprite][0] = v->sr_rows[sprite][0];
4984 src_h[sprite][1] = v->sr_rows[sprite][1];
4988 if (!v->two_sprites) {
4990 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4992 memcpy(dst, src_h[0][0], width);
4995 if (ysub[0] && ysub[1]) {
4996 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4997 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4998 } else if (ysub[0]) {
4999 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5000 src_h[1][0], alpha, width);
5001 } else if (ysub[1]) {
5002 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5003 src_h[0][0], (1<<16)-1-alpha, width);
5005 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5011 for (i = 0; i < 2; i++) {
5021 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5023 MpegEncContext *s = &v->s;
5024 AVCodecContext *avctx = s->avctx;
5027 vc1_parse_sprites(v, gb, &sd);
5029 if (!s->current_picture.f.data[0]) {
5030 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5034 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5035 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5039 if (v->sprite_output_frame.data[0])
5040 avctx->release_buffer(avctx, &v->sprite_output_frame);
5042 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5043 v->sprite_output_frame.reference = 0;
5044 if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
5045 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5049 vc1_draw_sprites(v, &sd);
5054 static void vc1_sprite_flush(AVCodecContext *avctx)
5056 VC1Context *v = avctx->priv_data;
5057 MpegEncContext *s = &v->s;
5058 AVFrame *f = &s->current_picture.f;
5061 /* Windows Media Image codecs have a convergence interval of two keyframes.
5062 Since we can't enforce it, clear to black the missing sprite. This is
5063 wrong but it looks better than doing nothing. */
5066 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5067 for (i = 0; i < v->sprite_height>>!!plane; i++)
5068 memset(f->data[plane] + i * f->linesize[plane],
5069 plane ? 128 : 0, f->linesize[plane]);
5074 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5076 MpegEncContext *s = &v->s;
5079 /* Allocate mb bitplanes */
5080 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5081 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5082 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5083 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5084 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5085 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5087 v->n_allocated_blks = s->mb_width + 2;
5088 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5089 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5090 v->cbp = v->cbp_base + s->mb_stride;
5091 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5092 v->ttblk = v->ttblk_base + s->mb_stride;
5093 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5094 v->is_intra = v->is_intra_base + s->mb_stride;
5095 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5096 v->luma_mv = v->luma_mv_base + s->mb_stride;
5098 /* allocate block type info in that way so it could be used with s->block_index[] */
5099 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5100 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5101 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5102 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5104 /* allocate memory to store block level MV info */
5105 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5106 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5107 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5108 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5109 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5110 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5111 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5112 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);
5113 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5114 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5115 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);
5117 /* Init coded blocks info */
5118 if (v->profile == PROFILE_ADVANCED) {
5119 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5121 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5125 ff_intrax8_common_init(&v->x8,s);
5127 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5128 for (i = 0; i < 4; i++)
5129 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5132 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5133 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5140 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5143 for (i = 0; i < 64; i++) {
5144 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5145 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5146 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5147 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5148 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5149 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5155 /** Initialize a VC1/WMV3 decoder
5156 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5157 * @todo TODO: Decypher remaining bits in extra_data
5159 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5161 VC1Context *v = avctx->priv_data;
5162 MpegEncContext *s = &v->s;
5165 /* save the container output size for WMImage */
5166 v->output_width = avctx->width;
5167 v->output_height = avctx->height;
5169 if (!avctx->extradata_size || !avctx->extradata)
5171 if (!(avctx->flags & CODEC_FLAG_GRAY))
5172 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5174 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5175 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5177 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5178 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5180 if (avctx->idct_algo == FF_IDCT_AUTO) {
5181 avctx->idct_algo = FF_IDCT_WMV2;
5184 if (ff_vc1_init_common(v) < 0)
5186 // ensure static VLC tables are initialized
5187 if (ff_msmpeg4_decode_init(avctx) < 0)
5189 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5191 // Hack to ensure the above functions will be called
5192 // again once we know all necessary settings.
5193 // That this is necessary might indicate a bug.
5194 ff_vc1_decode_end(avctx);
5195 ff_vc1dsp_init(&v->vc1dsp);
5197 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5200 // looks like WMV3 has a sequence header stored in the extradata
5201 // advanced sequence header may be before the first frame
5202 // the last byte of the extradata is a version number, 1 for the
5203 // samples we can decode
5205 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5207 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5210 count = avctx->extradata_size*8 - get_bits_count(&gb);
5212 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5213 count, get_bits(&gb, count));
5214 } else if (count < 0) {
5215 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5217 } else { // VC1/WVC1/WVP2
5218 const uint8_t *start = avctx->extradata;
5219 uint8_t *end = avctx->extradata + avctx->extradata_size;
5220 const uint8_t *next;
5221 int size, buf2_size;
5222 uint8_t *buf2 = NULL;
5223 int seq_initialized = 0, ep_initialized = 0;
5225 if (avctx->extradata_size < 16) {
5226 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5230 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5231 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5233 for (; next < end; start = next) {
5234 next = find_next_marker(start + 4, end);
5235 size = next - start - 4;
5238 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5239 init_get_bits(&gb, buf2, buf2_size * 8);
5240 switch (AV_RB32(start)) {
5241 case VC1_CODE_SEQHDR:
5242 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5246 seq_initialized = 1;
5248 case VC1_CODE_ENTRYPOINT:
5249 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5258 if (!seq_initialized || !ep_initialized) {
5259 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5262 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5265 avctx->profile = v->profile;
5266 if (v->profile == PROFILE_ADVANCED)
5267 avctx->level = v->level;
5269 avctx->has_b_frames = !!avctx->max_b_frames;
5271 s->mb_width = (avctx->coded_width + 15) >> 4;
5272 s->mb_height = (avctx->coded_height + 15) >> 4;
5274 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5275 ff_vc1_init_transposed_scantables(v);
5277 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5282 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5283 v->sprite_width = avctx->coded_width;
5284 v->sprite_height = avctx->coded_height;
5286 avctx->coded_width = avctx->width = v->output_width;
5287 avctx->coded_height = avctx->height = v->output_height;
5289 // prevent 16.16 overflows
5290 if (v->sprite_width > 1 << 14 ||
5291 v->sprite_height > 1 << 14 ||
5292 v->output_width > 1 << 14 ||
5293 v->output_height > 1 << 14) return -1;
5298 /** Close a VC1/WMV3 decoder
5299 * @warning Initial try at using MpegEncContext stuff
5301 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5303 VC1Context *v = avctx->priv_data;
5306 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5307 && v->sprite_output_frame.data[0])
5308 avctx->release_buffer(avctx, &v->sprite_output_frame);
5309 for (i = 0; i < 4; i++)
5310 av_freep(&v->sr_rows[i >> 1][i & 1]);
5311 av_freep(&v->hrd_rate);
5312 av_freep(&v->hrd_buffer);
5313 ff_MPV_common_end(&v->s);
5314 av_freep(&v->mv_type_mb_plane);
5315 av_freep(&v->direct_mb_plane);
5316 av_freep(&v->forward_mb_plane);
5317 av_freep(&v->fieldtx_plane);
5318 av_freep(&v->acpred_plane);
5319 av_freep(&v->over_flags_plane);
5320 av_freep(&v->mb_type_base);
5321 av_freep(&v->blk_mv_type_base);
5322 av_freep(&v->mv_f_base);
5323 av_freep(&v->mv_f_last_base);
5324 av_freep(&v->mv_f_next_base);
5325 av_freep(&v->block);
5326 av_freep(&v->cbp_base);
5327 av_freep(&v->ttblk_base);
5328 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5329 av_freep(&v->luma_mv_base);
5330 ff_intrax8_common_end(&v->x8);
5335 /** Decode a VC1/WMV3 frame
5336 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5338 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5339 int *got_frame, AVPacket *avpkt)
5341 const uint8_t *buf = avpkt->data;
5342 int buf_size = avpkt->size, n_slices = 0, i;
5343 VC1Context *v = avctx->priv_data;
5344 MpegEncContext *s = &v->s;
5345 AVFrame *pict = data;
5346 uint8_t *buf2 = NULL;
5347 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5348 int mb_height, n_slices1=-1;
5353 } *slices = NULL, *tmp;
5355 v->second_field = 0;
5357 if(s->flags & CODEC_FLAG_LOW_DELAY)
5360 /* no supplementary picture */
5361 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5362 /* special case for last picture */
5363 if (s->low_delay == 0 && s->next_picture_ptr) {
5364 *pict = s->next_picture_ptr->f;
5365 s->next_picture_ptr = NULL;
5373 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5374 if (v->profile < PROFILE_ADVANCED)
5375 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5377 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5380 //for advanced profile we may need to parse and unescape data
5381 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5383 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5385 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5386 const uint8_t *start, *end, *next;
5390 for (start = buf, end = buf + buf_size; next < end; start = next) {
5391 next = find_next_marker(start + 4, end);
5392 size = next - start - 4;
5393 if (size <= 0) continue;
5394 switch (AV_RB32(start)) {
5395 case VC1_CODE_FRAME:
5396 if (avctx->hwaccel ||
5397 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5399 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5401 case VC1_CODE_FIELD: {
5403 if (avctx->hwaccel ||
5404 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5405 buf_start_second_field = start;
5406 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5410 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5411 if (!slices[n_slices].buf)
5413 buf_size3 = vc1_unescape_buffer(start + 4, size,
5414 slices[n_slices].buf);
5415 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5417 /* assuming that the field marker is at the exact middle,
5418 hope it's correct */
5419 slices[n_slices].mby_start = s->mb_height >> 1;
5420 n_slices1 = n_slices - 1; // index of the last slice of the first field
5424 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5425 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5426 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5427 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5429 case VC1_CODE_SLICE: {
5431 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5435 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5436 if (!slices[n_slices].buf)
5438 buf_size3 = vc1_unescape_buffer(start + 4, size,
5439 slices[n_slices].buf);
5440 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5442 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5448 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5449 const uint8_t *divider;
5452 divider = find_next_marker(buf, buf + buf_size);
5453 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5454 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5456 } else { // found field marker, unescape second field
5457 if (avctx->hwaccel ||
5458 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5459 buf_start_second_field = divider;
5460 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5464 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5465 if (!slices[n_slices].buf)
5467 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5468 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5470 slices[n_slices].mby_start = s->mb_height >> 1;
5471 n_slices1 = n_slices - 1;
5474 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5476 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5478 init_get_bits(&s->gb, buf2, buf_size2*8);
5480 init_get_bits(&s->gb, buf, buf_size*8);
5482 if (v->res_sprite) {
5483 v->new_sprite = !get_bits1(&s->gb);
5484 v->two_sprites = get_bits1(&s->gb);
5485 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5486 we're using the sprite compositor. These are intentionally kept separate
5487 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5488 the vc1 one for WVP2 */
5489 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5490 if (v->new_sprite) {
5491 // switch AVCodecContext parameters to those of the sprites
5492 avctx->width = avctx->coded_width = v->sprite_width;
5493 avctx->height = avctx->coded_height = v->sprite_height;
5500 if (s->context_initialized &&
5501 (s->width != avctx->coded_width ||
5502 s->height != avctx->coded_height)) {
5503 ff_vc1_decode_end(avctx);
5506 if (!s->context_initialized) {
5507 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5510 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5512 if (v->profile == PROFILE_ADVANCED) {
5513 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5515 s->h_edge_pos = avctx->coded_width;
5516 s->v_edge_pos = avctx->coded_height;
5520 /* We need to set current_picture_ptr before reading the header,
5521 * otherwise we cannot store anything in there. */
5522 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5523 int i = ff_find_unused_picture(s, 0);
5526 s->current_picture_ptr = &s->picture[i];
5529 // do parse frame header
5530 v->pic_header_flag = 0;
5531 v->first_pic_header_flag = 1;
5532 if (v->profile < PROFILE_ADVANCED) {
5533 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5537 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5541 v->first_pic_header_flag = 0;
5543 if (avctx->debug & FF_DEBUG_PICT_INFO)
5544 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5546 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5547 && s->pict_type != AV_PICTURE_TYPE_I) {
5548 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5552 if ((s->mb_height >> v->field_mode) == 0) {
5553 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5557 // process pulldown flags
5558 s->current_picture_ptr->f.repeat_pict = 0;
5559 // Pulldown flags are only valid when 'broadcast' has been set.
5560 // So ticks_per_frame will be 2
5563 s->current_picture_ptr->f.repeat_pict = 1;
5564 } else if (v->rptfrm) {
5566 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5569 // for skipping the frame
5570 s->current_picture.f.pict_type = s->pict_type;
5571 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5573 /* skip B-frames if we don't have reference frames */
5574 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5577 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5578 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5579 avctx->skip_frame >= AVDISCARD_ALL) {
5583 if (s->next_p_frame_damaged) {
5584 if (s->pict_type == AV_PICTURE_TYPE_B)
5587 s->next_p_frame_damaged = 0;
5590 if (ff_MPV_frame_start(s, avctx) < 0) {
5594 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5595 v->s.current_picture_ptr->f.top_field_first = v->tff;
5597 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5598 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5600 if ((CONFIG_VC1_VDPAU_DECODER)
5601 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5602 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5603 else if (avctx->hwaccel) {
5604 if (v->field_mode && buf_start_second_field) {
5605 // decode first field
5606 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5607 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5609 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5611 if (avctx->hwaccel->end_frame(avctx) < 0)
5614 // decode second field
5615 s->gb = slices[n_slices1 + 1].gb;
5616 s->picture_structure = PICT_TOP_FIELD + v->tff;
5617 v->second_field = 1;
5618 v->pic_header_flag = 0;
5619 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5620 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5623 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
5625 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5627 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5629 if (avctx->hwaccel->end_frame(avctx) < 0)
5632 s->picture_structure = PICT_FRAME;
5633 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5635 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5637 if (avctx->hwaccel->end_frame(avctx) < 0)
5641 if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5642 goto err; // This codepath is still incomplete thus it is disabled
5644 ff_er_frame_start(s);
5646 v->bits = buf_size * 8;
5647 v->end_mb_x = s->mb_width;
5648 if (v->field_mode) {
5650 s->current_picture.f.linesize[0] <<= 1;
5651 s->current_picture.f.linesize[1] <<= 1;
5652 s->current_picture.f.linesize[2] <<= 1;
5654 s->uvlinesize <<= 1;
5655 tmp[0] = v->mv_f_last[0];
5656 tmp[1] = v->mv_f_last[1];
5657 v->mv_f_last[0] = v->mv_f_next[0];
5658 v->mv_f_last[1] = v->mv_f_next[1];
5659 v->mv_f_next[0] = v->mv_f[0];
5660 v->mv_f_next[1] = v->mv_f[1];
5661 v->mv_f[0] = tmp[0];
5662 v->mv_f[1] = tmp[1];
5664 mb_height = s->mb_height >> v->field_mode;
5665 for (i = 0; i <= n_slices; i++) {
5666 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5667 if (v->field_mode <= 0) {
5668 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5669 "picture boundary (%d >= %d)\n", i,
5670 slices[i - 1].mby_start, mb_height);
5673 v->second_field = 1;
5674 v->blocks_off = s->mb_width * s->mb_height << 1;
5675 v->mb_off = s->mb_stride * s->mb_height >> 1;
5677 v->second_field = 0;
5682 v->pic_header_flag = 0;
5683 if (v->field_mode && i == n_slices1 + 2) {
5684 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5685 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5688 } else if (get_bits1(&s->gb)) {
5689 v->pic_header_flag = 1;
5690 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5691 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5696 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5697 if (!v->field_mode || v->second_field)
5698 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5700 if (i >= n_slices) {
5701 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5704 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5706 if (s->end_mb_y <= s->start_mb_y) {
5707 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5710 ff_vc1_decode_blocks(v);
5712 s->gb = slices[i].gb;
5714 if (v->field_mode) {
5715 v->second_field = 0;
5716 if (s->pict_type == AV_PICTURE_TYPE_B) {
5717 memcpy(v->mv_f_base, v->mv_f_next_base,
5718 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5720 s->current_picture.f.linesize[0] >>= 1;
5721 s->current_picture.f.linesize[1] >>= 1;
5722 s->current_picture.f.linesize[2] >>= 1;
5724 s->uvlinesize >>= 1;
5726 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5727 get_bits_count(&s->gb), s->gb.size_in_bits);
5728 // if (get_bits_count(&s->gb) > buf_size * 8)
5730 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5736 ff_MPV_frame_end(s);
5738 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5740 avctx->width = avctx->coded_width = v->output_width;
5741 avctx->height = avctx->coded_height = v->output_height;
5742 if (avctx->skip_frame >= AVDISCARD_NONREF)
5744 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5745 if (vc1_decode_sprites(v, &s->gb))
5748 *pict = v->sprite_output_frame;
5751 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5752 *pict = s->current_picture_ptr->f;
5753 } else if (s->last_picture_ptr != NULL) {
5754 *pict = s->last_picture_ptr->f;
5756 if (s->last_picture_ptr || s->low_delay) {
5758 ff_print_debug_info(s, pict);
5764 for (i = 0; i < n_slices; i++)
5765 av_free(slices[i].buf);
5771 for (i = 0; i < n_slices; i++)
5772 av_free(slices[i].buf);
5778 static const AVProfile profiles[] = {
5779 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5780 { FF_PROFILE_VC1_MAIN, "Main" },
5781 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5782 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5783 { FF_PROFILE_UNKNOWN },
5786 AVCodec ff_vc1_decoder = {
5788 .type = AVMEDIA_TYPE_VIDEO,
5789 .id = AV_CODEC_ID_VC1,
5790 .priv_data_size = sizeof(VC1Context),
5791 .init = vc1_decode_init,
5792 .close = ff_vc1_decode_end,
5793 .decode = vc1_decode_frame,
5794 .flush = ff_mpeg_flush,
5795 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5796 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5797 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5798 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5801 #if CONFIG_WMV3_DECODER
5802 AVCodec ff_wmv3_decoder = {
5804 .type = AVMEDIA_TYPE_VIDEO,
5805 .id = AV_CODEC_ID_WMV3,
5806 .priv_data_size = sizeof(VC1Context),
5807 .init = vc1_decode_init,
5808 .close = ff_vc1_decode_end,
5809 .decode = vc1_decode_frame,
5810 .flush = ff_mpeg_flush,
5811 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5812 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5813 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5814 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5818 #if CONFIG_WMV3_VDPAU_DECODER
5819 AVCodec ff_wmv3_vdpau_decoder = {
5820 .name = "wmv3_vdpau",
5821 .type = AVMEDIA_TYPE_VIDEO,
5822 .id = AV_CODEC_ID_WMV3,
5823 .priv_data_size = sizeof(VC1Context),
5824 .init = vc1_decode_init,
5825 .close = ff_vc1_decode_end,
5826 .decode = vc1_decode_frame,
5827 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5828 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5829 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5830 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5834 #if CONFIG_VC1_VDPAU_DECODER
5835 AVCodec ff_vc1_vdpau_decoder = {
5836 .name = "vc1_vdpau",
5837 .type = AVMEDIA_TYPE_VIDEO,
5838 .id = AV_CODEC_ID_VC1,
5839 .priv_data_size = sizeof(VC1Context),
5840 .init = vc1_decode_init,
5841 .close = ff_vc1_decode_end,
5842 .decode = vc1_decode_frame,
5843 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5844 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5845 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5846 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5850 #if CONFIG_WMV3IMAGE_DECODER
5851 AVCodec ff_wmv3image_decoder = {
5852 .name = "wmv3image",
5853 .type = AVMEDIA_TYPE_VIDEO,
5854 .id = AV_CODEC_ID_WMV3IMAGE,
5855 .priv_data_size = sizeof(VC1Context),
5856 .init = vc1_decode_init,
5857 .close = ff_vc1_decode_end,
5858 .decode = vc1_decode_frame,
5859 .capabilities = CODEC_CAP_DR1,
5860 .flush = vc1_sprite_flush,
5861 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5862 .pix_fmts = ff_pixfmt_list_420
5866 #if CONFIG_VC1IMAGE_DECODER
5867 AVCodec ff_vc1image_decoder = {
5869 .type = AVMEDIA_TYPE_VIDEO,
5870 .id = AV_CODEC_ID_VC1IMAGE,
5871 .priv_data_size = sizeof(VC1Context),
5872 .init = vc1_decode_init,
5873 .close = ff_vc1_decode_end,
5874 .decode = vc1_decode_frame,
5875 .capabilities = CODEC_CAP_DR1,
5876 .flush = vc1_sprite_flush,
5877 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5878 .pix_fmts = ff_pixfmt_list_420