summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/codecs/m4v_h263/dec/src/get_pred_outside.cpp
blob: 9cd9022e6665fe24d6d58899c7b056a0f075f4bd (plain)
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
/* ------------------------------------------------------------------
 * Copyright (C) 1998-2009 PacketVideo
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
 * express or implied.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 * -------------------------------------------------------------------
 */
/*
------------------------------------------------------------------------------
 INPUT AND OUTPUT DEFINITIONS

 Inputs:
    xpos = x half-pixel of (x,y) coordinates within a VOP; motion
           compensated coordinates; native data type
    ypos = y half-pixel of (x,y) coordinates within a VOP; motion
           compensated coordinates; native data type
    comp = pointer to 8-bit compensated prediction values within a VOP;
           computed by this module (i/o); full-pel resolution; 8-bit data
    c_prev = pointer to previous 8-bit prediction values within a VOP;
         values range from (0-255); full-pel resolution; 8-bit data
    sh_d = pointer to residual values used to compensate the predicted
           value; values range from (-512 to 511); full-pel resolution;
           native data type
    width = width of the VOP in pixels (x axis); full-pel resolution;
        native data type
    height = height of the VOP in pixels (y axis); full-pel resolution;
         native data type
    rnd1 = rounding value for case when one dimension uses half-pel
           resolution; native data type
    rnd2 = rounding value for case when two dimensions uses half-pel
           resolution; native data type

 Outputs:
    returns 1

 Local Stores/Buffers/Pointers Needed:
    None

 Global Stores/Buffers/Pointers Needed:
    None

 Pointers and Buffers Modified:
    comp = buffer contains newly computed compensated prediction values

 Local Stores Modified:
    None

 Global Stores Modified:
    None

------------------------------------------------------------------------------
 FUNCTION DESCRIPTION

 Summary:

 This function performs motion compensated prediction for the case where
 the motion vector points to a block outside the VOP. The function interpolates
 the pixels that are outside the VOP using the boundary pixels for the block.
 Once the values are interpolated, the pixel values are computed for a block
 in the current VOP. The prediction values are generated by averaging pixel
 values in the previous VOP; the block position in the previous frame is
 computed from the current block's motion vector. The computed pixel values
 are calculated by adding the prediction values to the block residual values.

 Details:

 First, this functions determines which VOP boundary(ies) the motion vector
 is outside, i.e., left, right, top, bottom. xpos is compared to the left and
 right boundaries; ypos is compared to the top and bottom boundaries. The number
 of block pixels inside the the boundary in the x and y directions are stored
 in endx and endy, respectively. If the entire block is inside the x or y
 boundary, the respectively end is set to 0.

 After the boundaries are tested, any pixels lying outside a boundary are
 interpolated from the boundary pixels. For example, if the block is outside the
 bottom boundary, boundary pixels alone the bottom of the VOP as used to
 interpolated those pixels lying outside the bottom boundary. The interpolation
 used is a simple column-wise or row-wise copy of the boundary pixels (inside the
 block) depending on which boundary the block is outside. In our example, each
 boundary pixel would be copied column-wise to the pixel beneath it. If the
 block was outside right boundary, the boundary pixels would be copied row-wise
 to the pixel to the right of it. If the block was outside both an x and y
 boundary, the boundary pixels would be copied row-wise for the portion of the
 block outside the x boundary, and column-wise for the portion of the block
 outside the y boundary. And so on.

 Once the pixel interpolation is complete, the motion compensated output values
 (comp[]) are calculed from the motion compensated prediction (pred[])values and
 the residual values (sh_d[]) of the current frame. The prediction values are
 generated by averaging pixel values in the previous VOP; the block position in
 the previous frame is computed from the current block's motion vector. The
 computed pixel values are calculated by adding the prediction values to the
 block residual values.

*/

/*----------------------------------------------------------------------------
; INCLUDES
----------------------------------------------------------------------------*/
#include "mp4dec_lib.h"
#include "motion_comp.h"

#define PAD_CORNER {    temp = *prev; \
            temp |= (temp<<8);  \
            temp |= (temp<<16); \
            *((uint32*)ptr) = temp; \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  \
            *((uint32*)(ptr+=16)) = temp;  \
            *((uint32*)(ptr+4)) = temp;  }

