]> git.sesse.net Git - ffmpeg/blob - vhook/drawtext.c
COSMETICS: tabs --> spaces, some prettyprinting
[ffmpeg] / vhook / drawtext.c
1 /*
2  * drawtext.c: print text over the screen
3  ******************************************************************************
4  * Options:
5  * -f <filename>    font filename (MANDATORY!!!)
6  * -s <pixel_size>  font size in pixels [default 16]
7  * -b               print background
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
14  *                  occurs
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]
18  *
19  ******************************************************************************
20  * Features:
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)
24  * - Background box
25  * - Outline
26  ******************************************************************************
27  * Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
28  *
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.
33  *
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.
38  *
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
42  */
43
44 #define MAXSIZE_TEXT 1024
45
46 #include "framehook.h"
47
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <fcntl.h>
51 #include <stdarg.h>
52 #include <string.h>
53 #include <unistd.h>
54 #undef time
55 #include <sys/time.h>
56 #include <time.h>
57
58 #include <ft2build.h>
59 #include FT_FREETYPE_H
60 #include FT_GLYPH_H
61
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; \
66 }
67
68 #define COPY_3(dst,src) { \
69     dst[0]=src[0]; \
70     dst[1]=src[1]; \
71     dst[2]=src[2]; \
72 }
73
74
75
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]; \
80 }
81
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] ]; \
86 }
87
88
89 typedef struct {
90   unsigned char *text;
91   unsigned char *file;
92   unsigned int x;
93   unsigned int y;
94   int bg;
95   int outline;
96   unsigned char bgcolor[3]; /* YUV */
97   unsigned char fgcolor[3]; /* YUV */
98   FT_Library library;
99   FT_Face    face;
100   FT_Glyph   glyphs[ 255 ];
101   FT_Bitmap  bitmaps[ 255 ];
102   int        advance[ 255 ];
103   int        bitmap_left[ 255 ];
104   int        bitmap_top[ 255 ];
105   unsigned int glyphs_index[ 255 ];
106   int        text_height;
107   int        baseline;
108   int use_kerning;
109 } ContextInfo;
110
111
112 void Release(void *ctx)
113 {
114     if (ctx)
115         av_free(ctx);
116 }
117
118
119 int ParseColor(char *text, unsigned char yuv_color[3])
120 {
121   char tmp[3];
122   unsigned char rgb_color[3];
123   int i;
124
125   tmp[2] = '\0';
126
127   if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
128     return -1;
129
130   for (i=0; i < 3; i++)
131     {
132       tmp[0] = text[i*2+1];
133       tmp[1] = text[i*2+2];
134
135       rgb_color[i] = strtol(tmp, NULL, 16);
136     }
137
138   RGB_TO_YUV(rgb_color, yuv_color);
139
140   return 0;
141 }
142
143 int Configure(void **ctxp, int argc, char *argv[])
144 {
145     int c;
146     int error;
147     ContextInfo *ci=NULL;
148     char *font=NULL;
149     unsigned int size=16;
150     FT_BBox bbox;
151     int yMax, yMin;
152     *ctxp = av_mallocz(sizeof(ContextInfo));
153     ci = (ContextInfo *) *ctxp;
154
155     /* configure Context Info */
156     ci->text = NULL;
157     ci->file = NULL;
158     ci->x = ci->y = 0;
159     ci->fgcolor[0]=255;
160     ci->fgcolor[1]=128;
161     ci->fgcolor[2]=128;
162     ci->bgcolor[0]=0;
163     ci->fgcolor[1]=128;
164     ci->fgcolor[2]=128;
165     ci->bg = 0;
166     ci->outline = 0;
167     ci->text_height = 0;
168
169     optind = 0;
170     while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
171       switch (c) {
172       case 'f':
173         font = optarg;
174         break;
175       case 't':
176         ci->text = av_strdup(optarg);
177         break;
178       case 'T':
179         ci->file = av_strdup(optarg);
180         break;
181       case 'x':
182         ci->x = (unsigned int) atoi(optarg);
183         break;
184       case 'y':
185         ci->y = (unsigned int) atoi(optarg);
186         break;
187       case 's':
188         size = (unsigned int) atoi(optarg);
189         break;
190       case 'c':
191         if (ParseColor(optarg, ci->fgcolor) == -1)
192           {
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);
194             return -1;
195           }
196         break;
197       case 'C':
198         if (ParseColor(optarg, ci->bgcolor) == -1)
199           {
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);
201             return -1;
202           }
203         break;
204       case 'b':
205         ci->bg=1;
206         break;
207       case 'o':
208         ci->outline=1;
209         break;
210       case '?':
211         fprintf(stderr, "ERROR: Unrecognized argument '%s'\n", argv[optind]);
212         return -1;
213       }
214     }
215
216     if (!ci->text)
217       {
218         fprintf(stderr,"ERROR: No text provided (-t text)\n");
219         return -1;
220       }
221
222     if (ci->file)
223       {
224         FILE *fp;
225         if ((fp=fopen(ci->file, "r")) == NULL)
226           {
227             perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
228           }
229         else
230           {
231             fclose(fp);
232           }
233       }
234
235     if (!font)
236       {
237         fprintf(stderr,"ERROR: No font file provided! (-f filename)\n");
238         return -1;
239       }
240
241     if ((error = FT_Init_FreeType(&(ci->library))) != 0)
242       {
243         fprintf(stderr,"ERROR: Could not load FreeType (error# %d)\n",error);
244         return -1;
245       }
246
247     if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
248       {
249         fprintf(stderr,"ERROR: Could not load face: %s  (error# %d)\n",font, error);
250         return -1;
251       }
252
253     if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
254       {
255         fprintf(stderr,"ERROR: Could not set font size to %d pixels (error# %d)\n",size, error);
256         return -1;
257       }
258
259     ci->use_kerning = FT_HAS_KERNING(ci->face);
260
261     /* load and cache glyphs */
262     yMax = -32000;
263     yMin =  32000;
264     for (c=0; c < 256; c++)
265       {
266         /* Load char */
267         error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
268         if (error) continue;  /* ignore errors */
269
270         /* Save bitmap */
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;
276
277         /* Save advance */
278         ci->advance[c] = ci->face->glyph->advance.x >> 6;
279
280         /* Save glyph */
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 );
284
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)
288           yMax = bbox.yMax;
289         if (bbox.yMin < yMin)
290           yMin = bbox.yMin;
291
292       }
293
294     ci->text_height = yMax - yMin;
295     ci->baseline = yMax;
296
297     return 0;
298 }
299
300
301
302
303 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)
304 {
305   int r, c;
306   int spixel, dpixel[3], in_glyph=0;
307
308   if (bitmap->pixel_mode == ft_pixel_mode_mono)
309     {
310       in_glyph = 0;
311       for (r=0; (r < bitmap->rows) && (r+y < height); r++)
312         {
313           for (c=0; (c < bitmap->width) && (c+x < width); c++)
314             {
315               /* pixel in the picture (destination) */
316               GET_PIXEL(picture, dpixel, (c+x), (y+r));
317
318               /* pixel in the glyph bitmap (source) */
319               spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
320
321               if (spixel)
322                 COPY_3(dpixel, yuv_fgcolor);
323
324               if (outline)
325                 {
326                   /* border detection: */
327                   if ( (!in_glyph) && (spixel) )
328                     /* left border detected */
329                     {
330                       in_glyph = 1;
331                       /* draw left pixel border */
332                       if (c-1 >= 0)
333                         SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
334                     }
335                   else if ( (in_glyph) && (!spixel) )
336                     /* right border detected */
337                     {
338                       in_glyph = 0;
339                       /* 'draw' right pixel border */
340                       COPY_3(dpixel, yuv_bgcolor);
341                     }
342
343                   if (in_glyph)
344                     /* see if we have a top/bottom border */
345                     {
346                       /* top */
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));
350
351                       /* bottom */
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));
355
356                     }
357                 }
358
359               SET_PIXEL(picture, dpixel, (c+x), (y+r));
360             }
361         }
362     }
363 }
364
365
366 inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
367 {
368   int i, j;
369
370   for (j = 0; (j < height); j++)
371     for (i = 0; (i < width); i++)
372       {
373         SET_PIXEL(picture, yuv_color, (i+x), (y+j));
374       }
375
376 }
377
378
379
380
381 void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
382 {
383   ContextInfo *ci = (ContextInfo *) ctx;
384   FT_Face face = ci->face;
385   FT_GlyphSlot  slot = face->glyph;
386   unsigned char *text = ci->text;
387   unsigned char c;
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];
394   FT_Vector delta;
395
396   if (ci->file)
397     {
398       int fd = open(ci->file, O_RDONLY);
399
400       if (fd < 0)
401         {
402           text = ci->text;
403           perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
404         }
405       else
406         {
407           int l = read(fd, tbuff, sizeof(tbuff) - 1);
408
409           if (l >= 0)
410             {
411               tbuff[l] = 0;
412               text = tbuff;
413             }
414           else
415             {
416               text = ci->text;
417               perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
418             }
419           close(fd);
420         }
421     }
422   else
423     {
424       text = ci->text;
425     }
426
427   strftime(buff, sizeof(buff), text, localtime(&now));
428
429   text = buff;
430
431   size = strlen(text);
432
433
434
435
436   /* measure string size and save glyphs position*/
437   str_w = str_w_max = 0;
438   x = ci->x;
439   y = ci->y;
440   for (i=0; i < size; i++)
441     {
442       c = text[i];
443
444       /* kerning */
445       if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
446         {
447           FT_Get_Kerning( ci->face,
448                           ci->glyphs_index[ text[i-1] ],
449                           ci->glyphs_index[c],
450                           ft_kerning_default,
451                           &delta );
452
453           x += delta.x >> 6;
454         }
455
456       if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
457         {
458           str_w = width - ci->x - 1;
459
460           y += ci->text_height;
461           x = ci->x;
462         }
463
464
465       /* save position */
466       pos[i].x = x + ci->bitmap_left[c];
467       pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
468
469
470       x += ci->advance[c];
471
472
473       if (str_w > str_w_max)
474         str_w_max = str_w;
475
476     }
477
478
479
480
481   if (ci->bg)
482     {
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;
486       if ( y >= height )
487         y = height - 1 - 2*ci->y;
488
489       /* Draw Background */
490       draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
491     }
492
493
494
495   /* Draw Glyphs */
496   for (i=0; i < size; i++)
497     {
498       c = text[i];
499
500       if (
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 */
505           )
506         continue;
507
508         /* now, draw to our target surface */
509         draw_glyph( picture,
510                     &(ci->bitmaps[ c ]),
511                     pos[i].x,
512                     pos[i].y,
513                     width,
514                     height,
515                     ci->fgcolor,
516                     ci->bgcolor,
517                     ci->outline );
518
519       /* increment pen position */
520       x += slot->advance.x >> 6;
521     }
522
523
524 }
525