/lib/ffmpeg/libavcodec/vc1dec.c
C | 5908 lines | 5091 code | 462 blank | 355 comment | 1918 complexity | 268d2bc16967f1c51a395e9e3e5266dd MD5 | raw file
Possible License(s): GPL-3.0, CC-BY-SA-3.0, LGPL-2.0, 0BSD, Unlicense, GPL-2.0, AGPL-1.0, BSD-3-Clause, LGPL-2.1, LGPL-3.0
Large files files are truncated, but you can click here to view the full file
- /*
- * VC-1 and WMV3 decoder
- * Copyright (c) 2011 Mashiat Sarker Shakkhar
- * Copyright (c) 2006-2007 Konstantin Shishkov
- * Partly based on vc9.c (c) 2005 Anonymous, Alex Beregszaszi, Michael Niedermayer
- *
- * This file is part of FFmpeg.
- *
- * FFmpeg is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 2.1 of the License, or (at your option) any later version.
- *
- * FFmpeg is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public
- * License along with FFmpeg; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
- /**
- * @file
- * VC-1 and WMV3 decoder
- */
- #include "internal.h"
- #include "avcodec.h"
- #include "mpegvideo.h"
- #include "h263.h"
- #include "h264chroma.h"
- #include "vc1.h"
- #include "vc1data.h"
- #include "vc1acdata.h"
- #include "msmpeg4data.h"
- #include "unary.h"
- #include "mathops.h"
- #include "vdpau_internal.h"
- #include "libavutil/avassert.h"
- #undef NDEBUG
- #include <assert.h>
- #define MB_INTRA_VLC_BITS 9
- #define DC_VLC_BITS 9
- // offset tables for interlaced picture MVDATA decoding
- static const int offset_table1[9] = { 0, 1, 2, 4, 8, 16, 32, 64, 128 };
- static const int offset_table2[9] = { 0, 1, 3, 7, 15, 31, 63, 127, 255 };
- /***********************************************************************/
- /**
- * @name VC-1 Bitplane decoding
- * @see 8.7, p56
- * @{
- */
- /**
- * Imode types
- * @{
- */
- enum Imode {
- IMODE_RAW,
- IMODE_NORM2,
- IMODE_DIFF2,
- IMODE_NORM6,
- IMODE_DIFF6,
- IMODE_ROWSKIP,
- IMODE_COLSKIP
- };
- /** @} */ //imode defines
- static void init_block_index(VC1Context *v)
- {
- MpegEncContext *s = &v->s;
- ff_init_block_index(s);
- if (v->field_mode && v->second_field) {
- s->dest[0] += s->current_picture_ptr->f.linesize[0];
- s->dest[1] += s->current_picture_ptr->f.linesize[1];
- s->dest[2] += s->current_picture_ptr->f.linesize[2];
- }
- }
- /** @} */ //Bitplane group
- static void vc1_put_signed_blocks_clamped(VC1Context *v)
- {
- MpegEncContext *s = &v->s;
- int topleft_mb_pos, top_mb_pos;
- int stride_y, fieldtx = 0;
- int v_dist;
- /* The put pixels loop is always one MB row behind the decoding loop,
- * because we can only put pixels when overlap filtering is done, and
- * for filtering of the bottom edge of a MB, we need the next MB row
- * present as well.
- * Within the row, the put pixels loop is also one MB col behind the
- * decoding loop. The reason for this is again, because for filtering
- * of the right MB edge, we need the next MB present. */
- if (!s->first_slice_line) {
- if (s->mb_x) {
- topleft_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x - 1;
- if (v->fcm == ILACE_FRAME)
- fieldtx = v->fieldtx_plane[topleft_mb_pos];
- stride_y = s->linesize << fieldtx;
- v_dist = (16 - fieldtx) >> (fieldtx == 0);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][0],
- s->dest[0] - 16 * s->linesize - 16,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][1],
- s->dest[0] - 16 * s->linesize - 8,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][2],
- s->dest[0] - v_dist * s->linesize - 16,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][3],
- s->dest[0] - v_dist * s->linesize - 8,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][4],
- s->dest[1] - 8 * s->uvlinesize - 8,
- s->uvlinesize);
- s->dsp.put_signed_pixels_clamped(v->block[v->topleft_blk_idx][5],
- s->dest[2] - 8 * s->uvlinesize - 8,
- s->uvlinesize);
- }
- if (s->mb_x == s->mb_width - 1) {
- top_mb_pos = (s->mb_y - 1) * s->mb_stride + s->mb_x;
- if (v->fcm == ILACE_FRAME)
- fieldtx = v->fieldtx_plane[top_mb_pos];
- stride_y = s->linesize << fieldtx;
- v_dist = fieldtx ? 15 : 8;
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][0],
- s->dest[0] - 16 * s->linesize,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][1],
- s->dest[0] - 16 * s->linesize + 8,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][2],
- s->dest[0] - v_dist * s->linesize,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][3],
- s->dest[0] - v_dist * s->linesize + 8,
- stride_y);
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][4],
- s->dest[1] - 8 * s->uvlinesize,
- s->uvlinesize);
- s->dsp.put_signed_pixels_clamped(v->block[v->top_blk_idx][5],
- s->dest[2] - 8 * s->uvlinesize,
- s->uvlinesize);
- }
- }
- #define inc_blk_idx(idx) do { \
- idx++; \
- if (idx >= v->n_allocated_blks) \
- idx = 0; \
- } while (0)
- inc_blk_idx(v->topleft_blk_idx);
- inc_blk_idx(v->top_blk_idx);
- inc_blk_idx(v->left_blk_idx);
- inc_blk_idx(v->cur_blk_idx);
- }
- static void vc1_loop_filter_iblk(VC1Context *v, int pq)
- {
- MpegEncContext *s = &v->s;
- int j;
- if (!s->first_slice_line) {
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0], s->linesize, pq);
- if (s->mb_x)
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
- for (j = 0; j < 2; j++) {
- v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1], s->uvlinesize, pq);
- if (s->mb_x)
- v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
- }
- }
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0] + 8 * s->linesize, s->linesize, pq);
- if (s->mb_y == s->end_mb_y - 1) {
- if (s->mb_x) {
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0], s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter8(s->dest[1], s->uvlinesize, pq);
- v->vc1dsp.vc1_h_loop_filter8(s->dest[2], s->uvlinesize, pq);
- }
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] + 8, s->linesize, pq);
- }
- }
- static void vc1_loop_filter_iblk_delayed(VC1Context *v, int pq)
- {
- MpegEncContext *s = &v->s;
- int j;
- /* The loopfilter runs 1 row and 1 column behind the overlap filter, which
- * means it runs two rows/cols behind the decoding loop. */
- if (!s->first_slice_line) {
- if (s->mb_x) {
- if (s->mb_y >= s->start_mb_y + 2) {
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
- if (s->mb_x >= 2)
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 16, s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize - 8, s->linesize, pq);
- for (j = 0; j < 2; j++) {
- v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
- if (s->mb_x >= 2) {
- v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize - 8, s->uvlinesize, pq);
- }
- }
- }
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize - 16, s->linesize, pq);
- }
- if (s->mb_x == s->mb_width - 1) {
- if (s->mb_y >= s->start_mb_y + 2) {
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
- if (s->mb_x)
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize, s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 32 * s->linesize + 8, s->linesize, pq);
- for (j = 0; j < 2; j++) {
- v->vc1dsp.vc1_v_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
- if (s->mb_x >= 2) {
- v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 16 * s->uvlinesize, s->uvlinesize, pq);
- }
- }
- }
- v->vc1dsp.vc1_v_loop_filter16(s->dest[0] - 8 * s->linesize, s->linesize, pq);
- }
- if (s->mb_y == s->end_mb_y) {
- if (s->mb_x) {
- if (s->mb_x >= 2)
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 16, s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize - 8, s->linesize, pq);
- if (s->mb_x >= 2) {
- for (j = 0; j < 2; j++) {
- v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize - 8, s->uvlinesize, pq);
- }
- }
- }
- if (s->mb_x == s->mb_width - 1) {
- if (s->mb_x)
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize, s->linesize, pq);
- v->vc1dsp.vc1_h_loop_filter16(s->dest[0] - 16 * s->linesize + 8, s->linesize, pq);
- if (s->mb_x) {
- for (j = 0; j < 2; j++) {
- v->vc1dsp.vc1_h_loop_filter8(s->dest[j + 1] - 8 * s->uvlinesize, s->uvlinesize, pq);
- }
- }
- }
- }
- }
- }
- static void vc1_smooth_overlap_filter_iblk(VC1Context *v)
- {
- MpegEncContext *s = &v->s;
- int mb_pos;
- if (v->condover == CONDOVER_NONE)
- return;
- mb_pos = s->mb_x + s->mb_y * s->mb_stride;
- /* Within a MB, the horizontal overlap always runs before the vertical.
- * To accomplish that, we run the H on left and internal borders of the
- * currently decoded MB. Then, we wait for the next overlap iteration
- * to do H overlap on the right edge of this MB, before moving over and
- * running the V overlap. Therefore, the V overlap makes us trail by one
- * MB col and the H overlap filter makes us trail by one MB row. This
- * is reflected in the time at which we run the put_pixels loop. */
- if (v->condover == CONDOVER_ALL || v->pq >= 9 || v->over_flags_plane[mb_pos]) {
- if (s->mb_x && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
- v->over_flags_plane[mb_pos - 1])) {
- v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][1],
- v->block[v->cur_blk_idx][0]);
- v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][3],
- v->block[v->cur_blk_idx][2]);
- if (!(s->flags & CODEC_FLAG_GRAY)) {
- v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][4],
- v->block[v->cur_blk_idx][4]);
- v->vc1dsp.vc1_h_s_overlap(v->block[v->left_blk_idx][5],
- v->block[v->cur_blk_idx][5]);
- }
- }
- v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][0],
- v->block[v->cur_blk_idx][1]);
- v->vc1dsp.vc1_h_s_overlap(v->block[v->cur_blk_idx][2],
- v->block[v->cur_blk_idx][3]);
- if (s->mb_x == s->mb_width - 1) {
- if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
- v->over_flags_plane[mb_pos - s->mb_stride])) {
- v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][2],
- v->block[v->cur_blk_idx][0]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][3],
- v->block[v->cur_blk_idx][1]);
- if (!(s->flags & CODEC_FLAG_GRAY)) {
- v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][4],
- v->block[v->cur_blk_idx][4]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->top_blk_idx][5],
- v->block[v->cur_blk_idx][5]);
- }
- }
- v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][0],
- v->block[v->cur_blk_idx][2]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->cur_blk_idx][1],
- v->block[v->cur_blk_idx][3]);
- }
- }
- if (s->mb_x && (v->condover == CONDOVER_ALL || v->over_flags_plane[mb_pos - 1])) {
- if (!s->first_slice_line && (v->condover == CONDOVER_ALL || v->pq >= 9 ||
- v->over_flags_plane[mb_pos - s->mb_stride - 1])) {
- v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][2],
- v->block[v->left_blk_idx][0]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][3],
- v->block[v->left_blk_idx][1]);
- if (!(s->flags & CODEC_FLAG_GRAY)) {
- v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][4],
- v->block[v->left_blk_idx][4]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->topleft_blk_idx][5],
- v->block[v->left_blk_idx][5]);
- }
- }
- v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][0],
- v->block[v->left_blk_idx][2]);
- v->vc1dsp.vc1_v_s_overlap(v->block[v->left_blk_idx][1],
- v->block[v->left_blk_idx][3]);
- }
- }
- /** Do motion compensation over 1 macroblock
- * Mostly adapted hpel_motion and qpel_motion from mpegvideo.c
- */
- static void vc1_mc_1mv(VC1Context *v, int dir)
- {
- MpegEncContext *s = &v->s;
- DSPContext *dsp = &v->s.dsp;
- H264ChromaContext *h264chroma = &v->h264chroma;
- uint8_t *srcY, *srcU, *srcV;
- int dxy, mx, my, uvmx, uvmy, src_x, src_y, uvsrc_x, uvsrc_y;
- int off, off_uv;
- int v_edge_pos = s->v_edge_pos >> v->field_mode;
- if ((!v->field_mode ||
- (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
- !v->s.last_picture.f.data[0])
- return;
- mx = s->mv[dir][0][0];
- my = s->mv[dir][0][1];
- // store motion vectors for further use in B frames
- if (s->pict_type == AV_PICTURE_TYPE_P) {
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = mx;
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = my;
- }
- uvmx = (mx + ((mx & 3) == 3)) >> 1;
- uvmy = (my + ((my & 3) == 3)) >> 1;
- v->luma_mv[s->mb_x][0] = uvmx;
- v->luma_mv[s->mb_x][1] = uvmy;
- if (v->field_mode &&
- v->cur_field_type != v->ref_field_type[dir]) {
- my = my - 2 + 4 * v->cur_field_type;
- uvmy = uvmy - 2 + 4 * v->cur_field_type;
- }
- // fastuvmc shall be ignored for interlaced frame picture
- if (v->fastuvmc && (v->fcm != ILACE_FRAME)) {
- uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
- uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
- }
- if (v->field_mode) { // interlaced field picture
- if (!dir) {
- if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field) {
- srcY = s->current_picture.f.data[0];
- srcU = s->current_picture.f.data[1];
- srcV = s->current_picture.f.data[2];
- } else {
- srcY = s->last_picture.f.data[0];
- srcU = s->last_picture.f.data[1];
- srcV = s->last_picture.f.data[2];
- }
- } else {
- srcY = s->next_picture.f.data[0];
- srcU = s->next_picture.f.data[1];
- srcV = s->next_picture.f.data[2];
- }
- } else {
- if (!dir) {
- srcY = s->last_picture.f.data[0];
- srcU = s->last_picture.f.data[1];
- srcV = s->last_picture.f.data[2];
- } else {
- srcY = s->next_picture.f.data[0];
- srcU = s->next_picture.f.data[1];
- srcV = s->next_picture.f.data[2];
- }
- }
- if(!srcY)
- return;
- src_x = s->mb_x * 16 + (mx >> 2);
- src_y = s->mb_y * 16 + (my >> 2);
- uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
- uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
- if (v->profile != PROFILE_ADVANCED) {
- src_x = av_clip( src_x, -16, s->mb_width * 16);
- src_y = av_clip( src_y, -16, s->mb_height * 16);
- uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
- uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
- } else {
- src_x = av_clip( src_x, -17, s->avctx->coded_width);
- src_y = av_clip( src_y, -18, s->avctx->coded_height + 1);
- uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
- uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
- }
- srcY += src_y * s->linesize + src_x;
- srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
- srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
- if (v->field_mode && v->ref_field_type[dir]) {
- srcY += s->current_picture_ptr->f.linesize[0];
- srcU += s->current_picture_ptr->f.linesize[1];
- srcV += s->current_picture_ptr->f.linesize[2];
- }
- /* for grayscale we should not try to read from unknown area */
- if (s->flags & CODEC_FLAG_GRAY) {
- srcU = s->edge_emu_buffer + 18 * s->linesize;
- srcV = s->edge_emu_buffer + 18 * s->linesize;
- }
- if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
- || s->h_edge_pos < 22 || v_edge_pos < 22
- || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx&3) - 16 - s->mspel * 3
- || (unsigned)(src_y - 1) > v_edge_pos - (my&3) - 16 - 3) {
- uint8_t *uvbuf = s->edge_emu_buffer + 19 * s->linesize;
- srcY -= s->mspel * (1 + s->linesize);
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
- 17 + s->mspel * 2, 17 + s->mspel * 2,
- src_x - s->mspel, src_y - s->mspel,
- s->h_edge_pos, v_edge_pos);
- srcY = s->edge_emu_buffer;
- s->vdsp.emulated_edge_mc(uvbuf , srcU, s->uvlinesize, 8 + 1, 8 + 1,
- uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
- s->vdsp.emulated_edge_mc(uvbuf + 16, srcV, s->uvlinesize, 8 + 1, 8 + 1,
- uvsrc_x, uvsrc_y, s->h_edge_pos >> 1, v_edge_pos >> 1);
- srcU = uvbuf;
- srcV = uvbuf + 16;
- /* if we deal with range reduction we need to scale source blocks */
- if (v->rangeredfrm) {
- int i, j;
- uint8_t *src, *src2;
- src = srcY;
- for (j = 0; j < 17 + s->mspel * 2; j++) {
- for (i = 0; i < 17 + s->mspel * 2; i++)
- src[i] = ((src[i] - 128) >> 1) + 128;
- src += s->linesize;
- }
- src = srcU;
- src2 = srcV;
- for (j = 0; j < 9; j++) {
- for (i = 0; i < 9; i++) {
- src[i] = ((src[i] - 128) >> 1) + 128;
- src2[i] = ((src2[i] - 128) >> 1) + 128;
- }
- src += s->uvlinesize;
- src2 += s->uvlinesize;
- }
- }
- /* if we deal with intensity compensation we need to scale source blocks */
- if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
- int i, j;
- uint8_t *src, *src2;
- src = srcY;
- for (j = 0; j < 17 + s->mspel * 2; j++) {
- for (i = 0; i < 17 + s->mspel * 2; i++)
- src[i] = v->luty[src[i]];
- src += s->linesize;
- }
- src = srcU;
- src2 = srcV;
- for (j = 0; j < 9; j++) {
- for (i = 0; i < 9; i++) {
- src[i] = v->lutuv[src[i]];
- src2[i] = v->lutuv[src2[i]];
- }
- src += s->uvlinesize;
- src2 += s->uvlinesize;
- }
- }
- srcY += s->mspel * (1 + s->linesize);
- }
- off = 0;
- off_uv = 0;
- if (s->mspel) {
- dxy = ((my & 3) << 2) | (mx & 3);
- v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off , srcY , s->linesize, v->rnd);
- v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8, srcY + 8, s->linesize, v->rnd);
- srcY += s->linesize * 8;
- v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize , srcY , s->linesize, v->rnd);
- v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off + 8 * s->linesize + 8, srcY + 8, s->linesize, v->rnd);
- } else { // hpel mc - always used for luma
- dxy = (my & 2) | ((mx & 2) >> 1);
- if (!v->rnd)
- dsp->put_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
- else
- dsp->put_no_rnd_pixels_tab[0][dxy](s->dest[0] + off, srcY, s->linesize, 16);
- }
- if (s->flags & CODEC_FLAG_GRAY) return;
- /* Chroma MC always uses qpel bilinear */
- uvmx = (uvmx & 3) << 1;
- uvmy = (uvmy & 3) << 1;
- if (!v->rnd) {
- h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
- h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
- } else {
- v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off_uv, srcU, s->uvlinesize, 8, uvmx, uvmy);
- v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off_uv, srcV, s->uvlinesize, 8, uvmx, uvmy);
- }
- }
- static inline int median4(int a, int b, int c, int d)
- {
- if (a < b) {
- if (c < d) return (FFMIN(b, d) + FFMAX(a, c)) / 2;
- else return (FFMIN(b, c) + FFMAX(a, d)) / 2;
- } else {
- if (c < d) return (FFMIN(a, d) + FFMAX(b, c)) / 2;
- else return (FFMIN(a, c) + FFMAX(b, d)) / 2;
- }
- }
- /** Do motion compensation for 4-MV macroblock - luminance block
- */
- static void vc1_mc_4mv_luma(VC1Context *v, int n, int dir)
- {
- MpegEncContext *s = &v->s;
- DSPContext *dsp = &v->s.dsp;
- uint8_t *srcY;
- int dxy, mx, my, src_x, src_y;
- int off;
- int fieldmv = (v->fcm == ILACE_FRAME) ? v->blk_mv_type[s->block_index[n]] : 0;
- int v_edge_pos = s->v_edge_pos >> v->field_mode;
- if ((!v->field_mode ||
- (v->ref_field_type[dir] == 1 && v->cur_field_type == 1)) &&
- !v->s.last_picture.f.data[0])
- return;
- mx = s->mv[dir][n][0];
- my = s->mv[dir][n][1];
- if (!dir) {
- if (v->field_mode) {
- if ((v->cur_field_type != v->ref_field_type[dir]) && v->second_field)
- srcY = s->current_picture.f.data[0];
- else
- srcY = s->last_picture.f.data[0];
- } else
- srcY = s->last_picture.f.data[0];
- } else
- srcY = s->next_picture.f.data[0];
- if(!srcY)
- return;
- if (v->field_mode) {
- if (v->cur_field_type != v->ref_field_type[dir])
- my = my - 2 + 4 * v->cur_field_type;
- }
- if (s->pict_type == AV_PICTURE_TYPE_P && n == 3 && v->field_mode) {
- int same_count = 0, opp_count = 0, k;
- int chosen_mv[2][4][2], f;
- int tx, ty;
- for (k = 0; k < 4; k++) {
- f = v->mv_f[0][s->block_index[k] + v->blocks_off];
- chosen_mv[f][f ? opp_count : same_count][0] = s->mv[0][k][0];
- chosen_mv[f][f ? opp_count : same_count][1] = s->mv[0][k][1];
- opp_count += f;
- same_count += 1 - f;
- }
- f = opp_count > same_count;
- switch (f ? opp_count : same_count) {
- case 4:
- tx = median4(chosen_mv[f][0][0], chosen_mv[f][1][0],
- chosen_mv[f][2][0], chosen_mv[f][3][0]);
- ty = median4(chosen_mv[f][0][1], chosen_mv[f][1][1],
- chosen_mv[f][2][1], chosen_mv[f][3][1]);
- break;
- case 3:
- tx = mid_pred(chosen_mv[f][0][0], chosen_mv[f][1][0], chosen_mv[f][2][0]);
- ty = mid_pred(chosen_mv[f][0][1], chosen_mv[f][1][1], chosen_mv[f][2][1]);
- break;
- case 2:
- tx = (chosen_mv[f][0][0] + chosen_mv[f][1][0]) / 2;
- ty = (chosen_mv[f][0][1] + chosen_mv[f][1][1]) / 2;
- break;
- default:
- av_assert2(0);
- }
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
- for (k = 0; k < 4; k++)
- v->mv_f[1][s->block_index[k] + v->blocks_off] = f;
- }
- if (v->fcm == ILACE_FRAME) { // not sure if needed for other types of picture
- int qx, qy;
- int width = s->avctx->coded_width;
- int height = s->avctx->coded_height >> 1;
- qx = (s->mb_x * 16) + (mx >> 2);
- qy = (s->mb_y * 8) + (my >> 3);
- if (qx < -17)
- mx -= 4 * (qx + 17);
- else if (qx > width)
- mx -= 4 * (qx - width);
- if (qy < -18)
- my -= 8 * (qy + 18);
- else if (qy > height + 1)
- my -= 8 * (qy - height - 1);
- }
- if ((v->fcm == ILACE_FRAME) && fieldmv)
- off = ((n > 1) ? s->linesize : 0) + (n & 1) * 8;
- else
- off = s->linesize * 4 * (n & 2) + (n & 1) * 8;
- src_x = s->mb_x * 16 + (n & 1) * 8 + (mx >> 2);
- if (!fieldmv)
- src_y = s->mb_y * 16 + (n & 2) * 4 + (my >> 2);
- else
- src_y = s->mb_y * 16 + ((n > 1) ? 1 : 0) + (my >> 2);
- if (v->profile != PROFILE_ADVANCED) {
- src_x = av_clip(src_x, -16, s->mb_width * 16);
- src_y = av_clip(src_y, -16, s->mb_height * 16);
- } else {
- src_x = av_clip(src_x, -17, s->avctx->coded_width);
- if (v->fcm == ILACE_FRAME) {
- if (src_y & 1)
- src_y = av_clip(src_y, -17, s->avctx->coded_height + 1);
- else
- src_y = av_clip(src_y, -18, s->avctx->coded_height);
- } else {
- src_y = av_clip(src_y, -18, s->avctx->coded_height + 1);
- }
- }
- srcY += src_y * s->linesize + src_x;
- if (v->field_mode && v->ref_field_type[dir])
- srcY += s->current_picture_ptr->f.linesize[0];
- if (fieldmv && !(src_y & 1))
- v_edge_pos--;
- if (fieldmv && (src_y & 1) && src_y < 4)
- src_y--;
- if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
- || s->h_edge_pos < 13 || v_edge_pos < 23
- || (unsigned)(src_x - s->mspel) > s->h_edge_pos - (mx & 3) - 8 - s->mspel * 2
- || (unsigned)(src_y - (s->mspel << fieldmv)) > v_edge_pos - (my & 3) - ((8 + s->mspel * 2) << fieldmv)) {
- srcY -= s->mspel * (1 + (s->linesize << fieldmv));
- /* check emulate edge stride and offset */
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcY, s->linesize,
- 9 + s->mspel * 2, (9 + s->mspel * 2) << fieldmv,
- src_x - s->mspel, src_y - (s->mspel << fieldmv),
- s->h_edge_pos, v_edge_pos);
- srcY = s->edge_emu_buffer;
- /* if we deal with range reduction we need to scale source blocks */
- if (v->rangeredfrm) {
- int i, j;
- uint8_t *src;
- src = srcY;
- for (j = 0; j < 9 + s->mspel * 2; j++) {
- for (i = 0; i < 9 + s->mspel * 2; i++)
- src[i] = ((src[i] - 128) >> 1) + 128;
- src += s->linesize << fieldmv;
- }
- }
- /* if we deal with intensity compensation we need to scale source blocks */
- if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
- int i, j;
- uint8_t *src;
- src = srcY;
- for (j = 0; j < 9 + s->mspel * 2; j++) {
- for (i = 0; i < 9 + s->mspel * 2; i++)
- src[i] = v->luty[src[i]];
- src += s->linesize << fieldmv;
- }
- }
- srcY += s->mspel * (1 + (s->linesize << fieldmv));
- }
- if (s->mspel) {
- dxy = ((my & 3) << 2) | (mx & 3);
- v->vc1dsp.put_vc1_mspel_pixels_tab[dxy](s->dest[0] + off, srcY, s->linesize << fieldmv, v->rnd);
- } else { // hpel mc - always used for luma
- dxy = (my & 2) | ((mx & 2) >> 1);
- if (!v->rnd)
- dsp->put_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
- else
- dsp->put_no_rnd_pixels_tab[1][dxy](s->dest[0] + off, srcY, s->linesize, 8);
- }
- }
- static av_always_inline int get_chroma_mv(int *mvx, int *mvy, int *a, int flag, int *tx, int *ty)
- {
- int idx, i;
- static const int count[16] = { 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4};
- idx = ((a[3] != flag) << 3)
- | ((a[2] != flag) << 2)
- | ((a[1] != flag) << 1)
- | (a[0] != flag);
- if (!idx) {
- *tx = median4(mvx[0], mvx[1], mvx[2], mvx[3]);
- *ty = median4(mvy[0], mvy[1], mvy[2], mvy[3]);
- return 4;
- } else if (count[idx] == 1) {
- switch (idx) {
- case 0x1:
- *tx = mid_pred(mvx[1], mvx[2], mvx[3]);
- *ty = mid_pred(mvy[1], mvy[2], mvy[3]);
- return 3;
- case 0x2:
- *tx = mid_pred(mvx[0], mvx[2], mvx[3]);
- *ty = mid_pred(mvy[0], mvy[2], mvy[3]);
- return 3;
- case 0x4:
- *tx = mid_pred(mvx[0], mvx[1], mvx[3]);
- *ty = mid_pred(mvy[0], mvy[1], mvy[3]);
- return 3;
- case 0x8:
- *tx = mid_pred(mvx[0], mvx[1], mvx[2]);
- *ty = mid_pred(mvy[0], mvy[1], mvy[2]);
- return 3;
- }
- } else if (count[idx] == 2) {
- int t1 = 0, t2 = 0;
- for (i = 0; i < 3; i++)
- if (!a[i]) {
- t1 = i;
- break;
- }
- for (i = t1 + 1; i < 4; i++)
- if (!a[i]) {
- t2 = i;
- break;
- }
- *tx = (mvx[t1] + mvx[t2]) / 2;
- *ty = (mvy[t1] + mvy[t2]) / 2;
- return 2;
- } else {
- return 0;
- }
- return -1;
- }
- /** Do motion compensation for 4-MV macroblock - both chroma blocks
- */
- static void vc1_mc_4mv_chroma(VC1Context *v, int dir)
- {
- MpegEncContext *s = &v->s;
- H264ChromaContext *h264chroma = &v->h264chroma;
- uint8_t *srcU, *srcV;
- int uvmx, uvmy, uvsrc_x, uvsrc_y;
- int k, tx = 0, ty = 0;
- int mvx[4], mvy[4], intra[4], mv_f[4];
- int valid_count;
- int chroma_ref_type = v->cur_field_type, off = 0;
- int v_edge_pos = s->v_edge_pos >> v->field_mode;
- if (!v->field_mode && !v->s.last_picture.f.data[0])
- return;
- if (s->flags & CODEC_FLAG_GRAY)
- return;
- for (k = 0; k < 4; k++) {
- mvx[k] = s->mv[dir][k][0];
- mvy[k] = s->mv[dir][k][1];
- intra[k] = v->mb_type[0][s->block_index[k]];
- if (v->field_mode)
- mv_f[k] = v->mv_f[dir][s->block_index[k] + v->blocks_off];
- }
- /* calculate chroma MV vector from four luma MVs */
- if (!v->field_mode || (v->field_mode && !v->numref)) {
- valid_count = get_chroma_mv(mvx, mvy, intra, 0, &tx, &ty);
- chroma_ref_type = v->reffield;
- if (!valid_count) {
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = 0;
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = 0;
- v->luma_mv[s->mb_x][0] = v->luma_mv[s->mb_x][1] = 0;
- return; //no need to do MC for intra blocks
- }
- } else {
- int dominant = 0;
- if (mv_f[0] + mv_f[1] + mv_f[2] + mv_f[3] > 2)
- dominant = 1;
- valid_count = get_chroma_mv(mvx, mvy, mv_f, dominant, &tx, &ty);
- if (dominant)
- chroma_ref_type = !v->cur_field_type;
- }
- if (v->field_mode && chroma_ref_type == 1 && v->cur_field_type == 1 && !v->s.last_picture.f.data[0])
- return;
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][0] = tx;
- s->current_picture.f.motion_val[1][s->block_index[0] + v->blocks_off][1] = ty;
- uvmx = (tx + ((tx & 3) == 3)) >> 1;
- uvmy = (ty + ((ty & 3) == 3)) >> 1;
- v->luma_mv[s->mb_x][0] = uvmx;
- v->luma_mv[s->mb_x][1] = uvmy;
- if (v->fastuvmc) {
- uvmx = uvmx + ((uvmx < 0) ? (uvmx & 1) : -(uvmx & 1));
- uvmy = uvmy + ((uvmy < 0) ? (uvmy & 1) : -(uvmy & 1));
- }
- // Field conversion bias
- if (v->cur_field_type != chroma_ref_type)
- uvmy += 2 - 4 * chroma_ref_type;
- uvsrc_x = s->mb_x * 8 + (uvmx >> 2);
- uvsrc_y = s->mb_y * 8 + (uvmy >> 2);
- if (v->profile != PROFILE_ADVANCED) {
- uvsrc_x = av_clip(uvsrc_x, -8, s->mb_width * 8);
- uvsrc_y = av_clip(uvsrc_y, -8, s->mb_height * 8);
- } else {
- uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
- uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
- }
- if (!dir) {
- if (v->field_mode) {
- if ((v->cur_field_type != chroma_ref_type) && v->cur_field_type) {
- srcU = s->current_picture.f.data[1];
- srcV = s->current_picture.f.data[2];
- } else {
- srcU = s->last_picture.f.data[1];
- srcV = s->last_picture.f.data[2];
- }
- } else {
- srcU = s->last_picture.f.data[1];
- srcV = s->last_picture.f.data[2];
- }
- } else {
- srcU = s->next_picture.f.data[1];
- srcV = s->next_picture.f.data[2];
- }
- if(!srcU)
- return;
- srcU += uvsrc_y * s->uvlinesize + uvsrc_x;
- srcV += uvsrc_y * s->uvlinesize + uvsrc_x;
- if (v->field_mode) {
- if (chroma_ref_type) {
- srcU += s->current_picture_ptr->f.linesize[1];
- srcV += s->current_picture_ptr->f.linesize[2];
- }
- off = 0;
- }
- if (v->rangeredfrm || (v->mv_mode == MV_PMODE_INTENSITY_COMP)
- || s->h_edge_pos < 18 || v_edge_pos < 18
- || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 9
- || (unsigned)uvsrc_y > (v_edge_pos >> 1) - 9) {
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer , srcU, s->uvlinesize,
- 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
- s->h_edge_pos >> 1, v_edge_pos >> 1);
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
- 8 + 1, 8 + 1, uvsrc_x, uvsrc_y,
- s->h_edge_pos >> 1, v_edge_pos >> 1);
- srcU = s->edge_emu_buffer;
- srcV = s->edge_emu_buffer + 16;
- /* if we deal with range reduction we need to scale source blocks */
- if (v->rangeredfrm) {
- int i, j;
- uint8_t *src, *src2;
- src = srcU;
- src2 = srcV;
- for (j = 0; j < 9; j++) {
- for (i = 0; i < 9; i++) {
- src[i] = ((src[i] - 128) >> 1) + 128;
- src2[i] = ((src2[i] - 128) >> 1) + 128;
- }
- src += s->uvlinesize;
- src2 += s->uvlinesize;
- }
- }
- /* if we deal with intensity compensation we need to scale source blocks */
- if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
- int i, j;
- uint8_t *src, *src2;
- src = srcU;
- src2 = srcV;
- for (j = 0; j < 9; j++) {
- for (i = 0; i < 9; i++) {
- src[i] = v->lutuv[src[i]];
- src2[i] = v->lutuv[src2[i]];
- }
- src += s->uvlinesize;
- src2 += s->uvlinesize;
- }
- }
- }
- /* Chroma MC always uses qpel bilinear */
- uvmx = (uvmx & 3) << 1;
- uvmy = (uvmy & 3) << 1;
- if (!v->rnd) {
- h264chroma->put_h264_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
- h264chroma->put_h264_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
- } else {
- v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[1] + off, srcU, s->uvlinesize, 8, uvmx, uvmy);
- v->vc1dsp.put_no_rnd_vc1_chroma_pixels_tab[0](s->dest[2] + off, srcV, s->uvlinesize, 8, uvmx, uvmy);
- }
- }
- /** Do motion compensation for 4-MV field chroma macroblock (both U and V)
- */
- static void vc1_mc_4mv_chroma4(VC1Context *v)
- {
- MpegEncContext *s = &v->s;
- H264ChromaContext *h264chroma = &v->h264chroma;
- uint8_t *srcU, *srcV;
- int uvsrc_x, uvsrc_y;
- int uvmx_field[4], uvmy_field[4];
- int i, off, tx, ty;
- int fieldmv = v->blk_mv_type[s->block_index[0]];
- static const int s_rndtblfield[16] = { 0, 0, 1, 2, 4, 4, 5, 6, 2, 2, 3, 8, 6, 6, 7, 12 };
- int v_dist = fieldmv ? 1 : 4; // vertical offset for lower sub-blocks
- int v_edge_pos = s->v_edge_pos >> 1;
- if (!v->s.last_picture.f.data[0])
- return;
- if (s->flags & CODEC_FLAG_GRAY)
- return;
- for (i = 0; i < 4; i++) {
- tx = s->mv[0][i][0];
- uvmx_field[i] = (tx + ((tx & 3) == 3)) >> 1;
- ty = s->mv[0][i][1];
- if (fieldmv)
- uvmy_field[i] = (ty >> 4) * 8 + s_rndtblfield[ty & 0xF];
- else
- uvmy_field[i] = (ty + ((ty & 3) == 3)) >> 1;
- }
- for (i = 0; i < 4; i++) {
- off = (i & 1) * 4 + ((i & 2) ? v_dist * s->uvlinesize : 0);
- uvsrc_x = s->mb_x * 8 + (i & 1) * 4 + (uvmx_field[i] >> 2);
- uvsrc_y = s->mb_y * 8 + ((i & 2) ? v_dist : 0) + (uvmy_field[i] >> 2);
- // FIXME: implement proper pull-back (see vc1cropmv.c, vc1CROPMV_ChromaPullBack())
- uvsrc_x = av_clip(uvsrc_x, -8, s->avctx->coded_width >> 1);
- uvsrc_y = av_clip(uvsrc_y, -8, s->avctx->coded_height >> 1);
- srcU = s->last_picture.f.data[1] + uvsrc_y * s->uvlinesize + uvsrc_x;
- srcV = s->last_picture.f.data[2] + uvsrc_y * s->uvlinesize + uvsrc_x;
- uvmx_field[i] = (uvmx_field[i] & 3) << 1;
- uvmy_field[i] = (uvmy_field[i] & 3) << 1;
- if (fieldmv && !(uvsrc_y & 1))
- v_edge_pos = (s->v_edge_pos >> 1) - 1;
- if (fieldmv && (uvsrc_y & 1) && uvsrc_y < 2)
- uvsrc_y--;
- if ((v->mv_mode == MV_PMODE_INTENSITY_COMP)
- || s->h_edge_pos < 10 || v_edge_pos < (5 << fieldmv)
- || (unsigned)uvsrc_x > (s->h_edge_pos >> 1) - 5
- || (unsigned)uvsrc_y > v_edge_pos - (5 << fieldmv)) {
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer, srcU, s->uvlinesize,
- 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
- s->h_edge_pos >> 1, v_edge_pos);
- s->vdsp.emulated_edge_mc(s->edge_emu_buffer + 16, srcV, s->uvlinesize,
- 5, (5 << fieldmv), uvsrc_x, uvsrc_y,
- s->h_edge_pos >> 1, v_edge_pos);
- srcU = s->edge_emu_buffer;
- srcV = s->edge_emu_buffer + 16;
- /* if we deal with intensity compensation we need to scale source blocks */
- if (v->mv_mode == MV_PMODE_INTENSITY_COMP) {
- int i, j;
- uint8_t *src, *src2;
- src = srcU;
- src2 = srcV;
- for (j = 0; j < 5; j++) {
- for (i = 0; i < 5; i++) {
- src[i] = v->lutuv[src[i]];
- src2[i] = v->lutuv[src2[i]];
- }
- src += s->uvlinesize << 1;
- src2 += s->uvlinesize << 1;
- }
- }
- }
- if (!v->rnd) {
- h264chroma->put_h264_chroma_pixels_tab[1](s->dest[1] + off, srcU, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
- h264chroma->put_h264_chroma_pixels_tab[1](s->dest[2] + off, srcV, s->uvlinesize << fieldmv, 4, uvmx_field[i], uvmy_field[i]);
- } else {
- 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]);
- 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]);
- }
- }
- }
- /***********************************************************************/
- /**
- * @name VC-1 Block-level functions
- * @see 7.1.4, p91 and 8.1.1.7, p(1)04
- * @{
- */
- /**
- * @def GET_MQUANT
- * @brief Get macroblock-level quantizer scale
- */
- #define GET_MQUANT() \
- if (v->dquantfrm) { \
- int edges = 0; \
- if (v->dqprofile == DQPROFILE_ALL_MBS) { \
- if (v->dqbilevel) { \
- mquant = (get_bits1(gb)) ? v->altpq : v->pq; \
- } else { \
- mqdiff = get_bits(gb, 3); \
- if (mqdiff != 7) \
- mquant = v->pq + mqdiff; \
- else \
- mquant = get_bits(gb, 5); \
- } \
- } \
- if (v->dqprofile == DQPROFILE_SINGLE_EDGE) \
- edges = 1 << v->dqsbedge; \
- else if (v->dqprofile == DQPROFILE_DOUBLE_EDGES) \
- edges = (3 << v->dqsbedge) % 15; \
- else if (v->dqprofile == DQPROFILE_FOUR_EDGES) \
- edges = 15; \
- if ((edges&1) && !s->mb_x) \
- mquant = v->altpq; \
- if ((edges&2) && s->first_slice_line) \
- mquant = v->altpq; \
- if ((edges&4) && s->mb_x == (s->mb_width - 1)) \
- mquant = v->altpq; \
- if ((edges&8) && s->mb_y == (s->mb_height - 1)) \
- mquant = v->altpq; \
- if (!mquant || mquant > 31) { \
- av_log(v->s.avctx, AV_LOG_ERROR, \
- "Overriding invalid mquant %d\n", mquant); \
- mquant = 1; \
- } \
- }
- /**
- * @def GET_MVDATA(_dmv_x, _dmv_y)
- * @brief Get MV differentials
- * @see MVDATA decoding from 8.3.5.2, p(1)20
- * @param _dmv_x Horizontal differential for decoded MV
- * @param _dmv_y Vertical differential for decoded MV
- */
- #define GET_MVDATA(_dmv_x, _dmv_y) \
- index = 1 + get_vlc2(gb, ff_vc1_mv_diff_vlc[s->mv_table_index].table, \
- VC1_MV_DIFF_VLC_BITS, 2); \
- if (index > 36) { \
- mb_has_coeffs = 1; \
- index -= 37; \
- } else \
- mb_has_coeffs = 0; \
- s->mb_intra = 0; \
- if (!index) { \
- _dmv_x = _dmv_y = 0; \
- } else if (index == 35) { \
- _dmv_x = get_bits(gb, v->k_x - 1 + s->quarter_sample); \
- _dmv_y = get_bits(gb, v->k_y - 1 + s->quarter_sample); \
- } else if (index == 36) { \
- _dmv_x = 0; \
- _dmv_y = 0; \
- s->mb_intra = 1; \
- } else { \
- index1 = index % 6; \
- if (!s->quarter_sample && index1 == 5) val = 1; \
- else val = 0; \
- if (size_table[index1] - val > 0) \
- val = get_bits(gb, size_table[index1] - val); \
- else val = 0; \
- sign = 0 - (val&1); \
- _dmv_x = (sign ^ ((val>>1) + offset_table[index1])) - sign; \
- \
- index1 = index / 6; \
- if (!s->quarter_sample && index1 == 5) val = 1; \
- else val = 0; \
- if (size_table[index1] - val > 0) \
- val = get_bits(gb, size_table[index1] - val); \
- else val = 0; \
- sign = 0 - (val & 1); \
- _dmv_y = (sign ^ ((val >> 1) + offset_table[index1])) - sign; \
- }
- static av_always_inline void get_mvdata_interlaced(VC1Context *v, int *dmv_x,
- int *dmv_y, int *pred_flag)
- {
- int index, index1;
- int extend_x = 0, extend_y = 0;
- GetBitContext *gb = &v->s.gb;
- int bits, esc;
- int val, sign;
- const int* offs_tab;
- if (v->numref) {
- bits = VC1_2REF_MVDATA_VLC_BITS;
- esc = 125;
- } else {
- bits = VC1_1REF_MVDATA_VLC_BITS;
- esc = 71;
- }
- switch (v->dmvrange) {
- case 1:
- extend_x = 1;
- break;
- case 2:
- extend_y = 1;
- break;
- case 3:
- extend_x = extend_y = 1;
- break;
- }
- index = get_vlc2(gb, v->imv_vlc->table, bits, 3);
- if (index == esc) {
- *dmv_x = get_bits(gb, v->k_x);
- *dmv_y = get_bits(gb, v->k_y);
- if (v->numref) {
- if (pred_flag) {
- *pred_flag = *dmv_y & 1;
- *dmv_y = (*dmv_y + *pred_flag) >> 1;
- } else {
- *dmv_y = (*dmv_y + (*dmv_y & 1)) >> 1;
- }
- }
- }
- else {
- av_assert0(index < esc);
- if (extend_x)
- offs_tab = offset_table2;
- else
- offs_tab = offset_table1;
- index1 = (index + 1) % 9;
- if (index1 != 0) {
- val = get_bits(gb, index1 + extend_x);
- sign = 0 -(val & 1);
- *dmv_x = (sign ^ ((val >> 1) + offs_tab[index1])) - sign;
- } else
- *dmv_x = 0;
- if (extend_y)
- offs_tab = offset_table2;
- else
- offs_tab = offset_table1;
- index1 = (index + 1) / 9;
- if (index1 > v->numref) {
- val = get_bits(gb, (index1 + (extend_y << v->numref)) >> v->numref);
- sign = 0 - (val & 1);
- *dmv_y = (sign ^ ((val >> 1) + offs_tab[index1 >> v->numref])) - sign;
- } else
- *dmv_y = 0;
- if (v->numref && pred_flag)
- *pred_flag = index1 & 1;
- }
- }
- static av_always_inline int scaleforsame_x(VC1Context *v, int n /* MV */, int dir)
- {
- int scaledvalue, refdist;
- int scalesame1, scalesame2;
- int scalezone1_x, zone1offset_x;
- int table_index = dir ^ v->second_field;
- if (v->s.pict_type != AV_PICTURE_TYPE_B)
- refdist = v->refdist;
- else
- refdist = dir ? v->brfd : v->frfd;
- if (refdist > 3)
- refdist = 3;
- scalesame1 = ff_vc1_field_mvpred_scales[table_index][1][refdist];
- scalesame2 = ff_vc1_field_mvpred_scales[table_index][2][refdist];
- scalezone1_x = ff_vc1_field_mvpred_scales[table_index][3][refdist];
- zone1offset_x = ff_vc1_field_mvpred_scales[table_index][5][refdist];
- if (FFABS(n) > 255)
- scaledvalue = n;
- else {
- if (FFABS(n) < scalezone1_x)
- scaledvalue = (n * scalesame1) >> 8;
- else {
- if (n < 0)
- scaledvalue = ((n * scalesame2) >> 8) - zone1offset_x;
- else
- scaledvalue = ((n * scalesame2) >> 8) + zone1offset_x;
- }
- }
- return av_clip(scaledvalue, -v->range_x, v->range_x - 1);
- }
- static av_always_inline int scaleforsame_y(VC1Context *v, int i, int n /* MV */, int dir)
- {
- int scaledvalue, refdist;
- int scalesame1, scalesame2;
- int scalezone1_y,…
Large files files are truncated, but you can click here to view the full file