about summary refs log tree commit diff
path: root/converter/ppm/ppmtompeg/subsample.c
blob: e411feb416d750966d84b23b24635460af6c82e4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*===========================================================================*
 * subsample.c                                   *
 *                                       *
 *  Procedures concerned with subsampling                    *
 *                                       *
 * EXPORTED PROCEDURES:                              *
 *  LumMotionErrorA                              *
 *  LumMotionErrorB                              *
 *  LumMotionErrorC                              *
 *  LumMotionErrorD                              *
 *                                       *
 *===========================================================================*/

/*
 * Copyright (c) 1995 The Regents of the University of California.
 * All rights reserved.
 *
 * Permission to use, copy, modify, and distribute this software and its
 * documentation for any purpose, without fee, and without written agreement is
 * hereby granted, provided that the above copyright notice and the following
 * two paragraphs appear in all copies of this software.
 *
 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 */

/*==============*
 * HEADER FILES *
 *==============*/

#include "pm_c_util.h"
#include "all.h"
#include "mtypes.h"
#include "frames.h"
#include "bitio.h"
#include "prototypes.h"

#include "subsample.h"


static void
computePrevFyFx(MpegFrame * const prevFrame,
                int         const by,
                int         const bx,
                vector      const m,
                uint8 ***   const prevP,
                int *       const fyP,
                int *       const fxP) {

    boolean const xHalf = (ABS(m.x) % 2 == 1);
    boolean const yHalf = (ABS(m.y) % 2 == 1);

    MotionToFrameCoord(by, bx, m.y/2, m.x/2, fyP, fxP);

    if (xHalf) {
        if (m.x < 0)
            --*fxP;

        if (yHalf) {
            if (m.y < 0)
                --*fyP;

            *prevP = prevFrame->halfBoth;
        } else
            *prevP = prevFrame->halfX;
    } else if (yHalf) {
        if (m.y < 0)
            --*fyP;

        *prevP = prevFrame->halfY;
    } else
        *prevP = prevFrame->ref_y;
}



static int32
evenColDiff(const uint8 * const macross,
            const int32 * const currentRow) {

    return 0
        + ABS(macross[ 0] - currentRow[ 0])
        + ABS(macross[ 2] - currentRow[ 2])
        + ABS(macross[ 4] - currentRow[ 4])
        + ABS(macross[ 6] - currentRow[ 6])
        + ABS(macross[ 8] - currentRow[ 8])
        + ABS(macross[10] - currentRow[10])
        + ABS(macross[12] - currentRow[12])
        + ABS(macross[14] - currentRow[14]);
}



static int32
oddColDiff(const uint8 * const macross,
           const int32 * const currentRow) {

    return 0
        + ABS(macross[ 1] - currentRow[ 1])
        + ABS(macross[ 3] - currentRow[ 3])
        + ABS(macross[ 5] - currentRow[ 5])
        + ABS(macross[ 7] - currentRow[ 7])
        + ABS(macross[ 9] - currentRow[ 9])
        + ABS(macross[11] - currentRow[11])
        + ABS(macross[13] - currentRow[13])
        + ABS(macross[15] - currentRow[15]);
}



/*===========================================================================*
 *
 * LumMotionErrorA
 *
 *  compute the motion error for the A subsampling pattern
 *
 * RETURNS: the error, or some number greater if it is worse
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int32
LumMotionErrorA(const LumBlock * const currentBlockP,
                MpegFrame *      const prevFrame,
                int              const by,
                int              const bx,
                vector           const m,
                int32            const bestSoFar) {

    int32 diff; /* max value of diff is 255*256 = 65280 */
    uint8 ** prev;
    int fy, fx;
    unsigned int rowNumber;

    computePrevFyFx(prevFrame, by, bx, m, &prev, &fy, &fx);

    diff = 0;  /* initial value */

    for (rowNumber = 0; rowNumber < 16; rowNumber +=2) {
        uint8 *       const macross    = &(prev[fy + rowNumber][fx]);
        const int32 * const currentRow = currentBlockP->l[rowNumber];

        diff += evenColDiff(macross, currentRow);

        if (diff > bestSoFar)
            return diff;
    }
    return diff;
}



/*===========================================================================*
 *
 * LumMotionErrorB
 *
 *  compute the motion error for the B subsampling pattern
 *
 * RETURNS: the error, or some number greater if it is worse
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int32
LumMotionErrorB(const LumBlock * const currentBlockP,
                MpegFrame *      const prevFrame,
                int              const by,
                int              const bx,
                vector           const m,
                int32            const bestSoFar) {

    int32 diff;  /* max value of diff is 255*256 = 65280 */
    uint8 **prev;
    int fy, fx;
    unsigned int rowNumber;

    computePrevFyFx(prevFrame, by, bx, m, &prev, &fy, &fx);

    diff = 0;  /* initial value */

    for (rowNumber = 0; rowNumber < 16; rowNumber +=2) {
        uint8 *       const macross    = &(prev[fy + rowNumber][fx]);
        const int32 * const currentRow = currentBlockP->l[rowNumber];

        diff += oddColDiff(macross, currentRow);

        if (diff > bestSoFar)
            return diff;
    }
    return diff;
}



/*===========================================================================*
 *
 * LumMotionErrorC
 *
 *  compute the motion error for the C subsampling pattern
 *
 * RETURNS: the error, or some number greater if it is worse
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int32
LumMotionErrorC(const LumBlock * const currentBlockP,
                MpegFrame *      const prevFrame,
                int              const by,
                int              const bx,
                vector           const m,
                int32            const bestSoFar) {

    int32 diff;        /* max value of diff is 255*256 = 65280 */
    uint8 **prev;
    int fy, fx;
    unsigned int rowNumber;

    computePrevFyFx(prevFrame, by, bx, m, &prev, &fy, &fx);

    diff = 0;  /* initial value */

    for (rowNumber = 1; rowNumber < 16; rowNumber +=2) {
        uint8 *       const macross    = &(prev[fy + rowNumber][fx]);
        const int32 * const currentRow = currentBlockP->l[rowNumber];

        diff += evenColDiff(macross, currentRow);

        if (diff > bestSoFar)
            return diff;
    }
    return diff;
}



/*===========================================================================*
 *
 * LumMotionErrorD
 *
 *  compute the motion error for the D subsampling pattern
 *
 * RETURNS: the error, or some number greater if it is worse
 *
 * SIDE EFFECTS:    none
 *
 *===========================================================================*/
int32
LumMotionErrorD(const LumBlock * const currentBlockP,
                MpegFrame *      const prevFrame,
                int              const by,
                int              const bx,
                vector           const m,
                int32            const bestSoFar) {

    int32 diff;     /* max value of diff is 255*256 = 65280 */
    uint8 ** prev;
    int fy, fx;
    unsigned int rowNumber;

    computePrevFyFx(prevFrame, by, bx, m, &prev, &fy, &fx);

    diff = 0;  /* initial value */

    for (rowNumber = 1; rowNumber < 16; rowNumber +=2) {
        uint8 *       const macross    = &(prev[fy + rowNumber][fx]);
        const int32 * const currentRow = currentBlockP->l[rowNumber];

        diff += oddColDiff(macross, currentRow);

        if (diff > bestSoFar)
            return diff;
    }
    return diff;
}