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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
44 #define MAXSIZE_TEXT 1024
46 #include "framehook.h"
59 #include FT_FREETYPE_H
62 #define RGB_TO_YUV(rgb_color, yuv_color) { \
63 yuv_color[0] = ( 0.257 * rgb_color[0]) + (0.504 * rgb_color[1]) + (0.098 * rgb_color[2]) + 16; \
64 yuv_color[2] = ( 0.439 * rgb_color[0]) - (0.368 * rgb_color[1]) - (0.071 * rgb_color[2]) + 128; \
65 yuv_color[1] = (-0.148 * rgb_color[0]) - (0.291 * rgb_color[1]) + (0.439 * rgb_color[2]) + 128; \
68 #define COPY_3(dst,src) { \
76 #define SET_PIXEL(picture, yuv_color, x, y) { \
77 picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
78 picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
79 picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
82 #define GET_PIXEL(picture, yuv_color, x, y) { \
83 yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
84 yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
85 yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
96 unsigned char bgcolor[3]; /* YUV */
97 unsigned char fgcolor[3]; /* YUV */
100 FT_Glyph glyphs[ 255 ];
101 FT_Bitmap bitmaps[ 255 ];
103 int bitmap_left[ 255 ];
104 int bitmap_top[ 255 ];
105 unsigned int glyphs_index[ 255 ];
112 void Release(void *ctx)
119 static int ParseColor(char *text, unsigned char yuv_color[3])
122 unsigned char rgb_color[3];
127 if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
130 for (i=0; i < 3; i++)
132 tmp[0] = text[i*2+1];
133 tmp[1] = text[i*2+2];
135 rgb_color[i] = strtol(tmp, NULL, 16);
138 RGB_TO_YUV(rgb_color, yuv_color);
143 int Configure(void **ctxp, int argc, char *argv[])
147 ContextInfo *ci=NULL;
149 unsigned int size=16;
152 *ctxp = av_mallocz(sizeof(ContextInfo));
153 ci = (ContextInfo *) *ctxp;
155 /* configure Context Info */
170 while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
176 ci->text = av_strdup(optarg);
179 ci->file = av_strdup(optarg);
182 ci->x = (unsigned int) atoi(optarg);
185 ci->y = (unsigned int) atoi(optarg);
188 size = (unsigned int) atoi(optarg);
191 if (ParseColor(optarg, ci->fgcolor) == -1)
193 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);
198 if (ParseColor(optarg, ci->bgcolor) == -1)
200 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);
211 fprintf(stderr, "ERROR: Unrecognized argument '%s'\n", argv[optind]);
218 fprintf(stderr,"ERROR: No text provided (-t text)\n");
225 if ((fp=fopen(ci->file, "r")) == NULL)
227 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
237 fprintf(stderr,"ERROR: No font file provided! (-f filename)\n");
241 if ((error = FT_Init_FreeType(&(ci->library))) != 0)
243 fprintf(stderr,"ERROR: Could not load FreeType (error# %d)\n",error);
247 if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
249 fprintf(stderr,"ERROR: Could not load face: %s (error# %d)\n",font, error);
253 if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
255 fprintf(stderr,"ERROR: Could not set font size to %d pixels (error# %d)\n",size, error);
259 ci->use_kerning = FT_HAS_KERNING(ci->face);
261 /* load and cache glyphs */
264 for (c=0; c < 256; c++)
267 error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
268 if (error) continue; /* ignore errors */
271 ci->bitmaps[c] = ci->face->glyph->bitmap;
272 /* Save bitmap left */
273 ci->bitmap_left[c] = ci->face->glyph->bitmap_left;
274 /* Save bitmap top */
275 ci->bitmap_top[c] = ci->face->glyph->bitmap_top;
278 ci->advance[c] = ci->face->glyph->advance.x >> 6;
281 error = FT_Get_Glyph( ci->face->glyph, &(ci->glyphs[c]) );
282 /* Save glyph index */
283 ci->glyphs_index[c] = FT_Get_Char_Index( ci->face, (unsigned char) c );
285 /* Measure text height to calculate text_height (or the maximum text height) */
286 FT_Glyph_Get_CBox( ci->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
287 if (bbox.yMax > yMax)
289 if (bbox.yMin < yMin)
294 ci->text_height = yMax - yMin;
303 static 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)
306 int spixel, dpixel[3], in_glyph=0;
308 if (bitmap->pixel_mode == ft_pixel_mode_mono)
311 for (r=0; (r < bitmap->rows) && (r+y < height); r++)
313 for (c=0; (c < bitmap->width) && (c+x < width); c++)
315 /* pixel in the picture (destination) */
316 GET_PIXEL(picture, dpixel, (c+x), (y+r));
318 /* pixel in the glyph bitmap (source) */
319 spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
322 COPY_3(dpixel, yuv_fgcolor);
326 /* border detection: */
327 if ( (!in_glyph) && (spixel) )
328 /* left border detected */
331 /* draw left pixel border */
333 SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
335 else if ( (in_glyph) && (!spixel) )
336 /* right border detected */
339 /* 'draw' right pixel border */
340 COPY_3(dpixel, yuv_bgcolor);
344 /* see if we have a top/bottom border */
347 if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
348 /* we have a top border */
349 SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
352 if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
353 /* we have a bottom border */
354 SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
359 SET_PIXEL(picture, dpixel, (c+x), (y+r));
366 static inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
370 for (j = 0; (j < height); j++)
371 for (i = 0; (i < width); i++)
373 SET_PIXEL(picture, yuv_color, (i+x), (y+j));
381 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
383 ContextInfo *ci = (ContextInfo *) ctx;
384 FT_Face face = ci->face;
385 FT_GlyphSlot slot = face->glyph;
386 unsigned char *text = ci->text;
388 int x = 0, y = 0, i=0, size=0;
389 unsigned char buff[MAXSIZE_TEXT];
390 unsigned char tbuff[MAXSIZE_TEXT];
391 time_t now = time(0);
392 int str_w, str_w_max;
393 FT_Vector pos[MAXSIZE_TEXT];
398 int fd = open(ci->file, O_RDONLY);
403 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
407 int l = read(fd, tbuff, sizeof(tbuff) - 1);
417 perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
427 strftime(buff, sizeof(buff), text, localtime(&now));
436 /* measure string size and save glyphs position*/
437 str_w = str_w_max = 0;
440 for (i=0; i < size; i++)
445 if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
447 FT_Get_Kerning( ci->face,
448 ci->glyphs_index[ text[i-1] ],
456 if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
458 str_w = width - ci->x - 1;
460 y += ci->text_height;
466 pos[i].x = x + ci->bitmap_left[c];
467 pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
473 if (str_w > str_w_max)
483 /* Check if it doesn't pass the limits */
484 if ( str_w_max + ci->x >= width )
485 str_w_max = width - ci->x - 1;
487 y = height - 1 - 2*ci->y;
489 /* Draw Background */
490 draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
496 for (i=0; i < size; i++)
501 ( (c == '_') && (text == ci->text) ) || /* skip '_' (consider as space)
502 IF text was specified in cmd line
503 (which doesn't like neasted quotes) */
504 ( c == '\n' ) /* Skip new line char, just go to new line */
508 /* now, draw to our target surface */
519 /* increment pen position */
520 x += slot->advance.x >> 6;