Index: allfilters.c =================================================================== --- allfilters.c (revision 5734) +++ allfilters.c (working copy) @@ -54,6 +54,7 @@ REGISTER_FILTER (SPLIT, split, vf); REGISTER_FILTER (TRANSPOSE, transpose, vf); REGISTER_FILTER (VFLIP, vflip, vf); + REGISTER_FILTER (DRAWTEXT, drawtext, vf); REGISTER_FILTER (BUFFER, buffer, vsrc); REGISTER_FILTER (MOVIE, movie, vsrc); Index: Makefile =================================================================== --- Makefile (revision 5734) +++ Makefile (working copy) @@ -35,6 +35,7 @@ OBJS-$(CONFIG_SPLIT_FILTER) += vf_split.o OBJS-$(CONFIG_TRANSPOSE_FILTER) += vf_transpose.o OBJS-$(CONFIG_VFLIP_FILTER) += vf_vflip.o +OBJS-$(CONFIG_DRAWTEXT_FILTER) += vf_drawtext.o OBJS-$(CONFIG_BUFFER_FILTER) += vsrc_buffer.o OBJS-$(CONFIG_MOVIE_FILTER) += vsrc_movie.o Index: vf_drawtext.c =================================================================== --- vf_drawtext.c (revision 0) +++ vf_drawtext.c (revision 0) @@ -0,0 +1,589 @@ +/* + * vf_drawtext.c: print text over the screen + ****************************************************************************** + * Options: + * -f font filename (MANDATORY!!!) + * -s font size in pixels [default 16] + * -b print background + * -o outline glyphs (use the bg color) + * -x x position ( >= 0) [default 0] + * -y y position ( >= 0) [default 0] + * -t 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 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 + ****************************************************************************** + * Original vhook author: Gustavo Sverzut Barbieri + * Libavfilter version : S.N. Hemanth Meenakshisundaram + * + * Example usage : + * ffmpeg -i input.avi -vfilters \ + * 'drawtext=f:Vera.ttf:t:Test Text:x:5:y:5:s:20' output.avi + * + * This file is part of FFmpeg. + * + * FFmpeg 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.1 of the License, or (at your option) any later version. + * + * FFmpeg 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 FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/** + * @file libavfilter/vf_drawtext.c + * video filter to draw text over screen + */ + +#define MAXSIZE_TEXT 1024 + +#include "avfilter.h" +#include "libavutil/pixdesc.h" + +#include +#include +#include +#include +#include +#include +#undef time +#include +#include + +#include +#include +#include FT_FREETYPE_H +#include FT_GLYPH_H + +#define SCALEBITS 10 +#define DEF_DRAWTEXT_FONT_SZ 16 +#define MAX_DRAWTEXT_OPT 10 +#define ONE_HALF (1 << (SCALEBITS - 1)) +#define FIX(x) ((int) ((x) * (1<> SCALEBITS; \ + 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; \ + 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; \ +} while (0) + +#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; + 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; +} DtextContext; + +static int query_formats(AVFilterContext *ctx) +{ + static const enum PixelFormat pix_fmts[] = { + PIX_FMT_RGB48BE, PIX_FMT_RGB48LE, + PIX_FMT_ARGB, PIX_FMT_RGBA, + PIX_FMT_ABGR, PIX_FMT_BGRA, + PIX_FMT_RGB24, PIX_FMT_BGR24, + PIX_FMT_RGB565BE, PIX_FMT_RGB565LE, + PIX_FMT_RGB555BE, PIX_FMT_RGB555LE, + PIX_FMT_BGR565BE, PIX_FMT_BGR565LE, + PIX_FMT_BGR555BE, PIX_FMT_BGR555LE, + PIX_FMT_GRAY16BE, PIX_FMT_GRAY16LE, + PIX_FMT_YUV420P16LE, PIX_FMT_YUV420P16BE, + PIX_FMT_YUV422P16LE, PIX_FMT_YUV422P16BE, + PIX_FMT_YUV444P16LE, PIX_FMT_YUV444P16BE, + PIX_FMT_YUV444P, PIX_FMT_YUV422P, + PIX_FMT_YUV420P, PIX_FMT_YUV411P, + PIX_FMT_YUV410P, PIX_FMT_YUV440P, + PIX_FMT_YUVJ444P, PIX_FMT_YUVJ422P, + PIX_FMT_YUVJ420P, PIX_FMT_YUVJ440P, + PIX_FMT_YUVA420P, + PIX_FMT_RGB8, PIX_FMT_BGR8, + PIX_FMT_RGB4_BYTE, PIX_FMT_BGR4_BYTE, + PIX_FMT_PAL8, PIX_FMT_GRAY8, + PIX_FMT_NONE + }; + + avfilter_set_common_formats(ctx, avfilter_make_format_list(pix_fmts)); + + return 0; +} + +static int parse_color(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; +} + +static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque) +{ + int err, chars_read, num_opt = 0; + char str[256], cmd[2]; + const char *args_cur; + char *font = NULL; + int c; + unsigned int size=DEF_DRAWTEXT_FONT_SZ; + FT_BBox bbox; + int yMax, yMin; + DtextContext *dtext = ctx->priv; + + dtext->text = NULL; + dtext->file = NULL; + dtext->x = dtext->y = 0; + dtext->fgcolor[0]=255; + dtext->fgcolor[1]=128; + dtext->fgcolor[2]=128; + dtext->bgcolor[0]=0; + dtext->fgcolor[1]=128; + dtext->fgcolor[2]=128; + dtext->bg = 0; + dtext->outline = 0; + dtext->text_height = 0; + + optind = 1; + + if ((err = FT_Init_FreeType(&(dtext->library))) != 0) + { + av_log(NULL, AV_LOG_ERROR, "Could not load FreeType (error# %d).\n", err); + return -1; + } + + args_cur = args; + if(args_cur) { + while (2 == sscanf(args_cur, "%1[FtfxyiRGBA]:%255[^:]%n", cmd, str, &chars_read)) + { + if (num_opt>=MAX_DRAWTEXT_OPT) { + av_log(NULL, AV_LOG_ERROR, + "drawtext init() cannot handle more than %d arguments\n", + MAX_DRAWTEXT_OPT); + return -1; + } + switch(cmd[0]) { + case 'f': + font = av_strdup(str); + break; + case 't': + dtext->text = av_strdup(str); + break; + case 'T': + dtext->file = av_strdup(str); + break; + case 'x': + dtext->x = (unsigned int)atoi(str); + break; + case 'y': + dtext->y = (unsigned int)atoi(str); + break; + case 's': + size = (unsigned int)atoi(str); + break; + case 'c': + if (parse_color(optarg, dtext->fgcolor) == -1) + { + av_log(NULL, AV_LOG_ERROR, "Invalid foreground color: '%s'.\n", optarg); + return -1; + } + break; + case 'C': + if (parse_color(optarg, dtext->bgcolor) == -1) + { + av_log(NULL, AV_LOG_ERROR, "Invalid background color: '%s'.\n", optarg); + return -1; + } + break; + case 'b': + dtext->bg = 1; + break; + case 'o': + dtext->bg = 1; + break; + case '?': + av_log(NULL, AV_LOG_ERROR, "Unrecognized argument '%c:%s'\n", cmd[0], str); + return -1; + } + num_opt++; + args_cur += chars_read; + if (*args_cur==':') + args_cur++; + } + } + if (*font == 0) + { + av_log(NULL, AV_LOG_ERROR, "No font file provided! (-f filename)\n"); + return -1; + } + if ((err = FT_New_Face( dtext->library, font, 0, &(dtext->face) )) != 0) + { + av_log(NULL, AV_LOG_ERROR, "Could not load face: %s (error# %d).\n", font, err); + return -1; + } + if (!dtext->text || *(dtext->text) == 0) + { + av_log(NULL, AV_LOG_ERROR, "No text provided (-t text)\n"); + return -1; + } + if (dtext->file && *(dtext->file) != 0) + { + FILE *fp; + if ((fp=fopen(dtext->file, "r")) == NULL) + { + av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened.\ + Using text provided with -t switch: %s", strerror(errno)); + } + else + { + fclose(fp); + } + } + if ((err = FT_Set_Pixel_Sizes( dtext->face, 0, size)) != 0) + { + av_log(NULL, AV_LOG_ERROR, "Could not set font size to %d pixels (error# %d).\n", size, err); + return -1; + } + + dtext->use_kerning = FT_HAS_KERNING(dtext->face); + + /* load and cache glyphs */ + yMax = -32000; + yMin = 32000; + for (c=0; c < 256; c++) + { + /* Load char */ + err = FT_Load_Char( dtext->face, (unsigned char) c, FT_LOAD_RENDER | FT_LOAD_MONOCHROME ); + if (err) continue; /* ignore errors */ + + /* Save bitmap */ + dtext->bitmaps[c] = dtext->face->glyph->bitmap; + /* Save bitmap left */ + dtext->bitmap_left[c] = dtext->face->glyph->bitmap_left; + /* Save bitmap top */ + dtext->bitmap_top[c] = dtext->face->glyph->bitmap_top; + + /* Save advance */ + dtext->advance[c] = dtext->face->glyph->advance.x >> 6; + + /* Save glyph */ + err = FT_Get_Glyph( dtext->face->glyph, &(dtext->glyphs[c]) ); + /* Save glyph index */ + dtext->glyphs_index[c] = FT_Get_Char_Index( dtext->face, (unsigned char) c ); + + /* Measure text height to calculate text_height (or the maximum text height) */ + FT_Glyph_Get_CBox( dtext->glyphs[ c ], ft_glyph_bbox_pixels, &bbox ); + if (bbox.yMax > yMax) + yMax = bbox.yMax; + if (bbox.yMin < yMin) + yMin = bbox.yMin; + } + + dtext->text_height = yMax - yMin; + dtext->baseline = yMax; + av_free(font); + + return 0; +} + +static av_cold void uninit(AVFilterContext *ctx) +{ + DtextContext *dtext = ctx->priv; + av_freep(&dtext->text); + av_freep(&dtext->file); + FT_Done_Face(dtext->face); + FT_Done_FreeType(dtext->library); +} + +static int config_input(AVFilterLink *link) +{ + 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)); + } +} + +static void draw_text(DtextContext *dtext, AVPicture *picture, int width, int height) +{ + FT_Face face = dtext->face; + FT_GlyphSlot slot = face->glyph; + unsigned char *text = dtext->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 (dtext->file) + { + int fd = open(dtext->file, O_RDONLY); + if (fd < 0) + { + text = dtext->text; + av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be opened.\ + Using text provided with -t switch: %s", strerror(errno)); + } + else + { + int l = read(fd, tbuff, sizeof(tbuff) - 1); + if (l >= 0) + { + tbuff[l] = 0; + text = tbuff; + } + else + { + text = dtext->text; + av_log(NULL, AV_LOG_INFO, "WARNING: The file could not be read.\ + Using text provided with -t switch: %s", strerror(errno)); + } + close(fd); + } + } + else + { + text = dtext->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 = dtext->x; + y = dtext->y; + for (i=0; i < size; i++) + { + c = text[i]; + /* kerning */ + if ( (dtext->use_kerning) && (i > 0) && (dtext->glyphs_index[c]) ) + { + FT_Get_Kerning(dtext->face, dtext->glyphs_index[text[i-1]], + dtext->glyphs_index[c], ft_kerning_default, &delta); + x += delta.x >> 6; + } + + if (( (x + dtext->advance[ c ]) >= width ) || ( c == '\n' )) + { + str_w = width - dtext->x - 1; + y += dtext->text_height; + x = dtext->x; + } + + /* save position */ + pos[i].x = x + dtext->bitmap_left[c]; + pos[i].y = y - dtext->bitmap_top[c] + dtext->baseline; + x += dtext->advance[c]; + if (str_w > str_w_max) + str_w_max = str_w; + } + if (dtext->bg) + { + /* Check if it doesn't pass the limits */ + if ( str_w_max + dtext->x >= width ) + str_w_max = width - dtext->x - 1; + if ( y >= height ) + y = height - 1 - 2*dtext->y; + + /* Draw Background */ + draw_box( picture, dtext->x, dtext->y, str_w_max, y - dtext->y, dtext->bgcolor ); + } + + /* Draw Glyphs */ + for (i=0; i < size; i++) + { + c = text[i]; + + /* skip '_' (consider as space) if text was specified in cmd line and + * skip new line char, just go to new line */ + if (( (c == '_') && (text == dtext->text) ) || ( c == '\n' ) ) + continue; + + /* now, draw to our target surface */ + draw_glyph( picture, + &(dtext->bitmaps[ c ]), + pos[i].x, + pos[i].y, + width, + height, + dtext->fgcolor, + dtext->bgcolor, + dtext->outline ); + + /* increment pen position */ + x += slot->advance.x >> 6; + } +} + +static void end_frame(AVFilterLink *link) +{ + DtextContext *context = link->dst->priv; + AVFilterLink *output = link->dst->outputs[0]; + AVFilterPicRef *pic_ref = link->cur_pic; + AVPicture *pic = (AVPicture *)&pic_ref->data; + + draw_text(context, pic, pic_ref->w, pic_ref->h); + + avfilter_draw_slice(output, 0, pic_ref->h, 1); + avfilter_end_frame(output); +} + +AVFilter avfilter_vf_drawtext = { + .name = "drawtext", + .priv_size = sizeof(DtextContext), + .init = init, + .uninit = uninit, + + .query_formats = query_formats, + .inputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_VIDEO, + .get_video_buffer= avfilter_null_get_video_buffer, + .start_frame = avfilter_null_start_frame, + .end_frame = end_frame, + .config_props = config_input, + .min_perms = AV_PERM_WRITE | + AV_PERM_READ, + .rej_perms = AV_PERM_PRESERVE }, + { .name = NULL}}, + .outputs = (AVFilterPad[]) {{ .name = "default", + .type = AVMEDIA_TYPE_VIDEO, }, + { .name = NULL}}, +};