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) {
1464 // REFFIELD determines if the last field or the second-last field is
1465 // to be used as reference
1466 opposite = 1 - v->reffield;
1468 if (num_samefield <= num_oppfield)
1469 opposite = 1 - pred_flag;
1471 opposite = pred_flag;
1476 if (a_valid && !a_f) {
1477 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1478 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1480 if (b_valid && !b_f) {
1481 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1482 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1484 if (c_valid && !c_f) {
1485 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1486 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1488 v->mv_f[dir][xy + v->blocks_off] = 1;
1489 v->ref_field_type[dir] = !v->cur_field_type;
1491 if (a_valid && a_f) {
1492 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1493 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1495 if (b_valid && b_f) {
1496 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1497 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1499 if (c_valid && c_f) {
1500 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1501 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1503 v->mv_f[dir][xy + v->blocks_off] = 0;
1504 v->ref_field_type[dir] = v->cur_field_type;
1508 px = field_predA[0];
1509 py = field_predA[1];
1510 } else if (c_valid) {
1511 px = field_predC[0];
1512 py = field_predC[1];
1513 } else if (b_valid) {
1514 px = field_predB[0];
1515 py = field_predB[1];
1521 if (num_samefield + num_oppfield > 1) {
1522 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1523 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1526 /* Pullback MV as specified in 8.3.5.3.4 */
1527 if (!v->field_mode) {
1529 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1530 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1531 X = (s->mb_width << 6) - 4;
1532 Y = (s->mb_height << 6) - 4;
1534 if (qx + px < -60) px = -60 - qx;
1535 if (qy + py < -60) py = -60 - qy;
1537 if (qx + px < -28) px = -28 - qx;
1538 if (qy + py < -28) py = -28 - qy;
1540 if (qx + px > X) px = X - qx;
1541 if (qy + py > Y) py = Y - qy;
1544 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1545 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1546 hybridmv_thresh = 32;
1547 if (a_valid && c_valid) {
1548 if (is_intra[xy - wrap])
1549 sum = FFABS(px) + FFABS(py);
1551 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1552 if (sum > hybridmv_thresh) {
1553 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1554 px = field_predA[0];
1555 py = field_predA[1];
1557 px = field_predC[0];
1558 py = field_predC[1];
1561 if (is_intra[xy - 1])
1562 sum = FFABS(px) + FFABS(py);
1564 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1565 if (sum > hybridmv_thresh) {
1566 if (get_bits1(&s->gb)) {
1567 px = field_predA[0];
1568 py = field_predA[1];
1570 px = field_predC[0];
1571 py = field_predC[1];
1578 if (v->field_mode && !s->quarter_sample) {
1582 if (v->field_mode && v->numref)
1584 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1586 /* store MV using signed modulus of MV range defined in 4.11 */
1587 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;
1588 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;
1589 if (mv1) { /* duplicate motion data for 1-MV block */
1590 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];
1591 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];
1592 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];
1593 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];
1594 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];
1595 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];
1596 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1597 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];
1601 /** Predict and set motion vector for interlaced frame picture MBs
1603 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1604 int mvn, int r_x, int r_y, uint8_t* is_intra)
1606 MpegEncContext *s = &v->s;
1607 int xy, wrap, off = 0;
1608 int A[2], B[2], C[2];
1610 int a_valid = 0, b_valid = 0, c_valid = 0;
1611 int field_a, field_b, field_c; // 0: same, 1: opposit
1612 int total_valid, num_samefield, num_oppfield;
1613 int pos_c, pos_b, n_adj;
1615 wrap = s->b8_stride;
1616 xy = s->block_index[n];
1619 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1620 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1621 s->current_picture.f.motion_val[1][xy][0] = 0;
1622 s->current_picture.f.motion_val[1][xy][1] = 0;
1623 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1624 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1625 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1626 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1627 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1628 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1629 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1630 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1631 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1632 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1633 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1634 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1635 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1636 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1641 off = ((n == 0) || (n == 1)) ? 1 : -1;
1643 if (s->mb_x || (n == 1) || (n == 3)) {
1644 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1645 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1646 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1647 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1649 } else { // current block has frame mv and cand. has field MV (so average)
1650 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1651 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1652 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1653 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1656 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1662 /* Predict B and C */
1663 B[0] = B[1] = C[0] = C[1] = 0;
1664 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1665 if (!s->first_slice_line) {
1666 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1669 pos_b = s->block_index[n_adj] - 2 * wrap;
1670 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1671 n_adj = (n & 2) | (n & 1);
1673 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1674 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1675 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1676 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1677 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1680 if (s->mb_width > 1) {
1681 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1684 pos_c = s->block_index[2] - 2 * wrap + 2;
1685 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1688 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1689 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1690 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1691 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1692 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1694 if (s->mb_x == s->mb_width - 1) {
1695 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1698 pos_c = s->block_index[3] - 2 * wrap - 2;
1699 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1702 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1703 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1704 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1705 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1706 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1715 pos_b = s->block_index[1];
1717 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1718 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1719 pos_c = s->block_index[0];
1721 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1722 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1725 total_valid = a_valid + b_valid + c_valid;
1726 // check if predictor A is out of bounds
1727 if (!s->mb_x && !(n == 1 || n == 3)) {
1730 // check if predictor B is out of bounds
1731 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1732 B[0] = B[1] = C[0] = C[1] = 0;
1734 if (!v->blk_mv_type[xy]) {
1735 if (s->mb_width == 1) {
1739 if (total_valid >= 2) {
1740 px = mid_pred(A[0], B[0], C[0]);
1741 py = mid_pred(A[1], B[1], C[1]);
1742 } else if (total_valid) {
1743 if (a_valid) { px = A[0]; py = A[1]; }
1744 if (b_valid) { px = B[0]; py = B[1]; }
1745 if (c_valid) { px = C[0]; py = C[1]; }
1751 field_a = (A[1] & 4) ? 1 : 0;
1755 field_b = (B[1] & 4) ? 1 : 0;
1759 field_c = (C[1] & 4) ? 1 : 0;
1763 num_oppfield = field_a + field_b + field_c;
1764 num_samefield = total_valid - num_oppfield;
1765 if (total_valid == 3) {
1766 if ((num_samefield == 3) || (num_oppfield == 3)) {
1767 px = mid_pred(A[0], B[0], C[0]);
1768 py = mid_pred(A[1], B[1], C[1]);
1769 } else if (num_samefield >= num_oppfield) {
1770 /* take one MV from same field set depending on priority
1771 the check for B may not be necessary */
1772 px = !field_a ? A[0] : B[0];
1773 py = !field_a ? A[1] : B[1];
1775 px = field_a ? A[0] : B[0];
1776 py = field_a ? A[1] : B[1];
1778 } else if (total_valid == 2) {
1779 if (num_samefield >= num_oppfield) {
1780 if (!field_a && a_valid) {
1783 } else if (!field_b && b_valid) {
1786 } else if (c_valid) {
1791 if (field_a && a_valid) {
1794 } else if (field_b && b_valid) {
1797 } else if (c_valid) {
1802 } else if (total_valid == 1) {
1803 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1804 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1809 /* store MV using signed modulus of MV range defined in 4.11 */
1810 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;
1811 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;
1812 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1813 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1814 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1815 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1816 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1817 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1818 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1819 } else if (mvn == 2) { /* duplicate motion data for 2-Field MV block */
1820 s->current_picture.f.motion_val[0][xy + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1821 s->current_picture.f.motion_val[0][xy + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1822 s->mv[0][n + 1][0] = s->mv[0][n][0];
1823 s->mv[0][n + 1][1] = s->mv[0][n][1];
1827 /** Motion compensation for direct or interpolated blocks in B-frames
1829 static void vc1_interp_mc(VC1Context *v)
1831 MpegEncContext *s = &v->s;
1832 DSPContext *dsp = &v->s.dsp;
1833 uint8_t *srcY, *srcU, *srcV;
1834 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1836 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1838 if (!v->field_mode && !v->s.next_picture.f.data[0])
1841 mx = s->mv[1][0][0];
1842 my = s->mv[1][0][1];
1843 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1844 uvmy = (my + ((my & 3) == 3)) >> 1;
1845 if (v->field_mode) {
1846 if (v->cur_field_type != v->ref_field_type[1])
1847 my = my - 2 + 4 * v->cur_field_type;
1848 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1851 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1852 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1854 srcY = s->next_picture.f.data[0];
1855 srcU = s->next_picture.f.data[1];
1856 srcV = s->next_picture.f.data[2];
1858 src_x = s->mb_x * 16 + (mx >> 2);
1859 src_y = s->mb_y * 16 + (my >> 2);
1860 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1861 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1863 if (v->profile != PROFILE_ADVANCED) {
1864 src_x = av_clip( src_x, -16, s->mb_width * 16);
1865 src_y = av_clip( src_y, -16, s->mb_height * 16);
1866 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1867 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1869 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1870 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1871 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1872 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1875 srcY += src_y * s->linesize + src_x;
1876 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1877 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1879 if (v->field_mode && v->ref_field_type[1]) {
1880 srcY += s->current_picture_ptr->f.linesize[0];
1881 srcU += s->current_picture_ptr->f.linesize[1];
1882 srcV += s->current_picture_ptr->f.linesize[2];
1885 /* for grayscale we should not try to read from unknown area */
1886 if (s->flags & CODEC_FLAG_GRAY) {
1887 srcU = s->edge_emu_buffer + 18 * s->linesize;
1888 srcV = s->edge_emu_buffer + 18 * s->linesize;
1891 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1892 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1893 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1894 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1896 srcY -= s->mspel * (1 + s->linesize);
1897 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1898 17 + s->mspel * 2, 17 + s->mspel * 2,
1899 src_x - s->mspel, src_y - s->mspel,
1900 s->h_edge_pos, v_edge_pos);
1901 srcY = s->edge_emu_buffer;
1902 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1903 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1904 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1905 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1908 /* if we deal with range reduction we need to scale source blocks */
1909 if (v->rangeredfrm) {
1911 uint8_t *src, *src2;
1914 for (j = 0; j < 17 + s->mspel * 2; j++) {
1915 for (i = 0; i < 17 + s->mspel * 2; i++)
1916 src[i] = ((src[i] - 128) >> 1) + 128;
1921 for (j = 0; j < 9; j++) {
1922 for (i = 0; i < 9; i++) {
1923 src[i] = ((src[i] - 128) >> 1) + 128;
1924 src2[i] = ((src2[i] - 128) >> 1) + 128;
1926 src += s->uvlinesize;
1927 src2 += s->uvlinesize;
1930 srcY += s->mspel * (1 + s->linesize);
1933 if (v->field_mode && v->second_field) {
1934 off = s->current_picture_ptr->f.linesize[0];
1935 off_uv = s->current_picture_ptr->f.linesize[1];
1942 dxy = ((my & 3) << 2) | (mx & 3);
1943 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1944 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1945 srcY += s->linesize * 8;
1946 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1947 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1949 dxy = (my & 2) | ((mx & 2) >> 1);
1952 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1954 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1957 if (s->flags & CODEC_FLAG_GRAY) return;
1958 /* Chroma MC always uses qpel blilinear */
1959 uvmx = (uvmx & 3) << 1;
1960 uvmy = (uvmy & 3) << 1;
1962 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1963 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1965 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1966 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1970 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1974 #if B_FRACTION_DEN==256
1978 return 2 * ((value * n + 255) >> 9);
1979 return (value * n + 128) >> 8;
1982 n -= B_FRACTION_DEN;
1984 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1985 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1989 /** Reconstruct motion vector for B-frame and do motion compensation
1991 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
1992 int direct, int mode)
1995 v->mv_mode2 = v->mv_mode;
1996 v->mv_mode = MV_PMODE_INTENSITY_COMP;
2002 v->mv_mode = v->mv_mode2;
2005 if (mode == BMV_TYPE_INTERPOLATED) {
2009 v->mv_mode = v->mv_mode2;
2013 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2014 v->mv_mode = v->mv_mode2;
2015 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2017 v->mv_mode = v->mv_mode2;
2020 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2021 int direct, int mvtype)
2023 MpegEncContext *s = &v->s;
2024 int xy, wrap, off = 0;
2029 const uint8_t *is_intra = v->mb_type[0];
2033 /* scale MV difference to be quad-pel */
2034 dmv_x[0] <<= 1 - s->quarter_sample;
2035 dmv_y[0] <<= 1 - s->quarter_sample;
2036 dmv_x[1] <<= 1 - s->quarter_sample;
2037 dmv_y[1] <<= 1 - s->quarter_sample;
2039 wrap = s->b8_stride;
2040 xy = s->block_index[0];
2043 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2044 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2045 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2046 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2049 if (!v->field_mode) {
2050 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2051 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2052 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2053 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2055 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2056 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));
2057 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));
2058 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));
2059 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));
2062 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2063 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2064 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2065 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2069 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2070 C = s->current_picture.f.motion_val[0][xy - 2];
2071 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2072 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2073 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2075 if (!s->mb_x) C[0] = C[1] = 0;
2076 if (!s->first_slice_line) { // predictor A is not out of bounds
2077 if (s->mb_width == 1) {
2081 px = mid_pred(A[0], B[0], C[0]);
2082 py = mid_pred(A[1], B[1], C[1]);
2084 } else if (s->mb_x) { // predictor C is not out of bounds
2090 /* Pullback MV as specified in 8.3.5.3.4 */
2093 if (v->profile < PROFILE_ADVANCED) {
2094 qx = (s->mb_x << 5);
2095 qy = (s->mb_y << 5);
2096 X = (s->mb_width << 5) - 4;
2097 Y = (s->mb_height << 5) - 4;
2098 if (qx + px < -28) px = -28 - qx;
2099 if (qy + py < -28) py = -28 - qy;
2100 if (qx + px > X) px = X - qx;
2101 if (qy + py > Y) py = Y - qy;
2103 qx = (s->mb_x << 6);
2104 qy = (s->mb_y << 6);
2105 X = (s->mb_width << 6) - 4;
2106 Y = (s->mb_height << 6) - 4;
2107 if (qx + px < -60) px = -60 - qx;
2108 if (qy + py < -60) py = -60 - qy;
2109 if (qx + px > X) px = X - qx;
2110 if (qy + py > Y) py = Y - qy;
2113 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2114 if (0 && !s->first_slice_line && s->mb_x) {
2115 if (is_intra[xy - wrap])
2116 sum = FFABS(px) + FFABS(py);
2118 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2120 if (get_bits1(&s->gb)) {
2128 if (is_intra[xy - 2])
2129 sum = FFABS(px) + FFABS(py);
2131 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2133 if (get_bits1(&s->gb)) {
2143 /* store MV using signed modulus of MV range defined in 4.11 */
2144 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2145 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2147 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2148 C = s->current_picture.f.motion_val[1][xy - 2];
2149 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2150 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2151 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2155 if (!s->first_slice_line) { // predictor A is not out of bounds
2156 if (s->mb_width == 1) {
2160 px = mid_pred(A[0], B[0], C[0]);
2161 py = mid_pred(A[1], B[1], C[1]);
2163 } else if (s->mb_x) { // predictor C is not out of bounds
2169 /* Pullback MV as specified in 8.3.5.3.4 */
2172 if (v->profile < PROFILE_ADVANCED) {
2173 qx = (s->mb_x << 5);
2174 qy = (s->mb_y << 5);
2175 X = (s->mb_width << 5) - 4;
2176 Y = (s->mb_height << 5) - 4;
2177 if (qx + px < -28) px = -28 - qx;
2178 if (qy + py < -28) py = -28 - qy;
2179 if (qx + px > X) px = X - qx;
2180 if (qy + py > Y) py = Y - qy;
2182 qx = (s->mb_x << 6);
2183 qy = (s->mb_y << 6);
2184 X = (s->mb_width << 6) - 4;
2185 Y = (s->mb_height << 6) - 4;
2186 if (qx + px < -60) px = -60 - qx;
2187 if (qy + py < -60) py = -60 - qy;
2188 if (qx + px > X) px = X - qx;
2189 if (qy + py > Y) py = Y - qy;
2192 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2193 if (0 && !s->first_slice_line && s->mb_x) {
2194 if (is_intra[xy - wrap])
2195 sum = FFABS(px) + FFABS(py);
2197 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2199 if (get_bits1(&s->gb)) {
2207 if (is_intra[xy - 2])
2208 sum = FFABS(px) + FFABS(py);
2210 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2212 if (get_bits1(&s->gb)) {
2222 /* store MV using signed modulus of MV range defined in 4.11 */
2224 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2225 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2227 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2228 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2229 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2230 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2233 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2235 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2236 MpegEncContext *s = &v->s;
2237 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2239 if (v->bmvtype == BMV_TYPE_DIRECT) {
2240 int total_opp, k, f;
2241 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2242 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2243 v->bfraction, 0, s->quarter_sample);
2244 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2245 v->bfraction, 0, s->quarter_sample);
2246 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2247 v->bfraction, 1, s->quarter_sample);
2248 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2249 v->bfraction, 1, s->quarter_sample);
2251 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2252 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2253 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2254 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2255 f = (total_opp > 2) ? 1 : 0;
2257 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2258 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2261 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2262 for (k = 0; k < 4; k++) {
2263 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2264 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2265 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2266 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2267 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2268 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2272 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2273 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);
2274 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);
2277 if (dir) { // backward
2278 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);
2279 if (n == 3 || mv1) {
2280 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2283 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);
2284 if (n == 3 || mv1) {
2285 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2290 /** Get predicted DC value for I-frames only
2291 * prediction dir: left=0, top=1
2292 * @param s MpegEncContext
2293 * @param overlap flag indicating that overlap filtering is used
2294 * @param pq integer part of picture quantizer
2295 * @param[in] n block index in the current MB
2296 * @param dc_val_ptr Pointer to DC predictor
2297 * @param dir_ptr Prediction direction for use in AC prediction
2299 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2300 int16_t **dc_val_ptr, int *dir_ptr)
2302 int a, b, c, wrap, pred, scale;
2304 static const uint16_t dcpred[32] = {
2305 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2306 114, 102, 93, 85, 79, 73, 68, 64,
2307 60, 57, 54, 51, 49, 47, 45, 43,
2308 41, 39, 38, 37, 35, 34, 33
2311 /* find prediction - wmv3_dc_scale always used here in fact */
2312 if (n < 4) scale = s->y_dc_scale;
2313 else scale = s->c_dc_scale;
2315 wrap = s->block_wrap[n];
2316 dc_val = s->dc_val[0] + s->block_index[n];
2322 b = dc_val[ - 1 - wrap];
2323 a = dc_val[ - wrap];
2325 if (pq < 9 || !overlap) {
2326 /* Set outer values */
2327 if (s->first_slice_line && (n != 2 && n != 3))
2328 b = a = dcpred[scale];
2329 if (s->mb_x == 0 && (n != 1 && n != 3))
2330 b = c = dcpred[scale];
2332 /* Set outer values */
2333 if (s->first_slice_line && (n != 2 && n != 3))
2335 if (s->mb_x == 0 && (n != 1 && n != 3))
2339 if (abs(a - b) <= abs(b - c)) {
2341 *dir_ptr = 1; // left
2344 *dir_ptr = 0; // top
2347 /* update predictor */
2348 *dc_val_ptr = &dc_val[0];
2353 /** Get predicted DC value
2354 * prediction dir: left=0, top=1
2355 * @param s MpegEncContext
2356 * @param overlap flag indicating that overlap filtering is used
2357 * @param pq integer part of picture quantizer
2358 * @param[in] n block index in the current MB
2359 * @param a_avail flag indicating top block availability
2360 * @param c_avail flag indicating left block availability
2361 * @param dc_val_ptr Pointer to DC predictor
2362 * @param dir_ptr Prediction direction for use in AC prediction
2364 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2365 int a_avail, int c_avail,
2366 int16_t **dc_val_ptr, int *dir_ptr)
2368 int a, b, c, wrap, pred;
2370 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2374 wrap = s->block_wrap[n];
2375 dc_val = s->dc_val[0] + s->block_index[n];
2381 b = dc_val[ - 1 - wrap];
2382 a = dc_val[ - wrap];
2383 /* scale predictors if needed */
2384 q1 = s->current_picture.f.qscale_table[mb_pos];
2385 dqscale_index = s->y_dc_scale_table[q1] - 1;
2386 if (dqscale_index < 0)
2388 if (c_avail && (n != 1 && n != 3)) {
2389 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2391 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2393 if (a_avail && (n != 2 && n != 3)) {
2394 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2396 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2398 if (a_avail && c_avail && (n != 3)) {
2403 off -= s->mb_stride;
2404 q2 = s->current_picture.f.qscale_table[off];
2406 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2409 if (a_avail && c_avail) {
2410 if (abs(a - b) <= abs(b - c)) {
2412 *dir_ptr = 1; // left
2415 *dir_ptr = 0; // top
2417 } else if (a_avail) {
2419 *dir_ptr = 0; // top
2420 } else if (c_avail) {
2422 *dir_ptr = 1; // left
2425 *dir_ptr = 1; // left
2428 /* update predictor */
2429 *dc_val_ptr = &dc_val[0];
2433 /** @} */ // Block group
2436 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2437 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2441 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2442 uint8_t **coded_block_ptr)
2444 int xy, wrap, pred, a, b, c;
2446 xy = s->block_index[n];
2447 wrap = s->b8_stride;
2452 a = s->coded_block[xy - 1 ];
2453 b = s->coded_block[xy - 1 - wrap];
2454 c = s->coded_block[xy - wrap];
2463 *coded_block_ptr = &s->coded_block[xy];
2469 * Decode one AC coefficient
2470 * @param v The VC1 context
2471 * @param last Last coefficient
2472 * @param skip How much zero coefficients to skip
2473 * @param value Decoded AC coefficient value
2474 * @param codingset set of VLC to decode data
2477 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2478 int *value, int codingset)
2480 GetBitContext *gb = &v->s.gb;
2481 int index, escape, run = 0, level = 0, lst = 0;
2483 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2484 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2485 run = vc1_index_decode_table[codingset][index][0];
2486 level = vc1_index_decode_table[codingset][index][1];
2487 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2491 escape = decode210(gb);
2493 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2494 run = vc1_index_decode_table[codingset][index][0];
2495 level = vc1_index_decode_table[codingset][index][1];
2496 lst = index >= vc1_last_decode_table[codingset];
2499 level += vc1_last_delta_level_table[codingset][run];
2501 level += vc1_delta_level_table[codingset][run];
2504 run += vc1_last_delta_run_table[codingset][level] + 1;
2506 run += vc1_delta_run_table[codingset][level] + 1;
2512 lst = get_bits1(gb);
2513 if (v->s.esc3_level_length == 0) {
2514 if (v->pq < 8 || v->dquantfrm) { // table 59
2515 v->s.esc3_level_length = get_bits(gb, 3);
2516 if (!v->s.esc3_level_length)
2517 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2518 } else { // table 60
2519 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2521 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2523 run = get_bits(gb, v->s.esc3_run_length);
2524 sign = get_bits1(gb);
2525 level = get_bits(gb, v->s.esc3_level_length);
2536 /** Decode intra block in intra frames - should be faster than decode_intra_block
2537 * @param v VC1Context
2538 * @param block block to decode
2539 * @param[in] n subblock index
2540 * @param coded are AC coeffs present or not
2541 * @param codingset set of VLC to decode data
2543 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2544 int coded, int codingset)
2546 GetBitContext *gb = &v->s.gb;
2547 MpegEncContext *s = &v->s;
2548 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2551 int16_t *ac_val, *ac_val2;
2554 /* Get DC differential */
2556 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2558 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2561 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2565 if (dcdiff == 119 /* ESC index value */) {
2566 /* TODO: Optimize */
2567 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2568 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2569 else dcdiff = get_bits(gb, 8);
2572 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2573 else if (v->pq == 2)
2574 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2581 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2584 /* Store the quantized DC coeff, used for prediction */
2586 block[0] = dcdiff * s->y_dc_scale;
2588 block[0] = dcdiff * s->c_dc_scale;
2599 int last = 0, skip, value;
2600 const uint8_t *zz_table;
2604 scale = v->pq * 2 + v->halfpq;
2608 zz_table = v->zz_8x8[2];
2610 zz_table = v->zz_8x8[3];
2612 zz_table = v->zz_8x8[1];
2614 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2616 if (dc_pred_dir) // left
2619 ac_val -= 16 * s->block_wrap[n];
2622 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2626 block[zz_table[i++]] = value;
2629 /* apply AC prediction if needed */
2631 if (dc_pred_dir) { // left
2632 for (k = 1; k < 8; k++)
2633 block[k << v->left_blk_sh] += ac_val[k];
2635 for (k = 1; k < 8; k++)
2636 block[k << v->top_blk_sh] += ac_val[k + 8];
2639 /* save AC coeffs for further prediction */
2640 for (k = 1; k < 8; k++) {
2641 ac_val2[k] = block[k << v->left_blk_sh];
2642 ac_val2[k + 8] = block[k << v->top_blk_sh];
2645 /* scale AC coeffs */
2646 for (k = 1; k < 64; k++)
2650 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2653 if (s->ac_pred) i = 63;
2659 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2663 scale = v->pq * 2 + v->halfpq;
2664 memset(ac_val2, 0, 16 * 2);
2665 if (dc_pred_dir) { // left
2668 memcpy(ac_val2, ac_val, 8 * 2);
2670 ac_val -= 16 * s->block_wrap[n];
2672 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2675 /* apply AC prediction if needed */
2677 if (dc_pred_dir) { //left
2678 for (k = 1; k < 8; k++) {
2679 block[k << v->left_blk_sh] = ac_val[k] * scale;
2680 if (!v->pquantizer && block[k << v->left_blk_sh])
2681 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2684 for (k = 1; k < 8; k++) {
2685 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2686 if (!v->pquantizer && block[k << v->top_blk_sh])
2687 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2693 s->block_last_index[n] = i;
2698 /** Decode intra block in intra frames - should be faster than decode_intra_block
2699 * @param v VC1Context
2700 * @param block block to decode
2701 * @param[in] n subblock number
2702 * @param coded are AC coeffs present or not
2703 * @param codingset set of VLC to decode data
2704 * @param mquant quantizer value for this macroblock
2706 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2707 int coded, int codingset, int mquant)
2709 GetBitContext *gb = &v->s.gb;
2710 MpegEncContext *s = &v->s;
2711 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2714 int16_t *ac_val, *ac_val2;
2716 int a_avail = v->a_avail, c_avail = v->c_avail;
2717 int use_pred = s->ac_pred;
2720 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2722 /* Get DC differential */
2724 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2726 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2729 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2733 if (dcdiff == 119 /* ESC index value */) {
2734 /* TODO: Optimize */
2735 if (mquant == 1) dcdiff = get_bits(gb, 10);
2736 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2737 else dcdiff = get_bits(gb, 8);
2740 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2741 else if (mquant == 2)
2742 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2749 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2752 /* Store the quantized DC coeff, used for prediction */
2754 block[0] = dcdiff * s->y_dc_scale;
2756 block[0] = dcdiff * s->c_dc_scale;
2762 /* check if AC is needed at all */
2763 if (!a_avail && !c_avail)
2765 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2768 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2770 if (dc_pred_dir) // left
2773 ac_val -= 16 * s->block_wrap[n];
2775 q1 = s->current_picture.f.qscale_table[mb_pos];
2776 if ( dc_pred_dir && c_avail && mb_pos)
2777 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2778 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2779 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2780 if ( dc_pred_dir && n == 1)
2782 if (!dc_pred_dir && n == 2)
2788 int last = 0, skip, value;
2789 const uint8_t *zz_table;
2793 if (!use_pred && v->fcm == ILACE_FRAME) {
2794 zz_table = v->zzi_8x8;
2796 if (!dc_pred_dir) // top
2797 zz_table = v->zz_8x8[2];
2799 zz_table = v->zz_8x8[3];
2802 if (v->fcm != ILACE_FRAME)
2803 zz_table = v->zz_8x8[1];
2805 zz_table = v->zzi_8x8;
2809 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2813 block[zz_table[i++]] = value;
2816 /* apply AC prediction if needed */
2818 /* scale predictors if needed*/
2819 if (q2 && q1 != q2) {
2820 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2821 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2824 return AVERROR_INVALIDDATA;
2825 if (dc_pred_dir) { // left
2826 for (k = 1; k < 8; k++)
2827 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2829 for (k = 1; k < 8; k++)
2830 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2833 if (dc_pred_dir) { //left
2834 for (k = 1; k < 8; k++)
2835 block[k << v->left_blk_sh] += ac_val[k];
2837 for (k = 1; k < 8; k++)
2838 block[k << v->top_blk_sh] += ac_val[k + 8];
2842 /* save AC coeffs for further prediction */
2843 for (k = 1; k < 8; k++) {
2844 ac_val2[k ] = block[k << v->left_blk_sh];
2845 ac_val2[k + 8] = block[k << v->top_blk_sh];
2848 /* scale AC coeffs */
2849 for (k = 1; k < 64; k++)
2853 block[k] += (block[k] < 0) ? -mquant : mquant;
2856 if (use_pred) i = 63;
2857 } else { // no AC coeffs
2860 memset(ac_val2, 0, 16 * 2);
2861 if (dc_pred_dir) { // left
2863 memcpy(ac_val2, ac_val, 8 * 2);
2864 if (q2 && q1 != q2) {
2865 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2866 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2868 return AVERROR_INVALIDDATA;
2869 for (k = 1; k < 8; k++)
2870 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2875 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2876 if (q2 && q1 != q2) {
2877 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2878 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2880 return AVERROR_INVALIDDATA;
2881 for (k = 1; k < 8; k++)
2882 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2887 /* apply AC prediction if needed */
2889 if (dc_pred_dir) { // left
2890 for (k = 1; k < 8; k++) {
2891 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2892 if (!v->pquantizer && block[k << v->left_blk_sh])
2893 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2896 for (k = 1; k < 8; k++) {
2897 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2898 if (!v->pquantizer && block[k << v->top_blk_sh])
2899 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2905 s->block_last_index[n] = i;
2910 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2911 * @param v VC1Context
2912 * @param block block to decode
2913 * @param[in] n subblock index
2914 * @param coded are AC coeffs present or not
2915 * @param mquant block quantizer
2916 * @param codingset set of VLC to decode data
2918 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2919 int coded, int mquant, int codingset)
2921 GetBitContext *gb = &v->s.gb;
2922 MpegEncContext *s = &v->s;
2923 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2926 int16_t *ac_val, *ac_val2;
2928 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2929 int a_avail = v->a_avail, c_avail = v->c_avail;
2930 int use_pred = s->ac_pred;
2934 s->dsp.clear_block(block);
2936 /* XXX: Guard against dumb values of mquant */
2937 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2939 /* Set DC scale - y and c use the same */
2940 s->y_dc_scale = s->y_dc_scale_table[mquant];
2941 s->c_dc_scale = s->c_dc_scale_table[mquant];
2943 /* Get DC differential */
2945 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2947 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2950 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2954 if (dcdiff == 119 /* ESC index value */) {
2955 /* TODO: Optimize */
2956 if (mquant == 1) dcdiff = get_bits(gb, 10);
2957 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2958 else dcdiff = get_bits(gb, 8);
2961 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2962 else if (mquant == 2)
2963 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2970 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2973 /* Store the quantized DC coeff, used for prediction */
2976 block[0] = dcdiff * s->y_dc_scale;
2978 block[0] = dcdiff * s->c_dc_scale;
2984 /* check if AC is needed at all and adjust direction if needed */
2985 if (!a_avail) dc_pred_dir = 1;
2986 if (!c_avail) dc_pred_dir = 0;
2987 if (!a_avail && !c_avail) use_pred = 0;
2988 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2991 scale = mquant * 2 + v->halfpq;
2993 if (dc_pred_dir) //left
2996 ac_val -= 16 * s->block_wrap[n];
2998 q1 = s->current_picture.f.qscale_table[mb_pos];
2999 if (dc_pred_dir && c_avail && mb_pos)
3000 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
3001 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
3002 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
3003 if ( dc_pred_dir && n == 1)
3005 if (!dc_pred_dir && n == 2)
3007 if (n == 3) q2 = q1;
3010 int last = 0, skip, value;
3014 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3018 if (v->fcm == PROGRESSIVE)
3019 block[v->zz_8x8[0][i++]] = value;
3021 if (use_pred && (v->fcm == ILACE_FRAME)) {
3022 if (!dc_pred_dir) // top
3023 block[v->zz_8x8[2][i++]] = value;
3025 block[v->zz_8x8[3][i++]] = value;
3027 block[v->zzi_8x8[i++]] = value;
3032 /* apply AC prediction if needed */
3034 /* scale predictors if needed*/
3035 if (q2 && q1 != q2) {
3036 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3037 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3040 return AVERROR_INVALIDDATA;
3041 if (dc_pred_dir) { // left
3042 for (k = 1; k < 8; k++)
3043 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3045 for (k = 1; k < 8; k++)
3046 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3049 if (dc_pred_dir) { // left
3050 for (k = 1; k < 8; k++)
3051 block[k << v->left_blk_sh] += ac_val[k];
3053 for (k = 1; k < 8; k++)
3054 block[k << v->top_blk_sh] += ac_val[k + 8];
3058 /* save AC coeffs for further prediction */
3059 for (k = 1; k < 8; k++) {
3060 ac_val2[k ] = block[k << v->left_blk_sh];
3061 ac_val2[k + 8] = block[k << v->top_blk_sh];
3064 /* scale AC coeffs */
3065 for (k = 1; k < 64; k++)
3069 block[k] += (block[k] < 0) ? -mquant : mquant;
3072 if (use_pred) i = 63;
3073 } else { // no AC coeffs
3076 memset(ac_val2, 0, 16 * 2);
3077 if (dc_pred_dir) { // left
3079 memcpy(ac_val2, ac_val, 8 * 2);
3080 if (q2 && q1 != q2) {
3081 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3082 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3084 return AVERROR_INVALIDDATA;
3085 for (k = 1; k < 8; k++)
3086 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3091 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3092 if (q2 && q1 != q2) {
3093 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3094 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3096 return AVERROR_INVALIDDATA;
3097 for (k = 1; k < 8; k++)
3098 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3103 /* apply AC prediction if needed */
3105 if (dc_pred_dir) { // left
3106 for (k = 1; k < 8; k++) {
3107 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3108 if (!v->pquantizer && block[k << v->left_blk_sh])
3109 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3112 for (k = 1; k < 8; k++) {
3113 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3114 if (!v->pquantizer && block[k << v->top_blk_sh])
3115 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3121 s->block_last_index[n] = i;
3128 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3129 int mquant, int ttmb, int first_block,
3130 uint8_t *dst, int linesize, int skip_block,
3133 MpegEncContext *s = &v->s;
3134 GetBitContext *gb = &s->gb;
3137 int scale, off, idx, last, skip, value;
3138 int ttblk = ttmb & 7;
3141 s->dsp.clear_block(block);
3144 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)];
3146 if (ttblk == TT_4X4) {
3147 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3149 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3150 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3151 || (!v->res_rtm_flag && !first_block))) {
3152 subblkpat = decode012(gb);
3154 subblkpat ^= 3; // swap decoded pattern bits
3155 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3157 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3160 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3162 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3163 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3164 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3167 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3168 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3177 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3182 idx = v->zz_8x8[0][i++];
3184 idx = v->zzi_8x8[i++];
3185 block[idx] = value * scale;
3187 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3191 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3193 v->vc1dsp.vc1_inv_trans_8x8(block);
3194 s->dsp.add_pixels_clamped(block, dst, linesize);
3199 pat = ~subblkpat & 0xF;
3200 for (j = 0; j < 4; j++) {
3201 last = subblkpat & (1 << (3 - j));
3203 off = (j & 1) * 4 + (j & 2) * 16;
3205 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3210 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3212 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3213 block[idx + off] = value * scale;
3215 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3217 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3219 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3221 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3226 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3227 for (j = 0; j < 2; j++) {
3228 last = subblkpat & (1 << (1 - j));
3232 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3237 idx = v->zz_8x4[i++] + off;
3239 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3240 block[idx] = value * scale;
3242 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3244 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3246 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3248 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3253 pat = ~(subblkpat * 5) & 0xF;
3254 for (j = 0; j < 2; j++) {
3255 last = subblkpat & (1 << (1 - j));
3259 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3264 idx = v->zz_4x8[i++] + off;
3266 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3267 block[idx] = value * scale;
3269 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3271 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3273 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3275 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3281 *ttmb_out |= ttblk << (n * 4);
3285 /** @} */ // Macroblock group
3287 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3288 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3290 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3292 MpegEncContext *s = &v->s;
3293 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3294 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3295 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3296 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3297 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3300 if (block_num > 3) {
3301 dst = s->dest[block_num - 3];
3303 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3305 if (s->mb_y != s->end_mb_y || block_num < 2) {
3309 if (block_num > 3) {
3310 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3311 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3312 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3313 mv_stride = s->mb_stride;
3315 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3316 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3317 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3318 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3319 mv_stride = s->b8_stride;
3320 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3323 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3324 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3325 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3327 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3329 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3332 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3334 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3339 dst -= 4 * linesize;
3340 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3341 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3342 idx = (block_cbp | (block_cbp >> 2)) & 3;
3344 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3347 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3349 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3354 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3356 MpegEncContext *s = &v->s;
3357 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3358 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3359 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3360 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3361 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3364 if (block_num > 3) {
3365 dst = s->dest[block_num - 3] - 8 * linesize;
3367 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3370 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3373 if (block_num > 3) {
3374 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3375 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3376 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3378 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3379 : (mb_cbp >> ((block_num + 1) * 4));
3380 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3381 : (mb_is_intra >> ((block_num + 1) * 4));
3382 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3384 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3385 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3387 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3389 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3392 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3394 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3400 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3401 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3402 idx = (block_cbp | (block_cbp >> 1)) & 5;
3404 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3407 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3409 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3414 static void vc1_apply_p_loop_filter(VC1Context *v)
3416 MpegEncContext *s = &v->s;
3419 for (i = 0; i < 6; i++) {
3420 vc1_apply_p_v_loop_filter(v, i);
3423 /* V always precedes H, therefore we run H one MB before V;
3424 * at the end of a row, we catch up to complete the row */
3426 for (i = 0; i < 6; i++) {
3427 vc1_apply_p_h_loop_filter(v, i);
3429 if (s->mb_x == s->mb_width - 1) {
3431 ff_update_block_index(s);
3432 for (i = 0; i < 6; i++) {
3433 vc1_apply_p_h_loop_filter(v, i);
3439 /** Decode one P-frame MB
3441 static int vc1_decode_p_mb(VC1Context *v)
3443 MpegEncContext *s = &v->s;
3444 GetBitContext *gb = &s->gb;
3446 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3447 int cbp; /* cbp decoding stuff */
3448 int mqdiff, mquant; /* MB quantization */
3449 int ttmb = v->ttfrm; /* MB Transform type */
3451 int mb_has_coeffs = 1; /* last_flag */
3452 int dmv_x, dmv_y; /* Differential MV components */
3453 int index, index1; /* LUT indexes */
3454 int val, sign; /* temp values */
3455 int first_block = 1;
3457 int skipped, fourmv;
3458 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3460 mquant = v->pq; /* lossy initialization */
3462 if (v->mv_type_is_raw)
3463 fourmv = get_bits1(gb);
3465 fourmv = v->mv_type_mb_plane[mb_pos];
3467 skipped = get_bits1(gb);
3469 skipped = v->s.mbskip_table[mb_pos];
3471 if (!fourmv) { /* 1MV mode */
3473 GET_MVDATA(dmv_x, dmv_y);
3476 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3477 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3479 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3480 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3482 /* FIXME Set DC val for inter block ? */
3483 if (s->mb_intra && !mb_has_coeffs) {
3485 s->ac_pred = get_bits1(gb);
3487 } else if (mb_has_coeffs) {
3489 s->ac_pred = get_bits1(gb);
3490 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3496 s->current_picture.f.qscale_table[mb_pos] = mquant;
3498 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3499 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3500 VC1_TTMB_VLC_BITS, 2);
3501 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3503 for (i = 0; i < 6; i++) {
3504 s->dc_val[0][s->block_index[i]] = 0;
3506 val = ((cbp >> (5 - i)) & 1);
3507 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3508 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3510 /* check if prediction blocks A and C are available */
3511 v->a_avail = v->c_avail = 0;
3512 if (i == 2 || i == 3 || !s->first_slice_line)
3513 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3514 if (i == 1 || i == 3 || s->mb_x)
3515 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3517 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3518 (i & 4) ? v->codingset2 : v->codingset);
3519 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3521 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3523 for (j = 0; j < 64; j++)
3524 s->block[i][j] <<= 1;
3525 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3526 if (v->pq >= 9 && v->overlap) {
3528 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3530 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3532 block_cbp |= 0xF << (i << 2);
3533 block_intra |= 1 << i;
3535 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3536 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3537 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3538 block_cbp |= pat << (i << 2);
3539 if (!v->ttmbf && ttmb < 8)
3546 for (i = 0; i < 6; i++) {
3547 v->mb_type[0][s->block_index[i]] = 0;
3548 s->dc_val[0][s->block_index[i]] = 0;
3550 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3551 s->current_picture.f.qscale_table[mb_pos] = 0;
3552 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3555 } else { // 4MV mode
3556 if (!skipped /* unskipped MB */) {
3557 int intra_count = 0, coded_inter = 0;
3558 int is_intra[6], is_coded[6];
3560 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3561 for (i = 0; i < 6; i++) {
3562 val = ((cbp >> (5 - i)) & 1);
3563 s->dc_val[0][s->block_index[i]] = 0;
3570 GET_MVDATA(dmv_x, dmv_y);
3572 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3574 vc1_mc_4mv_luma(v, i, 0);
3575 intra_count += s->mb_intra;
3576 is_intra[i] = s->mb_intra;
3577 is_coded[i] = mb_has_coeffs;
3580 is_intra[i] = (intra_count >= 3);
3584 vc1_mc_4mv_chroma(v, 0);
3585 v->mb_type[0][s->block_index[i]] = is_intra[i];
3587 coded_inter = !is_intra[i] & is_coded[i];
3589 // if there are no coded blocks then don't do anything more
3591 if (!intra_count && !coded_inter)
3594 s->current_picture.f.qscale_table[mb_pos] = mquant;
3595 /* test if block is intra and has pred */
3598 for (i = 0; i < 6; i++)
3600 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3601 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3607 s->ac_pred = get_bits1(gb);
3611 if (!v->ttmbf && coded_inter)
3612 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3613 for (i = 0; i < 6; i++) {
3615 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3616 s->mb_intra = is_intra[i];
3618 /* check if prediction blocks A and C are available */
3619 v->a_avail = v->c_avail = 0;
3620 if (i == 2 || i == 3 || !s->first_slice_line)
3621 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3622 if (i == 1 || i == 3 || s->mb_x)
3623 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3625 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3626 (i & 4) ? v->codingset2 : v->codingset);
3627 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3629 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3631 for (j = 0; j < 64; j++)
3632 s->block[i][j] <<= 1;
3633 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3634 (i & 4) ? s->uvlinesize : s->linesize);
3635 if (v->pq >= 9 && v->overlap) {
3637 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3639 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3641 block_cbp |= 0xF << (i << 2);
3642 block_intra |= 1 << i;
3643 } else if (is_coded[i]) {
3644 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3645 first_block, s->dest[dst_idx] + off,
3646 (i & 4) ? s->uvlinesize : s->linesize,
3647 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3649 block_cbp |= pat << (i << 2);
3650 if (!v->ttmbf && ttmb < 8)
3655 } else { // skipped MB
3657 s->current_picture.f.qscale_table[mb_pos] = 0;
3658 for (i = 0; i < 6; i++) {
3659 v->mb_type[0][s->block_index[i]] = 0;
3660 s->dc_val[0][s->block_index[i]] = 0;
3662 for (i = 0; i < 4; i++) {
3663 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3664 vc1_mc_4mv_luma(v, i, 0);
3666 vc1_mc_4mv_chroma(v, 0);
3667 s->current_picture.f.qscale_table[mb_pos] = 0;
3671 v->cbp[s->mb_x] = block_cbp;
3672 v->ttblk[s->mb_x] = block_tt;
3673 v->is_intra[s->mb_x] = block_intra;
3678 /* Decode one macroblock in an interlaced frame p picture */
3680 static int vc1_decode_p_mb_intfr(VC1Context *v)
3682 MpegEncContext *s = &v->s;
3683 GetBitContext *gb = &s->gb;
3685 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3686 int cbp = 0; /* cbp decoding stuff */
3687 int mqdiff, mquant; /* MB quantization */
3688 int ttmb = v->ttfrm; /* MB Transform type */
3690 int mb_has_coeffs = 1; /* last_flag */
3691 int dmv_x, dmv_y; /* Differential MV components */
3692 int val; /* temp value */
3693 int first_block = 1;
3695 int skipped, fourmv = 0, twomv = 0;
3696 int block_cbp = 0, pat, block_tt = 0;
3697 int idx_mbmode = 0, mvbp;
3698 int stride_y, fieldtx;
3700 mquant = v->pq; /* Lossy initialization */
3703 skipped = get_bits1(gb);
3705 skipped = v->s.mbskip_table[mb_pos];
3707 if (v->fourmvswitch)
3708 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3710 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3711 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3712 /* store the motion vector type in a flag (useful later) */
3713 case MV_PMODE_INTFR_4MV:
3715 v->blk_mv_type[s->block_index[0]] = 0;
3716 v->blk_mv_type[s->block_index[1]] = 0;
3717 v->blk_mv_type[s->block_index[2]] = 0;
3718 v->blk_mv_type[s->block_index[3]] = 0;
3720 case MV_PMODE_INTFR_4MV_FIELD:
3722 v->blk_mv_type[s->block_index[0]] = 1;
3723 v->blk_mv_type[s->block_index[1]] = 1;
3724 v->blk_mv_type[s->block_index[2]] = 1;
3725 v->blk_mv_type[s->block_index[3]] = 1;
3727 case MV_PMODE_INTFR_2MV_FIELD:
3729 v->blk_mv_type[s->block_index[0]] = 1;
3730 v->blk_mv_type[s->block_index[1]] = 1;
3731 v->blk_mv_type[s->block_index[2]] = 1;
3732 v->blk_mv_type[s->block_index[3]] = 1;
3734 case MV_PMODE_INTFR_1MV:
3735 v->blk_mv_type[s->block_index[0]] = 0;
3736 v->blk_mv_type[s->block_index[1]] = 0;
3737 v->blk_mv_type[s->block_index[2]] = 0;
3738 v->blk_mv_type[s->block_index[3]] = 0;
3741 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3742 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3743 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3744 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3745 s->mb_intra = v->is_intra[s->mb_x] = 1;
3746 for (i = 0; i < 6; i++)
3747 v->mb_type[0][s->block_index[i]] = 1;
3748 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3749 mb_has_coeffs = get_bits1(gb);
3751 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3752 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3754 s->current_picture.f.qscale_table[mb_pos] = mquant;
3755 /* Set DC scale - y and c use the same (not sure if necessary here) */
3756 s->y_dc_scale = s->y_dc_scale_table[mquant];
3757 s->c_dc_scale = s->c_dc_scale_table[mquant];
3759 for (i = 0; i < 6; i++) {
3760 s->dc_val[0][s->block_index[i]] = 0;
3762 val = ((cbp >> (5 - i)) & 1);
3763 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3764 v->a_avail = v->c_avail = 0;
3765 if (i == 2 || i == 3 || !s->first_slice_line)
3766 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3767 if (i == 1 || i == 3 || s->mb_x)
3768 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3770 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3771 (i & 4) ? v->codingset2 : v->codingset);
3772 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3773 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3775 stride_y = s->linesize << fieldtx;
3776 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3778 stride_y = s->uvlinesize;
3781 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3785 } else { // inter MB
3786 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3788 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3789 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3790 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3792 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3793 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3794 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3797 s->mb_intra = v->is_intra[s->mb_x] = 0;
3798 for (i = 0; i < 6; i++)
3799 v->mb_type[0][s->block_index[i]] = 0;
3800 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3801 /* for all motion vector read MVDATA and motion compensate each block */
3805 for (i = 0; i < 6; i++) {
3808 val = ((mvbp >> (3 - i)) & 1);
3810 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3812 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3813 vc1_mc_4mv_luma(v, i, 0);
3814 } else if (i == 4) {
3815 vc1_mc_4mv_chroma4(v);
3822 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3824 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3825 vc1_mc_4mv_luma(v, 0, 0);
3826 vc1_mc_4mv_luma(v, 1, 0);
3829 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3831 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3832 vc1_mc_4mv_luma(v, 2, 0);
3833 vc1_mc_4mv_luma(v, 3, 0);
3834 vc1_mc_4mv_chroma4(v);
3836 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3839 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3841 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3845 GET_MQUANT(); // p. 227
3846 s->current_picture.f.qscale_table[mb_pos] = mquant;
3847 if (!v->ttmbf && cbp)
3848 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3849 for (i = 0; i < 6; i++) {
3850 s->dc_val[0][s->block_index[i]] = 0;
3852 val = ((cbp >> (5 - i)) & 1);
3854 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3856 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3858 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3859 first_block, s->dest[dst_idx] + off,
3860 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3861 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3862 block_cbp |= pat << (i << 2);
3863 if (!v->ttmbf && ttmb < 8)
3870 s->mb_intra = v->is_intra[s->mb_x] = 0;
3871 for (i = 0; i < 6; i++) {
3872 v->mb_type[0][s->block_index[i]] = 0;
3873 s->dc_val[0][s->block_index[i]] = 0;
3875 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3876 s->current_picture.f.qscale_table[mb_pos] = 0;
3877 v->blk_mv_type[s->block_index[0]] = 0;
3878 v->blk_mv_type[s->block_index[1]] = 0;
3879 v->blk_mv_type[s->block_index[2]] = 0;
3880 v->blk_mv_type[s->block_index[3]] = 0;
3881 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3884 if (s->mb_x == s->mb_width - 1)
3885 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3889 static int vc1_decode_p_mb_intfi(VC1Context *v)
3891 MpegEncContext *s = &v->s;
3892 GetBitContext *gb = &s->gb;
3894 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3895 int cbp = 0; /* cbp decoding stuff */
3896 int mqdiff, mquant; /* MB quantization */
3897 int ttmb = v->ttfrm; /* MB Transform type */
3899 int mb_has_coeffs = 1; /* last_flag */
3900 int dmv_x, dmv_y; /* Differential MV components */
3901 int val; /* temp values */
3902 int first_block = 1;
3905 int block_cbp = 0, pat, block_tt = 0;
3908 mquant = v->pq; /* Lossy initialization */
3910 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3911 if (idx_mbmode <= 1) { // intra MB
3912 s->mb_intra = v->is_intra[s->mb_x] = 1;
3913 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3914 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3915 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3917 s->current_picture.f.qscale_table[mb_pos] = mquant;
3918 /* Set DC scale - y and c use the same (not sure if necessary here) */
3919 s->y_dc_scale = s->y_dc_scale_table[mquant];
3920 s->c_dc_scale = s->c_dc_scale_table[mquant];
3921 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3922 mb_has_coeffs = idx_mbmode & 1;
3924 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3926 for (i = 0; i < 6; i++) {
3927 s->dc_val[0][s->block_index[i]] = 0;
3928 v->mb_type[0][s->block_index[i]] = 1;
3930 val = ((cbp >> (5 - i)) & 1);
3931 v->a_avail = v->c_avail = 0;
3932 if (i == 2 || i == 3 || !s->first_slice_line)
3933 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3934 if (i == 1 || i == 3 || s->mb_x)
3935 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3937 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3938 (i & 4) ? v->codingset2 : v->codingset);
3939 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3941 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3942 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3943 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3944 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3945 // TODO: loop filter
3948 s->mb_intra = v->is_intra[s->mb_x] = 0;
3949 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3950 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3951 if (idx_mbmode <= 5) { // 1-MV
3952 dmv_x = dmv_y = pred_flag = 0;
3953 if (idx_mbmode & 1) {
3954 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3956 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3958 mb_has_coeffs = !(idx_mbmode & 2);
3960 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3961 for (i = 0; i < 6; i++) {
3963 dmv_x = dmv_y = pred_flag = 0;
3964 val = ((v->fourmvbp >> (3 - i)) & 1);
3966 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3968 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3969 vc1_mc_4mv_luma(v, i, 0);
3971 vc1_mc_4mv_chroma(v, 0);
3973 mb_has_coeffs = idx_mbmode & 1;
3976 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3980 s->current_picture.f.qscale_table[mb_pos] = mquant;
3981 if (!v->ttmbf && cbp) {
3982 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3985 for (i = 0; i < 6; i++) {
3986 s->dc_val[0][s->block_index[i]] = 0;
3988 val = ((cbp >> (5 - i)) & 1);
3989 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
3990 if (v->second_field)
3991 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
3993 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3994 first_block, s->dest[dst_idx] + off,
3995 (i & 4) ? s->uvlinesize : s->linesize,
3996 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3998 block_cbp |= pat << (i << 2);
3999 if (!v->ttmbf && ttmb < 8) ttmb = -1;
4004 if (s->mb_x == s->mb_width - 1)
4005 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4009 /** Decode one B-frame MB (in Main profile)
4011 static void vc1_decode_b_mb(VC1Context *v)
4013 MpegEncContext *s = &v->s;
4014 GetBitContext *gb = &s->gb;
4016 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4017 int cbp = 0; /* cbp decoding stuff */
4018 int mqdiff, mquant; /* MB quantization */
4019 int ttmb = v->ttfrm; /* MB Transform type */
4020 int mb_has_coeffs = 0; /* last_flag */
4021 int index, index1; /* LUT indexes */
4022 int val, sign; /* temp values */
4023 int first_block = 1;
4025 int skipped, direct;
4026 int dmv_x[2], dmv_y[2];
4027 int bmvtype = BMV_TYPE_BACKWARD;
4029 mquant = v->pq; /* lossy initialization */
4033 direct = get_bits1(gb);
4035 direct = v->direct_mb_plane[mb_pos];
4037 skipped = get_bits1(gb);
4039 skipped = v->s.mbskip_table[mb_pos];
4041 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4042 for (i = 0; i < 6; i++) {
4043 v->mb_type[0][s->block_index[i]] = 0;
4044 s->dc_val[0][s->block_index[i]] = 0;
4046 s->current_picture.f.qscale_table[mb_pos] = 0;
4050 GET_MVDATA(dmv_x[0], dmv_y[0]);
4051 dmv_x[1] = dmv_x[0];
4052 dmv_y[1] = dmv_y[0];
4054 if (skipped || !s->mb_intra) {
4055 bmvtype = decode012(gb);
4058 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4061 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4064 bmvtype = BMV_TYPE_INTERPOLATED;
4065 dmv_x[0] = dmv_y[0] = 0;
4069 for (i = 0; i < 6; i++)
4070 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4074 bmvtype = BMV_TYPE_INTERPOLATED;
4075 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4076 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4080 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4083 s->current_picture.f.qscale_table[mb_pos] = mquant;
4085 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4086 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4087 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4088 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4090 if (!mb_has_coeffs && !s->mb_intra) {
4091 /* no coded blocks - effectively skipped */
4092 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4093 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4096 if (s->mb_intra && !mb_has_coeffs) {
4098 s->current_picture.f.qscale_table[mb_pos] = mquant;
4099 s->ac_pred = get_bits1(gb);
4101 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4103 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4104 GET_MVDATA(dmv_x[0], dmv_y[0]);
4105 if (!mb_has_coeffs) {
4106 /* interpolated skipped block */
4107 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4108 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4112 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4114 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4117 s->ac_pred = get_bits1(gb);
4118 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4120 s->current_picture.f.qscale_table[mb_pos] = mquant;
4121 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4122 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4126 for (i = 0; i < 6; i++) {
4127 s->dc_val[0][s->block_index[i]] = 0;
4129 val = ((cbp >> (5 - i)) & 1);
4130 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4131 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4133 /* check if prediction blocks A and C are available */
4134 v->a_avail = v->c_avail = 0;
4135 if (i == 2 || i == 3 || !s->first_slice_line)
4136 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4137 if (i == 1 || i == 3 || s->mb_x)
4138 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4140 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4141 (i & 4) ? v->codingset2 : v->codingset);
4142 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4144 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4146 for (j = 0; j < 64; j++)
4147 s->block[i][j] <<= 1;
4148 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4150 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4151 first_block, s->dest[dst_idx] + off,
4152 (i & 4) ? s->uvlinesize : s->linesize,
4153 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4154 if (!v->ttmbf && ttmb < 8)
4161 /** Decode one B-frame MB (in interlaced field B picture)
4163 static void vc1_decode_b_mb_intfi(VC1Context *v)
4165 MpegEncContext *s = &v->s;
4166 GetBitContext *gb = &s->gb;
4168 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4169 int cbp = 0; /* cbp decoding stuff */
4170 int mqdiff, mquant; /* MB quantization */
4171 int ttmb = v->ttfrm; /* MB Transform type */
4172 int mb_has_coeffs = 0; /* last_flag */
4173 int val; /* temp value */
4174 int first_block = 1;
4177 int dmv_x[2], dmv_y[2], pred_flag[2];
4178 int bmvtype = BMV_TYPE_BACKWARD;
4179 int idx_mbmode, interpmvp;
4181 mquant = v->pq; /* Lossy initialization */
4184 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4185 if (idx_mbmode <= 1) { // intra MB
4186 s->mb_intra = v->is_intra[s->mb_x] = 1;
4187 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4188 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4189 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4191 s->current_picture.f.qscale_table[mb_pos] = mquant;
4192 /* Set DC scale - y and c use the same (not sure if necessary here) */
4193 s->y_dc_scale = s->y_dc_scale_table[mquant];
4194 s->c_dc_scale = s->c_dc_scale_table[mquant];
4195 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4196 mb_has_coeffs = idx_mbmode & 1;
4198 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4200 for (i = 0; i < 6; i++) {
4201 s->dc_val[0][s->block_index[i]] = 0;
4203 val = ((cbp >> (5 - i)) & 1);
4204 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4205 v->a_avail = v->c_avail = 0;
4206 if (i == 2 || i == 3 || !s->first_slice_line)
4207 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4208 if (i == 1 || i == 3 || s->mb_x)
4209 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4211 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4212 (i & 4) ? v->codingset2 : v->codingset);
4213 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4215 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4217 for (j = 0; j < 64; j++)
4218 s->block[i][j] <<= 1;
4219 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4220 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4221 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4222 // TODO: yet to perform loop filter
4225 s->mb_intra = v->is_intra[s->mb_x] = 0;
4226 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4227 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4229 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4231 fwd = v->forward_mb_plane[mb_pos];
4232 if (idx_mbmode <= 5) { // 1-MV
4233 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4234 pred_flag[0] = pred_flag[1] = 0;
4236 bmvtype = BMV_TYPE_FORWARD;
4238 bmvtype = decode012(gb);
4241 bmvtype = BMV_TYPE_BACKWARD;
4244 bmvtype = BMV_TYPE_DIRECT;
4247 bmvtype = BMV_TYPE_INTERPOLATED;
4248 interpmvp = get_bits1(gb);
4251 v->bmvtype = bmvtype;
4252 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4253 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4255 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4256 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4258 if (bmvtype == BMV_TYPE_DIRECT) {
4259 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4260 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4262 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4263 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4264 mb_has_coeffs = !(idx_mbmode & 2);
4267 bmvtype = BMV_TYPE_FORWARD;
4268 v->bmvtype = bmvtype;
4269 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4270 for (i = 0; i < 6; i++) {
4272 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4273 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4274 val = ((v->fourmvbp >> (3 - i)) & 1);
4276 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4277 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4278 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4280 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4281 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4283 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4285 mb_has_coeffs = idx_mbmode & 1;
4288 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4292 s->current_picture.f.qscale_table[mb_pos] = mquant;
4293 if (!v->ttmbf && cbp) {
4294 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4297 for (i = 0; i < 6; i++) {
4298 s->dc_val[0][s->block_index[i]] = 0;
4300 val = ((cbp >> (5 - i)) & 1);
4301 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4302 if (v->second_field)
4303 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4305 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4306 first_block, s->dest[dst_idx] + off,
4307 (i & 4) ? s->uvlinesize : s->linesize,
4308 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4309 if (!v->ttmbf && ttmb < 8)
4317 /** Decode blocks of I-frame
4319 static void vc1_decode_i_blocks(VC1Context *v)
4322 MpegEncContext *s = &v->s;
4327 /* select codingmode used for VLC tables selection */
4328 switch (v->y_ac_table_index) {
4330 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4333 v->codingset = CS_HIGH_MOT_INTRA;
4336 v->codingset = CS_MID_RATE_INTRA;
4340 switch (v->c_ac_table_index) {
4342 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4345 v->codingset2 = CS_HIGH_MOT_INTER;
4348 v->codingset2 = CS_MID_RATE_INTER;
4352 /* Set DC scale - y and c use the same */
4353 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4354 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4357 s->mb_x = s->mb_y = 0;
4359 s->first_slice_line = 1;
4360 for (s->mb_y = 0; s->mb_y < s->end_mb_y; s->mb_y++) {
4362 ff_init_block_index(s);
4363 for (; s->mb_x < v->end_mb_x; s->mb_x++) {
4365 ff_update_block_index(s);
4366 dst[0] = s->dest[0];
4367 dst[1] = dst[0] + 8;
4368 dst[2] = s->dest[0] + s->linesize * 8;
4369 dst[3] = dst[2] + 8;
4370 dst[4] = s->dest[1];
4371 dst[5] = s->dest[2];
4372 s->dsp.clear_blocks(s->block[0]);
4373 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4374 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4375 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4376 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4377 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4379 // do actual MB decoding and displaying
4380 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4381 v->s.ac_pred = get_bits1(&v->s.gb);
4383 for (k = 0; k < 6; k++) {
4384 val = ((cbp >> (5 - k)) & 1);
4387 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4391 cbp |= val << (5 - k);
4393 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4395 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4397 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4398 if (v->pq >= 9 && v->overlap) {
4400 for (j = 0; j < 64; j++)
4401 s->block[k][j] <<= 1;
4402 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4405 for (j = 0; j < 64; j++)
4406 s->block[k][j] = (s->block[k][j] - 64) << 1;
4407 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4411 if (v->pq >= 9 && v->overlap) {
4413 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4414 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4415 if (!(s->flags & CODEC_FLAG_GRAY)) {
4416 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4417 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4420 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4421 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4422 if (!s->first_slice_line) {
4423 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4424 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4425 if (!(s->flags & CODEC_FLAG_GRAY)) {
4426 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4427 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4430 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4431 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4433 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4435 if (get_bits_count(&s->gb) > v->bits) {
4436 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4437 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4438 get_bits_count(&s->gb), v->bits);
4442 if (!v->s.loop_filter)
4443 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4445 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4447 s->first_slice_line = 0;
4449 if (v->s.loop_filter)
4450 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4452 /* This is intentionally mb_height and not end_mb_y - unlike in advanced
4453 * profile, these only differ are when decoding MSS2 rectangles. */
4454 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4457 /** Decode blocks of I-frame for advanced profile
4459 static void vc1_decode_i_blocks_adv(VC1Context *v)
4462 MpegEncContext *s = &v->s;
4468 GetBitContext *gb = &s->gb;
4470 /* select codingmode used for VLC tables selection */
4471 switch (v->y_ac_table_index) {
4473 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4476 v->codingset = CS_HIGH_MOT_INTRA;
4479 v->codingset = CS_MID_RATE_INTRA;
4483 switch (v->c_ac_table_index) {
4485 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4488 v->codingset2 = CS_HIGH_MOT_INTER;
4491 v->codingset2 = CS_MID_RATE_INTER;
4496 s->mb_x = s->mb_y = 0;
4498 s->first_slice_line = 1;
4499 s->mb_y = s->start_mb_y;
4500 if (s->start_mb_y) {
4502 ff_init_block_index(s);
4503 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4504 (1 + s->b8_stride) * sizeof(*s->coded_block));
4506 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4508 ff_init_block_index(s);
4509 for (;s->mb_x < s->mb_width; s->mb_x++) {
4510 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4511 ff_update_block_index(s);
4512 s->dsp.clear_blocks(block[0]);
4513 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4514 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4515 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4516 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4518 // do actual MB decoding and displaying
4519 if (v->fieldtx_is_raw)
4520 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4521 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4522 if ( v->acpred_is_raw)
4523 v->s.ac_pred = get_bits1(&v->s.gb);
4525 v->s.ac_pred = v->acpred_plane[mb_pos];
4527 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4528 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4532 s->current_picture.f.qscale_table[mb_pos] = mquant;
4533 /* Set DC scale - y and c use the same */
4534 s->y_dc_scale = s->y_dc_scale_table[mquant];
4535 s->c_dc_scale = s->c_dc_scale_table[mquant];
4537 for (k = 0; k < 6; k++) {
4538 val = ((cbp >> (5 - k)) & 1);
4541 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4545 cbp |= val << (5 - k);
4547 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4548 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4550 vc1_decode_i_block_adv(v, block[k], k, val,
4551 (k < 4) ? v->codingset : v->codingset2, mquant);
4553 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4555 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4558 vc1_smooth_overlap_filter_iblk(v);
4559 vc1_put_signed_blocks_clamped(v);
4560 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4562 if (get_bits_count(&s->gb) > v->bits) {
4563 // TODO: may need modification to handle slice coding
4564 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4565 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4566 get_bits_count(&s->gb), v->bits);
4570 if (!v->s.loop_filter)
4571 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4573 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4574 s->first_slice_line = 0;
4577 /* raw bottom MB row */
4579 ff_init_block_index(s);
4580 for (;s->mb_x < s->mb_width; s->mb_x++) {
4581 ff_update_block_index(s);
4582 vc1_put_signed_blocks_clamped(v);
4583 if (v->s.loop_filter)
4584 vc1_loop_filter_iblk_delayed(v, v->pq);
4586 if (v->s.loop_filter)
4587 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4588 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4589 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4592 static void vc1_decode_p_blocks(VC1Context *v)
4594 MpegEncContext *s = &v->s;
4595 int apply_loop_filter;
4597 /* select codingmode used for VLC tables selection */
4598 switch (v->c_ac_table_index) {
4600 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4603 v->codingset = CS_HIGH_MOT_INTRA;
4606 v->codingset = CS_MID_RATE_INTRA;
4610 switch (v->c_ac_table_index) {
4612 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4615 v->codingset2 = CS_HIGH_MOT_INTER;
4618 v->codingset2 = CS_MID_RATE_INTER;
4622 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4623 s->first_slice_line = 1;
4624 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4625 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4627 ff_init_block_index(s);
4628 for (; s->mb_x < s->mb_width; s->mb_x++) {
4629 ff_update_block_index(s);
4631 if (v->fcm == ILACE_FIELD)
4632 vc1_decode_p_mb_intfi(v);
4633 else if (v->fcm == ILACE_FRAME)
4634 vc1_decode_p_mb_intfr(v);
4635 else vc1_decode_p_mb(v);
4636 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4637 vc1_apply_p_loop_filter(v);
4638 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4639 // TODO: may need modification to handle slice coding
4640 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4641 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4642 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4646 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4647 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4648 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4649 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4650 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4651 s->first_slice_line = 0;
4653 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4655 ff_init_block_index(s);
4656 for (; s->mb_x < s->mb_width; s->mb_x++) {
4657 ff_update_block_index(s);
4658 vc1_apply_p_loop_filter(v);
4661 if (s->end_mb_y >= s->start_mb_y)
4662 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4663 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4664 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4667 static void vc1_decode_b_blocks(VC1Context *v)
4669 MpegEncContext *s = &v->s;
4671 /* select codingmode used for VLC tables selection */
4672 switch (v->c_ac_table_index) {
4674 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4677 v->codingset = CS_HIGH_MOT_INTRA;
4680 v->codingset = CS_MID_RATE_INTRA;
4684 switch (v->c_ac_table_index) {
4686 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4689 v->codingset2 = CS_HIGH_MOT_INTER;
4692 v->codingset2 = CS_MID_RATE_INTER;
4696 s->first_slice_line = 1;
4697 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4699 ff_init_block_index(s);
4700 for (; s->mb_x < s->mb_width; s->mb_x++) {
4701 ff_update_block_index(s);
4703 if (v->fcm == ILACE_FIELD)
4704 vc1_decode_b_mb_intfi(v);
4707 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4708 // TODO: may need modification to handle slice coding
4709 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4710 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4711 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4714 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4716 if (!v->s.loop_filter)
4717 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4719 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4720 s->first_slice_line = 0;
4722 if (v->s.loop_filter)
4723 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4724 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4725 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4728 static void vc1_decode_skip_blocks(VC1Context *v)
4730 MpegEncContext *s = &v->s;
4732 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4733 s->first_slice_line = 1;
4734 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4736 ff_init_block_index(s);
4737 ff_update_block_index(s);
4738 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4739 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4740 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4741 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4742 s->first_slice_line = 0;
4744 s->pict_type = AV_PICTURE_TYPE_P;
4747 void ff_vc1_decode_blocks(VC1Context *v)
4750 v->s.esc3_level_length = 0;
4752 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4755 v->left_blk_idx = -1;
4756 v->topleft_blk_idx = 1;
4758 switch (v->s.pict_type) {
4759 case AV_PICTURE_TYPE_I:
4760 if (v->profile == PROFILE_ADVANCED)
4761 vc1_decode_i_blocks_adv(v);
4763 vc1_decode_i_blocks(v);
4765 case AV_PICTURE_TYPE_P:
4766 if (v->p_frame_skipped)
4767 vc1_decode_skip_blocks(v);
4769 vc1_decode_p_blocks(v);
4771 case AV_PICTURE_TYPE_B:
4773 if (v->profile == PROFILE_ADVANCED)
4774 vc1_decode_i_blocks_adv(v);
4776 vc1_decode_i_blocks(v);
4778 vc1_decode_b_blocks(v);
4784 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4788 * Transform coefficients for both sprites in 16.16 fixed point format,
4789 * in the order they appear in the bitstream:
4791 * rotation 1 (unused)
4793 * rotation 2 (unused)
4800 int effect_type, effect_flag;
4801 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4802 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4805 static inline int get_fp_val(GetBitContext* gb)
4807 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4810 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4814 switch (get_bits(gb, 2)) {
4817 c[2] = get_fp_val(gb);
4821 c[0] = c[4] = get_fp_val(gb);
4822 c[2] = get_fp_val(gb);
4825 c[0] = get_fp_val(gb);
4826 c[2] = get_fp_val(gb);
4827 c[4] = get_fp_val(gb);
4830 c[0] = get_fp_val(gb);
4831 c[1] = get_fp_val(gb);
4832 c[2] = get_fp_val(gb);
4833 c[3] = get_fp_val(gb);
4834 c[4] = get_fp_val(gb);
4837 c[5] = get_fp_val(gb);
4839 c[6] = get_fp_val(gb);
4844 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4846 AVCodecContext *avctx = v->s.avctx;
4849 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4850 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4851 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4852 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4853 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4854 for (i = 0; i < 7; i++)
4855 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4856 sd->coefs[sprite][i] / (1<<16),
4857 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4858 av_log(avctx, AV_LOG_DEBUG, "\n");
4862 if (sd->effect_type = get_bits_long(gb, 30)) {
4863 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4865 vc1_sprite_parse_transform(gb, sd->effect_params1);
4868 vc1_sprite_parse_transform(gb, sd->effect_params1);
4869 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4872 for (i = 0; i < sd->effect_pcount1; i++)
4873 sd->effect_params1[i] = get_fp_val(gb);
4875 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4876 // effect 13 is simple alpha blending and matches the opacity above
4877 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4878 for (i = 0; i < sd->effect_pcount1; i++)
4879 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4880 sd->effect_params1[i] / (1 << 16),
4881 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4882 av_log(avctx, AV_LOG_DEBUG, "\n");
4885 sd->effect_pcount2 = get_bits(gb, 16);
4886 if (sd->effect_pcount2 > 10) {
4887 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4889 } else if (sd->effect_pcount2) {
4891 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4892 while (++i < sd->effect_pcount2) {
4893 sd->effect_params2[i] = get_fp_val(gb);
4894 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4895 sd->effect_params2[i] / (1 << 16),
4896 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4898 av_log(avctx, AV_LOG_DEBUG, "\n");
4901 if (sd->effect_flag = get_bits1(gb))
4902 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4904 if (get_bits_count(gb) >= gb->size_in_bits +
4905 (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE ? 64 : 0))
4906 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4907 if (get_bits_count(gb) < gb->size_in_bits - 8)
4908 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4911 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4913 int i, plane, row, sprite;
4914 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4915 uint8_t* src_h[2][2];
4916 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4918 MpegEncContext *s = &v->s;
4920 for (i = 0; i < 2; i++) {
4921 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4922 xadv[i] = sd->coefs[i][0];
4923 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4924 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4926 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4927 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4929 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4931 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4932 int width = v->output_width>>!!plane;
4934 for (row = 0; row < v->output_height>>!!plane; row++) {
4935 uint8_t *dst = v->sprite_output_frame.data[plane] +
4936 v->sprite_output_frame.linesize[plane] * row;
4938 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4939 uint8_t *iplane = s->current_picture.f.data[plane];
4940 int iline = s->current_picture.f.linesize[plane];
4941 int ycoord = yoff[sprite] + yadv[sprite] * row;
4942 int yline = ycoord >> 16;
4944 ysub[sprite] = ycoord & 0xFFFF;
4946 iplane = s->last_picture.f.data[plane];
4947 iline = s->last_picture.f.linesize[plane];
4949 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4950 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4951 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4953 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4955 if (sr_cache[sprite][0] != yline) {
4956 if (sr_cache[sprite][1] == yline) {
4957 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4958 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4960 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4961 sr_cache[sprite][0] = yline;
4964 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4965 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4966 iplane + next_line, xoff[sprite],
4967 xadv[sprite], width);
4968 sr_cache[sprite][1] = yline + 1;
4970 src_h[sprite][0] = v->sr_rows[sprite][0];
4971 src_h[sprite][1] = v->sr_rows[sprite][1];
4975 if (!v->two_sprites) {
4977 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4979 memcpy(dst, src_h[0][0], width);
4982 if (ysub[0] && ysub[1]) {
4983 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4984 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4985 } else if (ysub[0]) {
4986 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
4987 src_h[1][0], alpha, width);
4988 } else if (ysub[1]) {
4989 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
4990 src_h[0][0], (1<<16)-1-alpha, width);
4992 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
4998 for (i = 0; i < 2; i++) {
5008 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5010 MpegEncContext *s = &v->s;
5011 AVCodecContext *avctx = s->avctx;
5014 vc1_parse_sprites(v, gb, &sd);
5016 if (!s->current_picture.f.data[0]) {
5017 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5021 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5022 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5026 if (v->sprite_output_frame.data[0])
5027 avctx->release_buffer(avctx, &v->sprite_output_frame);
5029 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5030 v->sprite_output_frame.reference = 0;
5031 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
5032 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5036 vc1_draw_sprites(v, &sd);
5041 static void vc1_sprite_flush(AVCodecContext *avctx)
5043 VC1Context *v = avctx->priv_data;
5044 MpegEncContext *s = &v->s;
5045 AVFrame *f = &s->current_picture.f;
5048 /* Windows Media Image codecs have a convergence interval of two keyframes.
5049 Since we can't enforce it, clear to black the missing sprite. This is
5050 wrong but it looks better than doing nothing. */
5053 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5054 for (i = 0; i < v->sprite_height>>!!plane; i++)
5055 memset(f->data[plane] + i * f->linesize[plane],
5056 plane ? 128 : 0, f->linesize[plane]);
5061 av_cold int ff_vc1_decode_init_alloc_tables(VC1Context *v)
5063 MpegEncContext *s = &v->s;
5066 /* Allocate mb bitplanes */
5067 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5068 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5069 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5070 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5071 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5072 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5074 v->n_allocated_blks = s->mb_width + 2;
5075 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5076 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5077 v->cbp = v->cbp_base + s->mb_stride;
5078 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5079 v->ttblk = v->ttblk_base + s->mb_stride;
5080 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5081 v->is_intra = v->is_intra_base + s->mb_stride;
5082 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5083 v->luma_mv = v->luma_mv_base + s->mb_stride;
5085 /* allocate block type info in that way so it could be used with s->block_index[] */
5086 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5087 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5088 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5089 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5091 /* allocate memory to store block level MV info */
5092 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5093 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5094 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5095 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5096 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5097 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5098 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5099 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);
5100 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5101 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5102 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);
5104 /* Init coded blocks info */
5105 if (v->profile == PROFILE_ADVANCED) {
5106 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5108 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5112 ff_intrax8_common_init(&v->x8,s);
5114 if (s->avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || s->avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5115 for (i = 0; i < 4; i++)
5116 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5119 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5120 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5127 av_cold void ff_vc1_init_transposed_scantables(VC1Context *v)
5130 for (i = 0; i < 64; i++) {
5131 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5132 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5133 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5134 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5135 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5136 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5142 /** Initialize a VC1/WMV3 decoder
5143 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5144 * @todo TODO: Decypher remaining bits in extra_data
5146 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5148 VC1Context *v = avctx->priv_data;
5149 MpegEncContext *s = &v->s;
5152 /* save the container output size for WMImage */
5153 v->output_width = avctx->width;
5154 v->output_height = avctx->height;
5156 if (!avctx->extradata_size || !avctx->extradata)
5158 if (!(avctx->flags & CODEC_FLAG_GRAY))
5159 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5161 avctx->pix_fmt = AV_PIX_FMT_GRAY8;
5162 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5164 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5165 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5167 if (avctx->idct_algo == FF_IDCT_AUTO) {
5168 avctx->idct_algo = FF_IDCT_WMV2;
5171 if (ff_vc1_init_common(v) < 0)
5173 ff_vc1dsp_init(&v->vc1dsp);
5175 if (avctx->codec_id == AV_CODEC_ID_WMV3 || avctx->codec_id == AV_CODEC_ID_WMV3IMAGE) {
5178 // looks like WMV3 has a sequence header stored in the extradata
5179 // advanced sequence header may be before the first frame
5180 // the last byte of the extradata is a version number, 1 for the
5181 // samples we can decode
5183 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5185 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5188 count = avctx->extradata_size*8 - get_bits_count(&gb);
5190 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5191 count, get_bits(&gb, count));
5192 } else if (count < 0) {
5193 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5195 } else { // VC1/WVC1/WVP2
5196 const uint8_t *start = avctx->extradata;
5197 uint8_t *end = avctx->extradata + avctx->extradata_size;
5198 const uint8_t *next;
5199 int size, buf2_size;
5200 uint8_t *buf2 = NULL;
5201 int seq_initialized = 0, ep_initialized = 0;
5203 if (avctx->extradata_size < 16) {
5204 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5208 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5209 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5211 for (; next < end; start = next) {
5212 next = find_next_marker(start + 4, end);
5213 size = next - start - 4;
5216 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5217 init_get_bits(&gb, buf2, buf2_size * 8);
5218 switch (AV_RB32(start)) {
5219 case VC1_CODE_SEQHDR:
5220 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5224 seq_initialized = 1;
5226 case VC1_CODE_ENTRYPOINT:
5227 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5236 if (!seq_initialized || !ep_initialized) {
5237 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5240 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5243 avctx->profile = v->profile;
5244 if (v->profile == PROFILE_ADVANCED)
5245 avctx->level = v->level;
5247 avctx->has_b_frames = !!avctx->max_b_frames;
5249 s->mb_width = (avctx->coded_width + 15) >> 4;
5250 s->mb_height = (avctx->coded_height + 15) >> 4;
5252 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5253 ff_vc1_init_transposed_scantables(v);
5255 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5260 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5261 v->sprite_width = avctx->coded_width;
5262 v->sprite_height = avctx->coded_height;
5264 avctx->coded_width = avctx->width = v->output_width;
5265 avctx->coded_height = avctx->height = v->output_height;
5267 // prevent 16.16 overflows
5268 if (v->sprite_width > 1 << 14 ||
5269 v->sprite_height > 1 << 14 ||
5270 v->output_width > 1 << 14 ||
5271 v->output_height > 1 << 14) return -1;
5276 /** Close a VC1/WMV3 decoder
5277 * @warning Initial try at using MpegEncContext stuff
5279 av_cold int ff_vc1_decode_end(AVCodecContext *avctx)
5281 VC1Context *v = avctx->priv_data;
5284 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5285 && v->sprite_output_frame.data[0])
5286 avctx->release_buffer(avctx, &v->sprite_output_frame);
5287 for (i = 0; i < 4; i++)
5288 av_freep(&v->sr_rows[i >> 1][i & 1]);
5289 av_freep(&v->hrd_rate);
5290 av_freep(&v->hrd_buffer);
5291 ff_MPV_common_end(&v->s);
5292 av_freep(&v->mv_type_mb_plane);
5293 av_freep(&v->direct_mb_plane);
5294 av_freep(&v->forward_mb_plane);
5295 av_freep(&v->fieldtx_plane);
5296 av_freep(&v->acpred_plane);
5297 av_freep(&v->over_flags_plane);
5298 av_freep(&v->mb_type_base);
5299 av_freep(&v->blk_mv_type_base);
5300 av_freep(&v->mv_f_base);
5301 av_freep(&v->mv_f_last_base);
5302 av_freep(&v->mv_f_next_base);
5303 av_freep(&v->block);
5304 av_freep(&v->cbp_base);
5305 av_freep(&v->ttblk_base);
5306 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5307 av_freep(&v->luma_mv_base);
5308 ff_intrax8_common_end(&v->x8);
5313 /** Decode a VC1/WMV3 frame
5314 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5316 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5317 int *data_size, AVPacket *avpkt)
5319 const uint8_t *buf = avpkt->data;
5320 int buf_size = avpkt->size, n_slices = 0, i;
5321 VC1Context *v = avctx->priv_data;
5322 MpegEncContext *s = &v->s;
5323 AVFrame *pict = data;
5324 uint8_t *buf2 = NULL;
5325 const uint8_t *buf_start = buf;
5326 int mb_height, n_slices1=-1;
5331 } *slices = NULL, *tmp;
5333 if(s->flags & CODEC_FLAG_LOW_DELAY)
5336 /* no supplementary picture */
5337 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5338 /* special case for last picture */
5339 if (s->low_delay == 0 && s->next_picture_ptr) {
5340 *pict = s->next_picture_ptr->f;
5341 s->next_picture_ptr = NULL;
5343 *data_size = sizeof(AVFrame);
5349 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5350 if (v->profile < PROFILE_ADVANCED)
5351 avctx->pix_fmt = AV_PIX_FMT_VDPAU_WMV3;
5353 avctx->pix_fmt = AV_PIX_FMT_VDPAU_VC1;
5356 //for advanced profile we may need to parse and unescape data
5357 if (avctx->codec_id == AV_CODEC_ID_VC1 || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5359 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5361 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5362 const uint8_t *start, *end, *next;
5366 for (start = buf, end = buf + buf_size; next < end; start = next) {
5367 next = find_next_marker(start + 4, end);
5368 size = next - start - 4;
5369 if (size <= 0) continue;
5370 switch (AV_RB32(start)) {
5371 case VC1_CODE_FRAME:
5372 if (avctx->hwaccel ||
5373 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5375 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5377 case VC1_CODE_FIELD: {
5379 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5383 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5384 if (!slices[n_slices].buf)
5386 buf_size3 = vc1_unescape_buffer(start + 4, size,
5387 slices[n_slices].buf);
5388 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5390 /* assuming that the field marker is at the exact middle,
5391 hope it's correct */
5392 slices[n_slices].mby_start = s->mb_height >> 1;
5393 n_slices1 = n_slices - 1; // index of the last slice of the first field
5397 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5398 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5399 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5400 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5402 case VC1_CODE_SLICE: {
5404 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5408 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5409 if (!slices[n_slices].buf)
5411 buf_size3 = vc1_unescape_buffer(start + 4, size,
5412 slices[n_slices].buf);
5413 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5415 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5421 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5422 const uint8_t *divider;
5425 divider = find_next_marker(buf, buf + buf_size);
5426 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5427 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5429 } else { // found field marker, unescape second field
5430 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5434 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5435 if (!slices[n_slices].buf)
5437 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5438 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5440 slices[n_slices].mby_start = s->mb_height >> 1;
5441 n_slices1 = n_slices - 1;
5444 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5446 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5448 init_get_bits(&s->gb, buf2, buf_size2*8);
5450 init_get_bits(&s->gb, buf, buf_size*8);
5452 if (v->res_sprite) {
5453 v->new_sprite = !get_bits1(&s->gb);
5454 v->two_sprites = get_bits1(&s->gb);
5455 /* res_sprite means a Windows Media Image stream, AV_CODEC_ID_*IMAGE means
5456 we're using the sprite compositor. These are intentionally kept separate
5457 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5458 the vc1 one for WVP2 */
5459 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5460 if (v->new_sprite) {
5461 // switch AVCodecContext parameters to those of the sprites
5462 avctx->width = avctx->coded_width = v->sprite_width;
5463 avctx->height = avctx->coded_height = v->sprite_height;
5470 if (s->context_initialized &&
5471 (s->width != avctx->coded_width ||
5472 s->height != avctx->coded_height)) {
5473 ff_vc1_decode_end(avctx);
5476 if (!s->context_initialized) {
5477 if (ff_msmpeg4_decode_init(avctx) < 0 || ff_vc1_decode_init_alloc_tables(v) < 0)
5480 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5482 if (v->profile == PROFILE_ADVANCED) {
5483 s->h_edge_pos = avctx->coded_width;
5484 s->v_edge_pos = avctx->coded_height;
5488 /* We need to set current_picture_ptr before reading the header,
5489 * otherwise we cannot store anything in there. */
5490 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5491 int i = ff_find_unused_picture(s, 0);
5494 s->current_picture_ptr = &s->picture[i];
5497 // do parse frame header
5498 v->pic_header_flag = 0;
5499 if (v->profile < PROFILE_ADVANCED) {
5500 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5504 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5509 if (avctx->debug & FF_DEBUG_PICT_INFO)
5510 av_log(v->s.avctx, AV_LOG_DEBUG, "pict_type: %c\n", av_get_picture_type_char(s->pict_type));
5512 if ((avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE)
5513 && s->pict_type != AV_PICTURE_TYPE_I) {
5514 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5518 // process pulldown flags
5519 s->current_picture_ptr->f.repeat_pict = 0;
5520 // Pulldown flags are only valid when 'broadcast' has been set.
5521 // So ticks_per_frame will be 2
5524 s->current_picture_ptr->f.repeat_pict = 1;
5525 } else if (v->rptfrm) {
5527 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5530 // for skipping the frame
5531 s->current_picture.f.pict_type = s->pict_type;
5532 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5534 /* skip B-frames if we don't have reference frames */
5535 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
5538 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5539 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5540 avctx->skip_frame >= AVDISCARD_ALL) {
5544 if (s->next_p_frame_damaged) {
5545 if (s->pict_type == AV_PICTURE_TYPE_B)
5548 s->next_p_frame_damaged = 0;
5551 if (ff_MPV_frame_start(s, avctx) < 0) {
5555 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5556 v->s.current_picture_ptr->f.top_field_first = v->tff;
5558 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5559 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5561 if ((CONFIG_VC1_VDPAU_DECODER)
5562 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5563 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5564 else if (avctx->hwaccel) {
5565 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
5567 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5569 if (avctx->hwaccel->end_frame(avctx) < 0)
5572 ff_er_frame_start(s);
5574 v->bits = buf_size * 8;
5575 v->end_mb_x = s->mb_width;
5576 if (v->field_mode) {
5578 s->current_picture.f.linesize[0] <<= 1;
5579 s->current_picture.f.linesize[1] <<= 1;
5580 s->current_picture.f.linesize[2] <<= 1;
5582 s->uvlinesize <<= 1;
5583 tmp[0] = v->mv_f_last[0];
5584 tmp[1] = v->mv_f_last[1];
5585 v->mv_f_last[0] = v->mv_f_next[0];
5586 v->mv_f_last[1] = v->mv_f_next[1];
5587 v->mv_f_next[0] = v->mv_f[0];
5588 v->mv_f_next[1] = v->mv_f[1];
5589 v->mv_f[0] = tmp[0];
5590 v->mv_f[1] = tmp[1];
5592 mb_height = s->mb_height >> v->field_mode;
5593 for (i = 0; i <= n_slices; i++) {
5594 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5595 if (v->field_mode <= 0) {
5596 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5597 "picture boundary (%d >= %d)\n", i,
5598 slices[i - 1].mby_start, mb_height);
5601 v->second_field = 1;
5602 v->blocks_off = s->mb_width * s->mb_height << 1;
5603 v->mb_off = s->mb_stride * s->mb_height >> 1;
5605 v->second_field = 0;
5610 v->pic_header_flag = 0;
5611 if (v->field_mode && i == n_slices1 + 2) {
5612 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5613 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5616 } else if (get_bits1(&s->gb)) {
5617 v->pic_header_flag = 1;
5618 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5619 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5624 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5625 if (!v->field_mode || v->second_field)
5626 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5628 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5629 if (s->end_mb_y <= s->start_mb_y) {
5630 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5633 ff_vc1_decode_blocks(v);
5635 s->gb = slices[i].gb;
5637 if (v->field_mode) {
5638 v->second_field = 0;
5639 if (s->pict_type == AV_PICTURE_TYPE_B) {
5640 memcpy(v->mv_f_base, v->mv_f_next_base,
5641 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5643 s->current_picture.f.linesize[0] >>= 1;
5644 s->current_picture.f.linesize[1] >>= 1;
5645 s->current_picture.f.linesize[2] >>= 1;
5647 s->uvlinesize >>= 1;
5649 av_dlog(s->avctx, "Consumed %i/%i bits\n",
5650 get_bits_count(&s->gb), s->gb.size_in_bits);
5651 // if (get_bits_count(&s->gb) > buf_size * 8)
5653 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5659 ff_MPV_frame_end(s);
5661 if (avctx->codec_id == AV_CODEC_ID_WMV3IMAGE || avctx->codec_id == AV_CODEC_ID_VC1IMAGE) {
5663 avctx->width = avctx->coded_width = v->output_width;
5664 avctx->height = avctx->coded_height = v->output_height;
5665 if (avctx->skip_frame >= AVDISCARD_NONREF)
5667 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5668 if (vc1_decode_sprites(v, &s->gb))
5671 *pict = v->sprite_output_frame;
5672 *data_size = sizeof(AVFrame);
5674 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5675 *pict = s->current_picture_ptr->f;
5676 } else if (s->last_picture_ptr != NULL) {
5677 *pict = s->last_picture_ptr->f;
5679 if (s->last_picture_ptr || s->low_delay) {
5680 *data_size = sizeof(AVFrame);
5681 ff_print_debug_info(s, pict);
5687 for (i = 0; i < n_slices; i++)
5688 av_free(slices[i].buf);
5694 for (i = 0; i < n_slices; i++)
5695 av_free(slices[i].buf);
5701 static const AVProfile profiles[] = {
5702 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5703 { FF_PROFILE_VC1_MAIN, "Main" },
5704 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5705 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5706 { FF_PROFILE_UNKNOWN },
5709 AVCodec ff_vc1_decoder = {
5711 .type = AVMEDIA_TYPE_VIDEO,
5712 .id = AV_CODEC_ID_VC1,
5713 .priv_data_size = sizeof(VC1Context),
5714 .init = vc1_decode_init,
5715 .close = ff_vc1_decode_end,
5716 .decode = vc1_decode_frame,
5717 .flush = ff_mpeg_flush,
5718 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5719 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5720 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5721 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5724 #if CONFIG_WMV3_DECODER
5725 AVCodec ff_wmv3_decoder = {
5727 .type = AVMEDIA_TYPE_VIDEO,
5728 .id = AV_CODEC_ID_WMV3,
5729 .priv_data_size = sizeof(VC1Context),
5730 .init = vc1_decode_init,
5731 .close = ff_vc1_decode_end,
5732 .decode = vc1_decode_frame,
5733 .flush = ff_mpeg_flush,
5734 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5735 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5736 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5737 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5741 #if CONFIG_WMV3_VDPAU_DECODER
5742 AVCodec ff_wmv3_vdpau_decoder = {
5743 .name = "wmv3_vdpau",
5744 .type = AVMEDIA_TYPE_VIDEO,
5745 .id = AV_CODEC_ID_WMV3,
5746 .priv_data_size = sizeof(VC1Context),
5747 .init = vc1_decode_init,
5748 .close = ff_vc1_decode_end,
5749 .decode = vc1_decode_frame,
5750 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5751 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5752 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_WMV3, AV_PIX_FMT_NONE },
5753 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5757 #if CONFIG_VC1_VDPAU_DECODER
5758 AVCodec ff_vc1_vdpau_decoder = {
5759 .name = "vc1_vdpau",
5760 .type = AVMEDIA_TYPE_VIDEO,
5761 .id = AV_CODEC_ID_VC1,
5762 .priv_data_size = sizeof(VC1Context),
5763 .init = vc1_decode_init,
5764 .close = ff_vc1_decode_end,
5765 .decode = vc1_decode_frame,
5766 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5767 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5768 .pix_fmts = (const enum AVPixelFormat[]){ AV_PIX_FMT_VDPAU_VC1, AV_PIX_FMT_NONE },
5769 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5773 #if CONFIG_WMV3IMAGE_DECODER
5774 AVCodec ff_wmv3image_decoder = {
5775 .name = "wmv3image",
5776 .type = AVMEDIA_TYPE_VIDEO,
5777 .id = AV_CODEC_ID_WMV3IMAGE,
5778 .priv_data_size = sizeof(VC1Context),
5779 .init = vc1_decode_init,
5780 .close = ff_vc1_decode_end,
5781 .decode = vc1_decode_frame,
5782 .capabilities = CODEC_CAP_DR1,
5783 .flush = vc1_sprite_flush,
5784 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5785 .pix_fmts = ff_pixfmt_list_420
5789 #if CONFIG_VC1IMAGE_DECODER
5790 AVCodec ff_vc1image_decoder = {
5792 .type = AVMEDIA_TYPE_VIDEO,
5793 .id = AV_CODEC_ID_VC1IMAGE,
5794 .priv_data_size = sizeof(VC1Context),
5795 .init = vc1_decode_init,
5796 .close = ff_vc1_decode_end,
5797 .decode = vc1_decode_frame,
5798 .capabilities = CODEC_CAP_DR1,
5799 .flush = vc1_sprite_flush,
5800 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5801 .pix_fmts = ff_pixfmt_list_420