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);
1153 *pred_flag = *dmv_y & 1;
1154 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1156 *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
1161 av_assert0(index < esc);
1163 offs_tab = offset_table2;
1165 offs_tab = offset_table1;
1166 index1 = (index + 1) % 9;
1168 val = get_bits(gb, index1 + extend_x);
1169 sign = 0 -(val & 1);
1170 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1174 offs_tab = offset_table2;
1176 offs_tab = offset_table1;
1177 index1 = (index + 1) / 9;
1178 if (index1 > v->numref) {
1179 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1180 sign = 0 - (val & 1);
1181 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1184 if (v->numref && pred_flag)
1185 *pred_flag = index1 & 1;
1189 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1191 int scaledvalue, refdist;
1192 int scalesame1, scalesame2;
1193 int scalezone1_x, zone1offset_x;
1194 int table_index = dir ^ v->second_field;
1196 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1197 refdist = v->refdist;
1199 refdist = dir ? v->brfd : v->frfd;
1202 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1203 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1204 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1205 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1210 if (FFABS(n) < scalezone1_x)
1211 scaledvalue = (n * scalesame1) >> 8;
1214 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1216 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1219 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1222 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1224 int scaledvalue, refdist;
1225 int scalesame1, scalesame2;
1226 int scalezone1_y, zone1offset_y;
1227 int table_index = dir ^ v->second_field;
1229 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1230 refdist = v->refdist;
1232 refdist = dir ? v->brfd : v->frfd;
1235 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1236 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1237 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1238 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1243 if (FFABS(n) < scalezone1_y)
1244 scaledvalue = (n * scalesame1) >> 8;
1247 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1249 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1253 if (v->cur_field_type && !v->ref_field_type[dir])
1254 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1256 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1259 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1261 int scalezone1_x, zone1offset_x;
1262 int scaleopp1, scaleopp2, brfd;
1265 brfd = FFMIN(v->brfd, 3);
1266 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1267 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1268 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1269 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1274 if (FFABS(n) < scalezone1_x)
1275 scaledvalue = (n * scaleopp1) >> 8;
1278 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1280 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1283 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1286 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1288 int scalezone1_y, zone1offset_y;
1289 int scaleopp1, scaleopp2, brfd;
1292 brfd = FFMIN(v->brfd, 3);
1293 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1294 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1295 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1296 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1301 if (FFABS(n) < scalezone1_y)
1302 scaledvalue = (n * scaleopp1) >> 8;
1305 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1307 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1310 if (v->cur_field_type && !v->ref_field_type[dir]) {
1311 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1313 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1317 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1320 int brfd, scalesame;
1321 int hpel = 1 - v->s.quarter_sample;
1324 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1326 n = scaleforsame_y(v, i, n, dir) << hpel;
1328 n = scaleforsame_x(v, n, dir) << hpel;
1331 brfd = FFMIN(v->brfd, 3);
1332 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1334 n = (n * scalesame >> 8) << hpel;
1338 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1341 int refdist, scaleopp;
1342 int hpel = 1 - v->s.quarter_sample;
1345 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1347 n = scaleforopp_y(v, n, dir) << hpel;
1349 n = scaleforopp_x(v, n) << hpel;
1352 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1353 refdist = FFMIN(v->refdist, 3);
1355 refdist = dir ? v->brfd : v->frfd;
1356 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1358 n = (n * scaleopp >> 8) << hpel;
1362 /** Predict and set motion vector
1364 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1365 int mv1, int r_x, int r_y, uint8_t* is_intra,
1366 int pred_flag, int dir)
1368 MpegEncContext *s = &v->s;
1369 int xy, wrap, off = 0;
1373 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1374 int opposite, a_f, b_f, c_f;
1375 int16_t field_predA[2];
1376 int16_t field_predB[2];
1377 int16_t field_predC[2];
1378 int a_valid, b_valid, c_valid;
1379 int hybridmv_thresh, y_bias = 0;
1381 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1382 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1386 /* scale MV difference to be quad-pel */
1387 dmv_x <<= 1 - s->quarter_sample;
1388 dmv_y <<= 1 - s->quarter_sample;
1390 wrap = s->b8_stride;
1391 xy = s->block_index[n];
1394 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1395 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1396 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1397 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1398 if (mv1) { /* duplicate motion data for 1-MV block */
1399 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1400 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1401 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1402 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1403 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1404 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1405 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1406 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1407 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1408 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1409 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1410 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1411 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1416 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1417 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1419 if (v->field_mode && mixedmv_pic)
1420 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1422 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1424 //in 4-MV mode different blocks have different B predictor position
1427 off = (s->mb_x > 0) ? -1 : 1;
1430 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1439 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1441 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1442 b_valid = a_valid && (s->mb_width > 1);
1443 c_valid = s->mb_x || (n == 1 || n == 3);
1444 if (v->field_mode) {
1445 a_valid = a_valid && !is_intra[xy - wrap];
1446 b_valid = b_valid && !is_intra[xy - wrap + off];
1447 c_valid = c_valid && !is_intra[xy - 1];
1451 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1452 num_oppfield += a_f;
1453 num_samefield += 1 - a_f;
1454 field_predA[0] = A[0];
1455 field_predA[1] = A[1];
1457 field_predA[0] = field_predA[1] = 0;
1461 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1462 num_oppfield += b_f;
1463 num_samefield += 1 - b_f;
1464 field_predB[0] = B[0];
1465 field_predB[1] = B[1];
1467 field_predB[0] = field_predB[1] = 0;
1471 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1472 num_oppfield += c_f;
1473 num_samefield += 1 - c_f;
1474 field_predC[0] = C[0];
1475 field_predC[1] = C[1];
1477 field_predC[0] = field_predC[1] = 0;
1481 if (v->field_mode) {
1483 // REFFIELD determines if the last field or the second-last field is
1484 // to be used as reference
1485 opposite = 1 - v->reffield;
1487 if (num_samefield <= num_oppfield)
1488 opposite = 1 - pred_flag;
1490 opposite = pred_flag;
1495 if (a_valid && !a_f) {
1496 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1497 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1499 if (b_valid && !b_f) {
1500 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1501 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1503 if (c_valid && !c_f) {
1504 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1505 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1507 v->mv_f[dir][xy + v->blocks_off] = 1;
1508 v->ref_field_type[dir] = !v->cur_field_type;
1510 if (a_valid && a_f) {
1511 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1512 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1514 if (b_valid && b_f) {
1515 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1516 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1518 if (c_valid && c_f) {
1519 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1520 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1522 v->mv_f[dir][xy + v->blocks_off] = 0;
1523 v->ref_field_type[dir] = v->cur_field_type;
1527 px = field_predA[0];
1528 py = field_predA[1];
1529 } else if (c_valid) {
1530 px = field_predC[0];
1531 py = field_predC[1];
1532 } else if (b_valid) {
1533 px = field_predB[0];
1534 py = field_predB[1];
1540 if (num_samefield + num_oppfield > 1) {
1541 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1542 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1545 /* Pullback MV as specified in 8.3.5.3.4 */
1546 if (!v->field_mode) {
1548 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1549 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1550 X = (s->mb_width << 6) - 4;
1551 Y = (s->mb_height << 6) - 4;
1553 if (qx + px < -60) px = -60 - qx;
1554 if (qy + py < -60) py = -60 - qy;
1556 if (qx + px < -28) px = -28 - qx;
1557 if (qy + py < -28) py = -28 - qy;
1559 if (qx + px > X) px = X - qx;
1560 if (qy + py > Y) py = Y - qy;
1563 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1564 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1565 hybridmv_thresh = 32;
1566 if (a_valid && c_valid) {
1567 if (is_intra[xy - wrap])
1568 sum = FFABS(px) + FFABS(py);
1570 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1571 if (sum > hybridmv_thresh) {
1572 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1573 px = field_predA[0];
1574 py = field_predA[1];
1576 px = field_predC[0];
1577 py = field_predC[1];
1580 if (is_intra[xy - 1])
1581 sum = FFABS(px) + FFABS(py);
1583 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1584 if (sum > hybridmv_thresh) {
1585 if (get_bits1(&s->gb)) {
1586 px = field_predA[0];
1587 py = field_predA[1];
1589 px = field_predC[0];
1590 py = field_predC[1];
1597 if (v->field_mode && v->numref)
1599 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1601 /* store MV using signed modulus of MV range defined in 4.11 */
1602 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;
1603 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;
1604 if (mv1) { /* duplicate motion data for 1-MV block */
1605 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];
1606 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];
1607 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];
1608 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];
1609 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];
1610 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];
1611 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1612 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];
1616 /** Predict and set motion vector for interlaced frame picture MBs
1618 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1619 int mvn, int r_x, int r_y, uint8_t* is_intra)
1621 MpegEncContext *s = &v->s;
1622 int xy, wrap, off = 0;
1623 int A[2], B[2], C[2];
1625 int a_valid = 0, b_valid = 0, c_valid = 0;
1626 int field_a, field_b, field_c; // 0: same, 1: opposit
1627 int total_valid, num_samefield, num_oppfield;
1628 int pos_c, pos_b, n_adj;
1630 wrap = s->b8_stride;
1631 xy = s->block_index[n];
1634 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1635 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1636 s->current_picture.f.motion_val[1][xy][0] = 0;
1637 s->current_picture.f.motion_val[1][xy][1] = 0;
1638 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1639 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1640 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1641 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1642 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1643 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1644 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1645 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1646 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1647 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1648 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1649 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1650 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1651 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1656 off = ((n == 0) || (n == 1)) ? 1 : -1;
1658 if (s->mb_x || (n == 1) || (n == 3)) {
1659 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1660 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1661 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1662 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1664 } else { // current block has frame mv and cand. has field MV (so average)
1665 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1666 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1667 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1668 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1671 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1677 /* Predict B and C */
1678 B[0] = B[1] = C[0] = C[1] = 0;
1679 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1680 if (!s->first_slice_line) {
1681 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1684 pos_b = s->block_index[n_adj] - 2 * wrap;
1685 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1686 n_adj = (n & 2) | (n & 1);
1688 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1689 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1690 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1691 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1692 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1695 if (s->mb_width > 1) {
1696 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1699 pos_c = s->block_index[2] - 2 * wrap + 2;
1700 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1703 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1704 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1705 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1706 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1707 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1709 if (s->mb_x == s->mb_width - 1) {
1710 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1713 pos_c = s->block_index[3] - 2 * wrap - 2;
1714 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1717 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1718 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1719 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1720 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1721 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1730 pos_b = s->block_index[1];
1732 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1733 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1734 pos_c = s->block_index[0];
1736 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1737 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1740 total_valid = a_valid + b_valid + c_valid;
1741 // check if predictor A is out of bounds
1742 if (!s->mb_x && !(n == 1 || n == 3)) {
1745 // check if predictor B is out of bounds
1746 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1747 B[0] = B[1] = C[0] = C[1] = 0;
1749 if (!v->blk_mv_type[xy]) {
1750 if (s->mb_width == 1) {
1754 if (total_valid >= 2) {
1755 px = mid_pred(A[0], B[0], C[0]);
1756 py = mid_pred(A[1], B[1], C[1]);
1757 } else if (total_valid) {
1758 if (a_valid) { px = A[0]; py = A[1]; }
1759 else if (b_valid) { px = B[0]; py = B[1]; }
1760 else if (c_valid) { px = C[0]; py = C[1]; }
1767 field_a = (A[1] & 4) ? 1 : 0;
1771 field_b = (B[1] & 4) ? 1 : 0;
1775 field_c = (C[1] & 4) ? 1 : 0;
1779 num_oppfield = field_a + field_b + field_c;
1780 num_samefield = total_valid - num_oppfield;
1781 if (total_valid == 3) {
1782 if ((num_samefield == 3) || (num_oppfield == 3)) {
1783 px = mid_pred(A[0], B[0], C[0]);
1784 py = mid_pred(A[1], B[1], C[1]);
1785 } else if (num_samefield >= num_oppfield) {
1786 /* take one MV from same field set depending on priority
1787 the check for B may not be necessary */
1788 px = !field_a ? A[0] : B[0];
1789 py = !field_a ? A[1] : B[1];
1791 px = field_a ? A[0] : B[0];
1792 py = field_a ? A[1] : B[1];
1794 } else if (total_valid == 2) {
1795 if (num_samefield >= num_oppfield) {
1796 if (!field_a && a_valid) {
1799 } else if (!field_b && b_valid) {
1802 } else if (c_valid) {
1807 if (field_a && a_valid) {
1810 } else if (field_b && b_valid) {
1813 } else if (c_valid) {
1818 } else if (total_valid == 1) {
1819 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1820 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1825 /* store MV using signed modulus of MV range defined in 4.11 */
1826 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;
1827 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;
1828 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1829 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1830 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1831 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1832 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1833 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1834 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1835 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1836 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1837 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1838 s->mv[0][n + 1][0] = s->mv[0][n][0];
1839 s->mv[0][n + 1][1] = s->mv[0][n][1];
1843 /** Motion compensation for direct or interpolated blocks in B-frames
1845 static void vc1_interp_mc(VC1Context *v)
1847 MpegEncContext *s = &v->s;
1848 DSPContext *dsp = &v->s.dsp;
1849 uint8_t *srcY, *srcU, *srcV;
1850 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1852 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1854 if (!v->field_mode && !v->s.next_picture.f.data[0])
1857 mx = s->mv[1][0][0];
1858 my = s->mv[1][0][1];
1859 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1860 uvmy = (my + ((my & 3) == 3)) >> 1;
1861 if (v->field_mode) {
1862 if (v->cur_field_type != v->ref_field_type[1])
1863 my = my - 2 + 4 * v->cur_field_type;
1864 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1867 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1868 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1870 srcY = s->next_picture.f.data[0];
1871 srcU = s->next_picture.f.data[1];
1872 srcV = s->next_picture.f.data[2];
1874 src_x = s->mb_x * 16 + (mx >> 2);
1875 src_y = s->mb_y * 16 + (my >> 2);
1876 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1877 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1879 if (v->profile != PROFILE_ADVANCED) {
1880 src_x = av_clip( src_x, -16, s->mb_width * 16);
1881 src_y = av_clip( src_y, -16, s->mb_height * 16);
1882 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1883 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1885 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1886 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1887 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1888 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1891 srcY += src_y * s->linesize + src_x;
1892 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1893 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1895 if (v->field_mode && v->ref_field_type[1]) {
1896 srcY += s->current_picture_ptr->f.linesize[0];
1897 srcU += s->current_picture_ptr->f.linesize[1];
1898 srcV += s->current_picture_ptr->f.linesize[2];
1901 /* for grayscale we should not try to read from unknown area */
1902 if (s->flags & CODEC_FLAG_GRAY) {
1903 srcU = s->edge_emu_buffer + 18 * s->linesize;
1904 srcV = s->edge_emu_buffer + 18 * s->linesize;
1907 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1908 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1909 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1910 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1912 srcY -= s->mspel * (1 + s->linesize);
1913 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1914 17 + s->mspel * 2, 17 + s->mspel * 2,
1915 src_x - s->mspel, src_y - s->mspel,
1916 s->h_edge_pos, v_edge_pos);
1917 srcY = s->edge_emu_buffer;
1918 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1919 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1920 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1921 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1924 /* if we deal with range reduction we need to scale source blocks */
1925 if (v->rangeredfrm) {
1927 uint8_t *src, *src2;
1930 for (j = 0; j < 17 + s->mspel * 2; j++) {
1931 for (i = 0; i < 17 + s->mspel * 2; i++)
1932 src[i] = ((src[i] - 128) >> 1) + 128;
1937 for (j = 0; j < 9; j++) {
1938 for (i = 0; i < 9; i++) {
1939 src[i] = ((src[i] - 128) >> 1) + 128;
1940 src2[i] = ((src2[i] - 128) >> 1) + 128;
1942 src += s->uvlinesize;
1943 src2 += s->uvlinesize;
1946 srcY += s->mspel * (1 + s->linesize);
1949 if (v->field_mode && v->second_field) {
1950 off = s->current_picture_ptr->f.linesize[0];
1951 off_uv = s->current_picture_ptr->f.linesize[1];
1958 dxy = ((my & 3) << 2) | (mx & 3);
1959 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1960 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1961 srcY += s->linesize * 8;
1962 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1963 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1965 dxy = (my & 2) | ((mx & 2) >> 1);
1968 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1970 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1973 if (s->flags & CODEC_FLAG_GRAY) return;
1974 /* Chroma MC always uses qpel blilinear */
1975 uvmx = (uvmx & 3) << 1;
1976 uvmy = (uvmy & 3) << 1;
1978 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1979 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1981 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1982 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1986 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1990 #if B_FRACTION_DEN==256
1994 return 2 * ((value * n + 255) >> 9);
1995 return (value * n + 128) >> 8;
1998 n -= B_FRACTION_DEN;
2000 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2001 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2005 /** Reconstruct motion vector for B-frame and do motion compensation
2007 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2008 int direct, int mode)
2011 v->mv_mode2 = v->mv_mode;
2012 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2018 v->mv_mode = v->mv_mode2;
2021 if (mode == BMV_TYPE_INTERPOLATED) {
2025 v->mv_mode = v->mv_mode2;
2029 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2030 v->mv_mode = v->mv_mode2;
2031 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2033 v->mv_mode = v->mv_mode2;
2036 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2037 int direct, int mvtype)
2039 MpegEncContext *s = &v->s;
2040 int xy, wrap, off = 0;
2045 const uint8_t *is_intra = v->mb_type[0];
2049 /* scale MV difference to be quad-pel */
2050 dmv_x[0] <<= 1 - s->quarter_sample;
2051 dmv_y[0] <<= 1 - s->quarter_sample;
2052 dmv_x[1] <<= 1 - s->quarter_sample;
2053 dmv_y[1] <<= 1 - s->quarter_sample;
2055 wrap = s->b8_stride;
2056 xy = s->block_index[0];
2059 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2060 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2061 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2062 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2065 if (!v->field_mode) {
2066 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2067 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2068 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2069 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2071 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2072 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));
2073 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));
2074 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));
2075 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));
2078 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2079 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2080 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2081 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2085 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2086 C = s->current_picture.f.motion_val[0][xy - 2];
2087 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2088 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2089 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2091 if (!s->mb_x) C[0] = C[1] = 0;
2092 if (!s->first_slice_line) { // predictor A is not out of bounds
2093 if (s->mb_width == 1) {
2097 px = mid_pred(A[0], B[0], C[0]);
2098 py = mid_pred(A[1], B[1], C[1]);
2100 } else if (s->mb_x) { // predictor C is not out of bounds
2106 /* Pullback MV as specified in 8.3.5.3.4 */
2109 if (v->profile < PROFILE_ADVANCED) {
2110 qx = (s->mb_x << 5);
2111 qy = (s->mb_y << 5);
2112 X = (s->mb_width << 5) - 4;
2113 Y = (s->mb_height << 5) - 4;
2114 if (qx + px < -28) px = -28 - qx;
2115 if (qy + py < -28) py = -28 - qy;
2116 if (qx + px > X) px = X - qx;
2117 if (qy + py > Y) py = Y - qy;
2119 qx = (s->mb_x << 6);
2120 qy = (s->mb_y << 6);
2121 X = (s->mb_width << 6) - 4;
2122 Y = (s->mb_height << 6) - 4;
2123 if (qx + px < -60) px = -60 - qx;
2124 if (qy + py < -60) py = -60 - qy;
2125 if (qx + px > X) px = X - qx;
2126 if (qy + py > Y) py = Y - qy;
2129 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2130 if (0 && !s->first_slice_line && s->mb_x) {
2131 if (is_intra[xy - wrap])
2132 sum = FFABS(px) + FFABS(py);
2134 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2136 if (get_bits1(&s->gb)) {
2144 if (is_intra[xy - 2])
2145 sum = FFABS(px) + FFABS(py);
2147 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2149 if (get_bits1(&s->gb)) {
2159 /* store MV using signed modulus of MV range defined in 4.11 */
2160 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2161 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2163 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2164 C = s->current_picture.f.motion_val[1][xy - 2];
2165 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2166 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2167 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2171 if (!s->first_slice_line) { // predictor A is not out of bounds
2172 if (s->mb_width == 1) {
2176 px = mid_pred(A[0], B[0], C[0]);
2177 py = mid_pred(A[1], B[1], C[1]);
2179 } else if (s->mb_x) { // predictor C is not out of bounds
2185 /* Pullback MV as specified in 8.3.5.3.4 */
2188 if (v->profile < PROFILE_ADVANCED) {
2189 qx = (s->mb_x << 5);
2190 qy = (s->mb_y << 5);
2191 X = (s->mb_width << 5) - 4;
2192 Y = (s->mb_height << 5) - 4;
2193 if (qx + px < -28) px = -28 - qx;
2194 if (qy + py < -28) py = -28 - qy;
2195 if (qx + px > X) px = X - qx;
2196 if (qy + py > Y) py = Y - qy;
2198 qx = (s->mb_x << 6);
2199 qy = (s->mb_y << 6);
2200 X = (s->mb_width << 6) - 4;
2201 Y = (s->mb_height << 6) - 4;
2202 if (qx + px < -60) px = -60 - qx;
2203 if (qy + py < -60) py = -60 - qy;
2204 if (qx + px > X) px = X - qx;
2205 if (qy + py > Y) py = Y - qy;
2208 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2209 if (0 && !s->first_slice_line && s->mb_x) {
2210 if (is_intra[xy - wrap])
2211 sum = FFABS(px) + FFABS(py);
2213 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2215 if (get_bits1(&s->gb)) {
2223 if (is_intra[xy - 2])
2224 sum = FFABS(px) + FFABS(py);
2226 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2228 if (get_bits1(&s->gb)) {
2238 /* store MV using signed modulus of MV range defined in 4.11 */
2240 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2241 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2243 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2244 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2245 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2246 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2249 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2251 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2252 MpegEncContext *s = &v->s;
2253 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2255 if (v->bmvtype == BMV_TYPE_DIRECT) {
2256 int total_opp, k, f;
2257 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2258 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2259 v->bfraction, 0, s->quarter_sample);
2260 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2261 v->bfraction, 0, s->quarter_sample);
2262 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2263 v->bfraction, 1, s->quarter_sample);
2264 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2265 v->bfraction, 1, s->quarter_sample);
2267 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2268 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2269 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2270 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2271 f = (total_opp > 2) ? 1 : 0;
2273 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2274 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2277 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2278 for (k = 0; k < 4; k++) {
2279 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2280 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2281 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2282 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2283 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2284 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2288 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2289 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);
2290 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);
2293 if (dir) { // backward
2294 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);
2295 if (n == 3 || mv1) {
2296 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2299 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);
2300 if (n == 3 || mv1) {
2301 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2306 /** Get predicted DC value for I-frames only
2307 * prediction dir: left=0, top=1
2308 * @param s MpegEncContext
2309 * @param overlap flag indicating that overlap filtering is used
2310 * @param pq integer part of picture quantizer
2311 * @param[in] n block index in the current MB
2312 * @param dc_val_ptr Pointer to DC predictor
2313 * @param dir_ptr Prediction direction for use in AC prediction
2315 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2316 int16_t **dc_val_ptr, int *dir_ptr)
2318 int a, b, c, wrap, pred, scale;
2320 static const uint16_t dcpred[32] = {
2321 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2322 114, 102, 93, 85, 79, 73, 68, 64,
2323 60, 57, 54, 51, 49, 47, 45, 43,
2324 41, 39, 38, 37, 35, 34, 33
2327 /* find prediction - wmv3_dc_scale always used here in fact */
2328 if (n < 4) scale = s->y_dc_scale;
2329 else scale = s->c_dc_scale;
2331 wrap = s->block_wrap[n];
2332 dc_val = s->dc_val[0] + s->block_index[n];
2338 b = dc_val[ - 1 - wrap];
2339 a = dc_val[ - wrap];
2341 if (pq < 9 || !overlap) {
2342 /* Set outer values */
2343 if (s->first_slice_line && (n != 2 && n != 3))
2344 b = a = dcpred[scale];
2345 if (s->mb_x == 0 && (n != 1 && n != 3))
2346 b = c = dcpred[scale];
2348 /* Set outer values */
2349 if (s->first_slice_line && (n != 2 && n != 3))
2351 if (s->mb_x == 0 && (n != 1 && n != 3))
2355 if (abs(a - b) <= abs(b - c)) {
2357 *dir_ptr = 1; // left
2360 *dir_ptr = 0; // top
2363 /* update predictor */
2364 *dc_val_ptr = &dc_val[0];
2369 /** Get predicted DC value
2370 * prediction dir: left=0, top=1
2371 * @param s MpegEncContext
2372 * @param overlap flag indicating that overlap filtering is used
2373 * @param pq integer part of picture quantizer
2374 * @param[in] n block index in the current MB
2375 * @param a_avail flag indicating top block availability
2376 * @param c_avail flag indicating left block availability
2377 * @param dc_val_ptr Pointer to DC predictor
2378 * @param dir_ptr Prediction direction for use in AC prediction
2380 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2381 int a_avail, int c_avail,
2382 int16_t **dc_val_ptr, int *dir_ptr)
2384 int a, b, c, wrap, pred;
2386 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2390 wrap = s->block_wrap[n];
2391 dc_val = s->dc_val[0] + s->block_index[n];
2397 b = dc_val[ - 1 - wrap];
2398 a = dc_val[ - wrap];
2399 /* scale predictors if needed */
2400 q1 = s->current_picture.f.qscale_table[mb_pos];
2401 dqscale_index = s->y_dc_scale_table[q1] - 1;
2402 if (dqscale_index < 0)
2404 if (c_avail && (n != 1 && n != 3)) {
2405 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2407 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2409 if (a_avail && (n != 2 && n != 3)) {
2410 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2412 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2414 if (a_avail && c_avail && (n != 3)) {
2419 off -= s->mb_stride;
2420 q2 = s->current_picture.f.qscale_table[off];
2422 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2425 if (a_avail && c_avail) {
2426 if (abs(a - b) <= abs(b - c)) {
2428 *dir_ptr = 1; // left
2431 *dir_ptr = 0; // top
2433 } else if (a_avail) {
2435 *dir_ptr = 0; // top
2436 } else if (c_avail) {
2438 *dir_ptr = 1; // left
2441 *dir_ptr = 1; // left
2444 /* update predictor */
2445 *dc_val_ptr = &dc_val[0];
2449 /** @} */ // Block group
2452 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2453 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2457 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2458 uint8_t **coded_block_ptr)
2460 int xy, wrap, pred, a, b, c;
2462 xy = s->block_index[n];
2463 wrap = s->b8_stride;
2468 a = s->coded_block[xy - 1 ];
2469 b = s->coded_block[xy - 1 - wrap];
2470 c = s->coded_block[xy - wrap];
2479 *coded_block_ptr = &s->coded_block[xy];
2485 * Decode one AC coefficient
2486 * @param v The VC1 context
2487 * @param last Last coefficient
2488 * @param skip How much zero coefficients to skip
2489 * @param value Decoded AC coefficient value
2490 * @param codingset set of VLC to decode data
2493 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2494 int *value, int codingset)
2496 GetBitContext *gb = &v->s.gb;
2497 int index, escape, run = 0, level = 0, lst = 0;
2499 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2500 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2501 run = vc1_index_decode_table[codingset][index][0];
2502 level = vc1_index_decode_table[codingset][index][1];
2503 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2507 escape = decode210(gb);
2509 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2510 run = vc1_index_decode_table[codingset][index][0];
2511 level = vc1_index_decode_table[codingset][index][1];
2512 lst = index >= vc1_last_decode_table[codingset];
2515 level += vc1_last_delta_level_table[codingset][run];
2517 level += vc1_delta_level_table[codingset][run];
2520 run += vc1_last_delta_run_table[codingset][level] + 1;
2522 run += vc1_delta_run_table[codingset][level] + 1;
2528 lst = get_bits1(gb);
2529 if (v->s.esc3_level_length == 0) {
2530 if (v->pq < 8 || v->dquantfrm) { // table 59
2531 v->s.esc3_level_length = get_bits(gb, 3);
2532 if (!v->s.esc3_level_length)
2533 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2534 } else { // table 60
2535 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2537 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2539 run = get_bits(gb, v->s.esc3_run_length);
2540 sign = get_bits1(gb);
2541 level = get_bits(gb, v->s.esc3_level_length);
2552 /** Decode intra block in intra frames - should be faster than decode_intra_block
2553 * @param v VC1Context
2554 * @param block block to decode
2555 * @param[in] n subblock index
2556 * @param coded are AC coeffs present or not
2557 * @param codingset set of VLC to decode data
2559 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2560 int coded, int codingset)
2562 GetBitContext *gb = &v->s.gb;
2563 MpegEncContext *s = &v->s;
2564 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2567 int16_t *ac_val, *ac_val2;
2570 /* Get DC differential */
2572 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2574 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2577 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2581 if (dcdiff == 119 /* ESC index value */) {
2582 /* TODO: Optimize */
2583 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2584 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2585 else dcdiff = get_bits(gb, 8);
2588 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2589 else if (v->pq == 2)
2590 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2597 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2600 /* Store the quantized DC coeff, used for prediction */
2602 block[0] = dcdiff * s->y_dc_scale;
2604 block[0] = dcdiff * s->c_dc_scale;
2615 int last = 0, skip, value;
2616 const uint8_t *zz_table;
2620 scale = v->pq * 2 + v->halfpq;
2624 zz_table = v->zz_8x8[2];
2626 zz_table = v->zz_8x8[3];
2628 zz_table = v->zz_8x8[1];
2630 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2632 if (dc_pred_dir) // left
2635 ac_val -= 16 * s->block_wrap[n];
2638 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2642 block[zz_table[i++]] = value;
2645 /* apply AC prediction if needed */
2647 if (dc_pred_dir) { // left
2648 for (k = 1; k < 8; k++)
2649 block[k << v->left_blk_sh] += ac_val[k];
2651 for (k = 1; k < 8; k++)
2652 block[k << v->top_blk_sh] += ac_val[k + 8];
2655 /* save AC coeffs for further prediction */
2656 for (k = 1; k < 8; k++) {
2657 ac_val2[k] = block[k << v->left_blk_sh];
2658 ac_val2[k + 8] = block[k << v->top_blk_sh];
2661 /* scale AC coeffs */
2662 for (k = 1; k < 64; k++)
2666 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2669 if (s->ac_pred) i = 63;
2675 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2679 scale = v->pq * 2 + v->halfpq;
2680 memset(ac_val2, 0, 16 * 2);
2681 if (dc_pred_dir) { // left
2684 memcpy(ac_val2, ac_val, 8 * 2);
2686 ac_val -= 16 * s->block_wrap[n];
2688 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2691 /* apply AC prediction if needed */
2693 if (dc_pred_dir) { //left
2694 for (k = 1; k < 8; k++) {
2695 block[k << v->left_blk_sh] = ac_val[k] * scale;
2696 if (!v->pquantizer && block[k << v->left_blk_sh])
2697 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2700 for (k = 1; k < 8; k++) {
2701 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2702 if (!v->pquantizer && block[k << v->top_blk_sh])
2703 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2709 s->block_last_index[n] = i;
2714 /** Decode intra block in intra frames - should be faster than decode_intra_block
2715 * @param v VC1Context
2716 * @param block block to decode
2717 * @param[in] n subblock number
2718 * @param coded are AC coeffs present or not
2719 * @param codingset set of VLC to decode data
2720 * @param mquant quantizer value for this macroblock
2722 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2723 int coded, int codingset, int mquant)
2725 GetBitContext *gb = &v->s.gb;
2726 MpegEncContext *s = &v->s;
2727 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2730 int16_t *ac_val, *ac_val2;
2732 int a_avail = v->a_avail, c_avail = v->c_avail;
2733 int use_pred = s->ac_pred;
2736 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2738 /* Get DC differential */
2740 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2742 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2745 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2749 if (dcdiff == 119 /* ESC index value */) {
2750 /* TODO: Optimize */
2751 if (mquant == 1) dcdiff = get_bits(gb, 10);
2752 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2753 else dcdiff = get_bits(gb, 8);
2756 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2757 else if (mquant == 2)
2758 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2765 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2768 /* Store the quantized DC coeff, used for prediction */
2770 block[0] = dcdiff * s->y_dc_scale;
2772 block[0] = dcdiff * s->c_dc_scale;
2778 /* check if AC is needed at all */
2779 if (!a_avail && !c_avail)
2781 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2784 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2786 if (dc_pred_dir) // left
2789 ac_val -= 16 * s->block_wrap[n];
2791 q1 = s->current_picture.f.qscale_table[mb_pos];
2792 if ( dc_pred_dir && c_avail && mb_pos)
2793 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2794 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2795 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2796 if ( dc_pred_dir && n == 1)
2798 if (!dc_pred_dir && n == 2)
2804 int last = 0, skip, value;
2805 const uint8_t *zz_table;
2809 if (!use_pred && v->fcm == ILACE_FRAME) {
2810 zz_table = v->zzi_8x8;
2812 if (!dc_pred_dir) // top
2813 zz_table = v->zz_8x8[2];
2815 zz_table = v->zz_8x8[3];
2818 if (v->fcm != ILACE_FRAME)
2819 zz_table = v->zz_8x8[1];
2821 zz_table = v->zzi_8x8;
2825 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2829 block[zz_table[i++]] = value;
2832 /* apply AC prediction if needed */
2834 /* scale predictors if needed*/
2835 if (q2 && q1 != q2) {
2836 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2837 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2840 return AVERROR_INVALIDDATA;
2841 if (dc_pred_dir) { // left
2842 for (k = 1; k < 8; k++)
2843 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2845 for (k = 1; k < 8; k++)
2846 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2849 if (dc_pred_dir) { //left
2850 for (k = 1; k < 8; k++)
2851 block[k << v->left_blk_sh] += ac_val[k];
2853 for (k = 1; k < 8; k++)
2854 block[k << v->top_blk_sh] += ac_val[k + 8];
2858 /* save AC coeffs for further prediction */
2859 for (k = 1; k < 8; k++) {
2860 ac_val2[k ] = block[k << v->left_blk_sh];
2861 ac_val2[k + 8] = block[k << v->top_blk_sh];
2864 /* scale AC coeffs */
2865 for (k = 1; k < 64; k++)
2869 block[k] += (block[k] < 0) ? -mquant : mquant;
2872 if (use_pred) i = 63;
2873 } else { // no AC coeffs
2876 memset(ac_val2, 0, 16 * 2);
2877 if (dc_pred_dir) { // left
2879 memcpy(ac_val2, ac_val, 8 * 2);
2880 if (q2 && q1 != q2) {
2881 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2882 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2884 return AVERROR_INVALIDDATA;
2885 for (k = 1; k < 8; k++)
2886 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2891 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2892 if (q2 && q1 != q2) {
2893 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2894 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2896 return AVERROR_INVALIDDATA;
2897 for (k = 1; k < 8; k++)
2898 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2903 /* apply AC prediction if needed */
2905 if (dc_pred_dir) { // left
2906 for (k = 1; k < 8; k++) {
2907 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2908 if (!v->pquantizer && block[k << v->left_blk_sh])
2909 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2912 for (k = 1; k < 8; k++) {
2913 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2914 if (!v->pquantizer && block[k << v->top_blk_sh])
2915 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2921 s->block_last_index[n] = i;
2926 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2927 * @param v VC1Context
2928 * @param block block to decode
2929 * @param[in] n subblock index
2930 * @param coded are AC coeffs present or not
2931 * @param mquant block quantizer
2932 * @param codingset set of VLC to decode data
2934 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2935 int coded, int mquant, int codingset)
2937 GetBitContext *gb = &v->s.gb;
2938 MpegEncContext *s = &v->s;
2939 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2942 int16_t *ac_val, *ac_val2;
2944 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2945 int a_avail = v->a_avail, c_avail = v->c_avail;
2946 int use_pred = s->ac_pred;
2950 s->dsp.clear_block(block);
2952 /* XXX: Guard against dumb values of mquant */
2953 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2955 /* Set DC scale - y and c use the same */
2956 s->y_dc_scale = s->y_dc_scale_table[mquant];
2957 s->c_dc_scale = s->c_dc_scale_table[mquant];
2959 /* Get DC differential */
2961 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2963 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2966 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2970 if (dcdiff == 119 /* ESC index value */) {
2971 /* TODO: Optimize */
2972 if (mquant == 1) dcdiff = get_bits(gb, 10);
2973 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2974 else dcdiff = get_bits(gb, 8);
2977 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2978 else if (mquant == 2)
2979 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2986 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2989 /* Store the quantized DC coeff, used for prediction */
2992 block[0] = dcdiff * s->y_dc_scale;
2994 block[0] = dcdiff * s->c_dc_scale;
3000 /* check if AC is needed at all and adjust direction if needed */
3001 if (!a_avail) dc_pred_dir = 1;
3002 if (!c_avail) dc_pred_dir = 0;
3003 if (!a_avail && !c_avail) use_pred = 0;
3004 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3007 scale = mquant * 2 + v->halfpq;
3009 if (dc_pred_dir) //left
3012 ac_val -= 16 * s->block_wrap[n];
3014 q1 = s->current_picture.f.qscale_table[mb_pos];
3015 if (dc_pred_dir && c_avail && mb_pos)
3016 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3017 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3018 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3019 if ( dc_pred_dir && n == 1)
3021 if (!dc_pred_dir && n == 2)
3023 if (n == 3) q2 = q1;
3026 int last = 0, skip, value;
3030 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3034 if (v->fcm == PROGRESSIVE)
3035 block[v->zz_8x8[0][i++]] = value;
3037 if (use_pred && (v->fcm == ILACE_FRAME)) {
3038 if (!dc_pred_dir) // top
3039 block[v->zz_8x8[2][i++]] = value;
3041 block[v->zz_8x8[3][i++]] = value;
3043 block[v->zzi_8x8[i++]] = value;
3048 /* apply AC prediction if needed */
3050 /* scale predictors if needed*/
3051 if (q2 && q1 != q2) {
3052 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3053 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3056 return AVERROR_INVALIDDATA;
3057 if (dc_pred_dir) { // left
3058 for (k = 1; k < 8; k++)
3059 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3061 for (k = 1; k < 8; k++)
3062 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3065 if (dc_pred_dir) { // left
3066 for (k = 1; k < 8; k++)
3067 block[k << v->left_blk_sh] += ac_val[k];
3069 for (k = 1; k < 8; k++)
3070 block[k << v->top_blk_sh] += ac_val[k + 8];
3074 /* save AC coeffs for further prediction */
3075 for (k = 1; k < 8; k++) {
3076 ac_val2[k ] = block[k << v->left_blk_sh];
3077 ac_val2[k + 8] = block[k << v->top_blk_sh];
3080 /* scale AC coeffs */
3081 for (k = 1; k < 64; k++)
3085 block[k] += (block[k] < 0) ? -mquant : mquant;
3088 if (use_pred) i = 63;
3089 } else { // no AC coeffs
3092 memset(ac_val2, 0, 16 * 2);
3093 if (dc_pred_dir) { // left
3095 memcpy(ac_val2, ac_val, 8 * 2);
3096 if (q2 && q1 != q2) {
3097 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3098 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3100 return AVERROR_INVALIDDATA;
3101 for (k = 1; k < 8; k++)
3102 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3107 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3108 if (q2 && q1 != q2) {
3109 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3110 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3112 return AVERROR_INVALIDDATA;
3113 for (k = 1; k < 8; k++)
3114 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3119 /* apply AC prediction if needed */
3121 if (dc_pred_dir) { // left
3122 for (k = 1; k < 8; k++) {
3123 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3124 if (!v->pquantizer && block[k << v->left_blk_sh])
3125 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3128 for (k = 1; k < 8; k++) {
3129 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3130 if (!v->pquantizer && block[k << v->top_blk_sh])
3131 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3137 s->block_last_index[n] = i;
3144 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3145 int mquant, int ttmb, int first_block,
3146 uint8_t *dst, int linesize, int skip_block,
3149 MpegEncContext *s = &v->s;
3150 GetBitContext *gb = &s->gb;
3153 int scale, off, idx, last, skip, value;
3154 int ttblk = ttmb & 7;
3157 s->dsp.clear_block(block);
3160 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)];
3162 if (ttblk == TT_4X4) {
3163 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3165 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3166 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3167 || (!v->res_rtm_flag && !first_block))) {
3168 subblkpat = decode012(gb);
3170 subblkpat ^= 3; // swap decoded pattern bits
3171 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3173 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3176 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3178 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3179 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3180 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3183 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3184 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3193 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3198 idx = v->zz_8x8[0][i++];
3200 idx = v->zzi_8x8[i++];
3201 block[idx] = value * scale;
3203 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3207 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3209 v->vc1dsp.vc1_inv_trans_8x8(block);
3210 s->dsp.add_pixels_clamped(block, dst, linesize);
3215 pat = ~subblkpat & 0xF;
3216 for (j = 0; j < 4; j++) {
3217 last = subblkpat & (1 << (3 - j));
3219 off = (j & 1) * 4 + (j & 2) * 16;
3221 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3226 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3228 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3229 block[idx + off] = value * scale;
3231 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3233 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3235 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3237 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3242 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3243 for (j = 0; j < 2; j++) {
3244 last = subblkpat & (1 << (1 - j));
3248 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3253 idx = v->zz_8x4[i++] + off;
3255 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3256 block[idx] = value * scale;
3258 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3260 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3262 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3264 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3269 pat = ~(subblkpat * 5) & 0xF;
3270 for (j = 0; j < 2; j++) {
3271 last = subblkpat & (1 << (1 - j));
3275 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3280 idx = v->zz_4x8[i++] + off;
3282 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3283 block[idx] = value * scale;
3285 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3287 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3289 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3291 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3297 *ttmb_out |= ttblk << (n * 4);
3301 /** @} */ // Macroblock group
3303 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3304 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3306 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3308 MpegEncContext *s = &v->s;
3309 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3310 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3311 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3312 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3313 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3316 if (block_num > 3) {
3317 dst = s->dest[block_num - 3];
3319 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3321 if (s->mb_y != s->end_mb_y || block_num < 2) {
3325 if (block_num > 3) {
3326 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3327 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3328 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3329 mv_stride = s->mb_stride;
3331 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3332 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3333 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3334 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3335 mv_stride = s->b8_stride;
3336 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3339 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3340 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3341 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3343 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3345 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3348 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3350 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3355 dst -= 4 * linesize;
3356 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3357 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3358 idx = (block_cbp | (block_cbp >> 2)) & 3;
3360 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3363 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3365 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3370 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3372 MpegEncContext *s = &v->s;
3373 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3374 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3375 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3376 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3377 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3380 if (block_num > 3) {
3381 dst = s->dest[block_num - 3] - 8 * linesize;
3383 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3386 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3389 if (block_num > 3) {
3390 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3391 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3392 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3394 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3395 : (mb_cbp >> ((block_num + 1) * 4));
3396 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3397 : (mb_is_intra >> ((block_num + 1) * 4));
3398 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3400 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3401 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3403 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3405 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3408 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3410 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3416 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3417 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3418 idx = (block_cbp | (block_cbp >> 1)) & 5;
3420 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3423 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3425 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3430 static void vc1_apply_p_loop_filter(VC1Context *v)
3432 MpegEncContext *s = &v->s;
3435 for (i = 0; i < 6; i++) {
3436 vc1_apply_p_v_loop_filter(v, i);
3439 /* V always precedes H, therefore we run H one MB before V;
3440 * at the end of a row, we catch up to complete the row */
3442 for (i = 0; i < 6; i++) {
3443 vc1_apply_p_h_loop_filter(v, i);
3445 if (s->mb_x == s->mb_width - 1) {
3447 ff_update_block_index(s);
3448 for (i = 0; i < 6; i++) {
3449 vc1_apply_p_h_loop_filter(v, i);
3455 /** Decode one P-frame MB
3457 static int vc1_decode_p_mb(VC1Context *v)
3459 MpegEncContext *s = &v->s;
3460 GetBitContext *gb = &s->gb;
3462 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3463 int cbp; /* cbp decoding stuff */
3464 int mqdiff, mquant; /* MB quantization */
3465 int ttmb = v->ttfrm; /* MB Transform type */
3467 int mb_has_coeffs = 1; /* last_flag */
3468 int dmv_x, dmv_y; /* Differential MV components */
3469 int index, index1; /* LUT indexes */
3470 int val, sign; /* temp values */
3471 int first_block = 1;
3473 int skipped, fourmv;
3474 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3476 mquant = v->pq; /* lossy initialization */
3478 if (v->mv_type_is_raw)
3479 fourmv = get_bits1(gb);
3481 fourmv = v->mv_type_mb_plane[mb_pos];
3483 skipped = get_bits1(gb);
3485 skipped = v->s.mbskip_table[mb_pos];
3487 if (!fourmv) { /* 1MV mode */
3489 GET_MVDATA(dmv_x, dmv_y);
3492 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3493 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3495 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3496 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3498 /* FIXME Set DC val for inter block ? */
3499 if (s->mb_intra && !mb_has_coeffs) {
3501 s->ac_pred = get_bits1(gb);
3503 } else if (mb_has_coeffs) {
3505 s->ac_pred = get_bits1(gb);
3506 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3512 s->current_picture.f.qscale_table[mb_pos] = mquant;
3514 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3515 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3516 VC1_TTMB_VLC_BITS, 2);
3517 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3519 for (i = 0; i < 6; i++) {
3520 s->dc_val[0][s->block_index[i]] = 0;
3522 val = ((cbp >> (5 - i)) & 1);
3523 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3524 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3526 /* check if prediction blocks A and C are available */
3527 v->a_avail = v->c_avail = 0;
3528 if (i == 2 || i == 3 || !s->first_slice_line)
3529 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3530 if (i == 1 || i == 3 || s->mb_x)
3531 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3533 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3534 (i & 4) ? v->codingset2 : v->codingset);
3535 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3537 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3539 for (j = 0; j < 64; j++)
3540 s->block[i][j] <<= 1;
3541 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3542 if (v->pq >= 9 && v->overlap) {
3544 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3546 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3548 block_cbp |= 0xF << (i << 2);
3549 block_intra |= 1 << i;
3551 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3552 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3553 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3554 block_cbp |= pat << (i << 2);
3555 if (!v->ttmbf && ttmb < 8)
3562 for (i = 0; i < 6; i++) {
3563 v->mb_type[0][s->block_index[i]] = 0;
3564 s->dc_val[0][s->block_index[i]] = 0;
3566 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3567 s->current_picture.f.qscale_table[mb_pos] = 0;
3568 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3571 } else { // 4MV mode
3572 if (!skipped /* unskipped MB */) {
3573 int intra_count = 0, coded_inter = 0;
3574 int is_intra[6], is_coded[6];
3576 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3577 for (i = 0; i < 6; i++) {
3578 val = ((cbp >> (5 - i)) & 1);
3579 s->dc_val[0][s->block_index[i]] = 0;
3586 GET_MVDATA(dmv_x, dmv_y);
3588 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3590 vc1_mc_4mv_luma(v, i, 0);
3591 intra_count += s->mb_intra;
3592 is_intra[i] = s->mb_intra;
3593 is_coded[i] = mb_has_coeffs;
3596 is_intra[i] = (intra_count >= 3);
3600 vc1_mc_4mv_chroma(v, 0);
3601 v->mb_type[0][s->block_index[i]] = is_intra[i];
3603 coded_inter = !is_intra[i] & is_coded[i];
3605 // if there are no coded blocks then don't do anything more
3607 if (!intra_count && !coded_inter)
3610 s->current_picture.f.qscale_table[mb_pos] = mquant;
3611 /* test if block is intra and has pred */
3614 for (i = 0; i < 6; i++)
3616 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3617 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3623 s->ac_pred = get_bits1(gb);
3627 if (!v->ttmbf && coded_inter)
3628 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3629 for (i = 0; i < 6; i++) {
3631 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3632 s->mb_intra = is_intra[i];
3634 /* check if prediction blocks A and C are available */
3635 v->a_avail = v->c_avail = 0;
3636 if (i == 2 || i == 3 || !s->first_slice_line)
3637 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3638 if (i == 1 || i == 3 || s->mb_x)
3639 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3641 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3642 (i & 4) ? v->codingset2 : v->codingset);
3643 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3645 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3647 for (j = 0; j < 64; j++)
3648 s->block[i][j] <<= 1;
3649 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3650 (i & 4) ? s->uvlinesize : s->linesize);
3651 if (v->pq >= 9 && v->overlap) {
3653 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3655 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3657 block_cbp |= 0xF << (i << 2);
3658 block_intra |= 1 << i;
3659 } else if (is_coded[i]) {
3660 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3661 first_block, s->dest[dst_idx] + off,
3662 (i & 4) ? s->uvlinesize : s->linesize,
3663 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3665 block_cbp |= pat << (i << 2);
3666 if (!v->ttmbf && ttmb < 8)
3671 } else { // skipped MB
3673 s->current_picture.f.qscale_table[mb_pos] = 0;
3674 for (i = 0; i < 6; i++) {
3675 v->mb_type[0][s->block_index[i]] = 0;
3676 s->dc_val[0][s->block_index[i]] = 0;
3678 for (i = 0; i < 4; i++) {
3679 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3680 vc1_mc_4mv_luma(v, i, 0);
3682 vc1_mc_4mv_chroma(v, 0);
3683 s->current_picture.f.qscale_table[mb_pos] = 0;
3687 v->cbp[s->mb_x] = block_cbp;
3688 v->ttblk[s->mb_x] = block_tt;
3689 v->is_intra[s->mb_x] = block_intra;
3694 /* Decode one macroblock in an interlaced frame p picture */
3696 static int vc1_decode_p_mb_intfr(VC1Context *v)
3698 MpegEncContext *s = &v->s;
3699 GetBitContext *gb = &s->gb;
3701 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3702 int cbp = 0; /* cbp decoding stuff */
3703 int mqdiff, mquant; /* MB quantization */
3704 int ttmb = v->ttfrm; /* MB Transform type */
3706 int mb_has_coeffs = 1; /* last_flag */
3707 int dmv_x, dmv_y; /* Differential MV components */
3708 int val; /* temp value */
3709 int first_block = 1;
3711 int skipped, fourmv = 0, twomv = 0;
3712 int block_cbp = 0, pat, block_tt = 0;
3713 int idx_mbmode = 0, mvbp;
3714 int stride_y, fieldtx;
3716 mquant = v->pq; /* Lossy initialization */
3719 skipped = get_bits1(gb);
3721 skipped = v->s.mbskip_table[mb_pos];
3723 if (v->fourmvswitch)
3724 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3726 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3727 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3728 /* store the motion vector type in a flag (useful later) */
3729 case MV_PMODE_INTFR_4MV:
3731 v->blk_mv_type[s->block_index[0]] = 0;
3732 v->blk_mv_type[s->block_index[1]] = 0;
3733 v->blk_mv_type[s->block_index[2]] = 0;
3734 v->blk_mv_type[s->block_index[3]] = 0;
3736 case MV_PMODE_INTFR_4MV_FIELD:
3738 v->blk_mv_type[s->block_index[0]] = 1;
3739 v->blk_mv_type[s->block_index[1]] = 1;
3740 v->blk_mv_type[s->block_index[2]] = 1;
3741 v->blk_mv_type[s->block_index[3]] = 1;
3743 case MV_PMODE_INTFR_2MV_FIELD:
3745 v->blk_mv_type[s->block_index[0]] = 1;
3746 v->blk_mv_type[s->block_index[1]] = 1;
3747 v->blk_mv_type[s->block_index[2]] = 1;
3748 v->blk_mv_type[s->block_index[3]] = 1;
3750 case MV_PMODE_INTFR_1MV:
3751 v->blk_mv_type[s->block_index[0]] = 0;
3752 v->blk_mv_type[s->block_index[1]] = 0;
3753 v->blk_mv_type[s->block_index[2]] = 0;
3754 v->blk_mv_type[s->block_index[3]] = 0;
3757 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3758 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3759 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3760 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3761 s->mb_intra = v->is_intra[s->mb_x] = 1;
3762 for (i = 0; i < 6; i++)
3763 v->mb_type[0][s->block_index[i]] = 1;
3764 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3765 mb_has_coeffs = get_bits1(gb);
3767 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3768 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3770 s->current_picture.f.qscale_table[mb_pos] = mquant;
3771 /* Set DC scale - y and c use the same (not sure if necessary here) */
3772 s->y_dc_scale = s->y_dc_scale_table[mquant];
3773 s->c_dc_scale = s->c_dc_scale_table[mquant];
3775 for (i = 0; i < 6; i++) {
3776 s->dc_val[0][s->block_index[i]] = 0;
3778 val = ((cbp >> (5 - i)) & 1);
3779 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3780 v->a_avail = v->c_avail = 0;
3781 if (i == 2 || i == 3 || !s->first_slice_line)
3782 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3783 if (i == 1 || i == 3 || s->mb_x)
3784 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3786 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3787 (i & 4) ? v->codingset2 : v->codingset);
3788 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3789 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3791 stride_y = s->linesize << fieldtx;
3792 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3794 stride_y = s->uvlinesize;
3797 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3801 } else { // inter MB
3802 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3804 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3805 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3806 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3808 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3809 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3810 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3813 s->mb_intra = v->is_intra[s->mb_x] = 0;
3814 for (i = 0; i < 6; i++)
3815 v->mb_type[0][s->block_index[i]] = 0;
3816 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3817 /* for all motion vector read MVDATA and motion compensate each block */
3821 for (i = 0; i < 6; i++) {
3824 val = ((mvbp >> (3 - i)) & 1);
3826 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3828 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3829 vc1_mc_4mv_luma(v, i, 0);
3830 } else if (i == 4) {
3831 vc1_mc_4mv_chroma4(v);
3838 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3840 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3841 vc1_mc_4mv_luma(v, 0, 0);
3842 vc1_mc_4mv_luma(v, 1, 0);
3845 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3847 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3848 vc1_mc_4mv_luma(v, 2, 0);
3849 vc1_mc_4mv_luma(v, 3, 0);
3850 vc1_mc_4mv_chroma4(v);
3852 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3855 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3857 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3861 GET_MQUANT(); // p. 227
3862 s->current_picture.f.qscale_table[mb_pos] = mquant;
3863 if (!v->ttmbf && cbp)
3864 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3865 for (i = 0; i < 6; i++) {
3866 s->dc_val[0][s->block_index[i]] = 0;
3868 val = ((cbp >> (5 - i)) & 1);
3870 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3872 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3874 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3875 first_block, s->dest[dst_idx] + off,
3876 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3877 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3878 block_cbp |= pat << (i << 2);
3879 if (!v->ttmbf && ttmb < 8)
3886 s->mb_intra = v->is_intra[s->mb_x] = 0;
3887 for (i = 0; i < 6; i++) {
3888 v->mb_type[0][s->block_index[i]] = 0;
3889 s->dc_val[0][s->block_index[i]] = 0;
3891 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3892 s->current_picture.f.qscale_table[mb_pos] = 0;
3893 v->blk_mv_type[s->block_index[0]] = 0;
3894 v->blk_mv_type[s->block_index[1]] = 0;
3895 v->blk_mv_type[s->block_index[2]] = 0;
3896 v->blk_mv_type[s->block_index[3]] = 0;
3897 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3900 if (s->mb_x == s->mb_width - 1)
3901 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3905 static int vc1_decode_p_mb_intfi(VC1Context *v)
3907 MpegEncContext *s = &v->s;
3908 GetBitContext *gb = &s->gb;
3910 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3911 int cbp = 0; /* cbp decoding stuff */
3912 int mqdiff, mquant; /* MB quantization */
3913 int ttmb = v->ttfrm; /* MB Transform type */
3915 int mb_has_coeffs = 1; /* last_flag */
3916 int dmv_x, dmv_y; /* Differential MV components */
3917 int val; /* temp values */
3918 int first_block = 1;
3921 int block_cbp = 0, pat, block_tt = 0;
3924 mquant = v->pq; /* Lossy initialization */
3926 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3927 if (idx_mbmode <= 1) { // intra MB
3928 s->mb_intra = v->is_intra[s->mb_x] = 1;
3929 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3930 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3931 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3933 s->current_picture.f.qscale_table[mb_pos] = mquant;
3934 /* Set DC scale - y and c use the same (not sure if necessary here) */
3935 s->y_dc_scale = s->y_dc_scale_table[mquant];
3936 s->c_dc_scale = s->c_dc_scale_table[mquant];
3937 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3938 mb_has_coeffs = idx_mbmode & 1;
3940 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3942 for (i = 0; i < 6; i++) {
3943 s->dc_val[0][s->block_index[i]] = 0;
3944 v->mb_type[0][s->block_index[i]] = 1;
3946 val = ((cbp >> (5 - i)) & 1);
3947 v->a_avail = v->c_avail = 0;
3948 if (i == 2 || i == 3 || !s->first_slice_line)
3949 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3950 if (i == 1 || i == 3 || s->mb_x)
3951 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3953 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3954 (i & 4) ? v->codingset2 : v->codingset);
3955 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3957 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3958 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3959 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3960 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3961 // TODO: loop filter
3964 s->mb_intra = v->is_intra[s->mb_x] = 0;
3965 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3966 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3967 if (idx_mbmode <= 5) { // 1-MV
3968 dmv_x = dmv_y = pred_flag = 0;
3969 if (idx_mbmode & 1) {
3970 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3972 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3974 mb_has_coeffs = !(idx_mbmode & 2);
3976 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3977 for (i = 0; i < 6; i++) {
3979 dmv_x = dmv_y = pred_flag = 0;
3980 val = ((v->fourmvbp >> (3 - i)) & 1);
3982 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3984 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3985 vc1_mc_4mv_luma(v, i, 0);
3987 vc1_mc_4mv_chroma(v, 0);
3989 mb_has_coeffs = idx_mbmode & 1;
3992 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3996 s->current_picture.f.qscale_table[mb_pos] = mquant;
3997 if (!v->ttmbf && cbp) {
3998 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4001 for (i = 0; i < 6; i++) {
4002 s->dc_val[0][s->block_index[i]] = 0;
4004 val = ((cbp >> (5 - i)) & 1);
4005 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4006 if (v->second_field)
4007 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4009 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4010 first_block, s->dest[dst_idx] + off,
4011 (i & 4) ? s->uvlinesize : s->linesize,
4012 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4014 block_cbp |= pat << (i << 2);
4015 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4020 if (s->mb_x == s->mb_width - 1)
4021 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4025 /** Decode one B-frame MB (in Main profile)
4027 static void vc1_decode_b_mb(VC1Context *v)
4029 MpegEncContext *s = &v->s;
4030 GetBitContext *gb = &s->gb;
4032 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4033 int cbp = 0; /* cbp decoding stuff */
4034 int mqdiff, mquant; /* MB quantization */
4035 int ttmb = v->ttfrm; /* MB Transform type */
4036 int mb_has_coeffs = 0; /* last_flag */
4037 int index, index1; /* LUT indexes */
4038 int val, sign; /* temp values */
4039 int first_block = 1;
4041 int skipped, direct;
4042 int dmv_x[2], dmv_y[2];
4043 int bmvtype = BMV_TYPE_BACKWARD;
4045 mquant = v->pq; /* lossy initialization */
4049 direct = get_bits1(gb);
4051 direct = v->direct_mb_plane[mb_pos];
4053 skipped = get_bits1(gb);
4055 skipped = v->s.mbskip_table[mb_pos];
4057 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4058 for (i = 0; i < 6; i++) {
4059 v->mb_type[0][s->block_index[i]] = 0;
4060 s->dc_val[0][s->block_index[i]] = 0;
4062 s->current_picture.f.qscale_table[mb_pos] = 0;
4066 GET_MVDATA(dmv_x[0], dmv_y[0]);
4067 dmv_x[1] = dmv_x[0];
4068 dmv_y[1] = dmv_y[0];
4070 if (skipped || !s->mb_intra) {
4071 bmvtype = decode012(gb);
4074 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4077 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4080 bmvtype = BMV_TYPE_INTERPOLATED;
4081 dmv_x[0] = dmv_y[0] = 0;
4085 for (i = 0; i < 6; i++)
4086 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4090 bmvtype = BMV_TYPE_INTERPOLATED;
4091 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4092 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4096 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4099 s->current_picture.f.qscale_table[mb_pos] = mquant;
4101 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4102 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4103 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4104 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4106 if (!mb_has_coeffs && !s->mb_intra) {
4107 /* no coded blocks - effectively skipped */
4108 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4109 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4112 if (s->mb_intra && !mb_has_coeffs) {
4114 s->current_picture.f.qscale_table[mb_pos] = mquant;
4115 s->ac_pred = get_bits1(gb);
4117 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4119 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4120 GET_MVDATA(dmv_x[0], dmv_y[0]);
4121 if (!mb_has_coeffs) {
4122 /* interpolated skipped block */
4123 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4124 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4128 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4130 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4133 s->ac_pred = get_bits1(gb);
4134 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4136 s->current_picture.f.qscale_table[mb_pos] = mquant;
4137 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4138 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4142 for (i = 0; i < 6; i++) {
4143 s->dc_val[0][s->block_index[i]] = 0;
4145 val = ((cbp >> (5 - i)) & 1);
4146 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4147 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4149 /* check if prediction blocks A and C are available */
4150 v->a_avail = v->c_avail = 0;
4151 if (i == 2 || i == 3 || !s->first_slice_line)
4152 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4153 if (i == 1 || i == 3 || s->mb_x)
4154 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4156 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4157 (i & 4) ? v->codingset2 : v->codingset);
4158 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4160 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4162 for (j = 0; j < 64; j++)
4163 s->block[i][j] <<= 1;
4164 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4166 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4167 first_block, s->dest[dst_idx] + off,
4168 (i & 4) ? s->uvlinesize : s->linesize,
4169 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4170 if (!v->ttmbf && ttmb < 8)
4177 /** Decode one B-frame MB (in interlaced field B picture)
4179 static void vc1_decode_b_mb_intfi(VC1Context *v)
4181 MpegEncContext *s = &v->s;
4182 GetBitContext *gb = &s->gb;
4184 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4185 int cbp = 0; /* cbp decoding stuff */
4186 int mqdiff, mquant; /* MB quantization */
4187 int ttmb = v->ttfrm; /* MB Transform type */
4188 int mb_has_coeffs = 0; /* last_flag */
4189 int val; /* temp value */
4190 int first_block = 1;
4193 int dmv_x[2], dmv_y[2], pred_flag[2];
4194 int bmvtype = BMV_TYPE_BACKWARD;
4195 int idx_mbmode, interpmvp;
4197 mquant = v->pq; /* Lossy initialization */
4200 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4201 if (idx_mbmode <= 1) { // intra MB
4202 s->mb_intra = v->is_intra[s->mb_x] = 1;
4203 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4204 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4205 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4207 s->current_picture.f.qscale_table[mb_pos] = mquant;
4208 /* Set DC scale - y and c use the same (not sure if necessary here) */
4209 s->y_dc_scale = s->y_dc_scale_table[mquant];
4210 s->c_dc_scale = s->c_dc_scale_table[mquant];
4211 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4212 mb_has_coeffs = idx_mbmode & 1;
4214 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4216 for (i = 0; i < 6; i++) {
4217 s->dc_val[0][s->block_index[i]] = 0;
4219 val = ((cbp >> (5 - i)) & 1);
4220 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4221 v->a_avail = v->c_avail = 0;
4222 if (i == 2 || i == 3 || !s->first_slice_line)
4223 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4224 if (i == 1 || i == 3 || s->mb_x)
4225 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4227 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4228 (i & 4) ? v->codingset2 : v->codingset);
4229 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4231 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4233 for (j = 0; j < 64; j++)
4234 s->block[i][j] <<= 1;
4235 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4236 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4237 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4238 // TODO: yet to perform loop filter
4241 s->mb_intra = v->is_intra[s->mb_x] = 0;
4242 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4243 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4245 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4247 fwd = v->forward_mb_plane[mb_pos];
4248 if (idx_mbmode <= 5) { // 1-MV
4249 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4250 pred_flag[0] = pred_flag[1] = 0;
4252 bmvtype = BMV_TYPE_FORWARD;
4254 bmvtype = decode012(gb);
4257 bmvtype = BMV_TYPE_BACKWARD;
4260 bmvtype = BMV_TYPE_DIRECT;
4263 bmvtype = BMV_TYPE_INTERPOLATED;
4264 interpmvp = get_bits1(gb);
4267 v->bmvtype = bmvtype;
4268 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4269 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4271 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4272 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4274 if (bmvtype == BMV_TYPE_DIRECT) {
4275 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4276 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4278 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4279 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4280 mb_has_coeffs = !(idx_mbmode & 2);
4283 bmvtype = BMV_TYPE_FORWARD;
4284 v->bmvtype = bmvtype;
4285 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4286 for (i = 0; i < 6; i++) {
4288 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4289 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4290 val = ((v->fourmvbp >> (3 - i)) & 1);
4292 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4293 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4294 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4296 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4297 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4299 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4301 mb_has_coeffs = idx_mbmode & 1;
4304 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4308 s->current_picture.f.qscale_table[mb_pos] = mquant;
4309 if (!v->ttmbf && cbp) {
4310 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4313 for (i = 0; i < 6; i++) {
4314 s->dc_val[0][s->block_index[i]] = 0;
4316 val = ((cbp >> (5 - i)) & 1);
4317 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4318 if (v->second_field)
4319 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4321 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4322 first_block, s->dest[dst_idx] + off,
4323 (i & 4) ? s->uvlinesize : s->linesize,
4324 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4325 if (!v->ttmbf && ttmb < 8)
4333 /** Decode blocks of I-frame
4335 static void vc1_decode_i_blocks(VC1Context *v)
4338 MpegEncContext *s = &v->s;
4343 /* select codingmode used for VLC tables selection */
4344 switch (v->y_ac_table_index) {
4346 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4349 v->codingset = CS_HIGH_MOT_INTRA;
4352 v->codingset = CS_MID_RATE_INTRA;
4356 switch (v->c_ac_table_index) {
4358 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4361 v->codingset2 = CS_HIGH_MOT_INTER;
4364 v->codingset2 = CS_MID_RATE_INTER;
4368 /* Set DC scale - y and c use the same */
4369 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4370 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4373 s->mb_x = s->mb_y = 0;
4375 s->first_slice_line = 1;
4376 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4378 ff_init_block_index(s);
4379 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4381 ff_update_block_index(s);
4382 dst[0] = s->dest[0];
4383 dst[1] = dst[0] + 8;
4384 dst[2] = s->dest[0] + s->linesize * 8;
4385 dst[3] = dst[2] + 8;
4386 dst[4] = s->dest[1];
4387 dst[5] = s->dest[2];
4388 s->dsp.clear_blocks(s->block[0]);
4389 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4390 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4391 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4392 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4393 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4395 // do actual MB decoding and displaying
4396 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4397 v->s.ac_pred = get_bits1(&v->s.gb);
4399 for (k = 0; k < 6; k++) {
4400 val = ((cbp >> (5 - k)) & 1);
4403 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4407 cbp |= val << (5 - k);
4409 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4411 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4413 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4414 if (v->pq >= 9 && v->overlap) {
4416 for (j = 0; j < 64; j++)
4417 s->block[k][j] <<= 1;
4418 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4421 for (j = 0; j < 64; j++)
4422 s->block[k][j] = (s->block[k][j] - 64) << 1;
4423 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4427 if (v->pq >= 9 && v->overlap) {
4429 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4430 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4431 if (!(s->flags & CODEC_FLAG_GRAY)) {
4432 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4433 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4436 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4437 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4438 if (!s->first_slice_line) {
4439 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4440 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4441 if (!(s->flags & CODEC_FLAG_GRAY)) {
4442 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4443 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4446 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4447 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4449 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4451 if (get_bits_count(&s->gb) > v->bits) {
4452 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4453 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4454 get_bits_count(&s->gb), v->bits);
4458 if (!v->s.loop_filter)
4459 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4461 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4463 s->first_slice_line = 0;
4465 if (v->s.loop_filter)
4466 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4468 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4469 * profile, these only differ are when decoding MSS2 rectangles. */
4470 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4473 /** Decode blocks of I-frame for advanced profile
4475 static void vc1_decode_i_blocks_adv(VC1Context *v)
4478 MpegEncContext *s = &v->s;
4484 GetBitContext *gb = &s->gb;
4486 /* select codingmode used for VLC tables selection */
4487 switch (v->y_ac_table_index) {
4489 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4492 v->codingset = CS_HIGH_MOT_INTRA;
4495 v->codingset = CS_MID_RATE_INTRA;
4499 switch (v->c_ac_table_index) {
4501 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4504 v->codingset2 = CS_HIGH_MOT_INTER;
4507 v->codingset2 = CS_MID_RATE_INTER;
4512 s->mb_x = s->mb_y = 0;
4514 s->first_slice_line = 1;
4515 s->mb_y = s->start_mb_y;
4516 if (s->start_mb_y) {
4518 ff_init_block_index(s);
4519 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4520 (1 + s->b8_stride) * sizeof(*s->coded_block));
4522 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4524 ff_init_block_index(s);
4525 for (;s->mb_x < s->mb_width; s->mb_x++) {
4526 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4527 ff_update_block_index(s);
4528 s->dsp.clear_blocks(block[0]);
4529 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4530 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4531 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4532 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4534 // do actual MB decoding and displaying
4535 if (v->fieldtx_is_raw)
4536 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4537 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4538 if ( v->acpred_is_raw)
4539 v->s.ac_pred = get_bits1(&v->s.gb);
4541 v->s.ac_pred = v->acpred_plane[mb_pos];
4543 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4544 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4548 s->current_picture.f.qscale_table[mb_pos] = mquant;
4549 /* Set DC scale - y and c use the same */
4550 s->y_dc_scale = s->y_dc_scale_table[mquant];
4551 s->c_dc_scale = s->c_dc_scale_table[mquant];
4553 for (k = 0; k < 6; k++) {
4554 val = ((cbp >> (5 - k)) & 1);
4557 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4561 cbp |= val << (5 - k);
4563 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4564 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4566 vc1_decode_i_block_adv(v, block[k], k, val,
4567 (k < 4) ? v->codingset : v->codingset2, mquant);
4569 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4571 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4574 vc1_smooth_overlap_filter_iblk(v);
4575 vc1_put_signed_blocks_clamped(v);
4576 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4578 if (get_bits_count(&s->gb) > v->bits) {
4579 // TODO: may need modification to handle slice coding
4580 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4581 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4582 get_bits_count(&s->gb), v->bits);
4586 if (!v->s.loop_filter)
4587 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4589 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4590 s->first_slice_line = 0;
4593 /* raw bottom MB row */
4595 ff_init_block_index(s);
4596 for (;s->mb_x < s->mb_width; s->mb_x++) {
4597 ff_update_block_index(s);
4598 vc1_put_signed_blocks_clamped(v);
4599 if (v->s.loop_filter)
4600 vc1_loop_filter_iblk_delayed(v, v->pq);
4602 if (v->s.loop_filter)
4603 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4604 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4605 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4608 static void vc1_decode_p_blocks(VC1Context *v)
4610 MpegEncContext *s = &v->s;
4611 int apply_loop_filter;
4613 /* select codingmode used for VLC tables selection */
4614 switch (v->c_ac_table_index) {
4616 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4619 v->codingset = CS_HIGH_MOT_INTRA;
4622 v->codingset = CS_MID_RATE_INTRA;
4626 switch (v->c_ac_table_index) {
4628 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4631 v->codingset2 = CS_HIGH_MOT_INTER;
4634 v->codingset2 = CS_MID_RATE_INTER;
4638 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4639 s->first_slice_line = 1;
4640 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4641 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4643 ff_init_block_index(s);
4644 for (; s->mb_x < s->mb_width; s->mb_x++) {
4645 ff_update_block_index(s);
4647 if (v->fcm == ILACE_FIELD)
4648 vc1_decode_p_mb_intfi(v);
4649 else if (v->fcm == ILACE_FRAME)
4650 vc1_decode_p_mb_intfr(v);
4651 else vc1_decode_p_mb(v);
4652 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4653 vc1_apply_p_loop_filter(v);
4654 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4655 // TODO: may need modification to handle slice coding
4656 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4657 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4658 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4662 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4663 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4664 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4665 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4666 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4667 s->first_slice_line = 0;
4669 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4671 ff_init_block_index(s);
4672 for (; s->mb_x < s->mb_width; s->mb_x++) {
4673 ff_update_block_index(s);
4674 vc1_apply_p_loop_filter(v);
4677 if (s->end_mb_y >= s->start_mb_y)
4678 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4679 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4680 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4683 static void vc1_decode_b_blocks(VC1Context *v)
4685 MpegEncContext *s = &v->s;
4687 /* select codingmode used for VLC tables selection */
4688 switch (v->c_ac_table_index) {
4690 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4693 v->codingset = CS_HIGH_MOT_INTRA;
4696 v->codingset = CS_MID_RATE_INTRA;
4700 switch (v->c_ac_table_index) {
4702 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4705 v->codingset2 = CS_HIGH_MOT_INTER;
4708 v->codingset2 = CS_MID_RATE_INTER;
4712 s->first_slice_line = 1;
4713 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4715 ff_init_block_index(s);
4716 for (; s->mb_x < s->mb_width; s->mb_x++) {
4717 ff_update_block_index(s);
4719 if (v->fcm == ILACE_FIELD)
4720 vc1_decode_b_mb_intfi(v);
4723 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4724 // TODO: may need modification to handle slice coding
4725 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4726 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4727 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4730 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4732 if (!v->s.loop_filter)
4733 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4735 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4736 s->first_slice_line = 0;
4738 if (v->s.loop_filter)
4739 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4740 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4741 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4744 static void vc1_decode_skip_blocks(VC1Context *v)
4746 MpegEncContext *s = &v->s;
4748 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4749 s->first_slice_line = 1;
4750 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4752 ff_init_block_index(s);
4753 ff_update_block_index(s);
4754 if (s->last_picture.f.data[0]) {
4755 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4756 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4757 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4759 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4760 s->first_slice_line = 0;
4762 s->pict_type = AV_PICTURE_TYPE_P;
4765 void ff_vc1_decode_blocks(VC1Context *v)
4768 v->s.esc3_level_length = 0;
4770 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4773 v->left_blk_idx = -1;
4774 v->topleft_blk_idx = 1;
4776 switch (v->s.pict_type) {
4777 case AV_PICTURE_TYPE_I:
4778 if (v->profile == PROFILE_ADVANCED)
4779 vc1_decode_i_blocks_adv(v);
4781 vc1_decode_i_blocks(v);
4783 case AV_PICTURE_TYPE_P:
4784 if (v->p_frame_skipped)
4785 vc1_decode_skip_blocks(v);
4787 vc1_decode_p_blocks(v);
4789 case AV_PICTURE_TYPE_B:
4791 if (v->profile == PROFILE_ADVANCED)
4792 vc1_decode_i_blocks_adv(v);
4794 vc1_decode_i_blocks(v);
4796 vc1_decode_b_blocks(v);
4802 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4806 * Transform coefficients for both sprites in 16.16 fixed point format,
4807 * in the order they appear in the bitstream:
4809 * rotation 1 (unused)
4811 * rotation 2 (unused)
4818 int effect_type, effect_flag;
4819 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4820 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4823 static inline int get_fp_val(GetBitContext* gb)
4825 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4828 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4832 switch (get_bits(gb, 2)) {
4835 c[2] = get_fp_val(gb);
4839 c[0] = c[4] = get_fp_val(gb);
4840 c[2] = get_fp_val(gb);
4843 c[0] = get_fp_val(gb);
4844 c[2] = get_fp_val(gb);
4845 c[4] = get_fp_val(gb);
4848 c[0] = get_fp_val(gb);
4849 c[1] = get_fp_val(gb);
4850 c[2] = get_fp_val(gb);
4851 c[3] = get_fp_val(gb);
4852 c[4] = get_fp_val(gb);
4855 c[5] = get_fp_val(gb);
4857 c[6] = get_fp_val(gb);
4862 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4864 AVCodecContext *avctx = v->s.avctx;
4867 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4868 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4869 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4870 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4871 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4872 for (i = 0; i < 7; i++)
4873 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4874 sd->coefs[sprite][i] / (1<<16),
4875 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4876 av_log(avctx, AV_LOG_DEBUG, "\n");
4880 if (sd->effect_type = get_bits_long(gb, 30)) {
4881 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4883 vc1_sprite_parse_transform(gb, sd->effect_params1);
4886 vc1_sprite_parse_transform(gb, sd->effect_params1);
4887 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4890 for (i = 0; i < sd->effect_pcount1; i++)
4891 sd->effect_params1[i] = get_fp_val(gb);
4893 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4894 // effect 13 is simple alpha blending and matches the opacity above
4895 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4896 for (i = 0; i < sd->effect_pcount1; i++)
4897 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4898 sd->effect_params1[i] / (1 << 16),
4899 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4900 av_log(avctx, AV_LOG_DEBUG, "\n");
4903 sd->effect_pcount2 = get_bits(gb, 16);
4904 if (sd->effect_pcount2 > 10) {
4905 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4907 } else if (sd->effect_pcount2) {
4909 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4910 while (++i < sd->effect_pcount2) {
4911 sd->effect_params2[i] = get_fp_val(gb);
4912 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4913 sd->effect_params2[i] / (1 << 16),
4914 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4916 av_log(avctx, AV_LOG_DEBUG, "\n");
4919 if (sd->effect_flag = get_bits1(gb))
4920 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4922 if (get_bits_count(gb) >= gb->size_in_bits +
4923 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4924 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4925 if (get_bits_count(gb) < gb->size_in_bits - 8)
4926 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4929 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4931 int i, plane, row, sprite;
4932 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4933 uint8_t* src_h[2][2];
4934 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4936 MpegEncContext *s = &v->s;
4938 for (i = 0; i < 2; i++) {
4939 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4940 xadv[i] = sd->coefs[i][0];
4941 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4942 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4944 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4945 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4947 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4949 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4950 int width = v->output_width>>!!plane;
4952 for (row = 0; row < v->output_height>>!!plane; row++) {
4953 uint8_t *dst = v->sprite_output_frame.data[plane] +
4954 v->sprite_output_frame.linesize[plane] * row;
4956 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4957 uint8_t *iplane = s->current_picture.f.data[plane];
4958 int iline = s->current_picture.f.linesize[plane];
4959 int ycoord = yoff[sprite] + yadv[sprite] * row;
4960 int yline = ycoord >> 16;
4962 ysub[sprite] = ycoord & 0xFFFF;
4964 iplane = s->last_picture.f.data[plane];
4965 iline = s->last_picture.f.linesize[plane];
4967 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4968 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4969 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4971 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4973 if (sr_cache[sprite][0] != yline) {
4974 if (sr_cache[sprite][1] == yline) {
4975 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4976 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4978 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4979 sr_cache[sprite][0] = yline;
4982 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4983 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4984 iplane + next_line, xoff[sprite],
4985 xadv[sprite], width);
4986 sr_cache[sprite][1] = yline + 1;
4988 src_h[sprite][0] = v->sr_rows[sprite][0];
4989 src_h[sprite][1] = v->sr_rows[sprite][1];
4993 if (!v->two_sprites) {
4995 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4997 memcpy(dst, src_h[0][0], width);
5000 if (ysub[0] && ysub[1]) {
5001 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5002 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5003 } else if (ysub[0]) {
5004 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5005 src_h[1][0], alpha, width);
5006 } else if (ysub[1]) {
5007 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5008 src_h[0][0], (1<<16)-1-alpha, width);
5010 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5016 for (i = 0; i < 2; i++) {
5026 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5028 MpegEncContext *s = &v->s;
5029 AVCodecContext *avctx = s->avctx;
5032 vc1_parse_sprites(v, gb, &sd);
5034 if (!s->current_picture.f.data[0]) {
5035 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5039 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5040 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5044 if (v->sprite_output_frame.data[0])
5045 avctx->release_buffer(avctx, &v->sprite_output_frame);
5047 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5048 v->sprite_output_frame.reference = 0;
5049 if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
5050 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5054 vc1_draw_sprites(v, &sd);
5059 static void vc1_sprite_flush(AVCodecContext *avctx)
5061 VC1Context *v = avctx->priv_data;
5062 MpegEncContext *s = &v->s;
5063 AVFrame *f = &s->current_picture.f;
5066 /* Windows Media Image codecs have a convergence interval of two keyframes.
5067 Since we can't enforce it, clear to black the missing sprite. This is
5068 wrong but it looks better than doing nothing. */
5071 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5072 for (i = 0; i < v->sprite_height>>!!plane; i++)
5073 memset(f->data[plane] + i * f->linesize[plane],
5074 plane ? 128 : 0, f->linesize[plane]);
5079 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5081 MpegEncContext *s = &v->s;
5084 /* Allocate mb bitplanes */
5085 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5086 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5087 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5088 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5089 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5090 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5092 v->n_allocated_blks = s->mb_width + 2;
5093 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5094 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5095 v->cbp = v->cbp_base + s->mb_stride;
5096 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5097 v->ttblk = v->ttblk_base + s->mb_stride;
5098 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5099 v->is_intra = v->is_intra_base + s->mb_stride;
5100 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5101 v->luma_mv = v->luma_mv_base + s->mb_stride;
5103 /* allocate block type info in that way so it could be used with s->block_index[] */
5104 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5105 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5106 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5107 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5109 /* allocate memory to store block level MV info */
5110 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5111 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5112 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5113 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5114 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5115 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5116 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5117 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);
5118 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5119 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5120 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);
5122 /* Init coded blocks info */
5123 if (v->profile == PROFILE_ADVANCED) {
5124 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5126 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5130 ff_intrax8_common_init(&v->x8,s);
5132 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5133 for (i = 0; i < 4; i++)
5134 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5137 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5138 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5145 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5148 for (i = 0; i < 64; i++) {
5149 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5150 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5151 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5152 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5153 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5154 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5160 /** Initialize a VC1/WMV3 decoder
5161 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5162 * @todo TODO: Decypher remaining bits in extra_data
5164 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5166 VC1Context *v = avctx->priv_data;
5167 MpegEncContext *s = &v->s;
5170 /* save the container output size for WMImage */
5171 v->output_width = avctx->width;
5172 v->output_height = avctx->height;
5174 if (!avctx->extradata_size || !avctx->extradata)
5176 if (!(avctx->flags & CODEC_FLAG_GRAY))
5177 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5179 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5180 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5182 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5183 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5185 if (ff_vc1_init_common(v) < 0)
5187 // ensure static VLC tables are initialized
5188 if (ff_msmpeg4_decode_init(avctx) < 0)
5190 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5192 // Hack to ensure the above functions will be called
5193 // again once we know all necessary settings.
5194 // That this is necessary might indicate a bug.
5195 ff_vc1_decode_end(avctx);
5196 ff_vc1dsp_init(&v->vc1dsp);
5198 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5201 // looks like WMV3 has a sequence header stored in the extradata
5202 // advanced sequence header may be before the first frame
5203 // the last byte of the extradata is a version number, 1 for the
5204 // samples we can decode
5206 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5208 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5211 count = avctx->extradata_size*8 - get_bits_count(&gb);
5213 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5214 count, get_bits(&gb, count));
5215 } else if (count < 0) {
5216 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5218 } else { // VC1/WVC1/WVP2
5219 const uint8_t *start = avctx->extradata;
5220 uint8_t *end = avctx->extradata + avctx->extradata_size;
5221 const uint8_t *next;
5222 int size, buf2_size;
5223 uint8_t *buf2 = NULL;
5224 int seq_initialized = 0, ep_initialized = 0;
5226 if (avctx->extradata_size < 16) {
5227 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5231 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5232 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5234 for (; next < end; start = next) {
5235 next = find_next_marker(start + 4, end);
5236 size = next - start - 4;
5239 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5240 init_get_bits(&gb, buf2, buf2_size * 8);
5241 switch (AV_RB32(start)) {
5242 case VC1_CODE_SEQHDR:
5243 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5247 seq_initialized = 1;
5249 case VC1_CODE_ENTRYPOINT:
5250 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5259 if (!seq_initialized || !ep_initialized) {
5260 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5263 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5266 avctx->profile = v->profile;
5267 if (v->profile == PROFILE_ADVANCED)
5268 avctx->level = v->level;
5270 avctx->has_b_frames = !!avctx->max_b_frames;
5272 s->mb_width = (avctx->coded_width + 15) >> 4;
5273 s->mb_height = (avctx->coded_height + 15) >> 4;
5275 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5276 ff_vc1_init_transposed_scantables(v);
5278 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5283 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5284 v->sprite_width = avctx->coded_width;
5285 v->sprite_height = avctx->coded_height;
5287 avctx->coded_width = avctx->width = v->output_width;
5288 avctx->coded_height = avctx->height = v->output_height;
5290 // prevent 16.16 overflows
5291 if (v->sprite_width > 1 << 14 ||
5292 v->sprite_height > 1 << 14 ||
5293 v->output_width > 1 << 14 ||
5294 v->output_height > 1 << 14) return -1;
5299 /** Close a VC1/WMV3 decoder
5300 * @warning Initial try at using MpegEncContext stuff
5302 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5304 VC1Context *v = avctx->priv_data;
5307 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5308 && v->sprite_output_frame.data[0])
5309 avctx->release_buffer(avctx, &v->sprite_output_frame);
5310 for (i = 0; i < 4; i++)
5311 av_freep(&v->sr_rows[i >> 1][i & 1]);
5312 av_freep(&v->hrd_rate);
5313 av_freep(&v->hrd_buffer);
5314 ff_MPV_common_end(&v->s);
5315 av_freep(&v->mv_type_mb_plane);
5316 av_freep(&v->direct_mb_plane);
5317 av_freep(&v->forward_mb_plane);
5318 av_freep(&v->fieldtx_plane);
5319 av_freep(&v->acpred_plane);
5320 av_freep(&v->over_flags_plane);
5321 av_freep(&v->mb_type_base);
5322 av_freep(&v->blk_mv_type_base);
5323 av_freep(&v->mv_f_base);
5324 av_freep(&v->mv_f_last_base);
5325 av_freep(&v->mv_f_next_base);
5326 av_freep(&v->block);
5327 av_freep(&v->cbp_base);
5328 av_freep(&v->ttblk_base);
5329 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5330 av_freep(&v->luma_mv_base);
5331 ff_intrax8_common_end(&v->x8);
5336 /** Decode a VC1/WMV3 frame
5337 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5339 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5340 int *got_frame, AVPacket *avpkt)
5342 const uint8_t *buf = avpkt->data;
5343 int buf_size = avpkt->size, n_slices = 0, i;
5344 VC1Context *v = avctx->priv_data;
5345 MpegEncContext *s = &v->s;
5346 AVFrame *pict = data;
5347 uint8_t *buf2 = NULL;
5348 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5349 int mb_height, n_slices1=-1;
5354 } *slices = NULL, *tmp;
5356 v->second_field = 0;
5358 if(s->flags & CODEC_FLAG_LOW_DELAY)
5361 /* no supplementary picture */
5362 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5363 /* special case for last picture */
5364 if (s->low_delay == 0 && s->next_picture_ptr) {
5365 *pict = s->next_picture_ptr->f;
5366 s->next_picture_ptr = NULL;
5374 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5375 if (v->profile < PROFILE_ADVANCED)
5376 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5378 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5381 //for advanced profile we may need to parse and unescape data
5382 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5384 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5386 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5387 const uint8_t *start, *end, *next;
5391 for (start = buf, end = buf + buf_size; next < end; start = next) {
5392 next = find_next_marker(start + 4, end);
5393 size = next - start - 4;
5394 if (size <= 0) continue;
5395 switch (AV_RB32(start)) {
5396 case VC1_CODE_FRAME:
5397 if (avctx->hwaccel ||
5398 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5400 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5402 case VC1_CODE_FIELD: {
5404 if (avctx->hwaccel ||
5405 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5406 buf_start_second_field = start;
5407 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5411 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5412 if (!slices[n_slices].buf)
5414 buf_size3 = vc1_unescape_buffer(start + 4, size,
5415 slices[n_slices].buf);
5416 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5418 /* assuming that the field marker is at the exact middle,
5419 hope it's correct */
5420 slices[n_slices].mby_start = s->mb_height >> 1;
5421 n_slices1 = n_slices - 1; // index of the last slice of the first field
5425 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5426 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5427 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5428 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5430 case VC1_CODE_SLICE: {
5432 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5436 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5437 if (!slices[n_slices].buf)
5439 buf_size3 = vc1_unescape_buffer(start + 4, size,
5440 slices[n_slices].buf);
5441 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5443 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5449 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5450 const uint8_t *divider;
5453 divider = find_next_marker(buf, buf + buf_size);
5454 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5455 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5457 } else { // found field marker, unescape second field
5458 if (avctx->hwaccel ||
5459 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5460 buf_start_second_field = divider;
5461 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5465 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5466 if (!slices[n_slices].buf)
5468 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5469 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5471 slices[n_slices].mby_start = s->mb_height >> 1;
5472 n_slices1 = n_slices - 1;
5475 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5477 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5479 init_get_bits(&s->gb, buf2, buf_size2*8);
5481 init_get_bits(&s->gb, buf, buf_size*8);
5483 if (v->res_sprite) {
5484 v->new_sprite = !get_bits1(&s->gb);
5485 v->two_sprites = get_bits1(&s->gb);
5486 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5487 we're using the sprite compositor. These are intentionally kept separate
5488 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5489 the vc1 one for WVP2 */
5490 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5491 if (v->new_sprite) {
5492 // switch AVCodecContext parameters to those of the sprites
5493 avctx->width = avctx->coded_width = v->sprite_width;
5494 avctx->height = avctx->coded_height = v->sprite_height;
5501 if (s->context_initialized &&
5502 (s->width != avctx->coded_width ||
5503 s->height != avctx->coded_height)) {
5504 ff_vc1_decode_end(avctx);
5507 if (!s->context_initialized) {
5508 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5511 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5513 if (v->profile == PROFILE_ADVANCED) {
5514 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5516 s->h_edge_pos = avctx->coded_width;
5517 s->v_edge_pos = avctx->coded_height;
5521 /* We need to set current_picture_ptr before reading the header,
5522 * otherwise we cannot store anything in there. */
5523 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5524 int i = ff_find_unused_picture(s, 0);
5527 s->current_picture_ptr = &s->picture[i];
5530 // do parse frame header
5531 v->pic_header_flag = 0;
5532 v->first_pic_header_flag = 1;
5533 if (v->profile < PROFILE_ADVANCED) {
5534 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5538 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5542 v->first_pic_header_flag = 0;
5544 if (avctx->debug & FF_DEBUG_PICT_INFO)
5545 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5547 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5548 && s->pict_type != AV_PICTURE_TYPE_I) {
5549 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5553 if ((s->mb_height >> v->field_mode) == 0) {
5554 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5558 // process pulldown flags
5559 s->current_picture_ptr->f.repeat_pict = 0;
5560 // Pulldown flags are only valid when 'broadcast' has been set.
5561 // So ticks_per_frame will be 2
5564 s->current_picture_ptr->f.repeat_pict = 1;
5565 } else if (v->rptfrm) {
5567 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5570 // for skipping the frame
5571 s->current_picture.f.pict_type = s->pict_type;
5572 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5574 /* skip B-frames if we don't have reference frames */
5575 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5578 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5579 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5580 avctx->skip_frame >= AVDISCARD_ALL) {
5584 if (s->next_p_frame_damaged) {
5585 if (s->pict_type == AV_PICTURE_TYPE_B)
5588 s->next_p_frame_damaged = 0;
5591 if (ff_MPV_frame_start(s, avctx) < 0) {
5595 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5596 v->s.current_picture_ptr->f.top_field_first = v->tff;
5598 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5599 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5601 if ((CONFIG_VC1_VDPAU_DECODER)
5602 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5603 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5604 else if (avctx->hwaccel) {
5605 if (v->field_mode && buf_start_second_field) {
5606 // decode first field
5607 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5608 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5610 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5612 if (avctx->hwaccel->end_frame(avctx) < 0)
5615 // decode second field
5616 s->gb = slices[n_slices1 + 1].gb;
5617 s->picture_structure = PICT_TOP_FIELD + v->tff;
5618 v->second_field = 1;
5619 v->pic_header_flag = 0;
5620 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5621 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5624 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
5626 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5628 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5630 if (avctx->hwaccel->end_frame(avctx) < 0)
5633 s->picture_structure = PICT_FRAME;
5634 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5636 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5638 if (avctx->hwaccel->end_frame(avctx) < 0)
5642 if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5643 goto err; // This codepath is still incomplete thus it is disabled
5645 ff_er_frame_start(s);
5647 v->bits = buf_size * 8;
5648 v->end_mb_x = s->mb_width;
5649 if (v->field_mode) {
5651 s->current_picture.f.linesize[0] <<= 1;
5652 s->current_picture.f.linesize[1] <<= 1;
5653 s->current_picture.f.linesize[2] <<= 1;
5655 s->uvlinesize <<= 1;
5656 tmp[0] = v->mv_f_last[0];
5657 tmp[1] = v->mv_f_last[1];
5658 v->mv_f_last[0] = v->mv_f_next[0];
5659 v->mv_f_last[1] = v->mv_f_next[1];
5660 v->mv_f_next[0] = v->mv_f[0];
5661 v->mv_f_next[1] = v->mv_f[1];
5662 v->mv_f[0] = tmp[0];
5663 v->mv_f[1] = tmp[1];
5665 mb_height = s->mb_height >> v->field_mode;
5666 for (i = 0; i <= n_slices; i++) {
5667 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5668 if (v->field_mode <= 0) {
5669 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5670 "picture boundary (%d >= %d)\n", i,
5671 slices[i - 1].mby_start, mb_height);
5674 v->second_field = 1;
5675 v->blocks_off = s->mb_width * s->mb_height << 1;
5676 v->mb_off = s->mb_stride * s->mb_height >> 1;
5678 v->second_field = 0;
5683 v->pic_header_flag = 0;
5684 if (v->field_mode && i == n_slices1 + 2) {
5685 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5686 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5689 } else if (get_bits1(&s->gb)) {
5690 v->pic_header_flag = 1;
5691 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5692 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5697 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5698 if (!v->field_mode || v->second_field)
5699 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5701 if (i >= n_slices) {
5702 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5705 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5707 if (s->end_mb_y <= s->start_mb_y) {
5708 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5711 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
5712 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
5715 ff_vc1_decode_blocks(v);
5717 s->gb = slices[i].gb;
5719 if (v->field_mode) {
5720 v->second_field = 0;
5721 if (s->pict_type == AV_PICTURE_TYPE_B) {
5722 memcpy(v->mv_f_base, v->mv_f_next_base,
5723 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5725 s->current_picture.f.linesize[0] >>= 1;
5726 s->current_picture.f.linesize[1] >>= 1;
5727 s->current_picture.f.linesize[2] >>= 1;
5729 s->uvlinesize >>= 1;
5731 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5732 get_bits_count(&s->gb), s->gb.size_in_bits);
5733 // if (get_bits_count(&s->gb) > buf_size * 8)
5735 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5741 ff_MPV_frame_end(s);
5743 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5745 avctx->width = avctx->coded_width = v->output_width;
5746 avctx->height = avctx->coded_height = v->output_height;
5747 if (avctx->skip_frame >= AVDISCARD_NONREF)
5749 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5750 if (vc1_decode_sprites(v, &s->gb))
5753 *pict = v->sprite_output_frame;
5756 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5757 *pict = s->current_picture_ptr->f;
5758 } else if (s->last_picture_ptr != NULL) {
5759 *pict = s->last_picture_ptr->f;
5761 if (s->last_picture_ptr || s->low_delay) {
5763 ff_print_debug_info(s, pict);
5769 for (i = 0; i < n_slices; i++)
5770 av_free(slices[i].buf);
5776 for (i = 0; i < n_slices; i++)
5777 av_free(slices[i].buf);
5783 static const AVProfile profiles[] = {
5784 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5785 { FF_PROFILE_VC1_MAIN, "Main" },
5786 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5787 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5788 { FF_PROFILE_UNKNOWN },
5791 AVCodec ff_vc1_decoder = {
5793 .type = AVMEDIA_TYPE_VIDEO,
5794 .id = AV_CODEC_ID_VC1,
5795 .priv_data_size = sizeof(VC1Context),
5796 .init = vc1_decode_init,
5797 .close = ff_vc1_decode_end,
5798 .decode = vc1_decode_frame,
5799 .flush = ff_mpeg_flush,
5800 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5801 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5802 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5803 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5806 #if CONFIG_WMV3_DECODER
5807 AVCodec ff_wmv3_decoder = {
5809 .type = AVMEDIA_TYPE_VIDEO,
5810 .id = AV_CODEC_ID_WMV3,
5811 .priv_data_size = sizeof(VC1Context),
5812 .init = vc1_decode_init,
5813 .close = ff_vc1_decode_end,
5814 .decode = vc1_decode_frame,
5815 .flush = ff_mpeg_flush,
5816 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5817 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5818 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5819 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5823 #if CONFIG_WMV3_VDPAU_DECODER
5824 AVCodec ff_wmv3_vdpau_decoder = {
5825 .name = "wmv3_vdpau",
5826 .type = AVMEDIA_TYPE_VIDEO,
5827 .id = AV_CODEC_ID_WMV3,
5828 .priv_data_size = sizeof(VC1Context),
5829 .init = vc1_decode_init,
5830 .close = ff_vc1_decode_end,
5831 .decode = vc1_decode_frame,
5832 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5833 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5834 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5835 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5839 #if CONFIG_VC1_VDPAU_DECODER
5840 AVCodec ff_vc1_vdpau_decoder = {
5841 .name = "vc1_vdpau",
5842 .type = AVMEDIA_TYPE_VIDEO,
5843 .id = AV_CODEC_ID_VC1,
5844 .priv_data_size = sizeof(VC1Context),
5845 .init = vc1_decode_init,
5846 .close = ff_vc1_decode_end,
5847 .decode = vc1_decode_frame,
5848 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5849 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5850 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5851 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5855 #if CONFIG_WMV3IMAGE_DECODER
5856 AVCodec ff_wmv3image_decoder = {
5857 .name = "wmv3image",
5858 .type = AVMEDIA_TYPE_VIDEO,
5859 .id = AV_CODEC_ID_WMV3IMAGE,
5860 .priv_data_size = sizeof(VC1Context),
5861 .init = vc1_decode_init,
5862 .close = ff_vc1_decode_end,
5863 .decode = vc1_decode_frame,
5864 .capabilities = CODEC_CAP_DR1,
5865 .flush = vc1_sprite_flush,
5866 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5867 .pix_fmts = ff_pixfmt_list_420
5871 #if CONFIG_VC1IMAGE_DECODER
5872 AVCodec ff_vc1image_decoder = {
5874 .type = AVMEDIA_TYPE_VIDEO,
5875 .id = AV_CODEC_ID_VC1IMAGE,
5876 .priv_data_size = sizeof(VC1Context),
5877 .init = vc1_decode_init,
5878 .close = ff_vc1_decode_end,
5879 .decode = vc1_decode_frame,
5880 .capabilities = CODEC_CAP_DR1,
5881 .flush = vc1_sprite_flush,
5882 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5883 .pix_fmts = ff_pixfmt_list_420