2 * drawtext.c: print text over the screen
3 ******************************************************************************
5 * -f <filename> font filename (MANDATORY!!!)
6 * -s <pixel_size> font size in pixels [default 16]
8 * -o outline glyphs (use the bg color)
9 * -x <pos> x position ( >= 0) [default 0]
10 * -y <pos> y position ( >= 0) [default 0]
11 * -t <text> text to print (will be passed to strftime())
12 * MANDATORY: will be used even when -T is used.
13 * in this case, -t will be used if some error
15 * -T <filename> file with the text (re-read every frame)
16 * -c <#RRGGBB> foreground color ('internet' way) [default #ffffff]
17 * -C <#RRGGBB> background color ('internet' way) [default #000000]
19 ******************************************************************************
21 * - True Type, Type1 and others via FreeType2 library
22 * - Font kerning (better output)
23 * - Line Wrap (if the text doesn't fit, the next char go to the next line)
26 ******************************************************************************
27 * Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
29 * This library is free software; you can redistribute it and/or
30 * modify it under the terms of the GNU Lesser General Public
31 * License as published by the Free Software Foundation; either
32 * version 2 of the License, or (at your option) any later version.
34 * This library is distributed in the hope that it will be useful,
35 * but WITHOUT ANY WARRANTY; without even the implied warranty of
36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
37 * Lesser General Public License for more details.
39 * You should have received a copy of the GNU Lesser General Public
40 * License along with this library; if not, write to the Free Software
41 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
44 #define MAXSIZE_TEXT 1024
55 #include "framehook.h"
58 #include FT_FREETYPE_H
61 #define RGB_TO_YUV(rgb_color, yuv_color) { \
62 yuv_color[0] = ( 0.257 * rgb_color[0]) + (0.504 * rgb_color[1]) + (0.098 * rgb_color[2]) + 16; \
63 yuv_color[2] = ( 0.439 * rgb_color[0]) - (0.368 * rgb_color[1]) - (0.071 * rgb_color[2]) + 128; \
64 yuv_color[1] = (-0.148 * rgb_color[0]) - (0.291 * rgb_color[1]) + (0.439 * rgb_color[2]) + 128; \
67 #define COPY_3(dst,src) { \
75 #define SET_PIXEL(picture, yuv_color, x, y) { \
76 picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
77 picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
78 picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
81 #define GET_PIXEL(picture, yuv_color, x, y) { \
82 yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
83 yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
84 yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
95 unsigned char bgcolor
[3]; /* YUV */
96 unsigned char fgcolor
[3]; /* YUV */
99 FT_Glyph glyphs
[ 255 ];
100 FT_Bitmap bitmaps
[ 255 ];
102 int bitmap_left
[ 255 ];
103 int bitmap_top
[ 255 ];
104 unsigned int glyphs_index
[ 255 ];
111 void Release(void *ctx
)
118 int ParseColor(char *text
, unsigned char yuv_color
[3])
121 unsigned char rgb_color
[3];
126 if ((!text
) || (strlen(text
) != 7) || (text
[0] != '#') )
129 for (i
=0; i
< 3; i
++)
131 tmp
[0] = text
[i
*2+1];
132 tmp
[1] = text
[i
*2+2];
134 rgb_color
[i
] = strtol(tmp
, NULL
, 16);
137 RGB_TO_YUV(rgb_color
, yuv_color
);
142 int Configure(void **ctxp
, int argc
, char *argv
[])
146 ContextInfo
*ci
=NULL
;
148 unsigned int size
=16;
151 *ctxp
= av_mallocz(sizeof(ContextInfo
));
152 ci
= (ContextInfo
*) *ctxp
;
154 /* configure Context Info */
169 while ((c
= getopt(argc
, argv
, "f:t:T:x:y:s:c:C:bo")) > 0) {
175 ci
->text
= av_strdup(optarg
);
178 ci
->file
= av_strdup(optarg
);
181 ci
->x
= (unsigned int) atoi(optarg
);
184 ci
->y
= (unsigned int) atoi(optarg
);
187 size
= (unsigned int) atoi(optarg
);
190 if (ParseColor(optarg
, ci
->fgcolor
) == -1)
192 fprintf(stderr
, "ERROR: Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n",optarg
);
197 if (ParseColor(optarg
, ci
->bgcolor
) == -1)
199 fprintf(stderr
, "ERROR: Invalid foreground color: '%s'. You must specify the color in the internet way(packaged hex): #RRGGBB, ie: -c #ffffff (for white foreground)\n",optarg
);
210 fprintf(stderr
, "ERROR: Unrecognized argument '%s'\n", argv
[optind
]);
217 fprintf(stderr
,"ERROR: No text provided (-t text)\n");
224 if ((fp
=fopen(ci
->file
, "r")) == NULL
)
226 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
236 fprintf(stderr
,"ERROR: No font file provided! (-f filename)\n");
240 if ((error
= FT_Init_FreeType(&(ci
->library
))) != 0)
242 fprintf(stderr
,"ERROR: Could not load FreeType (error# %d)\n",error
);
246 if ((error
= FT_New_Face( ci
->library
, font
, 0, &(ci
->face
) )) != 0)
248 fprintf(stderr
,"ERROR: Could not load face: %s (error# %d)\n",font
, error
);
252 if ((error
= FT_Set_Pixel_Sizes( ci
->face
, 0, size
)) != 0)
254 fprintf(stderr
,"ERROR: Could not set font size to %d pixels (error# %d)\n",size
, error
);
258 ci
->use_kerning
= FT_HAS_KERNING(ci
->face
);
260 /* load and cache glyphs */
263 for (c
=0; c
< 256; c
++)
266 error
= FT_Load_Char( ci
->face
, (unsigned char) c
, FT_LOAD_RENDER
| FT_LOAD_MONOCHROME
);
267 if (error
) continue; /* ignore errors */
270 ci
->bitmaps
[c
] = ci
->face
->glyph
->bitmap
;
271 /* Save bitmap left */
272 ci
->bitmap_left
[c
] = ci
->face
->glyph
->bitmap_left
;
273 /* Save bitmap top */
274 ci
->bitmap_top
[c
] = ci
->face
->glyph
->bitmap_top
;
277 ci
->advance
[c
] = ci
->face
->glyph
->advance
.x
>> 6;
280 error
= FT_Get_Glyph( ci
->face
->glyph
, &(ci
->glyphs
[c
]) );
281 /* Save glyph index */
282 ci
->glyphs_index
[c
] = FT_Get_Char_Index( ci
->face
, (unsigned char) c
);
284 /* Measure text height to calculate text_height (or the maximum text height) */
285 FT_Glyph_Get_CBox( ci
->glyphs
[ c
], ft_glyph_bbox_pixels
, &bbox
);
286 if (bbox
.yMax
> yMax
)
288 if (bbox
.yMin
< yMin
)
293 ci
->text_height
= yMax
- yMin
;
302 inline void draw_glyph(AVPicture
*picture
, FT_Bitmap
*bitmap
, unsigned int x
, unsigned int y
, unsigned int width
, unsigned int height
, unsigned char yuv_fgcolor
[3], unsigned char yuv_bgcolor
[3], int outline
)
305 int spixel
, dpixel
[3], in_glyph
=0;
307 if (bitmap
->pixel_mode
== ft_pixel_mode_mono
)
310 for (r
=0; (r
< bitmap
->rows
) && (r
+y
< height
); r
++)
312 for (c
=0; (c
< bitmap
->width
) && (c
+x
< width
); c
++)
314 /* pixel in the picture (destination) */
315 GET_PIXEL(picture
, dpixel
, (c
+x
), (y
+r
));
317 /* pixel in the glyph bitmap (source) */
318 spixel
= bitmap
->buffer
[r
*bitmap
->pitch
+c
/8] & (0x80>>(c
%8));
321 COPY_3(dpixel
, yuv_fgcolor
);
325 /* border detection: */
326 if ( (!in_glyph
) && (spixel
) )
327 /* left border detected */
330 /* draw left pixel border */
332 SET_PIXEL(picture
, yuv_bgcolor
, (c
+x
-1), (y
+r
));
334 else if ( (in_glyph
) && (!spixel
) )
335 /* right border detected */
338 /* 'draw' right pixel border */
339 COPY_3(dpixel
, yuv_bgcolor
);
343 /* see if we have a top/bottom border */
346 if ( (r
-1 >= 0) && (! bitmap
->buffer
[(r
-1)*bitmap
->pitch
+c
/8] & (0x80>>(c
%8))) )
347 /* we have a top border */
348 SET_PIXEL(picture
, yuv_bgcolor
, (c
+x
), (y
+r
-1));
351 if ( (r
+1 < height
) && (! bitmap
->buffer
[(r
+1)*bitmap
->pitch
+c
/8] & (0x80>>(c
%8))) )
352 /* we have a bottom border */
353 SET_PIXEL(picture
, yuv_bgcolor
, (c
+x
), (y
+r
+1));
358 SET_PIXEL(picture
, dpixel
, (c
+x
), (y
+r
));
365 inline void draw_box(AVPicture
*picture
, unsigned int x
, unsigned int y
, unsigned int width
, unsigned int height
, unsigned char yuv_color
[3])
369 for (j
= 0; (j
< height
); j
++)
370 for (i
= 0; (i
< width
); i
++)
372 SET_PIXEL(picture
, yuv_color
, (i
+x
), (y
+j
));
380 void Process(void *ctx
, AVPicture
*picture
, enum PixelFormat pix_fmt
, int width
, int height
, int64_t pts
)
382 ContextInfo
*ci
= (ContextInfo
*) ctx
;
383 FT_Face face
= ci
->face
;
384 FT_GlyphSlot slot
= face
->glyph
;
385 unsigned char *text
= ci
->text
;
387 int x
= 0, y
= 0, i
=0, size
=0;
388 unsigned char buff
[MAXSIZE_TEXT
];
389 unsigned char tbuff
[MAXSIZE_TEXT
];
390 time_t now
= time(0);
391 int str_w
, str_w_max
;
392 FT_Vector pos
[MAXSIZE_TEXT
];
397 int fd
= open(ci
->file
, O_RDONLY
);
402 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
406 int l
= read(fd
, tbuff
, sizeof(tbuff
) - 1);
416 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
426 strftime(buff
, sizeof(buff
), text
, localtime(&now
));
435 /* measure string size and save glyphs position*/
436 str_w
= str_w_max
= 0;
439 for (i
=0; i
< size
; i
++)
444 if ( (ci
->use_kerning
) && (i
> 0) && (ci
->glyphs_index
[c
]) )
446 FT_Get_Kerning( ci
->face
,
447 ci
->glyphs_index
[ text
[i
-1] ],
455 if (( (x
+ ci
->advance
[ c
]) >= width
) || ( c
== '\n' ))
457 str_w
= width
- ci
->x
- 1;
459 y
+= ci
->text_height
;
465 pos
[i
].x
= x
+ ci
->bitmap_left
[c
];
466 pos
[i
].y
= y
- ci
->bitmap_top
[c
] + ci
->baseline
;
472 if (str_w
> str_w_max
)
482 /* Check if it doesn't pass the limits */
483 if ( str_w_max
+ ci
->x
>= width
)
484 str_w_max
= width
- ci
->x
- 1;
486 y
= height
- 1 - 2*ci
->y
;
488 /* Draw Background */
489 draw_box( picture
, ci
->x
, ci
->y
, str_w_max
, y
- ci
->y
, ci
->bgcolor
);
495 for (i
=0; i
< size
; i
++)
500 ( (c
== '_') && (text
== ci
->text
) ) || /* skip '_' (consider as space)
501 IF text was specified in cmd line
502 (which doesn't like neasted quotes) */
503 ( c
== '\n' ) /* Skip new line char, just go to new line */
507 /* now, draw to our target surface */
518 /* increment pen position */
519 x
+= slot
->advance
.x
>> 6;