1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525
|
/*
* drawtext.c: print text over the screen
******************************************************************************
* Options:
* -f <filename> font filename (MANDATORY!!!)
* -s <pixel_size> font size in pixels [default 16]
* -b print background
* -o outline glyphs (use the bg color)
* -x <pos> x position ( >= 0) [default 0]
* -y <pos> y position ( >= 0) [default 0]
* -t <text> text to print (will be passed to strftime())
* MANDATORY: will be used even when -T is used.
* in this case, -t will be used if some error
* occurs
* -T <filename> file with the text (re-read every frame)
* -c <#RRGGBB> foreground color ('internet' way) [default #ffffff]
* -C <#RRGGBB> background color ('internet' way) [default #000000]
*
******************************************************************************
* Features:
* - True Type, Type1 and others via FreeType2 library
* - Font kerning (better output)
* - Line Wrap (if the text doesn't fit, the next char go to the next line)
* - Background box
* - Outline
******************************************************************************
* Author: Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#define MAXSIZE_TEXT 1024
#include "framehook.h"
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#undef time
#include <sys/time.h>
#include <time.h>
#include <ft2build.h>
#include FT_FREETYPE_H
#include FT_GLYPH_H
#define RGB_TO_YUV(rgb_color, yuv_color) { \
yuv_color[0] = ( 0.257 * rgb_color[0]) + (0.504 * rgb_color[1]) + (0.098 * rgb_color[2]) + 16; \
yuv_color[2] = ( 0.439 * rgb_color[0]) - (0.368 * rgb_color[1]) - (0.071 * rgb_color[2]) + 128; \
yuv_color[1] = (-0.148 * rgb_color[0]) - (0.291 * rgb_color[1]) + (0.439 * rgb_color[2]) + 128; \
}
#define COPY_3(dst,src) { \
dst[0]=src[0]; \
dst[1]=src[1]; \
dst[2]=src[2]; \
}
#define SET_PIXEL(picture, yuv_color, x, y) { \
picture->data[0][ (x) + (y)*picture->linesize[0] ] = yuv_color[0]; \
picture->data[1][ ((x/2) + (y/2)*picture->linesize[1]) ] = yuv_color[1]; \
picture->data[2][ ((x/2) + (y/2)*picture->linesize[2]) ] = yuv_color[2]; \
}
#define GET_PIXEL(picture, yuv_color, x, y) { \
yuv_color[0] = picture->data[0][ (x) + (y)*picture->linesize[0] ]; \
yuv_color[1] = picture->data[1][ (x/2) + (y/2)*picture->linesize[1] ]; \
yuv_color[2] = picture->data[2][ (x/2) + (y/2)*picture->linesize[2] ]; \
}
typedef struct {
unsigned char *text;
unsigned char *file;
unsigned int x;
unsigned int y;
int bg;
int outline;
unsigned char bgcolor[3]; /* YUV */
unsigned char fgcolor[3]; /* YUV */
FT_Library library;
FT_Face face;
FT_Glyph glyphs[ 255 ];
FT_Bitmap bitmaps[ 255 ];
int advance[ 255 ];
int bitmap_left[ 255 ];
int bitmap_top[ 255 ];
unsigned int glyphs_index[ 255 ];
int text_height;
int baseline;
int use_kerning;
} ContextInfo;
void Release(void *ctx)
{
if (ctx)
av_free(ctx);
}
static int ParseColor(char *text, unsigned char yuv_color[3])
{
char tmp[3];
unsigned char rgb_color[3];
int i;
tmp[2] = '\0';
if ((!text) || (strlen(text) != 7) || (text[0] != '#') )
return -1;
for (i=0; i < 3; i++)
{
tmp[0] = text[i*2+1];
tmp[1] = text[i*2+2];
rgb_color[i] = strtol(tmp, NULL, 16);
}
RGB_TO_YUV(rgb_color, yuv_color);
return 0;
}
int Configure(void **ctxp, int argc, char *argv[])
{
int c;
int error;
ContextInfo *ci=NULL;
char *font=NULL;
unsigned int size=16;
FT_BBox bbox;
int yMax, yMin;
*ctxp = av_mallocz(sizeof(ContextInfo));
ci = (ContextInfo *) *ctxp;
/* configure Context Info */
ci->text = NULL;
ci->file = NULL;
ci->x = ci->y = 0;
ci->fgcolor[0]=255;
ci->fgcolor[1]=128;
ci->fgcolor[2]=128;
ci->bgcolor[0]=0;
ci->fgcolor[1]=128;
ci->fgcolor[2]=128;
ci->bg = 0;
ci->outline = 0;
ci->text_height = 0;
optind = 0;
while ((c = getopt(argc, argv, "f:t:T:x:y:s:c:C:bo")) > 0) {
switch (c) {
case 'f':
font = optarg;
break;
case 't':
ci->text = av_strdup(optarg);
break;
case 'T':
ci->file = av_strdup(optarg);
break;
case 'x':
ci->x = (unsigned int) atoi(optarg);
break;
case 'y':
ci->y = (unsigned int) atoi(optarg);
break;
case 's':
size = (unsigned int) atoi(optarg);
break;
case 'c':
if (ParseColor(optarg, ci->fgcolor) == -1)
{
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);
return -1;
}
break;
case 'C':
if (ParseColor(optarg, ci->bgcolor) == -1)
{
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);
return -1;
}
break;
case 'b':
ci->bg=1;
break;
case 'o':
ci->outline=1;
break;
case '?':
fprintf(stderr, "ERROR: Unrecognized argument '%s'\n", argv[optind]);
return -1;
}
}
if (!ci->text)
{
fprintf(stderr,"ERROR: No text provided (-t text)\n");
return -1;
}
if (ci->file)
{
FILE *fp;
if ((fp=fopen(ci->file, "r")) == NULL)
{
perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
}
else
{
fclose(fp);
}
}
if (!font)
{
fprintf(stderr,"ERROR: No font file provided! (-f filename)\n");
return -1;
}
if ((error = FT_Init_FreeType(&(ci->library))) != 0)
{
fprintf(stderr,"ERROR: Could not load FreeType (error# %d)\n",error);
return -1;
}
if ((error = FT_New_Face( ci->library, font, 0, &(ci->face) )) != 0)
{
fprintf(stderr,"ERROR: Could not load face: %s (error# %d)\n",font, error);
return -1;
}
if ((error = FT_Set_Pixel_Sizes( ci->face, 0, size)) != 0)
{
fprintf(stderr,"ERROR: Could not set font size to %d pixels (error# %d)\n",size, error);
return -1;
}
ci->use_kerning = FT_HAS_KERNING(ci->face);
/* load and cache glyphs */
yMax = -32000;
yMin = 32000;
for (c=0; c < 256; c++)
{
/* Load char */
error = FT_Load_Char( ci->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
if (error) continue; /* ignore errors */
/* Save bitmap */
ci->bitmaps[c] = ci->face->glyph->bitmap;
/* Save bitmap left */
ci->bitmap_left[c] = ci->face->glyph->bitmap_left;
/* Save bitmap top */
ci->bitmap_top[c] = ci->face->glyph->bitmap_top;
/* Save advance */
ci->advance[c] = ci->face->glyph->advance.x >> 6;
/* Save glyph */
error = FT_Get_Glyph( ci->face->glyph, &(ci->glyphs[c]) );
/* Save glyph index */
ci->glyphs_index[c] = FT_Get_Char_Index( ci->face, (unsigned char) c );
/* Measure text height to calculate text_height (or the maximum text height) */
FT_Glyph_Get_CBox( ci->glyphs[ c ], ft_glyph_bbox_pixels, &bbox );
if (bbox.yMax > yMax)
yMax = bbox.yMax;
if (bbox.yMin < yMin)
yMin = bbox.yMin;
}
ci->text_height = yMax - yMin;
ci->baseline = yMax;
return 0;
}
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)
{
int r, c;
int spixel, dpixel[3], in_glyph=0;
if (bitmap->pixel_mode == ft_pixel_mode_mono)
{
in_glyph = 0;
for (r=0; (r < bitmap->rows) && (r+y < height); r++)
{
for (c=0; (c < bitmap->width) && (c+x < width); c++)
{
/* pixel in the picture (destination) */
GET_PIXEL(picture, dpixel, (c+x), (y+r));
/* pixel in the glyph bitmap (source) */
spixel = bitmap->buffer[r*bitmap->pitch +c/8] & (0x80>>(c%8));
if (spixel)
COPY_3(dpixel, yuv_fgcolor);
if (outline)
{
/* border detection: */
if ( (!in_glyph) && (spixel) )
/* left border detected */
{
in_glyph = 1;
/* draw left pixel border */
if (c-1 >= 0)
SET_PIXEL(picture, yuv_bgcolor, (c+x-1), (y+r));
}
else if ( (in_glyph) && (!spixel) )
/* right border detected */
{
in_glyph = 0;
/* 'draw' right pixel border */
COPY_3(dpixel, yuv_bgcolor);
}
if (in_glyph)
/* see if we have a top/bottom border */
{
/* top */
if ( (r-1 >= 0) && (! bitmap->buffer[(r-1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
/* we have a top border */
SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r-1));
/* bottom */
if ( (r+1 < height) && (! bitmap->buffer[(r+1)*bitmap->pitch +c/8] & (0x80>>(c%8))) )
/* we have a bottom border */
SET_PIXEL(picture, yuv_bgcolor, (c+x), (y+r+1));
}
}
SET_PIXEL(picture, dpixel, (c+x), (y+r));
}
}
}
}
static inline void draw_box(AVPicture *picture, unsigned int x, unsigned int y, unsigned int width, unsigned int height, unsigned char yuv_color[3])
{
int i, j;
for (j = 0; (j < height); j++)
for (i = 0; (i < width); i++)
{
SET_PIXEL(picture, yuv_color, (i+x), (y+j));
}
}
void Process(void *ctx, AVPicture *picture, enum PixelFormat pix_fmt, int width, int height, int64_t pts)
{
ContextInfo *ci = (ContextInfo *) ctx;
FT_Face face = ci->face;
FT_GlyphSlot slot = face->glyph;
unsigned char *text = ci->text;
unsigned char c;
int x = 0, y = 0, i=0, size=0;
unsigned char buff[MAXSIZE_TEXT];
unsigned char tbuff[MAXSIZE_TEXT];
time_t now = time(0);
int str_w, str_w_max;
FT_Vector pos[MAXSIZE_TEXT];
FT_Vector delta;
if (ci->file)
{
int fd = open(ci->file, O_RDONLY);
if (fd < 0)
{
text = ci->text;
perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
}
else
{
int l = read(fd, tbuff, sizeof(tbuff) - 1);
if (l >= 0)
{
tbuff[l] = 0;
text = tbuff;
}
else
{
text = ci->text;
perror("WARNING: the file could not be opened. Using text provided with -t switch. ");
}
close(fd);
}
}
else
{
text = ci->text;
}
strftime(buff, sizeof(buff), text, localtime(&now));
text = buff;
size = strlen(text);
/* measure string size and save glyphs position*/
str_w = str_w_max = 0;
x = ci->x;
y = ci->y;
for (i=0; i < size; i++)
{
c = text[i];
/* kerning */
if ( (ci->use_kerning) && (i > 0) && (ci->glyphs_index[c]) )
{
FT_Get_Kerning( ci->face,
ci->glyphs_index[ text[i-1] ],
ci->glyphs_index[c],
ft_kerning_default,
&delta );
x += delta.x >> 6;
}
if (( (x + ci->advance[ c ]) >= width ) || ( c == '\n' ))
{
str_w = width - ci->x - 1;
y += ci->text_height;
x = ci->x;
}
/* save position */
pos[i].x = x + ci->bitmap_left[c];
pos[i].y = y - ci->bitmap_top[c] + ci->baseline;
x += ci->advance[c];
if (str_w > str_w_max)
str_w_max = str_w;
}
if (ci->bg)
{
/* Check if it doesn't pass the limits */
if ( str_w_max + ci->x >= width )
str_w_max = width - ci->x - 1;
if ( y >= height )
y = height - 1 - 2*ci->y;
/* Draw Background */
draw_box( picture, ci->x, ci->y, str_w_max, y - ci->y, ci->bgcolor );
}
/* Draw Glyphs */
for (i=0; i < size; i++)
{
c = text[i];
if (
( (c == '_') && (text == ci->text) ) || /* skip '_' (consider as space)
IF text was specified in cmd line
(which doesn't like neasted quotes) */
( c == '\n' ) /* Skip new line char, just go to new line */
)
continue;
/* now, draw to our target surface */
draw_glyph( picture,
&(ci->bitmaps[ c ]),
pos[i].x,
pos[i].y,
width,
height,
ci->fgcolor,
ci->bgcolor,
ci->outline );
/* increment pen position */
x += slot->advance.x >> 6;
}
}
|