postproc.c 43 KB
Newer Older
John Koleszar's avatar
John Koleszar committed
1
/*
2
 *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar's avatar
John Koleszar committed
3
 *
4
 *  Use of this source code is governed by a BSD-style license
5
6
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
7
 *  in the file PATENTS.  All contributing project authors may
8
 *  be found in the AUTHORS file in the root of the source tree.
John Koleszar's avatar
John Koleszar committed
9
10
11
 */


12
#include "vpx_config.h"
John Koleszar's avatar
John Koleszar committed
13
#include "vpx_rtcd.h"
John Koleszar's avatar
John Koleszar committed
14
15
#include "vpx_scale/yv12config.h"
#include "postproc.h"
16
#include "common.h"
John Koleszar's avatar
John Koleszar committed
17
18
19
#include "vpx_scale/yv12extend.h"
#include "vpx_scale/vpxscale.h"
#include "systemdependent.h"
20
#include "../encoder/variance.h"
John Koleszar's avatar
John Koleszar committed
21

22
#include <limits.h>
John Koleszar's avatar
John Koleszar committed
23
24
25
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
26
27
28
29
30
31

#define RGB_TO_YUV(t)                                                                       \
    ( (0.257*(float)(t>>16)) + (0.504*(float)(t>>8&0xff)) + (0.098*(float)(t&0xff)) + 16),  \
    (-(0.148*(float)(t>>16)) - (0.291*(float)(t>>8&0xff)) + (0.439*(float)(t&0xff)) + 128), \
    ( (0.439*(float)(t>>16)) - (0.368*(float)(t>>8&0xff)) - (0.071*(float)(t&0xff)) + 128)

32
/* global constants */
33
#define MFQE_PRECISION 4
34
#if CONFIG_POSTPROC_VISUALIZER
35
36
static const unsigned char MB_PREDICTION_MODE_colors[MB_MODE_COUNT][3] =
{
37
38
39
40
41
42
43
44
45
46
    { RGB_TO_YUV(0x98FB98) },   /* PaleGreen */
    { RGB_TO_YUV(0x00FF00) },   /* Green */
    { RGB_TO_YUV(0xADFF2F) },   /* GreenYellow */
    { RGB_TO_YUV(0x228B22) },   /* ForestGreen */
    { RGB_TO_YUV(0x006400) },   /* DarkGreen */
    { RGB_TO_YUV(0x98F5FF) },   /* Cadet Blue */
    { RGB_TO_YUV(0x6CA6CD) },   /* Sky Blue */
    { RGB_TO_YUV(0x00008B) },   /* Dark blue */
    { RGB_TO_YUV(0x551A8B) },   /* Purple */
    { RGB_TO_YUV(0xFF0000) }    /* Red */
47
48
};

49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
static const unsigned char B_PREDICTION_MODE_colors[B_MODE_COUNT][3] =
{
    { RGB_TO_YUV(0x6633ff) },   /* Purple */
    { RGB_TO_YUV(0xcc33ff) },   /* Magenta */
    { RGB_TO_YUV(0xff33cc) },   /* Pink */
    { RGB_TO_YUV(0xff3366) },   /* Coral */
    { RGB_TO_YUV(0x3366ff) },   /* Blue */
    { RGB_TO_YUV(0xed00f5) },   /* Dark Blue */
    { RGB_TO_YUV(0x2e00b8) },   /* Dark Purple */
    { RGB_TO_YUV(0xff6633) },   /* Orange */
    { RGB_TO_YUV(0x33ccff) },   /* Light Blue */
    { RGB_TO_YUV(0x8ab800) },   /* Green */
    { RGB_TO_YUV(0xffcc33) },   /* Light Orange */
    { RGB_TO_YUV(0x33ffcc) },   /* Aqua */
    { RGB_TO_YUV(0x66ff33) },   /* Light Green */
    { RGB_TO_YUV(0xccff33) },   /* Yellow */
};

67
static const unsigned char MV_REFERENCE_FRAME_colors[MAX_REF_FRAMES][3] =
68
{
69
70
71
72
    { RGB_TO_YUV(0x00ff00) },   /* Blue */
    { RGB_TO_YUV(0x0000ff) },   /* Green */
    { RGB_TO_YUV(0xffff00) },   /* Yellow */
    { RGB_TO_YUV(0xff0000) },   /* Red */
73
};
74
#endif
75

John Koleszar's avatar
John Koleszar committed
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
static const short kernel5[] =
{
    1, 1, 4, 1, 1
};

const short vp8_rv[] =
{
    8, 5, 2, 2, 8, 12, 4, 9, 8, 3,
    0, 3, 9, 0, 0, 0, 8, 3, 14, 4,
    10, 1, 11, 14, 1, 14, 9, 6, 12, 11,
    8, 6, 10, 0, 0, 8, 9, 0, 3, 14,
    8, 11, 13, 4, 2, 9, 0, 3, 9, 6,
    1, 2, 3, 14, 13, 1, 8, 2, 9, 7,
    3, 3, 1, 13, 13, 6, 6, 5, 2, 7,
    11, 9, 11, 8, 7, 3, 2, 0, 13, 13,
    14, 4, 12, 5, 12, 10, 8, 10, 13, 10,
    4, 14, 4, 10, 0, 8, 11, 1, 13, 7,
    7, 14, 6, 14, 13, 2, 13, 5, 4, 4,
    0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
    8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
    3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
    3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
    13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
    5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
    9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
    4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
    3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
    11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
    5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
    0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
    10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
    4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
    0, 10, 0, 5, 13, 2, 12, 7, 11, 13,
    8, 0, 4, 10, 7, 2, 7, 2, 2, 5,
    3, 4, 7, 3, 3, 14, 14, 5, 9, 13,
    3, 14, 3, 6, 3, 0, 11, 8, 13, 1,
    13, 1, 12, 0, 10, 9, 7, 6, 2, 8,
    5, 2, 13, 7, 1, 13, 14, 7, 6, 7,
    9, 6, 10, 11, 7, 8, 7, 5, 14, 8,
    4, 4, 0, 8, 7, 10, 0, 8, 14, 11,
    3, 12, 5, 7, 14, 3, 14, 5, 2, 6,
    11, 12, 12, 8, 0, 11, 13, 1, 2, 0,
    5, 10, 14, 7, 8, 0, 4, 11, 0, 8,
    0, 3, 10, 5, 8, 0, 11, 6, 7, 8,
    10, 7, 13, 9, 2, 5, 1, 5, 10, 2,
    4, 3, 5, 6, 10, 8, 9, 4, 11, 14,
    3, 8, 3, 7, 8, 5, 11, 4, 12, 3,
    11, 9, 14, 8, 14, 13, 4, 3, 1, 2,
    14, 6, 5, 4, 4, 11, 4, 6, 2, 1,
    5, 8, 8, 12, 13, 5, 14, 10, 12, 13,
    0, 9, 5, 5, 11, 10, 13, 9, 10, 13,
};

