visualize.c 14.2 KB
Newer Older
Loren Merritt's avatar
Loren Merritt committed
1
/*****************************************************************************
2
 * visualize.c: visualization
Loren Merritt's avatar
Loren Merritt committed
3
 *****************************************************************************
Loren Merritt's avatar
Loren Merritt committed
4
 * Copyright (C) 2005-2013 x264 project
5 6
 *
 * Authors: Tuukka Toivonen <tuukkat@ee.oulu.fi>
Loren Merritt's avatar
Loren Merritt committed
7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
21 22 23
 *
 * This program is also available under a commercial proprietary license.
 * For more information, contact us at licensing@x264.com.
Loren Merritt's avatar
Loren Merritt committed
24 25 26 27 28 29 30 31 32
 *****************************************************************************/

/*
 * Some explanation of the symbols used:
 * Red/pink: intra block
 * Blue: inter block
 * Green: skip block
 * Yellow: B-block (not visualized properly yet)
 *
33 34 35 36
 * Motion vectors have black dot at their target (ie. at the MB center),
 * instead of arrowhead. The black dot is enclosed in filled diamond with radius
 * depending on reference frame number (one frame back = zero width, normal case).
 *
Loren Merritt's avatar
Loren Merritt committed
37 38 39 40 41 42 43 44 45 46 47 48
 * The intra blocks have generally lines drawn perpendicular
 * to the prediction direction, so for example, if there is a pink block
 * with horizontal line at the top of it, it is interpolated by assuming
 * luma to be vertically constant.
 * DC predicted blocks have both horizontal and vertical lines,
 * pink blocks with a diagonal line are predicted using the planar function.
 */

#include "common.h"
#include "visualize.h"
#include "display.h"

49 50
typedef struct
{
Loren Merritt's avatar
Loren Merritt committed
51 52 53 54 55 56 57 58 59 60 61 62
    int     i_type;
    int     i_partition;
    int     i_sub_partition[4];
    int     i_intra16x16_pred_mode;
    int     intra4x4_pred_mode[4][4];
    int8_t  ref[2][4][4];                  /* [list][y][x] */
    int16_t mv[2][4][4][2];                /* [list][y][x][mvxy] */
} visualize_t;

/* Return string from stringlist corresponding to the given code */
#define GET_STRING(sl, code) get_string((sl), sizeof(sl)/sizeof(*(sl)), code)

63 64
typedef struct
{
Loren Merritt's avatar
Loren Merritt committed
65 66 67 68
    int code;
    char *string;
} stringlist_t;

69
static char *get_string( const stringlist_t *sl, int entries, int code )
Loren Merritt's avatar
Loren Merritt committed
70
{
71 72 73 74
    for( int i = 0; i < entries; i++ )
        if( sl[i].code == code )
            return sl[i].string;
    return "?";
Loren Merritt's avatar
Loren Merritt committed
75
}
76

Loren Merritt's avatar
Loren Merritt committed
77
/* Plot motion vector */
78
static void mv( int x0, int y0, int16_t dmv[2], int ref, int zoom, char *col )
Loren Merritt's avatar
Loren Merritt committed
79 80 81 82
{
    int dx = dmv[0];
    int dy = dmv[1];

83
    dx = (dx * zoom + 2) >> 2;
84
    dy = (dy * zoom + 2) >> 2;
85 86 87 88 89 90 91
    disp_line( 0, x0, y0, x0+dx, y0+dy );
    for( int i = 1; i < ref; i++ )
    {
        disp_line( 0, x0  , y0-i, x0+i, y0   );
        disp_line( 0, x0+i, y0  , x0  , y0+i );
        disp_line( 0, x0  , y0+i, x0-i, y0   );
        disp_line( 0, x0-i, y0  , x0  , y0-i );
92
    }
93 94 95
    disp_setcolor( "black" );
    disp_point( 0, x0, y0 );
    disp_setcolor( col );
Loren Merritt's avatar
Loren Merritt committed
96 97
}

98
int x264_visualize_init( x264_t *h )
Loren Merritt's avatar
Loren Merritt committed
99
{
100
    CHECKED_MALLOC( h->visualize, h->mb.i_mb_width * h->mb.i_mb_height * sizeof(visualize_t) );
101 102 103
    return 0;
fail:
    return -1;
Loren Merritt's avatar
Loren Merritt committed
104
}
105

