]> git.sesse.net Git - x264/blob - common/visualize.c
Fix printing of SEI header with VBV + ABR
[x264] / common / visualize.c
1 /*****************************************************************************
2  * x264: visualization module
3  *****************************************************************************
4  * Copyright (C) 2005 Tuukka Toivonen <tuukkat@ee.oulu.fi>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02111, USA.
19  *****************************************************************************/
20
21 /*
22  * Some explanation of the symbols used:
23  * Red/pink: intra block
24  * Blue: inter block
25  * Green: skip block
26  * Yellow: B-block (not visualized properly yet)
27  *
28  * Motion vectors have black dot at their target (ie. at the MB center),
29  * instead of arrowhead. The black dot is enclosed in filled diamond with radius
30  * depending on reference frame number (one frame back = zero width, normal case).
31  *
32  * The intra blocks have generally lines drawn perpendicular
33  * to the prediction direction, so for example, if there is a pink block
34  * with horizontal line at the top of it, it is interpolated by assuming
35  * luma to be vertically constant.
36  * DC predicted blocks have both horizontal and vertical lines,
37  * pink blocks with a diagonal line are predicted using the planar function.
38  */
39
40 #include "common.h"
41 #include "visualize.h"
42 #include "display.h"
43
44 typedef struct
45 {
46     int     i_type;
47     int     i_partition;
48     int     i_sub_partition[4];
49     int     i_intra16x16_pred_mode;
50     int     intra4x4_pred_mode[4][4];
51     int8_t  ref[2][4][4];                  /* [list][y][x] */
52     int16_t mv[2][4][4][2];                /* [list][y][x][mvxy] */
53 } visualize_t;
54
55 /* Return string from stringlist corresponding to the given code */
56 #define GET_STRING(sl, code) get_string((sl), sizeof(sl)/sizeof(*(sl)), code)
57
58 typedef struct
59 {
60     int code;
61     char *string;
62 } stringlist_t;
63
64 static char *get_string( const stringlist_t *sl, int entries, int code )
65 {
66     for( int i = 0; i < entries; i++ )
67         if( sl[i].code == code )
68             return sl[i].string;
69     return "?";
70 }
71
72 /* Plot motion vector */
73 static void mv( int x0, int y0, int16_t dmv[2], int ref, int zoom, char *col )
74 {
75     int dx = dmv[0];
76     int dy = dmv[1];
77
78     dx = (dx * zoom + 2) >> 2;
79     dy = (dy * zoom + 2) >> 2;
80     disp_line( 0, x0, y0, x0+dx, y0+dy );
81     for( int i = 1; i < ref; i++ )
82     {
83         disp_line( 0, x0  , y0-i, x0+i, y0   );
84         disp_line( 0, x0+i, y0  , x0  , y0+i );
85         disp_line( 0, x0  , y0+i, x0-i, y0   );
86         disp_line( 0, x0-i, y0  , x0  , y0-i );
87     }
88     disp_setcolor( "black" );
89     disp_point( 0, x0, y0 );
90     disp_setcolor( col );
91 }
92
93 int x264_visualize_init( x264_t *h )
94 {
95     CHECKED_MALLOC( h->visualize, h->sps->i_mb_width * h->sps->i_mb_height * sizeof(visualize_t) );
96     return 0;
97 fail:
98     return -1;
99 }
100
101 void x264_visualize_mb( x264_t *h )
102 {
103     visualize_t *v = (visualize_t*)h->visualize + h->mb.i_mb_xy;
104
105     /* Save all data for the MB that we need for drawing the visualization */
106     v->i_type = h->mb.i_type;
107     v->i_partition = h->mb.i_partition;
108     for( int i = 0; i < 4; i++ )
109         v->i_sub_partition[i] = h->mb.i_sub_partition[i];
110     for( int y = 0; y < 4; y++ )
111         for( int x = 0; x < 4; x++ )
112             v->intra4x4_pred_mode[y][x] = h->mb.cache.intra4x4_pred_mode[X264_SCAN8_0+y*8+x];
113     for( int l = 0; l < 2; l++ )
114         for( int y = 0; y < 4; y++ )
115             for( int x = 0; x < 4; x++ )
116             {
117                 for( int i = 0; i < 2; i++ )
118                     v->mv[l][y][x][i] = h->mb.cache.mv[l][X264_SCAN8_0+y*8+x][i];
119                 v->ref[l][y][x] = h->mb.cache.ref[l][X264_SCAN8_0+y*8+x];
120             }
121     v->i_intra16x16_pred_mode = h->mb.i_intra16x16_pred_mode;
122 }
123
124 void x264_visualize_close( x264_t *h )
125 {
126     x264_free(h->visualize);
127 }
128
129 /* Display visualization (block types, MVs) of the encoded frame */
130 /* FIXME: B-type MBs not handled yet properly */
131 void x264_visualize_show( x264_t *h )
132 {
133     static const stringlist_t mb_types[] =
134     {
135         /* Block types marked as NULL will not be drawn */
136         { I_4x4   , "red" },
137         { I_8x8   , "#ff5640" },
138         { I_16x16 , "#ff8060" },
139         { I_PCM   , "violet" },
140         { P_L0    , "SlateBlue" },
141         { P_8x8   , "blue" },
142         { P_SKIP  , "green" },
143         { B_DIRECT, "yellow" },
144         { B_L0_L0 , "yellow" },
145         { B_L0_L1 , "yellow" },
146         { B_L0_BI , "yellow" },
147         { B_L1_L0 , "yellow" },
148         { B_L1_L1 , "yellow" },
149         { B_L1_BI , "yellow" },
150         { B_BI_L0 , "yellow" },
151         { B_BI_L1 , "yellow" },
152         { B_BI_BI , "yellow" },
153         { B_8x8   , "yellow" },
154         { B_SKIP  , "yellow" },
155     };
156
157     static const int waitkey = 1;     /* Wait for enter after each frame */
158     static const int drawbox = 1;     /* Draw box around each block */
159     static const int borders = 0;     /* Display extrapolated borders outside frame */
160     static const int zoom = 2;        /* Zoom factor */
161
162     static const int pad = 32;
163     uint8_t *const frame = h->fdec->plane[0];
164     const int width = h->param.i_width;
165     const int height = h->param.i_height;
166     const int stride = h->fdec->i_stride[0];
167
168     if( borders )
169         disp_gray_zoom( 0, frame - pad*stride - pad, width+2*pad, height+2*pad, stride, "fdec", zoom );
170     else
171         disp_gray_zoom( 0, frame, width, height, stride, "fdec", zoom );
172
173     for( int mb_xy = 0; mb_xy < h->sps->i_mb_width * h->sps->i_mb_height; mb_xy++ )
174     {
175         visualize_t *const v = (visualize_t*)h->visualize + mb_xy;
176         const int mb_y = mb_xy / h->sps->i_mb_width;
177         const int mb_x = mb_xy % h->sps->i_mb_width;
178         char *const col = GET_STRING( mb_types, v->i_type );
179         int x = mb_x*16*zoom;
180         int y = mb_y*16*zoom;
181         int l = 0;
182
183         if( !col )
184             continue;
185
186         if( borders )
187         {
188             x += pad*zoom;
189             y += pad*zoom;
190         }
191
192         disp_setcolor( col );
193         if( drawbox ) disp_rect( 0, x, y, x+16*zoom-1, y+16*zoom-1 );
194
195         if( v->i_type==P_L0 || v->i_type==P_8x8 || v->i_type==P_SKIP )
196         {
197             /* Predicted (inter) mode, with motion vector */
198             if( v->i_partition == D_16x16 || v->i_type == P_SKIP )
199                 mv( x+8*zoom, y+8*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
200             else if (v->i_partition == D_16x8)
201             {
202                 if( drawbox ) disp_rect( 0, x, y, x+16*zoom, y+8*zoom );
203                 mv( x+8*zoom, y+4*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
204                 if( drawbox ) disp_rect( 0, x, y+8*zoom, x+16*zoom, y+16*zoom );
205                 mv( x+8*zoom, y+12*zoom, v->mv[l][2][0], v->ref[l][2][0], zoom, col );
206             }
207             else if( v->i_partition==D_8x16 )
208             {
209                 if( drawbox ) disp_rect( 0, x,          y, x+8*zoom,  y+16*zoom );
210                 mv( x+4*zoom, y+8*zoom, v->mv[l][0][0], v->ref[l][0][0], zoom, col );
211                 if( drawbox ) disp_rect( 0, x+8*zoom,   y, x+16*zoom, y+16*zoom );
212                 mv( x+12*zoom, y+8*zoom, v->mv[l][0][2], v->ref[l][0][2], zoom, col );
213             }
214             else if( v->i_partition==D_8x8 )
215             {
216                 for( int i = 0; i < 2; i++ )
217                     for( int j = 0; j < 2; j++ )
218                     {
219                         int sp = v->i_sub_partition[i*2+j];
220                         const int x0 = x + j*8*zoom;
221                         const int y0 = y + i*8*zoom;
222                         l = x264_mb_partition_listX_table[0][sp] ? 0 : 1; /* FIXME: not tested if this works */
223                         if( IS_SUB8x8(sp) )
224                         {
225                             if( drawbox ) disp_rect( 0, x0, y0, x0+8*zoom, y0+8*zoom );
226                             mv( x0+4*zoom, y0+4*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
227                         }
228                         else if( IS_SUB8x4(sp) )
229                         {
230                             if( drawbox ) disp_rect( 0, x0, y0, x0+8*zoom, y0+4*zoom );
231                             if( drawbox ) disp_rect( 0, x0, y0+4*zoom, x0+8*zoom, y0+8*zoom );
232                             mv( x0+4*zoom, y0+2*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
233                             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 );
234                         }
235                         else if( IS_SUB4x8(sp) )
236                         {
237                             if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom, y0+8*zoom );
238                             if( drawbox ) disp_rect( 0, x0+4*zoom, y0, x0+8*zoom, y0+8*zoom );
239                             mv( x0+2*zoom, y0+4*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
240                             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 );
241                         }
242                         else if( IS_SUB4x4(sp) )
243                         {
244                             if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom, y0+4*zoom );
245                             if( drawbox ) disp_rect( 0, x0+4*zoom, y0, x0+8*zoom, y0+4*zoom );
246                             if( drawbox ) disp_rect( 0, x0, y0+4*zoom, x0+4*zoom, y0+8*zoom );
247                             if( drawbox ) disp_rect( 0, x0+4*zoom, y0+4*zoom, x0+8*zoom, y0+8*zoom );
248                             mv( x0+2*zoom, y0+2*zoom, v->mv[l][2*i][2*j], v->ref[l][2*i][2*j], zoom, col );
249                             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 );
250                             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 );
251                             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 );
252                         }
253                     }
254             }
255         }
256
257         if( IS_INTRA(v->i_type) || v->i_type == I_PCM )
258         {
259             /* Intra coded */
260             if( v->i_type == I_16x16 )
261             {
262                 switch (v->i_intra16x16_pred_mode) {
263                 case I_PRED_16x16_V:
264                     disp_line( 0, x+2*zoom, y+2*zoom, x+14*zoom, y+2*zoom );
265                     break;
266                 case I_PRED_16x16_H:
267                     disp_line( 0, x+2*zoom, y+2*zoom, x+2*zoom, y+14*zoom );
268                     break;
269                 case I_PRED_16x16_DC:
270                 case I_PRED_16x16_DC_LEFT:
271                 case I_PRED_16x16_DC_TOP:
272                 case I_PRED_16x16_DC_128:
273                     disp_line( 0, x+2*zoom, y+2*zoom, x+14*zoom, y+2*zoom );
274                     disp_line( 0, x+2*zoom, y+2*zoom, x+2*zoom, y+14*zoom );
275                     break;
276                 case I_PRED_16x16_P:
277                     disp_line( 0, x+2*zoom, y+2*zoom, x+8*zoom, y+8*zoom );
278                     break;
279                 }
280             }
281             if( v->i_type==I_4x4 || v->i_type==I_8x8 )
282             {
283                 const int di = v->i_type == I_8x8 ? 2 : 1;
284                 const int zoom2 = zoom * di;
285                 for( int i = 0; i < 4; i += di )
286                     for( int j = 0; j < 4; j += di )
287                     {
288                         const int x0 = x + j*4*zoom;
289                         const int y0 = y + i*4*zoom;
290                         if( drawbox ) disp_rect( 0, x0, y0, x0+4*zoom2, y0+4*zoom2 );
291                         switch( v->intra4x4_pred_mode[i][j] )
292                         {
293                             case I_PRED_4x4_V:        /* Vertical */
294                                 disp_line( 0, x0+0*zoom2, y0+1*zoom2, x0+4*zoom2, y0+1*zoom2 );
295                                 break;
296                             case I_PRED_4x4_H:        /* Horizontal */
297                                 disp_line( 0, x0+1*zoom2, y0+0*zoom2, x0+1*zoom2, y0+4*zoom2 );
298                                 break;
299                             case I_PRED_4x4_DC:        /* DC, average from top and left sides */
300                             case I_PRED_4x4_DC_LEFT:
301                             case I_PRED_4x4_DC_TOP:
302                             case I_PRED_4x4_DC_128:
303                                 disp_line( 0, x0+1*zoom2, y0+1*zoom2, x0+4*zoom2, y0+1*zoom2 );
304                                 disp_line( 0, x0+1*zoom2, y0+1*zoom2, x0+1*zoom2, y0+4*zoom2 );
305                                 break;
306                             case I_PRED_4x4_DDL:    /* Topright-bottomleft */
307                                 disp_line( 0, x0+0*zoom2, y0+0*zoom2, x0+4*zoom2, y0+4*zoom2 );
308                                 break;
309                             case I_PRED_4x4_DDR:    /* Topleft-bottomright */
310                                 disp_line( 0, x0+0*zoom2, y0+4*zoom2, x0+4*zoom2, y0+0*zoom2 );
311                                 break;
312                             case I_PRED_4x4_VR:        /* Mix of topleft-bottomright and vertical */
313                                 disp_line( 0, x0+0*zoom2, y0+2*zoom2, x0+4*zoom2, y0+1*zoom2 );
314                                 break;
315                             case I_PRED_4x4_HD:        /* Mix of topleft-bottomright and horizontal */
316                                 disp_line( 0, x0+2*zoom2, y0+0*zoom2, x0+1*zoom2, y0+4*zoom2 );
317                                 break;
318                             case I_PRED_4x4_VL:        /* Mix of topright-bottomleft and vertical */
319                                 disp_line( 0, x0+0*zoom2, y0+1*zoom2, x0+4*zoom2, y0+2*zoom2 );
320                                 break;
321                             case I_PRED_4x4_HU:        /* Mix of topright-bottomleft and horizontal */
322                                 disp_line( 0, x0+1*zoom2, y0+0*zoom2, x0+2*zoom2, y0+4*zoom2 );
323                                 break;
324                         }
325                     }
326             }
327         }
328     }
329
330     disp_sync();
331     if( waitkey )
332         getchar();
333 }
334 /* }}} */
335
336 //EOF