extern void vp8_blit_text(const char *msg, unsigned char *address, const int pitch);
130
extern void vp8_blit_line(int x0, int x1, int y0, int y1, unsigned char *image, const int pitch);
John Koleszar's avatar
John Koleszar committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/***********************************************************************************************************
 */
void vp8_post_proc_down_and_across_c
(
    unsigned char *src_ptr,
    unsigned char *dst_ptr,
    int src_pixels_per_line,
    int dst_pixels_per_line,
    int rows,
    int cols,
    int flimit
)
{
    unsigned char *p_src, *p_dst;
    int row;
    int col;
    int i;
    int v;
    int pitch = src_pixels_per_line;
    unsigned char d[8];
    (void)dst_pixels_per_line;

    for (row = 0; row < rows; row++)
    {
155
        /* post_proc_down for one row */
John Koleszar's avatar
John Koleszar committed
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
        p_src = src_ptr;
        p_dst = dst_ptr;

        for (col = 0; col < cols; col++)
        {

            int kernel = 4;
            int v = p_src[col];

            for (i = -2; i <= 2; i++)
            {
                if (abs(v - p_src[col+i*pitch]) > flimit)
                    goto down_skip_convolve;

                kernel += kernel5[2+i] * p_src[col+i*pitch];
            }

            v = (kernel >> 3);
        down_skip_convolve:
            p_dst[col] = v;
        }

178
        /* now post_proc_across */
John Koleszar's avatar
John Koleszar committed
179
180
181
        p_src = dst_ptr;
        p_dst = dst_ptr;

182
183
184
185
186
187
        for (i = -8; i<0; i++)
          p_src[i]=p_src[0];

        for (i = cols; i<cols+8; i++)
          p_src[i]=p_src[cols-1];

John Koleszar's avatar
John Koleszar committed
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
        for (i = 0; i < 8; i++)
            d[i] = p_src[i];

        for (col = 0; col < cols; col++)
        {
            int kernel = 4;
            v = p_src[col];

            d[col&7] = v;

            for (i = -2; i <= 2; i++)
            {
                if (abs(v - p_src[col+i]) > flimit)
                    goto across_skip_convolve;

                kernel += kernel5[2+i] * p_src[col+i];
            }

            d[col&7] = (kernel >> 3);
        across_skip_convolve:

            if (col >= 2)
                p_dst[col-2] = d[(col-2)&7];
        }

213
        /* handle the last two pixels */
John Koleszar's avatar
John Koleszar committed
214
215
216
217
        p_dst[col-2] = d[(col-2)&7];
        p_dst[col-1] = d[(col-1)&7];


218
        /* next row */
John Koleszar's avatar
John Koleszar committed
219
220
221
222
223
        src_ptr += pitch;
        dst_ptr += pitch;
    }
}

224
static int q2mbl(int x)
John Koleszar's avatar
John Koleszar committed
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
{
    if (x < 20) x = 20;

    x = 50 + (x - 50) * 10 / 8;
    return x * x / 3;
}
void vp8_mbpost_proc_across_ip_c(unsigned char *src, int pitch, int rows, int cols, int flimit)
{
    int r, c, i;

    unsigned char *s = src;
    unsigned char d[16];

    for (r = 0; r < rows; r++)
    {
        int sumsq = 0;
        int sum   = 0;

243
244
245
246
247
248
249
250
        for (i = -8; i<0; i++)
          s[i]=s[0];

        // 17 avoids valgrind warning - we buffer values in c in d
        // and only write them when we've read 8 ahead...
        for (i = cols; i<cols+17; i++)
          s[i]=s[cols-1];

John Koleszar's avatar
John Koleszar committed
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
        for (i = -8; i <= 6; i++)
        {
            sumsq += s[i] * s[i];
            sum   += s[i];
            d[i+8] = 0;
        }

        for (c = 0; c < cols + 8; c++)
        {
            int x = s[c+7] - s[c-8];
            int y = s[c+7] + s[c-8];

            sum  += x;
            sumsq += x * y;

            d[c&15] = s[c];

            if (sumsq * 15 - sum * sum < flimit)
            {
                d[c&15] = (8 + sum + s[c]) >> 4;
            }

            s[c-8] = d[(c-8)&15];
        }

        s += pitch;
    }
}





void vp8_mbpost_proc_down_c(unsigned char *dst, int pitch, int rows, int cols, int flimit)
{
    int r, c, i;
    const short *rv3 = &vp8_rv[63&rand()];

289
    for (c = 0; c < cols; c++ )
John Koleszar's avatar
John Koleszar committed
290
291
292
293
294
295
296
    {
        unsigned char *s = &dst[c];
        int sumsq = 0;
        int sum   = 0;
        unsigned char d[16];
        const short *rv2 = rv3 + ((c * 17) & 127);

297
298
299
300
301
302
303
304
        for (i = -8; i < 0; i++)
          s[i*pitch]=s[0];

        // 17 avoids valgrind warning - we buffer values in c in d
        // and only write them when we've read 8 ahead...
        for (i = rows; i < rows+17; i++)
          s[i*pitch]=s[(rows-1)*pitch];

John Koleszar's avatar
John Koleszar committed
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
        for (i = -8; i <= 6; i++)
        {
            sumsq += s[i*pitch] * s[i*pitch];
            sum   += s[i*pitch];
        }

        for (r = 0; r < rows + 8; r++)
        {
            sumsq += s[7*pitch] * s[ 7*pitch] - s[-8*pitch] * s[-8*pitch];
            sum  += s[7*pitch] - s[-8*pitch];
            d[r&15] = s[0];

            if (sumsq * 15 - sum * sum < flimit)
            {
                d[r&15] = (rv2[r&127] + sum + s[0]) >> 4;
            }

            s[-8*pitch] = d[(r-8)&15];
            s += pitch;
        }
    }
}


static void vp8_deblock_and_de_macro_block(YV12_BUFFER_CONFIG         *source,
        YV12_BUFFER_CONFIG         *post,
        int                         q,
        int                         low_var_thresh,
John Koleszar's avatar
John Koleszar committed
333
        int                         flag)
