roqvideo.c 4.62 KB
Newer Older
1
/*
Vitor Sessak's avatar
Vitor Sessak committed
2 3
 * Copyright (C) 2003 Mike Melanson
 * Copyright (C) 2003 Dr. Tim Ferguson
4
 *
5
 * This file is part of Libav.
6
 *
7
 * Libav is free software; you can redistribute it and/or
8 9
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
10
 * version 2.1 of the License, or (at your option) any later version.
11
 *
12
 * Libav is distributed in the hope that it will be useful,
13 14 15 16 17
 * 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
18
 * License along with Libav; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 21 22
 */

/**
23
 * @file
24
 * id RoQ Video common functions based on work by Dr. Tim Ferguson
25 26 27
 */

#include "avcodec.h"
28
#include "roqvideo.h"
29

30 31 32 33 34 35 36 37 38 39
static inline void block_copy(unsigned char *out, unsigned char *in,
                              int outstride, int instride, int sz)
{
    int rows = sz;
    while(rows--) {
        memcpy(out, in, sz);
        out += outstride;
        in += instride;
    }
}
40

41
void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
42
{
43 44 45
    unsigned char *bptr;
    int boffs,stride;

46
    stride = ri->current_frame->linesize[0];
47
    boffs = y*stride + x;
48 49 50 51 52 53 54

    bptr = ri->current_frame->data[0] + boffs;
    bptr[0       ] = cell->y[0];
    bptr[1       ] = cell->y[1];
    bptr[stride  ] = cell->y[2];
    bptr[stride+1] = cell->y[3];

55 56 57
    stride = ri->current_frame->linesize[1];
    boffs = y*stride + x;

58 59 60 61 62 63 64 65 66 67 68
    bptr = ri->current_frame->data[1] + boffs;
    bptr[0       ] =
    bptr[1       ] =
    bptr[stride  ] =
    bptr[stride+1] = cell->u;

    bptr = ri->current_frame->data[2] + boffs;
    bptr[0       ] =
    bptr[1       ] =
    bptr[stride  ] =
    bptr[stride+1] = cell->v;
69 70
}

71
void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
72
{
73 74 75
    unsigned char *bptr;
    int boffs,stride;

76
    stride = ri->current_frame->linesize[0];
77
    boffs = y*stride + x;
78 79 80 81 82

    bptr = ri->current_frame->data[0] + boffs;
    bptr[         0] = bptr[         1] = bptr[stride    ] = bptr[stride  +1] = cell->y[0];
    bptr[         2] = bptr[         3] = bptr[stride  +2] = bptr[stride  +3] = cell->y[1];
    bptr[stride*2  ] = bptr[stride*2+1] = bptr[stride*3  ] = bptr[stride*3+1] = cell->y[2];
Vitor Sessak's avatar
Vitor Sessak committed
83
    bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->y[3];
84

85 86 87
    stride = ri->current_frame->linesize[1];
    boffs = y*stride + x;

88 89 90 91 92 93 94 95 96 97 98
    bptr = ri->current_frame->data[1] + boffs;
    bptr[         0] = bptr[         1] = bptr[stride    ] = bptr[stride  +1] =
    bptr[         2] = bptr[         3] = bptr[stride  +2] = bptr[stride  +3] =
    bptr[stride*2  ] = bptr[stride*2+1] = bptr[stride*3  ] = bptr[stride*3+1] =
    bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->u;

    bptr = ri->current_frame->data[2] + boffs;
    bptr[         0] = bptr[         1] = bptr[stride    ] = bptr[stride  +1] =
    bptr[         2] = bptr[         3] = bptr[stride  +2] = bptr[stride  +3] =
    bptr[stride*2  ] = bptr[stride*2+1] = bptr[stride*3  ] = bptr[stride*3+1] =
    bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->v;
99 100
}

101 102 103

static inline void apply_motion_generic(RoqContext *ri, int x, int y, int deltax,
                                        int deltay, int sz)
104
{
105
    int mx, my, cp;
106

107 108
    mx = x + deltax;
    my = y + deltay;
109

110
    /* check MV against frame boundaries */
111 112
    if ((mx < 0) || (mx > ri->width - sz) ||
        (my < 0) || (my > ri->height - sz)) {
113
        av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
114
            mx, my, ri->width, ri->height);
115 116 117
        return;
    }

118
    if (!ri->last_frame->data[0]) {
119 120 121 122
        av_log(ri->avctx, AV_LOG_ERROR, "Invalid decode type. Invalid header?\n");
        return;
    }

Vitor Sessak's avatar
Vitor Sessak committed
123
    for(cp = 0; cp < 3; cp++) {
124 125
        int outstride = ri->current_frame->linesize[cp];
        int instride  = ri->last_frame   ->linesize[cp];
126 127
        block_copy(ri->current_frame->data[cp] + y*outstride + x,
                   ri->last_frame->data[cp] + my*instride + mx,
128
                   outstride, instride, sz);
Vitor Sessak's avatar
Vitor Sessak committed
129
    }
130
}
131

132 133 134 135 136

void ff_apply_motion_4x4(RoqContext *ri, int x, int y,
                             int deltax, int deltay)
{
    apply_motion_generic(ri, x, y, deltax, deltay, 4);
137 138
}

139 140
void ff_apply_motion_8x8(RoqContext *ri, int x, int y,
                             int deltax, int deltay)
141
{
142
    apply_motion_generic(ri, x, y, deltax, deltay, 8);
143
}