#define PAD_ROW  {  temp = *((uint32*)prev); \
                    temp2 = *((uint32*)(prev+4)); \
            *((uint32*)ptr) =  temp;\
            *((uint32*)(ptr+4)) =  temp2; \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;\
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp2;}

#define PAD_EXTRA_4x8           {   temp = *((uint32*)(prev+8)); \
                *((uint32*)ptr) =  temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; \
                *((uint32*)(ptr+=16)) = temp; }

#define PAD_COL { temp = *prev; \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)ptr) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp; \
            temp = *(prev+=16); \
            temp|=(temp<<8);  temp|=(temp<<16); \
            *((uint32*)(ptr+=16)) = temp; \
            *((uint32*)(ptr+4)) = temp;}

/* copy 8x8 block */
#define COPY_BLOCK  {           *((uint32*)ptr) = *((uint32*)prev); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4));  }

#define COPY_12x8       {       *((uint32*)ptr) = *((uint32*)prev); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); \
            *((uint32*)(ptr+=16)) = *((uint32*)(prev+=width)); \
            *((uint32*)(ptr+4)) = *((uint32*)(prev+4)); \
            *((uint32*)(ptr+8)) = *((uint32*)(prev+8)); }

/*----------------------------------------------------------------------------
; FUNCTION CODE
----------------------------------------------------------------------------*/
int GetPredOutside(
    int xpos,       /* i */
    int ypos,       /* i */
    uint8 *c_prev,      /* i */
    uint8 *pred_block,      /* i */
    int width,      /* i */
    int height,     /* i */
    int rnd1,       /* i */
    int pred_width
)
{
    /*----------------------------------------------------------------------------
    ; Define all local variables
    ----------------------------------------------------------------------------*/
    uint8   *prev;      /* pointers to adjacent pixels in the    */
    uint8   pred[256];  /* storage for padded pixel values, 16x16 */
    uint8   *ptr;
    int xoffset;
    uint32 temp, temp2;

    /*----------------------------------------------------------------------------
    ; Function body here
    ----------------------------------------------------------------------------*/
    /* saturate xpos and ypos */
    if (xpos < -16) xpos = -16;
    if (xpos > ((width - 1) << 1)) xpos = (width - 1) << 1;
    if (ypos < -16) ypos = -16;
    if (ypos > ((height - 1) << 1)) ypos = (height - 1) << 1;

    if (xpos < 0)
    {
        if (ypos < 0) /* pad top left of frame */
        {
            /* copy the block */
            ptr = pred + (8 << 4) + 8;
            prev = c_prev;
            COPY_BLOCK

            /* pad the corner */
            ptr = pred;
            prev = pred + (8 << 4) + 8;
            PAD_CORNER

            /* pad top */
            ptr = pred + 8;
            prev = pred + (8 << 4) + 8;
            PAD_ROW

            /* pad left */
            ptr = pred + (8 << 4);
            prev = pred + (8 << 4) + 8;
            PAD_COL


            ptr = pred + (((ypos >> 1) + 8) << 4) + (xpos >> 1) + 8;

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
        else if ((ypos >> 1) < (height - B_SIZE)) /* pad left of frame */
        {
            /* copy block */
            ptr = pred + 8;
            prev = c_prev + (ypos >> 1) * width;
            COPY_BLOCK
            /* copy extra line */
            *((uint32*)(ptr += 16)) = *((uint32*)(prev += width));
            *((uint32*)(ptr + 4)) = *((uint32*)(prev + 4));

            /* pad left */
            ptr = pred;
            prev = pred + 8;
            PAD_COL
            /* pad extra line */
            temp = *(prev += 16);
            temp |= (temp << 8);
            temp |= (temp << 16);
            *((uint32*)(ptr += 16)) = temp;
            *((uint32*)(ptr + 4)) = temp;

            ptr = pred + 8 + (xpos >> 1);

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
        else /* pad bottom left of frame */
        {
            /* copy the block */
            ptr = pred + 8; /* point to the center */
            prev = c_prev + width * (height - 8);
            COPY_BLOCK

            /* pad the corner */
            ptr = pred + (8 << 4);
            prev = ptr - 8;
            PAD_CORNER

            /* pad bottom */
            ptr = pred + (8 << 4) + 8;
            prev = ptr - 16;
            PAD_ROW

            /* pad left */
            ptr = pred ;
            prev = ptr + 8;
            PAD_COL

            ptr = pred + 8 + (((ypos >> 1) - (height - 8)) << 4) + (xpos >> 1);

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
    }
    else if ((xpos >> 1) < (width - B_SIZE))
    {
        if (ypos < 0) /* pad top of frame */
        {
            xoffset = xpos >> 1;
            xoffset = xoffset & 0x3; /* word align ptr */

            /* copy block */
            ptr = pred + (8 << 4);
            prev = c_prev + (xpos >> 1) - xoffset;

            if (xoffset || (xpos&1)) /* copy extra 4x8 */
            {
                COPY_12x8
            }
            else
            {
                COPY_BLOCK
            }

            /* pad top */
            ptr = pred;
            prev = pred + (8 << 4);
            PAD_ROW
            if (xoffset || (xpos&1)) /* pad extra 4x8 */
            {
                ptr = pred + 8;
                PAD_EXTRA_4x8
            }

            ptr = pred + (((ypos >> 1) + 8) << 4) + xoffset;

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
        else /* pad bottom of frame */
        {
            xoffset = xpos >> 1;
            xoffset = xoffset & 0x3; /* word align ptr */
            /* copy block */
            ptr = pred ;
            prev = c_prev + width * (height - 8) + (xpos >> 1) - xoffset;
            if (xoffset  || (xpos&1))
            {
                COPY_12x8
            }
            else
            {
                COPY_BLOCK
            }

            /* pad bottom */
            ptr = pred + (8 << 4);
            prev = ptr - 16;
            PAD_ROW
            if (xoffset || (xpos&1))
            {
                ptr = pred + (8 << 4) + 8;
                PAD_EXTRA_4x8
            }

            ptr = pred + (((ypos >> 1) - (height - 8)) << 4) + xoffset;

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
    }
    else
    {
        if (ypos < 0) /* pad top right of frame */
        {
            /* copy block */
            ptr = pred + (8 << 4);
            prev = c_prev + width - 8;
            COPY_BLOCK

            /* pad top-right */
            ptr = pred + 8;
            prev = pred + (8 << 4) + 7;
            PAD_CORNER

            /* pad top */
            ptr = pred ;
            prev = pred + (8 << 4);
            PAD_ROW;

            /* pad right */
            ptr = pred + (8 << 4) + 8;
            prev = ptr - 1;
            PAD_COL;

            ptr = pred + ((8 + (ypos >> 1)) << 4) + (8 - (width - (xpos >> 1)));

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
        else if ((ypos >> 1) < (height - B_SIZE)) /* pad right of frame */
        {
            /* copy block */
            ptr = pred;
            prev = c_prev + (ypos >> 1) * width + width - 8;
            COPY_BLOCK
            /* copy extra line */
            *((uint32*)(ptr += 16)) = *((uint32*)(prev += width));
            *((uint32*)(ptr + 4)) = *((uint32*)(prev + 4));

            /* pad right */
            ptr = pred + 8;
            prev = ptr - 1;
            PAD_COL;
            /* pad extra line */
            temp = *(prev += 16);
            temp |= (temp << 8);
            temp |= (temp << 16);
            *((uint32*)(ptr += 16)) = temp;
            *((uint32*)(ptr + 4)) = temp;


            ptr = pred + 8 - (width - (xpos >> 1));

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;

        }
        else /* pad bottom right of frame */
        {
            /* copy block */
            ptr = pred;
            prev = c_prev + width * (height - 8) + width - 8;
            COPY_BLOCK

            /* pad bottom-right */
            ptr = pred + (8 << 4) + 8;
            prev = ptr - 17;
            PAD_CORNER

            /* pad right */
            ptr = pred + 8;
            prev = ptr - 1;
            PAD_COL

            /* pad bottom */
            ptr = pred + (8 << 4);
            prev = ptr - 16;
            PAD_ROW

            ptr = pred + 8 - (width - (xpos >> 1)) + ((8 - (height - (ypos >> 1))) << 4);

            GetPredAdvBTable[ypos&1][xpos&1](ptr, pred_block, 16, (pred_width << 1) | rnd1);

            return 1;
        }
    }
}