John Koleszar's avatar
John Koleszar committed
334
335
336
337
338
339
{
    double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
    int ppl = (int)(level + .5);
    (void) low_var_thresh;
    (void) flag;

John Koleszar's avatar
John Koleszar committed
340
341
342
    vp8_post_proc_down_and_across(source->y_buffer, post->y_buffer, source->y_stride,  post->y_stride, source->y_height, source->y_width,  ppl);
    vp8_mbpost_proc_across_ip(post->y_buffer, post->y_stride, post->y_height, post->y_width, q2mbl(q));
    vp8_mbpost_proc_down(post->y_buffer, post->y_stride, post->y_height, post->y_width, q2mbl(q));
John Koleszar's avatar
John Koleszar committed
343

John Koleszar's avatar
John Koleszar committed
344
345
    vp8_post_proc_down_and_across(source->u_buffer, post->u_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
    vp8_post_proc_down_and_across(source->v_buffer, post->v_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
John Koleszar's avatar
John Koleszar committed
346
347
348

}

Luca Barbato's avatar
Luca Barbato committed
349
void vp8_deblock(YV12_BUFFER_CONFIG         *source,
350
351
352
                 YV12_BUFFER_CONFIG         *post,
                 int                         q,
                 int                         low_var_thresh,
John Koleszar's avatar
John Koleszar committed
353
                 int                         flag)
John Koleszar's avatar
John Koleszar committed
354
355
356
357
358
359
{
    double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
    int ppl = (int)(level + .5);
    (void) low_var_thresh;
    (void) flag;

John Koleszar's avatar
John Koleszar committed
360
361
362
    vp8_post_proc_down_and_across(source->y_buffer, post->y_buffer, source->y_stride,  post->y_stride, source->y_height, source->y_width,   ppl);
    vp8_post_proc_down_and_across(source->u_buffer, post->u_buffer, source->uv_stride, post->uv_stride,  source->uv_height, source->uv_width, ppl);
    vp8_post_proc_down_and_across(source->v_buffer, post->v_buffer, source->uv_stride, post->uv_stride, source->uv_height, source->uv_width, ppl);
John Koleszar's avatar
John Koleszar committed
363
364
365
366
367
368
}

void vp8_de_noise(YV12_BUFFER_CONFIG         *source,
                  YV12_BUFFER_CONFIG         *post,
                  int                         q,
                  int                         low_var_thresh,
John Koleszar's avatar
John Koleszar committed
369
                  int                         flag)
John Koleszar's avatar
John Koleszar committed
370
371
372
373
374
375
376
{
    double level = 6.0e-05 * q * q * q - .0067 * q * q + .306 * q + .0065;
    int ppl = (int)(level + .5);
    (void) post;
    (void) low_var_thresh;
    (void) flag;

John Koleszar's avatar
John Koleszar committed
377
    vp8_post_proc_down_and_across(
John Koleszar's avatar
John Koleszar committed
378
379
380
381
382
383
384
        source->y_buffer + 2 * source->y_stride + 2,
        source->y_buffer + 2 * source->y_stride + 2,
        source->y_stride,
        source->y_stride,
        source->y_height - 4,
        source->y_width - 4,
        ppl);
John Koleszar's avatar
John Koleszar committed
385
    vp8_post_proc_down_and_across(
John Koleszar's avatar
John Koleszar committed
386
387
388
389
390
391
        source->u_buffer + 2 * source->uv_stride + 2,
        source->u_buffer + 2 * source->uv_stride + 2,
        source->uv_stride,
        source->uv_stride,
        source->uv_height - 4,
        source->uv_width - 4, ppl);
John Koleszar's avatar
John Koleszar committed
392
    vp8_post_proc_down_and_across(
John Koleszar's avatar
John Koleszar committed
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
        source->v_buffer + 2 * source->uv_stride + 2,
        source->v_buffer + 2 * source->uv_stride + 2,
        source->uv_stride,
        source->uv_stride,
        source->uv_height - 4,
        source->uv_width - 4, ppl);

}

double vp8_gaussian(double sigma, double mu, double x)
{
    return 1 / (sigma * sqrt(2.0 * 3.14159265)) *
           (exp(-(x - mu) * (x - mu) / (2 * sigma * sigma)));
}

extern void (*vp8_clear_system_state)(void);


static void fillrd(struct postproc_state *state, int q, int a)
{
    char char_dist[300];

    double sigma;
    int ai = a, qi = q, i;

    vp8_clear_system_state();


    sigma = ai + .5 + .6 * (63 - qi) / 63.0;

423
424
425
    /* set up a lookup table of 256 entries that matches
     * a gaussian distribution with sigma determined by q.
     */
John Koleszar's avatar
John Koleszar committed
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
515
    {
        double i;
        int next, j;

        next = 0;

        for (i = -32; i < 32; i++)
        {
            int a = (int)(.5 + 256 * vp8_gaussian(sigma, 0, i));

            if (a)
            {
                for (j = 0; j < a; j++)
                {
                    char_dist[next+j] = (char) i;
                }

                next = next + j;
            }

        }

        for (next = next; next < 256; next++)
            char_dist[next] = 0;

    }

    for (i = 0; i < 3072; i++)
    {
        state->noise[i] = char_dist[rand() & 0xff];
    }

    for (i = 0; i < 16; i++)
    {
        state->blackclamp[i] = -char_dist[0];
        state->whiteclamp[i] = -char_dist[0];
        state->bothclamp[i] = -2 * char_dist[0];
    }

    state->last_q = q;
    state->last_noise = a;
}

/****************************************************************************
 *
 *  ROUTINE       : plane_add_noise_c
 *
 *  INPUTS        : unsigned char *Start    starting address of buffer to add gaussian
 *                                  noise to
 *                  unsigned int Width    width of plane
 *                  unsigned int Height   height of plane
 *                  int  Pitch    distance between subsequent lines of frame
 *                  int  q        quantizer used to determine amount of noise
 *                                  to add
 *
 *  OUTPUTS       : None.
 *
 *  RETURNS       : void.
 *
 *  FUNCTION      : adds gaussian noise to a plane of pixels
 *
 *  SPECIAL NOTES : None.
 *
 ****************************************************************************/
void vp8_plane_add_noise_c(unsigned char *Start, char *noise,
                           char blackclamp[16],
                           char whiteclamp[16],
                           char bothclamp[16],
                           unsigned int Width, unsigned int Height, int Pitch)
{
    unsigned int i, j;

    for (i = 0; i < Height; i++)
    {
        unsigned char *Pos = Start + i * Pitch;
        char  *Ref = (char *)(noise + (rand() & 0xff));

        for (j = 0; j < Width; j++)
        {
            if (Pos[j] < blackclamp[0])
                Pos[j] = blackclamp[0];

            if (Pos[j] > 255 + whiteclamp[0])
                Pos[j] = 255 + whiteclamp[0];

            Pos[j] += Ref[j];
        }
    }
}

516
517
518
519
/* Blend the macro block with a solid colored square.  Leave the
 * edges unblended to give distinction to macro blocks in areas
 * filled with the same color block.
 */
520
void vp8_blend_mb_inner_c (unsigned char *y, unsigned char *u, unsigned char *v,
521
522
523
524
525
526
527
                        int y1, int u1, int v1, int alpha, int stride)
{
    int i, j;
    int y1_const = y1*((1<<16)-alpha);
    int u1_const = u1*((1<<16)-alpha);
    int v1_const = v1*((1<<16)-alpha);

528
529
    y += 2*stride + 2;
    for (i = 0; i < 12; i++)
530
    {
531
        for (j = 0; j < 12; j++)
532
533
534
535
536
537
        {
            y[j] = (y[j]*alpha + y1_const)>>16;
        }
        y += stride;
    }

538
539
540
541
542
543
    stride >>= 1;

    u += stride + 1;
    v += stride + 1;

    for (i = 0; i < 6; i++)
544
    {
545
        for (j = 0; j < 6; j++)
546
547
548
549
        {
            u[j] = (u[j]*alpha + u1_const)>>16;
            v[j] = (v[j]*alpha + v1_const)>>16;
        }
550
551
        u += stride;
        v += stride;
552
553
    }
}
John Koleszar's avatar
John Koleszar committed
554

555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
/* Blend only the edge of the macro block.  Leave center
 * unblended to allow for other visualizations to be layered.
 */
void vp8_blend_mb_outer_c (unsigned char *y, unsigned char *u, unsigned char *v,
                        int y1, int u1, int v1, int alpha, int stride)
{
    int i, j;
    int y1_const = y1*((1<<16)-alpha);
    int u1_const = u1*((1<<16)-alpha);
    int v1_const = v1*((1<<16)-alpha);

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 16; j++)
        {
            y[j] = (y[j]*alpha + y1_const)>>16;
        }
        y += stride;
    }

    for (i = 0; i < 12; i++)
    {
        y[0]  = (y[0]*alpha  + y1_const)>>16;
        y[1]  = (y[1]*alpha  + y1_const)>>16;
        y[14] = (y[14]*alpha + y1_const)>>16;
        y[15] = (y[15]*alpha + y1_const)>>16;
        y += stride;
    }

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 16; j++)
        {
            y[j] = (y[j]*alpha + y1_const)>>16;
        }
        y += stride;
    }

    stride >>= 1;

    for (j = 0; j < 8; j++)
    {
        u[j] = (u[j]*alpha + u1_const)>>16;
        v[j] = (v[j]*alpha + v1_const)>>16;
    }
    u += stride;
    v += stride;

    for (i = 0; i < 6; i++)
    {
        u[0] = (u[0]*alpha + u1_const)>>16;
        v[0] = (v[0]*alpha + v1_const)>>16;

        u[7] = (u[7]*alpha + u1_const)>>16;
        v[7] = (v[7]*alpha + v1_const)>>16;

        u += stride;
        v += stride;
    }

    for (j = 0; j < 8; j++)
    {
        u[j] = (u[j]*alpha + u1_const)>>16;
        v[j] = (v[j]*alpha + v1_const)>>16;
    }
}

