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 - s->mspel) > v_edge_pos - (my&3) - 16 - s->mspel * 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);
799 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
800 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
801 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
802 return; //no need to do MC for intra blocks
806 if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
808 valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
810 chroma_ref_type = !v->cur_field_type;
812 if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
814 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
815 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
816 uvmx = (tx + ((tx & 3) == 3)) >> 1;
817 uvmy = (ty + ((ty & 3) == 3)) >> 1;
819 v->luma_mv[s->mb_x][0] = uvmx;
820 v->luma_mv[s->mb_x][1] = uvmy;
823 uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
824 uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
826 // Field conversion bias
827 if (v->cur_field_type != chroma_ref_type)
828 uvmy += 2 - 4 * chroma_ref_type;
830 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
831 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
833 if (v->profile != PROFILE_ADVANCED) {
834 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
835 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
837 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
838 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
843 if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
844 srcU = s->current_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
845 srcV = s->current_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
847 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
848 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
851 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
852 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
855 srcU = s->next_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
856 srcV = s->next_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
860 if (chroma_ref_type) {
861 srcU += s->current_picture_ptr->f.linesize[1];
862 srcV += s->current_picture_ptr->f.linesize[2];
864 off = v->second_field ? s->current_picture_ptr->f.linesize[1] : 0;
867 if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
868 || s->h_edge_pos < 18 || v_edge_pos < 18
869 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
870 || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
871 s->dsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
872 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
873 s->h_edge_pos >> 1, v_edge_pos >> 1);
874 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
875 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
876 s->h_edge_pos >> 1, v_edge_pos >> 1);
877 srcU = s->edge_emu_buffer;
878 srcV = s->edge_emu_buffer + 16;
880 /* if we deal with range reduction we need to scale source blocks */
881 if (v->rangeredfrm) {
887 for (j = 0; j < 9; j++) {
888 for (i = 0; i < 9; i++) {
889 src[i] = ((src[i] - 128) >> 1) + 128;
890 src2[i] = ((src2[i] - 128) >> 1) + 128;
892 src += s->uvlinesize;
893 src2 += s->uvlinesize;
896 /* if we deal with intensity compensation we need to scale source blocks */
897 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
903 for (j = 0; j < 9; j++) {
904 for (i = 0; i < 9; i++) {
905 src[i] = v->lutuv[src[i]];
906 src2[i] = v->lutuv[src2[i]];
908 src += s->uvlinesize;
909 src2 += s->uvlinesize;
914 /* Chroma MC always uses qpel bilinear */
915 uvmx = (uvmx & 3) << 1;
916 uvmy = (uvmy & 3) << 1;
918 dsp->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
919 dsp->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
921 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
922 v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
926 /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
928 static void vc1_mc_4mv_chroma4(VC1Context *v)
930 MpegEncContext *s = &v->s;
931 DSPContext *dsp = &v->s.dsp;
932 uint8_t *srcU, *srcV;
933 int uvsrc_x, uvsrc_y;
934 int uvmx_field[4], uvmy_field[4];
936 int fieldmv = v->blk_mv_type[s->block_index[0]];
937 static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
938 int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
939 int v_edge_pos = s->v_edge_pos >> 1;
941 if (!v->s.last_picture.f.data[0])
943 if (s->flags & CODEC_FLAG_GRAY)
946 for (i = 0; i < 4; i++) {
948 uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
951 uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
953 uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
956 for (i = 0; i < 4; i++) {
957 off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
958 uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
959 uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
960 // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
961 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
962 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
963 srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
964 srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
965 uvmx_field[i] = (uvmx_field[i] & 3) << 1;
966 uvmy_field[i] = (uvmy_field[i] & 3) << 1;
968 if (fieldmv && !(uvsrc_y & 1))
970 if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
972 if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
973 || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
974 || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
975 || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
976 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
977 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
978 s->h_edge_pos >> 1, v_edge_pos);
979 s->dsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
980 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
981 s->h_edge_pos >> 1, v_edge_pos);
982 srcU = s->edge_emu_buffer;
983 srcV = s->edge_emu_buffer + 16;
985 /* if we deal with intensity compensation we need to scale source blocks */
986 if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
992 for (j = 0; j < 5; j++) {
993 for (i = 0; i < 5; i++) {
994 src[i] = v->lutuv[src[i]];
995 src2[i] = v->lutuv[src2[i]];
997 src += s->uvlinesize << 1;
998 src2 += s->uvlinesize << 1;
1003 dsp->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1004 dsp->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
1006 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]);
1007 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]);
1012 /***********************************************************************/
1014 * @name VC-1 Block-level functions
1015 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
1021 * @brief Get macroblock-level quantizer scale
1023 #define GET_MQUANT() \
1024 if (v->dquantfrm) { \
1026 if (v->dqprofile == DQPROFILE_ALL_MBS) { \
1027 if (v->dqbilevel) { \
1028 mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
1030 mqdiff = get_bits(gb, 3); \
1032 mquant = v->pq + mqdiff; \
1034 mquant = get_bits(gb, 5); \
1037 if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
1038 edges = 1 << v->dqsbedge; \
1039 else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
1040 edges = (3 << v->dqsbedge) % 15; \
1041 else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
1043 if ((edges&1) && !s->mb_x) \
1044 mquant = v->altpq; \
1045 if ((edges&2) && s->first_slice_line) \
1046 mquant = v->altpq; \
1047 if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
1048 mquant = v->altpq; \
1049 if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
1050 mquant = v->altpq; \
1051 if (!mquant || mquant > 31) { \
1052 av_log(v->s.avctx, AV_LOG_ERROR, \
1053 "Overriding invalid mquant %d\n", mquant); \
1059 * @def GET_MVDATA(_dmv_x, _dmv_y)
1060 * @brief Get MV differentials
1061 * @see MVDATA decoding from 8.3.5.2, p(1)20
1062 * @param _dmv_x Horizontal differential for decoded MV
1063 * @param _dmv_y Vertical differential for decoded MV
1065 #define GET_MVDATA(_dmv_x, _dmv_y) \
1066 index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
1067 VC1_MV_DIFF_VLC_BITS, 2); \
1069 mb_has_coeffs = 1; \
1072 mb_has_coeffs = 0; \
1075 _dmv_x = _dmv_y = 0; \
1076 } else if (index == 35) { \
1077 _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
1078 _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
1079 } else if (index == 36) { \
1084 index1 = index % 6; \
1085 if (!s->quarter_sample && index1 == 5) val = 1; \
1087 if (size_table[index1] - val > 0) \
1088 val = get_bits(gb, size_table[index1] - val); \
1090 sign = 0 - (val&1); \
1091 _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
1093 index1 = index / 6; \
1094 if (!s->quarter_sample && index1 == 5) val = 1; \
1096 if (size_table[index1] - val > 0) \
1097 val = get_bits(gb, size_table[index1] - val); \
1099 sign = 0 - (val & 1); \
1100 _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
1103 static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
1104 int *dmv_y, int *pred_flag)
1107 int extend_x = 0, extend_y = 0;
1108 GetBitContext *gb = &v->s.gb;
1111 const int* offs_tab;
1114 bits = VC1_2REF_MVDATA_VLC_BITS;
1117 bits = VC1_1REF_MVDATA_VLC_BITS;
1120 switch (v->dmvrange) {
1128 extend_x = extend_y = 1;
1131 index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
1133 *dmv_x = get_bits(gb, v->k_x);
1134 *dmv_y = get_bits(gb, v->k_y);
1136 *pred_flag = *dmv_y & 1;
1137 *dmv_y = (*dmv_y + *pred_flag) >> 1;
1141 av_assert0(index < esc);
1143 offs_tab = offset_table2;
1145 offs_tab = offset_table1;
1146 index1 = (index + 1) % 9;
1148 val = get_bits(gb, index1 + extend_x);
1149 sign = 0 -(val & 1);
1150 *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
1154 offs_tab = offset_table2;
1156 offs_tab = offset_table1;
1157 index1 = (index + 1) / 9;
1158 if (index1 > v->numref) {
1159 val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
1160 sign = 0 - (val & 1);
1161 *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
1165 *pred_flag = index1 & 1;
1169 static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
1171 int scaledvalue, refdist;
1172 int scalesame1, scalesame2;
1173 int scalezone1_x, zone1offset_x;
1174 int table_index = dir ^ v->second_field;
1176 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1177 refdist = v->refdist;
1179 refdist = dir ? v->brfd : v->frfd;
1182 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1183 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1184 scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
1185 zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
1190 if (FFABS(n) < scalezone1_x)
1191 scaledvalue = (n * scalesame1) >> 8;
1194 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
1196 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
1199 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1202 static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
1204 int scaledvalue, refdist;
1205 int scalesame1, scalesame2;
1206 int scalezone1_y, zone1offset_y;
1207 int table_index = dir ^ v->second_field;
1209 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1210 refdist = v->refdist;
1212 refdist = dir ? v->brfd : v->frfd;
1215 scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
1216 scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
1217 scalezone1_y = ff_vc1_field_mvpred_scales[table_index][4][refdist];
1218 zone1offset_y = ff_vc1_field_mvpred_scales[table_index][6][refdist];
1223 if (FFABS(n) < scalezone1_y)
1224 scaledvalue = (n * scalesame1) >> 8;
1227 scaledvalue = ((n * scalesame2) >> 8) - zone1offset_y;
1229 scaledvalue = ((n * scalesame2) >> 8) + zone1offset_y;
1233 if (v->cur_field_type && !v->ref_field_type[dir])
1234 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1236 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1239 static av_always_inline int scaleforopp_x(VC1Context *v, int n /* MV */)
1241 int scalezone1_x, zone1offset_x;
1242 int scaleopp1, scaleopp2, brfd;
1245 brfd = FFMIN(v->brfd, 3);
1246 scalezone1_x = ff_vc1_b_field_mvpred_scales[3][brfd];
1247 zone1offset_x = ff_vc1_b_field_mvpred_scales[5][brfd];
1248 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1249 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1254 if (FFABS(n) < scalezone1_x)
1255 scaledvalue = (n * scaleopp1) >> 8;
1258 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_x;
1260 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_x;
1263 return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
1266 static av_always_inline int scaleforopp_y(VC1Context *v, int n /* MV */, int dir)
1268 int scalezone1_y, zone1offset_y;
1269 int scaleopp1, scaleopp2, brfd;
1272 brfd = FFMIN(v->brfd, 3);
1273 scalezone1_y = ff_vc1_b_field_mvpred_scales[4][brfd];
1274 zone1offset_y = ff_vc1_b_field_mvpred_scales[6][brfd];
1275 scaleopp1 = ff_vc1_b_field_mvpred_scales[1][brfd];
1276 scaleopp2 = ff_vc1_b_field_mvpred_scales[2][brfd];
1281 if (FFABS(n) < scalezone1_y)
1282 scaledvalue = (n * scaleopp1) >> 8;
1285 scaledvalue = ((n * scaleopp2) >> 8) - zone1offset_y;
1287 scaledvalue = ((n * scaleopp2) >> 8) + zone1offset_y;
1290 if (v->cur_field_type && !v->ref_field_type[dir]) {
1291 return av_clip(scaledvalue, -v->range_y / 2 + 1, v->range_y / 2);
1293 return av_clip(scaledvalue, -v->range_y / 2, v->range_y / 2 - 1);
1297 static av_always_inline int scaleforsame(VC1Context *v, int i, int n /* MV */,
1300 int brfd, scalesame;
1301 int hpel = 1 - v->s.quarter_sample;
1304 if (v->s.pict_type != AV_PICTURE_TYPE_B || v->second_field || !dir) {
1306 n = scaleforsame_y(v, i, n, dir) << hpel;
1308 n = scaleforsame_x(v, n, dir) << hpel;
1311 brfd = FFMIN(v->brfd, 3);
1312 scalesame = ff_vc1_b_field_mvpred_scales[0][brfd];
1314 n = (n * scalesame >> 8) << hpel;
1318 static av_always_inline int scaleforopp(VC1Context *v, int n /* MV */,
1321 int refdist, scaleopp;
1322 int hpel = 1 - v->s.quarter_sample;
1325 if (v->s.pict_type == AV_PICTURE_TYPE_B && !v->second_field && dir == 1) {
1327 n = scaleforopp_y(v, n, dir) << hpel;
1329 n = scaleforopp_x(v, n) << hpel;
1332 if (v->s.pict_type != AV_PICTURE_TYPE_B)
1333 refdist = FFMIN(v->refdist, 3);
1335 refdist = dir ? v->brfd : v->frfd;
1336 scaleopp = ff_vc1_field_mvpred_scales[dir ^ v->second_field][0][refdist];
1338 n = (n * scaleopp >> 8) << hpel;
1342 /** Predict and set motion vector
1344 static inline void vc1_pred_mv(VC1Context *v, int n, int dmv_x, int dmv_y,
1345 int mv1, int r_x, int r_y, uint8_t* is_intra,
1346 int pred_flag, int dir)
1348 MpegEncContext *s = &v->s;
1349 int xy, wrap, off = 0;
1353 int mixedmv_pic, num_samefield = 0, num_oppfield = 0;
1354 int opposit, a_f, b_f, c_f;
1355 int16_t field_predA[2];
1356 int16_t field_predB[2];
1357 int16_t field_predC[2];
1358 int a_valid, b_valid, c_valid;
1359 int hybridmv_thresh, y_bias = 0;
1361 if (v->mv_mode == MV_PMODE_MIXED_MV ||
1362 ((v->mv_mode == MV_PMODE_INTENSITY_COMP) && (v->mv_mode2 == MV_PMODE_MIXED_MV)))
1366 /* scale MV difference to be quad-pel */
1367 dmv_x <<= 1 - s->quarter_sample;
1368 dmv_y <<= 1 - s->quarter_sample;
1370 wrap = s->b8_stride;
1371 xy = s->block_index[n];
1374 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = 0;
1375 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = 0;
1376 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = 0;
1377 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
1378 if (mv1) { /* duplicate motion data for 1-MV block */
1379 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][0] = 0;
1380 s->current_picture.f.motion_val[0][xy + 1 + v->blocks_off][1] = 0;
1381 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][0] = 0;
1382 s->current_picture.f.motion_val[0][xy + wrap + v->blocks_off][1] = 0;
1383 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][0] = 0;
1384 s->current_picture.f.motion_val[0][xy + wrap + 1 + v->blocks_off][1] = 0;
1385 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1386 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][0] = 0;
1387 s->current_picture.f.motion_val[1][xy + 1 + v->blocks_off][1] = 0;
1388 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1389 s->current_picture.f.motion_val[1][xy + wrap + v->blocks_off][1] = 0;
1390 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][0] = 0;
1391 s->current_picture.f.motion_val[1][xy + wrap + 1 + v->blocks_off][1] = 0;
1396 C = s->current_picture.f.motion_val[dir][xy - 1 + v->blocks_off];
1397 A = s->current_picture.f.motion_val[dir][xy - wrap + v->blocks_off];
1399 if (v->field_mode && mixedmv_pic)
1400 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
1402 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 2;
1404 //in 4-MV mode different blocks have different B predictor position
1407 off = (s->mb_x > 0) ? -1 : 1;
1410 off = (s->mb_x == (s->mb_width - 1)) ? -1 : 1;
1419 B = s->current_picture.f.motion_val[dir][xy - wrap + off + v->blocks_off];
1421 a_valid = !s->first_slice_line || (n == 2 || n == 3);
1422 b_valid = a_valid && (s->mb_width > 1);
1423 c_valid = s->mb_x || (n == 1 || n == 3);
1424 if (v->field_mode) {
1425 a_valid = a_valid && !is_intra[xy - wrap];
1426 b_valid = b_valid && !is_intra[xy - wrap + off];
1427 c_valid = c_valid && !is_intra[xy - 1];
1431 a_f = v->mv_f[dir][xy - wrap + v->blocks_off];
1432 num_oppfield += a_f;
1433 num_samefield += 1 - a_f;
1434 field_predA[0] = A[0];
1435 field_predA[1] = A[1];
1437 field_predA[0] = field_predA[1] = 0;
1441 b_f = v->mv_f[dir][xy - wrap + off + v->blocks_off];
1442 num_oppfield += b_f;
1443 num_samefield += 1 - b_f;
1444 field_predB[0] = B[0];
1445 field_predB[1] = B[1];
1447 field_predB[0] = field_predB[1] = 0;
1451 c_f = v->mv_f[dir][xy - 1 + v->blocks_off];
1452 num_oppfield += c_f;
1453 num_samefield += 1 - c_f;
1454 field_predC[0] = C[0];
1455 field_predC[1] = C[1];
1457 field_predC[0] = field_predC[1] = 0;
1461 if (v->field_mode) {
1462 if (num_samefield <= num_oppfield)
1463 opposit = 1 - pred_flag;
1465 opposit = pred_flag;
1469 if (a_valid && !a_f) {
1470 field_predA[0] = scaleforopp(v, field_predA[0], 0, dir);
1471 field_predA[1] = scaleforopp(v, field_predA[1], 1, dir);
1473 if (b_valid && !b_f) {
1474 field_predB[0] = scaleforopp(v, field_predB[0], 0, dir);
1475 field_predB[1] = scaleforopp(v, field_predB[1], 1, dir);
1477 if (c_valid && !c_f) {
1478 field_predC[0] = scaleforopp(v, field_predC[0], 0, dir);
1479 field_predC[1] = scaleforopp(v, field_predC[1], 1, dir);
1481 v->mv_f[dir][xy + v->blocks_off] = 1;
1482 v->ref_field_type[dir] = !v->cur_field_type;
1484 if (a_valid && a_f) {
1485 field_predA[0] = scaleforsame(v, n, field_predA[0], 0, dir);
1486 field_predA[1] = scaleforsame(v, n, field_predA[1], 1, dir);
1488 if (b_valid && b_f) {
1489 field_predB[0] = scaleforsame(v, n, field_predB[0], 0, dir);
1490 field_predB[1] = scaleforsame(v, n, field_predB[1], 1, dir);
1492 if (c_valid && c_f) {
1493 field_predC[0] = scaleforsame(v, n, field_predC[0], 0, dir);
1494 field_predC[1] = scaleforsame(v, n, field_predC[1], 1, dir);
1496 v->mv_f[dir][xy + v->blocks_off] = 0;
1497 v->ref_field_type[dir] = v->cur_field_type;
1501 px = field_predA[0];
1502 py = field_predA[1];
1503 } else if (c_valid) {
1504 px = field_predC[0];
1505 py = field_predC[1];
1506 } else if (b_valid) {
1507 px = field_predB[0];
1508 py = field_predB[1];
1514 if (num_samefield + num_oppfield > 1) {
1515 px = mid_pred(field_predA[0], field_predB[0], field_predC[0]);
1516 py = mid_pred(field_predA[1], field_predB[1], field_predC[1]);
1519 /* Pullback MV as specified in 8.3.5.3.4 */
1520 if (!v->field_mode) {
1522 qx = (s->mb_x << 6) + ((n == 1 || n == 3) ? 32 : 0);
1523 qy = (s->mb_y << 6) + ((n == 2 || n == 3) ? 32 : 0);
1524 X = (s->mb_width << 6) - 4;
1525 Y = (s->mb_height << 6) - 4;
1527 if (qx + px < -60) px = -60 - qx;
1528 if (qy + py < -60) py = -60 - qy;
1530 if (qx + px < -28) px = -28 - qx;
1531 if (qy + py < -28) py = -28 - qy;
1533 if (qx + px > X) px = X - qx;
1534 if (qy + py > Y) py = Y - qy;
1537 if (!v->field_mode || s->pict_type != AV_PICTURE_TYPE_B) {
1538 /* Calculate hybrid prediction as specified in 8.3.5.3.5 (also 10.3.5.4.3.5) */
1539 hybridmv_thresh = 32;
1540 if (a_valid && c_valid) {
1541 if (is_intra[xy - wrap])
1542 sum = FFABS(px) + FFABS(py);
1544 sum = FFABS(px - field_predA[0]) + FFABS(py - field_predA[1]);
1545 if (sum > hybridmv_thresh) {
1546 if (get_bits1(&s->gb)) { // read HYBRIDPRED bit
1547 px = field_predA[0];
1548 py = field_predA[1];
1550 px = field_predC[0];
1551 py = field_predC[1];
1554 if (is_intra[xy - 1])
1555 sum = FFABS(px) + FFABS(py);
1557 sum = FFABS(px - field_predC[0]) + FFABS(py - field_predC[1]);
1558 if (sum > hybridmv_thresh) {
1559 if (get_bits1(&s->gb)) {
1560 px = field_predA[0];
1561 py = field_predA[1];
1563 px = field_predC[0];
1564 py = field_predC[1];
1571 if (v->field_mode && !s->quarter_sample) {
1575 if (v->field_mode && v->numref)
1577 if (v->field_mode && v->cur_field_type && v->ref_field_type[dir] == 0)
1579 /* store MV using signed modulus of MV range defined in 4.11 */
1580 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;
1581 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;
1582 if (mv1) { /* duplicate motion data for 1-MV block */
1583 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];
1584 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];
1585 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];
1586 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];
1587 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];
1588 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];
1589 v->mv_f[dir][xy + 1 + v->blocks_off] = v->mv_f[dir][xy + v->blocks_off];
1590 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];
1594 /** Predict and set motion vector for interlaced frame picture MBs
1596 static inline void vc1_pred_mv_intfr(VC1Context *v, int n, int dmv_x, int dmv_y,
1597 int mvn, int r_x, int r_y, uint8_t* is_intra)
1599 MpegEncContext *s = &v->s;
1600 int xy, wrap, off = 0;
1601 int A[2], B[2], C[2];
1603 int a_valid = 0, b_valid = 0, c_valid = 0;
1604 int field_a, field_b, field_c; // 0: same, 1: opposit
1605 int total_valid, num_samefield, num_oppfield;
1606 int pos_c, pos_b, n_adj;
1608 wrap = s->b8_stride;
1609 xy = s->block_index[n];
1612 s->mv[0][n][0] = s->current_picture.f.motion_val[0][xy][0] = 0;
1613 s->mv[0][n][1] = s->current_picture.f.motion_val[0][xy][1] = 0;
1614 s->current_picture.f.motion_val[1][xy][0] = 0;
1615 s->current_picture.f.motion_val[1][xy][1] = 0;
1616 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1617 s->current_picture.f.motion_val[0][xy + 1][0] = 0;
1618 s->current_picture.f.motion_val[0][xy + 1][1] = 0;
1619 s->current_picture.f.motion_val[0][xy + wrap][0] = 0;
1620 s->current_picture.f.motion_val[0][xy + wrap][1] = 0;
1621 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = 0;
1622 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = 0;
1623 v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
1624 s->current_picture.f.motion_val[1][xy + 1][0] = 0;
1625 s->current_picture.f.motion_val[1][xy + 1][1] = 0;
1626 s->current_picture.f.motion_val[1][xy + wrap][0] = 0;
1627 s->current_picture.f.motion_val[1][xy + wrap][1] = 0;
1628 s->current_picture.f.motion_val[1][xy + wrap + 1][0] = 0;
1629 s->current_picture.f.motion_val[1][xy + wrap + 1][1] = 0;
1634 off = ((n == 0) || (n == 1)) ? 1 : -1;
1636 if (s->mb_x || (n == 1) || (n == 3)) {
1637 if ((v->blk_mv_type[xy]) // current block (MB) has a field MV
1638 || (!v->blk_mv_type[xy] && !v->blk_mv_type[xy - 1])) { // or both have frame MV
1639 A[0] = s->current_picture.f.motion_val[0][xy - 1][0];
1640 A[1] = s->current_picture.f.motion_val[0][xy - 1][1];
1642 } else { // current block has frame mv and cand. has field MV (so average)
1643 A[0] = (s->current_picture.f.motion_val[0][xy - 1][0]
1644 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][0] + 1) >> 1;
1645 A[1] = (s->current_picture.f.motion_val[0][xy - 1][1]
1646 + s->current_picture.f.motion_val[0][xy - 1 + off * wrap][1] + 1) >> 1;
1649 if (!(n & 1) && v->is_intra[s->mb_x - 1]) {
1655 /* Predict B and C */
1656 B[0] = B[1] = C[0] = C[1] = 0;
1657 if (n == 0 || n == 1 || v->blk_mv_type[xy]) {
1658 if (!s->first_slice_line) {
1659 if (!v->is_intra[s->mb_x - s->mb_stride]) {
1662 pos_b = s->block_index[n_adj] - 2 * wrap;
1663 if (v->blk_mv_type[pos_b] && v->blk_mv_type[xy]) {
1664 n_adj = (n & 2) | (n & 1);
1666 B[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][0];
1667 B[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap][1];
1668 if (v->blk_mv_type[pos_b] && !v->blk_mv_type[xy]) {
1669 B[0] = (B[0] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][0] + 1) >> 1;
1670 B[1] = (B[1] + s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap][1] + 1) >> 1;
1673 if (s->mb_width > 1) {
1674 if (!v->is_intra[s->mb_x - s->mb_stride + 1]) {
1677 pos_c = s->block_index[2] - 2 * wrap + 2;
1678 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1681 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][0];
1682 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap + 2][1];
1683 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1684 C[0] = (1 + C[0] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][0])) >> 1;
1685 C[1] = (1 + C[1] + (s->current_picture.f.motion_val[0][s->block_index[n_adj ^ 2] - 2 * wrap + 2][1])) >> 1;
1687 if (s->mb_x == s->mb_width - 1) {
1688 if (!v->is_intra[s->mb_x - s->mb_stride - 1]) {
1691 pos_c = s->block_index[3] - 2 * wrap - 2;
1692 if (v->blk_mv_type[pos_c] && v->blk_mv_type[xy]) {
1695 C[0] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][0];
1696 C[1] = s->current_picture.f.motion_val[0][s->block_index[n_adj] - 2 * wrap - 2][1];
1697 if (v->blk_mv_type[pos_c] && !v->blk_mv_type[xy]) {
1698 C[0] = (1 + C[0] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][0]) >> 1;
1699 C[1] = (1 + C[1] + s->current_picture.f.motion_val[0][s->block_index[1] - 2 * wrap - 2][1]) >> 1;
1708 pos_b = s->block_index[1];
1710 B[0] = s->current_picture.f.motion_val[0][pos_b][0];
1711 B[1] = s->current_picture.f.motion_val[0][pos_b][1];
1712 pos_c = s->block_index[0];
1714 C[0] = s->current_picture.f.motion_val[0][pos_c][0];
1715 C[1] = s->current_picture.f.motion_val[0][pos_c][1];
1718 total_valid = a_valid + b_valid + c_valid;
1719 // check if predictor A is out of bounds
1720 if (!s->mb_x && !(n == 1 || n == 3)) {
1723 // check if predictor B is out of bounds
1724 if ((s->first_slice_line && v->blk_mv_type[xy]) || (s->first_slice_line && !(n & 2))) {
1725 B[0] = B[1] = C[0] = C[1] = 0;
1727 if (!v->blk_mv_type[xy]) {
1728 if (s->mb_width == 1) {
1732 if (total_valid >= 2) {
1733 px = mid_pred(A[0], B[0], C[0]);
1734 py = mid_pred(A[1], B[1], C[1]);
1735 } else if (total_valid) {
1736 if (a_valid) { px = A[0]; py = A[1]; }
1737 if (b_valid) { px = B[0]; py = B[1]; }
1738 if (c_valid) { px = C[0]; py = C[1]; }
1744 field_a = (A[1] & 4) ? 1 : 0;
1748 field_b = (B[1] & 4) ? 1 : 0;
1752 field_c = (C[1] & 4) ? 1 : 0;
1756 num_oppfield = field_a + field_b + field_c;
1757 num_samefield = total_valid - num_oppfield;
1758 if (total_valid == 3) {
1759 if ((num_samefield == 3) || (num_oppfield == 3)) {
1760 px = mid_pred(A[0], B[0], C[0]);
1761 py = mid_pred(A[1], B[1], C[1]);
1762 } else if (num_samefield >= num_oppfield) {
1763 /* take one MV from same field set depending on priority
1764 the check for B may not be necessary */
1765 px = !field_a ? A[0] : B[0];
1766 py = !field_a ? A[1] : B[1];
1768 px = field_a ? A[0] : B[0];
1769 py = field_a ? A[1] : B[1];
1771 } else if (total_valid == 2) {
1772 if (num_samefield >= num_oppfield) {
1773 if (!field_a && a_valid) {
1776 } else if (!field_b && b_valid) {
1779 } else if (c_valid) {
1784 if (field_a && a_valid) {
1787 } else if (field_b && b_valid) {
1790 } else if (c_valid) {
1795 } else if (total_valid == 1) {
1796 px = (a_valid) ? A[0] : ((b_valid) ? B[0] : C[0]);
1797 py = (a_valid) ? A[1] : ((b_valid) ? B[1] : C[1]);
1802 /* store MV using signed modulus of MV range defined in 4.11 */
1803 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;
1804 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;
1805 if (mvn == 1) { /* duplicate motion data for 1-MV block */
1806 s->current_picture.f.motion_val[0][xy + 1 ][0] = s->current_picture.f.motion_val[0][xy][0];
1807 s->current_picture.f.motion_val[0][xy + 1 ][1] = s->current_picture.f.motion_val[0][xy][1];
1808 s->current_picture.f.motion_val[0][xy + wrap ][0] = s->current_picture.f.motion_val[0][xy][0];
1809 s->current_picture.f.motion_val[0][xy + wrap ][1] = s->current_picture.f.motion_val[0][xy][1];
1810 s->current_picture.f.motion_val[0][xy + wrap + 1][0] = s->current_picture.f.motion_val[0][xy][0];
1811 s->current_picture.f.motion_val[0][xy + wrap + 1][1] = s->current_picture.f.motion_val[0][xy][1];
1812 } else if (mvn == 2) { /* duplicate motion data for 2-Field 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->mv[0][n + 1][0] = s->mv[0][n][0];
1816 s->mv[0][n + 1][1] = s->mv[0][n][1];
1820 /** Motion compensation for direct or interpolated blocks in B-frames
1822 static void vc1_interp_mc(VC1Context *v)
1824 MpegEncContext *s = &v->s;
1825 DSPContext *dsp = &v->s.dsp;
1826 uint8_t *srcY, *srcU, *srcV;
1827 int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
1829 int v_edge_pos = s->v_edge_pos >> v->field_mode;
1831 if (!v->field_mode && !v->s.next_picture.f.data[0])
1834 mx = s->mv[1][0][0];
1835 my = s->mv[1][0][1];
1836 uvmx = (mx + ((mx & 3) == 3)) >> 1;
1837 uvmy = (my + ((my & 3) == 3)) >> 1;
1838 if (v->field_mode) {
1839 if (v->cur_field_type != v->ref_field_type[1])
1840 my = my - 2 + 4 * v->cur_field_type;
1841 uvmy = uvmy - 2 + 4 * v->cur_field_type;
1844 uvmx = uvmx + ((uvmx < 0) ? -(uvmx & 1) : (uvmx & 1));
1845 uvmy = uvmy + ((uvmy < 0) ? -(uvmy & 1) : (uvmy & 1));
1847 srcY = s->next_picture.f.data[0];
1848 srcU = s->next_picture.f.data[1];
1849 srcV = s->next_picture.f.data[2];
1851 src_x = s->mb_x * 16 + (mx >> 2);
1852 src_y = s->mb_y * 16 + (my >> 2);
1853 uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
1854 uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
1856 if (v->profile != PROFILE_ADVANCED) {
1857 src_x = av_clip( src_x, -16, s->mb_width * 16);
1858 src_y = av_clip( src_y, -16, s->mb_height * 16);
1859 uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
1860 uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
1862 src_x = av_clip( src_x, -17, s->avctx->coded_width);
1863 src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
1864 uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
1865 uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
1868 srcY += src_y * s->linesize + src_x;
1869 srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
1870 srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
1872 if (v->field_mode && v->ref_field_type[1]) {
1873 srcY += s->current_picture_ptr->f.linesize[0];
1874 srcU += s->current_picture_ptr->f.linesize[1];
1875 srcV += s->current_picture_ptr->f.linesize[2];
1878 /* for grayscale we should not try to read from unknown area */
1879 if (s->flags & CODEC_FLAG_GRAY) {
1880 srcU = s->edge_emu_buffer + 18 * s->linesize;
1881 srcV = s->edge_emu_buffer + 18 * s->linesize;
1884 if (v->rangeredfrm || s->h_edge_pos < 22 || v_edge_pos < 22
1885 || (unsigned)(src_x - 1) > s->h_edge_pos - (mx & 3) - 16 - 3
1886 || (unsigned)(src_y - 1) > v_edge_pos - (my & 3) - 16 - 3) {
1887 uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
1889 srcY -= s->mspel * (1 + s->linesize);
1890 s->dsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
1891 17 + s->mspel * 2, 17 + s->mspel * 2,
1892 src_x - s->mspel, src_y - s->mspel,
1893 s->h_edge_pos, v_edge_pos);
1894 srcY = s->edge_emu_buffer;
1895 s->dsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
1896 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1897 s->dsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
1898 uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
1901 /* if we deal with range reduction we need to scale source blocks */
1902 if (v->rangeredfrm) {
1904 uint8_t *src, *src2;
1907 for (j = 0; j < 17 + s->mspel * 2; j++) {
1908 for (i = 0; i < 17 + s->mspel * 2; i++)
1909 src[i] = ((src[i] - 128) >> 1) + 128;
1914 for (j = 0; j < 9; j++) {
1915 for (i = 0; i < 9; i++) {
1916 src[i] = ((src[i] - 128) >> 1) + 128;
1917 src2[i] = ((src2[i] - 128) >> 1) + 128;
1919 src += s->uvlinesize;
1920 src2 += s->uvlinesize;
1923 srcY += s->mspel * (1 + s->linesize);
1926 if (v->field_mode && v->second_field) {
1927 off = s->current_picture_ptr->f.linesize[0];
1928 off_uv = s->current_picture_ptr->f.linesize[1];
1935 dxy = ((my & 3) << 2) | (mx & 3);
1936 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
1937 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
1938 srcY += s->linesize * 8;
1939 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
1940 v->vc1dsp.avg_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
1942 dxy = (my & 2) | ((mx & 2) >> 1);
1945 dsp->avg_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1947 dsp->avg_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
1950 if (s->flags & CODEC_FLAG_GRAY) return;
1951 /* Chroma MC always uses qpel blilinear */
1952 uvmx = (uvmx & 3) << 1;
1953 uvmy = (uvmy & 3) << 1;
1955 dsp->avg_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1956 dsp->avg_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1958 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
1959 v->vc1dsp.avg_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
1963 static av_always_inline int scale_mv(int value, int bfrac, int inv, int qs)
1967 #if B_FRACTION_DEN==256
1971 return 2 * ((value * n + 255) >> 9);
1972 return (value * n + 128) >> 8;
1975 n -= B_FRACTION_DEN;
1977 return 2 * ((value * n + B_FRACTION_DEN - 1) / (2 * B_FRACTION_DEN));
1978 return (value * n + B_FRACTION_DEN/2) / B_FRACTION_DEN;
1982 /** Reconstruct motion vector for B-frame and do motion compensation
1984 static inline void vc1_b_mc(VC1Context *v, int dmv_x[2], int dmv_y[2],
1985 int direct, int mode)
1988 v->mv_mode2 = v->mv_mode;
1989 v->mv_mode = MV_PMODE_INTENSITY_COMP;
1995 v->mv_mode = v->mv_mode2;
1998 if (mode == BMV_TYPE_INTERPOLATED) {
2002 v->mv_mode = v->mv_mode2;
2006 if (v->use_ic && (mode == BMV_TYPE_BACKWARD))
2007 v->mv_mode = v->mv_mode2;
2008 vc1_mc_1mv(v, (mode == BMV_TYPE_BACKWARD));
2010 v->mv_mode = v->mv_mode2;
2013 static inline void vc1_pred_b_mv(VC1Context *v, int dmv_x[2], int dmv_y[2],
2014 int direct, int mvtype)
2016 MpegEncContext *s = &v->s;
2017 int xy, wrap, off = 0;
2022 const uint8_t *is_intra = v->mb_type[0];
2026 /* scale MV difference to be quad-pel */
2027 dmv_x[0] <<= 1 - s->quarter_sample;
2028 dmv_y[0] <<= 1 - s->quarter_sample;
2029 dmv_x[1] <<= 1 - s->quarter_sample;
2030 dmv_y[1] <<= 1 - s->quarter_sample;
2032 wrap = s->b8_stride;
2033 xy = s->block_index[0];
2036 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] =
2037 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] =
2038 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] =
2039 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = 0;
2042 if (!v->field_mode) {
2043 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 0, s->quarter_sample);
2044 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 0, s->quarter_sample);
2045 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][xy][0], v->bfraction, 1, s->quarter_sample);
2046 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][xy][1], v->bfraction, 1, s->quarter_sample);
2048 /* Pullback predicted motion vectors as specified in 8.4.5.4 */
2049 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));
2050 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));
2051 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));
2052 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));
2055 s->current_picture.f.motion_val[0][xy + v->blocks_off][0] = s->mv[0][0][0];
2056 s->current_picture.f.motion_val[0][xy + v->blocks_off][1] = s->mv[0][0][1];
2057 s->current_picture.f.motion_val[1][xy + v->blocks_off][0] = s->mv[1][0][0];
2058 s->current_picture.f.motion_val[1][xy + v->blocks_off][1] = s->mv[1][0][1];
2062 if ((mvtype == BMV_TYPE_FORWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2063 C = s->current_picture.f.motion_val[0][xy - 2];
2064 A = s->current_picture.f.motion_val[0][xy - wrap * 2];
2065 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2066 B = s->current_picture.f.motion_val[0][xy - wrap * 2 + off];
2068 if (!s->mb_x) C[0] = C[1] = 0;
2069 if (!s->first_slice_line) { // predictor A is not out of bounds
2070 if (s->mb_width == 1) {
2074 px = mid_pred(A[0], B[0], C[0]);
2075 py = mid_pred(A[1], B[1], C[1]);
2077 } else if (s->mb_x) { // predictor C is not out of bounds
2083 /* Pullback MV as specified in 8.3.5.3.4 */
2086 if (v->profile < PROFILE_ADVANCED) {
2087 qx = (s->mb_x << 5);
2088 qy = (s->mb_y << 5);
2089 X = (s->mb_width << 5) - 4;
2090 Y = (s->mb_height << 5) - 4;
2091 if (qx + px < -28) px = -28 - qx;
2092 if (qy + py < -28) py = -28 - qy;
2093 if (qx + px > X) px = X - qx;
2094 if (qy + py > Y) py = Y - qy;
2096 qx = (s->mb_x << 6);
2097 qy = (s->mb_y << 6);
2098 X = (s->mb_width << 6) - 4;
2099 Y = (s->mb_height << 6) - 4;
2100 if (qx + px < -60) px = -60 - qx;
2101 if (qy + py < -60) py = -60 - qy;
2102 if (qx + px > X) px = X - qx;
2103 if (qy + py > Y) py = Y - qy;
2106 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2107 if (0 && !s->first_slice_line && s->mb_x) {
2108 if (is_intra[xy - wrap])
2109 sum = FFABS(px) + FFABS(py);
2111 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2113 if (get_bits1(&s->gb)) {
2121 if (is_intra[xy - 2])
2122 sum = FFABS(px) + FFABS(py);
2124 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2126 if (get_bits1(&s->gb)) {
2136 /* store MV using signed modulus of MV range defined in 4.11 */
2137 s->mv[0][0][0] = ((px + dmv_x[0] + r_x) & ((r_x << 1) - 1)) - r_x;
2138 s->mv[0][0][1] = ((py + dmv_y[0] + r_y) & ((r_y << 1) - 1)) - r_y;
2140 if ((mvtype == BMV_TYPE_BACKWARD) || (mvtype == BMV_TYPE_INTERPOLATED)) {
2141 C = s->current_picture.f.motion_val[1][xy - 2];
2142 A = s->current_picture.f.motion_val[1][xy - wrap * 2];
2143 off = (s->mb_x == (s->mb_width - 1)) ? -2 : 2;
2144 B = s->current_picture.f.motion_val[1][xy - wrap * 2 + off];
2148 if (!s->first_slice_line) { // predictor A is not out of bounds
2149 if (s->mb_width == 1) {
2153 px = mid_pred(A[0], B[0], C[0]);
2154 py = mid_pred(A[1], B[1], C[1]);
2156 } else if (s->mb_x) { // predictor C is not out of bounds
2162 /* Pullback MV as specified in 8.3.5.3.4 */
2165 if (v->profile < PROFILE_ADVANCED) {
2166 qx = (s->mb_x << 5);
2167 qy = (s->mb_y << 5);
2168 X = (s->mb_width << 5) - 4;
2169 Y = (s->mb_height << 5) - 4;
2170 if (qx + px < -28) px = -28 - qx;
2171 if (qy + py < -28) py = -28 - qy;
2172 if (qx + px > X) px = X - qx;
2173 if (qy + py > Y) py = Y - qy;
2175 qx = (s->mb_x << 6);
2176 qy = (s->mb_y << 6);
2177 X = (s->mb_width << 6) - 4;
2178 Y = (s->mb_height << 6) - 4;
2179 if (qx + px < -60) px = -60 - qx;
2180 if (qy + py < -60) py = -60 - qy;
2181 if (qx + px > X) px = X - qx;
2182 if (qy + py > Y) py = Y - qy;
2185 /* Calculate hybrid prediction as specified in 8.3.5.3.5 */
2186 if (0 && !s->first_slice_line && s->mb_x) {
2187 if (is_intra[xy - wrap])
2188 sum = FFABS(px) + FFABS(py);
2190 sum = FFABS(px - A[0]) + FFABS(py - A[1]);
2192 if (get_bits1(&s->gb)) {
2200 if (is_intra[xy - 2])
2201 sum = FFABS(px) + FFABS(py);
2203 sum = FFABS(px - C[0]) + FFABS(py - C[1]);
2205 if (get_bits1(&s->gb)) {
2215 /* store MV using signed modulus of MV range defined in 4.11 */
2217 s->mv[1][0][0] = ((px + dmv_x[1] + r_x) & ((r_x << 1) - 1)) - r_x;
2218 s->mv[1][0][1] = ((py + dmv_y[1] + r_y) & ((r_y << 1) - 1)) - r_y;
2220 s->current_picture.f.motion_val[0][xy][0] = s->mv[0][0][0];
2221 s->current_picture.f.motion_val[0][xy][1] = s->mv[0][0][1];
2222 s->current_picture.f.motion_val[1][xy][0] = s->mv[1][0][0];
2223 s->current_picture.f.motion_val[1][xy][1] = s->mv[1][0][1];
2226 static inline void vc1_pred_b_mv_intfi(VC1Context *v, int n, int *dmv_x, int *dmv_y, int mv1, int *pred_flag)
2228 int dir = (v->bmvtype == BMV_TYPE_BACKWARD) ? 1 : 0;
2229 MpegEncContext *s = &v->s;
2230 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2232 if (v->bmvtype == BMV_TYPE_DIRECT) {
2233 int total_opp, k, f;
2234 if (s->next_picture.f.mb_type[mb_pos + v->mb_off] != MB_TYPE_INTRA) {
2235 s->mv[0][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2236 v->bfraction, 0, s->quarter_sample);
2237 s->mv[0][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2238 v->bfraction, 0, s->quarter_sample);
2239 s->mv[1][0][0] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0],
2240 v->bfraction, 1, s->quarter_sample);
2241 s->mv[1][0][1] = scale_mv(s->next_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1],
2242 v->bfraction, 1, s->quarter_sample);
2244 total_opp = v->mv_f_next[0][s->block_index[0] + v->blocks_off]
2245 + v->mv_f_next[0][s->block_index[1] + v->blocks_off]
2246 + v->mv_f_next[0][s->block_index[2] + v->blocks_off]
2247 + v->mv_f_next[0][s->block_index[3] + v->blocks_off];
2248 f = (total_opp > 2) ? 1 : 0;
2250 s->mv[0][0][0] = s->mv[0][0][1] = 0;
2251 s->mv[1][0][0] = s->mv[1][0][1] = 0;
2254 v->ref_field_type[0] = v->ref_field_type[1] = v->cur_field_type ^ f;
2255 for (k = 0; k < 4; k++) {
2256 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][0] = s->mv[0][0][0];
2257 s->current_picture.f.motion_val[0][s->block_index[k] + v->blocks_off][1] = s->mv[0][0][1];
2258 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][0] = s->mv[1][0][0];
2259 s->current_picture.f.motion_val[1][s->block_index[k] + v->blocks_off][1] = s->mv[1][0][1];
2260 v->mv_f[0][s->block_index[k] + v->blocks_off] = f;
2261 v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
2265 if (v->bmvtype == BMV_TYPE_INTERPOLATED) {
2266 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);
2267 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);
2270 if (dir) { // backward
2271 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);
2272 if (n == 3 || mv1) {
2273 vc1_pred_mv(v, 0, dmv_x[0], dmv_y[0], 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
2276 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);
2277 if (n == 3 || mv1) {
2278 vc1_pred_mv(v, 0, dmv_x[1], dmv_y[1], 1, v->range_x, v->range_y, v->mb_type[0], 0, 1);
2283 /** Get predicted DC value for I-frames only
2284 * prediction dir: left=0, top=1
2285 * @param s MpegEncContext
2286 * @param overlap flag indicating that overlap filtering is used
2287 * @param pq integer part of picture quantizer
2288 * @param[in] n block index in the current MB
2289 * @param dc_val_ptr Pointer to DC predictor
2290 * @param dir_ptr Prediction direction for use in AC prediction
2292 static inline int vc1_i_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2293 int16_t **dc_val_ptr, int *dir_ptr)
2295 int a, b, c, wrap, pred, scale;
2297 static const uint16_t dcpred[32] = {
2298 -1, 1024, 512, 341, 256, 205, 171, 146, 128,
2299 114, 102, 93, 85, 79, 73, 68, 64,
2300 60, 57, 54, 51, 49, 47, 45, 43,
2301 41, 39, 38, 37, 35, 34, 33
2304 /* find prediction - wmv3_dc_scale always used here in fact */
2305 if (n < 4) scale = s->y_dc_scale;
2306 else scale = s->c_dc_scale;
2308 wrap = s->block_wrap[n];
2309 dc_val = s->dc_val[0] + s->block_index[n];
2315 b = dc_val[ - 1 - wrap];
2316 a = dc_val[ - wrap];
2318 if (pq < 9 || !overlap) {
2319 /* Set outer values */
2320 if (s->first_slice_line && (n != 2 && n != 3))
2321 b = a = dcpred[scale];
2322 if (s->mb_x == 0 && (n != 1 && n != 3))
2323 b = c = dcpred[scale];
2325 /* Set outer values */
2326 if (s->first_slice_line && (n != 2 && n != 3))
2328 if (s->mb_x == 0 && (n != 1 && n != 3))
2332 if (abs(a - b) <= abs(b - c)) {
2334 *dir_ptr = 1; // left
2337 *dir_ptr = 0; // top
2340 /* update predictor */
2341 *dc_val_ptr = &dc_val[0];
2346 /** Get predicted DC value
2347 * prediction dir: left=0, top=1
2348 * @param s MpegEncContext
2349 * @param overlap flag indicating that overlap filtering is used
2350 * @param pq integer part of picture quantizer
2351 * @param[in] n block index in the current MB
2352 * @param a_avail flag indicating top block availability
2353 * @param c_avail flag indicating left block availability
2354 * @param dc_val_ptr Pointer to DC predictor
2355 * @param dir_ptr Prediction direction for use in AC prediction
2357 static inline int vc1_pred_dc(MpegEncContext *s, int overlap, int pq, int n,
2358 int a_avail, int c_avail,
2359 int16_t **dc_val_ptr, int *dir_ptr)
2361 int a, b, c, wrap, pred;
2363 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2367 wrap = s->block_wrap[n];
2368 dc_val = s->dc_val[0] + s->block_index[n];
2374 b = dc_val[ - 1 - wrap];
2375 a = dc_val[ - wrap];
2376 /* scale predictors if needed */
2377 q1 = s->current_picture.f.qscale_table[mb_pos];
2378 dqscale_index = s->y_dc_scale_table[q1] - 1;
2379 if (dqscale_index < 0)
2381 if (c_avail && (n != 1 && n != 3)) {
2382 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2384 c = (c * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2386 if (a_avail && (n != 2 && n != 3)) {
2387 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2389 a = (a * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2391 if (a_avail && c_avail && (n != 3)) {
2396 off -= s->mb_stride;
2397 q2 = s->current_picture.f.qscale_table[off];
2399 b = (b * s->y_dc_scale_table[q2] * ff_vc1_dqscale[dqscale_index] + 0x20000) >> 18;
2402 if (a_avail && c_avail) {
2403 if (abs(a - b) <= abs(b - c)) {
2405 *dir_ptr = 1; // left
2408 *dir_ptr = 0; // top
2410 } else if (a_avail) {
2412 *dir_ptr = 0; // top
2413 } else if (c_avail) {
2415 *dir_ptr = 1; // left
2418 *dir_ptr = 1; // left
2421 /* update predictor */
2422 *dc_val_ptr = &dc_val[0];
2426 /** @} */ // Block group
2429 * @name VC1 Macroblock-level functions in Simple/Main Profiles
2430 * @see 7.1.4, p91 and 8.1.1.7, p(1)04
2434 static inline int vc1_coded_block_pred(MpegEncContext * s, int n,
2435 uint8_t **coded_block_ptr)
2437 int xy, wrap, pred, a, b, c;
2439 xy = s->block_index[n];
2440 wrap = s->b8_stride;
2445 a = s->coded_block[xy - 1 ];
2446 b = s->coded_block[xy - 1 - wrap];
2447 c = s->coded_block[xy - wrap];
2456 *coded_block_ptr = &s->coded_block[xy];
2462 * Decode one AC coefficient
2463 * @param v The VC1 context
2464 * @param last Last coefficient
2465 * @param skip How much zero coefficients to skip
2466 * @param value Decoded AC coefficient value
2467 * @param codingset set of VLC to decode data
2470 static void vc1_decode_ac_coeff(VC1Context *v, int *last, int *skip,
2471 int *value, int codingset)
2473 GetBitContext *gb = &v->s.gb;
2474 int index, escape, run = 0, level = 0, lst = 0;
2476 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2477 if (index != ff_vc1_ac_sizes[codingset] - 1) {
2478 run = vc1_index_decode_table[codingset][index][0];
2479 level = vc1_index_decode_table[codingset][index][1];
2480 lst = index >= vc1_last_decode_table[codingset] || get_bits_left(gb) < 0;
2484 escape = decode210(gb);
2486 index = get_vlc2(gb, ff_vc1_ac_coeff_table[codingset].table, AC_VLC_BITS, 3);
2487 run = vc1_index_decode_table[codingset][index][0];
2488 level = vc1_index_decode_table[codingset][index][1];
2489 lst = index >= vc1_last_decode_table[codingset];
2492 level += vc1_last_delta_level_table[codingset][run];
2494 level += vc1_delta_level_table[codingset][run];
2497 run += vc1_last_delta_run_table[codingset][level] + 1;
2499 run += vc1_delta_run_table[codingset][level] + 1;
2505 lst = get_bits1(gb);
2506 if (v->s.esc3_level_length == 0) {
2507 if (v->pq < 8 || v->dquantfrm) { // table 59
2508 v->s.esc3_level_length = get_bits(gb, 3);
2509 if (!v->s.esc3_level_length)
2510 v->s.esc3_level_length = get_bits(gb, 2) + 8;
2511 } else { // table 60
2512 v->s.esc3_level_length = get_unary(gb, 1, 6) + 2;
2514 v->s.esc3_run_length = 3 + get_bits(gb, 2);
2516 run = get_bits(gb, v->s.esc3_run_length);
2517 sign = get_bits1(gb);
2518 level = get_bits(gb, v->s.esc3_level_length);
2529 /** Decode intra block in intra frames - should be faster than decode_intra_block
2530 * @param v VC1Context
2531 * @param block block to decode
2532 * @param[in] n subblock index
2533 * @param coded are AC coeffs present or not
2534 * @param codingset set of VLC to decode data
2536 static int vc1_decode_i_block(VC1Context *v, DCTELEM block[64], int n,
2537 int coded, int codingset)
2539 GetBitContext *gb = &v->s.gb;
2540 MpegEncContext *s = &v->s;
2541 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2544 int16_t *ac_val, *ac_val2;
2547 /* Get DC differential */
2549 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2551 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2554 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2558 if (dcdiff == 119 /* ESC index value */) {
2559 /* TODO: Optimize */
2560 if (v->pq == 1) dcdiff = get_bits(gb, 10);
2561 else if (v->pq == 2) dcdiff = get_bits(gb, 9);
2562 else dcdiff = get_bits(gb, 8);
2565 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2566 else if (v->pq == 2)
2567 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2574 dcdiff += vc1_i_pred_dc(&v->s, v->overlap, v->pq, n, &dc_val, &dc_pred_dir);
2577 /* Store the quantized DC coeff, used for prediction */
2579 block[0] = dcdiff * s->y_dc_scale;
2581 block[0] = dcdiff * s->c_dc_scale;
2592 int last = 0, skip, value;
2593 const uint8_t *zz_table;
2597 scale = v->pq * 2 + v->halfpq;
2601 zz_table = v->zz_8x8[2];
2603 zz_table = v->zz_8x8[3];
2605 zz_table = v->zz_8x8[1];
2607 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2609 if (dc_pred_dir) // left
2612 ac_val -= 16 * s->block_wrap[n];
2615 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2619 block[zz_table[i++]] = value;
2622 /* apply AC prediction if needed */
2624 if (dc_pred_dir) { // left
2625 for (k = 1; k < 8; k++)
2626 block[k << v->left_blk_sh] += ac_val[k];
2628 for (k = 1; k < 8; k++)
2629 block[k << v->top_blk_sh] += ac_val[k + 8];
2632 /* save AC coeffs for further prediction */
2633 for (k = 1; k < 8; k++) {
2634 ac_val2[k] = block[k << v->left_blk_sh];
2635 ac_val2[k + 8] = block[k << v->top_blk_sh];
2638 /* scale AC coeffs */
2639 for (k = 1; k < 64; k++)
2643 block[k] += (block[k] < 0) ? -v->pq : v->pq;
2646 if (s->ac_pred) i = 63;
2652 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2656 scale = v->pq * 2 + v->halfpq;
2657 memset(ac_val2, 0, 16 * 2);
2658 if (dc_pred_dir) { // left
2661 memcpy(ac_val2, ac_val, 8 * 2);
2663 ac_val -= 16 * s->block_wrap[n];
2665 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2668 /* apply AC prediction if needed */
2670 if (dc_pred_dir) { //left
2671 for (k = 1; k < 8; k++) {
2672 block[k << v->left_blk_sh] = ac_val[k] * scale;
2673 if (!v->pquantizer && block[k << v->left_blk_sh])
2674 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -v->pq : v->pq;
2677 for (k = 1; k < 8; k++) {
2678 block[k << v->top_blk_sh] = ac_val[k + 8] * scale;
2679 if (!v->pquantizer && block[k << v->top_blk_sh])
2680 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -v->pq : v->pq;
2686 s->block_last_index[n] = i;
2691 /** Decode intra block in intra frames - should be faster than decode_intra_block
2692 * @param v VC1Context
2693 * @param block block to decode
2694 * @param[in] n subblock number
2695 * @param coded are AC coeffs present or not
2696 * @param codingset set of VLC to decode data
2697 * @param mquant quantizer value for this macroblock
2699 static int vc1_decode_i_block_adv(VC1Context *v, DCTELEM block[64], int n,
2700 int coded, int codingset, int mquant)
2702 GetBitContext *gb = &v->s.gb;
2703 MpegEncContext *s = &v->s;
2704 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2707 int16_t *ac_val, *ac_val2;
2709 int a_avail = v->a_avail, c_avail = v->c_avail;
2710 int use_pred = s->ac_pred;
2713 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2715 /* Get DC differential */
2717 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2719 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2722 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2726 if (dcdiff == 119 /* ESC index value */) {
2727 /* TODO: Optimize */
2728 if (mquant == 1) dcdiff = get_bits(gb, 10);
2729 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2730 else dcdiff = get_bits(gb, 8);
2733 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2734 else if (mquant == 2)
2735 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2742 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, v->a_avail, v->c_avail, &dc_val, &dc_pred_dir);
2745 /* Store the quantized DC coeff, used for prediction */
2747 block[0] = dcdiff * s->y_dc_scale;
2749 block[0] = dcdiff * s->c_dc_scale;
2755 /* check if AC is needed at all */
2756 if (!a_avail && !c_avail)
2758 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2761 scale = mquant * 2 + ((mquant == v->pq) ? v->halfpq : 0);
2763 if (dc_pred_dir) // left
2766 ac_val -= 16 * s->block_wrap[n];
2768 q1 = s->current_picture.f.qscale_table[mb_pos];
2769 if ( dc_pred_dir && c_avail && mb_pos)
2770 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2771 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2772 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2773 if ( dc_pred_dir && n == 1)
2775 if (!dc_pred_dir && n == 2)
2781 int last = 0, skip, value;
2782 const uint8_t *zz_table;
2786 if (!use_pred && v->fcm == ILACE_FRAME) {
2787 zz_table = v->zzi_8x8;
2789 if (!dc_pred_dir) // top
2790 zz_table = v->zz_8x8[2];
2792 zz_table = v->zz_8x8[3];
2795 if (v->fcm != ILACE_FRAME)
2796 zz_table = v->zz_8x8[1];
2798 zz_table = v->zzi_8x8;
2802 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
2806 block[zz_table[i++]] = value;
2809 /* apply AC prediction if needed */
2811 /* scale predictors if needed*/
2812 if (q2 && q1 != q2) {
2813 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2814 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2817 return AVERROR_INVALIDDATA;
2818 if (dc_pred_dir) { // left
2819 for (k = 1; k < 8; k++)
2820 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2822 for (k = 1; k < 8; k++)
2823 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2826 if (dc_pred_dir) { //left
2827 for (k = 1; k < 8; k++)
2828 block[k << v->left_blk_sh] += ac_val[k];
2830 for (k = 1; k < 8; k++)
2831 block[k << v->top_blk_sh] += ac_val[k + 8];
2835 /* save AC coeffs for further prediction */
2836 for (k = 1; k < 8; k++) {
2837 ac_val2[k ] = block[k << v->left_blk_sh];
2838 ac_val2[k + 8] = block[k << v->top_blk_sh];
2841 /* scale AC coeffs */
2842 for (k = 1; k < 64; k++)
2846 block[k] += (block[k] < 0) ? -mquant : mquant;
2849 if (use_pred) i = 63;
2850 } else { // no AC coeffs
2853 memset(ac_val2, 0, 16 * 2);
2854 if (dc_pred_dir) { // left
2856 memcpy(ac_val2, ac_val, 8 * 2);
2857 if (q2 && q1 != q2) {
2858 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2859 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2861 return AVERROR_INVALIDDATA;
2862 for (k = 1; k < 8; k++)
2863 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2868 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
2869 if (q2 && q1 != q2) {
2870 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
2871 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
2873 return AVERROR_INVALIDDATA;
2874 for (k = 1; k < 8; k++)
2875 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
2880 /* apply AC prediction if needed */
2882 if (dc_pred_dir) { // left
2883 for (k = 1; k < 8; k++) {
2884 block[k << v->left_blk_sh] = ac_val2[k] * scale;
2885 if (!v->pquantizer && block[k << v->left_blk_sh])
2886 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
2889 for (k = 1; k < 8; k++) {
2890 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
2891 if (!v->pquantizer && block[k << v->top_blk_sh])
2892 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
2898 s->block_last_index[n] = i;
2903 /** Decode intra block in inter frames - more generic version than vc1_decode_i_block
2904 * @param v VC1Context
2905 * @param block block to decode
2906 * @param[in] n subblock index
2907 * @param coded are AC coeffs present or not
2908 * @param mquant block quantizer
2909 * @param codingset set of VLC to decode data
2911 static int vc1_decode_intra_block(VC1Context *v, DCTELEM block[64], int n,
2912 int coded, int mquant, int codingset)
2914 GetBitContext *gb = &v->s.gb;
2915 MpegEncContext *s = &v->s;
2916 int dc_pred_dir = 0; /* Direction of the DC prediction used */
2919 int16_t *ac_val, *ac_val2;
2921 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
2922 int a_avail = v->a_avail, c_avail = v->c_avail;
2923 int use_pred = s->ac_pred;
2927 s->dsp.clear_block(block);
2929 /* XXX: Guard against dumb values of mquant */
2930 mquant = (mquant < 1) ? 0 : ((mquant > 31) ? 31 : mquant);
2932 /* Set DC scale - y and c use the same */
2933 s->y_dc_scale = s->y_dc_scale_table[mquant];
2934 s->c_dc_scale = s->c_dc_scale_table[mquant];
2936 /* Get DC differential */
2938 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_luma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2940 dcdiff = get_vlc2(&s->gb, ff_msmp4_dc_chroma_vlc[s->dc_table_index].table, DC_VLC_BITS, 3);
2943 av_log(s->avctx, AV_LOG_ERROR, "Illegal DC VLC\n");
2947 if (dcdiff == 119 /* ESC index value */) {
2948 /* TODO: Optimize */
2949 if (mquant == 1) dcdiff = get_bits(gb, 10);
2950 else if (mquant == 2) dcdiff = get_bits(gb, 9);
2951 else dcdiff = get_bits(gb, 8);
2954 dcdiff = (dcdiff << 2) + get_bits(gb, 2) - 3;
2955 else if (mquant == 2)
2956 dcdiff = (dcdiff << 1) + get_bits1(gb) - 1;
2963 dcdiff += vc1_pred_dc(&v->s, v->overlap, mquant, n, a_avail, c_avail, &dc_val, &dc_pred_dir);
2966 /* Store the quantized DC coeff, used for prediction */
2969 block[0] = dcdiff * s->y_dc_scale;
2971 block[0] = dcdiff * s->c_dc_scale;
2977 /* check if AC is needed at all and adjust direction if needed */
2978 if (!a_avail) dc_pred_dir = 1;
2979 if (!c_avail) dc_pred_dir = 0;
2980 if (!a_avail && !c_avail) use_pred = 0;
2981 ac_val = s->ac_val[0][0] + s->block_index[n] * 16;
2984 scale = mquant * 2 + v->halfpq;
2986 if (dc_pred_dir) //left
2989 ac_val -= 16 * s->block_wrap[n];
2991 q1 = s->current_picture.f.qscale_table[mb_pos];
2992 if (dc_pred_dir && c_avail && mb_pos)
2993 q2 = s->current_picture.f.qscale_table[mb_pos - 1];
2994 if (!dc_pred_dir && a_avail && mb_pos >= s->mb_stride)
2995 q2 = s->current_picture.f.qscale_table[mb_pos - s->mb_stride];
2996 if ( dc_pred_dir && n == 1)
2998 if (!dc_pred_dir && n == 2)
3000 if (n == 3) q2 = q1;
3003 int last = 0, skip, value;
3007 vc1_decode_ac_coeff(v, &last, &skip, &value, codingset);
3011 if (v->fcm == PROGRESSIVE)
3012 block[v->zz_8x8[0][i++]] = value;
3014 if (use_pred && (v->fcm == ILACE_FRAME)) {
3015 if (!dc_pred_dir) // top
3016 block[v->zz_8x8[2][i++]] = value;
3018 block[v->zz_8x8[3][i++]] = value;
3020 block[v->zzi_8x8[i++]] = value;
3025 /* apply AC prediction if needed */
3027 /* scale predictors if needed*/
3028 if (q2 && q1 != q2) {
3029 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3030 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3033 return AVERROR_INVALIDDATA;
3034 if (dc_pred_dir) { // left
3035 for (k = 1; k < 8; k++)
3036 block[k << v->left_blk_sh] += (ac_val[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3038 for (k = 1; k < 8; k++)
3039 block[k << v->top_blk_sh] += (ac_val[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3042 if (dc_pred_dir) { // left
3043 for (k = 1; k < 8; k++)
3044 block[k << v->left_blk_sh] += ac_val[k];
3046 for (k = 1; k < 8; k++)
3047 block[k << v->top_blk_sh] += ac_val[k + 8];
3051 /* save AC coeffs for further prediction */
3052 for (k = 1; k < 8; k++) {
3053 ac_val2[k ] = block[k << v->left_blk_sh];
3054 ac_val2[k + 8] = block[k << v->top_blk_sh];
3057 /* scale AC coeffs */
3058 for (k = 1; k < 64; k++)
3062 block[k] += (block[k] < 0) ? -mquant : mquant;
3065 if (use_pred) i = 63;
3066 } else { // no AC coeffs
3069 memset(ac_val2, 0, 16 * 2);
3070 if (dc_pred_dir) { // left
3072 memcpy(ac_val2, ac_val, 8 * 2);
3073 if (q2 && q1 != q2) {
3074 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3075 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3077 return AVERROR_INVALIDDATA;
3078 for (k = 1; k < 8; k++)
3079 ac_val2[k] = (ac_val2[k] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3084 memcpy(ac_val2 + 8, ac_val + 8, 8 * 2);
3085 if (q2 && q1 != q2) {
3086 q1 = q1 * 2 + ((q1 == v->pq) ? v->halfpq : 0) - 1;
3087 q2 = q2 * 2 + ((q2 == v->pq) ? v->halfpq : 0) - 1;
3089 return AVERROR_INVALIDDATA;
3090 for (k = 1; k < 8; k++)
3091 ac_val2[k + 8] = (ac_val2[k + 8] * q2 * ff_vc1_dqscale[q1 - 1] + 0x20000) >> 18;
3096 /* apply AC prediction if needed */
3098 if (dc_pred_dir) { // left
3099 for (k = 1; k < 8; k++) {
3100 block[k << v->left_blk_sh] = ac_val2[k] * scale;
3101 if (!v->pquantizer && block[k << v->left_blk_sh])
3102 block[k << v->left_blk_sh] += (block[k << v->left_blk_sh] < 0) ? -mquant : mquant;
3105 for (k = 1; k < 8; k++) {
3106 block[k << v->top_blk_sh] = ac_val2[k + 8] * scale;
3107 if (!v->pquantizer && block[k << v->top_blk_sh])
3108 block[k << v->top_blk_sh] += (block[k << v->top_blk_sh] < 0) ? -mquant : mquant;
3114 s->block_last_index[n] = i;
3121 static int vc1_decode_p_block(VC1Context *v, DCTELEM block[64], int n,
3122 int mquant, int ttmb, int first_block,
3123 uint8_t *dst, int linesize, int skip_block,
3126 MpegEncContext *s = &v->s;
3127 GetBitContext *gb = &s->gb;
3130 int scale, off, idx, last, skip, value;
3131 int ttblk = ttmb & 7;
3134 s->dsp.clear_block(block);
3137 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)];
3139 if (ttblk == TT_4X4) {
3140 subblkpat = ~(get_vlc2(gb, ff_vc1_subblkpat_vlc[v->tt_index].table, VC1_SUBBLKPAT_VLC_BITS, 1) + 1);
3142 if ((ttblk != TT_8X8 && ttblk != TT_4X4)
3143 && ((v->ttmbf || (ttmb != -1 && (ttmb & 8) && !first_block))
3144 || (!v->res_rtm_flag && !first_block))) {
3145 subblkpat = decode012(gb);
3147 subblkpat ^= 3; // swap decoded pattern bits
3148 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM)
3150 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT)
3153 scale = 2 * mquant + ((v->pq == mquant) ? v->halfpq : 0);
3155 // convert transforms like 8X4_TOP to generic TT and SUBBLKPAT
3156 if (ttblk == TT_8X4_TOP || ttblk == TT_8X4_BOTTOM) {
3157 subblkpat = 2 - (ttblk == TT_8X4_TOP);
3160 if (ttblk == TT_4X8_RIGHT || ttblk == TT_4X8_LEFT) {
3161 subblkpat = 2 - (ttblk == TT_4X8_LEFT);
3170 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3175 idx = v->zz_8x8[0][i++];
3177 idx = v->zzi_8x8[i++];
3178 block[idx] = value * scale;
3180 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3184 v->vc1dsp.vc1_inv_trans_8x8_dc(dst, linesize, block);
3186 v->vc1dsp.vc1_inv_trans_8x8(block);
3187 s->dsp.add_pixels_clamped(block, dst, linesize);
3192 pat = ~subblkpat & 0xF;
3193 for (j = 0; j < 4; j++) {
3194 last = subblkpat & (1 << (3 - j));
3196 off = (j & 1) * 4 + (j & 2) * 16;
3198 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3203 idx = ff_vc1_simple_progressive_4x4_zz[i++];
3205 idx = ff_vc1_adv_interlaced_4x4_zz[i++];
3206 block[idx + off] = value * scale;
3208 block[idx + off] += (block[idx + off] < 0) ? -mquant : mquant;
3210 if (!(subblkpat & (1 << (3 - j))) && !skip_block) {
3212 v->vc1dsp.vc1_inv_trans_4x4_dc(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3214 v->vc1dsp.vc1_inv_trans_4x4(dst + (j & 1) * 4 + (j & 2) * 2 * linesize, linesize, block + off);
3219 pat = ~((subblkpat & 2) * 6 + (subblkpat & 1) * 3) & 0xF;
3220 for (j = 0; j < 2; j++) {
3221 last = subblkpat & (1 << (1 - j));
3225 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3230 idx = v->zz_8x4[i++] + off;
3232 idx = ff_vc1_adv_interlaced_8x4_zz[i++] + off;
3233 block[idx] = value * scale;
3235 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3237 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3239 v->vc1dsp.vc1_inv_trans_8x4_dc(dst + j * 4 * linesize, linesize, block + off);
3241 v->vc1dsp.vc1_inv_trans_8x4(dst + j * 4 * linesize, linesize, block + off);
3246 pat = ~(subblkpat * 5) & 0xF;
3247 for (j = 0; j < 2; j++) {
3248 last = subblkpat & (1 << (1 - j));
3252 vc1_decode_ac_coeff(v, &last, &skip, &value, v->codingset2);
3257 idx = v->zz_4x8[i++] + off;
3259 idx = ff_vc1_adv_interlaced_4x8_zz[i++] + off;
3260 block[idx] = value * scale;
3262 block[idx] += (block[idx] < 0) ? -mquant : mquant;
3264 if (!(subblkpat & (1 << (1 - j))) && !skip_block) {
3266 v->vc1dsp.vc1_inv_trans_4x8_dc(dst + j * 4, linesize, block + off);
3268 v->vc1dsp.vc1_inv_trans_4x8(dst + j*4, linesize, block + off);
3274 *ttmb_out |= ttblk << (n * 4);
3278 /** @} */ // Macroblock group
3280 static const int size_table [6] = { 0, 2, 3, 4, 5, 8 };
3281 static const int offset_table[6] = { 0, 1, 3, 7, 15, 31 };
3283 static av_always_inline void vc1_apply_p_v_loop_filter(VC1Context *v, int block_num)
3285 MpegEncContext *s = &v->s;
3286 int mb_cbp = v->cbp[s->mb_x - s->mb_stride],
3287 block_cbp = mb_cbp >> (block_num * 4), bottom_cbp,
3288 mb_is_intra = v->is_intra[s->mb_x - s->mb_stride],
3289 block_is_intra = mb_is_intra >> (block_num * 4), bottom_is_intra;
3290 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3293 if (block_num > 3) {
3294 dst = s->dest[block_num - 3];
3296 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 8) * linesize;
3298 if (s->mb_y != s->end_mb_y || block_num < 2) {
3302 if (block_num > 3) {
3303 bottom_cbp = v->cbp[s->mb_x] >> (block_num * 4);
3304 bottom_is_intra = v->is_intra[s->mb_x] >> (block_num * 4);
3305 mv = &v->luma_mv[s->mb_x - s->mb_stride];
3306 mv_stride = s->mb_stride;
3308 bottom_cbp = (block_num < 2) ? (mb_cbp >> ((block_num + 2) * 4))
3309 : (v->cbp[s->mb_x] >> ((block_num - 2) * 4));
3310 bottom_is_intra = (block_num < 2) ? (mb_is_intra >> ((block_num + 2) * 4))
3311 : (v->is_intra[s->mb_x] >> ((block_num - 2) * 4));
3312 mv_stride = s->b8_stride;
3313 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - 2 * mv_stride];
3316 if (bottom_is_intra & 1 || block_is_intra & 1 ||
3317 mv[0][0] != mv[mv_stride][0] || mv[0][1] != mv[mv_stride][1]) {
3318 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3320 idx = ((bottom_cbp >> 2) | block_cbp) & 3;
3322 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3325 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3327 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3332 dst -= 4 * linesize;
3333 ttblk = (v->ttblk[s->mb_x - s->mb_stride] >> (block_num * 4)) & 0xF;
3334 if (ttblk == TT_4X4 || ttblk == TT_8X4) {
3335 idx = (block_cbp | (block_cbp >> 2)) & 3;
3337 v->vc1dsp.vc1_v_loop_filter8(dst, linesize, v->pq);
3340 v->vc1dsp.vc1_v_loop_filter4(dst + 4, linesize, v->pq);
3342 v->vc1dsp.vc1_v_loop_filter4(dst, linesize, v->pq);
3347 static av_always_inline void vc1_apply_p_h_loop_filter(VC1Context *v, int block_num)
3349 MpegEncContext *s = &v->s;
3350 int mb_cbp = v->cbp[s->mb_x - 1 - s->mb_stride],
3351 block_cbp = mb_cbp >> (block_num * 4), right_cbp,
3352 mb_is_intra = v->is_intra[s->mb_x - 1 - s->mb_stride],
3353 block_is_intra = mb_is_intra >> (block_num * 4), right_is_intra;
3354 int idx, linesize = block_num > 3 ? s->uvlinesize : s->linesize, ttblk;
3357 if (block_num > 3) {
3358 dst = s->dest[block_num - 3] - 8 * linesize;
3360 dst = s->dest[0] + (block_num & 1) * 8 + ((block_num & 2) * 4 - 16) * linesize - 8;
3363 if (s->mb_x != s->mb_width || !(block_num & 5)) {
3366 if (block_num > 3) {
3367 right_cbp = v->cbp[s->mb_x - s->mb_stride] >> (block_num * 4);
3368 right_is_intra = v->is_intra[s->mb_x - s->mb_stride] >> (block_num * 4);
3369 mv = &v->luma_mv[s->mb_x - s->mb_stride - 1];
3371 right_cbp = (block_num & 1) ? (v->cbp[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3372 : (mb_cbp >> ((block_num + 1) * 4));
3373 right_is_intra = (block_num & 1) ? (v->is_intra[s->mb_x - s->mb_stride] >> ((block_num - 1) * 4))
3374 : (mb_is_intra >> ((block_num + 1) * 4));
3375 mv = &s->current_picture.f.motion_val[0][s->block_index[block_num] - s->b8_stride * 2 - 2];
3377 if (block_is_intra & 1 || right_is_intra & 1 || mv[0][0] != mv[1][0] || mv[0][1] != mv[1][1]) {
3378 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3380 idx = ((right_cbp >> 1) | block_cbp) & 5; // FIXME check
3382 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3385 v->vc1dsp.vc1_h_loop_filter4(dst + 4 * linesize, linesize, v->pq);
3387 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3393 ttblk = (v->ttblk[s->mb_x - s->mb_stride - 1] >> (block_num * 4)) & 0xf;
3394 if (ttblk == TT_4X4 || ttblk == TT_4X8) {
3395 idx = (block_cbp | (block_cbp >> 1)) & 5;
3397 v->vc1dsp.vc1_h_loop_filter8(dst, linesize, v->pq);
3400 v->vc1dsp.vc1_h_loop_filter4(dst + linesize * 4, linesize, v->pq);
3402 v->vc1dsp.vc1_h_loop_filter4(dst, linesize, v->pq);
3407 static void vc1_apply_p_loop_filter(VC1Context *v)
3409 MpegEncContext *s = &v->s;
3412 for (i = 0; i < 6; i++) {
3413 vc1_apply_p_v_loop_filter(v, i);
3416 /* V always precedes H, therefore we run H one MB before V;
3417 * at the end of a row, we catch up to complete the row */
3419 for (i = 0; i < 6; i++) {
3420 vc1_apply_p_h_loop_filter(v, i);
3422 if (s->mb_x == s->mb_width - 1) {
3424 ff_update_block_index(s);
3425 for (i = 0; i < 6; i++) {
3426 vc1_apply_p_h_loop_filter(v, i);
3432 /** Decode one P-frame MB
3434 static int vc1_decode_p_mb(VC1Context *v)
3436 MpegEncContext *s = &v->s;
3437 GetBitContext *gb = &s->gb;
3439 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3440 int cbp; /* cbp decoding stuff */
3441 int mqdiff, mquant; /* MB quantization */
3442 int ttmb = v->ttfrm; /* MB Transform type */
3444 int mb_has_coeffs = 1; /* last_flag */
3445 int dmv_x, dmv_y; /* Differential MV components */
3446 int index, index1; /* LUT indexes */
3447 int val, sign; /* temp values */
3448 int first_block = 1;
3450 int skipped, fourmv;
3451 int block_cbp = 0, pat, block_tt = 0, block_intra = 0;
3453 mquant = v->pq; /* lossy initialization */
3455 if (v->mv_type_is_raw)
3456 fourmv = get_bits1(gb);
3458 fourmv = v->mv_type_mb_plane[mb_pos];
3460 skipped = get_bits1(gb);
3462 skipped = v->s.mbskip_table[mb_pos];
3464 if (!fourmv) { /* 1MV mode */
3466 GET_MVDATA(dmv_x, dmv_y);
3469 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3470 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3472 s->current_picture.f.mb_type[mb_pos] = s->mb_intra ? MB_TYPE_INTRA : MB_TYPE_16x16;
3473 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3475 /* FIXME Set DC val for inter block ? */
3476 if (s->mb_intra && !mb_has_coeffs) {
3478 s->ac_pred = get_bits1(gb);
3480 } else if (mb_has_coeffs) {
3482 s->ac_pred = get_bits1(gb);
3483 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3489 s->current_picture.f.qscale_table[mb_pos] = mquant;
3491 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
3492 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table,
3493 VC1_TTMB_VLC_BITS, 2);
3494 if (!s->mb_intra) vc1_mc_1mv(v, 0);
3496 for (i = 0; i < 6; i++) {
3497 s->dc_val[0][s->block_index[i]] = 0;
3499 val = ((cbp >> (5 - i)) & 1);
3500 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3501 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3503 /* check if prediction blocks A and C are available */
3504 v->a_avail = v->c_avail = 0;
3505 if (i == 2 || i == 3 || !s->first_slice_line)
3506 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3507 if (i == 1 || i == 3 || s->mb_x)
3508 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3510 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3511 (i & 4) ? v->codingset2 : v->codingset);
3512 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3514 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3516 for (j = 0; j < 64; j++)
3517 s->block[i][j] <<= 1;
3518 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3519 if (v->pq >= 9 && v->overlap) {
3521 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3523 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3525 block_cbp |= 0xF << (i << 2);
3526 block_intra |= 1 << i;
3528 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb, first_block,
3529 s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize,
3530 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3531 block_cbp |= pat << (i << 2);
3532 if (!v->ttmbf && ttmb < 8)
3539 for (i = 0; i < 6; i++) {
3540 v->mb_type[0][s->block_index[i]] = 0;
3541 s->dc_val[0][s->block_index[i]] = 0;
3543 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3544 s->current_picture.f.qscale_table[mb_pos] = 0;
3545 vc1_pred_mv(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3548 } else { // 4MV mode
3549 if (!skipped /* unskipped MB */) {
3550 int intra_count = 0, coded_inter = 0;
3551 int is_intra[6], is_coded[6];
3553 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3554 for (i = 0; i < 6; i++) {
3555 val = ((cbp >> (5 - i)) & 1);
3556 s->dc_val[0][s->block_index[i]] = 0;
3563 GET_MVDATA(dmv_x, dmv_y);
3565 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3567 vc1_mc_4mv_luma(v, i, 0);
3568 intra_count += s->mb_intra;
3569 is_intra[i] = s->mb_intra;
3570 is_coded[i] = mb_has_coeffs;
3573 is_intra[i] = (intra_count >= 3);
3577 vc1_mc_4mv_chroma(v, 0);
3578 v->mb_type[0][s->block_index[i]] = is_intra[i];
3580 coded_inter = !is_intra[i] & is_coded[i];
3582 // if there are no coded blocks then don't do anything more
3584 if (!intra_count && !coded_inter)
3587 s->current_picture.f.qscale_table[mb_pos] = mquant;
3588 /* test if block is intra and has pred */
3591 for (i = 0; i < 6; i++)
3593 if (((!s->first_slice_line || (i == 2 || i == 3)) && v->mb_type[0][s->block_index[i] - s->block_wrap[i]])
3594 || ((s->mb_x || (i == 1 || i == 3)) && v->mb_type[0][s->block_index[i] - 1])) {
3600 s->ac_pred = get_bits1(gb);
3604 if (!v->ttmbf && coded_inter)
3605 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3606 for (i = 0; i < 6; i++) {
3608 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3609 s->mb_intra = is_intra[i];
3611 /* check if prediction blocks A and C are available */
3612 v->a_avail = v->c_avail = 0;
3613 if (i == 2 || i == 3 || !s->first_slice_line)
3614 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3615 if (i == 1 || i == 3 || s->mb_x)
3616 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3618 vc1_decode_intra_block(v, s->block[i], i, is_coded[i], mquant,
3619 (i & 4) ? v->codingset2 : v->codingset);
3620 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3622 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3624 for (j = 0; j < 64; j++)
3625 s->block[i][j] <<= 1;
3626 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off,
3627 (i & 4) ? s->uvlinesize : s->linesize);
3628 if (v->pq >= 9 && v->overlap) {
3630 v->vc1dsp.vc1_h_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3632 v->vc1dsp.vc1_v_overlap(s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
3634 block_cbp |= 0xF << (i << 2);
3635 block_intra |= 1 << i;
3636 } else if (is_coded[i]) {
3637 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3638 first_block, s->dest[dst_idx] + off,
3639 (i & 4) ? s->uvlinesize : s->linesize,
3640 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3642 block_cbp |= pat << (i << 2);
3643 if (!v->ttmbf && ttmb < 8)
3648 } else { // skipped MB
3650 s->current_picture.f.qscale_table[mb_pos] = 0;
3651 for (i = 0; i < 6; i++) {
3652 v->mb_type[0][s->block_index[i]] = 0;
3653 s->dc_val[0][s->block_index[i]] = 0;
3655 for (i = 0; i < 4; i++) {
3656 vc1_pred_mv(v, i, 0, 0, 0, v->range_x, v->range_y, v->mb_type[0], 0, 0);
3657 vc1_mc_4mv_luma(v, i, 0);
3659 vc1_mc_4mv_chroma(v, 0);
3660 s->current_picture.f.qscale_table[mb_pos] = 0;
3664 v->cbp[s->mb_x] = block_cbp;
3665 v->ttblk[s->mb_x] = block_tt;
3666 v->is_intra[s->mb_x] = block_intra;
3671 /* Decode one macroblock in an interlaced frame p picture */
3673 static int vc1_decode_p_mb_intfr(VC1Context *v)
3675 MpegEncContext *s = &v->s;
3676 GetBitContext *gb = &s->gb;
3678 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3679 int cbp = 0; /* cbp decoding stuff */
3680 int mqdiff, mquant; /* MB quantization */
3681 int ttmb = v->ttfrm; /* MB Transform type */
3683 int mb_has_coeffs = 1; /* last_flag */
3684 int dmv_x, dmv_y; /* Differential MV components */
3685 int val; /* temp value */
3686 int first_block = 1;
3688 int skipped, fourmv = 0, twomv = 0;
3689 int block_cbp = 0, pat, block_tt = 0;
3690 int idx_mbmode = 0, mvbp;
3691 int stride_y, fieldtx;
3693 mquant = v->pq; /* Lossy initialization */
3696 skipped = get_bits1(gb);
3698 skipped = v->s.mbskip_table[mb_pos];
3700 if (v->fourmvswitch)
3701 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_4MV_MBMODE_VLC_BITS, 2); // try getting this done
3703 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_INTFR_NON4MV_MBMODE_VLC_BITS, 2); // in a single line
3704 switch (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0]) {
3705 /* store the motion vector type in a flag (useful later) */
3706 case MV_PMODE_INTFR_4MV:
3708 v->blk_mv_type[s->block_index[0]] = 0;
3709 v->blk_mv_type[s->block_index[1]] = 0;
3710 v->blk_mv_type[s->block_index[2]] = 0;
3711 v->blk_mv_type[s->block_index[3]] = 0;
3713 case MV_PMODE_INTFR_4MV_FIELD:
3715 v->blk_mv_type[s->block_index[0]] = 1;
3716 v->blk_mv_type[s->block_index[1]] = 1;
3717 v->blk_mv_type[s->block_index[2]] = 1;
3718 v->blk_mv_type[s->block_index[3]] = 1;
3720 case MV_PMODE_INTFR_2MV_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_1MV:
3728 v->blk_mv_type[s->block_index[0]] = 0;
3729 v->blk_mv_type[s->block_index[1]] = 0;
3730 v->blk_mv_type[s->block_index[2]] = 0;
3731 v->blk_mv_type[s->block_index[3]] = 0;
3734 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_INTRA) { // intra MB
3735 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
3736 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
3737 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
3738 s->mb_intra = v->is_intra[s->mb_x] = 1;
3739 for (i = 0; i < 6; i++)
3740 v->mb_type[0][s->block_index[i]] = 1;
3741 fieldtx = v->fieldtx_plane[mb_pos] = get_bits1(gb);
3742 mb_has_coeffs = get_bits1(gb);
3744 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3745 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3747 s->current_picture.f.qscale_table[mb_pos] = mquant;
3748 /* Set DC scale - y and c use the same (not sure if necessary here) */
3749 s->y_dc_scale = s->y_dc_scale_table[mquant];
3750 s->c_dc_scale = s->c_dc_scale_table[mquant];
3752 for (i = 0; i < 6; i++) {
3753 s->dc_val[0][s->block_index[i]] = 0;
3755 val = ((cbp >> (5 - i)) & 1);
3756 v->mb_type[0][s->block_index[i]] = s->mb_intra;
3757 v->a_avail = v->c_avail = 0;
3758 if (i == 2 || i == 3 || !s->first_slice_line)
3759 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3760 if (i == 1 || i == 3 || s->mb_x)
3761 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3763 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3764 (i & 4) ? v->codingset2 : v->codingset);
3765 if ((i>3) && (s->flags & CODEC_FLAG_GRAY)) continue;
3766 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3768 stride_y = s->linesize << fieldtx;
3769 off = (fieldtx) ? ((i & 1) * 8) + ((i & 2) >> 1) * s->linesize : (i & 1) * 8 + 4 * (i & 2) * s->linesize;
3771 stride_y = s->uvlinesize;
3774 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, stride_y);
3778 } else { // inter MB
3779 mb_has_coeffs = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][3];
3781 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3782 if (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_2MV_FIELD) {
3783 v->twomvbp = get_vlc2(gb, v->twomvbp_vlc->table, VC1_2MV_BLOCK_PATTERN_VLC_BITS, 1);
3785 if ((ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV)
3786 || (ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][0] == MV_PMODE_INTFR_4MV_FIELD)) {
3787 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3790 s->mb_intra = v->is_intra[s->mb_x] = 0;
3791 for (i = 0; i < 6; i++)
3792 v->mb_type[0][s->block_index[i]] = 0;
3793 fieldtx = v->fieldtx_plane[mb_pos] = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][1];
3794 /* for all motion vector read MVDATA and motion compensate each block */
3798 for (i = 0; i < 6; i++) {
3801 val = ((mvbp >> (3 - i)) & 1);
3803 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3805 vc1_pred_mv_intfr(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0]);
3806 vc1_mc_4mv_luma(v, i, 0);
3807 } else if (i == 4) {
3808 vc1_mc_4mv_chroma4(v);
3815 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3817 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3818 vc1_mc_4mv_luma(v, 0, 0);
3819 vc1_mc_4mv_luma(v, 1, 0);
3822 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3824 vc1_pred_mv_intfr(v, 2, dmv_x, dmv_y, 2, v->range_x, v->range_y, v->mb_type[0]);
3825 vc1_mc_4mv_luma(v, 2, 0);
3826 vc1_mc_4mv_luma(v, 3, 0);
3827 vc1_mc_4mv_chroma4(v);
3829 mvbp = ff_vc1_mbmode_intfrp[v->fourmvswitch][idx_mbmode][2];
3832 get_mvdata_interlaced(v, &dmv_x, &dmv_y, 0);
3834 vc1_pred_mv_intfr(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0]);
3838 GET_MQUANT(); // p. 227
3839 s->current_picture.f.qscale_table[mb_pos] = mquant;
3840 if (!v->ttmbf && cbp)
3841 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3842 for (i = 0; i < 6; i++) {
3843 s->dc_val[0][s->block_index[i]] = 0;
3845 val = ((cbp >> (5 - i)) & 1);
3847 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3849 off = (i & 4) ? 0 : ((i & 1) * 8 + ((i > 1) * s->linesize));
3851 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3852 first_block, s->dest[dst_idx] + off,
3853 (i & 4) ? s->uvlinesize : (s->linesize << fieldtx),
3854 (i & 4) && (s->flags & CODEC_FLAG_GRAY), &block_tt);
3855 block_cbp |= pat << (i << 2);
3856 if (!v->ttmbf && ttmb < 8)
3863 s->mb_intra = v->is_intra[s->mb_x] = 0;
3864 for (i = 0; i < 6; i++) {
3865 v->mb_type[0][s->block_index[i]] = 0;
3866 s->dc_val[0][s->block_index[i]] = 0;
3868 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_SKIP;
3869 s->current_picture.f.qscale_table[mb_pos] = 0;
3870 v->blk_mv_type[s->block_index[0]] = 0;
3871 v->blk_mv_type[s->block_index[1]] = 0;
3872 v->blk_mv_type[s->block_index[2]] = 0;
3873 v->blk_mv_type[s->block_index[3]] = 0;
3874 vc1_pred_mv_intfr(v, 0, 0, 0, 1, v->range_x, v->range_y, v->mb_type[0]);
3877 if (s->mb_x == s->mb_width - 1)
3878 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0])*s->mb_stride);
3882 static int vc1_decode_p_mb_intfi(VC1Context *v)
3884 MpegEncContext *s = &v->s;
3885 GetBitContext *gb = &s->gb;
3887 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
3888 int cbp = 0; /* cbp decoding stuff */
3889 int mqdiff, mquant; /* MB quantization */
3890 int ttmb = v->ttfrm; /* MB Transform type */
3892 int mb_has_coeffs = 1; /* last_flag */
3893 int dmv_x, dmv_y; /* Differential MV components */
3894 int val; /* temp values */
3895 int first_block = 1;
3898 int block_cbp = 0, pat, block_tt = 0;
3901 mquant = v->pq; /* Lossy initialization */
3903 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
3904 if (idx_mbmode <= 1) { // intra MB
3905 s->mb_intra = v->is_intra[s->mb_x] = 1;
3906 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
3907 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
3908 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
3910 s->current_picture.f.qscale_table[mb_pos] = mquant;
3911 /* Set DC scale - y and c use the same (not sure if necessary here) */
3912 s->y_dc_scale = s->y_dc_scale_table[mquant];
3913 s->c_dc_scale = s->c_dc_scale_table[mquant];
3914 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
3915 mb_has_coeffs = idx_mbmode & 1;
3917 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
3919 for (i = 0; i < 6; i++) {
3920 s->dc_val[0][s->block_index[i]] = 0;
3921 v->mb_type[0][s->block_index[i]] = 1;
3923 val = ((cbp >> (5 - i)) & 1);
3924 v->a_avail = v->c_avail = 0;
3925 if (i == 2 || i == 3 || !s->first_slice_line)
3926 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
3927 if (i == 1 || i == 3 || s->mb_x)
3928 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
3930 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
3931 (i & 4) ? v->codingset2 : v->codingset);
3932 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
3934 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
3935 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
3936 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
3937 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
3938 // TODO: loop filter
3941 s->mb_intra = v->is_intra[s->mb_x] = 0;
3942 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
3943 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
3944 if (idx_mbmode <= 5) { // 1-MV
3945 dmv_x = dmv_y = pred_flag = 0;
3946 if (idx_mbmode & 1) {
3947 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3949 vc1_pred_mv(v, 0, dmv_x, dmv_y, 1, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3951 mb_has_coeffs = !(idx_mbmode & 2);
3953 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
3954 for (i = 0; i < 6; i++) {
3956 dmv_x = dmv_y = pred_flag = 0;
3957 val = ((v->fourmvbp >> (3 - i)) & 1);
3959 get_mvdata_interlaced(v, &dmv_x, &dmv_y, &pred_flag);
3961 vc1_pred_mv(v, i, dmv_x, dmv_y, 0, v->range_x, v->range_y, v->mb_type[0], pred_flag, 0);
3962 vc1_mc_4mv_luma(v, i, 0);
3964 vc1_mc_4mv_chroma(v, 0);
3966 mb_has_coeffs = idx_mbmode & 1;
3969 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
3973 s->current_picture.f.qscale_table[mb_pos] = mquant;
3974 if (!v->ttmbf && cbp) {
3975 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
3978 for (i = 0; i < 6; i++) {
3979 s->dc_val[0][s->block_index[i]] = 0;
3981 val = ((cbp >> (5 - i)) & 1);
3982 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
3983 if (v->second_field)
3984 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
3986 pat = vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
3987 first_block, s->dest[dst_idx] + off,
3988 (i & 4) ? s->uvlinesize : s->linesize,
3989 (i & 4) && (s->flags & CODEC_FLAG_GRAY),
3991 block_cbp |= pat << (i << 2);
3992 if (!v->ttmbf && ttmb < 8) ttmb = -1;
3997 if (s->mb_x == s->mb_width - 1)
3998 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4002 /** Decode one B-frame MB (in Main profile)
4004 static void vc1_decode_b_mb(VC1Context *v)
4006 MpegEncContext *s = &v->s;
4007 GetBitContext *gb = &s->gb;
4009 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4010 int cbp = 0; /* cbp decoding stuff */
4011 int mqdiff, mquant; /* MB quantization */
4012 int ttmb = v->ttfrm; /* MB Transform type */
4013 int mb_has_coeffs = 0; /* last_flag */
4014 int index, index1; /* LUT indexes */
4015 int val, sign; /* temp values */
4016 int first_block = 1;
4018 int skipped, direct;
4019 int dmv_x[2], dmv_y[2];
4020 int bmvtype = BMV_TYPE_BACKWARD;
4022 mquant = v->pq; /* lossy initialization */
4026 direct = get_bits1(gb);
4028 direct = v->direct_mb_plane[mb_pos];
4030 skipped = get_bits1(gb);
4032 skipped = v->s.mbskip_table[mb_pos];
4034 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4035 for (i = 0; i < 6; i++) {
4036 v->mb_type[0][s->block_index[i]] = 0;
4037 s->dc_val[0][s->block_index[i]] = 0;
4039 s->current_picture.f.qscale_table[mb_pos] = 0;
4043 GET_MVDATA(dmv_x[0], dmv_y[0]);
4044 dmv_x[1] = dmv_x[0];
4045 dmv_y[1] = dmv_y[0];
4047 if (skipped || !s->mb_intra) {
4048 bmvtype = decode012(gb);
4051 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_BACKWARD : BMV_TYPE_FORWARD;
4054 bmvtype = (v->bfraction >= (B_FRACTION_DEN/2)) ? BMV_TYPE_FORWARD : BMV_TYPE_BACKWARD;
4057 bmvtype = BMV_TYPE_INTERPOLATED;
4058 dmv_x[0] = dmv_y[0] = 0;
4062 for (i = 0; i < 6; i++)
4063 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4067 bmvtype = BMV_TYPE_INTERPOLATED;
4068 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4069 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4073 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4076 s->current_picture.f.qscale_table[mb_pos] = mquant;
4078 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4079 dmv_x[0] = dmv_y[0] = dmv_x[1] = dmv_y[1] = 0;
4080 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4081 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4083 if (!mb_has_coeffs && !s->mb_intra) {
4084 /* no coded blocks - effectively skipped */
4085 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4086 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4089 if (s->mb_intra && !mb_has_coeffs) {
4091 s->current_picture.f.qscale_table[mb_pos] = mquant;
4092 s->ac_pred = get_bits1(gb);
4094 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4096 if (bmvtype == BMV_TYPE_INTERPOLATED) {
4097 GET_MVDATA(dmv_x[0], dmv_y[0]);
4098 if (!mb_has_coeffs) {
4099 /* interpolated skipped block */
4100 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4101 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4105 vc1_pred_b_mv(v, dmv_x, dmv_y, direct, bmvtype);
4107 vc1_b_mc(v, dmv_x, dmv_y, direct, bmvtype);
4110 s->ac_pred = get_bits1(gb);
4111 cbp = get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4113 s->current_picture.f.qscale_table[mb_pos] = mquant;
4114 if (!v->ttmbf && !s->mb_intra && mb_has_coeffs)
4115 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4119 for (i = 0; i < 6; i++) {
4120 s->dc_val[0][s->block_index[i]] = 0;
4122 val = ((cbp >> (5 - i)) & 1);
4123 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4124 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4126 /* check if prediction blocks A and C are available */
4127 v->a_avail = v->c_avail = 0;
4128 if (i == 2 || i == 3 || !s->first_slice_line)
4129 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4130 if (i == 1 || i == 3 || s->mb_x)
4131 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4133 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4134 (i & 4) ? v->codingset2 : v->codingset);
4135 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4137 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4139 for (j = 0; j < 64; j++)
4140 s->block[i][j] <<= 1;
4141 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, i & 4 ? s->uvlinesize : s->linesize);
4143 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4144 first_block, s->dest[dst_idx] + off,
4145 (i & 4) ? s->uvlinesize : s->linesize,
4146 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4147 if (!v->ttmbf && ttmb < 8)
4154 /** Decode one B-frame MB (in interlaced field B picture)
4156 static void vc1_decode_b_mb_intfi(VC1Context *v)
4158 MpegEncContext *s = &v->s;
4159 GetBitContext *gb = &s->gb;
4161 int mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4162 int cbp = 0; /* cbp decoding stuff */
4163 int mqdiff, mquant; /* MB quantization */
4164 int ttmb = v->ttfrm; /* MB Transform type */
4165 int mb_has_coeffs = 0; /* last_flag */
4166 int val; /* temp value */
4167 int first_block = 1;
4170 int dmv_x[2], dmv_y[2], pred_flag[2];
4171 int bmvtype = BMV_TYPE_BACKWARD;
4172 int idx_mbmode, interpmvp;
4174 mquant = v->pq; /* Lossy initialization */
4177 idx_mbmode = get_vlc2(gb, v->mbmode_vlc->table, VC1_IF_MBMODE_VLC_BITS, 2);
4178 if (idx_mbmode <= 1) { // intra MB
4179 s->mb_intra = v->is_intra[s->mb_x] = 1;
4180 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4181 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4182 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4184 s->current_picture.f.qscale_table[mb_pos] = mquant;
4185 /* Set DC scale - y and c use the same (not sure if necessary here) */
4186 s->y_dc_scale = s->y_dc_scale_table[mquant];
4187 s->c_dc_scale = s->c_dc_scale_table[mquant];
4188 v->s.ac_pred = v->acpred_plane[mb_pos] = get_bits1(gb);
4189 mb_has_coeffs = idx_mbmode & 1;
4191 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_ICBPCY_VLC_BITS, 2);
4193 for (i = 0; i < 6; i++) {
4194 s->dc_val[0][s->block_index[i]] = 0;
4196 val = ((cbp >> (5 - i)) & 1);
4197 v->mb_type[0][s->block_index[i]] = s->mb_intra;
4198 v->a_avail = v->c_avail = 0;
4199 if (i == 2 || i == 3 || !s->first_slice_line)
4200 v->a_avail = v->mb_type[0][s->block_index[i] - s->block_wrap[i]];
4201 if (i == 1 || i == 3 || s->mb_x)
4202 v->c_avail = v->mb_type[0][s->block_index[i] - 1];
4204 vc1_decode_intra_block(v, s->block[i], i, val, mquant,
4205 (i & 4) ? v->codingset2 : v->codingset);
4206 if ((i>3) && (s->flags & CODEC_FLAG_GRAY))
4208 v->vc1dsp.vc1_inv_trans_8x8(s->block[i]);
4210 for (j = 0; j < 64; j++)
4211 s->block[i][j] <<= 1;
4212 off = (i & 4) ? 0 : ((i & 1) * 8 + (i & 2) * 4 * s->linesize);
4213 off += v->second_field ? ((i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0]) : 0;
4214 s->dsp.put_signed_pixels_clamped(s->block[i], s->dest[dst_idx] + off, (i & 4) ? s->uvlinesize : s->linesize);
4215 // TODO: yet to perform loop filter
4218 s->mb_intra = v->is_intra[s->mb_x] = 0;
4219 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_16x16;
4220 for (i = 0; i < 6; i++) v->mb_type[0][s->block_index[i]] = 0;
4222 fwd = v->forward_mb_plane[mb_pos] = get_bits1(gb);
4224 fwd = v->forward_mb_plane[mb_pos];
4225 if (idx_mbmode <= 5) { // 1-MV
4226 dmv_x[0] = dmv_x[1] = dmv_y[0] = dmv_y[1] = 0;
4227 pred_flag[0] = pred_flag[1] = 0;
4229 bmvtype = BMV_TYPE_FORWARD;
4231 bmvtype = decode012(gb);
4234 bmvtype = BMV_TYPE_BACKWARD;
4237 bmvtype = BMV_TYPE_DIRECT;
4240 bmvtype = BMV_TYPE_INTERPOLATED;
4241 interpmvp = get_bits1(gb);
4244 v->bmvtype = bmvtype;
4245 if (bmvtype != BMV_TYPE_DIRECT && idx_mbmode & 1) {
4246 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD], &dmv_y[bmvtype == BMV_TYPE_BACKWARD], &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4248 if (bmvtype == BMV_TYPE_INTERPOLATED && interpmvp) {
4249 get_mvdata_interlaced(v, &dmv_x[1], &dmv_y[1], &pred_flag[1]);
4251 if (bmvtype == BMV_TYPE_DIRECT) {
4252 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4253 dmv_x[1] = dmv_y[1] = pred_flag[0] = 0;
4255 vc1_pred_b_mv_intfi(v, 0, dmv_x, dmv_y, 1, pred_flag);
4256 vc1_b_mc(v, dmv_x, dmv_y, (bmvtype == BMV_TYPE_DIRECT), bmvtype);
4257 mb_has_coeffs = !(idx_mbmode & 2);
4260 bmvtype = BMV_TYPE_FORWARD;
4261 v->bmvtype = bmvtype;
4262 v->fourmvbp = get_vlc2(gb, v->fourmvbp_vlc->table, VC1_4MV_BLOCK_PATTERN_VLC_BITS, 1);
4263 for (i = 0; i < 6; i++) {
4265 dmv_x[0] = dmv_y[0] = pred_flag[0] = 0;
4266 dmv_x[1] = dmv_y[1] = pred_flag[1] = 0;
4267 val = ((v->fourmvbp >> (3 - i)) & 1);
4269 get_mvdata_interlaced(v, &dmv_x[bmvtype == BMV_TYPE_BACKWARD],
4270 &dmv_y[bmvtype == BMV_TYPE_BACKWARD],
4271 &pred_flag[bmvtype == BMV_TYPE_BACKWARD]);
4273 vc1_pred_b_mv_intfi(v, i, dmv_x, dmv_y, 0, pred_flag);
4274 vc1_mc_4mv_luma(v, i, bmvtype == BMV_TYPE_BACKWARD);
4276 vc1_mc_4mv_chroma(v, bmvtype == BMV_TYPE_BACKWARD);
4278 mb_has_coeffs = idx_mbmode & 1;
4281 cbp = 1 + get_vlc2(&v->s.gb, v->cbpcy_vlc->table, VC1_CBPCY_P_VLC_BITS, 2);
4285 s->current_picture.f.qscale_table[mb_pos] = mquant;
4286 if (!v->ttmbf && cbp) {
4287 ttmb = get_vlc2(gb, ff_vc1_ttmb_vlc[v->tt_index].table, VC1_TTMB_VLC_BITS, 2);
4290 for (i = 0; i < 6; i++) {
4291 s->dc_val[0][s->block_index[i]] = 0;
4293 val = ((cbp >> (5 - i)) & 1);
4294 off = (i & 4) ? 0 : (i & 1) * 8 + (i & 2) * 4 * s->linesize;
4295 if (v->second_field)
4296 off += (i & 4) ? s->current_picture_ptr->f.linesize[1] : s->current_picture_ptr->f.linesize[0];
4298 vc1_decode_p_block(v, s->block[i], i, mquant, ttmb,
4299 first_block, s->dest[dst_idx] + off,
4300 (i & 4) ? s->uvlinesize : s->linesize,
4301 (i & 4) && (s->flags & CODEC_FLAG_GRAY), NULL);
4302 if (!v->ttmbf && ttmb < 8)
4310 /** Decode blocks of I-frame
4312 static void vc1_decode_i_blocks(VC1Context *v)
4315 MpegEncContext *s = &v->s;
4320 /* select codingmode used for VLC tables selection */
4321 switch (v->y_ac_table_index) {
4323 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4326 v->codingset = CS_HIGH_MOT_INTRA;
4329 v->codingset = CS_MID_RATE_INTRA;
4333 switch (v->c_ac_table_index) {
4335 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4338 v->codingset2 = CS_HIGH_MOT_INTER;
4341 v->codingset2 = CS_MID_RATE_INTER;
4345 /* Set DC scale - y and c use the same */
4346 s->y_dc_scale = s->y_dc_scale_table[v->pq];
4347 s->c_dc_scale = s->c_dc_scale_table[v->pq];
4350 s->mb_x = s->mb_y = 0;
4352 s->first_slice_line = 1;
4353 for (s->mb_y = 0; s->mb_y < s->mb_height; s->mb_y++) {
4355 ff_init_block_index(s);
4356 for (; s->mb_x < s->mb_width; s->mb_x++) {
4358 ff_update_block_index(s);
4359 dst[0] = s->dest[0];
4360 dst[1] = dst[0] + 8;
4361 dst[2] = s->dest[0] + s->linesize * 8;
4362 dst[3] = dst[2] + 8;
4363 dst[4] = s->dest[1];
4364 dst[5] = s->dest[2];
4365 s->dsp.clear_blocks(s->block[0]);
4366 mb_pos = s->mb_x + s->mb_y * s->mb_width;
4367 s->current_picture.f.mb_type[mb_pos] = MB_TYPE_INTRA;
4368 s->current_picture.f.qscale_table[mb_pos] = v->pq;
4369 s->current_picture.f.motion_val[1][s->block_index[0]][0] = 0;
4370 s->current_picture.f.motion_val[1][s->block_index[0]][1] = 0;
4372 // do actual MB decoding and displaying
4373 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4374 v->s.ac_pred = get_bits1(&v->s.gb);
4376 for (k = 0; k < 6; k++) {
4377 val = ((cbp >> (5 - k)) & 1);
4380 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4384 cbp |= val << (5 - k);
4386 vc1_decode_i_block(v, s->block[k], k, val, (k < 4) ? v->codingset : v->codingset2);
4388 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4390 v->vc1dsp.vc1_inv_trans_8x8(s->block[k]);
4391 if (v->pq >= 9 && v->overlap) {
4393 for (j = 0; j < 64; j++)
4394 s->block[k][j] <<= 1;
4395 s->dsp.put_signed_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4398 for (j = 0; j < 64; j++)
4399 s->block[k][j] = (s->block[k][j] - 64) << 1;
4400 s->dsp.put_pixels_clamped(s->block[k], dst[k], k & 4 ? s->uvlinesize : s->linesize);
4404 if (v->pq >= 9 && v->overlap) {
4406 v->vc1dsp.vc1_h_overlap(s->dest[0], s->linesize);
4407 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4408 if (!(s->flags & CODEC_FLAG_GRAY)) {
4409 v->vc1dsp.vc1_h_overlap(s->dest[1], s->uvlinesize);
4410 v->vc1dsp.vc1_h_overlap(s->dest[2], s->uvlinesize);
4413 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8, s->linesize);
4414 v->vc1dsp.vc1_h_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4415 if (!s->first_slice_line) {
4416 v->vc1dsp.vc1_v_overlap(s->dest[0], s->linesize);
4417 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8, s->linesize);
4418 if (!(s->flags & CODEC_FLAG_GRAY)) {
4419 v->vc1dsp.vc1_v_overlap(s->dest[1], s->uvlinesize);
4420 v->vc1dsp.vc1_v_overlap(s->dest[2], s->uvlinesize);
4423 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize, s->linesize);
4424 v->vc1dsp.vc1_v_overlap(s->dest[0] + 8 * s->linesize + 8, s->linesize);
4426 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4428 if (get_bits_count(&s->gb) > v->bits) {
4429 ff_er_add_slice(s, 0, 0, s->mb_x, s->mb_y, ER_MB_ERROR);
4430 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4431 get_bits_count(&s->gb), v->bits);
4435 if (!v->s.loop_filter)
4436 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4438 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4440 s->first_slice_line = 0;
4442 if (v->s.loop_filter)
4443 ff_draw_horiz_band(s, (s->mb_height - 1) * 16, 16);
4444 ff_er_add_slice(s, 0, 0, s->mb_width - 1, s->mb_height - 1, ER_MB_END);
4447 /** Decode blocks of I-frame for advanced profile
4449 static void vc1_decode_i_blocks_adv(VC1Context *v)
4452 MpegEncContext *s = &v->s;
4458 GetBitContext *gb = &s->gb;
4460 /* select codingmode used for VLC tables selection */
4461 switch (v->y_ac_table_index) {
4463 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4466 v->codingset = CS_HIGH_MOT_INTRA;
4469 v->codingset = CS_MID_RATE_INTRA;
4473 switch (v->c_ac_table_index) {
4475 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4478 v->codingset2 = CS_HIGH_MOT_INTER;
4481 v->codingset2 = CS_MID_RATE_INTER;
4486 s->mb_x = s->mb_y = 0;
4488 s->first_slice_line = 1;
4489 s->mb_y = s->start_mb_y;
4490 if (s->start_mb_y) {
4492 ff_init_block_index(s);
4493 memset(&s->coded_block[s->block_index[0] - s->b8_stride], 0,
4494 (1 + s->b8_stride) * sizeof(*s->coded_block));
4496 for (; s->mb_y < s->end_mb_y; s->mb_y++) {
4498 ff_init_block_index(s);
4499 for (;s->mb_x < s->mb_width; s->mb_x++) {
4500 DCTELEM (*block)[64] = v->block[v->cur_blk_idx];
4501 ff_update_block_index(s);
4502 s->dsp.clear_blocks(block[0]);
4503 mb_pos = s->mb_x + s->mb_y * s->mb_stride;
4504 s->current_picture.f.mb_type[mb_pos + v->mb_off] = MB_TYPE_INTRA;
4505 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
4506 s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
4508 // do actual MB decoding and displaying
4509 if (v->fieldtx_is_raw)
4510 v->fieldtx_plane[mb_pos] = get_bits1(&v->s.gb);
4511 cbp = get_vlc2(&v->s.gb, ff_msmp4_mb_i_vlc.table, MB_INTRA_VLC_BITS, 2);
4512 if ( v->acpred_is_raw)
4513 v->s.ac_pred = get_bits1(&v->s.gb);
4515 v->s.ac_pred = v->acpred_plane[mb_pos];
4517 if (v->condover == CONDOVER_SELECT && v->overflg_is_raw)
4518 v->over_flags_plane[mb_pos] = get_bits1(&v->s.gb);
4522 s->current_picture.f.qscale_table[mb_pos] = mquant;
4523 /* Set DC scale - y and c use the same */
4524 s->y_dc_scale = s->y_dc_scale_table[mquant];
4525 s->c_dc_scale = s->c_dc_scale_table[mquant];
4527 for (k = 0; k < 6; k++) {
4528 val = ((cbp >> (5 - k)) & 1);
4531 int pred = vc1_coded_block_pred(&v->s, k, &coded_val);
4535 cbp |= val << (5 - k);
4537 v->a_avail = !s->first_slice_line || (k == 2 || k == 3);
4538 v->c_avail = !!s->mb_x || (k == 1 || k == 3);
4540 vc1_decode_i_block_adv(v, block[k], k, val,
4541 (k < 4) ? v->codingset : v->codingset2, mquant);
4543 if (k > 3 && (s->flags & CODEC_FLAG_GRAY))
4545 v->vc1dsp.vc1_inv_trans_8x8(block[k]);
4548 vc1_smooth_overlap_filter_iblk(v);
4549 vc1_put_signed_blocks_clamped(v);
4550 if (v->s.loop_filter) vc1_loop_filter_iblk_delayed(v, v->pq);
4552 if (get_bits_count(&s->gb) > v->bits) {
4553 // TODO: may need modification to handle slice coding
4554 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4555 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i\n",
4556 get_bits_count(&s->gb), v->bits);
4560 if (!v->s.loop_filter)
4561 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4563 ff_draw_horiz_band(s, (s->mb_y-1) * 16, 16);
4564 s->first_slice_line = 0;
4567 /* raw bottom MB row */
4569 ff_init_block_index(s);
4570 for (;s->mb_x < s->mb_width; s->mb_x++) {
4571 ff_update_block_index(s);
4572 vc1_put_signed_blocks_clamped(v);
4573 if (v->s.loop_filter)
4574 vc1_loop_filter_iblk_delayed(v, v->pq);
4576 if (v->s.loop_filter)
4577 ff_draw_horiz_band(s, (s->end_mb_y-1)*16, 16);
4578 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4579 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4582 static void vc1_decode_p_blocks(VC1Context *v)
4584 MpegEncContext *s = &v->s;
4585 int apply_loop_filter;
4587 /* select codingmode used for VLC tables selection */
4588 switch (v->c_ac_table_index) {
4590 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4593 v->codingset = CS_HIGH_MOT_INTRA;
4596 v->codingset = CS_MID_RATE_INTRA;
4600 switch (v->c_ac_table_index) {
4602 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4605 v->codingset2 = CS_HIGH_MOT_INTER;
4608 v->codingset2 = CS_MID_RATE_INTER;
4612 apply_loop_filter = s->loop_filter && !(s->avctx->skip_loop_filter >= AVDISCARD_NONKEY);
4613 s->first_slice_line = 1;
4614 memset(v->cbp_base, 0, sizeof(v->cbp_base[0])*2*s->mb_stride);
4615 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4617 ff_init_block_index(s);
4618 for (; s->mb_x < s->mb_width; s->mb_x++) {
4619 ff_update_block_index(s);
4621 if (v->fcm == ILACE_FIELD)
4622 vc1_decode_p_mb_intfi(v);
4623 else if (v->fcm == ILACE_FRAME)
4624 vc1_decode_p_mb_intfr(v);
4625 else vc1_decode_p_mb(v);
4626 if (s->mb_y != s->start_mb_y && apply_loop_filter && v->fcm == PROGRESSIVE)
4627 vc1_apply_p_loop_filter(v);
4628 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4629 // TODO: may need modification to handle slice coding
4630 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4631 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4632 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4636 memmove(v->cbp_base, v->cbp, sizeof(v->cbp_base[0]) * s->mb_stride);
4637 memmove(v->ttblk_base, v->ttblk, sizeof(v->ttblk_base[0]) * s->mb_stride);
4638 memmove(v->is_intra_base, v->is_intra, sizeof(v->is_intra_base[0]) * s->mb_stride);
4639 memmove(v->luma_mv_base, v->luma_mv, sizeof(v->luma_mv_base[0]) * s->mb_stride);
4640 if (s->mb_y != s->start_mb_y) ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4641 s->first_slice_line = 0;
4643 if (apply_loop_filter && v->fcm == PROGRESSIVE) {
4645 ff_init_block_index(s);
4646 for (; s->mb_x < s->mb_width; s->mb_x++) {
4647 ff_update_block_index(s);
4648 vc1_apply_p_loop_filter(v);
4651 if (s->end_mb_y >= s->start_mb_y)
4652 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4653 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4654 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4657 static void vc1_decode_b_blocks(VC1Context *v)
4659 MpegEncContext *s = &v->s;
4661 /* select codingmode used for VLC tables selection */
4662 switch (v->c_ac_table_index) {
4664 v->codingset = (v->pqindex <= 8) ? CS_HIGH_RATE_INTRA : CS_LOW_MOT_INTRA;
4667 v->codingset = CS_HIGH_MOT_INTRA;
4670 v->codingset = CS_MID_RATE_INTRA;
4674 switch (v->c_ac_table_index) {
4676 v->codingset2 = (v->pqindex <= 8) ? CS_HIGH_RATE_INTER : CS_LOW_MOT_INTER;
4679 v->codingset2 = CS_HIGH_MOT_INTER;
4682 v->codingset2 = CS_MID_RATE_INTER;
4686 s->first_slice_line = 1;
4687 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4689 ff_init_block_index(s);
4690 for (; s->mb_x < s->mb_width; s->mb_x++) {
4691 ff_update_block_index(s);
4693 if (v->fcm == ILACE_FIELD)
4694 vc1_decode_b_mb_intfi(v);
4697 if (get_bits_count(&s->gb) > v->bits || get_bits_count(&s->gb) < 0) {
4698 // TODO: may need modification to handle slice coding
4699 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_x, s->mb_y, ER_MB_ERROR);
4700 av_log(s->avctx, AV_LOG_ERROR, "Bits overconsumption: %i > %i at %ix%i\n",
4701 get_bits_count(&s->gb), v->bits, s->mb_x, s->mb_y);
4704 if (v->s.loop_filter) vc1_loop_filter_iblk(v, v->pq);
4706 if (!v->s.loop_filter)
4707 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4709 ff_draw_horiz_band(s, (s->mb_y - 1) * 16, 16);
4710 s->first_slice_line = 0;
4712 if (v->s.loop_filter)
4713 ff_draw_horiz_band(s, (s->end_mb_y - 1) * 16, 16);
4714 ff_er_add_slice(s, 0, s->start_mb_y << v->field_mode, s->mb_width - 1,
4715 (s->end_mb_y << v->field_mode) - 1, ER_MB_END);
4718 static void vc1_decode_skip_blocks(VC1Context *v)
4720 MpegEncContext *s = &v->s;
4722 ff_er_add_slice(s, 0, s->start_mb_y, s->mb_width - 1, s->end_mb_y - 1, ER_MB_END);
4723 s->first_slice_line = 1;
4724 for (s->mb_y = s->start_mb_y; s->mb_y < s->end_mb_y; s->mb_y++) {
4726 ff_init_block_index(s);
4727 ff_update_block_index(s);
4728 memcpy(s->dest[0], s->last_picture.f.data[0] + s->mb_y * 16 * s->linesize, s->linesize * 16);
4729 memcpy(s->dest[1], s->last_picture.f.data[1] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4730 memcpy(s->dest[2], s->last_picture.f.data[2] + s->mb_y * 8 * s->uvlinesize, s->uvlinesize * 8);
4731 ff_draw_horiz_band(s, s->mb_y * 16, 16);
4732 s->first_slice_line = 0;
4734 s->pict_type = AV_PICTURE_TYPE_P;
4737 static void vc1_decode_blocks(VC1Context *v)
4740 v->s.esc3_level_length = 0;
4742 ff_intrax8_decode_picture(&v->x8, 2*v->pq + v->halfpq, v->pq * !v->pquantizer);
4745 v->left_blk_idx = -1;
4746 v->topleft_blk_idx = 1;
4748 switch (v->s.pict_type) {
4749 case AV_PICTURE_TYPE_I:
4750 if (v->profile == PROFILE_ADVANCED)
4751 vc1_decode_i_blocks_adv(v);
4753 vc1_decode_i_blocks(v);
4755 case AV_PICTURE_TYPE_P:
4756 if (v->p_frame_skipped)
4757 vc1_decode_skip_blocks(v);
4759 vc1_decode_p_blocks(v);
4761 case AV_PICTURE_TYPE_B:
4763 if (v->profile == PROFILE_ADVANCED)
4764 vc1_decode_i_blocks_adv(v);
4766 vc1_decode_i_blocks(v);
4768 vc1_decode_b_blocks(v);
4774 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
4778 * Transform coefficients for both sprites in 16.16 fixed point format,
4779 * in the order they appear in the bitstream:
4781 * rotation 1 (unused)
4783 * rotation 2 (unused)
4790 int effect_type, effect_flag;
4791 int effect_pcount1, effect_pcount2; ///< amount of effect parameters stored in effect_params
4792 int effect_params1[15], effect_params2[10]; ///< effect parameters in 16.16 fixed point format
4795 static inline int get_fp_val(GetBitContext* gb)
4797 return (get_bits_long(gb, 30) - (1 << 29)) << 1;
4800 static void vc1_sprite_parse_transform(GetBitContext* gb, int c[7])
4804 switch (get_bits(gb, 2)) {
4807 c[2] = get_fp_val(gb);
4811 c[0] = c[4] = get_fp_val(gb);
4812 c[2] = get_fp_val(gb);
4815 c[0] = get_fp_val(gb);
4816 c[2] = get_fp_val(gb);
4817 c[4] = get_fp_val(gb);
4820 c[0] = get_fp_val(gb);
4821 c[1] = get_fp_val(gb);
4822 c[2] = get_fp_val(gb);
4823 c[3] = get_fp_val(gb);
4824 c[4] = get_fp_val(gb);
4827 c[5] = get_fp_val(gb);
4829 c[6] = get_fp_val(gb);
4834 static void vc1_parse_sprites(VC1Context *v, GetBitContext* gb, SpriteData* sd)
4836 AVCodecContext *avctx = v->s.avctx;
4839 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4840 vc1_sprite_parse_transform(gb, sd->coefs[sprite]);
4841 if (sd->coefs[sprite][1] || sd->coefs[sprite][3])
4842 av_log_ask_for_sample(avctx, "Rotation coefficients are not zero");
4843 av_log(avctx, AV_LOG_DEBUG, sprite ? "S2:" : "S1:");
4844 for (i = 0; i < 7; i++)
4845 av_log(avctx, AV_LOG_DEBUG, " %d.%.3d",
4846 sd->coefs[sprite][i] / (1<<16),
4847 (abs(sd->coefs[sprite][i]) & 0xFFFF) * 1000 / (1 << 16));
4848 av_log(avctx, AV_LOG_DEBUG, "\n");
4852 if (sd->effect_type = get_bits_long(gb, 30)) {
4853 switch (sd->effect_pcount1 = get_bits(gb, 4)) {
4855 vc1_sprite_parse_transform(gb, sd->effect_params1);
4858 vc1_sprite_parse_transform(gb, sd->effect_params1);
4859 vc1_sprite_parse_transform(gb, sd->effect_params1 + 7);
4862 for (i = 0; i < sd->effect_pcount1; i++)
4863 sd->effect_params1[i] = get_fp_val(gb);
4865 if (sd->effect_type != 13 || sd->effect_params1[0] != sd->coefs[0][6]) {
4866 // effect 13 is simple alpha blending and matches the opacity above
4867 av_log(avctx, AV_LOG_DEBUG, "Effect: %d; params: ", sd->effect_type);
4868 for (i = 0; i < sd->effect_pcount1; i++)
4869 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4870 sd->effect_params1[i] / (1 << 16),
4871 (abs(sd->effect_params1[i]) & 0xFFFF) * 1000 / (1 << 16));
4872 av_log(avctx, AV_LOG_DEBUG, "\n");
4875 sd->effect_pcount2 = get_bits(gb, 16);
4876 if (sd->effect_pcount2 > 10) {
4877 av_log(avctx, AV_LOG_ERROR, "Too many effect parameters\n");
4879 } else if (sd->effect_pcount2) {
4881 av_log(avctx, AV_LOG_DEBUG, "Effect params 2: ");
4882 while (++i < sd->effect_pcount2) {
4883 sd->effect_params2[i] = get_fp_val(gb);
4884 av_log(avctx, AV_LOG_DEBUG, " %d.%.2d",
4885 sd->effect_params2[i] / (1 << 16),
4886 (abs(sd->effect_params2[i]) & 0xFFFF) * 1000 / (1 << 16));
4888 av_log(avctx, AV_LOG_DEBUG, "\n");
4891 if (sd->effect_flag = get_bits1(gb))
4892 av_log(avctx, AV_LOG_DEBUG, "Effect flag set\n");
4894 if (get_bits_count(gb) >= gb->size_in_bits +
4895 (avctx->codec_id == CODEC_ID_WMV3IMAGE ? 64 : 0))
4896 av_log(avctx, AV_LOG_ERROR, "Buffer overrun\n");
4897 if (get_bits_count(gb) < gb->size_in_bits - 8)
4898 av_log(avctx, AV_LOG_WARNING, "Buffer not fully read\n");
4901 static void vc1_draw_sprites(VC1Context *v, SpriteData* sd)
4903 int i, plane, row, sprite;
4904 int sr_cache[2][2] = { { -1, -1 }, { -1, -1 } };
4905 uint8_t* src_h[2][2];
4906 int xoff[2], xadv[2], yoff[2], yadv[2], alpha;
4908 MpegEncContext *s = &v->s;
4910 for (i = 0; i < 2; i++) {
4911 xoff[i] = av_clip(sd->coefs[i][2], 0, v->sprite_width-1 << 16);
4912 xadv[i] = sd->coefs[i][0];
4913 if (xadv[i] != 1<<16 || (v->sprite_width << 16) - (v->output_width << 16) - xoff[i])
4914 xadv[i] = av_clip(xadv[i], 0, ((v->sprite_width<<16) - xoff[i] - 1) / v->output_width);
4916 yoff[i] = av_clip(sd->coefs[i][5], 0, v->sprite_height-1 << 16);
4917 yadv[i] = av_clip(sd->coefs[i][4], 0, ((v->sprite_height << 16) - yoff[i]) / v->output_height);
4919 alpha = av_clip(sd->coefs[1][6], 0, (1<<16) - 1);
4921 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++) {
4922 int width = v->output_width>>!!plane;
4924 for (row = 0; row < v->output_height>>!!plane; row++) {
4925 uint8_t *dst = v->sprite_output_frame.data[plane] +
4926 v->sprite_output_frame.linesize[plane] * row;
4928 for (sprite = 0; sprite <= v->two_sprites; sprite++) {
4929 uint8_t *iplane = s->current_picture.f.data[plane];
4930 int iline = s->current_picture.f.linesize[plane];
4931 int ycoord = yoff[sprite] + yadv[sprite] * row;
4932 int yline = ycoord >> 16;
4934 ysub[sprite] = ycoord & 0xFFFF;
4936 iplane = s->last_picture.f.data[plane];
4937 iline = s->last_picture.f.linesize[plane];
4939 next_line = FFMIN(yline + 1, (v->sprite_height >> !!plane) - 1) * iline;
4940 if (!(xoff[sprite] & 0xFFFF) && xadv[sprite] == 1 << 16) {
4941 src_h[sprite][0] = iplane + (xoff[sprite] >> 16) + yline * iline;
4943 src_h[sprite][1] = iplane + (xoff[sprite] >> 16) + next_line;
4945 if (sr_cache[sprite][0] != yline) {
4946 if (sr_cache[sprite][1] == yline) {
4947 FFSWAP(uint8_t*, v->sr_rows[sprite][0], v->sr_rows[sprite][1]);
4948 FFSWAP(int, sr_cache[sprite][0], sr_cache[sprite][1]);
4950 v->vc1dsp.sprite_h(v->sr_rows[sprite][0], iplane + yline * iline, xoff[sprite], xadv[sprite], width);
4951 sr_cache[sprite][0] = yline;
4954 if (ysub[sprite] && sr_cache[sprite][1] != yline + 1) {
4955 v->vc1dsp.sprite_h(v->sr_rows[sprite][1],
4956 iplane + next_line, xoff[sprite],
4957 xadv[sprite], width);
4958 sr_cache[sprite][1] = yline + 1;
4960 src_h[sprite][0] = v->sr_rows[sprite][0];
4961 src_h[sprite][1] = v->sr_rows[sprite][1];
4965 if (!v->two_sprites) {
4967 v->vc1dsp.sprite_v_single(dst, src_h[0][0], src_h[0][1], ysub[0], width);
4969 memcpy(dst, src_h[0][0], width);
4972 if (ysub[0] && ysub[1]) {
4973 v->vc1dsp.sprite_v_double_twoscale(dst, src_h[0][0], src_h[0][1], ysub[0],
4974 src_h[1][0], src_h[1][1], ysub[1], alpha, width);
4975 } else if (ysub[0]) {
4976 v->vc1dsp.sprite_v_double_onescale(dst, src_h[0][0], src_h[0][1], ysub[0],
4977 src_h[1][0], alpha, width);
4978 } else if (ysub[1]) {
4979 v->vc1dsp.sprite_v_double_onescale(dst, src_h[1][0], src_h[1][1], ysub[1],
4980 src_h[0][0], (1<<16)-1-alpha, width);
4982 v->vc1dsp.sprite_v_double_noscale(dst, src_h[0][0], src_h[1][0], alpha, width);
4988 for (i = 0; i < 2; i++) {
4998 static int vc1_decode_sprites(VC1Context *v, GetBitContext* gb)
5000 MpegEncContext *s = &v->s;
5001 AVCodecContext *avctx = s->avctx;
5004 vc1_parse_sprites(v, gb, &sd);
5006 if (!s->current_picture.f.data[0]) {
5007 av_log(avctx, AV_LOG_ERROR, "Got no sprites\n");
5011 if (v->two_sprites && (!s->last_picture_ptr || !s->last_picture.f.data[0])) {
5012 av_log(avctx, AV_LOG_WARNING, "Need two sprites, only got one\n");
5016 if (v->sprite_output_frame.data[0])
5017 avctx->release_buffer(avctx, &v->sprite_output_frame);
5019 v->sprite_output_frame.buffer_hints = FF_BUFFER_HINTS_VALID;
5020 v->sprite_output_frame.reference = 0;
5021 if (avctx->get_buffer(avctx, &v->sprite_output_frame) < 0) {
5022 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
5026 vc1_draw_sprites(v, &sd);
5031 static void vc1_sprite_flush(AVCodecContext *avctx)
5033 VC1Context *v = avctx->priv_data;
5034 MpegEncContext *s = &v->s;
5035 AVFrame *f = &s->current_picture.f;
5038 /* Windows Media Image codecs have a convergence interval of two keyframes.
5039 Since we can't enforce it, clear to black the missing sprite. This is
5040 wrong but it looks better than doing nothing. */
5043 for (plane = 0; plane < (s->flags&CODEC_FLAG_GRAY ? 1 : 3); plane++)
5044 for (i = 0; i < v->sprite_height>>!!plane; i++)
5045 memset(f->data[plane] + i * f->linesize[plane],
5046 plane ? 128 : 0, f->linesize[plane]);
5051 static av_cold int vc1_decode_init_alloc_tables(VC1Context *v)
5053 MpegEncContext *s = &v->s;
5056 /* Allocate mb bitplanes */
5057 v->mv_type_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5058 v->direct_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5059 v->forward_mb_plane = av_malloc (s->mb_stride * s->mb_height);
5060 v->fieldtx_plane = av_mallocz(s->mb_stride * s->mb_height);
5061 v->acpred_plane = av_malloc (s->mb_stride * s->mb_height);
5062 v->over_flags_plane = av_malloc (s->mb_stride * s->mb_height);
5064 v->n_allocated_blks = s->mb_width + 2;
5065 v->block = av_malloc(sizeof(*v->block) * v->n_allocated_blks);
5066 v->cbp_base = av_malloc(sizeof(v->cbp_base[0]) * 2 * s->mb_stride);
5067 v->cbp = v->cbp_base + s->mb_stride;
5068 v->ttblk_base = av_malloc(sizeof(v->ttblk_base[0]) * 2 * s->mb_stride);
5069 v->ttblk = v->ttblk_base + s->mb_stride;
5070 v->is_intra_base = av_mallocz(sizeof(v->is_intra_base[0]) * 2 * s->mb_stride);
5071 v->is_intra = v->is_intra_base + s->mb_stride;
5072 v->luma_mv_base = av_malloc(sizeof(v->luma_mv_base[0]) * 2 * s->mb_stride);
5073 v->luma_mv = v->luma_mv_base + s->mb_stride;
5075 /* allocate block type info in that way so it could be used with s->block_index[] */
5076 v->mb_type_base = av_malloc(s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5077 v->mb_type[0] = v->mb_type_base + s->b8_stride + 1;
5078 v->mb_type[1] = v->mb_type_base + s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride + 1;
5079 v->mb_type[2] = v->mb_type[1] + s->mb_stride * (s->mb_height + 1);
5081 /* allocate memory to store block level MV info */
5082 v->blk_mv_type_base = av_mallocz( s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5083 v->blk_mv_type = v->blk_mv_type_base + s->b8_stride + 1;
5084 v->mv_f_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5085 v->mv_f[0] = v->mv_f_base + s->b8_stride + 1;
5086 v->mv_f[1] = v->mv_f[0] + (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2);
5087 v->mv_f_last_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5088 v->mv_f_last[0] = v->mv_f_last_base + s->b8_stride + 1;
5089 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);
5090 v->mv_f_next_base = av_mallocz(2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5091 v->mv_f_next[0] = v->mv_f_next_base + s->b8_stride + 1;
5092 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);
5094 /* Init coded blocks info */
5095 if (v->profile == PROFILE_ADVANCED) {
5096 // if (alloc_bitplane(&v->over_flags_plane, s->mb_width, s->mb_height) < 0)
5098 // if (alloc_bitplane(&v->ac_pred_plane, s->mb_width, s->mb_height) < 0)
5102 ff_intrax8_common_init(&v->x8,s);
5104 if (s->avctx->codec_id == CODEC_ID_WMV3IMAGE || s->avctx->codec_id == CODEC_ID_VC1IMAGE) {
5105 for (i = 0; i < 4; i++)
5106 if (!(v->sr_rows[i >> 1][i & 1] = av_malloc(v->output_width))) return -1;
5109 if (!v->mv_type_mb_plane || !v->direct_mb_plane || !v->acpred_plane || !v->over_flags_plane ||
5110 !v->block || !v->cbp_base || !v->ttblk_base || !v->is_intra_base || !v->luma_mv_base ||
5117 /** Initialize a VC1/WMV3 decoder
5118 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5119 * @todo TODO: Decypher remaining bits in extra_data
5121 static av_cold int vc1_decode_init(AVCodecContext *avctx)
5123 VC1Context *v = avctx->priv_data;
5124 MpegEncContext *s = &v->s;
5128 /* save the container output size for WMImage */
5129 v->output_width = avctx->width;
5130 v->output_height = avctx->height;
5132 if (!avctx->extradata_size || !avctx->extradata)
5134 if (!(avctx->flags & CODEC_FLAG_GRAY))
5135 avctx->pix_fmt = avctx->get_format(avctx, avctx->codec->pix_fmts);
5137 avctx->pix_fmt = PIX_FMT_GRAY8;
5138 avctx->hwaccel = ff_find_hwaccel(avctx->codec->id, avctx->pix_fmt);
5140 avctx->flags |= CODEC_FLAG_EMU_EDGE;
5141 v->s.flags |= CODEC_FLAG_EMU_EDGE;
5143 if (avctx->idct_algo == FF_IDCT_AUTO) {
5144 avctx->idct_algo = FF_IDCT_WMV2;
5147 if (ff_vc1_init_common(v) < 0)
5149 ff_vc1dsp_init(&v->vc1dsp);
5151 if (avctx->codec_id == CODEC_ID_WMV3 || avctx->codec_id == CODEC_ID_WMV3IMAGE) {
5154 // looks like WMV3 has a sequence header stored in the extradata
5155 // advanced sequence header may be before the first frame
5156 // the last byte of the extradata is a version number, 1 for the
5157 // samples we can decode
5159 init_get_bits(&gb, avctx->extradata, avctx->extradata_size*8);
5161 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0)
5164 count = avctx->extradata_size*8 - get_bits_count(&gb);
5166 av_log(avctx, AV_LOG_INFO, "Extra data: %i bits left, value: %X\n",
5167 count, get_bits(&gb, count));
5168 } else if (count < 0) {
5169 av_log(avctx, AV_LOG_INFO, "Read %i bits in overflow\n", -count);
5171 } else { // VC1/WVC1/WVP2
5172 const uint8_t *start = avctx->extradata;
5173 uint8_t *end = avctx->extradata + avctx->extradata_size;
5174 const uint8_t *next;
5175 int size, buf2_size;
5176 uint8_t *buf2 = NULL;
5177 int seq_initialized = 0, ep_initialized = 0;
5179 if (avctx->extradata_size < 16) {
5180 av_log(avctx, AV_LOG_ERROR, "Extradata size too small: %i\n", avctx->extradata_size);
5184 buf2 = av_mallocz(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
5185 start = find_next_marker(start, end); // in WVC1 extradata first byte is its size, but can be 0 in mkv
5187 for (; next < end; start = next) {
5188 next = find_next_marker(start + 4, end);
5189 size = next - start - 4;
5192 buf2_size = vc1_unescape_buffer(start + 4, size, buf2);
5193 init_get_bits(&gb, buf2, buf2_size * 8);
5194 switch (AV_RB32(start)) {
5195 case VC1_CODE_SEQHDR:
5196 if (ff_vc1_decode_sequence_header(avctx, v, &gb) < 0) {
5200 seq_initialized = 1;
5202 case VC1_CODE_ENTRYPOINT:
5203 if (ff_vc1_decode_entry_point(avctx, v, &gb) < 0) {
5212 if (!seq_initialized || !ep_initialized) {
5213 av_log(avctx, AV_LOG_ERROR, "Incomplete extradata\n");
5216 v->res_sprite = (avctx->codec_tag == MKTAG('W','V','P','2'));
5219 avctx->profile = v->profile;
5220 if (v->profile == PROFILE_ADVANCED)
5221 avctx->level = v->level;
5223 avctx->has_b_frames = !!avctx->max_b_frames;
5225 s->mb_width = (avctx->coded_width + 15) >> 4;
5226 s->mb_height = (avctx->coded_height + 15) >> 4;
5228 if (v->profile == PROFILE_ADVANCED || v->res_fasttx) {
5229 for (i = 0; i < 64; i++) {
5230 #define transpose(x) ((x >> 3) | ((x & 7) << 3))
5231 v->zz_8x8[0][i] = transpose(ff_wmv1_scantable[0][i]);
5232 v->zz_8x8[1][i] = transpose(ff_wmv1_scantable[1][i]);
5233 v->zz_8x8[2][i] = transpose(ff_wmv1_scantable[2][i]);
5234 v->zz_8x8[3][i] = transpose(ff_wmv1_scantable[3][i]);
5235 v->zzi_8x8[i] = transpose(ff_vc1_adv_interlaced_8x8_zz[i]);
5240 memcpy(v->zz_8x8, ff_wmv1_scantable, 4*64);
5245 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5246 v->sprite_width = avctx->coded_width;
5247 v->sprite_height = avctx->coded_height;
5249 avctx->coded_width = avctx->width = v->output_width;
5250 avctx->coded_height = avctx->height = v->output_height;
5252 // prevent 16.16 overflows
5253 if (v->sprite_width > 1 << 14 ||
5254 v->sprite_height > 1 << 14 ||
5255 v->output_width > 1 << 14 ||
5256 v->output_height > 1 << 14) return -1;
5261 /** Close a VC1/WMV3 decoder
5262 * @warning Initial try at using MpegEncContext stuff
5264 static av_cold int vc1_decode_end(AVCodecContext *avctx)
5266 VC1Context *v = avctx->priv_data;
5269 if ((avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE)
5270 && v->sprite_output_frame.data[0])
5271 avctx->release_buffer(avctx, &v->sprite_output_frame);
5272 for (i = 0; i < 4; i++)
5273 av_freep(&v->sr_rows[i >> 1][i & 1]);
5274 av_freep(&v->hrd_rate);
5275 av_freep(&v->hrd_buffer);
5276 ff_MPV_common_end(&v->s);
5277 av_freep(&v->mv_type_mb_plane);
5278 av_freep(&v->direct_mb_plane);
5279 av_freep(&v->forward_mb_plane);
5280 av_freep(&v->fieldtx_plane);
5281 av_freep(&v->acpred_plane);
5282 av_freep(&v->over_flags_plane);
5283 av_freep(&v->mb_type_base);
5284 av_freep(&v->blk_mv_type_base);
5285 av_freep(&v->mv_f_base);
5286 av_freep(&v->mv_f_last_base);
5287 av_freep(&v->mv_f_next_base);
5288 av_freep(&v->block);
5289 av_freep(&v->cbp_base);
5290 av_freep(&v->ttblk_base);
5291 av_freep(&v->is_intra_base); // FIXME use v->mb_type[]
5292 av_freep(&v->luma_mv_base);
5293 ff_intrax8_common_end(&v->x8);
5298 /** Decode a VC1/WMV3 frame
5299 * @todo TODO: Handle VC-1 IDUs (Transport level?)
5301 static int vc1_decode_frame(AVCodecContext *avctx, void *data,
5302 int *data_size, AVPacket *avpkt)
5304 const uint8_t *buf = avpkt->data;
5305 int buf_size = avpkt->size, n_slices = 0, i;
5306 VC1Context *v = avctx->priv_data;
5307 MpegEncContext *s = &v->s;
5308 AVFrame *pict = data;
5309 uint8_t *buf2 = NULL;
5310 const uint8_t *buf_start = buf;
5311 int mb_height, n_slices1=-1;
5316 } *slices = NULL, *tmp;
5318 if(s->flags & CODEC_FLAG_LOW_DELAY)
5321 /* no supplementary picture */
5322 if (buf_size == 0 || (buf_size == 4 && AV_RB32(buf) == VC1_CODE_ENDOFSEQ)) {
5323 /* special case for last picture */
5324 if (s->low_delay == 0 && s->next_picture_ptr) {
5325 *pict = s->next_picture_ptr->f;
5326 s->next_picture_ptr = NULL;
5328 *data_size = sizeof(AVFrame);
5334 if (s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU) {
5335 if (v->profile < PROFILE_ADVANCED)
5336 avctx->pix_fmt = PIX_FMT_VDPAU_WMV3;
5338 avctx->pix_fmt = PIX_FMT_VDPAU_VC1;
5341 //for advanced profile we may need to parse and unescape data
5342 if (avctx->codec_id == CODEC_ID_VC1 || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5344 buf2 = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5346 if (IS_MARKER(AV_RB32(buf))) { /* frame starts with marker and needs to be parsed */
5347 const uint8_t *start, *end, *next;
5351 for (start = buf, end = buf + buf_size; next < end; start = next) {
5352 next = find_next_marker(start + 4, end);
5353 size = next - start - 4;
5354 if (size <= 0) continue;
5355 switch (AV_RB32(start)) {
5356 case VC1_CODE_FRAME:
5357 if (avctx->hwaccel ||
5358 s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5360 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5362 case VC1_CODE_FIELD: {
5364 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5367 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5368 if (!slices[n_slices].buf)
5370 buf_size3 = vc1_unescape_buffer(start + 4, size,
5371 slices[n_slices].buf);
5372 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5374 /* assuming that the field marker is at the exact middle,
5375 hope it's correct */
5376 slices[n_slices].mby_start = s->mb_height >> 1;
5377 n_slices1 = n_slices - 1; // index of the last slice of the first field
5381 case VC1_CODE_ENTRYPOINT: /* it should be before frame data */
5382 buf_size2 = vc1_unescape_buffer(start + 4, size, buf2);
5383 init_get_bits(&s->gb, buf2, buf_size2 * 8);
5384 ff_vc1_decode_entry_point(avctx, v, &s->gb);
5386 case VC1_CODE_SLICE: {
5388 slices = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5391 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5392 if (!slices[n_slices].buf)
5394 buf_size3 = vc1_unescape_buffer(start + 4, size,
5395 slices[n_slices].buf);
5396 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5398 slices[n_slices].mby_start = get_bits(&slices[n_slices].gb, 9);
5404 } else if (v->interlace && ((buf[0] & 0xC0) == 0xC0)) { /* WVC1 interlaced stores both fields divided by marker */
5405 const uint8_t *divider;
5408 divider = find_next_marker(buf, buf + buf_size);
5409 if ((divider == (buf + buf_size)) || AV_RB32(divider) != VC1_CODE_FIELD) {
5410 av_log(avctx, AV_LOG_ERROR, "Error in WVC1 interlaced frame\n");
5412 } else { // found field marker, unescape second field
5413 tmp = av_realloc(slices, sizeof(*slices) * (n_slices+1));
5417 slices[n_slices].buf = av_mallocz(buf_size + FF_INPUT_BUFFER_PADDING_SIZE);
5418 if (!slices[n_slices].buf)
5420 buf_size3 = vc1_unescape_buffer(divider + 4, buf + buf_size - divider - 4, slices[n_slices].buf);
5421 init_get_bits(&slices[n_slices].gb, slices[n_slices].buf,
5423 slices[n_slices].mby_start = s->mb_height >> 1;
5424 n_slices1 = n_slices - 1;
5427 buf_size2 = vc1_unescape_buffer(buf, divider - buf, buf2);
5429 buf_size2 = vc1_unescape_buffer(buf, buf_size, buf2);
5431 init_get_bits(&s->gb, buf2, buf_size2*8);
5433 init_get_bits(&s->gb, buf, buf_size*8);
5435 if (v->res_sprite) {
5436 v->new_sprite = !get_bits1(&s->gb);
5437 v->two_sprites = get_bits1(&s->gb);
5438 /* res_sprite means a Windows Media Image stream, CODEC_ID_*IMAGE means
5439 we're using the sprite compositor. These are intentionally kept separate
5440 so you can get the raw sprites by using the wmv3 decoder for WMVP or
5441 the vc1 one for WVP2 */
5442 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5443 if (v->new_sprite) {
5444 // switch AVCodecContext parameters to those of the sprites
5445 avctx->width = avctx->coded_width = v->sprite_width;
5446 avctx->height = avctx->coded_height = v->sprite_height;
5453 if (s->context_initialized &&
5454 (s->width != avctx->coded_width ||
5455 s->height != avctx->coded_height)) {
5456 vc1_decode_end(avctx);
5459 if (!s->context_initialized) {
5460 if (ff_msmpeg4_decode_init(avctx) < 0 || vc1_decode_init_alloc_tables(v) < 0)
5463 s->low_delay = !avctx->has_b_frames || v->res_sprite;
5465 if (v->profile == PROFILE_ADVANCED) {
5466 s->h_edge_pos = avctx->coded_width;
5467 s->v_edge_pos = avctx->coded_height;
5471 /* We need to set current_picture_ptr before reading the header,
5472 * otherwise we cannot store anything in there. */
5473 if (s->current_picture_ptr == NULL || s->current_picture_ptr->f.data[0]) {
5474 int i = ff_find_unused_picture(s, 0);
5477 s->current_picture_ptr = &s->picture[i];
5480 // do parse frame header
5481 v->pic_header_flag = 0;
5482 if (v->profile < PROFILE_ADVANCED) {
5483 if (ff_vc1_parse_frame_header(v, &s->gb) < 0) {
5487 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5492 if ((avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE)
5493 && s->pict_type != AV_PICTURE_TYPE_I) {
5494 av_log(v->s.avctx, AV_LOG_ERROR, "Sprite decoder: expected I-frame\n");
5498 // process pulldown flags
5499 s->current_picture_ptr->f.repeat_pict = 0;
5500 // Pulldown flags are only valid when 'broadcast' has been set.
5501 // So ticks_per_frame will be 2
5504 s->current_picture_ptr->f.repeat_pict = 1;
5505 } else if (v->rptfrm) {
5507 s->current_picture_ptr->f.repeat_pict = v->rptfrm * 2;
5510 // for skipping the frame
5511 s->current_picture.f.pict_type = s->pict_type;
5512 s->current_picture.f.key_frame = s->pict_type == AV_PICTURE_TYPE_I;
5514 /* skip B-frames if we don't have reference frames */
5515 if (s->last_picture_ptr == NULL && (s->pict_type == AV_PICTURE_TYPE_B || s->dropable)) {
5518 if ((avctx->skip_frame >= AVDISCARD_NONREF && s->pict_type == AV_PICTURE_TYPE_B) ||
5519 (avctx->skip_frame >= AVDISCARD_NONKEY && s->pict_type != AV_PICTURE_TYPE_I) ||
5520 avctx->skip_frame >= AVDISCARD_ALL) {
5524 if (s->next_p_frame_damaged) {
5525 if (s->pict_type == AV_PICTURE_TYPE_B)
5528 s->next_p_frame_damaged = 0;
5531 if (ff_MPV_frame_start(s, avctx) < 0) {
5535 v->s.current_picture_ptr->f.interlaced_frame = (v->fcm != PROGRESSIVE);
5536 v->s.current_picture_ptr->f.top_field_first = v->tff;
5538 s->me.qpel_put = s->dsp.put_qpel_pixels_tab;
5539 s->me.qpel_avg = s->dsp.avg_qpel_pixels_tab;
5541 if ((CONFIG_VC1_VDPAU_DECODER)
5542 &&s->avctx->codec->capabilities&CODEC_CAP_HWACCEL_VDPAU)
5543 ff_vdpau_vc1_decode_picture(s, buf_start, (buf + buf_size) - buf_start);
5544 else if (avctx->hwaccel) {
5545 if (avctx->hwaccel->start_frame(avctx, buf, buf_size) < 0)
5547 if (avctx->hwaccel->decode_slice(avctx, buf_start, (buf + buf_size) - buf_start) < 0)
5549 if (avctx->hwaccel->end_frame(avctx) < 0)
5552 ff_er_frame_start(s);
5554 v->bits = buf_size * 8;
5555 if (v->field_mode) {
5557 s->current_picture.f.linesize[0] <<= 1;
5558 s->current_picture.f.linesize[1] <<= 1;
5559 s->current_picture.f.linesize[2] <<= 1;
5561 s->uvlinesize <<= 1;
5562 tmp[0] = v->mv_f_last[0];
5563 tmp[1] = v->mv_f_last[1];
5564 v->mv_f_last[0] = v->mv_f_next[0];
5565 v->mv_f_last[1] = v->mv_f_next[1];
5566 v->mv_f_next[0] = v->mv_f[0];
5567 v->mv_f_next[1] = v->mv_f[1];
5568 v->mv_f[0] = tmp[0];
5569 v->mv_f[1] = tmp[1];
5571 mb_height = s->mb_height >> v->field_mode;
5572 for (i = 0; i <= n_slices; i++) {
5573 if (i > 0 && slices[i - 1].mby_start >= mb_height) {
5574 if (v->field_mode <= 0) {
5575 av_log(v->s.avctx, AV_LOG_ERROR, "Slice %d starts beyond "
5576 "picture boundary (%d >= %d)\n", i,
5577 slices[i - 1].mby_start, mb_height);
5580 v->second_field = 1;
5581 v->blocks_off = s->mb_width * s->mb_height << 1;
5582 v->mb_off = s->mb_stride * s->mb_height >> 1;
5584 v->second_field = 0;
5589 v->pic_header_flag = 0;
5590 if (v->field_mode && i == n_slices1 + 2) {
5591 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5592 av_log(v->s.avctx, AV_LOG_ERROR, "Field header damaged\n");
5595 } else if (get_bits1(&s->gb)) {
5596 v->pic_header_flag = 1;
5597 if (ff_vc1_parse_frame_header_adv(v, &s->gb) < 0) {
5598 av_log(v->s.avctx, AV_LOG_ERROR, "Slice header damaged\n");
5603 s->start_mb_y = (i == 0) ? 0 : FFMAX(0, slices[i-1].mby_start % mb_height);
5604 if (!v->field_mode || v->second_field)
5605 s->end_mb_y = (i == n_slices ) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5607 s->end_mb_y = (i <= n_slices1 + 1) ? mb_height : FFMIN(mb_height, slices[i].mby_start % mb_height);
5608 if (s->end_mb_y <= s->start_mb_y) {
5609 av_log(v->s.avctx, AV_LOG_ERROR, "end mb y %d %d invalid\n", s->end_mb_y, s->start_mb_y);
5612 vc1_decode_blocks(v);
5614 s->gb = slices[i].gb;
5616 if (v->field_mode) {
5617 v->second_field = 0;
5618 if (s->pict_type == AV_PICTURE_TYPE_B) {
5619 memcpy(v->mv_f_base, v->mv_f_next_base,
5620 2 * (s->b8_stride * (s->mb_height * 2 + 1) + s->mb_stride * (s->mb_height + 1) * 2));
5622 s->current_picture.f.linesize[0] >>= 1;
5623 s->current_picture.f.linesize[1] >>= 1;
5624 s->current_picture.f.linesize[2] >>= 1;
5626 s->uvlinesize >>= 1;
5628 //av_log(s->avctx, AV_LOG_INFO, "Consumed %i/%i bits\n", get_bits_count(&s->gb), s->gb.size_in_bits);
5629 // if (get_bits_count(&s->gb) > buf_size * 8)
5631 if(s->error_occurred && s->pict_type == AV_PICTURE_TYPE_B)
5637 ff_MPV_frame_end(s);
5639 if (avctx->codec_id == CODEC_ID_WMV3IMAGE || avctx->codec_id == CODEC_ID_VC1IMAGE) {
5641 avctx->width = avctx->coded_width = v->output_width;
5642 avctx->height = avctx->coded_height = v->output_height;
5643 if (avctx->skip_frame >= AVDISCARD_NONREF)
5645 #if CONFIG_WMV3IMAGE_DECODER || CONFIG_VC1IMAGE_DECODER
5646 if (vc1_decode_sprites(v, &s->gb))
5649 *pict = v->sprite_output_frame;
5650 *data_size = sizeof(AVFrame);
5652 if (s->pict_type == AV_PICTURE_TYPE_B || s->low_delay) {
5653 *pict = s->current_picture_ptr->f;
5654 } else if (s->last_picture_ptr != NULL) {
5655 *pict = s->last_picture_ptr->f;
5657 if (s->last_picture_ptr || s->low_delay) {
5658 *data_size = sizeof(AVFrame);
5659 ff_print_debug_info(s, pict);
5665 for (i = 0; i < n_slices; i++)
5666 av_free(slices[i].buf);
5672 for (i = 0; i < n_slices; i++)
5673 av_free(slices[i].buf);
5679 static const AVProfile profiles[] = {
5680 { FF_PROFILE_VC1_SIMPLE, "Simple" },
5681 { FF_PROFILE_VC1_MAIN, "Main" },
5682 { FF_PROFILE_VC1_COMPLEX, "Complex" },
5683 { FF_PROFILE_VC1_ADVANCED, "Advanced" },
5684 { FF_PROFILE_UNKNOWN },
5687 AVCodec ff_vc1_decoder = {
5689 .type = AVMEDIA_TYPE_VIDEO,
5691 .priv_data_size = sizeof(VC1Context),
5692 .init = vc1_decode_init,
5693 .close = vc1_decode_end,
5694 .decode = vc1_decode_frame,
5695 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5696 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1"),
5697 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5698 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5701 #if CONFIG_WMV3_DECODER
5702 AVCodec ff_wmv3_decoder = {
5704 .type = AVMEDIA_TYPE_VIDEO,
5705 .id = CODEC_ID_WMV3,
5706 .priv_data_size = sizeof(VC1Context),
5707 .init = vc1_decode_init,
5708 .close = vc1_decode_end,
5709 .decode = vc1_decode_frame,
5710 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY,
5711 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9"),
5712 .pix_fmts = ff_hwaccel_pixfmt_list_420,
5713 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5717 #if CONFIG_WMV3_VDPAU_DECODER
5718 AVCodec ff_wmv3_vdpau_decoder = {
5719 .name = "wmv3_vdpau",
5720 .type = AVMEDIA_TYPE_VIDEO,
5721 .id = CODEC_ID_WMV3,
5722 .priv_data_size = sizeof(VC1Context),
5723 .init = vc1_decode_init,
5724 .close = vc1_decode_end,
5725 .decode = vc1_decode_frame,
5726 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5727 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 VDPAU"),
5728 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_WMV3, PIX_FMT_NONE },
5729 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5733 #if CONFIG_VC1_VDPAU_DECODER
5734 AVCodec ff_vc1_vdpau_decoder = {
5735 .name = "vc1_vdpau",
5736 .type = AVMEDIA_TYPE_VIDEO,
5738 .priv_data_size = sizeof(VC1Context),
5739 .init = vc1_decode_init,
5740 .close = vc1_decode_end,
5741 .decode = vc1_decode_frame,
5742 .capabilities = CODEC_CAP_DR1 | CODEC_CAP_DELAY | CODEC_CAP_HWACCEL_VDPAU,
5743 .long_name = NULL_IF_CONFIG_SMALL("SMPTE VC-1 VDPAU"),
5744 .pix_fmts = (const enum PixelFormat[]){ PIX_FMT_VDPAU_VC1, PIX_FMT_NONE },
5745 .profiles = NULL_IF_CONFIG_SMALL(profiles)
5749 #if CONFIG_WMV3IMAGE_DECODER
5750 AVCodec ff_wmv3image_decoder = {
5751 .name = "wmv3image",
5752 .type = AVMEDIA_TYPE_VIDEO,
5753 .id = CODEC_ID_WMV3IMAGE,
5754 .priv_data_size = sizeof(VC1Context),
5755 .init = vc1_decode_init,
5756 .close = vc1_decode_end,
5757 .decode = vc1_decode_frame,
5758 .capabilities = CODEC_CAP_DR1,
5759 .flush = vc1_sprite_flush,
5760 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image"),
5761 .pix_fmts = ff_pixfmt_list_420
5765 #if CONFIG_VC1IMAGE_DECODER
5766 AVCodec ff_vc1image_decoder = {
5768 .type = AVMEDIA_TYPE_VIDEO,
5769 .id = CODEC_ID_VC1IMAGE,
5770 .priv_data_size = sizeof(VC1Context),
5771 .init = vc1_decode_init,
5772 .close = vc1_decode_end,
5773 .decode = vc1_decode_frame,
5774 .capabilities = CODEC_CAP_DR1,
5775 .flush = vc1_sprite_flush,
5776 .long_name = NULL_IF_CONFIG_SMALL("Windows Media Video 9 Image v2"),
5777 .pix_fmts = ff_pixfmt_list_420