50 #include FT_FREETYPE_H
72 static double drand(
void *opaque,
double min,
double max)
145 #define OFFSET(x) offsetof(DrawTextContext, x)
162 {
"fix_bounds",
"if true, check and fix text coords to avoid clipping",
166 {
"ft_load_flags",
"set font loading flags for libfreetype",
OFFSET(ft_load_flags),
AV_OPT_TYPE_FLAGS, {.i64=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, 0,
"ft_load_flags" },
167 {
"default",
"set default", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_DEFAULT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
168 {
"no_scale",
"set no_scale", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_SCALE}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
169 {
"no_hinting",
"set no_hinting", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_HINTING}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
170 {
"render",
"set render", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_RENDER}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
171 {
"no_bitmap",
"set no_bitmap", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_BITMAP}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
172 {
"vertical_layout",
"set vertical_layout", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_VERTICAL_LAYOUT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
173 {
"force_autohint",
"set force_autohint", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_FORCE_AUTOHINT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
174 {
"crop_bitmap",
"set crop_bitmap", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_CROP_BITMAP}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
175 {
"pedantic",
"set pedantic", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_PEDANTIC}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
176 {
"ignore_global_advance_width",
"set ignore_global_advance_width", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
177 {
"no_recurse",
"set no_recurse", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_RECURSE}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
178 {
"ignore_transform",
"set ignore_transform", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_IGNORE_TRANSFORM}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
179 {
"monochrome",
"set monochrome", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_MONOCHROME}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
180 {
"linear_design",
"set linear_design", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_LINEAR_DESIGN}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
181 {
"no_autohint",
"set no_autohint", 0,
AV_OPT_TYPE_CONST, {.i64 = FT_LOAD_NO_AUTOHINT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
196 #undef __FTERRORS_H__
197 #define FT_ERROR_START_LIST {
198 #define FT_ERRORDEF(e, v, s) { (e), (s) },
199 #define FT_ERROR_END_LIST { 0, NULL } };
208 #define FT_ERRMSG(e) ft_errors[e].err_msg
222 const Glyph *
a = key, *bb =
b;
223 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
224 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
243 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
249 if (FT_Get_Glyph(dtext->
face->glyph, glyph->glyph)) {
254 glyph->bitmap = dtext->
face->glyph->bitmap;
255 glyph->bitmap_left = dtext->
face->glyph->bitmap_left;
256 glyph->bitmap_top = dtext->
face->glyph->bitmap_top;
257 glyph->advance = dtext->
face->glyph->advance.x >> 6;
260 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
309 "Both text and text file provided. Please provide only one\n");
314 "The text file '%s' could not be read or is empty\n",
321 memcpy(dtext->
text, textbuf, textbuf_size);
322 dtext->
text[textbuf_size] = 0;
328 "Either text or a valid file must be provided\n");
350 if ((err = FT_Init_FreeType(&(dtext->
library)))) {
352 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
362 if ((err = FT_Set_Pixel_Sizes(dtext->
face, 0, dtext->
fontsize))) {
374 if ((err =
load_glyph(ctx, &glyph,
' ') < 0)) {
378 dtext->
tabsize *= glyph->advance;
380 #if !HAVE_LOCALTIME_R
424 FT_Done_Face(dtext->
face);
425 FT_Done_FreeType(dtext->
library);
427 for (i = 0; i < 4; i++) {
436 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
442 uint32_t code = 0, prev_code = 0;
443 int x = 0, y = 0, i = 0, ret;
444 int text_height, baseline;
445 char *text = dtext->
text;
448 int y_min = 32000, y_max = -32000;
450 Glyph *glyph =
NULL, *prev_glyph =
NULL;
456 time_t now = time(0);
462 buf_size = 2*strlen(dtext->
text)+1;
464 localtime_r(&now, <ime);
468 if (strftime(buf, buf_size, dtext->
text, <ime) != 0 || *buf == 0)
493 for (i = 0, p = text; *p; i++) {
505 y_min =
FFMIN(glyph->bbox.yMin, y_min);
506 y_max =
FFMAX(glyph->bbox.yMax, y_max);
508 text_height = y_max - y_min;
513 for (i = 0, p = text; *p; i++) {
517 if (prev_code ==
'\r' && code ==
'\n')
522 str_w =
FFMAX(str_w, x - dtext->
x);
534 if (dtext->
use_kerning && prev_glyph && glyph->code) {
535 FT_Get_Kerning(dtext->
face, prev_glyph->code, glyph->code,
536 ft_kerning_default, &delta);
540 if (x + glyph->bbox.xMax >= width) {
541 str_w =
FFMAX(str_w, x);
547 dtext->
positions[i].x = x + glyph->bitmap_left;
548 dtext->
positions[i].y = y - glyph->bitmap_top + baseline;
550 else x += glyph->advance;
554 y =
FFMIN(y + text_height, height - 1);
624 #define GET_BITMAP_VAL(r, c) \
625 bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
626 (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
627 bitmap->buffer[(r) * bitmap->pitch + (c)]
629 #define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) { \
630 luma_pos = ((x) ) + ((y) ) * picref->linesize[0]; \
631 alpha = yuva_color[3] * (val) * 129; \
632 picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * picref->data[0][luma_pos] ) >> 23; \
633 if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
634 chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
635 chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
636 picref->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * picref->data[1][chroma_pos1]) >> 23; \
637 picref->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * picref->data[2][chroma_pos2]) >> 23; \
642 unsigned int y,
unsigned int width,
unsigned int height,
643 const uint8_t yuva_color[4],
int hsub,
int vsub)
646 unsigned int luma_pos, chroma_pos1, chroma_pos2;
649 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
650 for (c = 0; c < bitmap->width && c+x <
width; c++) {
656 SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
663 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
664 p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
665 alpha = rgba_color[3] * (val) * 129; \
666 *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
667 *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
668 *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
672 unsigned int x,
unsigned int y,
673 unsigned int width,
unsigned int height,
int pixel_step,
680 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
681 for (c = 0; c < bitmap->width && c+x <
width; c++) {
687 SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
688 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
698 int hsub,
int vsub,
int is_rgba_packed,
uint8_t rgba_map[4])
702 if (color[3] != 0xFF) {
703 if (is_rgba_packed) {
705 for (j = 0; j <
height; j++)
706 for (i = 0; i <
width; i++)
708 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
710 unsigned int luma_pos, chroma_pos1, chroma_pos2;
711 for (j = 0; j <
height; j++)
712 for (i = 0; i <
width; i++)
717 line, pixel_step, hsub, vsub,
718 x, y, width, height);
731 for (i = 0, p = text; *p; i++) {
736 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
742 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
743 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
753 yuvcolor, dtext->
hsub, dtext->
vsub);
768 drawbox(picref, dtext->
x, dtext->
y, dtext->
w, dtext->
h,
774 if ((ret =
draw_glyphs(dtext, picref, width, height,
782 if ((ret =
draw_glyphs(dtext, picref, width, height,
798 }
else if (d > INT_MAX || d < INT_MIN) {
799 *n = d > INT_MAX ? INT_MAX : INT_MIN;
834 if (dtext->
x < 0) dtext->
x = 0;
835 if (dtext->
y < 0) dtext->
y = 0;
836 if ((
unsigned)dtext->
x + (
unsigned)dtext->
w > inlink->
w)
837 dtext->
x = inlink->
w - dtext->
w;
838 if ((
unsigned)dtext->
y + (
unsigned)dtext->
h > inlink->
h)
839 dtext->
y = inlink->
h - dtext->
h;
842 dtext->
x &= ~((1 << dtext->
hsub) - 1);
843 dtext->
y &= ~((1 << dtext->
vsub) - 1);
845 av_dlog(ctx,
"n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
847 dtext->
x, dtext->
y, dtext->
x+dtext->
w, dtext->
y+dtext->
h);
881 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
887 .
inputs = avfilter_vf_drawtext_inputs,
888 .
outputs = avfilter_vf_drawtext_outputs,
int draw
set to zero to prevent drawing
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
const AVPixFmtDescriptor * av_pix_fmt_desc_get(enum AVPixelFormat pix_fmt)
static double drand(void *opaque, double min, double max)
AVFilterBufferRefVideoProps * video
video buffer specific properties
static const AVOption drawtext_options[]
unsigned int fontsize
font size to use
int linesize[8]
number of bytes per line
static const char * drawtext_get_name(void *ctx)
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 Y samples)
static const AVFilterPad outputs[]
#define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub)
packed RGB 8:8:8, 24bpp, RGBRGB...
uint8_t * fontfile
font to be used
int h
agreed upon image height
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
int ff_filter_frame(AVFilterLink *link, AVFilterBufferRef *frame)
Send a frame of data to the next filter.
int av_set_options_string(void *ctx, const char *opts, const char *key_val_sep, const char *pairs_sep)
Parse the key/value pairs list in opts.
Various defines for YUV<->RGB conversion.
uint8_t * text
text to be drawn
uint8_t * box_line[4]
line used for filling the box background
av_dlog(ac->avr,"%d samples - audio_convert: %s to %s (%s)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt), use_generic?ac->func_descr_generic:ac->func_descr)
int av_expr_parse(AVExpr **expr, const char *s, const char *const *const_names, const char *const *func1_names, double(*const *funcs1)(void *, double), const char *const *func2_names, double(*const *funcs2)(void *, double, double), int log_offset, void *log_ctx)
Parse an expression.
static int draw_text(AVFilterContext *ctx, AVFilterBufferRef *picref, int width, int height)
uint8_t log2_chroma_w
Amount to shift the luma width right to find the chroma width.
uint8_t shadowcolor[4]
shadow color
uint8_t boxcolor_rgba[4]
background color in RGBA
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
uint8_t fontcolor_rgba[4]
foreground color in RGBA
struct AVTreeNode * av_tree_node_alloc(void)
Allocate an AVTreeNode.
#define AV_PERM_READ
can read from the buffer
const char * name
Pad name.
AVFilterLink ** inputs
array of pointers to input links
AVFilter avfilter_vf_drawtext
static int glyph_enu_free(void *opaque, void *elem)
void * av_tree_find(const AVTreeNode *t, void *key, int(*cmp)(void *key, const void *b), void *next[2])
static av_always_inline av_const int isnan(float x)
FT_Face face
freetype font face handle
void avfilter_unref_bufferp(AVFilterBufferRef **ref)
Remove a reference to a buffer and set the pointer to NULL.
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
static double av_q2d(AVRational a)
Convert rational to double.
static const AVFilterPad avfilter_vf_drawtext_inputs[]
static av_cold void uninit(AVFilterContext *ctx)
FT_Vector * positions
positions for each element in the text
void av_tree_destroy(AVTreeNode *t)
int av_parse_color(uint8_t *rgba_color, const char *color_string, int slen, void *log_ctx)
Put the RGBA values that correspond to color_string in rgba_color.
int64_t pts
presentation timestamp.
char * fontcolor_string
font color as string
A filter pad used for either input or output.
uint8_t fontcolor[4]
foreground color
A link between two filters.
void * av_tree_insert(AVTreeNode **tp, void *key, int(*cmp)(void *key, const void *b), AVTreeNode **next)
Insert or remove an element.
double var_values[VAR_VARS_NB]
void av_file_unmap(uint8_t *bufptr, size_t size)
Unmap or free the buffer bufptr created by av_file_map().
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
int av_file_map(const char *filename, uint8_t **bufptr, size_t *size, int log_offset, void *log_ctx)
Read the file with name filename, and put its content in a newly allocated buffer or map it with mmap...
uint8_t log2_chroma_h
Amount to shift the luma height right to find the chroma height.
AVExpr * y_pexpr
parsed expressions for x and y
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
void * priv
private data for use by the filter
int y
position to start drawing text
AVRational time_base
Define the time base used by the PTS of the frames/samples which will pass through this link...
void av_log(void *avcl, int level, const char *fmt,...)
static av_always_inline av_const double round(double x)
static int dtext_prepare_text(AVFilterContext *ctx)
#define GET_BITMAP_VAL(r, c)
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
static const char *const fun2_names[]
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
int w
agreed upon image width
static int draw_glyph_rgb(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, int pixel_step, const uint8_t rgba_color[4], const uint8_t rgba_map[4])
packed RGB 8:8:8, 24bpp, BGRBGR...
double(* eval_func2)(void *, double a, double b)
int format
agreed upon media format
A reference to an AVFilterBuffer.
static int draw_glyph_yuv(AVFilterBufferRef *picref, FT_Bitmap *bitmap, unsigned int x, unsigned int y, unsigned int width, unsigned int height, const uint8_t yuva_color[4], int hsub, int vsub)
char * boxcolor_string
box color as string
char * av_strdup(const char *s)
Duplicate the string s.
size_t expanded_text_size
size in bytes of the expanded_text buffer
static void drawbox(AVFilterBufferRef *picref, unsigned int x, unsigned int y, unsigned int width, unsigned int height, uint8_t *line[4], int pixel_step[4], uint8_t color[4], int hsub, int vsub, int is_rgba_packed, uint8_t rgba_map[4])
Descriptor that unambiguously describes how the bits of a pixel are stored in the up to 4 data planes...
#define GET_UTF8(val, GET_BYTE, ERROR)
int pixel_step[4]
distance in bytes between the component of each pixel
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
Describe the class of an AVClass context structure.
static const AVFilterPad inputs[]
struct AVTreeNode * glyphs
rendered glyphs, stored using the UTF-32 char code
int h
dimension of the text block
static int is_newline(uint32_t c)
const char * name
filter name
av_cold void av_lfg_init(AVLFG *c, unsigned int seed)
AVFilterLink ** outputs
array of pointers to output links
struct ft_error ft_errors[]
short int draw_box
draw box around text - true or false
static int config_input(AVFilterLink *inlink)
#define RGB_TO_U_CCIR(r1, g1, b1, shift)
#define AV_PERM_PRESERVE
nobody else can overwrite the buffer
static av_cold int init(AVFilterContext *ctx, const char *args)
#define RGB_TO_V_CCIR(r1, g1, b1, shift)
uint8_t shadowcolor_rgba[4]
shadow color in RGBA
uint8_t rgba_map[4]
map RGBA offsets to the positions in the packed RGBA format
static const AVClass drawtext_class
static const char *const var_names[]
int use_kerning
font kerning is used - true/false
int vsub
chroma subsampling values
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
char * shadowcolor_string
shadow color as string
common internal and external API header
static int glyph_cmp(void *key, const void *b)
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
static int query_formats(AVFilterContext *ctx)
FT_Library library
freetype font library handle
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
static const uint8_t color[]
static const AVFilterPad avfilter_vf_drawtext_outputs[]
#define AV_PERM_WRITE
can write to the buffer
#define RGB_TO_Y_CCIR(r, g, b)
uint8_t * expanded_text
used to contain the strftime()-expanded text
static int draw_glyphs(DrawTextContext *dtext, AVFilterBufferRef *picref, int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
double av_expr_eval(AVExpr *e, const double *const_values, void *opaque)
Evaluate a previously parsed expression.
AVFilterContext * dst
dest filter
int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4], enum AVPixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba, uint8_t rgba_map_ptr[4])
uint8_t * data[8]
picture/audio data for each plane
uint8_t boxcolor[4]
background color
static const eval_func2 fun2[]
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
#define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off)
int fix_bounds
do we let it go out of frame bounds - t/f
uint32_t av_get_random_seed(void)
Get random data.
char * textfile
file with text to be drawn
static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
Load glyphs corresponding to the UTF-32 codepoint code.
void ff_draw_rectangle(uint8_t *dst[4], int dst_linesize[4], uint8_t *src[4], int pixelstep[4], int hsub, int vsub, int x, int y, int w, int h)
int ft_load_flags
flags used for loading fonts, see FT_LOAD_*
void av_tree_enumerate(AVTreeNode *t, void *opaque, int(*cmp)(void *opaque, void *elem), int(*enu)(void *opaque, void *elem))
Apply enu(opaque, &elem) to all the elements in the tree in a given range.
AVFilterBufferRef * ff_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
size_t nb_positions
number of elements of positions array
AVPixelFormat
Pixel format.
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static int normalize_double(int *n, double d)
static int filter_frame(AVFilterLink *inlink, AVFilterBufferRef *frame)
simple arithmetic expression evaluator