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 file is part of FFmpeg.
31 * FFmpeg is free software; you can redistribute it and/or
32 * modify it under the terms of the GNU Lesser General Public
33 * License as published by the Free Software Foundation; either
34 * version 2.1 of the License, or (at your option) any later version.
36 * FFmpeg is distributed in the hope that it will be useful,
37 * but WITHOUT ANY WARRANTY; without even the implied warranty of
38 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
39 * Lesser General Public License for more details.
41 * You should have received a copy of the GNU Lesser General Public
42 * License along with FFmpeg; if not, write to the Free Software
43 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
46 #define MAXSIZE_TEXT 1024
48 #include "framehook.h"
61 #include FT_FREETYPE_H
65 #define ONE_HALF (1 << (SCALEBITS - 1))
66 #define FIX(x) ((int) ((x) * (1<<SCALEBITS) + 0.5))
68 #define RGB_TO_YUV(rgb_color, yuv_color) do { \
69 yuv_color[0] = (FIX(0.29900) * rgb_color[0] + FIX(0.58700) * rgb_color[1] + FIX(0.11400) * rgb_color[2] + ONE_HALF) >> SCALEBITS; \
70 yuv_color[2] = ((FIX(0.50000) * rgb_color[0] - FIX(0.41869) * rgb_color[1] - FIX(0.08131) * rgb_color[2] + ONE_HALF - 1) >> SCALEBITS) + 128; \
71 yuv_color[1] = ((- FIX(0.16874) * rgb_color[0] - FIX(0.33126) * rgb_color[1] + FIX(0.50000) * rgb_color[2] + ONE_HALF - 1) >> SCALEBITS) + 128; \
74 #define COPY_3(dst,src) { \
82 #define SET_PIXEL(picture, yuv_color, x, y) { \
83 picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
84 picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
85 picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
88 #define GET_PIXEL(picture, yuv_color, x, y) { \
89 yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
90 yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
91 yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
102 unsigned char bgcolor[3]; /* YUV */
103 unsigned char fgcolor[3]; /* YUV */
106 FT_Glyph glyphs[ 255 ];
107 FT_Bitmap bitmaps[ 255 ];
109 int bitmap_left[ 255 ];
110 int bitmap_top[ 255 ];
111 unsigned int glyphs_index[ 255 ];
118 void Release(void *ctx)
125 static int ParseColor(char *text, unsigned char yuv_color[3])
128 unsigned char rgb_color[3];
133 if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
136 for (i=0; i < 3; i++)
138 tmp[0] = text[i*2+1];
139 tmp[1] = text[i*2+2];
141 rgb_color[i] = strtol(tmp, NULL, 16);
144 RGB_TO_YUV(rgb_color, yuv_color);
149 int Configure(void **ctxp, int argc, char *argv[])
153 ContextInfo *ci=NULL;
155 unsigned int size=16;
158 *ctxp = av_mallocz(sizeof(ContextInfo));
159 ci = (ContextInfo *) *ctxp;
161 /* configure Context Info */
176 while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
182 ci->text = av_strdup(optarg);
185 ci->file = av_strdup(optarg);
188 ci->x = (unsigned int) atoi(optarg);
191 ci->y = (unsigned int) atoi(optarg);
194 size = (unsigned int) atoi(optarg);
197 if (ParseColor(optarg, ci->fgcolor) == -1)
199 av_log(NULL, AV_LOG_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);
204 if (ParseColor(optarg, ci->bgcolor) == -1)
206 av_log(NULL, AV_LOG_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);
217 av_log(NULL, AV_LOG_ERROR, "Unrecognized argument '%s'\n", argv[optind]);
224 av_log(NULL, AV_LOG_ERROR, "No text provided (-t text)\n");
231 if ((fp=fopen(ci->file, "r")) == NULL)
233 av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
243 av_log(NULL, AV_LOG_ERROR, "No font file provided! (-f filename)\n");
247 if ((error = FT_Init_FreeType(&(ci->library))) != 0)
249 av_log(NULL, AV_LOG_ERROR, "Could not load FreeType (error# %d).\n", error);
253 if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
255 av_log(NULL, AV_LOG_ERROR, "Could not load face: %s (error# %d).\n", font, error);
259 if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
261 av_log(NULL, AV_LOG_ERROR, "Could not set font size to %d pixels (error# %d).\n", size, error);
265 ci->use_kerning = FT_HAS_KERNING(ci->face);
267 /* load and cache glyphs */
270 for (c=0; c < 256; c++)
273 error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
274 if (error) continue; /* ignore errors */
277 ci->bitmaps[c] = ci->face->glyph->bitmap;
278 /* Save bitmap left */
279 ci->bitmap_left[c] = ci->face->glyph->bitmap_left;
280 /* Save bitmap top */
281 ci->bitmap_top[c] = ci->face->glyph->bitmap_top;
284 ci->advance[c] = ci->face->glyph->advance.x >> 6;
287 error = FT_Get_Glyph( ci->face->glyph, &(ci->glyphs[c]) );
288 /* Save glyph index */
289 ci->glyphs_index[c] = FT_Get_Char_Index( ci->face, (unsigned char) c );
291 /* Measure text height to calculate text_height (or the maximum text height) */
292 FT_Glyph_Get_CBox( ci->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
293 if (bbox.yMax > yMax)
295 if (bbox.yMin < yMin)
300 ci->text_height = yMax - yMin;
309 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)
312 int spixel, dpixel[3], in_glyph=0;
314 if (bitmap->pixel_mode == ft_pixel_mode_mono)
317 for (r=0; (r < bitmap->rows) && (r+y < height); r++)
319 for (c=0; (c < bitmap->width) && (c+x < width); c++)
321 /* pixel in the picture (destination) */
322 GET_PIXEL(picture, dpixel, (c+x), (y+r));
324 /* pixel in the glyph bitmap (source) */
325 spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
328 COPY_3(dpixel, yuv_fgcolor);
332 /* border detection: */
333 if ( (!in_glyph) && (spixel) )
334 /* left border detected */
337 /* draw left pixel border */
339 SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
341 else if ( (in_glyph) && (!spixel) )
342 /* right border detected */
345 /* 'draw' right pixel border */
346 COPY_3(dpixel, yuv_bgcolor);
350 /* see if we have a top/bottom border */
353 if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
354 /* we have a top border */
355 SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
358 if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
359 /* we have a bottom border */
360 SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
365 SET_PIXEL(picture, dpixel, (c+x), (y+r));
372 static inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
376 for (j = 0; (j < height); j++)
377 for (i = 0; (i < width); i++)
379 SET_PIXEL(picture, yuv_color, (i+x), (y+j));
387 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
389 ContextInfo *ci = (ContextInfo *) ctx;
390 FT_Face face = ci->face;
391 FT_GlyphSlot slot = face->glyph;
392 unsigned char *text = ci->text;
394 int x = 0, y = 0, i=0, size=0;
395 unsigned char buff[MAXSIZE_TEXT];
396 unsigned char tbuff[MAXSIZE_TEXT];
397 time_t now = time(0);
398 int str_w, str_w_max;
399 FT_Vector pos[MAXSIZE_TEXT];
404 int fd = open(ci->file, O_RDONLY);
409 av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened. Using text provided with -t switch: %s", strerror(errno));
413 int l = read(fd, tbuff, sizeof(tbuff) - 1);
423 av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be read. Using text provided with -t switch: %s", strerror(errno));
433 strftime(buff, sizeof(buff), text, localtime(&now));
442 /* measure string size and save glyphs position*/
443 str_w = str_w_max = 0;
446 for (i=0; i < size; i++)
451 if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
453 FT_Get_Kerning( ci->face,
454 ci->glyphs_index[ text[i-1] ],
462 if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
464 str_w = width - ci->x - 1;
466 y += ci->text_height;
472 pos[i].x = x + ci->bitmap_left[c];
473 pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
479 if (str_w > str_w_max)
489 /* Check if it doesn't pass the limits */
490 if ( str_w_max + ci->x >= width )
491 str_w_max = width - ci->x - 1;
493 y = height - 1 - 2*ci->y;
495 /* Draw Background */
496 draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
502 for (i=0; i < size; i++)
507 ( (c == '_') && (text == ci->text) ) || /* skip '_' (consider as space)
508 IF text was specified in cmd line
509 (which doesn't like neasted quotes) */
510 ( c == '\n' ) /* Skip new line char, just go to new line */
514 /* now, draw to our target surface */
525 /* increment pen position */
526 x += slot->advance.x >> 6;