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];
398 src_x = s->mb_x * 16 + (mx >> 2);
399 src_y = s->mb_y * 16 + (my >> 2);
400 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
401 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
403 if (v->profile != PROFILE_ADVANCED) {
404 src_x = av_clip( src_x, -16, s->mb_width * 16);
405 src_y = av_clip( src_y, -16, s->mb_height * 16);
406 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
407 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
409 src_x = av_clip( src_x, -17, s->avctx->coded_width);
410 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
411 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
412 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
415 srcY += src_y * s->linesize + src_x;
416 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
417 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
419 if (v->field_mode && v->ref_field_type[dir]) {
420 srcY += s->current_picture_ptr->f.linesize[0];
421 srcU += s->current_picture_ptr->f.linesize[1];
422 srcV += s->current_picture_ptr->f.linesize[2];
425 /* for grayscale we should not try to read from unknown area */
426 if (s->flags & CODEC_FLAG_GRAY) {
427 srcU = s->edge_emu_buffer + 18 * s->linesize;
428 srcV = s->edge_emu_buffer + 18 * s->linesize;
431 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
432 || s->h_edge_pos < 22 || v_edge_pos < 22
433 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
434 || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
435 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
437 srcY -= s->mspel * (1 + s->linesize);
438 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
439 17 + s->mspel * 2, 17 + s->mspel * 2,
440 src_x - s->mspel, src_y - s->mspel,
441 s->h_edge_pos, v_edge_pos);
442 srcY = s->edge_emu_buffer;
443 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
444 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
445 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
446 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
449 /* if we deal with range reduction we need to scale source blocks */
450 if (v->rangeredfrm) {
455 for (j = 0; j < 17 + s->mspel * 2; j++) {
456 for (i = 0; i < 17 + s->mspel * 2; i++)
457 src[i] = ((src[i] - 128) >> 1) + 128;
462 for (j = 0; j < 9; j++) {
463 for (i = 0; i < 9; i++) {
464 src[i] = ((src[i] - 128) >> 1) + 128;
465 src2[i] = ((src2[i] - 128) >> 1) + 128;
467 src += s->uvlinesize;
468 src2 += s->uvlinesize;
471 /* if we deal with intensity compensation we need to scale source blocks */
472 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
477 for (j = 0; j < 17 + s->mspel * 2; j++) {
478 for (i = 0; i < 17 + s->mspel * 2; i++)
479 src[i] = v->luty[src[i]];
484 for (j = 0; j < 9; j++) {
485 for (i = 0; i < 9; i++) {
486 src[i] = v->lutuv[src[i]];
487 src2[i] = v->lutuv[src2[i]];
489 src += s->uvlinesize;
490 src2 += s->uvlinesize;
493 srcY += s->mspel * (1 + s->linesize);
496 if (v->field_mode && v->second_field) {
497 off = s->current_picture_ptr->f.linesize[0];
498 off_uv = s->current_picture_ptr->f.linesize[1];
504 dxy = ((my & 3) << 2) | (mx & 3);
505 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
506 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
507 srcY += s->linesize * 8;
508 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
509 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
510 } else { // hpel mc - always used for luma
511 dxy = (my & 2) | ((mx & 2) >> 1);
513 dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
515 dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
518 if (s->flags & CODEC_FLAG_GRAY) return;
519 /* Chroma MC always uses qpel bilinear */
520 uvmx = (uvmx & 3) << 1;
521 uvmy = (uvmy & 3) << 1;
523 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
524 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
526 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
527 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
531 static inline int median4(int a, int b, int c, int d)
534 if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
535 else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
537 if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
538 else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
542 /** Do motion compensation for 4-MV macroblock - luminance block
544 static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
546 MpegEncContext *s = &v->s;
547 DSPContext *dsp = &v->s.dsp;
549 int dxy, mx, my, src_x, src_y;
551 int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
552 int v_edge_pos = s->v_edge_pos >> v->field_mode;
554 if ((!v->field_mode ||
555 (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
556 !v->s.last_picture.f.data[0])
559 mx = s->mv[dir][n][0];
560 my = s->mv[dir][n][1];
564 if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
565 srcY = s->current_picture.f.data[0];
567 srcY = s->last_picture.f.data[0];
569 srcY = s->last_picture.f.data[0];
571 srcY = s->next_picture.f.data[0];
574 if (v->cur_field_type != v->ref_field_type[dir])
575 my = my - 2 + 4 * v->cur_field_type;
578 if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
579 int same_count = 0, opp_count = 0, k;
580 int chosen_mv[2][4][2], f;
582 for (k = 0; k < 4; k++) {
583 f = v->mv_f[0][s->block_index[k] + v->blocks_off];
584 chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
585 chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
589 f = opp_count > same_count;
590 switch (f ? opp_count : same_count) {
592 tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
593 chosen_mv[f][2][0], chosen_mv[f][3][0]);
594 ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
595 chosen_mv[f][2][1], chosen_mv[f][3][1]);
598 tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
599 ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
602 tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
603 ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
606 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
607 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
608 for (k = 0; k < 4; k++)
609 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
612 if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
614 int width = s->avctx->coded_width;
615 int height = s->avctx->coded_height >> 1;
616 qx = (s->mb_x * 16) + (mx >> 2);
617 qy = (s->mb_y * 8) + (my >> 3);
622 mx -= 4 * (qx - width);
625 else if (qy > height + 1)
626 my -= 8 * (qy - height - 1);
629 if ((v->fcm == ILACE_FRAME) && fieldmv)
630 off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
632 off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
633 if (v->field_mode && v->second_field)
634 off += s->current_picture_ptr->f.linesize[0];
636 src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
638 src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
640 src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
642 if (v->profile != PROFILE_ADVANCED) {
643 src_x = av_clip(src_x, -16, s->mb_width * 16);
644 src_y = av_clip(src_y, -16, s->mb_height * 16);
646 src_x = av_clip(src_x, -17, s->avctx->coded_width);
647 if (v->fcm == ILACE_FRAME) {
649 src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
651 src_y = av_clip(src_y, -18, s->avctx->coded_height);
653 src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
657 srcY += src_y * s->linesize + src_x;
658 if (v->field_mode && v->ref_field_type[dir])
659 srcY += s->current_picture_ptr->f.linesize[0];
661 if (fieldmv && !(src_y & 1))
663 if (fieldmv && (src_y & 1) && src_y < 4)
665 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
666 || s->h_edge_pos < 13 || v_edge_pos < 23
667 || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
668 || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
669 srcY -= s->mspel * (1 + (s->linesize << fieldmv));
670 /* check emulate edge stride and offset */
671 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
672 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
673 src_x - s->mspel, src_y - (s->mspel << fieldmv),
674 s->h_edge_pos, v_edge_pos);
675 srcY = s->edge_emu_buffer;
676 /* if we deal with range reduction we need to scale source blocks */
677 if (v->rangeredfrm) {
682 for (j = 0; j < 9 + s->mspel * 2; j++) {
683 for (i = 0; i < 9 + s->mspel * 2; i++)
684 src[i] = ((src[i] - 128) >> 1) + 128;
685 src += s->linesize << fieldmv;
688 /* if we deal with intensity compensation we need to scale source blocks */
689 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
694 for (j = 0; j < 9 + s->mspel * 2; j++) {
695 for (i = 0; i < 9 + s->mspel * 2; i++)
696 src[i] = v->luty[src[i]];
697 src += s->linesize << fieldmv;
700 srcY += s->mspel * (1 + (s->linesize << fieldmv));
704 dxy = ((my & 3) << 2) | (mx & 3);
705 v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
706 } else { // hpel mc - always used for luma
707 dxy = (my & 2) | ((mx & 2) >> 1);
709 dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
711 dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
715 static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
718 static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
720 idx = ((a[3] != flag) << 3)
721 | ((a[2] != flag) << 2)
722 | ((a[1] != flag) << 1)
725 *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
726 *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
728 } else if (count[idx] == 1) {
731 *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
732 *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
735 *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
736 *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
739 *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
740 *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
743 *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
744 *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
747 } else if (count[idx] == 2) {
749 for (i = 0; i < 3; i++)
754 for (i = t1 + 1; i < 4; i++)
759 *tx = (mvx[t1] + mvx[t2]) / 2;
760 *ty = (mvy[t1] + mvy[t2]) / 2;
768 /** Do motion compensation for 4-MV macroblock - both chroma blocks
770 static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
772 MpegEncContext *s = &v->s;
773 DSPContext *dsp = &v->s.dsp;
774 uint8_t *srcU, *srcV;
775 int uvmx, uvmy, uvsrc_x, uvsrc_y;
776 int k, tx = 0, ty = 0;
777 int mvx[4], mvy[4], intra[4], mv_f[4];
779 int chroma_ref_type = v->cur_field_type, off = 0;
780 int v_edge_pos = s->v_edge_pos >> v->field_mode;
782 if (!v->field_mode && !v->s.last_picture.f.data[0])
784 if (s->flags & CODEC_FLAG_GRAY)
787 for (k = 0; k < 4; k++) {
788 mvx[k] = s->mv[dir][k][0];
789 mvy[k] = s->mv[dir][k][1];
790 intra[k] = v->mb_type[0][s->block_index[k]];
792 mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
795 /* calculate chroma MV vector from four luma MVs */
796 if (!v->field_mode || (v->field_mode && !v->numref)) {
797 valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
798 chroma_ref_type = v->reffield;
800 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
801 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
802 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
803 return; //no need to do MC for intra blocks
807 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
809 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
811 chroma_ref_type = !v->cur_field_type;
813 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
815 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
816 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
817 uvmx = (tx + ((tx & 3) == 3)) >> 1;
818 uvmy = (ty + ((ty & 3) == 3)) >> 1;
820 v->luma_mv[s->mb_x][0] = uvmx;
821 v->luma_mv[s->mb_x][1] = uvmy;
824 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
825 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
827 // Field conversion bias
828 if (v->cur_field_type != chroma_ref_type)
829 uvmy += 2 - 4 * chroma_ref_type;
831 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
832 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
834 if (v->profile != PROFILE_ADVANCED) {
835 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
836 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
838 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
839 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
844 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
845 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
846 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
848 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
849 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
852 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
853 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
856 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
857 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
861 if (chroma_ref_type) {
862 srcU += s->current_picture_ptr->f.linesize[1];
863 srcV += s->current_picture_ptr->f.linesize[2];
865 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
868 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
869 || s->h_edge_pos < 18 || v_edge_pos < 18
870 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
871 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
872 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
873 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
874 s->h_edge_pos >> 1, v_edge_pos >> 1);
875 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
876 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
877 s->h_edge_pos >> 1, v_edge_pos >> 1);
878 srcU = s->edge_emu_buffer;
879 srcV = s->edge_emu_buffer + 16;
881 /* if we deal with range reduction we need to scale source blocks */
882 if (v->rangeredfrm) {
888 for (j = 0; j < 9; j++) {
889 for (i = 0; i < 9; i++) {
890 src[i] = ((src[i] - 128) >> 1) + 128;
891 src2[i] = ((src2[i] - 128) >> 1) + 128;
893 src += s->uvlinesize;
894 src2 += s->uvlinesize;
897 /* if we deal with intensity compensation we need to scale source blocks */
898 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
904 for (j = 0; j < 9; j++) {
905 for (i = 0; i < 9; i++) {
906 src[i] = v->lutuv[src[i]];
907 src2[i] = v->lutuv[src2[i]];
909 src += s->uvlinesize;
910 src2 += s->uvlinesize;
915 /* Chroma MC always uses qpel bilinear */
916 uvmx = (uvmx & 3) << 1;
917 uvmy = (uvmy & 3) << 1;
919 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
920 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
922 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
923 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
927 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
929 static void vc1_mc_4mv_chroma4(VC1Context *v)
931 MpegEncContext *s = &v->s;
932 DSPContext *dsp = &v->s.dsp;
933 uint8_t *srcU, *srcV;
934 int uvsrc_x, uvsrc_y;
935 int uvmx_field[4], uvmy_field[4];
937 int fieldmv = v->blk_mv_type[s->block_index[0]];
938 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
939 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
940 int v_edge_pos = s->v_edge_pos >> 1;
942 if (!v->s.last_picture.f.data[0])
944 if (s->flags & CODEC_FLAG_GRAY)
947 for (i = 0; i < 4; i++) {
949 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
952 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
954 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
957 for (i = 0; i < 4; i++) {
958 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
959 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
960 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
961 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
962 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
963 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
964 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
965 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
966 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
967 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
969 if (fieldmv && !(uvsrc_y & 1))
971 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
973 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
974 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
975 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
976 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
977 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
978 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
979 s->h_edge_pos >> 1, v_edge_pos);
980 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
981 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
982 s->h_edge_pos >> 1, v_edge_pos);
983 srcU = s->edge_emu_buffer;
984 srcV = s->edge_emu_buffer + 16;
986 /* if we deal with intensity compensation we need to scale source blocks */
987 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
993 for (j = 0; j < 5; j++) {
994 for (i = 0; i < 5; i++) {
995 src[i] = v->lutuv[src[i]];
996 src2[i] = v->lutuv[src2[i]];
998 src += s->uvlinesize << 1;
999 src2 += s->uvlinesize << 1;
1004 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1005 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1007 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]);
1008 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]);
1013 /***********************************************************************/
1015 * @name VC-1 Block-level functions
1016 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1022 * @brief Get macroblock-level quantizer scale
1024 #define GET_MQUANT() \
1025 if (v->dquantfrm) { \
1027 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1028 if (v->dqbilevel) { \
1029 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1031 mqdiff = get_bits(gb, 3); \
1033 mquant = v->pq + mqdiff; \
1035 mquant = get_bits(gb, 5); \
1038 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1039 edges = 1 << v->dqsbedge; \
1040 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1041 edges = (3 << v->dqsbedge) % 15; \
1042 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1044 if ((edges&1) && !s->mb_x) \
1045 mquant = v->altpq; \
1046 if ((edges&2) && s->first_slice_line) \
1047 mquant = v->altpq; \
1048 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1049 mquant = v->altpq; \
1050 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1051 mquant = v->altpq; \
1052 if (!mquant || mquant > 31) { \
1053 av_log(v->s.avctx, AV_LOG_ERROR, \
1054 "Overriding invalid mquant %d\n", mquant); \
1060 * @def GET_MVDATA(_dmv_x, _dmv_y)
1061 * @brief Get MV differentials
1062 * @see MVDATA decoding from 8.3.5.2, p(1)20
1063 * @param _dmv_x Horizontal differential for decoded MV
1064 * @param _dmv_y Vertical differential for decoded MV
1066 #define GET_MVDATA(_dmv_x, _dmv_y) \
1067 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1068 VC1_MV_DIFF_VLC_BITS, 2); \
1070 mb_has_coeffs = 1; \
1073 mb_has_coeffs = 0; \
1076 _dmv_x = _dmv_y = 0; \
1077 } else if (index == 35) { \
1078 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1079 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1080 } else if (index == 36) { \
1085 index1 = index % 6; \
1086 if (!s->quarter_sample && index1 == 5) val = 1; \
1088 if (size_table[index1] - val > 0) \
1089 val = get_bits(gb, size_table[index1] - val); \
1091 sign = 0 - (val&1); \
1092 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1094 index1 = index / 6; \
1095 if (!s->quarter_sample && index1 == 5) val = 1; \
1097 if (size_table[index1] - val > 0) \
1098 val = get_bits(gb, size_table[index1] - val); \
1100 sign = 0 - (val & 1); \
1101 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1104 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1105 int *dmv_y, int *pred_flag)
1108 int extend_x = 0, extend_y = 0;
1109 GetBitContext *gb = &v->s.gb;
1112 const int* offs_tab;
1115 bits = VC1_2REF_MVDATA_VLC_BITS;
1118 bits = VC1_1REF_MVDATA_VLC_BITS;
1121 switch (v->dmvrange) {
1129 extend_x = extend_y = 1;
1132 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1134 *dmv_x = get_bits(gb, v->k_x);
1135 *dmv_y = get_bits(gb, v->k_y);
1137 *pred_flag = *dmv_y & 1;
1138 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1142 av_assert0(index < esc);
1144 offs_tab = offset_table2;
1146 offs_tab = offset_table1;
1147 index1 = (index + 1) % 9;
1149 val = get_bits(gb, index1 + extend_x);
1150 sign = 0 -(val & 1);
1151 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1155 offs_tab = offset_table2;
1157 offs_tab = offset_table1;
1158 index1 = (index + 1) / 9;
1159 if (index1 > v->numref) {
1160 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1161 sign = 0 - (val & 1);
1162 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1166 *pred_flag = index1 & 1;
1170 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1172 int scaledvalue, refdist;
1173 int scalesame1, scalesame2;
1174 int scalezone1_x, zone1offset_x;
1175 int table_index = dir ^ v->second_field;
1177 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1178 refdist = v->refdist;
1180 refdist = dir ? v->brfd : v->frfd;
1183 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1184 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1185 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1186 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1191 if (FFABS(n) < scalezone1_x)
1192 scaledvalue = (n * scalesame1) >> 8;
1195 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1197 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1200 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1203 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1205 int scaledvalue, refdist;
1206 int scalesame1, scalesame2;
1207 int scalezone1_y, zone1offset_y;
1208 int table_index = dir ^ v->second_field;
1210 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1211 refdist = v->refdist;
1213 refdist = dir ? v->brfd : v->frfd;
1216 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1217 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1218 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1219 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1224 if (FFABS(n) < scalezone1_y)
1225 scaledvalue = (n * scalesame1) >> 8;
1228 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1230 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1234 if (v->cur_field_type && !v->ref_field_type[dir])
1235 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1237 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1240 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1242 int scalezone1_x, zone1offset_x;
1243 int scaleopp1, scaleopp2, brfd;
1246 brfd = FFMIN(v->brfd, 3);
1247 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1248 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1249 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1250 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1255 if (FFABS(n) < scalezone1_x)
1256 scaledvalue = (n * scaleopp1) >> 8;
1259 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1261 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1264 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1267 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1269 int scalezone1_y, zone1offset_y;
1270 int scaleopp1, scaleopp2, brfd;
1273 brfd = FFMIN(v->brfd, 3);
1274 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1275 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1276 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1277 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1282 if (FFABS(n) < scalezone1_y)
1283 scaledvalue = (n * scaleopp1) >> 8;
1286 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1288 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1291 if (v->cur_field_type && !v->ref_field_type[dir]) {
1292 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1294 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1298 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1301 int brfd, scalesame;
1302 int hpel = 1 - v->s.quarter_sample;
1305 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1307 n = scaleforsame_y(v, i, n, dir) << hpel;
1309 n = scaleforsame_x(v, n, dir) << hpel;
1312 brfd = FFMIN(v->brfd, 3);
1313 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1315 n = (n * scalesame >> 8) << hpel;
1319 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1322 int refdist, scaleopp;
1323 int hpel = 1 - v->s.quarter_sample;
1326 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1328 n = scaleforopp_y(v, n, dir) << hpel;
1330 n = scaleforopp_x(v, n) << hpel;
1333 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1334 refdist = FFMIN(v->refdist, 3);
1336 refdist = dir ? v->brfd : v->frfd;
1337 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1339 n = (n * scaleopp >> 8) << hpel;
1343 /** Predict and set motion vector
1345 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1346 int mv1, int r_x, int r_y, uint8_t* is_intra,
1347 int pred_flag, int dir)
1349 MpegEncContext *s = &v->s;
1350 int xy, wrap, off = 0;
1354 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1355 int opposite, a_f, b_f, c_f;
1356 int16_t field_predA[2];
1357 int16_t field_predB[2];
1358 int16_t field_predC[2];
1359 int a_valid, b_valid, c_valid;
1360 int hybridmv_thresh, y_bias = 0;
1362 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1363 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1367 /* scale MV difference to be quad-pel */
1368 dmv_x <<= 1 - s->quarter_sample;
1369 dmv_y <<= 1 - s->quarter_sample;
1371 wrap = s->b8_stride;
1372 xy = s->block_index[n];
1375 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1376 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1377 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1378 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1379 if (mv1) { /* duplicate motion data for 1-MV block */
1380 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1381 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1382 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1383 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1384 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1385 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1386 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1387 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1388 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1389 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1390 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1391 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1392 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1397 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1398 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1400 if (v->field_mode && mixedmv_pic)
1401 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1403 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1405 //in 4-MV mode different blocks have different B predictor position
1408 off = (s->mb_x > 0) ? -1 : 1;
1411 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1420 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1422 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1423 b_valid = a_valid && (s->mb_width > 1);
1424 c_valid = s->mb_x || (n == 1 || n == 3);
1425 if (v->field_mode) {
1426 a_valid = a_valid && !is_intra[xy - wrap];
1427 b_valid = b_valid && !is_intra[xy - wrap + off];
1428 c_valid = c_valid && !is_intra[xy - 1];
1432 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1433 num_oppfield += a_f;
1434 num_samefield += 1 - a_f;
1435 field_predA[0] = A[0];
1436 field_predA[1] = A[1];
1438 field_predA[0] = field_predA[1] = 0;
1442 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1443 num_oppfield += b_f;
1444 num_samefield += 1 - b_f;
1445 field_predB[0] = B[0];
1446 field_predB[1] = B[1];
1448 field_predB[0] = field_predB[1] = 0;
1452 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1453 num_oppfield += c_f;
1454 num_samefield += 1 - c_f;
1455 field_predC[0] = C[0];
1456 field_predC[1] = C[1];
1458 field_predC[0] = field_predC[1] = 0;
1462 if (v->field_mode) {
1463 if (num_samefield <= num_oppfield)
1464 opposite = 1 - pred_flag;
1466 opposite = pred_flag;
1470 if (a_valid && !a_f) {
1471 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1472 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1474 if (b_valid && !b_f) {
1475 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1476 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1478 if (c_valid && !c_f) {
1479 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1480 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1482 v->mv_f[dir][xy + v->blocks_off] = 1;
1483 v->ref_field_type[dir] = !v->cur_field_type;
1485 if (a_valid && a_f) {
1486 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1487 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1489 if (b_valid && b_f) {
1490 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1491 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1493 if (c_valid && c_f) {
1494 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1495 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1497 v->mv_f[dir][xy + v->blocks_off] = 0;
1498 v->ref_field_type[dir] = v->cur_field_type;
1502 px = field_predA[0];
1503 py = field_predA[1];
1504 } else if (c_valid) {
1505 px = field_predC[0];
1506 py = field_predC[1];
1507 } else if (b_valid) {
1508 px = field_predB[0];
1509 py = field_predB[1];
1515 if (num_samefield + num_oppfield > 1) {
1516 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1517 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1520 /* Pullback MV as specified in 8.3.5.3.4 */
1521 if (!v->field_mode) {
1523 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1524 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1525 X = (s->mb_width << 6) - 4;
1526 Y = (s->mb_height << 6) - 4;
1528 if (qx + px < -60) px = -60 - qx;
1529 if (qy + py < -60) py = -60 - qy;
1531 if (qx + px < -28) px = -28 - qx;
1532 if (qy + py < -28) py = -28 - qy;
1534 if (qx + px > X) px = X - qx;
1535 if (qy + py > Y) py = Y - qy;
1538 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1539 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1540 hybridmv_thresh = 32;
1541 if (a_valid && c_valid) {
1542 if (is_intra[xy - wrap])
1543 sum = FFABS(px) + FFABS(py);
1545 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1546 if (sum > hybridmv_thresh) {
1547 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1548 px = field_predA[0];
1549 py = field_predA[1];
1551 px = field_predC[0];
1552 py = field_predC[1];
1555 if (is_intra[xy - 1])
1556 sum = FFABS(px) + FFABS(py);
1558 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1559 if (sum > hybridmv_thresh) {
1560 if (get_bits1(&s->gb)) {
1561 px = field_predA[0];
1562 py = field_predA[1];
1564 px = field_predC[0];
1565 py = field_predC[1];
1572 if (v->field_mode && !s->quarter_sample) {
1576 if (v->field_mode && v->numref)
1578 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1580 /* store MV using signed modulus of MV range defined in 4.11 */
1581 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;
1582 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;
1583 if (mv1) { /* duplicate motion data for 1-MV block */
1584 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];
1585 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];
1586 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];
1587 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];
1588 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];
1589 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];
1590 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1591 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];
1595 /** Predict and set motion vector for interlaced frame picture MBs
1597 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1598 int mvn, int r_x, int r_y, uint8_t* is_intra)
1600 MpegEncContext *s = &v->s;
1601 int xy, wrap, off = 0;
1602 int A[2], B[2], C[2];
1604 int a_valid = 0, b_valid = 0, c_valid = 0;
1605 int field_a, field_b, field_c; // 0: same, 1: opposit
1606 int total_valid, num_samefield, num_oppfield;
1607 int pos_c, pos_b, n_adj;
1609 wrap = s->b8_stride;
1610 xy = s->block_index[n];
1613 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1614 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1615 s->current_picture.f.motion_val[1][xy][0] = 0;
1616 s->current_picture.f.motion_val[1][xy][1] = 0;
1617 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1618 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1619 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1620 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1621 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1622 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1623 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1624 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1625 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1626 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1627 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1628 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1629 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1630 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1635 off = ((n == 0) || (n == 1)) ? 1 : -1;
1637 if (s->mb_x || (n == 1) || (n == 3)) {
1638 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1639 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1640 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1641 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1643 } else { // current block has frame mv and cand. has field MV (so average)
1644 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1645 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1646 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1647 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1650 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1656 /* Predict B and C */
1657 B[0] = B[1] = C[0] = C[1] = 0;
1658 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1659 if (!s->first_slice_line) {
1660 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1663 pos_b = s->block_index[n_adj] - 2 * wrap;
1664 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1665 n_adj = (n & 2) | (n & 1);
1667 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1668 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1669 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1670 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1671 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1674 if (s->mb_width > 1) {
1675 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1678 pos_c = s->block_index[2] - 2 * wrap + 2;
1679 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1682 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1683 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1684 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1685 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1686 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1688 if (s->mb_x == s->mb_width - 1) {
1689 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1692 pos_c = s->block_index[3] - 2 * wrap - 2;
1693 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1696 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1697 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1698 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1699 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1700 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1709 pos_b = s->block_index[1];
1711 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1712 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1713 pos_c = s->block_index[0];
1715 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1716 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1719 total_valid = a_valid + b_valid + c_valid;
1720 // check if predictor A is out of bounds
1721 if (!s->mb_x && !(n == 1 || n == 3)) {
1724 // check if predictor B is out of bounds
1725 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1726 B[0] = B[1] = C[0] = C[1] = 0;
1728 if (!v->blk_mv_type[xy]) {
1729 if (s->mb_width == 1) {
1733 if (total_valid >= 2) {
1734 px = mid_pred(A[0], B[0], C[0]);
1735 py = mid_pred(A[1], B[1], C[1]);
1736 } else if (total_valid) {
1737 if (a_valid) { px = A[0]; py = A[1]; }
1738 if (b_valid) { px = B[0]; py = B[1]; }
1739 if (c_valid) { px = C[0]; py = C[1]; }
1745 field_a = (A[1] & 4) ? 1 : 0;
1749 field_b = (B[1] & 4) ? 1 : 0;
1753 field_c = (C[1] & 4) ? 1 : 0;
1757 num_oppfield = field_a + field_b + field_c;
1758 num_samefield = total_valid - num_oppfield;
1759 if (total_valid == 3) {
1760 if ((num_samefield == 3) || (num_oppfield == 3)) {
1761 px = mid_pred(A[0], B[0], C[0]);
1762 py = mid_pred(A[1], B[1], C[1]);
1763 } else if (num_samefield >= num_oppfield) {
1764 /* take one MV from same field set depending on priority
1765 the check for B may not be necessary */
1766 px = !field_a ? A[0] : B[0];
1767 py = !field_a ? A[1] : B[1];
1769 px = field_a ? A[0] : B[0];
1770 py = field_a ? A[1] : B[1];
1772 } else if (total_valid == 2) {
1773 if (num_samefield >= num_oppfield) {
1774 if (!field_a && a_valid) {
1777 } else if (!field_b && b_valid) {
1780 } else if (c_valid) {
1785 if (field_a && a_valid) {
1788 } else if (field_b && b_valid) {
1791 } else if (c_valid) {
1796 } else if (total_valid == 1) {
1797 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1798 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1803 /* store MV using signed modulus of MV range defined in 4.11 */
1804 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;
1805 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;
1806 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1807 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1808 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1809 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1810 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1811 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1812 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1813 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1814 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1815 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1816 s->mv[0][n + 1][0] = s->mv[0][n][0];
1817 s->mv[0][n + 1][1] = s->mv[0][n][1];
1821 /** Motion compensation for direct or interpolated blocks in B-frames
1823 static void vc1_interp_mc(VC1Context *v)
1825 MpegEncContext *s = &v->s;
1826 DSPContext *dsp = &v->s.dsp;
1827 uint8_t *srcY, *srcU, *srcV;
1828 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1830 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1832 if (!v->field_mode && !v->s.next_picture.f.data[0])
1835 mx = s->mv[1][0][0];
1836 my = s->mv[1][0][1];
1837 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1838 uvmy = (my + ((my & 3) == 3)) >> 1;
1839 if (v->field_mode) {
1840 if (v->cur_field_type != v->ref_field_type[1])
1841 my = my - 2 + 4 * v->cur_field_type;
1842 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1845 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1846 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1848 srcY = s->next_picture.f.data[0];
1849 srcU = s->next_picture.f.data[1];
1850 srcV = s->next_picture.f.data[2];
1852 src_x = s->mb_x * 16 + (mx >> 2);
1853 src_y = s->mb_y * 16 + (my >> 2);
1854 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1855 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1857 if (v->profile != PROFILE_ADVANCED) {
1858 src_x = av_clip( src_x, -16, s->mb_width * 16);
1859 src_y = av_clip( src_y, -16, s->mb_height * 16);
1860 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1861 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1863 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1864 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1865 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1866 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1869 srcY += src_y * s->linesize + src_x;
1870 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1871 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1873 if (v->field_mode && v->ref_field_type[1]) {
1874 srcY += s->current_picture_ptr->f.linesize[0];
1875 srcU += s->current_picture_ptr->f.linesize[1];
1876 srcV += s->current_picture_ptr->f.linesize[2];
1879 /* for grayscale we should not try to read from unknown area */
1880 if (s->flags & CODEC_FLAG_GRAY) {
1881 srcU = s->edge_emu_buffer + 18 * s->linesize;
1882 srcV = s->edge_emu_buffer + 18 * s->linesize;
1885 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1886 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1887 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1888 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1890 srcY -= s->mspel * (1 + s->linesize);
1891 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1892 17 + s->mspel * 2, 17 + s->mspel * 2,
1893 src_x - s->mspel, src_y - s->mspel,
1894 s->h_edge_pos, v_edge_pos);
1895 srcY = s->edge_emu_buffer;
1896 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1897 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1898 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1899 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1902 /* if we deal with range reduction we need to scale source blocks */
1903 if (v->rangeredfrm) {
1905 uint8_t *src, *src2;
1908 for (j = 0; j < 17 + s->mspel * 2; j++) {
1909 for (i = 0; i < 17 + s->mspel * 2; i++)
1910 src[i] = ((src[i] - 128) >> 1) + 128;
1915 for (j = 0; j < 9; j++) {
1916 for (i = 0; i < 9; i++) {
1917 src[i] = ((src[i] - 128) >> 1) + 128;
1918 src2[i] = ((src2[i] - 128) >> 1) + 128;
1920 src += s->uvlinesize;
1921 src2 += s->uvlinesize;
1924 srcY += s->mspel * (1 + s->linesize);
1927 if (v->field_mode && v->second_field) {
1928 off = s->current_picture_ptr->f.linesize[0];
1929 off_uv = s->current_picture_ptr->f.linesize[1];
1936 dxy = ((my & 3) << 2) | (mx & 3);
1937 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1938 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1939 srcY += s->linesize * 8;
1940 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1941 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1943 dxy = (my & 2) | ((mx & 2) >> 1);
1946 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1948 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1951 if (s->flags & CODEC_FLAG_GRAY) return;
1952 /* Chroma MC always uses qpel blilinear */
1953 uvmx = (uvmx & 3) << 1;
1954 uvmy = (uvmy & 3) << 1;
1956 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1957 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1959 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1960 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1964 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1968 #if B_FRACTION_DEN==256
1972 return 2 * ((value * n + 255) >> 9);
1973 return (value * n + 128) >> 8;
1976 n -= B_FRACTION_DEN;
1978 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1979 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1983 /** Reconstruct motion vector for B-frame and do motion compensation
1985 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
1986 int direct, int mode)
1989 v->mv_mode2 = v->mv_mode;
1990 v->mv_mode = MV_PMODE_INTENSITY_COMP;
1996 v->mv_mode = v->mv_mode2;
1999 if (mode == BMV_TYPE_INTERPOLATED) {
2003 v->mv_mode = v->mv_mode2;
2007 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2008 v->mv_mode = v->mv_mode2;
2009 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2011 v->mv_mode = v->mv_mode2;
2014 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2015 int direct, int mvtype)
2017 MpegEncContext *s = &v->s;
2018 int xy, wrap, off = 0;
2023 const uint8_t *is_intra = v->mb_type[0];
2027 /* scale MV difference to be quad-pel */
2028 dmv_x[0] <<= 1 - s->quarter_sample;
2029 dmv_y[0] <<= 1 - s->quarter_sample;
2030 dmv_x[1] <<= 1 - s->quarter_sample;
2031 dmv_y[1] <<= 1 - s->quarter_sample;
2033 wrap = s->b8_stride;
2034 xy = s->block_index[0];
2037 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2038 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2039 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2040 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2043 if (!v->field_mode) {
2044 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2045 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2046 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2047 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2049 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2050 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));
2051 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));
2052 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));
2053 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));
2056 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2057 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2058 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2059 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2063 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2064 C = s->current_picture.f.motion_val[0][xy - 2];
2065 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2066 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2067 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2069 if (!s->mb_x) C[0] = C[1] = 0;
2070 if (!s->first_slice_line) { // predictor A is not out of bounds
2071 if (s->mb_width == 1) {
2075 px = mid_pred(A[0], B[0], C[0]);
2076 py = mid_pred(A[1], B[1], C[1]);
2078 } else if (s->mb_x) { // predictor C is not out of bounds
2084 /* Pullback MV as specified in 8.3.5.3.4 */
2087 if (v->profile < PROFILE_ADVANCED) {
2088 qx = (s->mb_x << 5);
2089 qy = (s->mb_y << 5);
2090 X = (s->mb_width << 5) - 4;
2091 Y = (s->mb_height << 5) - 4;
2092 if (qx + px < -28) px = -28 - qx;
2093 if (qy + py < -28) py = -28 - qy;
2094 if (qx + px > X) px = X - qx;
2095 if (qy + py > Y) py = Y - qy;
2097 qx = (s->mb_x << 6);
2098 qy = (s->mb_y << 6);
2099 X = (s->mb_width << 6) - 4;
2100 Y = (s->mb_height << 6) - 4;
2101 if (qx + px < -60) px = -60 - qx;
2102 if (qy + py < -60) py = -60 - qy;
2103 if (qx + px > X) px = X - qx;
2104 if (qy + py > Y) py = Y - qy;
2107 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2108 if (0 && !s->first_slice_line && s->mb_x) {
2109 if (is_intra[xy - wrap])
2110 sum = FFABS(px) + FFABS(py);
2112 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2114 if (get_bits1(&s->gb)) {
2122 if (is_intra[xy - 2])
2123 sum = FFABS(px) + FFABS(py);
2125 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2127 if (get_bits1(&s->gb)) {
2137 /* store MV using signed modulus of MV range defined in 4.11 */
2138 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2139 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2141 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2142 C = s->current_picture.f.motion_val[1][xy - 2];
2143 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2144 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2145 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2149 if (!s->first_slice_line) { // predictor A is not out of bounds
2150 if (s->mb_width == 1) {
2154 px = mid_pred(A[0], B[0], C[0]);
2155 py = mid_pred(A[1], B[1], C[1]);
2157 } else if (s->mb_x) { // predictor C is not out of bounds
2163 /* Pullback MV as specified in 8.3.5.3.4 */
2166 if (v->profile < PROFILE_ADVANCED) {
2167 qx = (s->mb_x << 5);
2168 qy = (s->mb_y << 5);
2169 X = (s->mb_width << 5) - 4;
2170 Y = (s->mb_height << 5) - 4;
2171 if (qx + px < -28) px = -28 - qx;
2172 if (qy + py < -28) py = -28 - qy;
2173 if (qx + px > X) px = X - qx;
2174 if (qy + py > Y) py = Y - qy;
2176 qx = (s->mb_x << 6);
2177 qy = (s->mb_y << 6);
2178 X = (s->mb_width << 6) - 4;
2179 Y = (s->mb_height << 6) - 4;
2180 if (qx + px < -60) px = -60 - qx;
2181 if (qy + py < -60) py = -60 - qy;
2182 if (qx + px > X) px = X - qx;
2183 if (qy + py > Y) py = Y - qy;
2186 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2187 if (0 && !s->first_slice_line && s->mb_x) {
2188 if (is_intra[xy - wrap])
2189 sum = FFABS(px) + FFABS(py);
2191 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2193 if (get_bits1(&s->gb)) {
2201 if (is_intra[xy - 2])
2202 sum = FFABS(px) + FFABS(py);
2204 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2206 if (get_bits1(&s->gb)) {
2216 /* store MV using signed modulus of MV range defined in 4.11 */
2218 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2219 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2221 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2222 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2223 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2224 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2227 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2229 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2230 MpegEncContext *s = &v->s;
2231 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2233 if (v->bmvtype == BMV_TYPE_DIRECT) {
2234 int total_opp, k, f;
2235 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2236 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2237 v->bfraction, 0, s->quarter_sample);
2238 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2239 v->bfraction, 0, s->quarter_sample);
2240 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2241 v->bfraction, 1, s->quarter_sample);
2242 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2243 v->bfraction, 1, s->quarter_sample);
2245 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2246 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2247 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2248 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2249 f = (total_opp > 2) ? 1 : 0;
2251 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2252 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2255 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2256 for (k = 0; k < 4; k++) {
2257 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2258 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2259 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2260 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2261 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2262 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2266 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2267 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);
2268 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);
2271 if (dir) { // backward
2272 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);
2273 if (n == 3 || mv1) {
2274 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2277 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);
2278 if (n == 3 || mv1) {
2279 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2284 /** Get predicted DC value for I-frames only
2285 * prediction dir: left=0, top=1
2286 * @param s MpegEncContext
2287 * @param overlap flag indicating that overlap filtering is used
2288 * @param pq integer part of picture quantizer
2289 * @param[in] n block index in the current MB
2290 * @param dc_val_ptr Pointer to DC predictor
2291 * @param dir_ptr Prediction direction for use in AC prediction
2293 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2294 int16_t **dc_val_ptr, int *dir_ptr)
2296 int a, b, c, wrap, pred, scale;
2298 static const uint16_t dcpred[32] = {
2299 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2300 114, 102, 93, 85, 79, 73, 68, 64,
2301 60, 57, 54, 51, 49, 47, 45, 43,
2302 41, 39, 38, 37, 35, 34, 33
2305 /* find prediction - wmv3_dc_scale always used here in fact */
2306 if (n < 4) scale = s->y_dc_scale;
2307 else scale = s->c_dc_scale;
2309 wrap = s->block_wrap[n];
2310 dc_val = s->dc_val[0] + s->block_index[n];
2316 b = dc_val[ - 1 - wrap];
2317 a = dc_val[ - wrap];
2319 if (pq < 9 || !overlap) {
2320 /* Set outer values */
2321 if (s->first_slice_line && (n != 2 && n != 3))
2322 b = a = dcpred[scale];
2323 if (s->mb_x == 0 && (n != 1 && n != 3))
2324 b = c = dcpred[scale];
2326 /* Set outer values */
2327 if (s->first_slice_line && (n != 2 && n != 3))
2329 if (s->mb_x == 0 && (n != 1 && n != 3))
2333 if (abs(a - b) <= abs(b - c)) {
2335 *dir_ptr = 1; // left
2338 *dir_ptr = 0; // top
2341 /* update predictor */
2342 *dc_val_ptr = &dc_val[0];
2347 /** Get predicted DC value
2348 * prediction dir: left=0, top=1
2349 * @param s MpegEncContext
2350 * @param overlap flag indicating that overlap filtering is used
2351 * @param pq integer part of picture quantizer
2352 * @param[in] n block index in the current MB
2353 * @param a_avail flag indicating top block availability
2354 * @param c_avail flag indicating left block availability
2355 * @param dc_val_ptr Pointer to DC predictor
2356 * @param dir_ptr Prediction direction for use in AC prediction
2358 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2359 int a_avail, int c_avail,
2360 int16_t **dc_val_ptr, int *dir_ptr)
2362 int a, b, c, wrap, pred;
2364 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2368 wrap = s->block_wrap[n];
2369 dc_val = s->dc_val[0] + s->block_index[n];
2375 b = dc_val[ - 1 - wrap];
2376 a = dc_val[ - wrap];
2377 /* scale predictors if needed */
2378 q1 = s->current_picture.f.qscale_table[mb_pos];
2379 dqscale_index = s->y_dc_scale_table[q1] - 1;
2380 if (dqscale_index < 0)
2382 if (c_avail && (n != 1 && n != 3)) {
2383 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2385 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2387 if (a_avail && (n != 2 && n != 3)) {
2388 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2390 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2392 if (a_avail && c_avail && (n != 3)) {
2397 off -= s->mb_stride;
2398 q2 = s->current_picture.f.qscale_table[off];
2400 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2403 if (a_avail && c_avail) {
2404 if (abs(a - b) <= abs(b - c)) {
2406 *dir_ptr = 1; // left
2409 *dir_ptr = 0; // top
2411 } else if (a_avail) {
2413 *dir_ptr = 0; // top
2414 } else if (c_avail) {
2416 *dir_ptr = 1; // left
2419 *dir_ptr = 1; // left
2422 /* update predictor */
2423 *dc_val_ptr = &dc_val[0];
2427 /** @} */ // Block group
2430 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2431 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2435 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2436 uint8_t **coded_block_ptr)
2438 int xy, wrap, pred, a, b, c;
2440 xy = s->block_index[n];
2441 wrap = s->b8_stride;
2446 a = s->coded_block[xy - 1 ];
2447 b = s->coded_block[xy - 1 - wrap];
2448 c = s->coded_block[xy - wrap];
2457 *coded_block_ptr = &s->coded_block[xy];
2463 * Decode one AC coefficient
2464 * @param v The VC1 context
2465 * @param last Last coefficient
2466 * @param skip How much zero coefficients to skip
2467 * @param value Decoded AC coefficient value
2468 * @param codingset set of VLC to decode data
2471 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2472 int *value, int codingset)
2474 GetBitContext *gb = &v->s.gb;
2475 int index, escape, run = 0, level = 0, lst = 0;
2477 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2478 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2479 run = vc1_index_decode_table[codingset][index][0];
2480 level = vc1_index_decode_table[codingset][index][1];
2481 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2485 escape = decode210(gb);
2487 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2488 run = vc1_index_decode_table[codingset][index][0];
2489 level = vc1_index_decode_table[codingset][index][1];
2490 lst = index >= vc1_last_decode_table[codingset];
2493 level += vc1_last_delta_level_table[codingset][run];
2495 level += vc1_delta_level_table[codingset][run];
2498 run += vc1_last_delta_run_table[codingset][level] + 1;
2500 run += vc1_delta_run_table[codingset][level] + 1;
2506 lst = get_bits1(gb);
2507 if (v->s.esc3_level_length == 0) {
2508 if (v->pq < 8 || v->dquantfrm) { // table 59
2509 v->s.esc3_level_length = get_bits(gb, 3);
2510 if (!v->s.esc3_level_length)
2511 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2512 } else { // table 60
2513 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2515 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2517 run = get_bits(gb, v->s.esc3_run_length);
2518 sign = get_bits1(gb);
2519 level = get_bits(gb, v->s.esc3_level_length);
2530 /** Decode intra block in intra frames - should be faster than decode_intra_block
2531 * @param v VC1Context
2532 * @param block block to decode
2533 * @param[in] n subblock index
2534 * @param coded are AC coeffs present or not
2535 * @param codingset set of VLC to decode data
2537 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2538 int coded, int codingset)
2540 GetBitContext *gb = &v->s.gb;
2541 MpegEncContext *s = &v->s;
2542 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2545 int16_t *ac_val, *ac_val2;
2548 /* Get DC differential */
2550 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2552 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2555 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2559 if (dcdiff == 119 /* ESC index value */) {
2560 /* TODO: Optimize */
2561 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2562 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2563 else dcdiff = get_bits(gb, 8);
2566 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2567 else if (v->pq == 2)
2568 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2575 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2578 /* Store the quantized DC coeff, used for prediction */
2580 block[0] = dcdiff * s->y_dc_scale;
2582 block[0] = dcdiff * s->c_dc_scale;
2593 int last = 0, skip, value;
2594 const uint8_t *zz_table;
2598 scale = v->pq * 2 + v->halfpq;
2602 zz_table = v->zz_8x8[2];
2604 zz_table = v->zz_8x8[3];
2606 zz_table = v->zz_8x8[1];
2608 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2610 if (dc_pred_dir) // left
2613 ac_val -= 16 * s->block_wrap[n];
2616 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2620 block[zz_table[i++]] = value;
2623 /* apply AC prediction if needed */
2625 if (dc_pred_dir) { // left
2626 for (k = 1; k < 8; k++)
2627 block[k << v->left_blk_sh] += ac_val[k];
2629 for (k = 1; k < 8; k++)
2630 block[k << v->top_blk_sh] += ac_val[k + 8];
2633 /* save AC coeffs for further prediction */
2634 for (k = 1; k < 8; k++) {
2635 ac_val2[k] = block[k << v->left_blk_sh];
2636 ac_val2[k + 8] = block[k << v->top_blk_sh];
2639 /* scale AC coeffs */
2640 for (k = 1; k < 64; k++)
2644 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2647 if (s->ac_pred) i = 63;
2653 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2657 scale = v->pq * 2 + v->halfpq;
2658 memset(ac_val2, 0, 16 * 2);
2659 if (dc_pred_dir) { // left
2662 memcpy(ac_val2, ac_val, 8 * 2);
2664 ac_val -= 16 * s->block_wrap[n];
2666 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2669 /* apply AC prediction if needed */
2671 if (dc_pred_dir) { //left
2672 for (k = 1; k < 8; k++) {
2673 block[k << v->left_blk_sh] = ac_val[k] * scale;
2674 if (!v->pquantizer && block[k << v->left_blk_sh])
2675 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2678 for (k = 1; k < 8; k++) {
2679 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2680 if (!v->pquantizer && block[k << v->top_blk_sh])
2681 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2687 s->block_last_index[n] = i;
2692 /** Decode intra block in intra frames - should be faster than decode_intra_block
2693 * @param v VC1Context
2694 * @param block block to decode
2695 * @param[in] n subblock number
2696 * @param coded are AC coeffs present or not
2697 * @param codingset set of VLC to decode data
2698 * @param mquant quantizer value for this macroblock
2700 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2701 int coded, int codingset, int mquant)
2703 GetBitContext *gb = &v->s.gb;
2704 MpegEncContext *s = &v->s;
2705 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2708 int16_t *ac_val, *ac_val2;
2710 int a_avail = v->a_avail, c_avail = v->c_avail;
2711 int use_pred = s->ac_pred;
2714 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2716 /* Get DC differential */
2718 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2720 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2723 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2727 if (dcdiff == 119 /* ESC index value */) {
2728 /* TODO: Optimize */
2729 if (mquant == 1) dcdiff = get_bits(gb, 10);
2730 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2731 else dcdiff = get_bits(gb, 8);
2734 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2735 else if (mquant == 2)
2736 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2743 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2746 /* Store the quantized DC coeff, used for prediction */
2748 block[0] = dcdiff * s->y_dc_scale;
2750 block[0] = dcdiff * s->c_dc_scale;
2756 /* check if AC is needed at all */
2757 if (!a_avail && !c_avail)
2759 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2762 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2764 if (dc_pred_dir) // left
2767 ac_val -= 16 * s->block_wrap[n];
2769 q1 = s->current_picture.f.qscale_table[mb_pos];
2770 if ( dc_pred_dir && c_avail && mb_pos)
2771 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2772 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2773 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2774 if ( dc_pred_dir && n == 1)
2776 if (!dc_pred_dir && n == 2)
2782 int last = 0, skip, value;
2783 const uint8_t *zz_table;
2787 if (!use_pred && v->fcm == ILACE_FRAME) {
2788 zz_table = v->zzi_8x8;
2790 if (!dc_pred_dir) // top
2791 zz_table = v->zz_8x8[2];
2793 zz_table = v->zz_8x8[3];
2796 if (v->fcm != ILACE_FRAME)
2797 zz_table = v->zz_8x8[1];
2799 zz_table = v->zzi_8x8;
2803 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2807 block[zz_table[i++]] = value;
2810 /* apply AC prediction if needed */
2812 /* scale predictors if needed*/
2813 if (q2 && q1 != q2) {
2814 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2815 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2818 return AVERROR_INVALIDDATA;
2819 if (dc_pred_dir) { // left
2820 for (k = 1; k < 8; k++)
2821 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2823 for (k = 1; k < 8; k++)
2824 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2827 if (dc_pred_dir) { //left
2828 for (k = 1; k < 8; k++)
2829 block[k << v->left_blk_sh] += ac_val[k];
2831 for (k = 1; k < 8; k++)
2832 block[k << v->top_blk_sh] += ac_val[k + 8];
2836 /* save AC coeffs for further prediction */
2837 for (k = 1; k < 8; k++) {
2838 ac_val2[k ] = block[k << v->left_blk_sh];
2839 ac_val2[k + 8] = block[k << v->top_blk_sh];
2842 /* scale AC coeffs */
2843 for (k = 1; k < 64; k++)
2847 block[k] += (block[k] < 0) ? -mquant : mquant;
2850 if (use_pred) i = 63;
2851 } else { // no AC coeffs
2854 memset(ac_val2, 0, 16 * 2);
2855 if (dc_pred_dir) { // left
2857 memcpy(ac_val2, ac_val, 8 * 2);
2858 if (q2 && q1 != q2) {
2859 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2860 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2862 return AVERROR_INVALIDDATA;
2863 for (k = 1; k < 8; k++)
2864 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2869 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2870 if (q2 && q1 != q2) {
2871 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2872 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2874 return AVERROR_INVALIDDATA;
2875 for (k = 1; k < 8; k++)
2876 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2881 /* apply AC prediction if needed */
2883 if (dc_pred_dir) { // left
2884 for (k = 1; k < 8; k++) {
2885 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2886 if (!v->pquantizer && block[k << v->left_blk_sh])
2887 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2890 for (k = 1; k < 8; k++) {
2891 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2892 if (!v->pquantizer && block[k << v->top_blk_sh])
2893 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2899 s->block_last_index[n] = i;
2904 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2905 * @param v VC1Context
2906 * @param block block to decode
2907 * @param[in] n subblock index
2908 * @param coded are AC coeffs present or not
2909 * @param mquant block quantizer
2910 * @param codingset set of VLC to decode data
2912 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2913 int coded, int mquant, int codingset)
2915 GetBitContext *gb = &v->s.gb;
2916 MpegEncContext *s = &v->s;
2917 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2920 int16_t *ac_val, *ac_val2;
2922 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2923 int a_avail = v->a_avail, c_avail = v->c_avail;
2924 int use_pred = s->ac_pred;
2928 s->dsp.clear_block(block);
2930 /* XXX: Guard against dumb values of mquant */
2931 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2933 /* Set DC scale - y and c use the same */
2934 s->y_dc_scale = s->y_dc_scale_table[mquant];
2935 s->c_dc_scale = s->c_dc_scale_table[mquant];
2937 /* Get DC differential */
2939 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2941 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2944 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2948 if (dcdiff == 119 /* ESC index value */) {
2949 /* TODO: Optimize */
2950 if (mquant == 1) dcdiff = get_bits(gb, 10);
2951 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2952 else dcdiff = get_bits(gb, 8);
2955 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2956 else if (mquant == 2)
2957 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2964 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2967 /* Store the quantized DC coeff, used for prediction */
2970 block[0] = dcdiff * s->y_dc_scale;
2972 block[0] = dcdiff * s->c_dc_scale;
2978 /* check if AC is needed at all and adjust direction if needed */
2979 if (!a_avail) dc_pred_dir = 1;
2980 if (!c_avail) dc_pred_dir = 0;
2981 if (!a_avail && !c_avail) use_pred = 0;
2982 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2985 scale = mquant * 2 + v->halfpq;
2987 if (dc_pred_dir) //left
2990 ac_val -= 16 * s->block_wrap[n];
2992 q1 = s->current_picture.f.qscale_table[mb_pos];
2993 if (dc_pred_dir && c_avail && mb_pos)
2994 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2995 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2996 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2997 if ( dc_pred_dir && n == 1)
2999 if (!dc_pred_dir && n == 2)
3001 if (n == 3) q2 = q1;
3004 int last = 0, skip, value;
3008 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3012 if (v->fcm == PROGRESSIVE)
3013 block[v->zz_8x8[0][i++]] = value;
3015 if (use_pred && (v->fcm == ILACE_FRAME)) {
3016 if (!dc_pred_dir) // top
3017 block[v->zz_8x8[2][i++]] = value;
3019 block[v->zz_8x8[3][i++]] = value;
3021 block[v->zzi_8x8[i++]] = value;
3026 /* apply AC prediction if needed */
3028 /* scale predictors if needed*/
3029 if (q2 && q1 != q2) {
3030 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3031 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3034 return AVERROR_INVALIDDATA;
3035 if (dc_pred_dir) { // left
3036 for (k = 1; k < 8; k++)
3037 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3039 for (k = 1; k < 8; k++)
3040 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3043 if (dc_pred_dir) { // left
3044 for (k = 1; k < 8; k++)
3045 block[k << v->left_blk_sh] += ac_val[k];
3047 for (k = 1; k < 8; k++)
3048 block[k << v->top_blk_sh] += ac_val[k + 8];
3052 /* save AC coeffs for further prediction */
3053 for (k = 1; k < 8; k++) {
3054 ac_val2[k ] = block[k << v->left_blk_sh];
3055 ac_val2[k + 8] = block[k << v->top_blk_sh];
3058 /* scale AC coeffs */
3059 for (k = 1; k < 64; k++)
3063 block[k] += (block[k] < 0) ? -mquant : mquant;
3066 if (use_pred) i = 63;
3067 } else { // no AC coeffs
3070 memset(ac_val2, 0, 16 * 2);
3071 if (dc_pred_dir) { // left
3073 memcpy(ac_val2, ac_val, 8 * 2);
3074 if (q2 && q1 != q2) {
3075 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3076 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3078 return AVERROR_INVALIDDATA;
3079 for (k = 1; k < 8; k++)
3080 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3085 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3086 if (q2 && q1 != q2) {
3087 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3088 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3090 return AVERROR_INVALIDDATA;
3091 for (k = 1; k < 8; k++)
3092 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3097 /* apply AC prediction if needed */
3099 if (dc_pred_dir) { // left
3100 for (k = 1; k < 8; k++) {
3101 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3102 if (!v->pquantizer && block[k << v->left_blk_sh])
3103 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3106 for (k = 1; k < 8; k++) {
3107 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3108 if (!v->pquantizer && block[k << v->top_blk_sh])
3109 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3115 s->block_last_index[n] = i;
3122 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3123 int mquant, int ttmb, int first_block,
3124 uint8_t *dst, int linesize, int skip_block,
3127 MpegEncContext *s = &v->s;
3128 GetBitContext *gb = &s->gb;
3131 int scale, off, idx, last, skip, value;
3132 int ttblk = ttmb & 7;
3135 s->dsp.clear_block(block);
3138 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)];
3140 if (ttblk == TT_4X4) {
3141 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3143 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3144 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3145 || (!v->res_rtm_flag && !first_block))) {
3146 subblkpat = decode012(gb);
3148 subblkpat ^= 3; // swap decoded pattern bits
3149 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3151 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3154 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3156 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3157 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3158 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3161 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3162 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3171 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3176 idx = v->zz_8x8[0][i++];
3178 idx = v->zzi_8x8[i++];
3179 block[idx] = value * scale;
3181 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3185 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3187 v->vc1dsp.vc1_inv_trans_8x8(block);
3188 s->dsp.add_pixels_clamped(block, dst, linesize);
3193 pat = ~subblkpat & 0xF;
3194 for (j = 0; j < 4; j++) {
3195 last = subblkpat & (1 << (3 - j));
3197 off = (j & 1) * 4 + (j & 2) * 16;
3199 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3204 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3206 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3207 block[idx + off] = value * scale;
3209 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3211 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3213 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3215 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3220 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3221 for (j = 0; j < 2; j++) {
3222 last = subblkpat & (1 << (1 - j));
3226 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3231 idx = v->zz_8x4[i++] + off;
3233 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3234 block[idx] = value * scale;
3236 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3238 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3240 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3242 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3247 pat = ~(subblkpat * 5) & 0xF;
3248 for (j = 0; j < 2; j++) {
3249 last = subblkpat & (1 << (1 - j));
3253 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3258 idx = v->zz_4x8[i++] + off;
3260 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3261 block[idx] = value * scale;
3263 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3265 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3267 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3269 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3275 *ttmb_out |= ttblk << (n * 4);
3279 /** @} */ // Macroblock group
3281 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3282 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3284 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3286 MpegEncContext *s = &v->s;
3287 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3288 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3289 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3290 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3291 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3294 if (block_num > 3) {
3295 dst = s->dest[block_num - 3];
3297 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3299 if (s->mb_y != s->end_mb_y || block_num < 2) {
3303 if (block_num > 3) {
3304 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3305 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3306 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3307 mv_stride = s->mb_stride;
3309 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3310 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3311 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3312 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3313 mv_stride = s->b8_stride;
3314 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3317 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3318 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3319 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3321 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3323 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3326 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3328 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3333 dst -= 4 * linesize;
3334 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3335 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3336 idx = (block_cbp | (block_cbp >> 2)) & 3;
3338 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3341 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3343 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3348 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3350 MpegEncContext *s = &v->s;
3351 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3352 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3353 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3354 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3355 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3358 if (block_num > 3) {
3359 dst = s->dest[block_num - 3] - 8 * linesize;
3361 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3364 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3367 if (block_num > 3) {
3368 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3369 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3370 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3372 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3373 : (mb_cbp >> ((block_num + 1) * 4));
3374 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3375 : (mb_is_intra >> ((block_num + 1) * 4));
3376 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3378 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3379 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3381 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3383 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3386 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3388 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3394 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3395 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3396 idx = (block_cbp | (block_cbp >> 1)) & 5;
3398 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3401 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3403 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3408 static void vc1_apply_p_loop_filter(VC1Context *v)
3410 MpegEncContext *s = &v->s;
3413 for (i = 0; i < 6; i++) {
3414 vc1_apply_p_v_loop_filter(v, i);
3417 /* V always precedes H, therefore we run H one MB before V;
3418 * at the end of a row, we catch up to complete the row */
3420 for (i = 0; i < 6; i++) {
3421 vc1_apply_p_h_loop_filter(v, i);
3423 if (s->mb_x == s->mb_width - 1) {
3425 ff_update_block_index(s);
3426 for (i = 0; i < 6; i++) {
3427 vc1_apply_p_h_loop_filter(v, i);
3433 /** Decode one P-frame MB
3435 static int vc1_decode_p_mb(VC1Context *v)
3437 MpegEncContext *s = &v->s;
3438 GetBitContext *gb = &s->gb;
3440 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3441 int cbp; /* cbp decoding stuff */
3442 int mqdiff, mquant; /* MB quantization */
3443 int ttmb = v->ttfrm; /* MB Transform type */
3445 int mb_has_coeffs = 1; /* last_flag */
3446 int dmv_x, dmv_y; /* Differential MV components */
3447 int index, index1; /* LUT indexes */
3448 int val, sign; /* temp values */
3449 int first_block = 1;
3451 int skipped, fourmv;
3452 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3454 mquant = v->pq; /* lossy initialization */
3456 if (v->mv_type_is_raw)
3457 fourmv = get_bits1(gb);
3459 fourmv = v->mv_type_mb_plane[mb_pos];
3461 skipped = get_bits1(gb);
3463 skipped = v->s.mbskip_table[mb_pos];
3465 if (!fourmv) { /* 1MV mode */
3467 GET_MVDATA(dmv_x, dmv_y);
3470 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3471 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3473 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3474 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3476 /* FIXME Set DC val for inter block ? */
3477 if (s->mb_intra && !mb_has_coeffs) {
3479 s->ac_pred = get_bits1(gb);
3481 } else if (mb_has_coeffs) {
3483 s->ac_pred = get_bits1(gb);
3484 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3490 s->current_picture.f.qscale_table[mb_pos] = mquant;
3492 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3493 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3494 VC1_TTMB_VLC_BITS, 2);
3495 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3497 for (i = 0; i < 6; i++) {
3498 s->dc_val[0][s->block_index[i]] = 0;
3500 val = ((cbp >> (5 - i)) & 1);
3501 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3502 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3504 /* check if prediction blocks A and C are available */
3505 v->a_avail = v->c_avail = 0;
3506 if (i == 2 || i == 3 || !s->first_slice_line)
3507 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3508 if (i == 1 || i == 3 || s->mb_x)
3509 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3511 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3512 (i & 4) ? v->codingset2 : v->codingset);
3513 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3515 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3517 for (j = 0; j < 64; j++)
3518 s->block[i][j] <<= 1;
3519 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3520 if (v->pq >= 9 && v->overlap) {
3522 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3524 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3526 block_cbp |= 0xF << (i << 2);
3527 block_intra |= 1 << i;
3529 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3530 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3531 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3532 block_cbp |= pat << (i << 2);
3533 if (!v->ttmbf && ttmb < 8)
3540 for (i = 0; i < 6; i++) {
3541 v->mb_type[0][s->block_index[i]] = 0;
3542 s->dc_val[0][s->block_index[i]] = 0;
3544 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3545 s->current_picture.f.qscale_table[mb_pos] = 0;
3546 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3549 } else { // 4MV mode
3550 if (!skipped /* unskipped MB */) {
3551 int intra_count = 0, coded_inter = 0;
3552 int is_intra[6], is_coded[6];
3554 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3555 for (i = 0; i < 6; i++) {
3556 val = ((cbp >> (5 - i)) & 1);
3557 s->dc_val[0][s->block_index[i]] = 0;
3564 GET_MVDATA(dmv_x, dmv_y);
3566 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3568 vc1_mc_4mv_luma(v, i, 0);
3569 intra_count += s->mb_intra;
3570 is_intra[i] = s->mb_intra;
3571 is_coded[i] = mb_has_coeffs;
3574 is_intra[i] = (intra_count >= 3);
3578 vc1_mc_4mv_chroma(v, 0);
3579 v->mb_type[0][s->block_index[i]] = is_intra[i];
3581 coded_inter = !is_intra[i] & is_coded[i];
3583 // if there are no coded blocks then don't do anything more
3585 if (!intra_count && !coded_inter)
3588 s->current_picture.f.qscale_table[mb_pos] = mquant;
3589 /* test if block is intra and has pred */
3592 for (i = 0; i < 6; i++)
3594 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3595 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3601 s->ac_pred = get_bits1(gb);
3605 if (!v->ttmbf && coded_inter)
3606 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3607 for (i = 0; i < 6; i++) {
3609 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3610 s->mb_intra = is_intra[i];
3612 /* check if prediction blocks A and C are available */
3613 v->a_avail = v->c_avail = 0;
3614 if (i == 2 || i == 3 || !s->first_slice_line)
3615 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3616 if (i == 1 || i == 3 || s->mb_x)
3617 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3619 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3620 (i & 4) ? v->codingset2 : v->codingset);
3621 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3623 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3625 for (j = 0; j < 64; j++)
3626 s->block[i][j] <<= 1;
3627 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3628 (i & 4) ? s->uvlinesize : s->linesize);
3629 if (v->pq >= 9 && v->overlap) {
3631 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3633 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3635 block_cbp |= 0xF << (i << 2);
3636 block_intra |= 1 << i;
3637 } else if (is_coded[i]) {
3638 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3639 first_block, s->dest[dst_idx] + off,
3640 (i & 4) ? s->uvlinesize : s->linesize,
3641 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3643 block_cbp |= pat << (i << 2);
3644 if (!v->ttmbf && ttmb < 8)
3649 } else { // skipped MB
3651 s->current_picture.f.qscale_table[mb_pos] = 0;
3652 for (i = 0; i < 6; i++) {
3653 v->mb_type[0][s->block_index[i]] = 0;
3654 s->dc_val[0][s->block_index[i]] = 0;
3656 for (i = 0; i < 4; i++) {
3657 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3658 vc1_mc_4mv_luma(v, i, 0);
3660 vc1_mc_4mv_chroma(v, 0);
3661 s->current_picture.f.qscale_table[mb_pos] = 0;
3665 v->cbp[s->mb_x] = block_cbp;
3666 v->ttblk[s->mb_x] = block_tt;
3667 v->is_intra[s->mb_x] = block_intra;
3672 /* Decode one macroblock in an interlaced frame p picture */
3674 static int vc1_decode_p_mb_intfr(VC1Context *v)
3676 MpegEncContext *s = &v->s;
3677 GetBitContext *gb = &s->gb;
3679 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3680 int cbp = 0; /* cbp decoding stuff */
3681 int mqdiff, mquant; /* MB quantization */
3682 int ttmb = v->ttfrm; /* MB Transform type */
3684 int mb_has_coeffs = 1; /* last_flag */
3685 int dmv_x, dmv_y; /* Differential MV components */
3686 int val; /* temp value */
3687 int first_block = 1;
3689 int skipped, fourmv = 0, twomv = 0;
3690 int block_cbp = 0, pat, block_tt = 0;
3691 int idx_mbmode = 0, mvbp;
3692 int stride_y, fieldtx;
3694 mquant = v->pq; /* Lossy initialization */
3697 skipped = get_bits1(gb);
3699 skipped = v->s.mbskip_table[mb_pos];
3701 if (v->fourmvswitch)
3702 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3704 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3705 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3706 /* store the motion vector type in a flag (useful later) */
3707 case MV_PMODE_INTFR_4MV:
3709 v->blk_mv_type[s->block_index[0]] = 0;
3710 v->blk_mv_type[s->block_index[1]] = 0;
3711 v->blk_mv_type[s->block_index[2]] = 0;
3712 v->blk_mv_type[s->block_index[3]] = 0;
3714 case MV_PMODE_INTFR_4MV_FIELD:
3716 v->blk_mv_type[s->block_index[0]] = 1;
3717 v->blk_mv_type[s->block_index[1]] = 1;
3718 v->blk_mv_type[s->block_index[2]] = 1;
3719 v->blk_mv_type[s->block_index[3]] = 1;
3721 case MV_PMODE_INTFR_2MV_FIELD:
3723 v->blk_mv_type[s->block_index[0]] = 1;
3724 v->blk_mv_type[s->block_index[1]] = 1;
3725 v->blk_mv_type[s->block_index[2]] = 1;
3726 v->blk_mv_type[s->block_index[3]] = 1;
3728 case MV_PMODE_INTFR_1MV:
3729 v->blk_mv_type[s->block_index[0]] = 0;
3730 v->blk_mv_type[s->block_index[1]] = 0;
3731 v->blk_mv_type[s->block_index[2]] = 0;
3732 v->blk_mv_type[s->block_index[3]] = 0;
3735 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3736 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3737 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3738 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3739 s->mb_intra = v->is_intra[s->mb_x] = 1;
3740 for (i = 0; i < 6; i++)
3741 v->mb_type[0][s->block_index[i]] = 1;
3742 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3743 mb_has_coeffs = get_bits1(gb);
3745 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3746 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3748 s->current_picture.f.qscale_table[mb_pos] = mquant;
3749 /* Set DC scale - y and c use the same (not sure if necessary here) */
3750 s->y_dc_scale = s->y_dc_scale_table[mquant];
3751 s->c_dc_scale = s->c_dc_scale_table[mquant];
3753 for (i = 0; i < 6; i++) {
3754 s->dc_val[0][s->block_index[i]] = 0;
3756 val = ((cbp >> (5 - i)) & 1);
3757 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3758 v->a_avail = v->c_avail = 0;
3759 if (i == 2 || i == 3 || !s->first_slice_line)
3760 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3761 if (i == 1 || i == 3 || s->mb_x)
3762 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3764 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3765 (i & 4) ? v->codingset2 : v->codingset);
3766 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3767 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3769 stride_y = s->linesize << fieldtx;
3770 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3772 stride_y = s->uvlinesize;
3775 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3779 } else { // inter MB
3780 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3782 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3783 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3784 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3786 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3787 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3788 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3791 s->mb_intra = v->is_intra[s->mb_x] = 0;
3792 for (i = 0; i < 6; i++)
3793 v->mb_type[0][s->block_index[i]] = 0;
3794 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3795 /* for all motion vector read MVDATA and motion compensate each block */
3799 for (i = 0; i < 6; i++) {
3802 val = ((mvbp >> (3 - i)) & 1);
3804 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3806 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3807 vc1_mc_4mv_luma(v, i, 0);
3808 } else if (i == 4) {
3809 vc1_mc_4mv_chroma4(v);
3816 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3818 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3819 vc1_mc_4mv_luma(v, 0, 0);
3820 vc1_mc_4mv_luma(v, 1, 0);
3823 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3825 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3826 vc1_mc_4mv_luma(v, 2, 0);
3827 vc1_mc_4mv_luma(v, 3, 0);
3828 vc1_mc_4mv_chroma4(v);
3830 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3833 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3835 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3839 GET_MQUANT(); // p. 227
3840 s->current_picture.f.qscale_table[mb_pos] = mquant;
3841 if (!v->ttmbf && cbp)
3842 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3843 for (i = 0; i < 6; i++) {
3844 s->dc_val[0][s->block_index[i]] = 0;
3846 val = ((cbp >> (5 - i)) & 1);
3848 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3850 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3852 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3853 first_block, s->dest[dst_idx] + off,
3854 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3855 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3856 block_cbp |= pat << (i << 2);
3857 if (!v->ttmbf && ttmb < 8)
3864 s->mb_intra = v->is_intra[s->mb_x] = 0;
3865 for (i = 0; i < 6; i++) {
3866 v->mb_type[0][s->block_index[i]] = 0;
3867 s->dc_val[0][s->block_index[i]] = 0;
3869 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3870 s->current_picture.f.qscale_table[mb_pos] = 0;
3871 v->blk_mv_type[s->block_index[0]] = 0;
3872 v->blk_mv_type[s->block_index[1]] = 0;
3873 v->blk_mv_type[s->block_index[2]] = 0;
3874 v->blk_mv_type[s->block_index[3]] = 0;
3875 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3878 if (s->mb_x == s->mb_width - 1)
3879 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3883 static int vc1_decode_p_mb_intfi(VC1Context *v)
3885 MpegEncContext *s = &v->s;
3886 GetBitContext *gb = &s->gb;
3888 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3889 int cbp = 0; /* cbp decoding stuff */
3890 int mqdiff, mquant; /* MB quantization */
3891 int ttmb = v->ttfrm; /* MB Transform type */
3893 int mb_has_coeffs = 1; /* last_flag */
3894 int dmv_x, dmv_y; /* Differential MV components */
3895 int val; /* temp values */
3896 int first_block = 1;
3899 int block_cbp = 0, pat, block_tt = 0;
3902 mquant = v->pq; /* Lossy initialization */
3904 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3905 if (idx_mbmode <= 1) { // intra MB
3906 s->mb_intra = v->is_intra[s->mb_x] = 1;
3907 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3908 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3909 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3911 s->current_picture.f.qscale_table[mb_pos] = mquant;
3912 /* Set DC scale - y and c use the same (not sure if necessary here) */
3913 s->y_dc_scale = s->y_dc_scale_table[mquant];
3914 s->c_dc_scale = s->c_dc_scale_table[mquant];
3915 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3916 mb_has_coeffs = idx_mbmode & 1;
3918 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3920 for (i = 0; i < 6; i++) {
3921 s->dc_val[0][s->block_index[i]] = 0;
3922 v->mb_type[0][s->block_index[i]] = 1;
3924 val = ((cbp >> (5 - i)) & 1);
3925 v->a_avail = v->c_avail = 0;
3926 if (i == 2 || i == 3 || !s->first_slice_line)
3927 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3928 if (i == 1 || i == 3 || s->mb_x)
3929 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3931 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3932 (i & 4) ? v->codingset2 : v->codingset);
3933 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3935 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3936 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3937 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3938 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3939 // TODO: loop filter
3942 s->mb_intra = v->is_intra[s->mb_x] = 0;
3943 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3944 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3945 if (idx_mbmode <= 5) { // 1-MV
3946 dmv_x = dmv_y = pred_flag = 0;
3947 if (idx_mbmode & 1) {
3948 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3950 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3952 mb_has_coeffs = !(idx_mbmode & 2);
3954 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3955 for (i = 0; i < 6; i++) {
3957 dmv_x = dmv_y = pred_flag = 0;
3958 val = ((v->fourmvbp >> (3 - i)) & 1);
3960 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3962 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3963 vc1_mc_4mv_luma(v, i, 0);
3965 vc1_mc_4mv_chroma(v, 0);
3967 mb_has_coeffs = idx_mbmode & 1;
3970 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3974 s->current_picture.f.qscale_table[mb_pos] = mquant;
3975 if (!v->ttmbf && cbp) {
3976 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3979 for (i = 0; i < 6; i++) {
3980 s->dc_val[0][s->block_index[i]] = 0;
3982 val = ((cbp >> (5 - i)) & 1);
3983 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
3984 if (v->second_field)
3985 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
3987 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3988 first_block, s->dest[dst_idx] + off,
3989 (i & 4) ? s->uvlinesize : s->linesize,
3990 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3992 block_cbp |= pat << (i << 2);
3993 if (!v->ttmbf && ttmb < 8) ttmb = -1;
3998 if (s->mb_x == s->mb_width - 1)
3999 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4003 /** Decode one B-frame MB (in Main profile)
4005 static void vc1_decode_b_mb(VC1Context *v)
4007 MpegEncContext *s = &v->s;
4008 GetBitContext *gb = &s->gb;
4010 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4011 int cbp = 0; /* cbp decoding stuff */
4012 int mqdiff, mquant; /* MB quantization */
4013 int ttmb = v->ttfrm; /* MB Transform type */
4014 int mb_has_coeffs = 0; /* last_flag */
4015 int index, index1; /* LUT indexes */
4016 int val, sign; /* temp values */
4017 int first_block = 1;
4019 int skipped, direct;
4020 int dmv_x[2], dmv_y[2];
4021 int bmvtype = BMV_TYPE_BACKWARD;
4023 mquant = v->pq; /* lossy initialization */
4027 direct = get_bits1(gb);
4029 direct = v->direct_mb_plane[mb_pos];
4031 skipped = get_bits1(gb);
4033 skipped = v->s.mbskip_table[mb_pos];
4035 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4036 for (i = 0; i < 6; i++) {
4037 v->mb_type[0][s->block_index[i]] = 0;
4038 s->dc_val[0][s->block_index[i]] = 0;
4040 s->current_picture.f.qscale_table[mb_pos] = 0;
4044 GET_MVDATA(dmv_x[0], dmv_y[0]);
4045 dmv_x[1] = dmv_x[0];
4046 dmv_y[1] = dmv_y[0];
4048 if (skipped || !s->mb_intra) {
4049 bmvtype = decode012(gb);
4052 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4055 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4058 bmvtype = BMV_TYPE_INTERPOLATED;
4059 dmv_x[0] = dmv_y[0] = 0;
4063 for (i = 0; i < 6; i++)
4064 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4068 bmvtype = BMV_TYPE_INTERPOLATED;
4069 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4070 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4074 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4077 s->current_picture.f.qscale_table[mb_pos] = mquant;
4079 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4080 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4081 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4082 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4084 if (!mb_has_coeffs && !s->mb_intra) {
4085 /* no coded blocks - effectively skipped */
4086 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4087 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4090 if (s->mb_intra && !mb_has_coeffs) {
4092 s->current_picture.f.qscale_table[mb_pos] = mquant;
4093 s->ac_pred = get_bits1(gb);
4095 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4097 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4098 GET_MVDATA(dmv_x[0], dmv_y[0]);
4099 if (!mb_has_coeffs) {
4100 /* interpolated skipped block */
4101 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4102 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4106 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4108 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4111 s->ac_pred = get_bits1(gb);
4112 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4114 s->current_picture.f.qscale_table[mb_pos] = mquant;
4115 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4116 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4120 for (i = 0; i < 6; i++) {
4121 s->dc_val[0][s->block_index[i]] = 0;
4123 val = ((cbp >> (5 - i)) & 1);
4124 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4125 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4127 /* check if prediction blocks A and C are available */
4128 v->a_avail = v->c_avail = 0;
4129 if (i == 2 || i == 3 || !s->first_slice_line)
4130 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4131 if (i == 1 || i == 3 || s->mb_x)
4132 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4134 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4135 (i & 4) ? v->codingset2 : v->codingset);
4136 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4138 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4140 for (j = 0; j < 64; j++)
4141 s->block[i][j] <<= 1;
4142 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4144 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4145 first_block, s->dest[dst_idx] + off,
4146 (i & 4) ? s->uvlinesize : s->linesize,
4147 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4148 if (!v->ttmbf && ttmb < 8)
4155 /** Decode one B-frame MB (in interlaced field B picture)
4157 static void vc1_decode_b_mb_intfi(VC1Context *v)
4159 MpegEncContext *s = &v->s;
4160 GetBitContext *gb = &s->gb;
4162 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4163 int cbp = 0; /* cbp decoding stuff */
4164 int mqdiff, mquant; /* MB quantization */
4165 int ttmb = v->ttfrm; /* MB Transform type */
4166 int mb_has_coeffs = 0; /* last_flag */
4167 int val; /* temp value */
4168 int first_block = 1;
4171 int dmv_x[2], dmv_y[2], pred_flag[2];
4172 int bmvtype = BMV_TYPE_BACKWARD;
4173 int idx_mbmode, interpmvp;
4175 mquant = v->pq; /* Lossy initialization */
4178 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4179 if (idx_mbmode <= 1) { // intra MB
4180 s->mb_intra = v->is_intra[s->mb_x] = 1;
4181 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4182 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4183 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4185 s->current_picture.f.qscale_table[mb_pos] = mquant;
4186 /* Set DC scale - y and c use the same (not sure if necessary here) */
4187 s->y_dc_scale = s->y_dc_scale_table[mquant];
4188 s->c_dc_scale = s->c_dc_scale_table[mquant];
4189 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4190 mb_has_coeffs = idx_mbmode & 1;
4192 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4194 for (i = 0; i < 6; i++) {
4195 s->dc_val[0][s->block_index[i]] = 0;
4197 val = ((cbp >> (5 - i)) & 1);
4198 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4199 v->a_avail = v->c_avail = 0;
4200 if (i == 2 || i == 3 || !s->first_slice_line)
4201 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4202 if (i == 1 || i == 3 || s->mb_x)
4203 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4205 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4206 (i & 4) ? v->codingset2 : v->codingset);
4207 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4209 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4211 for (j = 0; j < 64; j++)
4212 s->block[i][j] <<= 1;
4213 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4214 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4215 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4216 // TODO: yet to perform loop filter
4219 s->mb_intra = v->is_intra[s->mb_x] = 0;
4220 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4221 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4223 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4225 fwd = v->forward_mb_plane[mb_pos];
4226 if (idx_mbmode <= 5) { // 1-MV
4227 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4228 pred_flag[0] = pred_flag[1] = 0;
4230 bmvtype = BMV_TYPE_FORWARD;
4232 bmvtype = decode012(gb);
4235 bmvtype = BMV_TYPE_BACKWARD;
4238 bmvtype = BMV_TYPE_DIRECT;
4241 bmvtype = BMV_TYPE_INTERPOLATED;
4242 interpmvp = get_bits1(gb);
4245 v->bmvtype = bmvtype;
4246 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4247 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4249 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4250 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4252 if (bmvtype == BMV_TYPE_DIRECT) {
4253 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4254 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4256 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4257 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4258 mb_has_coeffs = !(idx_mbmode & 2);
4261 bmvtype = BMV_TYPE_FORWARD;
4262 v->bmvtype = bmvtype;
4263 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4264 for (i = 0; i < 6; i++) {
4266 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4267 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4268 val = ((v->fourmvbp >> (3 - i)) & 1);
4270 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4271 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4272 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4274 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4275 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4277 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4279 mb_has_coeffs = idx_mbmode & 1;
4282 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4286 s->current_picture.f.qscale_table[mb_pos] = mquant;
4287 if (!v->ttmbf && cbp) {
4288 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4291 for (i = 0; i < 6; i++) {
4292 s->dc_val[0][s->block_index[i]] = 0;
4294 val = ((cbp >> (5 - i)) & 1);
4295 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4296 if (v->second_field)
4297 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4299 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4300 first_block, s->dest[dst_idx] + off,
4301 (i & 4) ? s->uvlinesize : s->linesize,
4302 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4303 if (!v->ttmbf && ttmb < 8)
4311 /** Decode blocks of I-frame
4313 static void vc1_decode_i_blocks(VC1Context *v)
4316 MpegEncContext *s = &v->s;
4321 /* select codingmode used for VLC tables selection */
4322 switch (v->y_ac_table_index) {
4324 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4327 v->codingset = CS_HIGH_MOT_INTRA;
4330 v->codingset = CS_MID_RATE_INTRA;
4334 switch (v->c_ac_table_index) {
4336 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4339 v->codingset2 = CS_HIGH_MOT_INTER;
4342 v->codingset2 = CS_MID_RATE_INTER;
4346 /* Set DC scale - y and c use the same */
4347 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4348 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4351 s->mb_x = s->mb_y = 0;
4353 s->first_slice_line = 1;
4354 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4356 ff_init_block_index(s);
4357 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4359 ff_update_block_index(s);
4360 dst[0] = s->dest[0];
4361 dst[1] = dst[0] + 8;
4362 dst[2] = s->dest[0] + s->linesize * 8;
4363 dst[3] = dst[2] + 8;
4364 dst[4] = s->dest[1];
4365 dst[5] = s->dest[2];
4366 s->dsp.clear_blocks(s->block[0]);
4367 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4368 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4369 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4370 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4371 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4373 // do actual MB decoding and displaying
4374 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4375 v->s.ac_pred = get_bits1(&v->s.gb);
4377 for (k = 0; k < 6; k++) {
4378 val = ((cbp >> (5 - k)) & 1);
4381 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4385 cbp |= val << (5 - k);
4387 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4389 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4391 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4392 if (v->pq >= 9 && v->overlap) {
4394 for (j = 0; j < 64; j++)
4395 s->block[k][j] <<= 1;
4396 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4399 for (j = 0; j < 64; j++)
4400 s->block[k][j] = (s->block[k][j] - 64) << 1;
4401 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4405 if (v->pq >= 9 && v->overlap) {
4407 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4408 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4409 if (!(s->flags & CODEC_FLAG_GRAY)) {
4410 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4411 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4414 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4415 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4416 if (!s->first_slice_line) {
4417 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4418 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4419 if (!(s->flags & CODEC_FLAG_GRAY)) {
4420 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4421 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4424 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4425 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4427 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4429 if (get_bits_count(&s->gb) > v->bits) {
4430 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4431 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4432 get_bits_count(&s->gb), v->bits);
4436 if (!v->s.loop_filter)
4437 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4439 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4441 s->first_slice_line = 0;
4443 if (v->s.loop_filter)
4444 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4446 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4447 * profile, these only differ are when decoding MSS2 rectangles. */
4448 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4451 /** Decode blocks of I-frame for advanced profile
4453 static void vc1_decode_i_blocks_adv(VC1Context *v)
4456 MpegEncContext *s = &v->s;
4462 GetBitContext *gb = &s->gb;
4464 /* select codingmode used for VLC tables selection */
4465 switch (v->y_ac_table_index) {
4467 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4470 v->codingset = CS_HIGH_MOT_INTRA;
4473 v->codingset = CS_MID_RATE_INTRA;
4477 switch (v->c_ac_table_index) {
4479 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4482 v->codingset2 = CS_HIGH_MOT_INTER;
4485 v->codingset2 = CS_MID_RATE_INTER;
4490 s->mb_x = s->mb_y = 0;
4492 s->first_slice_line = 1;
4493 s->mb_y = s->start_mb_y;
4494 if (s->start_mb_y) {
4496 ff_init_block_index(s);
4497 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4498 (1 + s->b8_stride) * sizeof(*s->coded_block));
4500 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4502 ff_init_block_index(s);
4503 for (;s->mb_x < s->mb_width; s->mb_x++) {
4504 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4505 ff_update_block_index(s);
4506 s->dsp.clear_blocks(block[0]);
4507 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4508 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4509 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4510 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4512 // do actual MB decoding and displaying
4513 if (v->fieldtx_is_raw)
4514 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4515 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4516 if ( v->acpred_is_raw)
4517 v->s.ac_pred = get_bits1(&v->s.gb);
4519 v->s.ac_pred = v->acpred_plane[mb_pos];
4521 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4522 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4526 s->current_picture.f.qscale_table[mb_pos] = mquant;
4527 /* Set DC scale - y and c use the same */
4528 s->y_dc_scale = s->y_dc_scale_table[mquant];
4529 s->c_dc_scale = s->c_dc_scale_table[mquant];
4531 for (k = 0; k < 6; k++) {
4532 val = ((cbp >> (5 - k)) & 1);
4535 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4539 cbp |= val << (5 - k);
4541 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4542 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4544 vc1_decode_i_block_adv(v, block[k], k, val,
4545 (k < 4) ? v->codingset : v->codingset2, mquant);
4547 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4549 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4552 vc1_smooth_overlap_filter_iblk(v);
4553 vc1_put_signed_blocks_clamped(v);
4554 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4556 if (get_bits_count(&s->gb) > v->bits) {
4557 // TODO: may need modification to handle slice coding
4558 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4559 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4560 get_bits_count(&s->gb), v->bits);
4564 if (!v->s.loop_filter)
4565 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4567 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4568 s->first_slice_line = 0;
4571 /* raw bottom MB row */
4573 ff_init_block_index(s);
4574 for (;s->mb_x < s->mb_width; s->mb_x++) {
4575 ff_update_block_index(s);
4576 vc1_put_signed_blocks_clamped(v);
4577 if (v->s.loop_filter)
4578 vc1_loop_filter_iblk_delayed(v, v->pq);
4580 if (v->s.loop_filter)
4581 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4582 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4583 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4586 static void vc1_decode_p_blocks(VC1Context *v)
4588 MpegEncContext *s = &v->s;
4589 int apply_loop_filter;
4591 /* select codingmode used for VLC tables selection */
4592 switch (v->c_ac_table_index) {
4594 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4597 v->codingset = CS_HIGH_MOT_INTRA;
4600 v->codingset = CS_MID_RATE_INTRA;
4604 switch (v->c_ac_table_index) {
4606 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4609 v->codingset2 = CS_HIGH_MOT_INTER;
4612 v->codingset2 = CS_MID_RATE_INTER;
4616 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4617 s->first_slice_line = 1;
4618 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4619 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4621 ff_init_block_index(s);
4622 for (; s->mb_x < s->mb_width; s->mb_x++) {
4623 ff_update_block_index(s);
4625 if (v->fcm == ILACE_FIELD)
4626 vc1_decode_p_mb_intfi(v);
4627 else if (v->fcm == ILACE_FRAME)
4628 vc1_decode_p_mb_intfr(v);
4629 else vc1_decode_p_mb(v);
4630 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4631 vc1_apply_p_loop_filter(v);
4632 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4633 // TODO: may need modification to handle slice coding
4634 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4635 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4636 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4640 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4641 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4642 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4643 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4644 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4645 s->first_slice_line = 0;
4647 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4649 ff_init_block_index(s);
4650 for (; s->mb_x < s->mb_width; s->mb_x++) {
4651 ff_update_block_index(s);
4652 vc1_apply_p_loop_filter(v);
4655 if (s->end_mb_y >= s->start_mb_y)
4656 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4657 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4658 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4661 static void vc1_decode_b_blocks(VC1Context *v)
4663 MpegEncContext *s = &v->s;
4665 /* select codingmode used for VLC tables selection */
4666 switch (v->c_ac_table_index) {
4668 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4671 v->codingset = CS_HIGH_MOT_INTRA;
4674 v->codingset = CS_MID_RATE_INTRA;
4678 switch (v->c_ac_table_index) {
4680 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4683 v->codingset2 = CS_HIGH_MOT_INTER;
4686 v->codingset2 = CS_MID_RATE_INTER;
4690 s->first_slice_line = 1;
4691 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4693 ff_init_block_index(s);
4694 for (; s->mb_x < s->mb_width; s->mb_x++) {
4695 ff_update_block_index(s);
4697 if (v->fcm == ILACE_FIELD)
4698 vc1_decode_b_mb_intfi(v);
4701 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4702 // TODO: may need modification to handle slice coding
4703 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4704 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4705 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4708 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4710 if (!v->s.loop_filter)
4711 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4713 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4714 s->first_slice_line = 0;
4716 if (v->s.loop_filter)
4717 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4718 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4719 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4722 static void vc1_decode_skip_blocks(VC1Context *v)
4724 MpegEncContext *s = &v->s;
4726 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4727 s->first_slice_line = 1;
4728 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4730 ff_init_block_index(s);
4731 ff_update_block_index(s);
4732 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4733 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4734 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4735 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4736 s->first_slice_line = 0;
4738 s->pict_type = AV_PICTURE_TYPE_P;
4741 void ff_vc1_decode_blocks(VC1Context *v)
4744 v->s.esc3_level_length = 0;
4746 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4749 v->left_blk_idx = -1;
4750 v->topleft_blk_idx = 1;
4752 switch (v->s.pict_type) {
4753 case AV_PICTURE_TYPE_I:
4754 if (v->profile == PROFILE_ADVANCED)
4755 vc1_decode_i_blocks_adv(v);
4757 vc1_decode_i_blocks(v);
4759 case AV_PICTURE_TYPE_P:
4760 if (v->p_frame_skipped)
4761 vc1_decode_skip_blocks(v);
4763 vc1_decode_p_blocks(v);
4765 case AV_PICTURE_TYPE_B:
4767 if (v->profile == PROFILE_ADVANCED)
4768 vc1_decode_i_blocks_adv(v);
4770 vc1_decode_i_blocks(v);
4772 vc1_decode_b_blocks(v);
4778 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4782 * Transform coefficients for both sprites in 16.16 fixed point format,
4783 * in the order they appear in the bitstream:
4785 * rotation 1 (unused)
4787 * rotation 2 (unused)
4794 int effect_type, effect_flag;
4795 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4796 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4799 static inline int get_fp_val(GetBitContext* gb)
4801 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4804 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4808 switch (get_bits(gb, 2)) {
4811 c[2] = get_fp_val(gb);
4815 c[0] = c[4] = get_fp_val(gb);
4816 c[2] = get_fp_val(gb);
4819 c[0] = get_fp_val(gb);
4820 c[2] = get_fp_val(gb);
4821 c[4] = get_fp_val(gb);
4824 c[0] = get_fp_val(gb);
4825 c[1] = get_fp_val(gb);
4826 c[2] = get_fp_val(gb);
4827 c[3] = get_fp_val(gb);
4828 c[4] = get_fp_val(gb);
4831 c[5] = get_fp_val(gb);
4833 c[6] = get_fp_val(gb);
4838 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4840 AVCodecContext *avctx = v->s.avctx;
4843 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4844 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4845 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4846 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4847 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4848 for (i = 0; i < 7; i++)
4849 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4850 sd->coefs[sprite][i] / (1<<16),
4851 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4852 av_log(avctx, AV_LOG_DEBUG, "\n");
4856 if (sd->effect_type = get_bits_long(gb, 30)) {
4857 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4859 vc1_sprite_parse_transform(gb, sd->effect_params1);
4862 vc1_sprite_parse_transform(gb, sd->effect_params1);
4863 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4866 for (i = 0; i < sd->effect_pcount1; i++)
4867 sd->effect_params1[i] = get_fp_val(gb);
4869 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4870 // effect 13 is simple alpha blending and matches the opacity above
4871 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4872 for (i = 0; i < sd->effect_pcount1; i++)
4873 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4874 sd->effect_params1[i] / (1 << 16),
4875 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4876 av_log(avctx, AV_LOG_DEBUG, "\n");
4879 sd->effect_pcount2 = get_bits(gb, 16);
4880 if (sd->effect_pcount2 > 10) {
4881 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4883 } else if (sd->effect_pcount2) {
4885 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4886 while (++i < sd->effect_pcount2) {
4887 sd->effect_params2[i] = get_fp_val(gb);
4888 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4889 sd->effect_params2[i] / (1 << 16),
4890 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4892 av_log(avctx, AV_LOG_DEBUG, "\n");
4895 if (sd->effect_flag = get_bits1(gb))
4896 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4898 if (get_bits_count(gb) >= gb->size_in_bits +
4899 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4900 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4901 if (get_bits_count(gb) < gb->size_in_bits - 8)
4902 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4905 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4907 int i, plane, row, sprite;
4908 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4909 uint8_t* src_h[2][2];
4910 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4912 MpegEncContext *s = &v->s;
4914 for (i = 0; i < 2; i++) {
4915 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4916 xadv[i] = sd->coefs[i][0];
4917 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4918 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4920 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4921 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4923 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4925 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4926 int width = v->output_width>>!!plane;
4928 for (row = 0; row < v->output_height>>!!plane; row++) {
4929 uint8_t *dst = v->sprite_output_frame.data[plane] +
4930 v->sprite_output_frame.linesize[plane] * row;
4932 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4933 uint8_t *iplane = s->current_picture.f.data[plane];
4934 int iline = s->current_picture.f.linesize[plane];
4935 int ycoord = yoff[sprite] + yadv[sprite] * row;
4936 int yline = ycoord >> 16;
4938 ysub[sprite] = ycoord & 0xFFFF;
4940 iplane = s->last_picture.f.data[plane];
4941 iline = s->last_picture.f.linesize[plane];
4943 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4944 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4945 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4947 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4949 if (sr_cache[sprite][0] != yline) {
4950 if (sr_cache[sprite][1] == yline) {
4951 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4952 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4954 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4955 sr_cache[sprite][0] = yline;
4958 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4959 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4960 iplane + next_line, xoff[sprite],
4961 xadv[sprite], width);
4962 sr_cache[sprite][1] = yline + 1;
4964 src_h[sprite][0] = v->sr_rows[sprite][0];
4965 src_h[sprite][1] = v->sr_rows[sprite][1];
4969 if (!v->two_sprites) {
4971 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4973 memcpy(dst, src_h[0][0], width);
4976 if (ysub[0] && ysub[1]) {
4977 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4978 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4979 } else if (ysub[0]) {
4980 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
4981 src_h[1][0], alpha, width);
4982 } else if (ysub[1]) {
4983 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
4984 src_h[0][0], (1<<16)-1-alpha, width);
4986 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
4992 for (i = 0; i < 2; i++) {
5002 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5004 MpegEncContext *s = &v->s;
5005 AVCodecContext *avctx = s->avctx;
5008 vc1_parse_sprites(v, gb, &sd);
5010 if (!s->current_picture.f.data[0]) {
5011 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5015 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5016 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5020 if (v->sprite_output_frame.data[0])
5021 avctx->release_buffer(avctx, &v->sprite_output_frame);
5023 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5024 v->sprite_output_frame.reference = 0;
5025 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
5026 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5030 vc1_draw_sprites(v, &sd);
5035 static void vc1_sprite_flush(AVCodecContext *avctx)
5037 VC1Context *v = avctx->priv_data;
5038 MpegEncContext *s = &v->s;
5039 AVFrame *f = &s->current_picture.f;
5042 /* Windows Media Image codecs have a convergence interval of two keyframes.
5043 Since we can't enforce it, clear to black the missing sprite. This is
5044 wrong but it looks better than doing nothing. */
5047 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5048 for (i = 0; i < v->sprite_height>>!!plane; i++)
5049 memset(f->data[plane] + i * f->linesize[plane],
5050 plane ? 128 : 0, f->linesize[plane]);
5055 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5057 MpegEncContext *s = &v->s;
5060 /* Allocate mb bitplanes */
5061 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5062 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5063 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5064 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5065 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5066 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5068 v->n_allocated_blks = s->mb_width + 2;
5069 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5070 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5071 v->cbp = v->cbp_base + s->mb_stride;
5072 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5073 v->ttblk = v->ttblk_base + s->mb_stride;
5074 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5075 v->is_intra = v->is_intra_base + s->mb_stride;
5076 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5077 v->luma_mv = v->luma_mv_base + s->mb_stride;
5079 /* allocate block type info in that way so it could be used with s->block_index[] */
5080 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5081 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5082 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5083 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5085 /* allocate memory to store block level MV info */
5086 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5087 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5088 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5089 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5090 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5091 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5092 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5093 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);
5094 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5095 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5096 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);
5098 /* Init coded blocks info */
5099 if (v->profile == PROFILE_ADVANCED) {
5100 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5102 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5106 ff_intrax8_common_init(&v->x8,s);
5108 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5109 for (i = 0; i < 4; i++)
5110 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5113 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5114 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5121 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5124 for (i = 0; i < 64; i++) {
5125 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5126 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5127 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5128 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5129 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5130 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5136 /** Initialize a VC1/WMV3 decoder
5137 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5138 * @todo TODO: Decypher remaining bits in extra_data
5140 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5142 VC1Context *v = avctx->priv_data;
5143 MpegEncContext *s = &v->s;
5146 /* save the container output size for WMImage */
5147 v->output_width = avctx->width;
5148 v->output_height = avctx->height;
5150 if (!avctx->extradata_size || !avctx->extradata)
5152 if (!(avctx->flags & CODEC_FLAG_GRAY))
5153 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5155 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5156 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5158 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5159 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5161 if (avctx->idct_algo == FF_IDCT_AUTO) {
5162 avctx->idct_algo = FF_IDCT_WMV2;
5165 if (ff_vc1_init_common(v) < 0)
5167 ff_vc1dsp_init(&v->vc1dsp);
5169 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5172 // looks like WMV3 has a sequence header stored in the extradata
5173 // advanced sequence header may be before the first frame
5174 // the last byte of the extradata is a version number, 1 for the
5175 // samples we can decode
5177 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5179 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5182 count = avctx->extradata_size*8 - get_bits_count(&gb);
5184 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5185 count, get_bits(&gb, count));
5186 } else if (count < 0) {
5187 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5189 } else { // VC1/WVC1/WVP2
5190 const uint8_t *start = avctx->extradata;
5191 uint8_t *end = avctx->extradata + avctx->extradata_size;
5192 const uint8_t *next;
5193 int size, buf2_size;
5194 uint8_t *buf2 = NULL;
5195 int seq_initialized = 0, ep_initialized = 0;
5197 if (avctx->extradata_size < 16) {
5198 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5202 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5203 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5205 for (; next < end; start = next) {
5206 next = find_next_marker(start + 4, end);
5207 size = next - start - 4;
5210 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5211 init_get_bits(&gb, buf2, buf2_size * 8);
5212 switch (AV_RB32(start)) {
5213 case VC1_CODE_SEQHDR:
5214 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5218 seq_initialized = 1;
5220 case VC1_CODE_ENTRYPOINT:
5221 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5230 if (!seq_initialized || !ep_initialized) {
5231 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5234 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5237 avctx->profile = v->profile;
5238 if (v->profile == PROFILE_ADVANCED)
5239 avctx->level = v->level;
5241 avctx->has_b_frames = !!avctx->max_b_frames;
5243 s->mb_width = (avctx->coded_width + 15) >> 4;
5244 s->mb_height = (avctx->coded_height + 15) >> 4;
5246 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5247 ff_vc1_init_transposed_scantables(v);
5249 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5254 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5255 v->sprite_width = avctx->coded_width;
5256 v->sprite_height = avctx->coded_height;
5258 avctx->coded_width = avctx->width = v->output_width;
5259 avctx->coded_height = avctx->height = v->output_height;
5261 // prevent 16.16 overflows
5262 if (v->sprite_width > 1 << 14 ||
5263 v->sprite_height > 1 << 14 ||
5264 v->output_width > 1 << 14 ||
5265 v->output_height > 1 << 14) return -1;
5270 /** Close a VC1/WMV3 decoder
5271 * @warning Initial try at using MpegEncContext stuff
5273 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5275 VC1Context *v = avctx->priv_data;
5278 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5279 && v->sprite_output_frame.data[0])
5280 avctx->release_buffer(avctx, &v->sprite_output_frame);
5281 for (i = 0; i < 4; i++)
5282 av_freep(&v->sr_rows[i >> 1][i & 1]);
5283 av_freep(&v->hrd_rate);
5284 av_freep(&v->hrd_buffer);
5285 ff_MPV_common_end(&v->s);
5286 av_freep(&v->mv_type_mb_plane);
5287 av_freep(&v->direct_mb_plane);
5288 av_freep(&v->forward_mb_plane);
5289 av_freep(&v->fieldtx_plane);
5290 av_freep(&v->acpred_plane);
5291 av_freep(&v->over_flags_plane);
5292 av_freep(&v->mb_type_base);
5293 av_freep(&v->blk_mv_type_base);
5294 av_freep(&v->mv_f_base);
5295 av_freep(&v->mv_f_last_base);
5296 av_freep(&v->mv_f_next_base);
5297 av_freep(&v->block);
5298 av_freep(&v->cbp_base);
5299 av_freep(&v->ttblk_base);
5300 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5301 av_freep(&v->luma_mv_base);
5302 ff_intrax8_common_end(&v->x8);
5307 /** Decode a VC1/WMV3 frame
5308 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5310 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5311 int *data_size, AVPacket *avpkt)
5313 const uint8_t *buf = avpkt->data;
5314 int buf_size = avpkt->size, n_slices = 0, i;
5315 VC1Context *v = avctx->priv_data;
5316 MpegEncContext *s = &v->s;
5317 AVFrame *pict = data;
5318 uint8_t *buf2 = NULL;
5319 const uint8_t *buf_start = buf;
5320 int mb_height, n_slices1=-1;
5325 } *slices = NULL, *tmp;
5327 if(s->flags & CODEC_FLAG_LOW_DELAY)
5330 /* no supplementary picture */
5331 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5332 /* special case for last picture */
5333 if (s->low_delay == 0 && s->next_picture_ptr) {
5334 *pict = s->next_picture_ptr->f;
5335 s->next_picture_ptr = NULL;
5337 *data_size = sizeof(AVFrame);
5343 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5344 if (v->profile < PROFILE_ADVANCED)
5345 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5347 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5350 //for advanced profile we may need to parse and unescape data
5351 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5353 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5355 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5356 const uint8_t *start, *end, *next;
5360 for (start = buf, end = buf + buf_size; next < end; start = next) {
5361 next = find_next_marker(start + 4, end);
5362 size = next - start - 4;
5363 if (size <= 0) continue;
5364 switch (AV_RB32(start)) {
5365 case VC1_CODE_FRAME:
5366 if (avctx->hwaccel ||
5367 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5369 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5371 case VC1_CODE_FIELD: {
5373 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5377 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5378 if (!slices[n_slices].buf)
5380 buf_size3 = vc1_unescape_buffer(start + 4, size,
5381 slices[n_slices].buf);
5382 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5384 /* assuming that the field marker is at the exact middle,
5385 hope it's correct */
5386 slices[n_slices].mby_start = s->mb_height >> 1;
5387 n_slices1 = n_slices - 1; // index of the last slice of the first field
5391 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5392 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5393 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5394 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5396 case VC1_CODE_SLICE: {
5398 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5402 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5403 if (!slices[n_slices].buf)
5405 buf_size3 = vc1_unescape_buffer(start + 4, size,
5406 slices[n_slices].buf);
5407 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5409 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5415 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5416 const uint8_t *divider;
5419 divider = find_next_marker(buf, buf + buf_size);
5420 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5421 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5423 } else { // found field marker, unescape second field
5424 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5428 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5429 if (!slices[n_slices].buf)
5431 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5432 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5434 slices[n_slices].mby_start = s->mb_height >> 1;
5435 n_slices1 = n_slices - 1;
5438 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5440 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5442 init_get_bits(&s->gb, buf2, buf_size2*8);
5444 init_get_bits(&s->gb, buf, buf_size*8);
5446 if (v->res_sprite) {
5447 v->new_sprite = !get_bits1(&s->gb);
5448 v->two_sprites = get_bits1(&s->gb);
5449 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5450 we're using the sprite compositor. These are intentionally kept separate
5451 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5452 the vc1 one for WVP2 */
5453 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5454 if (v->new_sprite) {
5455 // switch AVCodecContext parameters to those of the sprites
5456 avctx->width = avctx->coded_width = v->sprite_width;
5457 avctx->height = avctx->coded_height = v->sprite_height;
5464 if (s->context_initialized &&
5465 (s->width != avctx->coded_width ||
5466 s->height != avctx->coded_height)) {
5467 ff_vc1_decode_end(avctx);
5470 if (!s->context_initialized) {
5471 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5474 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5476 if (v->profile == PROFILE_ADVANCED) {
5477 s->h_edge_pos = avctx->coded_width;
5478 s->v_edge_pos = avctx->coded_height;
5482 /* We need to set current_picture_ptr before reading the header,
5483 * otherwise we cannot store anything in there. */
5484 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5485 int i = ff_find_unused_picture(s, 0);
5488 s->current_picture_ptr = &s->picture[i];
5491 // do parse frame header
5492 v->pic_header_flag = 0;
5493 if (v->profile < PROFILE_ADVANCED) {
5494 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5498 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5503 if (avctx->debug & FF_DEBUG_PICT_INFO)
5504 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5506 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5507 && s->pict_type != AV_PICTURE_TYPE_I) {
5508 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5512 // process pulldown flags
5513 s->current_picture_ptr->f.repeat_pict = 0;
5514 // Pulldown flags are only valid when 'broadcast' has been set.
5515 // So ticks_per_frame will be 2
5518 s->current_picture_ptr->f.repeat_pict = 1;
5519 } else if (v->rptfrm) {
5521 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5524 // for skipping the frame
5525 s->current_picture.f.pict_type = s->pict_type;
5526 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5528 /* skip B-frames if we don't have reference frames */
5529 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
5532 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5533 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5534 avctx->skip_frame >= AVDISCARD_ALL) {
5538 if (s->next_p_frame_damaged) {
5539 if (s->pict_type == AV_PICTURE_TYPE_B)
5542 s->next_p_frame_damaged = 0;
5545 if (ff_MPV_frame_start(s, avctx) < 0) {
5549 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5550 v->s.current_picture_ptr->f.top_field_first = v->tff;
5552 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5553 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5555 if ((CONFIG_VC1_VDPAU_DECODER)
5556 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5557 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5558 else if (avctx->hwaccel) {
5559 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
5561 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5563 if (avctx->hwaccel->end_frame(avctx) < 0)
5566 ff_er_frame_start(s);
5568 v->bits = buf_size * 8;
5569 v->end_mb_x = s->mb_width;
5570 if (v->field_mode) {
5572 s->current_picture.f.linesize[0] <<= 1;
5573 s->current_picture.f.linesize[1] <<= 1;
5574 s->current_picture.f.linesize[2] <<= 1;
5576 s->uvlinesize <<= 1;
5577 tmp[0] = v->mv_f_last[0];
5578 tmp[1] = v->mv_f_last[1];
5579 v->mv_f_last[0] = v->mv_f_next[0];
5580 v->mv_f_last[1] = v->mv_f_next[1];
5581 v->mv_f_next[0] = v->mv_f[0];
5582 v->mv_f_next[1] = v->mv_f[1];
5583 v->mv_f[0] = tmp[0];
5584 v->mv_f[1] = tmp[1];
5586 mb_height = s->mb_height >> v->field_mode;
5587 for (i = 0; i <= n_slices; i++) {
5588 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5589 if (v->field_mode <= 0) {
5590 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5591 "picture boundary (%d >= %d)\n", i,
5592 slices[i - 1].mby_start, mb_height);
5595 v->second_field = 1;
5596 v->blocks_off = s->mb_width * s->mb_height << 1;
5597 v->mb_off = s->mb_stride * s->mb_height >> 1;
5599 v->second_field = 0;
5604 v->pic_header_flag = 0;
5605 if (v->field_mode && i == n_slices1 + 2) {
5606 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5607 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5610 } else if (get_bits1(&s->gb)) {
5611 v->pic_header_flag = 1;
5612 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5613 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5618 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5619 if (!v->field_mode || v->second_field)
5620 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5622 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5623 if (s->end_mb_y <= s->start_mb_y) {
5624 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5627 ff_vc1_decode_blocks(v);
5629 s->gb = slices[i].gb;
5631 if (v->field_mode) {
5632 v->second_field = 0;
5633 if (s->pict_type == AV_PICTURE_TYPE_B) {
5634 memcpy(v->mv_f_base, v->mv_f_next_base,
5635 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5637 s->current_picture.f.linesize[0] >>= 1;
5638 s->current_picture.f.linesize[1] >>= 1;
5639 s->current_picture.f.linesize[2] >>= 1;
5641 s->uvlinesize >>= 1;
5643 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5644 get_bits_count(&s->gb), s->gb.size_in_bits);
5645 // if (get_bits_count(&s->gb) > buf_size * 8)
5647 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5653 ff_MPV_frame_end(s);
5655 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5657 avctx->width = avctx->coded_width = v->output_width;
5658 avctx->height = avctx->coded_height = v->output_height;
5659 if (avctx->skip_frame >= AVDISCARD_NONREF)
5661 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5662 if (vc1_decode_sprites(v, &s->gb))
5665 *pict = v->sprite_output_frame;
5666 *data_size = sizeof(AVFrame);
5668 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5669 *pict = s->current_picture_ptr->f;
5670 } else if (s->last_picture_ptr != NULL) {
5671 *pict = s->last_picture_ptr->f;
5673 if (s->last_picture_ptr || s->low_delay) {
5674 *data_size = sizeof(AVFrame);
5675 ff_print_debug_info(s, pict);
5681 for (i = 0; i < n_slices; i++)
5682 av_free(slices[i].buf);
5688 for (i = 0; i < n_slices; i++)
5689 av_free(slices[i].buf);
5695 static const AVProfile profiles[] = {
5696 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5697 { FF_PROFILE_VC1_MAIN, "Main" },
5698 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5699 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5700 { FF_PROFILE_UNKNOWN },
5703 AVCodec ff_vc1_decoder = {
5705 .type = AVMEDIA_TYPE_VIDEO,
5706 .id = AV_CODEC_ID_VC1,
5707 .priv_data_size = sizeof(VC1Context),
5708 .init = vc1_decode_init,
5709 .close = ff_vc1_decode_end,
5710 .decode = vc1_decode_frame,
5711 .flush = ff_mpeg_flush,
5712 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5713 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5714 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5715 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5718 #if CONFIG_WMV3_DECODER
5719 AVCodec ff_wmv3_decoder = {
5721 .type = AVMEDIA_TYPE_VIDEO,
5722 .id = AV_CODEC_ID_WMV3,
5723 .priv_data_size = sizeof(VC1Context),
5724 .init = vc1_decode_init,
5725 .close = ff_vc1_decode_end,
5726 .decode = vc1_decode_frame,
5727 .flush = ff_mpeg_flush,
5728 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5729 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5730 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5731 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5735 #if CONFIG_WMV3_VDPAU_DECODER
5736 AVCodec ff_wmv3_vdpau_decoder = {
5737 .name = "wmv3_vdpau",
5738 .type = AVMEDIA_TYPE_VIDEO,
5739 .id = AV_CODEC_ID_WMV3,
5740 .priv_data_size = sizeof(VC1Context),
5741 .init = vc1_decode_init,
5742 .close = ff_vc1_decode_end,
5743 .decode = vc1_decode_frame,
5744 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5745 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5746 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5747 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5751 #if CONFIG_VC1_VDPAU_DECODER
5752 AVCodec ff_vc1_vdpau_decoder = {
5753 .name = "vc1_vdpau",
5754 .type = AVMEDIA_TYPE_VIDEO,
5755 .id = AV_CODEC_ID_VC1,
5756 .priv_data_size = sizeof(VC1Context),
5757 .init = vc1_decode_init,
5758 .close = ff_vc1_decode_end,
5759 .decode = vc1_decode_frame,
5760 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5761 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5762 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5763 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5767 #if CONFIG_WMV3IMAGE_DECODER
5768 AVCodec ff_wmv3image_decoder = {
5769 .name = "wmv3image",
5770 .type = AVMEDIA_TYPE_VIDEO,
5771 .id = AV_CODEC_ID_WMV3IMAGE,
5772 .priv_data_size = sizeof(VC1Context),
5773 .init = vc1_decode_init,
5774 .close = ff_vc1_decode_end,
5775 .decode = vc1_decode_frame,
5776 .capabilities = CODEC_CAP_DR1,
5777 .flush = vc1_sprite_flush,
5778 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5779 .pix_fmts = ff_pixfmt_list_420
5783 #if CONFIG_VC1IMAGE_DECODER
5784 AVCodec ff_vc1image_decoder = {
5786 .type = AVMEDIA_TYPE_VIDEO,
5787 .id = AV_CODEC_ID_VC1IMAGE,
5788 .priv_data_size = sizeof(VC1Context),
5789 .init = vc1_decode_init,
5790 .close = ff_vc1_decode_end,
5791 .decode = vc1_decode_frame,
5792 .capabilities = CODEC_CAP_DR1,
5793 .flush = vc1_sprite_flush,
5794 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5795 .pix_fmts = ff_pixfmt_list_420