void vp8_blend_b_c (unsigned char *y, unsigned char *u, unsigned char *v,
                        int y1, int u1, int v1, int alpha, int stride)
{
    int i, j;
    int y1_const = y1*((1<<16)-alpha);
    int u1_const = u1*((1<<16)-alpha);
    int v1_const = v1*((1<<16)-alpha);

    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            y[j] = (y[j]*alpha + y1_const)>>16;
        }
        y += stride;
    }

    stride >>= 1;

    for (i = 0; i < 2; i++)
    {
        for (j = 0; j < 2; j++)
        {
            u[j] = (u[j]*alpha + u1_const)>>16;
            v[j] = (v[j]*alpha + v1_const)>>16;
        }
        u += stride;
        v += stride;
    }
}

653
654
static void constrain_line (int x0, int *x1, int y0, int *y1, int width, int height)
{
655
656
    int dx;
    int dy;
657
658
659

    if (*x1 > width)
    {
660
661
662
        dx = *x1 - x0;
        dy = *y1 - y0;

663
        *x1 = width;
664
        if (dx)
665
666
667
668
            *y1 = ((width-x0)*dy)/dx + y0;
    }
    if (*x1 < 0)
    {
669
670
671
        dx = *x1 - x0;
        dy = *y1 - y0;

672
        *x1 = 0;
673
        if (dx)
674
675
676
677
            *y1 = ((0-x0)*dy)/dx + y0;
    }
    if (*y1 > height)
    {
678
679
680
        dx = *x1 - x0;
        dy = *y1 - y0;

681
        *y1 = height;
682
        if (dy)
683
684
685
686
            *x1 = ((height-y0)*dx)/dy + x0;
    }
    if (*y1 < 0)
    {
687
688
689
        dx = *x1 - x0;
        dy = *y1 - y0;

690
        *y1 = 0;
691
        if (dy)
692
693
694
695
            *x1 = ((0-y0)*dx)/dy + x0;
    }
}

696

697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
static void multiframe_quality_enhance_block
(
    int blksize, /* Currently only values supported are 16, 8, 4 */
    int qcurr,
    int qprev,
    unsigned char *y,
    unsigned char *u,
    unsigned char *v,
    int y_stride,
    int uv_stride,
    unsigned char *yd,
    unsigned char *ud,
    unsigned char *vd,
    int yd_stride,
    int uvd_stride
)
{
    static const unsigned char VP8_ZEROS[16]=
    {
         0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
    };
    int blksizeby2 = blksize >> 1;
719
    int qdiff = qcurr - qprev;
720
721
722
723
724
725
726
727
728
729
730
731

    int i, j;
    unsigned char *yp;
    unsigned char *ydp;
    unsigned char *up;
    unsigned char *udp;
    unsigned char *vp;
    unsigned char *vdp;

    unsigned int act, sse, sad, thr;
    if (blksize == 16)
    {
John Koleszar's avatar
John Koleszar committed
732
        act = (vp8_variance16x16(yd, yd_stride, VP8_ZEROS, 0, &sse)+128)>>8;
733
        sad = (vp8_sad16x16(y, y_stride, yd, yd_stride, INT_MAX)+128)>>8;
734
735
736
    }
    else if (blksize == 8)
    {
John Koleszar's avatar
John Koleszar committed
737
        act = (vp8_variance8x8(yd, yd_stride, VP8_ZEROS, 0, &sse)+32)>>6;
738
        sad = (vp8_sad8x8(y, y_stride, yd, yd_stride, INT_MAX)+32)>>6;
739
740
741
    }
    else
    {
John Koleszar's avatar
John Koleszar committed
742
        act = (vp8_variance4x4(yd, yd_stride, VP8_ZEROS, 0, &sse)+8)>>4;
743
        sad = (vp8_sad4x4(y, y_stride, yd, yd_stride, INT_MAX)+8)>>4;
744
    }
745
746
747
748
    /* thr = qdiff/8 + log2(act) + log4(qprev) */
    thr = (qdiff>>3);
    while (act>>=1) thr++;
    while (qprev>>=2) thr++;
749
750
    if (sad < thr)
    {
751
752
        static const int roundoff = (1 << (MFQE_PRECISION - 1));
        int ifactor = (sad << MFQE_PRECISION) / thr;
753
        ifactor >>= (qdiff >> 5);
754
755
756
        // TODO: SIMD optimize this section
        if (ifactor)
        {
757
            int icfactor = (1 << MFQE_PRECISION) - ifactor;
758
759
760
            for (yp = y, ydp = yd, i = 0; i < blksize; ++i, yp += y_stride, ydp += yd_stride)
            {
                for (j = 0; j < blksize; ++j)
761
                    ydp[j] = (int)((yp[j] * ifactor + ydp[j] * icfactor + roundoff) >> MFQE_PRECISION);
762
763
764
765
            }
            for (up = u, udp = ud, i = 0; i < blksizeby2; ++i, up += uv_stride, udp += uvd_stride)
            {
                for (j = 0; j < blksizeby2; ++j)
766
                    udp[j] = (int)((up[j] * ifactor + udp[j] * icfactor + roundoff) >> MFQE_PRECISION);
767
768
769
770
            }
            for (vp = v, vdp = vd, i = 0; i < blksizeby2; ++i, vp += uv_stride, vdp += uvd_stride)
            {
                for (j = 0; j < blksizeby2; ++j)
771
                    vdp[j] = (int)((vp[j] * ifactor + vdp[j] * icfactor + roundoff) >> MFQE_PRECISION);
772
773
774
775
776
777
778
            }
        }
    }
    else
    {
        if (blksize == 16)
        {
John Koleszar's avatar
John Koleszar committed
779
780
781
            vp8_copy_mem16x16(y, y_stride, yd, yd_stride);
            vp8_copy_mem8x8(u, uv_stride, ud, uvd_stride);
            vp8_copy_mem8x8(v, uv_stride, vd, uvd_stride);
782
783
784
        }
        else if (blksize == 8)
        {
John Koleszar's avatar
John Koleszar committed
785
            vp8_copy_mem8x8(y, y_stride, yd, yd_stride);
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
            for (up = u, udp = ud, i = 0; i < blksizeby2; ++i, up += uv_stride, udp += uvd_stride)
                vpx_memcpy(udp, up, blksizeby2);
            for (vp = v, vdp = vd, i = 0; i < blksizeby2; ++i, vp += uv_stride, vdp += uvd_stride)
                vpx_memcpy(vdp, vp, blksizeby2);
        }
        else
        {
            for (yp = y, ydp = yd, i = 0; i < blksize; ++i, yp += y_stride, ydp += yd_stride)
                vpx_memcpy(ydp, yp, blksize);
            for (up = u, udp = ud, i = 0; i < blksizeby2; ++i, up += uv_stride, udp += uvd_stride)
                vpx_memcpy(udp, up, blksizeby2);
            for (vp = v, vdp = vd, i = 0; i < blksizeby2; ++i, vp += uv_stride, vdp += uvd_stride)
                vpx_memcpy(vdp, vp, blksizeby2);
        }
    }
}