Loren Merritt's avatar
Loren Merritt committed
106 107 108 109
void x264_visualize_mb( x264_t *h )
{
    visualize_t *v = (visualize_t*)h->visualize + h->mb.i_mb_xy;

110
    /* Save all data for the MB that we need for drawing the visualization */
Loren Merritt's avatar
Loren Merritt committed
111 112
    v->i_type = h->mb.i_type;
    v->i_partition = h->mb.i_partition;
113 114 115 116 117 118 119 120 121 122 123 124 125
    for( int i = 0; i < 4; i++ )
        v->i_sub_partition[i] = h->mb.i_sub_partition[i];
    for( int y = 0; y < 4; y++ )
        for( int x = 0; x < 4; x++ )
            v->intra4x4_pred_mode[y][x] = h->mb.cache.intra4x4_pred_mode[X264_SCAN8_0+y*8+x];
    for( int l = 0; l < 2; l++ )
        for( int y = 0; y < 4; y++ )
            for( int x = 0; x < 4; x++ )
            {
                for( int i = 0; i < 2; i++ )
                    v->mv[l][y][x][i] = h->mb.cache.mv[l][X264_SCAN8_0+y*8+x][i];
                v->ref[l][y][x] = h->mb.cache.ref[l][X264_SCAN8_0+y*8+x];
            }
Loren Merritt's avatar
Loren Merritt committed
126 127
    v->i_intra16x16_pred_mode = h->mb.i_intra16x16_pred_mode;
}
128

Loren Merritt's avatar
Loren Merritt committed
129 130 131 132
void x264_visualize_close( x264_t *h )
{
    x264_free(h->visualize);
}
133

