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 if (b_valid) { px = B[0]; py = B[1]; }
1760 if (c_valid) { px = C[0]; py = C[1]; }
1766 field_a = (A[1] & 4) ? 1 : 0;
1770 field_b = (B[1] & 4) ? 1 : 0;
1774 field_c = (C[1] & 4) ? 1 : 0;
1778 num_oppfield = field_a + field_b + field_c;
1779 num_samefield = total_valid - num_oppfield;
1780 if (total_valid == 3) {
1781 if ((num_samefield == 3) || (num_oppfield == 3)) {
1782 px = mid_pred(A[0], B[0], C[0]);
1783 py = mid_pred(A[1], B[1], C[1]);
1784 } else if (num_samefield >= num_oppfield) {
1785 /* take one MV from same field set depending on priority
1786 the check for B may not be necessary */
1787 px = !field_a ? A[0] : B[0];
1788 py = !field_a ? A[1] : B[1];
1790 px = field_a ? A[0] : B[0];
1791 py = field_a ? A[1] : B[1];
1793 } else if (total_valid == 2) {
1794 if (num_samefield >= num_oppfield) {
1795 if (!field_a && a_valid) {
1798 } else if (!field_b && b_valid) {
1801 } else if (c_valid) {
1806 if (field_a && a_valid) {
1809 } else if (field_b && b_valid) {
1812 } else if (c_valid) {
1817 } else if (total_valid == 1) {
1818 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1819 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1824 /* store MV using signed modulus of MV range defined in 4.11 */
1825 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;
1826 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;
1827 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1828 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1829 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1830 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1831 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1832 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1833 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1834 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1835 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1836 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1837 s->mv[0][n + 1][0] = s->mv[0][n][0];
1838 s->mv[0][n + 1][1] = s->mv[0][n][1];
1842 /** Motion compensation for direct or interpolated blocks in B-frames
1844 static void vc1_interp_mc(VC1Context *v)
1846 MpegEncContext *s = &v->s;
1847 DSPContext *dsp = &v->s.dsp;
1848 uint8_t *srcY, *srcU, *srcV;
1849 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1851 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1853 if (!v->field_mode && !v->s.next_picture.f.data[0])
1856 mx = s->mv[1][0][0];
1857 my = s->mv[1][0][1];
1858 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1859 uvmy = (my + ((my & 3) == 3)) >> 1;
1860 if (v->field_mode) {
1861 if (v->cur_field_type != v->ref_field_type[1])
1862 my = my - 2 + 4 * v->cur_field_type;
1863 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1866 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1867 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1869 srcY = s->next_picture.f.data[0];
1870 srcU = s->next_picture.f.data[1];
1871 srcV = s->next_picture.f.data[2];
1873 src_x = s->mb_x * 16 + (mx >> 2);
1874 src_y = s->mb_y * 16 + (my >> 2);
1875 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1876 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1878 if (v->profile != PROFILE_ADVANCED) {
1879 src_x = av_clip( src_x, -16, s->mb_width * 16);
1880 src_y = av_clip( src_y, -16, s->mb_height * 16);
1881 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1882 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1884 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1885 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1886 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1887 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1890 srcY += src_y * s->linesize + src_x;
1891 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1892 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1894 if (v->field_mode && v->ref_field_type[1]) {
1895 srcY += s->current_picture_ptr->f.linesize[0];
1896 srcU += s->current_picture_ptr->f.linesize[1];
1897 srcV += s->current_picture_ptr->f.linesize[2];
1900 /* for grayscale we should not try to read from unknown area */
1901 if (s->flags & CODEC_FLAG_GRAY) {
1902 srcU = s->edge_emu_buffer + 18 * s->linesize;
1903 srcV = s->edge_emu_buffer + 18 * s->linesize;
1906 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1907 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1908 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1909 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1911 srcY -= s->mspel * (1 + s->linesize);
1912 s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1913 17 + s->mspel * 2, 17 + s->mspel * 2,
1914 src_x - s->mspel, src_y - s->mspel,
1915 s->h_edge_pos, v_edge_pos);
1916 srcY = s->edge_emu_buffer;
1917 s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1918 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1919 s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1920 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1923 /* if we deal with range reduction we need to scale source blocks */
1924 if (v->rangeredfrm) {
1926 uint8_t *src, *src2;
1929 for (j = 0; j < 17 + s->mspel * 2; j++) {
1930 for (i = 0; i < 17 + s->mspel * 2; i++)
1931 src[i] = ((src[i] - 128) >> 1) + 128;
1936 for (j = 0; j < 9; j++) {
1937 for (i = 0; i < 9; i++) {
1938 src[i] = ((src[i] - 128) >> 1) + 128;
1939 src2[i] = ((src2[i] - 128) >> 1) + 128;
1941 src += s->uvlinesize;
1942 src2 += s->uvlinesize;
1945 srcY += s->mspel * (1 + s->linesize);
1948 if (v->field_mode && v->second_field) {
1949 off = s->current_picture_ptr->f.linesize[0];
1950 off_uv = s->current_picture_ptr->f.linesize[1];
1957 dxy = ((my & 3) << 2) | (mx & 3);
1958 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1959 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1960 srcY += s->linesize * 8;
1961 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1962 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1964 dxy = (my & 2) | ((mx & 2) >> 1);
1967 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1969 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1972 if (s->flags & CODEC_FLAG_GRAY) return;
1973 /* Chroma MC always uses qpel blilinear */
1974 uvmx = (uvmx & 3) << 1;
1975 uvmy = (uvmy & 3) << 1;
1977 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1978 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1980 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1981 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1985 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1989 #if B_FRACTION_DEN==256
1993 return 2 * ((value * n + 255) >> 9);
1994 return (value * n + 128) >> 8;
1997 n -= B_FRACTION_DEN;
1999 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
2000 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
2004 /** Reconstruct motion vector for B-frame and do motion compensation
2006 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
2007 int direct, int mode)
2010 v->mv_mode2 = v->mv_mode;
2011 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2017 v->mv_mode = v->mv_mode2;
2020 if (mode == BMV_TYPE_INTERPOLATED) {
2024 v->mv_mode = v->mv_mode2;
2028 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2029 v->mv_mode = v->mv_mode2;
2030 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2032 v->mv_mode = v->mv_mode2;
2035 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2036 int direct, int mvtype)
2038 MpegEncContext *s = &v->s;
2039 int xy, wrap, off = 0;
2044 const uint8_t *is_intra = v->mb_type[0];
2048 /* scale MV difference to be quad-pel */
2049 dmv_x[0] <<= 1 - s->quarter_sample;
2050 dmv_y[0] <<= 1 - s->quarter_sample;
2051 dmv_x[1] <<= 1 - s->quarter_sample;
2052 dmv_y[1] <<= 1 - s->quarter_sample;
2054 wrap = s->b8_stride;
2055 xy = s->block_index[0];
2058 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2059 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2060 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2061 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2064 if (!v->field_mode) {
2065 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2066 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2067 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2068 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2070 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2071 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));
2072 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));
2073 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));
2074 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));
2077 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2078 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2079 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2080 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2084 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2085 C = s->current_picture.f.motion_val[0][xy - 2];
2086 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2087 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2088 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2090 if (!s->mb_x) C[0] = C[1] = 0;
2091 if (!s->first_slice_line) { // predictor A is not out of bounds
2092 if (s->mb_width == 1) {
2096 px = mid_pred(A[0], B[0], C[0]);
2097 py = mid_pred(A[1], B[1], C[1]);
2099 } else if (s->mb_x) { // predictor C is not out of bounds
2105 /* Pullback MV as specified in 8.3.5.3.4 */
2108 if (v->profile < PROFILE_ADVANCED) {
2109 qx = (s->mb_x << 5);
2110 qy = (s->mb_y << 5);
2111 X = (s->mb_width << 5) - 4;
2112 Y = (s->mb_height << 5) - 4;
2113 if (qx + px < -28) px = -28 - qx;
2114 if (qy + py < -28) py = -28 - qy;
2115 if (qx + px > X) px = X - qx;
2116 if (qy + py > Y) py = Y - qy;
2118 qx = (s->mb_x << 6);
2119 qy = (s->mb_y << 6);
2120 X = (s->mb_width << 6) - 4;
2121 Y = (s->mb_height << 6) - 4;
2122 if (qx + px < -60) px = -60 - qx;
2123 if (qy + py < -60) py = -60 - qy;
2124 if (qx + px > X) px = X - qx;
2125 if (qy + py > Y) py = Y - qy;
2128 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2129 if (0 && !s->first_slice_line && s->mb_x) {
2130 if (is_intra[xy - wrap])
2131 sum = FFABS(px) + FFABS(py);
2133 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2135 if (get_bits1(&s->gb)) {
2143 if (is_intra[xy - 2])
2144 sum = FFABS(px) + FFABS(py);
2146 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2148 if (get_bits1(&s->gb)) {
2158 /* store MV using signed modulus of MV range defined in 4.11 */
2159 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2160 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2162 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2163 C = s->current_picture.f.motion_val[1][xy - 2];
2164 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2165 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2166 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2170 if (!s->first_slice_line) { // predictor A is not out of bounds
2171 if (s->mb_width == 1) {
2175 px = mid_pred(A[0], B[0], C[0]);
2176 py = mid_pred(A[1], B[1], C[1]);
2178 } else if (s->mb_x) { // predictor C is not out of bounds
2184 /* Pullback MV as specified in 8.3.5.3.4 */
2187 if (v->profile < PROFILE_ADVANCED) {
2188 qx = (s->mb_x << 5);
2189 qy = (s->mb_y << 5);
2190 X = (s->mb_width << 5) - 4;
2191 Y = (s->mb_height << 5) - 4;
2192 if (qx + px < -28) px = -28 - qx;
2193 if (qy + py < -28) py = -28 - qy;
2194 if (qx + px > X) px = X - qx;
2195 if (qy + py > Y) py = Y - qy;
2197 qx = (s->mb_x << 6);
2198 qy = (s->mb_y << 6);
2199 X = (s->mb_width << 6) - 4;
2200 Y = (s->mb_height << 6) - 4;
2201 if (qx + px < -60) px = -60 - qx;
2202 if (qy + py < -60) py = -60 - qy;
2203 if (qx + px > X) px = X - qx;
2204 if (qy + py > Y) py = Y - qy;
2207 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2208 if (0 && !s->first_slice_line && s->mb_x) {
2209 if (is_intra[xy - wrap])
2210 sum = FFABS(px) + FFABS(py);
2212 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2214 if (get_bits1(&s->gb)) {
2222 if (is_intra[xy - 2])
2223 sum = FFABS(px) + FFABS(py);
2225 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2227 if (get_bits1(&s->gb)) {
2237 /* store MV using signed modulus of MV range defined in 4.11 */
2239 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2240 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2242 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2243 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2244 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2245 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2248 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2250 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2251 MpegEncContext *s = &v->s;
2252 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2254 if (v->bmvtype == BMV_TYPE_DIRECT) {
2255 int total_opp, k, f;
2256 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2257 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2258 v->bfraction, 0, s->quarter_sample);
2259 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2260 v->bfraction, 0, s->quarter_sample);
2261 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2262 v->bfraction, 1, s->quarter_sample);
2263 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2264 v->bfraction, 1, s->quarter_sample);
2266 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2267 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2268 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2269 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2270 f = (total_opp > 2) ? 1 : 0;
2272 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2273 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2276 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2277 for (k = 0; k < 4; k++) {
2278 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2279 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2280 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2281 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2282 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2283 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2287 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2288 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);
2289 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);
2292 if (dir) { // backward
2293 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);
2294 if (n == 3 || mv1) {
2295 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2298 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);
2299 if (n == 3 || mv1) {
2300 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2305 /** Get predicted DC value for I-frames only
2306 * prediction dir: left=0, top=1
2307 * @param s MpegEncContext
2308 * @param overlap flag indicating that overlap filtering is used
2309 * @param pq integer part of picture quantizer
2310 * @param[in] n block index in the current MB
2311 * @param dc_val_ptr Pointer to DC predictor
2312 * @param dir_ptr Prediction direction for use in AC prediction
2314 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2315 int16_t **dc_val_ptr, int *dir_ptr)
2317 int a, b, c, wrap, pred, scale;
2319 static const uint16_t dcpred[32] = {
2320 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2321 114, 102, 93, 85, 79, 73, 68, 64,
2322 60, 57, 54, 51, 49, 47, 45, 43,
2323 41, 39, 38, 37, 35, 34, 33
2326 /* find prediction - wmv3_dc_scale always used here in fact */
2327 if (n < 4) scale = s->y_dc_scale;
2328 else scale = s->c_dc_scale;
2330 wrap = s->block_wrap[n];
2331 dc_val = s->dc_val[0] + s->block_index[n];
2337 b = dc_val[ - 1 - wrap];
2338 a = dc_val[ - wrap];
2340 if (pq < 9 || !overlap) {
2341 /* Set outer values */
2342 if (s->first_slice_line && (n != 2 && n != 3))
2343 b = a = dcpred[scale];
2344 if (s->mb_x == 0 && (n != 1 && n != 3))
2345 b = c = dcpred[scale];
2347 /* Set outer values */
2348 if (s->first_slice_line && (n != 2 && n != 3))
2350 if (s->mb_x == 0 && (n != 1 && n != 3))
2354 if (abs(a - b) <= abs(b - c)) {
2356 *dir_ptr = 1; // left
2359 *dir_ptr = 0; // top
2362 /* update predictor */
2363 *dc_val_ptr = &dc_val[0];
2368 /** Get predicted DC value
2369 * prediction dir: left=0, top=1
2370 * @param s MpegEncContext
2371 * @param overlap flag indicating that overlap filtering is used
2372 * @param pq integer part of picture quantizer
2373 * @param[in] n block index in the current MB
2374 * @param a_avail flag indicating top block availability
2375 * @param c_avail flag indicating left block availability
2376 * @param dc_val_ptr Pointer to DC predictor
2377 * @param dir_ptr Prediction direction for use in AC prediction
2379 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2380 int a_avail, int c_avail,
2381 int16_t **dc_val_ptr, int *dir_ptr)
2383 int a, b, c, wrap, pred;
2385 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2389 wrap = s->block_wrap[n];
2390 dc_val = s->dc_val[0] + s->block_index[n];
2396 b = dc_val[ - 1 - wrap];
2397 a = dc_val[ - wrap];
2398 /* scale predictors if needed */
2399 q1 = s->current_picture.f.qscale_table[mb_pos];
2400 dqscale_index = s->y_dc_scale_table[q1] - 1;
2401 if (dqscale_index < 0)
2403 if (c_avail && (n != 1 && n != 3)) {
2404 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2406 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2408 if (a_avail && (n != 2 && n != 3)) {
2409 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2411 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2413 if (a_avail && c_avail && (n != 3)) {
2418 off -= s->mb_stride;
2419 q2 = s->current_picture.f.qscale_table[off];
2421 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2424 if (a_avail && c_avail) {
2425 if (abs(a - b) <= abs(b - c)) {
2427 *dir_ptr = 1; // left
2430 *dir_ptr = 0; // top
2432 } else if (a_avail) {
2434 *dir_ptr = 0; // top
2435 } else if (c_avail) {
2437 *dir_ptr = 1; // left
2440 *dir_ptr = 1; // left
2443 /* update predictor */
2444 *dc_val_ptr = &dc_val[0];
2448 /** @} */ // Block group
2451 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2452 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2456 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2457 uint8_t **coded_block_ptr)
2459 int xy, wrap, pred, a, b, c;
2461 xy = s->block_index[n];
2462 wrap = s->b8_stride;
2467 a = s->coded_block[xy - 1 ];
2468 b = s->coded_block[xy - 1 - wrap];
2469 c = s->coded_block[xy - wrap];
2478 *coded_block_ptr = &s->coded_block[xy];
2484 * Decode one AC coefficient
2485 * @param v The VC1 context
2486 * @param last Last coefficient
2487 * @param skip How much zero coefficients to skip
2488 * @param value Decoded AC coefficient value
2489 * @param codingset set of VLC to decode data
2492 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2493 int *value, int codingset)
2495 GetBitContext *gb = &v->s.gb;
2496 int index, escape, run = 0, level = 0, lst = 0;
2498 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2499 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2500 run = vc1_index_decode_table[codingset][index][0];
2501 level = vc1_index_decode_table[codingset][index][1];
2502 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2506 escape = decode210(gb);
2508 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2509 run = vc1_index_decode_table[codingset][index][0];
2510 level = vc1_index_decode_table[codingset][index][1];
2511 lst = index >= vc1_last_decode_table[codingset];
2514 level += vc1_last_delta_level_table[codingset][run];
2516 level += vc1_delta_level_table[codingset][run];
2519 run += vc1_last_delta_run_table[codingset][level] + 1;
2521 run += vc1_delta_run_table[codingset][level] + 1;
2527 lst = get_bits1(gb);
2528 if (v->s.esc3_level_length == 0) {
2529 if (v->pq < 8 || v->dquantfrm) { // table 59
2530 v->s.esc3_level_length = get_bits(gb, 3);
2531 if (!v->s.esc3_level_length)
2532 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2533 } else { // table 60
2534 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2536 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2538 run = get_bits(gb, v->s.esc3_run_length);
2539 sign = get_bits1(gb);
2540 level = get_bits(gb, v->s.esc3_level_length);
2551 /** Decode intra block in intra frames - should be faster than decode_intra_block
2552 * @param v VC1Context
2553 * @param block block to decode
2554 * @param[in] n subblock index
2555 * @param coded are AC coeffs present or not
2556 * @param codingset set of VLC to decode data
2558 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2559 int coded, int codingset)
2561 GetBitContext *gb = &v->s.gb;
2562 MpegEncContext *s = &v->s;
2563 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2566 int16_t *ac_val, *ac_val2;
2569 /* Get DC differential */
2571 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2573 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2576 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2580 if (dcdiff == 119 /* ESC index value */) {
2581 /* TODO: Optimize */
2582 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2583 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2584 else dcdiff = get_bits(gb, 8);
2587 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2588 else if (v->pq == 2)
2589 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2596 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2599 /* Store the quantized DC coeff, used for prediction */
2601 block[0] = dcdiff * s->y_dc_scale;
2603 block[0] = dcdiff * s->c_dc_scale;
2614 int last = 0, skip, value;
2615 const uint8_t *zz_table;
2619 scale = v->pq * 2 + v->halfpq;
2623 zz_table = v->zz_8x8[2];
2625 zz_table = v->zz_8x8[3];
2627 zz_table = v->zz_8x8[1];
2629 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2631 if (dc_pred_dir) // left
2634 ac_val -= 16 * s->block_wrap[n];
2637 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2641 block[zz_table[i++]] = value;
2644 /* apply AC prediction if needed */
2646 if (dc_pred_dir) { // left
2647 for (k = 1; k < 8; k++)
2648 block[k << v->left_blk_sh] += ac_val[k];
2650 for (k = 1; k < 8; k++)
2651 block[k << v->top_blk_sh] += ac_val[k + 8];
2654 /* save AC coeffs for further prediction */
2655 for (k = 1; k < 8; k++) {
2656 ac_val2[k] = block[k << v->left_blk_sh];
2657 ac_val2[k + 8] = block[k << v->top_blk_sh];
2660 /* scale AC coeffs */
2661 for (k = 1; k < 64; k++)
2665 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2668 if (s->ac_pred) i = 63;
2674 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2678 scale = v->pq * 2 + v->halfpq;
2679 memset(ac_val2, 0, 16 * 2);
2680 if (dc_pred_dir) { // left
2683 memcpy(ac_val2, ac_val, 8 * 2);
2685 ac_val -= 16 * s->block_wrap[n];
2687 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2690 /* apply AC prediction if needed */
2692 if (dc_pred_dir) { //left
2693 for (k = 1; k < 8; k++) {
2694 block[k << v->left_blk_sh] = ac_val[k] * scale;
2695 if (!v->pquantizer && block[k << v->left_blk_sh])
2696 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2699 for (k = 1; k < 8; k++) {
2700 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2701 if (!v->pquantizer && block[k << v->top_blk_sh])
2702 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2708 s->block_last_index[n] = i;
2713 /** Decode intra block in intra frames - should be faster than decode_intra_block
2714 * @param v VC1Context
2715 * @param block block to decode
2716 * @param[in] n subblock number
2717 * @param coded are AC coeffs present or not
2718 * @param codingset set of VLC to decode data
2719 * @param mquant quantizer value for this macroblock
2721 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2722 int coded, int codingset, int mquant)
2724 GetBitContext *gb = &v->s.gb;
2725 MpegEncContext *s = &v->s;
2726 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2729 int16_t *ac_val, *ac_val2;
2731 int a_avail = v->a_avail, c_avail = v->c_avail;
2732 int use_pred = s->ac_pred;
2735 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2737 /* Get DC differential */
2739 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2741 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2744 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2748 if (dcdiff == 119 /* ESC index value */) {
2749 /* TODO: Optimize */
2750 if (mquant == 1) dcdiff = get_bits(gb, 10);
2751 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2752 else dcdiff = get_bits(gb, 8);
2755 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2756 else if (mquant == 2)
2757 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2764 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2767 /* Store the quantized DC coeff, used for prediction */
2769 block[0] = dcdiff * s->y_dc_scale;
2771 block[0] = dcdiff * s->c_dc_scale;
2777 /* check if AC is needed at all */
2778 if (!a_avail && !c_avail)
2780 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2783 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2785 if (dc_pred_dir) // left
2788 ac_val -= 16 * s->block_wrap[n];
2790 q1 = s->current_picture.f.qscale_table[mb_pos];
2791 if ( dc_pred_dir && c_avail && mb_pos)
2792 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2793 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2794 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2795 if ( dc_pred_dir && n == 1)
2797 if (!dc_pred_dir && n == 2)
2803 int last = 0, skip, value;
2804 const uint8_t *zz_table;
2808 if (!use_pred && v->fcm == ILACE_FRAME) {
2809 zz_table = v->zzi_8x8;
2811 if (!dc_pred_dir) // top
2812 zz_table = v->zz_8x8[2];
2814 zz_table = v->zz_8x8[3];
2817 if (v->fcm != ILACE_FRAME)
2818 zz_table = v->zz_8x8[1];
2820 zz_table = v->zzi_8x8;
2824 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2828 block[zz_table[i++]] = value;
2831 /* apply AC prediction if needed */
2833 /* scale predictors if needed*/
2834 if (q2 && q1 != q2) {
2835 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2836 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2839 return AVERROR_INVALIDDATA;
2840 if (dc_pred_dir) { // left
2841 for (k = 1; k < 8; k++)
2842 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2844 for (k = 1; k < 8; k++)
2845 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2848 if (dc_pred_dir) { //left
2849 for (k = 1; k < 8; k++)
2850 block[k << v->left_blk_sh] += ac_val[k];
2852 for (k = 1; k < 8; k++)
2853 block[k << v->top_blk_sh] += ac_val[k + 8];
2857 /* save AC coeffs for further prediction */
2858 for (k = 1; k < 8; k++) {
2859 ac_val2[k ] = block[k << v->left_blk_sh];
2860 ac_val2[k + 8] = block[k << v->top_blk_sh];
2863 /* scale AC coeffs */
2864 for (k = 1; k < 64; k++)
2868 block[k] += (block[k] < 0) ? -mquant : mquant;
2871 if (use_pred) i = 63;
2872 } else { // no AC coeffs
2875 memset(ac_val2, 0, 16 * 2);
2876 if (dc_pred_dir) { // left
2878 memcpy(ac_val2, ac_val, 8 * 2);
2879 if (q2 && q1 != q2) {
2880 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2881 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2883 return AVERROR_INVALIDDATA;
2884 for (k = 1; k < 8; k++)
2885 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2890 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2891 if (q2 && q1 != q2) {
2892 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2893 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2895 return AVERROR_INVALIDDATA;
2896 for (k = 1; k < 8; k++)
2897 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2902 /* apply AC prediction if needed */
2904 if (dc_pred_dir) { // left
2905 for (k = 1; k < 8; k++) {
2906 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2907 if (!v->pquantizer && block[k << v->left_blk_sh])
2908 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2911 for (k = 1; k < 8; k++) {
2912 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2913 if (!v->pquantizer && block[k << v->top_blk_sh])
2914 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2920 s->block_last_index[n] = i;
2925 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2926 * @param v VC1Context
2927 * @param block block to decode
2928 * @param[in] n subblock index
2929 * @param coded are AC coeffs present or not
2930 * @param mquant block quantizer
2931 * @param codingset set of VLC to decode data
2933 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2934 int coded, int mquant, int codingset)
2936 GetBitContext *gb = &v->s.gb;
2937 MpegEncContext *s = &v->s;
2938 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2941 int16_t *ac_val, *ac_val2;
2943 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2944 int a_avail = v->a_avail, c_avail = v->c_avail;
2945 int use_pred = s->ac_pred;
2949 s->dsp.clear_block(block);
2951 /* XXX: Guard against dumb values of mquant */
2952 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2954 /* Set DC scale - y and c use the same */
2955 s->y_dc_scale = s->y_dc_scale_table[mquant];
2956 s->c_dc_scale = s->c_dc_scale_table[mquant];
2958 /* Get DC differential */
2960 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2962 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2965 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2969 if (dcdiff == 119 /* ESC index value */) {
2970 /* TODO: Optimize */
2971 if (mquant == 1) dcdiff = get_bits(gb, 10);
2972 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2973 else dcdiff = get_bits(gb, 8);
2976 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2977 else if (mquant == 2)
2978 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2985 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2988 /* Store the quantized DC coeff, used for prediction */
2991 block[0] = dcdiff * s->y_dc_scale;
2993 block[0] = dcdiff * s->c_dc_scale;
2999 /* check if AC is needed at all and adjust direction if needed */
3000 if (!a_avail) dc_pred_dir = 1;
3001 if (!c_avail) dc_pred_dir = 0;
3002 if (!a_avail && !c_avail) use_pred = 0;
3003 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
3006 scale = mquant * 2 + v->halfpq;
3008 if (dc_pred_dir) //left
3011 ac_val -= 16 * s->block_wrap[n];
3013 q1 = s->current_picture.f.qscale_table[mb_pos];
3014 if (dc_pred_dir && c_avail && mb_pos)
3015 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3016 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3017 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3018 if ( dc_pred_dir && n == 1)
3020 if (!dc_pred_dir && n == 2)
3022 if (n == 3) q2 = q1;
3025 int last = 0, skip, value;
3029 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3033 if (v->fcm == PROGRESSIVE)
3034 block[v->zz_8x8[0][i++]] = value;
3036 if (use_pred && (v->fcm == ILACE_FRAME)) {
3037 if (!dc_pred_dir) // top
3038 block[v->zz_8x8[2][i++]] = value;
3040 block[v->zz_8x8[3][i++]] = value;
3042 block[v->zzi_8x8[i++]] = value;
3047 /* apply AC prediction if needed */
3049 /* scale predictors if needed*/
3050 if (q2 && q1 != q2) {
3051 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3052 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3055 return AVERROR_INVALIDDATA;
3056 if (dc_pred_dir) { // left
3057 for (k = 1; k < 8; k++)
3058 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3060 for (k = 1; k < 8; k++)
3061 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3064 if (dc_pred_dir) { // left
3065 for (k = 1; k < 8; k++)
3066 block[k << v->left_blk_sh] += ac_val[k];
3068 for (k = 1; k < 8; k++)
3069 block[k << v->top_blk_sh] += ac_val[k + 8];
3073 /* save AC coeffs for further prediction */
3074 for (k = 1; k < 8; k++) {
3075 ac_val2[k ] = block[k << v->left_blk_sh];
3076 ac_val2[k + 8] = block[k << v->top_blk_sh];
3079 /* scale AC coeffs */
3080 for (k = 1; k < 64; k++)
3084 block[k] += (block[k] < 0) ? -mquant : mquant;
3087 if (use_pred) i = 63;
3088 } else { // no AC coeffs
3091 memset(ac_val2, 0, 16 * 2);
3092 if (dc_pred_dir) { // left
3094 memcpy(ac_val2, ac_val, 8 * 2);
3095 if (q2 && q1 != q2) {
3096 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3097 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3099 return AVERROR_INVALIDDATA;
3100 for (k = 1; k < 8; k++)
3101 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3106 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3107 if (q2 && q1 != q2) {
3108 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3109 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3111 return AVERROR_INVALIDDATA;
3112 for (k = 1; k < 8; k++)
3113 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3118 /* apply AC prediction if needed */
3120 if (dc_pred_dir) { // left
3121 for (k = 1; k < 8; k++) {
3122 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3123 if (!v->pquantizer && block[k << v->left_blk_sh])
3124 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3127 for (k = 1; k < 8; k++) {
3128 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3129 if (!v->pquantizer && block[k << v->top_blk_sh])
3130 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3136 s->block_last_index[n] = i;
3143 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3144 int mquant, int ttmb, int first_block,
3145 uint8_t *dst, int linesize, int skip_block,
3148 MpegEncContext *s = &v->s;
3149 GetBitContext *gb = &s->gb;
3152 int scale, off, idx, last, skip, value;
3153 int ttblk = ttmb & 7;
3156 s->dsp.clear_block(block);
3159 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)];
3161 if (ttblk == TT_4X4) {
3162 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3164 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3165 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3166 || (!v->res_rtm_flag && !first_block))) {
3167 subblkpat = decode012(gb);
3169 subblkpat ^= 3; // swap decoded pattern bits
3170 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3172 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3175 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3177 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3178 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3179 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3182 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3183 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3192 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3197 idx = v->zz_8x8[0][i++];
3199 idx = v->zzi_8x8[i++];
3200 block[idx] = value * scale;
3202 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3206 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3208 v->vc1dsp.vc1_inv_trans_8x8(block);
3209 s->dsp.add_pixels_clamped(block, dst, linesize);
3214 pat = ~subblkpat & 0xF;
3215 for (j = 0; j < 4; j++) {
3216 last = subblkpat & (1 << (3 - j));
3218 off = (j & 1) * 4 + (j & 2) * 16;
3220 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3225 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3227 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3228 block[idx + off] = value * scale;
3230 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3232 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3234 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3236 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3241 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3242 for (j = 0; j < 2; j++) {
3243 last = subblkpat & (1 << (1 - j));
3247 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3252 idx = v->zz_8x4[i++] + off;
3254 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3255 block[idx] = value * scale;
3257 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3259 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3261 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3263 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3268 pat = ~(subblkpat * 5) & 0xF;
3269 for (j = 0; j < 2; j++) {
3270 last = subblkpat & (1 << (1 - j));
3274 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3279 idx = v->zz_4x8[i++] + off;
3281 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3282 block[idx] = value * scale;
3284 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3286 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3288 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3290 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3296 *ttmb_out |= ttblk << (n * 4);
3300 /** @} */ // Macroblock group
3302 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3303 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3305 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3307 MpegEncContext *s = &v->s;
3308 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3309 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3310 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3311 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3312 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3315 if (block_num > 3) {
3316 dst = s->dest[block_num - 3];
3318 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3320 if (s->mb_y != s->end_mb_y || block_num < 2) {
3324 if (block_num > 3) {
3325 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3326 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3327 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3328 mv_stride = s->mb_stride;
3330 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3331 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3332 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3333 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3334 mv_stride = s->b8_stride;
3335 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3338 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3339 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3340 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3342 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3344 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3347 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3349 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3354 dst -= 4 * linesize;
3355 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3356 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3357 idx = (block_cbp | (block_cbp >> 2)) & 3;
3359 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3362 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3364 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3369 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3371 MpegEncContext *s = &v->s;
3372 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3373 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3374 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3375 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3376 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3379 if (block_num > 3) {
3380 dst = s->dest[block_num - 3] - 8 * linesize;
3382 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3385 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3388 if (block_num > 3) {
3389 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3390 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3391 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3393 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3394 : (mb_cbp >> ((block_num + 1) * 4));
3395 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3396 : (mb_is_intra >> ((block_num + 1) * 4));
3397 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3399 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3400 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3402 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3404 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3407 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3409 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3415 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3416 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3417 idx = (block_cbp | (block_cbp >> 1)) & 5;
3419 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3422 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3424 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3429 static void vc1_apply_p_loop_filter(VC1Context *v)
3431 MpegEncContext *s = &v->s;
3434 for (i = 0; i < 6; i++) {
3435 vc1_apply_p_v_loop_filter(v, i);
3438 /* V always precedes H, therefore we run H one MB before V;
3439 * at the end of a row, we catch up to complete the row */
3441 for (i = 0; i < 6; i++) {
3442 vc1_apply_p_h_loop_filter(v, i);
3444 if (s->mb_x == s->mb_width - 1) {
3446 ff_update_block_index(s);
3447 for (i = 0; i < 6; i++) {
3448 vc1_apply_p_h_loop_filter(v, i);
3454 /** Decode one P-frame MB
3456 static int vc1_decode_p_mb(VC1Context *v)
3458 MpegEncContext *s = &v->s;
3459 GetBitContext *gb = &s->gb;
3461 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3462 int cbp; /* cbp decoding stuff */
3463 int mqdiff, mquant; /* MB quantization */
3464 int ttmb = v->ttfrm; /* MB Transform type */
3466 int mb_has_coeffs = 1; /* last_flag */
3467 int dmv_x, dmv_y; /* Differential MV components */
3468 int index, index1; /* LUT indexes */
3469 int val, sign; /* temp values */
3470 int first_block = 1;
3472 int skipped, fourmv;
3473 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3475 mquant = v->pq; /* lossy initialization */
3477 if (v->mv_type_is_raw)
3478 fourmv = get_bits1(gb);
3480 fourmv = v->mv_type_mb_plane[mb_pos];
3482 skipped = get_bits1(gb);
3484 skipped = v->s.mbskip_table[mb_pos];
3486 if (!fourmv) { /* 1MV mode */
3488 GET_MVDATA(dmv_x, dmv_y);
3491 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3492 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3494 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3495 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3497 /* FIXME Set DC val for inter block ? */
3498 if (s->mb_intra && !mb_has_coeffs) {
3500 s->ac_pred = get_bits1(gb);
3502 } else if (mb_has_coeffs) {
3504 s->ac_pred = get_bits1(gb);
3505 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3511 s->current_picture.f.qscale_table[mb_pos] = mquant;
3513 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3514 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3515 VC1_TTMB_VLC_BITS, 2);
3516 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3518 for (i = 0; i < 6; i++) {
3519 s->dc_val[0][s->block_index[i]] = 0;
3521 val = ((cbp >> (5 - i)) & 1);
3522 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3523 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3525 /* check if prediction blocks A and C are available */
3526 v->a_avail = v->c_avail = 0;
3527 if (i == 2 || i == 3 || !s->first_slice_line)
3528 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3529 if (i == 1 || i == 3 || s->mb_x)
3530 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3532 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3533 (i & 4) ? v->codingset2 : v->codingset);
3534 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3536 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3538 for (j = 0; j < 64; j++)
3539 s->block[i][j] <<= 1;
3540 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3541 if (v->pq >= 9 && v->overlap) {
3543 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3545 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3547 block_cbp |= 0xF << (i << 2);
3548 block_intra |= 1 << i;
3550 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3551 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3552 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3553 block_cbp |= pat << (i << 2);
3554 if (!v->ttmbf && ttmb < 8)
3561 for (i = 0; i < 6; i++) {
3562 v->mb_type[0][s->block_index[i]] = 0;
3563 s->dc_val[0][s->block_index[i]] = 0;
3565 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3566 s->current_picture.f.qscale_table[mb_pos] = 0;
3567 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3570 } else { // 4MV mode
3571 if (!skipped /* unskipped MB */) {
3572 int intra_count = 0, coded_inter = 0;
3573 int is_intra[6], is_coded[6];
3575 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3576 for (i = 0; i < 6; i++) {
3577 val = ((cbp >> (5 - i)) & 1);
3578 s->dc_val[0][s->block_index[i]] = 0;
3585 GET_MVDATA(dmv_x, dmv_y);
3587 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3589 vc1_mc_4mv_luma(v, i, 0);
3590 intra_count += s->mb_intra;
3591 is_intra[i] = s->mb_intra;
3592 is_coded[i] = mb_has_coeffs;
3595 is_intra[i] = (intra_count >= 3);
3599 vc1_mc_4mv_chroma(v, 0);
3600 v->mb_type[0][s->block_index[i]] = is_intra[i];
3602 coded_inter = !is_intra[i] & is_coded[i];
3604 // if there are no coded blocks then don't do anything more
3606 if (!intra_count && !coded_inter)
3609 s->current_picture.f.qscale_table[mb_pos] = mquant;
3610 /* test if block is intra and has pred */
3613 for (i = 0; i < 6; i++)
3615 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3616 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3622 s->ac_pred = get_bits1(gb);
3626 if (!v->ttmbf && coded_inter)
3627 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3628 for (i = 0; i < 6; i++) {
3630 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3631 s->mb_intra = is_intra[i];
3633 /* check if prediction blocks A and C are available */
3634 v->a_avail = v->c_avail = 0;
3635 if (i == 2 || i == 3 || !s->first_slice_line)
3636 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3637 if (i == 1 || i == 3 || s->mb_x)
3638 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3640 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3641 (i & 4) ? v->codingset2 : v->codingset);
3642 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3644 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3646 for (j = 0; j < 64; j++)
3647 s->block[i][j] <<= 1;
3648 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3649 (i & 4) ? s->uvlinesize : s->linesize);
3650 if (v->pq >= 9 && v->overlap) {
3652 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3654 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3656 block_cbp |= 0xF << (i << 2);
3657 block_intra |= 1 << i;
3658 } else if (is_coded[i]) {
3659 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3660 first_block, s->dest[dst_idx] + off,
3661 (i & 4) ? s->uvlinesize : s->linesize,
3662 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3664 block_cbp |= pat << (i << 2);
3665 if (!v->ttmbf && ttmb < 8)
3670 } else { // skipped MB
3672 s->current_picture.f.qscale_table[mb_pos] = 0;
3673 for (i = 0; i < 6; i++) {
3674 v->mb_type[0][s->block_index[i]] = 0;
3675 s->dc_val[0][s->block_index[i]] = 0;
3677 for (i = 0; i < 4; i++) {
3678 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3679 vc1_mc_4mv_luma(v, i, 0);
3681 vc1_mc_4mv_chroma(v, 0);
3682 s->current_picture.f.qscale_table[mb_pos] = 0;
3686 v->cbp[s->mb_x] = block_cbp;
3687 v->ttblk[s->mb_x] = block_tt;
3688 v->is_intra[s->mb_x] = block_intra;
3693 /* Decode one macroblock in an interlaced frame p picture */
3695 static int vc1_decode_p_mb_intfr(VC1Context *v)
3697 MpegEncContext *s = &v->s;
3698 GetBitContext *gb = &s->gb;
3700 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3701 int cbp = 0; /* cbp decoding stuff */
3702 int mqdiff, mquant; /* MB quantization */
3703 int ttmb = v->ttfrm; /* MB Transform type */
3705 int mb_has_coeffs = 1; /* last_flag */
3706 int dmv_x, dmv_y; /* Differential MV components */
3707 int val; /* temp value */
3708 int first_block = 1;
3710 int skipped, fourmv = 0, twomv = 0;
3711 int block_cbp = 0, pat, block_tt = 0;
3712 int idx_mbmode = 0, mvbp;
3713 int stride_y, fieldtx;
3715 mquant = v->pq; /* Lossy initialization */
3718 skipped = get_bits1(gb);
3720 skipped = v->s.mbskip_table[mb_pos];
3722 if (v->fourmvswitch)
3723 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3725 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3726 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3727 /* store the motion vector type in a flag (useful later) */
3728 case MV_PMODE_INTFR_4MV:
3730 v->blk_mv_type[s->block_index[0]] = 0;
3731 v->blk_mv_type[s->block_index[1]] = 0;
3732 v->blk_mv_type[s->block_index[2]] = 0;
3733 v->blk_mv_type[s->block_index[3]] = 0;
3735 case MV_PMODE_INTFR_4MV_FIELD:
3737 v->blk_mv_type[s->block_index[0]] = 1;
3738 v->blk_mv_type[s->block_index[1]] = 1;
3739 v->blk_mv_type[s->block_index[2]] = 1;
3740 v->blk_mv_type[s->block_index[3]] = 1;
3742 case MV_PMODE_INTFR_2MV_FIELD:
3744 v->blk_mv_type[s->block_index[0]] = 1;
3745 v->blk_mv_type[s->block_index[1]] = 1;
3746 v->blk_mv_type[s->block_index[2]] = 1;
3747 v->blk_mv_type[s->block_index[3]] = 1;
3749 case MV_PMODE_INTFR_1MV:
3750 v->blk_mv_type[s->block_index[0]] = 0;
3751 v->blk_mv_type[s->block_index[1]] = 0;
3752 v->blk_mv_type[s->block_index[2]] = 0;
3753 v->blk_mv_type[s->block_index[3]] = 0;
3756 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3757 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3758 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3759 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3760 s->mb_intra = v->is_intra[s->mb_x] = 1;
3761 for (i = 0; i < 6; i++)
3762 v->mb_type[0][s->block_index[i]] = 1;
3763 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3764 mb_has_coeffs = get_bits1(gb);
3766 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3767 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3769 s->current_picture.f.qscale_table[mb_pos] = mquant;
3770 /* Set DC scale - y and c use the same (not sure if necessary here) */
3771 s->y_dc_scale = s->y_dc_scale_table[mquant];
3772 s->c_dc_scale = s->c_dc_scale_table[mquant];
3774 for (i = 0; i < 6; i++) {
3775 s->dc_val[0][s->block_index[i]] = 0;
3777 val = ((cbp >> (5 - i)) & 1);
3778 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3779 v->a_avail = v->c_avail = 0;
3780 if (i == 2 || i == 3 || !s->first_slice_line)
3781 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3782 if (i == 1 || i == 3 || s->mb_x)
3783 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3785 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3786 (i & 4) ? v->codingset2 : v->codingset);
3787 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3788 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3790 stride_y = s->linesize << fieldtx;
3791 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3793 stride_y = s->uvlinesize;
3796 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3800 } else { // inter MB
3801 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3803 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3804 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3805 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3807 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3808 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3809 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3812 s->mb_intra = v->is_intra[s->mb_x] = 0;
3813 for (i = 0; i < 6; i++)
3814 v->mb_type[0][s->block_index[i]] = 0;
3815 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3816 /* for all motion vector read MVDATA and motion compensate each block */
3820 for (i = 0; i < 6; i++) {
3823 val = ((mvbp >> (3 - i)) & 1);
3825 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3827 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3828 vc1_mc_4mv_luma(v, i, 0);
3829 } else if (i == 4) {
3830 vc1_mc_4mv_chroma4(v);
3837 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3839 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3840 vc1_mc_4mv_luma(v, 0, 0);
3841 vc1_mc_4mv_luma(v, 1, 0);
3844 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3846 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3847 vc1_mc_4mv_luma(v, 2, 0);
3848 vc1_mc_4mv_luma(v, 3, 0);
3849 vc1_mc_4mv_chroma4(v);
3851 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3854 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3856 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3860 GET_MQUANT(); // p. 227
3861 s->current_picture.f.qscale_table[mb_pos] = mquant;
3862 if (!v->ttmbf && cbp)
3863 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3864 for (i = 0; i < 6; i++) {
3865 s->dc_val[0][s->block_index[i]] = 0;
3867 val = ((cbp >> (5 - i)) & 1);
3869 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3871 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3873 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3874 first_block, s->dest[dst_idx] + off,
3875 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3876 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3877 block_cbp |= pat << (i << 2);
3878 if (!v->ttmbf && ttmb < 8)
3885 s->mb_intra = v->is_intra[s->mb_x] = 0;
3886 for (i = 0; i < 6; i++) {
3887 v->mb_type[0][s->block_index[i]] = 0;
3888 s->dc_val[0][s->block_index[i]] = 0;
3890 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3891 s->current_picture.f.qscale_table[mb_pos] = 0;
3892 v->blk_mv_type[s->block_index[0]] = 0;
3893 v->blk_mv_type[s->block_index[1]] = 0;
3894 v->blk_mv_type[s->block_index[2]] = 0;
3895 v->blk_mv_type[s->block_index[3]] = 0;
3896 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3899 if (s->mb_x == s->mb_width - 1)
3900 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3904 static int vc1_decode_p_mb_intfi(VC1Context *v)
3906 MpegEncContext *s = &v->s;
3907 GetBitContext *gb = &s->gb;
3909 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3910 int cbp = 0; /* cbp decoding stuff */
3911 int mqdiff, mquant; /* MB quantization */
3912 int ttmb = v->ttfrm; /* MB Transform type */
3914 int mb_has_coeffs = 1; /* last_flag */
3915 int dmv_x, dmv_y; /* Differential MV components */
3916 int val; /* temp values */
3917 int first_block = 1;
3920 int block_cbp = 0, pat, block_tt = 0;
3923 mquant = v->pq; /* Lossy initialization */
3925 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3926 if (idx_mbmode <= 1) { // intra MB
3927 s->mb_intra = v->is_intra[s->mb_x] = 1;
3928 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3929 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3930 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3932 s->current_picture.f.qscale_table[mb_pos] = mquant;
3933 /* Set DC scale - y and c use the same (not sure if necessary here) */
3934 s->y_dc_scale = s->y_dc_scale_table[mquant];
3935 s->c_dc_scale = s->c_dc_scale_table[mquant];
3936 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3937 mb_has_coeffs = idx_mbmode & 1;
3939 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3941 for (i = 0; i < 6; i++) {
3942 s->dc_val[0][s->block_index[i]] = 0;
3943 v->mb_type[0][s->block_index[i]] = 1;
3945 val = ((cbp >> (5 - i)) & 1);
3946 v->a_avail = v->c_avail = 0;
3947 if (i == 2 || i == 3 || !s->first_slice_line)
3948 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3949 if (i == 1 || i == 3 || s->mb_x)
3950 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3952 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3953 (i & 4) ? v->codingset2 : v->codingset);
3954 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3956 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3957 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3958 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3959 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3960 // TODO: loop filter
3963 s->mb_intra = v->is_intra[s->mb_x] = 0;
3964 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3965 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3966 if (idx_mbmode <= 5) { // 1-MV
3967 dmv_x = dmv_y = pred_flag = 0;
3968 if (idx_mbmode & 1) {
3969 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3971 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3973 mb_has_coeffs = !(idx_mbmode & 2);
3975 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3976 for (i = 0; i < 6; i++) {
3978 dmv_x = dmv_y = pred_flag = 0;
3979 val = ((v->fourmvbp >> (3 - i)) & 1);
3981 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3983 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3984 vc1_mc_4mv_luma(v, i, 0);
3986 vc1_mc_4mv_chroma(v, 0);
3988 mb_has_coeffs = idx_mbmode & 1;
3991 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3995 s->current_picture.f.qscale_table[mb_pos] = mquant;
3996 if (!v->ttmbf && cbp) {
3997 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4000 for (i = 0; i < 6; i++) {
4001 s->dc_val[0][s->block_index[i]] = 0;
4003 val = ((cbp >> (5 - i)) & 1);
4004 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4005 if (v->second_field)
4006 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4008 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4009 first_block, s->dest[dst_idx] + off,
4010 (i & 4) ? s->uvlinesize : s->linesize,
4011 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
4013 block_cbp |= pat << (i << 2);
4014 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4019 if (s->mb_x == s->mb_width - 1)
4020 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4024 /** Decode one B-frame MB (in Main profile)
4026 static void vc1_decode_b_mb(VC1Context *v)
4028 MpegEncContext *s = &v->s;
4029 GetBitContext *gb = &s->gb;
4031 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4032 int cbp = 0; /* cbp decoding stuff */
4033 int mqdiff, mquant; /* MB quantization */
4034 int ttmb = v->ttfrm; /* MB Transform type */
4035 int mb_has_coeffs = 0; /* last_flag */
4036 int index, index1; /* LUT indexes */
4037 int val, sign; /* temp values */
4038 int first_block = 1;
4040 int skipped, direct;
4041 int dmv_x[2], dmv_y[2];
4042 int bmvtype = BMV_TYPE_BACKWARD;
4044 mquant = v->pq; /* lossy initialization */
4048 direct = get_bits1(gb);
4050 direct = v->direct_mb_plane[mb_pos];
4052 skipped = get_bits1(gb);
4054 skipped = v->s.mbskip_table[mb_pos];
4056 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4057 for (i = 0; i < 6; i++) {
4058 v->mb_type[0][s->block_index[i]] = 0;
4059 s->dc_val[0][s->block_index[i]] = 0;
4061 s->current_picture.f.qscale_table[mb_pos] = 0;
4065 GET_MVDATA(dmv_x[0], dmv_y[0]);
4066 dmv_x[1] = dmv_x[0];
4067 dmv_y[1] = dmv_y[0];
4069 if (skipped || !s->mb_intra) {
4070 bmvtype = decode012(gb);
4073 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4076 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4079 bmvtype = BMV_TYPE_INTERPOLATED;
4080 dmv_x[0] = dmv_y[0] = 0;
4084 for (i = 0; i < 6; i++)
4085 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4089 bmvtype = BMV_TYPE_INTERPOLATED;
4090 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4091 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4095 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4098 s->current_picture.f.qscale_table[mb_pos] = mquant;
4100 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4101 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4102 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4103 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4105 if (!mb_has_coeffs && !s->mb_intra) {
4106 /* no coded blocks - effectively skipped */
4107 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4108 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4111 if (s->mb_intra && !mb_has_coeffs) {
4113 s->current_picture.f.qscale_table[mb_pos] = mquant;
4114 s->ac_pred = get_bits1(gb);
4116 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4118 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4119 GET_MVDATA(dmv_x[0], dmv_y[0]);
4120 if (!mb_has_coeffs) {
4121 /* interpolated skipped block */
4122 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4123 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4127 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4129 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4132 s->ac_pred = get_bits1(gb);
4133 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4135 s->current_picture.f.qscale_table[mb_pos] = mquant;
4136 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4137 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4141 for (i = 0; i < 6; i++) {
4142 s->dc_val[0][s->block_index[i]] = 0;
4144 val = ((cbp >> (5 - i)) & 1);
4145 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4146 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4148 /* check if prediction blocks A and C are available */
4149 v->a_avail = v->c_avail = 0;
4150 if (i == 2 || i == 3 || !s->first_slice_line)
4151 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4152 if (i == 1 || i == 3 || s->mb_x)
4153 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4155 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4156 (i & 4) ? v->codingset2 : v->codingset);
4157 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4159 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4161 for (j = 0; j < 64; j++)
4162 s->block[i][j] <<= 1;
4163 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4165 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4166 first_block, s->dest[dst_idx] + off,
4167 (i & 4) ? s->uvlinesize : s->linesize,
4168 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4169 if (!v->ttmbf && ttmb < 8)
4176 /** Decode one B-frame MB (in interlaced field B picture)
4178 static void vc1_decode_b_mb_intfi(VC1Context *v)
4180 MpegEncContext *s = &v->s;
4181 GetBitContext *gb = &s->gb;
4183 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4184 int cbp = 0; /* cbp decoding stuff */
4185 int mqdiff, mquant; /* MB quantization */
4186 int ttmb = v->ttfrm; /* MB Transform type */
4187 int mb_has_coeffs = 0; /* last_flag */
4188 int val; /* temp value */
4189 int first_block = 1;
4192 int dmv_x[2], dmv_y[2], pred_flag[2];
4193 int bmvtype = BMV_TYPE_BACKWARD;
4194 int idx_mbmode, interpmvp;
4196 mquant = v->pq; /* Lossy initialization */
4199 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4200 if (idx_mbmode <= 1) { // intra MB
4201 s->mb_intra = v->is_intra[s->mb_x] = 1;
4202 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4203 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4204 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4206 s->current_picture.f.qscale_table[mb_pos] = mquant;
4207 /* Set DC scale - y and c use the same (not sure if necessary here) */
4208 s->y_dc_scale = s->y_dc_scale_table[mquant];
4209 s->c_dc_scale = s->c_dc_scale_table[mquant];
4210 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4211 mb_has_coeffs = idx_mbmode & 1;
4213 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4215 for (i = 0; i < 6; i++) {
4216 s->dc_val[0][s->block_index[i]] = 0;
4218 val = ((cbp >> (5 - i)) & 1);
4219 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4220 v->a_avail = v->c_avail = 0;
4221 if (i == 2 || i == 3 || !s->first_slice_line)
4222 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4223 if (i == 1 || i == 3 || s->mb_x)
4224 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4226 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4227 (i & 4) ? v->codingset2 : v->codingset);
4228 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4230 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4232 for (j = 0; j < 64; j++)
4233 s->block[i][j] <<= 1;
4234 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4235 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4236 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4237 // TODO: yet to perform loop filter
4240 s->mb_intra = v->is_intra[s->mb_x] = 0;
4241 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4242 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4244 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4246 fwd = v->forward_mb_plane[mb_pos];
4247 if (idx_mbmode <= 5) { // 1-MV
4248 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4249 pred_flag[0] = pred_flag[1] = 0;
4251 bmvtype = BMV_TYPE_FORWARD;
4253 bmvtype = decode012(gb);
4256 bmvtype = BMV_TYPE_BACKWARD;
4259 bmvtype = BMV_TYPE_DIRECT;
4262 bmvtype = BMV_TYPE_INTERPOLATED;
4263 interpmvp = get_bits1(gb);
4266 v->bmvtype = bmvtype;
4267 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4268 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4270 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4271 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4273 if (bmvtype == BMV_TYPE_DIRECT) {
4274 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4275 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4277 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4278 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4279 mb_has_coeffs = !(idx_mbmode & 2);
4282 bmvtype = BMV_TYPE_FORWARD;
4283 v->bmvtype = bmvtype;
4284 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4285 for (i = 0; i < 6; i++) {
4287 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4288 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4289 val = ((v->fourmvbp >> (3 - i)) & 1);
4291 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4292 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4293 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4295 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4296 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4298 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4300 mb_has_coeffs = idx_mbmode & 1;
4303 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4307 s->current_picture.f.qscale_table[mb_pos] = mquant;
4308 if (!v->ttmbf && cbp) {
4309 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4312 for (i = 0; i < 6; i++) {
4313 s->dc_val[0][s->block_index[i]] = 0;
4315 val = ((cbp >> (5 - i)) & 1);
4316 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4317 if (v->second_field)
4318 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4320 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4321 first_block, s->dest[dst_idx] + off,
4322 (i & 4) ? s->uvlinesize : s->linesize,
4323 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4324 if (!v->ttmbf && ttmb < 8)
4332 /** Decode blocks of I-frame
4334 static void vc1_decode_i_blocks(VC1Context *v)
4337 MpegEncContext *s = &v->s;
4342 /* select codingmode used for VLC tables selection */
4343 switch (v->y_ac_table_index) {
4345 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4348 v->codingset = CS_HIGH_MOT_INTRA;
4351 v->codingset = CS_MID_RATE_INTRA;
4355 switch (v->c_ac_table_index) {
4357 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4360 v->codingset2 = CS_HIGH_MOT_INTER;
4363 v->codingset2 = CS_MID_RATE_INTER;
4367 /* Set DC scale - y and c use the same */
4368 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4369 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4372 s->mb_x = s->mb_y = 0;
4374 s->first_slice_line = 1;
4375 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4377 ff_init_block_index(s);
4378 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4380 ff_update_block_index(s);
4381 dst[0] = s->dest[0];
4382 dst[1] = dst[0] + 8;
4383 dst[2] = s->dest[0] + s->linesize * 8;
4384 dst[3] = dst[2] + 8;
4385 dst[4] = s->dest[1];
4386 dst[5] = s->dest[2];
4387 s->dsp.clear_blocks(s->block[0]);
4388 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4389 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4390 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4391 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4392 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4394 // do actual MB decoding and displaying
4395 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4396 v->s.ac_pred = get_bits1(&v->s.gb);
4398 for (k = 0; k < 6; k++) {
4399 val = ((cbp >> (5 - k)) & 1);
4402 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4406 cbp |= val << (5 - k);
4408 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4410 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4412 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4413 if (v->pq >= 9 && v->overlap) {
4415 for (j = 0; j < 64; j++)
4416 s->block[k][j] <<= 1;
4417 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4420 for (j = 0; j < 64; j++)
4421 s->block[k][j] = (s->block[k][j] - 64) << 1;
4422 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4426 if (v->pq >= 9 && v->overlap) {
4428 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4429 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4430 if (!(s->flags & CODEC_FLAG_GRAY)) {
4431 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4432 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4435 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4436 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4437 if (!s->first_slice_line) {
4438 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4439 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4440 if (!(s->flags & CODEC_FLAG_GRAY)) {
4441 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4442 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4445 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4446 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4448 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4450 if (get_bits_count(&s->gb) > v->bits) {
4451 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4452 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4453 get_bits_count(&s->gb), v->bits);
4457 if (!v->s.loop_filter)
4458 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4460 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4462 s->first_slice_line = 0;
4464 if (v->s.loop_filter)
4465 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4467 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4468 * profile, these only differ are when decoding MSS2 rectangles. */
4469 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4472 /** Decode blocks of I-frame for advanced profile
4474 static void vc1_decode_i_blocks_adv(VC1Context *v)
4477 MpegEncContext *s = &v->s;
4483 GetBitContext *gb = &s->gb;
4485 /* select codingmode used for VLC tables selection */
4486 switch (v->y_ac_table_index) {
4488 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4491 v->codingset = CS_HIGH_MOT_INTRA;
4494 v->codingset = CS_MID_RATE_INTRA;
4498 switch (v->c_ac_table_index) {
4500 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4503 v->codingset2 = CS_HIGH_MOT_INTER;
4506 v->codingset2 = CS_MID_RATE_INTER;
4511 s->mb_x = s->mb_y = 0;
4513 s->first_slice_line = 1;
4514 s->mb_y = s->start_mb_y;
4515 if (s->start_mb_y) {
4517 ff_init_block_index(s);
4518 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4519 (1 + s->b8_stride) * sizeof(*s->coded_block));
4521 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4523 ff_init_block_index(s);
4524 for (;s->mb_x < s->mb_width; s->mb_x++) {
4525 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4526 ff_update_block_index(s);
4527 s->dsp.clear_blocks(block[0]);
4528 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4529 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4530 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4531 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4533 // do actual MB decoding and displaying
4534 if (v->fieldtx_is_raw)
4535 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4536 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4537 if ( v->acpred_is_raw)
4538 v->s.ac_pred = get_bits1(&v->s.gb);
4540 v->s.ac_pred = v->acpred_plane[mb_pos];
4542 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4543 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4547 s->current_picture.f.qscale_table[mb_pos] = mquant;
4548 /* Set DC scale - y and c use the same */
4549 s->y_dc_scale = s->y_dc_scale_table[mquant];
4550 s->c_dc_scale = s->c_dc_scale_table[mquant];
4552 for (k = 0; k < 6; k++) {
4553 val = ((cbp >> (5 - k)) & 1);
4556 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4560 cbp |= val << (5 - k);
4562 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4563 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4565 vc1_decode_i_block_adv(v, block[k], k, val,
4566 (k < 4) ? v->codingset : v->codingset2, mquant);
4568 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4570 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4573 vc1_smooth_overlap_filter_iblk(v);
4574 vc1_put_signed_blocks_clamped(v);
4575 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4577 if (get_bits_count(&s->gb) > v->bits) {
4578 // TODO: may need modification to handle slice coding
4579 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4580 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4581 get_bits_count(&s->gb), v->bits);
4585 if (!v->s.loop_filter)
4586 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4588 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4589 s->first_slice_line = 0;
4592 /* raw bottom MB row */
4594 ff_init_block_index(s);
4595 for (;s->mb_x < s->mb_width; s->mb_x++) {
4596 ff_update_block_index(s);
4597 vc1_put_signed_blocks_clamped(v);
4598 if (v->s.loop_filter)
4599 vc1_loop_filter_iblk_delayed(v, v->pq);
4601 if (v->s.loop_filter)
4602 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4603 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4604 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4607 static void vc1_decode_p_blocks(VC1Context *v)
4609 MpegEncContext *s = &v->s;
4610 int apply_loop_filter;
4612 /* select codingmode used for VLC tables selection */
4613 switch (v->c_ac_table_index) {
4615 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4618 v->codingset = CS_HIGH_MOT_INTRA;
4621 v->codingset = CS_MID_RATE_INTRA;
4625 switch (v->c_ac_table_index) {
4627 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4630 v->codingset2 = CS_HIGH_MOT_INTER;
4633 v->codingset2 = CS_MID_RATE_INTER;
4637 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4638 s->first_slice_line = 1;
4639 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4640 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4642 ff_init_block_index(s);
4643 for (; s->mb_x < s->mb_width; s->mb_x++) {
4644 ff_update_block_index(s);
4646 if (v->fcm == ILACE_FIELD)
4647 vc1_decode_p_mb_intfi(v);
4648 else if (v->fcm == ILACE_FRAME)
4649 vc1_decode_p_mb_intfr(v);
4650 else vc1_decode_p_mb(v);
4651 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4652 vc1_apply_p_loop_filter(v);
4653 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4654 // TODO: may need modification to handle slice coding
4655 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4656 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4657 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4661 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4662 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4663 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4664 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4665 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4666 s->first_slice_line = 0;
4668 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4670 ff_init_block_index(s);
4671 for (; s->mb_x < s->mb_width; s->mb_x++) {
4672 ff_update_block_index(s);
4673 vc1_apply_p_loop_filter(v);
4676 if (s->end_mb_y >= s->start_mb_y)
4677 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4678 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4679 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4682 static void vc1_decode_b_blocks(VC1Context *v)
4684 MpegEncContext *s = &v->s;
4686 /* select codingmode used for VLC tables selection */
4687 switch (v->c_ac_table_index) {
4689 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4692 v->codingset = CS_HIGH_MOT_INTRA;
4695 v->codingset = CS_MID_RATE_INTRA;
4699 switch (v->c_ac_table_index) {
4701 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4704 v->codingset2 = CS_HIGH_MOT_INTER;
4707 v->codingset2 = CS_MID_RATE_INTER;
4711 s->first_slice_line = 1;
4712 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4714 ff_init_block_index(s);
4715 for (; s->mb_x < s->mb_width; s->mb_x++) {
4716 ff_update_block_index(s);
4718 if (v->fcm == ILACE_FIELD)
4719 vc1_decode_b_mb_intfi(v);
4722 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4723 // TODO: may need modification to handle slice coding
4724 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4725 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4726 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4729 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4731 if (!v->s.loop_filter)
4732 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4734 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4735 s->first_slice_line = 0;
4737 if (v->s.loop_filter)
4738 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4739 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4740 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4743 static void vc1_decode_skip_blocks(VC1Context *v)
4745 MpegEncContext *s = &v->s;
4747 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4748 s->first_slice_line = 1;
4749 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4751 ff_init_block_index(s);
4752 ff_update_block_index(s);
4753 if (s->last_picture.f.data[0]) {
4754 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4755 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4756 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4758 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4759 s->first_slice_line = 0;
4761 s->pict_type = AV_PICTURE_TYPE_P;
4764 void ff_vc1_decode_blocks(VC1Context *v)
4767 v->s.esc3_level_length = 0;
4769 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4772 v->left_blk_idx = -1;
4773 v->topleft_blk_idx = 1;
4775 switch (v->s.pict_type) {
4776 case AV_PICTURE_TYPE_I:
4777 if (v->profile == PROFILE_ADVANCED)
4778 vc1_decode_i_blocks_adv(v);
4780 vc1_decode_i_blocks(v);
4782 case AV_PICTURE_TYPE_P:
4783 if (v->p_frame_skipped)
4784 vc1_decode_skip_blocks(v);
4786 vc1_decode_p_blocks(v);
4788 case AV_PICTURE_TYPE_B:
4790 if (v->profile == PROFILE_ADVANCED)
4791 vc1_decode_i_blocks_adv(v);
4793 vc1_decode_i_blocks(v);
4795 vc1_decode_b_blocks(v);
4801 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4805 * Transform coefficients for both sprites in 16.16 fixed point format,
4806 * in the order they appear in the bitstream:
4808 * rotation 1 (unused)
4810 * rotation 2 (unused)
4817 int effect_type, effect_flag;
4818 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4819 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4822 static inline int get_fp_val(GetBitContext* gb)
4824 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4827 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4831 switch (get_bits(gb, 2)) {
4834 c[2] = get_fp_val(gb);
4838 c[0] = c[4] = get_fp_val(gb);
4839 c[2] = get_fp_val(gb);
4842 c[0] = get_fp_val(gb);
4843 c[2] = get_fp_val(gb);
4844 c[4] = get_fp_val(gb);
4847 c[0] = get_fp_val(gb);
4848 c[1] = get_fp_val(gb);
4849 c[2] = get_fp_val(gb);
4850 c[3] = get_fp_val(gb);
4851 c[4] = get_fp_val(gb);
4854 c[5] = get_fp_val(gb);
4856 c[6] = get_fp_val(gb);
4861 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4863 AVCodecContext *avctx = v->s.avctx;
4866 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4867 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4868 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4869 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4870 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4871 for (i = 0; i < 7; i++)
4872 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4873 sd->coefs[sprite][i] / (1<<16),
4874 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4875 av_log(avctx, AV_LOG_DEBUG, "\n");
4879 if (sd->effect_type = get_bits_long(gb, 30)) {
4880 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4882 vc1_sprite_parse_transform(gb, sd->effect_params1);
4885 vc1_sprite_parse_transform(gb, sd->effect_params1);
4886 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4889 for (i = 0; i < sd->effect_pcount1; i++)
4890 sd->effect_params1[i] = get_fp_val(gb);
4892 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4893 // effect 13 is simple alpha blending and matches the opacity above
4894 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4895 for (i = 0; i < sd->effect_pcount1; i++)
4896 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4897 sd->effect_params1[i] / (1 << 16),
4898 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4899 av_log(avctx, AV_LOG_DEBUG, "\n");
4902 sd->effect_pcount2 = get_bits(gb, 16);
4903 if (sd->effect_pcount2 > 10) {
4904 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4906 } else if (sd->effect_pcount2) {
4908 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4909 while (++i < sd->effect_pcount2) {
4910 sd->effect_params2[i] = get_fp_val(gb);
4911 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4912 sd->effect_params2[i] / (1 << 16),
4913 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4915 av_log(avctx, AV_LOG_DEBUG, "\n");
4918 if (sd->effect_flag = get_bits1(gb))
4919 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4921 if (get_bits_count(gb) >= gb->size_in_bits +
4922 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4923 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4924 if (get_bits_count(gb) < gb->size_in_bits - 8)
4925 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4928 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4930 int i, plane, row, sprite;
4931 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4932 uint8_t* src_h[2][2];
4933 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4935 MpegEncContext *s = &v->s;
4937 for (i = 0; i < 2; i++) {
4938 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4939 xadv[i] = sd->coefs[i][0];
4940 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4941 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4943 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4944 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4946 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4948 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4949 int width = v->output_width>>!!plane;
4951 for (row = 0; row < v->output_height>>!!plane; row++) {
4952 uint8_t *dst = v->sprite_output_frame.data[plane] +
4953 v->sprite_output_frame.linesize[plane] * row;
4955 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4956 uint8_t *iplane = s->current_picture.f.data[plane];
4957 int iline = s->current_picture.f.linesize[plane];
4958 int ycoord = yoff[sprite] + yadv[sprite] * row;
4959 int yline = ycoord >> 16;
4961 ysub[sprite] = ycoord & 0xFFFF;
4963 iplane = s->last_picture.f.data[plane];
4964 iline = s->last_picture.f.linesize[plane];
4966 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4967 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4968 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4970 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4972 if (sr_cache[sprite][0] != yline) {
4973 if (sr_cache[sprite][1] == yline) {
4974 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4975 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4977 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4978 sr_cache[sprite][0] = yline;
4981 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4982 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4983 iplane + next_line, xoff[sprite],
4984 xadv[sprite], width);
4985 sr_cache[sprite][1] = yline + 1;
4987 src_h[sprite][0] = v->sr_rows[sprite][0];
4988 src_h[sprite][1] = v->sr_rows[sprite][1];
4992 if (!v->two_sprites) {
4994 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4996 memcpy(dst, src_h[0][0], width);
4999 if (ysub[0] && ysub[1]) {
5000 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
5001 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
5002 } else if (ysub[0]) {
5003 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
5004 src_h[1][0], alpha, width);
5005 } else if (ysub[1]) {
5006 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
5007 src_h[0][0], (1<<16)-1-alpha, width);
5009 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
5015 for (i = 0; i < 2; i++) {
5025 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5027 MpegEncContext *s = &v->s;
5028 AVCodecContext *avctx = s->avctx;
5031 vc1_parse_sprites(v, gb, &sd);
5033 if (!s->current_picture.f.data[0]) {
5034 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5038 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5039 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5043 if (v->sprite_output_frame.data[0])
5044 avctx->release_buffer(avctx, &v->sprite_output_frame);
5046 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5047 v->sprite_output_frame.reference = 0;
5048 if (ff_get_buffer(avctx, &v->sprite_output_frame) < 0) {
5049 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5053 vc1_draw_sprites(v, &sd);
5058 static void vc1_sprite_flush(AVCodecContext *avctx)
5060 VC1Context *v = avctx->priv_data;
5061 MpegEncContext *s = &v->s;
5062 AVFrame *f = &s->current_picture.f;
5065 /* Windows Media Image codecs have a convergence interval of two keyframes.
5066 Since we can't enforce it, clear to black the missing sprite. This is
5067 wrong but it looks better than doing nothing. */
5070 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5071 for (i = 0; i < v->sprite_height>>!!plane; i++)
5072 memset(f->data[plane] + i * f->linesize[plane],
5073 plane ? 128 : 0, f->linesize[plane]);
5078 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5080 MpegEncContext *s = &v->s;
5083 /* Allocate mb bitplanes */
5084 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5085 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5086 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5087 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5088 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5089 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5091 v->n_allocated_blks = s->mb_width + 2;
5092 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5093 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5094 v->cbp = v->cbp_base + s->mb_stride;
5095 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5096 v->ttblk = v->ttblk_base + s->mb_stride;
5097 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5098 v->is_intra = v->is_intra_base + s->mb_stride;
5099 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5100 v->luma_mv = v->luma_mv_base + s->mb_stride;
5102 /* allocate block type info in that way so it could be used with s->block_index[] */
5103 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5104 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5105 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5106 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5108 /* allocate memory to store block level MV info */
5109 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5110 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5111 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5112 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5113 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5114 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5115 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5116 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);
5117 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5118 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5119 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);
5121 /* Init coded blocks info */
5122 if (v->profile == PROFILE_ADVANCED) {
5123 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5125 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5129 ff_intrax8_common_init(&v->x8,s);
5131 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5132 for (i = 0; i < 4; i++)
5133 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5136 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5137 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5144 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5147 for (i = 0; i < 64; i++) {
5148 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5149 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5150 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5151 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5152 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5153 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5159 /** Initialize a VC1/WMV3 decoder
5160 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5161 * @todo TODO: Decypher remaining bits in extra_data
5163 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5165 VC1Context *v = avctx->priv_data;
5166 MpegEncContext *s = &v->s;
5169 /* save the container output size for WMImage */
5170 v->output_width = avctx->width;
5171 v->output_height = avctx->height;
5173 if (!avctx->extradata_size || !avctx->extradata)
5175 if (!(avctx->flags & CODEC_FLAG_GRAY))
5176 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5178 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5179 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5181 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5182 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5184 if (avctx->idct_algo == FF_IDCT_AUTO) {
5185 avctx->idct_algo = FF_IDCT_WMV2;
5188 if (ff_vc1_init_common(v) < 0)
5190 // ensure static VLC tables are initialized
5191 if (ff_msmpeg4_decode_init(avctx) < 0)
5193 if (ff_vc1_decode_init_alloc_tables(v) < 0)
5195 // Hack to ensure the above functions will be called
5196 // again once we know all necessary settings.
5197 // That this is necessary might indicate a bug.
5198 ff_vc1_decode_end(avctx);
5199 ff_vc1dsp_init(&v->vc1dsp);
5201 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5204 // looks like WMV3 has a sequence header stored in the extradata
5205 // advanced sequence header may be before the first frame
5206 // the last byte of the extradata is a version number, 1 for the
5207 // samples we can decode
5209 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5211 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5214 count = avctx->extradata_size*8 - get_bits_count(&gb);
5216 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5217 count, get_bits(&gb, count));
5218 } else if (count < 0) {
5219 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5221 } else { // VC1/WVC1/WVP2
5222 const uint8_t *start = avctx->extradata;
5223 uint8_t *end = avctx->extradata + avctx->extradata_size;
5224 const uint8_t *next;
5225 int size, buf2_size;
5226 uint8_t *buf2 = NULL;
5227 int seq_initialized = 0, ep_initialized = 0;
5229 if (avctx->extradata_size < 16) {
5230 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5234 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5235 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5237 for (; next < end; start = next) {
5238 next = find_next_marker(start + 4, end);
5239 size = next - start - 4;
5242 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5243 init_get_bits(&gb, buf2, buf2_size * 8);
5244 switch (AV_RB32(start)) {
5245 case VC1_CODE_SEQHDR:
5246 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5250 seq_initialized = 1;
5252 case VC1_CODE_ENTRYPOINT:
5253 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5262 if (!seq_initialized || !ep_initialized) {
5263 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5266 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5269 avctx->profile = v->profile;
5270 if (v->profile == PROFILE_ADVANCED)
5271 avctx->level = v->level;
5273 avctx->has_b_frames = !!avctx->max_b_frames;
5275 s->mb_width = (avctx->coded_width + 15) >> 4;
5276 s->mb_height = (avctx->coded_height + 15) >> 4;
5278 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5279 ff_vc1_init_transposed_scantables(v);
5281 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5286 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5287 v->sprite_width = avctx->coded_width;
5288 v->sprite_height = avctx->coded_height;
5290 avctx->coded_width = avctx->width = v->output_width;
5291 avctx->coded_height = avctx->height = v->output_height;
5293 // prevent 16.16 overflows
5294 if (v->sprite_width > 1 << 14 ||
5295 v->sprite_height > 1 << 14 ||
5296 v->output_width > 1 << 14 ||
5297 v->output_height > 1 << 14) return -1;
5302 /** Close a VC1/WMV3 decoder
5303 * @warning Initial try at using MpegEncContext stuff
5305 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5307 VC1Context *v = avctx->priv_data;
5310 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5311 && v->sprite_output_frame.data[0])
5312 avctx->release_buffer(avctx, &v->sprite_output_frame);
5313 for (i = 0; i < 4; i++)
5314 av_freep(&v->sr_rows[i >> 1][i & 1]);
5315 av_freep(&v->hrd_rate);
5316 av_freep(&v->hrd_buffer);
5317 ff_MPV_common_end(&v->s);
5318 av_freep(&v->mv_type_mb_plane);
5319 av_freep(&v->direct_mb_plane);
5320 av_freep(&v->forward_mb_plane);
5321 av_freep(&v->fieldtx_plane);
5322 av_freep(&v->acpred_plane);
5323 av_freep(&v->over_flags_plane);
5324 av_freep(&v->mb_type_base);
5325 av_freep(&v->blk_mv_type_base);
5326 av_freep(&v->mv_f_base);
5327 av_freep(&v->mv_f_last_base);
5328 av_freep(&v->mv_f_next_base);
5329 av_freep(&v->block);
5330 av_freep(&v->cbp_base);
5331 av_freep(&v->ttblk_base);
5332 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5333 av_freep(&v->luma_mv_base);
5334 ff_intrax8_common_end(&v->x8);
5339 /** Decode a VC1/WMV3 frame
5340 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5342 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5343 int *got_frame, AVPacket *avpkt)
5345 const uint8_t *buf = avpkt->data;
5346 int buf_size = avpkt->size, n_slices = 0, i;
5347 VC1Context *v = avctx->priv_data;
5348 MpegEncContext *s = &v->s;
5349 AVFrame *pict = data;
5350 uint8_t *buf2 = NULL;
5351 const uint8_t *buf_start = buf, *buf_start_second_field = NULL;
5352 int mb_height, n_slices1=-1;
5357 } *slices = NULL, *tmp;
5359 v->second_field = 0;
5361 if(s->flags & CODEC_FLAG_LOW_DELAY)
5364 /* no supplementary picture */
5365 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5366 /* special case for last picture */
5367 if (s->low_delay == 0 && s->next_picture_ptr) {
5368 *pict = s->next_picture_ptr->f;
5369 s->next_picture_ptr = NULL;
5377 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5378 if (v->profile < PROFILE_ADVANCED)
5379 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5381 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5384 //for advanced profile we may need to parse and unescape data
5385 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5387 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5389 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5390 const uint8_t *start, *end, *next;
5394 for (start = buf, end = buf + buf_size; next < end; start = next) {
5395 next = find_next_marker(start + 4, end);
5396 size = next - start - 4;
5397 if (size <= 0) continue;
5398 switch (AV_RB32(start)) {
5399 case VC1_CODE_FRAME:
5400 if (avctx->hwaccel ||
5401 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5403 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5405 case VC1_CODE_FIELD: {
5407 if (avctx->hwaccel ||
5408 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5409 buf_start_second_field = start;
5410 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5414 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5415 if (!slices[n_slices].buf)
5417 buf_size3 = vc1_unescape_buffer(start + 4, size,
5418 slices[n_slices].buf);
5419 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5421 /* assuming that the field marker is at the exact middle,
5422 hope it's correct */
5423 slices[n_slices].mby_start = s->mb_height >> 1;
5424 n_slices1 = n_slices - 1; // index of the last slice of the first field
5428 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5429 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5430 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5431 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5433 case VC1_CODE_SLICE: {
5435 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5439 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5440 if (!slices[n_slices].buf)
5442 buf_size3 = vc1_unescape_buffer(start + 4, size,
5443 slices[n_slices].buf);
5444 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5446 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5452 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5453 const uint8_t *divider;
5456 divider = find_next_marker(buf, buf + buf_size);
5457 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5458 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5460 } else { // found field marker, unescape second field
5461 if (avctx->hwaccel ||
5462 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5463 buf_start_second_field = divider;
5464 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5468 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5469 if (!slices[n_slices].buf)
5471 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5472 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5474 slices[n_slices].mby_start = s->mb_height >> 1;
5475 n_slices1 = n_slices - 1;
5478 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5480 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5482 init_get_bits(&s->gb, buf2, buf_size2*8);
5484 init_get_bits(&s->gb, buf, buf_size*8);
5486 if (v->res_sprite) {
5487 v->new_sprite = !get_bits1(&s->gb);
5488 v->two_sprites = get_bits1(&s->gb);
5489 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5490 we're using the sprite compositor. These are intentionally kept separate
5491 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5492 the vc1 one for WVP2 */
5493 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5494 if (v->new_sprite) {
5495 // switch AVCodecContext parameters to those of the sprites
5496 avctx->width = avctx->coded_width = v->sprite_width;
5497 avctx->height = avctx->coded_height = v->sprite_height;
5504 if (s->context_initialized &&
5505 (s->width != avctx->coded_width ||
5506 s->height != avctx->coded_height)) {
5507 ff_vc1_decode_end(avctx);
5510 if (!s->context_initialized) {
5511 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5514 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5516 if (v->profile == PROFILE_ADVANCED) {
5517 if(avctx->coded_width<=1 || avctx->coded_height<=1)
5519 s->h_edge_pos = avctx->coded_width;
5520 s->v_edge_pos = avctx->coded_height;
5524 /* We need to set current_picture_ptr before reading the header,
5525 * otherwise we cannot store anything in there. */
5526 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5527 int i = ff_find_unused_picture(s, 0);
5530 s->current_picture_ptr = &s->picture[i];
5533 // do parse frame header
5534 v->pic_header_flag = 0;
5535 v->first_pic_header_flag = 1;
5536 if (v->profile < PROFILE_ADVANCED) {
5537 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5541 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5545 v->first_pic_header_flag = 0;
5547 if (avctx->debug & FF_DEBUG_PICT_INFO)
5548 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5550 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5551 && s->pict_type != AV_PICTURE_TYPE_I) {
5552 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5556 if ((s->mb_height >> v->field_mode) == 0) {
5557 av_log(v->s.avctx, AV_LOG_ERROR, "image too short\n");
5561 // process pulldown flags
5562 s->current_picture_ptr->f.repeat_pict = 0;
5563 // Pulldown flags are only valid when 'broadcast' has been set.
5564 // So ticks_per_frame will be 2
5567 s->current_picture_ptr->f.repeat_pict = 1;
5568 } else if (v->rptfrm) {
5570 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5573 // for skipping the frame
5574 s->current_picture.f.pict_type = s->pict_type;
5575 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5577 /* skip B-frames if we don't have reference frames */
5578 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->droppable)) {
5581 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5582 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5583 avctx->skip_frame >= AVDISCARD_ALL) {
5587 if (s->next_p_frame_damaged) {
5588 if (s->pict_type == AV_PICTURE_TYPE_B)
5591 s->next_p_frame_damaged = 0;
5594 if (ff_MPV_frame_start(s, avctx) < 0) {
5598 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5599 v->s.current_picture_ptr->f.top_field_first = v->tff;
5601 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5602 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5604 if ((CONFIG_VC1_VDPAU_DECODER)
5605 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5606 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5607 else if (avctx->hwaccel) {
5608 if (v->field_mode && buf_start_second_field) {
5609 // decode first field
5610 s->picture_structure = PICT_BOTTOM_FIELD - v->tff;
5611 if (avctx->hwaccel->start_frame(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5613 if (avctx->hwaccel->decode_slice(avctx, buf_start, buf_start_second_field - buf_start) < 0)
5615 if (avctx->hwaccel->end_frame(avctx) < 0)
5618 // decode second field
5619 s->gb = slices[n_slices1 + 1].gb;
5620 s->picture_structure = PICT_TOP_FIELD + v->tff;
5621 v->second_field = 1;
5622 v->pic_header_flag = 0;
5623 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5624 av_log(avctx, AV_LOG_ERROR, "parsing header for second field failed");
5627 v->s.current_picture_ptr->f.pict_type = v->s.pict_type;
5629 if (avctx->hwaccel->start_frame(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5631 if (avctx->hwaccel->decode_slice(avctx, buf_start_second_field, (buf + buf_size) - buf_start_second_field) < 0)
5633 if (avctx->hwaccel->end_frame(avctx) < 0)
5636 s->picture_structure = PICT_FRAME;
5637 if (avctx->hwaccel->start_frame(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5639 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5641 if (avctx->hwaccel->end_frame(avctx) < 0)
5645 if (v->fcm == ILACE_FRAME && s->pict_type == AV_PICTURE_TYPE_B)
5646 goto err; // This codepath is still incomplete thus it is disabled
5648 ff_er_frame_start(s);
5650 v->bits = buf_size * 8;
5651 v->end_mb_x = s->mb_width;
5652 if (v->field_mode) {
5654 s->current_picture.f.linesize[0] <<= 1;
5655 s->current_picture.f.linesize[1] <<= 1;
5656 s->current_picture.f.linesize[2] <<= 1;
5658 s->uvlinesize <<= 1;
5659 tmp[0] = v->mv_f_last[0];
5660 tmp[1] = v->mv_f_last[1];
5661 v->mv_f_last[0] = v->mv_f_next[0];
5662 v->mv_f_last[1] = v->mv_f_next[1];
5663 v->mv_f_next[0] = v->mv_f[0];
5664 v->mv_f_next[1] = v->mv_f[1];
5665 v->mv_f[0] = tmp[0];
5666 v->mv_f[1] = tmp[1];
5668 mb_height = s->mb_height >> v->field_mode;
5669 for (i = 0; i <= n_slices; i++) {
5670 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5671 if (v->field_mode <= 0) {
5672 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5673 "picture boundary (%d >= %d)\n", i,
5674 slices[i - 1].mby_start, mb_height);
5677 v->second_field = 1;
5678 v->blocks_off = s->mb_width * s->mb_height << 1;
5679 v->mb_off = s->mb_stride * s->mb_height >> 1;
5681 v->second_field = 0;
5686 v->pic_header_flag = 0;
5687 if (v->field_mode && i == n_slices1 + 2) {
5688 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5689 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5692 } else if (get_bits1(&s->gb)) {
5693 v->pic_header_flag = 1;
5694 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5695 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5700 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5701 if (!v->field_mode || v->second_field)
5702 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5704 if (i >= n_slices) {
5705 av_log(v->s.avctx, AV_LOG_ERROR, "first field slice count too large\n");
5708 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5710 if (s->end_mb_y <= s->start_mb_y) {
5711 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5714 if (!v->p_frame_skipped && s->pict_type != AV_PICTURE_TYPE_I && !v->cbpcy_vlc) {
5715 av_log(v->s.avctx, AV_LOG_ERROR, "missing cbpcy_vlc\n");
5718 ff_vc1_decode_blocks(v);
5720 s->gb = slices[i].gb;
5722 if (v->field_mode) {
5723 v->second_field = 0;
5724 if (s->pict_type == AV_PICTURE_TYPE_B) {
5725 memcpy(v->mv_f_base, v->mv_f_next_base,
5726 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5728 s->current_picture.f.linesize[0] >>= 1;
5729 s->current_picture.f.linesize[1] >>= 1;
5730 s->current_picture.f.linesize[2] >>= 1;
5732 s->uvlinesize >>= 1;
5734 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5735 get_bits_count(&s->gb), s->gb.size_in_bits);
5736 // if (get_bits_count(&s->gb) > buf_size * 8)
5738 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5744 ff_MPV_frame_end(s);
5746 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5748 avctx->width = avctx->coded_width = v->output_width;
5749 avctx->height = avctx->coded_height = v->output_height;
5750 if (avctx->skip_frame >= AVDISCARD_NONREF)
5752 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5753 if (vc1_decode_sprites(v, &s->gb))
5756 *pict = v->sprite_output_frame;
5759 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5760 *pict = s->current_picture_ptr->f;
5761 } else if (s->last_picture_ptr != NULL) {
5762 *pict = s->last_picture_ptr->f;
5764 if (s->last_picture_ptr || s->low_delay) {
5766 ff_print_debug_info(s, pict);
5772 for (i = 0; i < n_slices; i++)
5773 av_free(slices[i].buf);
5779 for (i = 0; i < n_slices; i++)
5780 av_free(slices[i].buf);
5786 static const AVProfile profiles[] = {
5787 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5788 { FF_PROFILE_VC1_MAIN, "Main" },
5789 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5790 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5791 { FF_PROFILE_UNKNOWN },
5794 AVCodec ff_vc1_decoder = {
5796 .type = AVMEDIA_TYPE_VIDEO,
5797 .id = AV_CODEC_ID_VC1,
5798 .priv_data_size = sizeof(VC1Context),
5799 .init = vc1_decode_init,
5800 .close = ff_vc1_decode_end,
5801 .decode = vc1_decode_frame,
5802 .flush = ff_mpeg_flush,
5803 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5804 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5805 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5806 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5809 #if CONFIG_WMV3_DECODER
5810 AVCodec ff_wmv3_decoder = {
5812 .type = AVMEDIA_TYPE_VIDEO,
5813 .id = AV_CODEC_ID_WMV3,
5814 .priv_data_size = sizeof(VC1Context),
5815 .init = vc1_decode_init,
5816 .close = ff_vc1_decode_end,
5817 .decode = vc1_decode_frame,
5818 .flush = ff_mpeg_flush,
5819 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5820 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5821 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5822 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5826 #if CONFIG_WMV3_VDPAU_DECODER
5827 AVCodec ff_wmv3_vdpau_decoder = {
5828 .name = "wmv3_vdpau",
5829 .type = AVMEDIA_TYPE_VIDEO,
5830 .id = AV_CODEC_ID_WMV3,
5831 .priv_data_size = sizeof(VC1Context),
5832 .init = vc1_decode_init,
5833 .close = ff_vc1_decode_end,
5834 .decode = vc1_decode_frame,
5835 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5836 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5837 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5838 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5842 #if CONFIG_VC1_VDPAU_DECODER
5843 AVCodec ff_vc1_vdpau_decoder = {
5844 .name = "vc1_vdpau",
5845 .type = AVMEDIA_TYPE_VIDEO,
5846 .id = AV_CODEC_ID_VC1,
5847 .priv_data_size = sizeof(VC1Context),
5848 .init = vc1_decode_init,
5849 .close = ff_vc1_decode_end,
5850 .decode = vc1_decode_frame,
5851 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5852 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5853 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5854 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5858 #if CONFIG_WMV3IMAGE_DECODER
5859 AVCodec ff_wmv3image_decoder = {
5860 .name = "wmv3image",
5861 .type = AVMEDIA_TYPE_VIDEO,
5862 .id = AV_CODEC_ID_WMV3IMAGE,
5863 .priv_data_size = sizeof(VC1Context),
5864 .init = vc1_decode_init,
5865 .close = ff_vc1_decode_end,
5866 .decode = vc1_decode_frame,
5867 .capabilities = CODEC_CAP_DR1,
5868 .flush = vc1_sprite_flush,
5869 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5870 .pix_fmts = ff_pixfmt_list_420
5874 #if CONFIG_VC1IMAGE_DECODER
5875 AVCodec ff_vc1image_decoder = {
5877 .type = AVMEDIA_TYPE_VIDEO,
5878 .id = AV_CODEC_ID_VC1IMAGE,
5879 .priv_data_size = sizeof(VC1Context),
5880 .init = vc1_decode_init,
5881 .close = ff_vc1_decode_end,
5882 .decode = vc1_decode_frame,
5883 .capabilities = CODEC_CAP_DR1,
5884 .flush = vc1_sprite_flush,
5885 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5886 .pix_fmts = ff_pixfmt_list_420