void vp8_multiframe_quality_enhance
(
    VP8_COMMON *cm
)
{
    YV12_BUFFER_CONFIG *show = cm->frame_to_show;
    YV12_BUFFER_CONFIG *dest = &cm->post_proc_buffer;

    FRAME_TYPE frame_type = cm->frame_type;
    /* Point at base of Mb MODE_INFO list has motion vectors etc */
    const MODE_INFO *mode_info_context = cm->mi;
    int mb_row;
    int mb_col;
816
817
    int qcurr = cm->base_qindex;
    int qprev = cm->postproc_state.last_base_qindex;
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838

    unsigned char *y_ptr, *u_ptr, *v_ptr;
    unsigned char *yd_ptr, *ud_ptr, *vd_ptr;

    /* Set up the buffer pointers */
    y_ptr = show->y_buffer;
    u_ptr = show->u_buffer;
    v_ptr = show->v_buffer;
    yd_ptr = dest->y_buffer;
    ud_ptr = dest->u_buffer;
    vd_ptr = dest->v_buffer;

    /* postprocess each macro block */
    for (mb_row = 0; mb_row < cm->mb_rows; mb_row++)
    {
        for (mb_col = 0; mb_col < cm->mb_cols; mb_col++)
        {
            /* if motion is high there will likely be no benefit */
            if (((frame_type == INTER_FRAME &&
                  abs(mode_info_context->mbmi.mv.as_mv.row) <= 10 &&
                  abs(mode_info_context->mbmi.mv.as_mv.col) <= 10) ||
839
                 (frame_type == KEY_FRAME)))
840
            {
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
                if (mode_info_context->mbmi.mode == B_PRED || mode_info_context->mbmi.mode == SPLITMV)
                {
                    int i, j;
                    for (i=0; i<2; ++i)
                        for (j=0; j<2; ++j)
                            multiframe_quality_enhance_block(8,
                                                             qcurr,
                                                             qprev,
                                                             y_ptr + 8*(i*show->y_stride+j),
                                                             u_ptr + 4*(i*show->uv_stride+j),
                                                             v_ptr + 4*(i*show->uv_stride+j),
                                                             show->y_stride,
                                                             show->uv_stride,
                                                             yd_ptr + 8*(i*dest->y_stride+j),
                                                             ud_ptr + 4*(i*dest->uv_stride+j),
                                                             vd_ptr + 4*(i*dest->uv_stride+j),
                                                             dest->y_stride,
                                                             dest->uv_stride);
                }
                else
                {
                    multiframe_quality_enhance_block(16,
                                                     qcurr,
                                                     qprev,
                                                     y_ptr,
                                                     u_ptr,
                                                     v_ptr,
                                                     show->y_stride,
                                                     show->uv_stride,
                                                     yd_ptr,
                                                     ud_ptr,
                                                     vd_ptr,
                                                     dest->y_stride,
                                                     dest->uv_stride);

                }
877
878
879
            }
            else
            {
John Koleszar's avatar
John Koleszar committed
880
881
882
                vp8_copy_mem16x16(y_ptr, show->y_stride, yd_ptr, dest->y_stride);
                vp8_copy_mem8x8(u_ptr, show->uv_stride, ud_ptr, dest->uv_stride);
                vp8_copy_mem8x8(v_ptr, show->uv_stride, vd_ptr, dest->uv_stride);
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
            }
            y_ptr += 16;
            u_ptr += 8;
            v_ptr += 8;
            yd_ptr += 16;
            ud_ptr += 8;
            vd_ptr += 8;
            mode_info_context++;     /* step to next MB */
        }

        y_ptr += show->y_stride  * 16 - 16 * cm->mb_cols;
        u_ptr += show->uv_stride *  8 - 8 * cm->mb_cols;
        v_ptr += show->uv_stride *  8 - 8 * cm->mb_cols;
        yd_ptr += dest->y_stride  * 16 - 16 * cm->mb_cols;
        ud_ptr += dest->uv_stride *  8 - 8 * cm->mb_cols;
        vd_ptr += dest->uv_stride *  8 - 8 * cm->mb_cols;

        mode_info_context++;         /* Skip border mb */
    }
}
903