Loren Merritt's avatar
Loren Merritt committed
134
/* Display visualization (block types, MVs) of the encoded frame */
135
/* FIXME: B-type MBs not handled yet properly */
Loren Merritt's avatar
Loren Merritt committed
136 137
void x264_visualize_show( x264_t *h )
{
138 139
    static const stringlist_t mb_types[] =
    {
Loren Merritt's avatar
Loren Merritt committed
140 141
        /* Block types marked as NULL will not be drawn */
        { I_4x4   , "red" },
142
        { I_8x8   , "#ff5640" },
143
        { I_16x16 , "#ff8060" },
Loren Merritt's avatar
Loren Merritt committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
        { I_PCM   , "violet" },
        { P_L0    , "SlateBlue" },
        { P_8x8   , "blue" },
        { P_SKIP  , "green" },
        { B_DIRECT, "yellow" },
        { B_L0_L0 , "yellow" },
        { B_L0_L1 , "yellow" },
        { B_L0_BI , "yellow" },
        { B_L1_L0 , "yellow" },
        { B_L1_L1 , "yellow" },
        { B_L1_BI , "yellow" },
        { B_BI_L0 , "yellow" },
        { B_BI_L1 , "yellow" },
        { B_BI_BI , "yellow" },
        { B_8x8   , "yellow" },
        { B_SKIP  , "yellow" },
    };

162 163 164 165
    static const int waitkey = 1;     /* Wait for enter after each frame */
    static const int drawbox = 1;     /* Draw box around each block */
    static const int borders = 0;     /* Display extrapolated borders outside frame */
    static const int zoom = 2;        /* Zoom factor */
Loren Merritt's avatar
Loren Merritt committed
166 167

    static const int pad = 32;
168
    pixel *const frame = h->fdec->plane[0];
Loren Merritt's avatar
Loren Merritt committed
169 170 171 172
    const int width = h->param.i_width;
    const int height = h->param.i_height;
    const int stride = h->fdec->i_stride[0];

173 174 175 176
    if( borders )
        disp_gray_zoom( 0, frame - pad*stride - pad, width+2*pad, height+2*pad, stride, "fdec", zoom );
    else
        disp_gray_zoom( 0, frame, width, height, stride, "fdec", zoom );
Loren Merritt's avatar
Loren Merritt committed
177

178
    for( int mb_xy = 0; mb_xy < h->mb.i_mb_width * h->mb.i_mb_height; mb_xy++ )
Loren Merritt's avatar
Loren Merritt committed
179
    {
180
        visualize_t *const v = (visualize_t*)h->visualize + mb_xy;
181 182
        const int mb_y = mb_xy / h->mb.i_mb_width;
        const int mb_x = mb_xy % h->mb.i_mb_width;
183
        char *const col = GET_STRING( mb_types, v->i_type );
Loren Merritt's avatar
Loren Merritt committed
184 185
        int x = mb_x*16*zoom;
        int y = mb_y*16*zoom;
186 187
        int l = 0;

188 189 190 191 192
        if( !col )
            continue;

        if( borders )
        {
Loren Merritt's avatar
Loren Merritt committed
193 194 195 196
            x += pad*zoom;
            y += pad*zoom;
        }

197 198
        disp_setcolor( col );
        if( drawbox ) disp_rect( 0, x, y, x+16*zoom-1, y+16*zoom-1 );
Loren Merritt's avatar
Loren Merritt committed
199

200 201
        if( v->i_type==P_L0 || v->i_type==P_8x8 || v->i_type==P_SKIP )
        {
Loren Merritt's avatar
Loren Merritt committed
202
            /* Predicted (inter) mode, with motion vector */
203 204 205 206 207 208 209 210
            if( v->i_partition == D_16x16 || v->i_type == P_SKIP )
                mv( x+8*zoom, y+8*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
            else if (v->i_partition == D_16x8)
            {
                if( drawbox ) disp_rect( 0, x, y, x+16*zoom, y+8*zoom );
                mv( x+8*zoom, y+4*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
                if( drawbox ) disp_rect( 0, x, y+8*zoom, x+16*zoom, y+16*zoom );
                mv( x+8*zoom, y+12*zoom, v->mv[l][2][0], v->ref[l][2][0], zoom, col );
Loren Merritt's avatar
Loren Merritt committed
211
            }
212 213 214 215 216 217
            else if( v->i_partition==D_8x16 )
            {
                if( drawbox ) disp_rect( 0, x,          y, x+8*zoom,  y+16*zoom );
                mv( x+4*zoom, y+8*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
                if( drawbox ) disp_rect( 0, x+8*zoom,   y, x+16*zoom, y+16*zoom );
                mv( x+12*zoom, y+8*zoom, v->mv[l][0][2], v->ref[l][0][2], zoom, col );
Loren Merritt's avatar
Loren Merritt committed
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
            else if( v->i_partition==D_8x8 )
            {
                for( int i = 0; i < 2; i++ )
                    for( int j = 0; j < 2; j++ )
                    {
                        int sp = v->i_sub_partition[i*2+j];
                        const int x0 = x + j*8*zoom;
                        const int y0 = y + i*8*zoom;
                        l = x264_mb_partition_listX_table[0][sp] ? 0 : 1; /* FIXME: not tested if this works */
                        if( IS_SUB8x8(sp) )
                        {
                            if( drawbox ) disp_rect( 0, x0, y0, x0+8*zoom, y0+8*zoom );
                            mv( x0+4*zoom, y0+4*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
                        }
                        else if( IS_SUB8x4(sp) )
                        {
                            if( drawbox ) disp_rect( 0, x0, y0, x0+8*zoom, y0+4*zoom );
                            if( drawbox ) disp_rect( 0, x0, y0+4*zoom, x0+8*zoom, y0+8*zoom );
                            mv( x0+4*zoom, y0+2*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
                            mv( x0+4*zoom, y0+6*zoom, v->mv[l][2*i+1][2*j], v->ref[l][2*i+1][2*j], zoom, col );
                        }
                        else if( IS_SUB4x8(sp) )
                        {
                            if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom, y0+8*zoom );
                            if( drawbox ) disp_rect( 0, x0+4*zoom, y0, x0+8*zoom, y0+8*zoom );
                            mv( x0+2*zoom, y0+4*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
                            mv( x0+6*zoom, y0+4*zoom, v->mv[l][2*i][2*j+1], v->ref[l][2*i][2*j+1], zoom, col );
                        }
                        else if( IS_SUB4x4(sp) )
                        {
                            if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom, y0+4*zoom );
                            if( drawbox ) disp_rect( 0, x0+4*zoom, y0, x0+8*zoom, y0+4*zoom );
                            if( drawbox ) disp_rect( 0, x0, y0+4*zoom, x0+4*zoom, y0+8*zoom );
                            if( drawbox ) disp_rect( 0, x0+4*zoom, y0+4*zoom, x0+8*zoom, y0+8*zoom );
                            mv( x0+2*zoom, y0+2*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
                            mv( x0+6*zoom, y0+2*zoom, v->mv[l][2*i][2*j+1], v->ref[l][2*i][2*j+1], zoom, col );
                            mv( x0+2*zoom, y0+6*zoom, v->mv[l][2*i+1][2*j], v->ref[l][2*i+1][2*j], zoom, col );
                            mv( x0+6*zoom, y0+6*zoom, v->mv[l][2*i+1][2*j+1], v->ref[l][2*i+1][2*j+1], zoom, col );
                        }
Loren Merritt's avatar
Loren Merritt committed
258 259 260 261
                    }
            }
        }

262 263
        if( IS_INTRA(v->i_type) || v->i_type == I_PCM )
        {
Loren Merritt's avatar
Loren Merritt committed
264
            /* Intra coded */
265 266
            if( v->i_type == I_16x16 )
            {
Loren Merritt's avatar
Loren Merritt committed
267 268
                switch (v->i_intra16x16_pred_mode) {
                case I_PRED_16x16_V:
269
                    disp_line( 0, x+2*zoom, y+2*zoom, x+14*zoom, y+2*zoom );
Loren Merritt's avatar
Loren Merritt committed
270 271
                    break;
                case I_PRED_16x16_H:
272
                    disp_line( 0, x+2*zoom, y+2*zoom, x+2*zoom, y+14*zoom );
Loren Merritt's avatar
Loren Merritt committed
273 274 275 276 277
                    break;
                case I_PRED_16x16_DC:
                case I_PRED_16x16_DC_LEFT:
                case I_PRED_16x16_DC_TOP:
                case I_PRED_16x16_DC_128:
278 279
                    disp_line( 0, x+2*zoom, y+2*zoom, x+14*zoom, y+2*zoom );
                    disp_line( 0, x+2*zoom, y+2*zoom, x+2*zoom, y+14*zoom );
Loren Merritt's avatar
Loren Merritt committed
280 281
                    break;
                case I_PRED_16x16_P:
282
                    disp_line( 0, x+2*zoom, y+2*zoom, x+8*zoom, y+8*zoom );
Loren Merritt's avatar
Loren Merritt committed
283 284 285
                    break;
                }
            }
286 287 288
            if( v->i_type==I_4x4 || v->i_type==I_8x8 )
            {
                const int di = v->i_type == I_8x8 ? 2 : 1;
289
                const int zoom2 = zoom * di;
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
                for( int i = 0; i < 4; i += di )
                    for( int j = 0; j < 4; j += di )
                    {
                        const int x0 = x + j*4*zoom;
                        const int y0 = y + i*4*zoom;
                        if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom2, y0+4*zoom2 );
                        switch( v->intra4x4_pred_mode[i][j] )
                        {
                            case I_PRED_4x4_V:        /* Vertical */
                                disp_line( 0, x0+0*zoom2, y0+1*zoom2, x0+4*zoom2, y0+1*zoom2 );
                                break;
                            case I_PRED_4x4_H:        /* Horizontal */
                                disp_line( 0, x0+1*zoom2, y0+0*zoom2, x0+1*zoom2, y0+4*zoom2 );
                                break;
                            case I_PRED_4x4_DC:        /* DC, average from top and left sides */
                            case I_PRED_4x4_DC_LEFT:
                            case I_PRED_4x4_DC_TOP:
                            case I_PRED_4x4_DC_128:
                                disp_line( 0, x0+1*zoom2, y0+1*zoom2, x0+4*zoom2, y0+1*zoom2 );
                                disp_line( 0, x0+1*zoom2, y0+1*zoom2, x0+1*zoom2, y0+4*zoom2 );
                                break;
                            case I_PRED_4x4_DDL:    /* Topright-bottomleft */
                                disp_line( 0, x0+0*zoom2, y0+0*zoom2, x0+4*zoom2, y0+4*zoom2 );
                                break;
                            case I_PRED_4x4_DDR:    /* Topleft-bottomright */
                                disp_line( 0, x0+0*zoom2, y0+4*zoom2, x0+4*zoom2, y0+0*zoom2 );
                                break;
                            case I_PRED_4x4_VR:        /* Mix of topleft-bottomright and vertical */
                                disp_line( 0, x0+0*zoom2, y0+2*zoom2, x0+4*zoom2, y0+1*zoom2 );
                                break;
                            case I_PRED_4x4_HD:        /* Mix of topleft-bottomright and horizontal */
                                disp_line( 0, x0+2*zoom2, y0+0*zoom2, x0+1*zoom2, y0+4*zoom2 );
                                break;
                            case I_PRED_4x4_VL:        /* Mix of topright-bottomleft and vertical */
                                disp_line( 0, x0+0*zoom2, y0+1*zoom2, x0+4*zoom2, y0+2*zoom2 );
                                break;
                            case I_PRED_4x4_HU:        /* Mix of topright-bottomleft and horizontal */
                                disp_line( 0, x0+1*zoom2, y0+0*zoom2, x0+2*zoom2, y0+4*zoom2 );
                                break;
                        }
Loren Merritt's avatar
Loren Merritt committed
330 331 332 333 334 335
                    }
            }
        }
    }

    disp_sync();
336 337
    if( waitkey )
        getchar();
Loren Merritt's avatar
Loren Merritt committed
338 339 340 341
}
/* }}} */

//EOF