use the right stride
[libav.git] / libavcodec / roqvideo.c
1 /*
2 * Copyright (C) 2003 Mike Melanson
3 * Copyright (C) 2003 Dr. Tim Ferguson
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * 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 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; 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 roqvideo.c
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 static inline void block_copy(unsigned char *out, unsigned char *in,
31 int outstride, int instride, int sz)
32 {
33 int rows = sz;
34 while(rows--) {
35 memcpy(out, in, sz);
36 out += outstride;
37 in += instride;
38 }
39 }
40
41 void ff_apply_vector_2x2(RoqContext *ri, int x, int y, roq_cell *cell)
42 {
43 unsigned char *bptr;
44 int boffs,stride;
45
46 stride = ri->y_stride;
47 boffs = (y * stride) + x;
48
49 bptr = ri->current_frame->data[0] + boffs;
50 bptr[0 ] = cell->y[0];
51 bptr[1 ] = cell->y[1];
52 bptr[stride ] = cell->y[2];
53 bptr[stride+1] = cell->y[3];
54
55 stride = ri->c_stride;
56 bptr = ri->current_frame->data[1] + boffs;
57 bptr[0 ] =
58 bptr[1 ] =
59 bptr[stride ] =
60 bptr[stride+1] = cell->u;
61
62 bptr = ri->current_frame->data[2] + boffs;
63 bptr[0 ] =
64 bptr[1 ] =
65 bptr[stride ] =
66 bptr[stride+1] = cell->v;
67 }
68
69 void ff_apply_vector_4x4(RoqContext *ri, int x, int y, roq_cell *cell)
70 {
71 unsigned char *bptr;
72 int boffs,stride;
73
74 stride = ri->y_stride;
75 boffs = (y * stride) + x;
76
77 bptr = ri->current_frame->data[0] + boffs;
78 bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] = cell->y[0];
79 bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] = cell->y[1];
80 bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] = cell->y[2];
81 bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->y[3];
82
83 stride = ri->c_stride;
84 bptr = ri->current_frame->data[1] + boffs;
85 bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] =
86 bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] =
87 bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] =
88 bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->u;
89
90 bptr = ri->current_frame->data[2] + boffs;
91 bptr[ 0] = bptr[ 1] = bptr[stride ] = bptr[stride +1] =
92 bptr[ 2] = bptr[ 3] = bptr[stride +2] = bptr[stride +3] =
93 bptr[stride*2 ] = bptr[stride*2+1] = bptr[stride*3 ] = bptr[stride*3+1] =
94 bptr[stride*2+2] = bptr[stride*2+3] = bptr[stride*3+2] = bptr[stride*3+3] = cell->v;
95 }
96
97
98 static inline void apply_motion_generic(RoqContext *ri, int x, int y, int deltax,
99 int deltay, int sz)
100 {
101 int mx, my, cp;
102
103 mx = x + deltax;
104 my = y + deltay;
105
106 /* check MV against frame boundaries */
107 if ((mx < 0) || (mx > ri->width - sz) ||
108 (my < 0) || (my > ri->height - sz)) {
109 av_log(ri->avctx, AV_LOG_ERROR, "motion vector out of bounds: MV = (%d, %d), boundaries = (0, 0, %d, %d)\n",
110 mx, my, ri->width, ri->height);
111 return;
112 }
113
114 for(cp = 0; cp < 3; cp++) {
115 int stride = ri->current_frame->linesize[cp];
116 block_copy(ri->current_frame->data[cp] + (y*stride) + x,
117 ri->last_frame->data[cp] + (my*stride) + mx,
118 stride, stride, sz);
119 }
120
121 }
122
123
124 void ff_apply_motion_4x4(RoqContext *ri, int x, int y,
125 int deltax, int deltay)
126 {
127 apply_motion_generic(ri, x, y, deltax, deltay, 4);
128 }
129
130 void ff_apply_motion_8x8(RoqContext *ri, int x, int y,
131 int deltax, int deltay)
132 {
133 apply_motion_generic(ri, x, y, deltax, deltay, 8);
134 }