904
int vp8_post_proc_frame(VP8_COMMON *oci, YV12_BUFFER_CONFIG *dest, vp8_ppflags_t *ppflags)
John Koleszar's avatar
John Koleszar committed
905
906
{
    int q = oci->filter_level * 10 / 6;
907
908
909
    int flags = ppflags->post_proc_flag;
    int deblock_level = ppflags->deblocking_level;
    int noise_level = ppflags->noise_level;
John Koleszar's avatar
John Koleszar committed
910
911
912
913
914
915
916
917
918
919
920

    if (!oci->frame_to_show)
        return -1;

    if (q > 63)
        q = 63;

    if (!flags)
    {
        *dest = *oci->frame_to_show;

921
        /* handle problem with extending borders */
John Koleszar's avatar
John Koleszar committed
922
923
924
        dest->y_width = oci->Width;
        dest->y_height = oci->Height;
        dest->uv_height = dest->y_height / 2;
925
        oci->postproc_state.last_base_qindex = oci->base_qindex;
John Koleszar's avatar
John Koleszar committed
926
927
928
        return 0;
    }

929
930
931
932
933
    /* Allocate post_proc_buffer_int if needed */
    if ((flags & VP8D_MFQE) && !oci->post_proc_buffer_int_used)
    {
        if ((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK))
        {
934
935
936
937
938
939
940
941
942
943
            int width = (oci->Width + 15) & ~15;
            int height = (oci->Height + 15) & ~15;

            if (vp8_yv12_alloc_frame_buffer(&oci->post_proc_buffer_int,
                                            width, height, VP8BORDERINPIXELS))
                vpx_internal_error(&oci->error, VPX_CODEC_MEM_ERROR,
                                   "Failed to allocate MFQE framebuffer");

            oci->post_proc_buffer_int_used = 1;

944
945
946
947
            // insure that postproc is set to all 0's so that post proc
            // doesn't pull random data in from edge
            vpx_memset((&oci->post_proc_buffer_int)->buffer_alloc,126,(&oci->post_proc_buffer)->frame_size);

948
949
950
        }
    }

John Koleszar's avatar
John Koleszar committed
951
952
953
954
#if ARCH_X86||ARCH_X86_64
    vpx_reset_mmx_state();
#endif

955
956
957
958
959
960
961
962
963
964
965
966
    if ((flags & VP8D_MFQE) &&
         oci->current_video_frame >= 2 &&
         oci->base_qindex - oci->postproc_state.last_base_qindex >= 10)
    {
        vp8_multiframe_quality_enhance(oci);
        if (((flags & VP8D_DEBLOCK) || (flags & VP8D_DEMACROBLOCK)) &&
            oci->post_proc_buffer_int_used)
        {
            vp8_yv12_copy_frame_ptr(&oci->post_proc_buffer, &oci->post_proc_buffer_int);
            if (flags & VP8D_DEMACROBLOCK)
            {
                vp8_deblock_and_de_macro_block(&oci->post_proc_buffer_int, &oci->post_proc_buffer,
John Koleszar's avatar
John Koleszar committed
967
                                               q + (deblock_level - 5) * 10, 1, 0);
968
969
970
971
            }
            else if (flags & VP8D_DEBLOCK)
            {
                vp8_deblock(&oci->post_proc_buffer_int, &oci->post_proc_buffer,
John Koleszar's avatar
John Koleszar committed
972
                            q, 1, 0);
973
974
            }
        }
975
976
        /* Move partially towards the base q of the previous frame */
        oci->postproc_state.last_base_qindex = (3*oci->postproc_state.last_base_qindex + oci->base_qindex)>>2;
977
978
    }
    else if (flags & VP8D_DEMACROBLOCK)
John Koleszar's avatar
John Koleszar committed
979
980
    {
        vp8_deblock_and_de_macro_block(oci->frame_to_show, &oci->post_proc_buffer,
John Koleszar's avatar
John Koleszar committed
981
                                       q + (deblock_level - 5) * 10, 1, 0);
982
        oci->postproc_state.last_base_qindex = oci->base_qindex;
John Koleszar's avatar
John Koleszar committed
983
984
985
986
    }
    else if (flags & VP8D_DEBLOCK)
    {
        vp8_deblock(oci->frame_to_show, &oci->post_proc_buffer,
John Koleszar's avatar
John Koleszar committed
987
                    q, 1, 0);
988
        oci->postproc_state.last_base_qindex = oci->base_qindex;
John Koleszar's avatar
John Koleszar committed
989
990
991
992
    }
    else
    {
        vp8_yv12_copy_frame_ptr(oci->frame_to_show, &oci->post_proc_buffer);
993
        oci->postproc_state.last_base_qindex = oci->base_qindex;
John Koleszar's avatar
John Koleszar committed
994
995
996
997
998
999
1000
1001
1002
1003
    }

    if (flags & VP8D_ADDNOISE)
    {
        if (oci->postproc_state.last_q != q
            || oci->postproc_state.last_noise != noise_level)
        {
            fillrd(&oci->postproc_state, 63 - q, noise_level);
        }

John Koleszar's avatar
John Koleszar committed
1004
        vp8_plane_add_noise
John Koleszar's avatar
John Koleszar committed
1005
1006
1007
1008
1009
1010
1011
1012
1013
        (oci->post_proc_buffer.y_buffer,
         oci->postproc_state.noise,
         oci->postproc_state.blackclamp,
         oci->postproc_state.whiteclamp,
         oci->postproc_state.bothclamp,
         oci->post_proc_buffer.y_width, oci->post_proc_buffer.y_height,
         oci->post_proc_buffer.y_stride);
    }

1014
1015
#if CONFIG_POSTPROC_VISUALIZER
    if (flags & VP8D_DEBUG_TXT_FRAME_INFO)
John Koleszar's avatar
John Koleszar committed
1016
    {
Henrik Lundin's avatar
Henrik Lundin committed
1017
        char message[512];
John Koleszar's avatar
John Koleszar committed
1018
1019
1020
1021
1022
1023
1024
1025
1026
        sprintf(message, "F%1dG%1dQ%3dF%3dP%d_s%dx%d",
                (oci->frame_type == KEY_FRAME),
                oci->refresh_golden_frame,
                oci->base_qindex,
                oci->filter_level,
                flags,
                oci->mb_cols, oci->mb_rows);
        vp8_blit_text(message, oci->post_proc_buffer.y_buffer, oci->post_proc_buffer.y_stride);
    }
1027

1028
    if (flags & VP8D_DEBUG_TXT_MBLK_MODES)
John Koleszar's avatar
John Koleszar committed
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
    {
        int i, j;
        unsigned char *y_ptr;
        YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
        int mb_rows = post->y_height >> 4;
        int mb_cols = post->y_width  >> 4;
        int mb_index = 0;
        MODE_INFO *mi = oci->mi;

        y_ptr = post->y_buffer + 4 * post->y_stride + 4;

1040
        /* vp8_filter each macro block */
John Koleszar's avatar
John Koleszar committed
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
        for (i = 0; i < mb_rows; i++)
        {
            for (j = 0; j < mb_cols; j++)
            {
                char zz[4];

                sprintf(zz, "%c", mi[mb_index].mbmi.mode + 'a');

                vp8_blit_text(zz, y_ptr, post->y_stride);
                mb_index ++;
                y_ptr += 16;
            }

1054
            mb_index ++; /* border */
John Koleszar's avatar
John Koleszar committed
1055
1056
1057
1058
            y_ptr += post->y_stride  * 16 - post->y_width;

        }
    }
1059

1060
    if (flags & VP8D_DEBUG_TXT_DC_DIFF)
John Koleszar's avatar
John Koleszar committed
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
    {
        int i, j;
        unsigned char *y_ptr;
        YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
        int mb_rows = post->y_height >> 4;
        int mb_cols = post->y_width  >> 4;
        int mb_index = 0;
        MODE_INFO *mi = oci->mi;

        y_ptr = post->y_buffer + 4 * post->y_stride + 4;

1072
        /* vp8_filter each macro block */
John Koleszar's avatar
John Koleszar committed
1073
1074
1075
1076
1077
        for (i = 0; i < mb_rows; i++)
        {
            for (j = 0; j < mb_cols; j++)
            {
                char zz[4];
1078
1079
                int dc_diff = !(mi[mb_index].mbmi.mode != B_PRED &&
                              mi[mb_index].mbmi.mode != SPLITMV &&
1080
                              mi[mb_index].mbmi.mb_skip_coeff);
John Koleszar's avatar
John Koleszar committed
1081
1082
1083
1084

                if (oci->frame_type == KEY_FRAME)
                    sprintf(zz, "a");
                else
1085
                    sprintf(zz, "%c", dc_diff + '0');
John Koleszar's avatar
John Koleszar committed
1086
1087
1088
1089
1090
1091

                vp8_blit_text(zz, y_ptr, post->y_stride);
                mb_index ++;
                y_ptr += 16;
            }

1092
            mb_index ++; /* border */
John Koleszar's avatar
John Koleszar committed
1093
1094
1095
1096
            y_ptr += post->y_stride  * 16 - post->y_width;

        }
    }
1097

1098
    if (flags & VP8D_DEBUG_TXT_RATE_INFO)
John Koleszar's avatar
John Koleszar committed
1099
    {
Henrik Lundin's avatar
Henrik Lundin committed
1100
        char message[512];
John Koleszar's avatar
John Koleszar committed
1101
1102
1103
        sprintf(message, "Bitrate: %10.2f frame_rate: %10.2f ", oci->bitrate, oci->framerate);
        vp8_blit_text(message, oci->post_proc_buffer.y_buffer, oci->post_proc_buffer.y_stride);
    }
1104

1105
    /* Draw motion vectors */
1106
    if ((flags & VP8D_DEBUG_DRAW_MV) && ppflags->display_mv_flag)
1107
1108
1109
1110
1111
1112
1113
1114
1115
    {
        YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
        int width  = post->y_width;
        int height = post->y_height;
        unsigned char *y_buffer = oci->post_proc_buffer.y_buffer;
        int y_stride = oci->post_proc_buffer.y_stride;
        MODE_INFO *mi = oci->mi;
        int x0, y0;

1116
        for (y0 = 0; y0 < height; y0 += 16)
1117
        {
1118
            for (x0 = 0; x0 < width; x0 += 16)
1119
            {
1120
1121
                int x1, y1;

1122
1123
1124
1125
1126
1127
                if (!(ppflags->display_mv_flag & (1<<mi->mbmi.mode)))
                {
                    mi++;
                    continue;
                }

1128
1129
                if (mi->mbmi.mode == SPLITMV)
                {
1130
1131
1132
1133
                    switch (mi->mbmi.partitioning)
                    {
                        case 0 :    /* mv_top_bottom */
                        {
1134
                            union b_mode_info *bmi = &mi->bmi[0];
1135
                            MV *mv = &bmi->mv.as_mv;
1136

1137
1138
                            x1 = x0 + 8 + (mv->col >> 3);
                            y1 = y0 + 4 + (mv->row >> 3);
1139

1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
                            constrain_line (x0+8, &x1, y0+4, &y1, width, height);
                            vp8_blit_line  (x0+8,  x1, y0+4,  y1, y_buffer, y_stride);

                            bmi = &mi->bmi[8];

                            x1 = x0 + 8 + (mv->col >> 3);
                            y1 = y0 +12 + (mv->row >> 3);

                            constrain_line (x0+8, &x1, y0+12, &y1, width, height);
                            vp8_blit_line  (x0+8,  x1, y0+12,  y1, y_buffer, y_stride);

                            break;
                        }
                        case 1 :    /* mv_left_right */
                        {
1155
                            union b_mode_info *bmi = &mi->bmi[0];
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
                            MV *mv = &bmi->mv.as_mv;

                            x1 = x0 + 4 + (mv->col >> 3);
                            y1 = y0 + 8 + (mv->row >> 3);

                            constrain_line (x0+4, &x1, y0+8, &y1, width, height);
                            vp8_blit_line  (x0+4,  x1, y0+8,  y1, y_buffer, y_stride);

                            bmi = &mi->bmi[2];

                            x1 = x0 +12 + (mv->col >> 3);
                            y1 = y0 + 8 + (mv->row >> 3);

                            constrain_line (x0+12, &x1, y0+8, &y1, width, height);
                            vp8_blit_line  (x0+12,  x1, y0+8,  y1, y_buffer, y_stride);

                            break;
                        }
                        case 2 :    /* mv_quarters   */
1175
                        {
1176
                            union b_mode_info *bmi = &mi->bmi[0];
1177
                            MV *mv = &bmi->mv.as_mv;
1178

1179
1180
                            x1 = x0 + 4 + (mv->col >> 3);
                            y1 = y0 + 4 + (mv->row >> 3);
1181

1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
                            constrain_line (x0+4, &x1, y0+4, &y1, width, height);
                            vp8_blit_line  (x0+4,  x1, y0+4,  y1, y_buffer, y_stride);

                            bmi = &mi->bmi[2];

                            x1 = x0 +12 + (mv->col >> 3);
                            y1 = y0 + 4 + (mv->row >> 3);

                            constrain_line (x0+12, &x1, y0+4, &y1, width, height);
                            vp8_blit_line  (x0+12,  x1, y0+4,  y1, y_buffer, y_stride);

                            bmi = &mi->bmi[8];

                            x1 = x0 + 4 + (mv->col >> 3);
                            y1 = y0 +12 + (mv->row >> 3);

                            constrain_line (x0+4, &x1, y0+12, &y1, width, height);
                            vp8_blit_line  (x0+4,  x1, y0+12,  y1, y_buffer, y_stride);

                            bmi = &mi->bmi[10];

                            x1 = x0 +12 + (mv->col >> 3);
                            y1 = y0 +12 + (mv->row >> 3);

                            constrain_line (x0+12, &x1, y0+12, &y1, width, height);
                            vp8_blit_line  (x0+12,  x1, y0+12,  y1, y_buffer, y_stride);
                            break;
                        }
                        default :
                        {
1212
                            union b_mode_info *bmi = mi->bmi;
1213
1214
1215
                            int bx0, by0;

                            for (by0 = y0; by0 < (y0+16); by0 += 4)
1216
                            {
1217
1218
1219
1220
1221
1222
                                for (bx0 = x0; bx0 < (x0+16); bx0 += 4)
                                {
                                    MV *mv = &bmi->mv.as_mv;

                                    x1 = bx0 + 2 + (mv->col >> 3);
                                    y1 = by0 + 2 + (mv->row >> 3);
1223

1224
1225
1226
1227
1228
1229
                                    constrain_line (bx0+2, &x1, by0+2, &y1, width, height);
                                    vp8_blit_line  (bx0+2,  x1, by0+2,  y1, y_buffer, y_stride);

                                    bmi++;
                                }
                            }
1230
1231
1232
1233
                        }
                    }
                }
                else if (mi->mbmi.mode >= NEARESTMV)
1234
1235
                {
                    MV *mv = &mi->mbmi.mv.as_mv;
1236
1237
                    const int lx0 = x0 + 8;
                    const int ly0 = y0 + 8;
John Koleszar's avatar
John Koleszar committed
1238

1239
1240
                    x1 = lx0 + (mv->col >> 3);
                    y1 = ly0 + (mv->row >> 3);
John Koleszar's avatar
John Koleszar committed
1241

1242
                    if (x1 != lx0 && y1 != ly0)
1243
                    {
1244
1245
                        constrain_line (lx0, &x1, ly0-1, &y1, width, height);
                        vp8_blit_line  (lx0,  x1, ly0-1,  y1, y_buffer, y_stride);
1246

1247
1248
                        constrain_line (lx0, &x1, ly0+1, &y1, width, height);
                        vp8_blit_line  (lx0,  x1, ly0+1,  y1, y_buffer, y_stride);
1249
1250
                    }
                    else
1251
                        vp8_blit_line  (lx0,  x1, ly0,  y1, y_buffer, y_stride);
1252
                }
1253

1254
1255
1256
1257
1258
                mi++;
            }
            mi++;
        }
    }
John Koleszar's avatar
John Koleszar committed
1259

1260
    /* Color in block modes */
1261
1262
    if ((flags & VP8D_DEBUG_CLR_BLK_MODES)
        && (ppflags->display_mb_modes_flag || ppflags->display_b_modes_flag))
1263
    {
1264
        int y, x;
1265
1266
1267
1268
1269
1270
1271
1272
1273
        YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
        int width  = post->y_width;
        int height = post->y_height;
        unsigned char *y_ptr = oci->post_proc_buffer.y_buffer;
        unsigned char *u_ptr = oci->post_proc_buffer.u_buffer;
        unsigned char *v_ptr = oci->post_proc_buffer.v_buffer;
        int y_stride = oci->post_proc_buffer.y_stride;
        MODE_INFO *mi = oci->mi;

1274
        for (y = 0; y < height; y += 16)
1275
        {
1276
            for (x = 0; x < width; x += 16)
1277
1278
1279
            {
                int Y = 0, U = 0, V = 0;

1280
1281
                if (mi->mbmi.mode == B_PRED &&
                    ((ppflags->display_mb_modes_flag & B_PRED) || ppflags->display_b_modes_flag))
1282
1283
1284
                {
                    int by, bx;
                    unsigned char *yl, *ul, *vl;
1285
                    union b_mode_info *bmi = mi->bmi;
1286
1287
1288
1289

                    yl = y_ptr + x;
                    ul = u_ptr + (x>>1);
                    vl = v_ptr + (x>>1);
1290

1291
1292
1293
1294
                    for (by = 0; by < 16; by += 4)
                    {
                        for (bx = 0; bx < 16; bx += 4)
                        {
1295
1296
1297
                            if ((ppflags->display_b_modes_flag & (1<<mi->mbmi.mode))
                                || (ppflags->display_mb_modes_flag & B_PRED))
                            {
1298
1299
1300
                                Y = B_PREDICTION_MODE_colors[bmi->as_mode][0];
                                U = B_PREDICTION_MODE_colors[bmi->as_mode][1];
                                V = B_PREDICTION_MODE_colors[bmi->as_mode][2];
1301

John Koleszar's avatar
John Koleszar committed
1302
                                vp8_blend_b
1303
1304
                                    (yl+bx, ul+(bx>>1), vl+(bx>>1), Y, U, V, 0xc000, y_stride);
                            }
1305
1306
1307
1308
1309
1310
1311
1312
                            bmi++;
                        }

                        yl += y_stride*4;
                        ul += y_stride*1;
                        vl += y_stride*1;
                    }
                }
1313
                else if (ppflags->display_mb_modes_flag & (1<<mi->mbmi.mode))
1314
1315
1316
1317
1318
                {
                    Y = MB_PREDICTION_MODE_colors[mi->mbmi.mode][0];
                    U = MB_PREDICTION_MODE_colors[mi->mbmi.mode][1];
                    V = MB_PREDICTION_MODE_colors[mi->mbmi.mode][2];

John Koleszar's avatar
John Koleszar committed
1319
                    vp8_blend_mb_inner
1320
1321
                        (y_ptr+x, u_ptr+(x>>1), v_ptr+(x>>1), Y, U, V, 0xc000, y_stride);
                }
1322

1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
                mi++;
            }
            y_ptr += y_stride*16;
            u_ptr += y_stride*4;
            v_ptr += y_stride*4;

            mi++;
        }
    }

1333
    /* Color in frame reference blocks */
1334
    if ((flags & VP8D_DEBUG_CLR_FRM_REF_BLKS) && ppflags->display_ref_frame_flag)
1335
    {
1336
        int y, x;
1337
1338
1339
1340
1341
1342
1343
1344
1345
        YV12_BUFFER_CONFIG *post = &oci->post_proc_buffer;
        int width  = post->y_width;
        int height = post->y_height;
        unsigned char *y_ptr = oci->post_proc_buffer.y_buffer;
        unsigned char *u_ptr = oci->post_proc_buffer.u_buffer;
        unsigned char *v_ptr = oci->post_proc_buffer.v_buffer;
        int y_stride = oci->post_proc_buffer.y_stride;
        MODE_INFO *mi = oci->mi;

1346
        for (y = 0; y < height; y += 16)
1347
        {
1348
            for (x = 0; x < width; x +=16)
1349
1350
1351
            {
                int Y = 0, U = 0, V = 0;

1352
1353
1354
1355
1356
                if (ppflags->display_ref_frame_flag & (1<<mi->mbmi.ref_frame))
                {
                    Y = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][0];
                    U = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][1];
                    V = MV_REFERENCE_FRAME_colors[mi->mbmi.ref_frame][2];
1357

John Koleszar's avatar
John Koleszar committed
1358
                    vp8_blend_mb_outer
1359
1360
                        (y_ptr+x, u_ptr+(x>>1), v_ptr+(x>>1), Y, U, V, 0xc000, y_stride);
                }
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370

                mi++;
            }
            y_ptr += y_stride*16;
            u_ptr += y_stride*4;
            v_ptr += y_stride*4;

            mi++;
        }
    }
1371
#endif
1372

John Koleszar's avatar
John Koleszar committed
1373
1374
    *dest = oci->post_proc_buffer;

1375
    /* handle problem with extending borders */
John Koleszar's avatar
John Koleszar committed
1376
1377
1378
1379
1380
    dest->y_width = oci->Width;
    dest->y_height = oci->Height;
    dest->uv_height = dest->y_height / 2;
    return 0;
}