48 #include FT_FREETYPE_H
70 static double drand(
void *opaque,
double min,
double max)
114 uint8_t fontcolor[4];
116 uint8_t shadowcolor[4];
117 uint8_t fontcolor_rgba[4];
118 uint8_t boxcolor_rgba[4];
119 uint8_t shadowcolor_rgba[4];
132 uint8_t *box_line[4];
142 #define OFFSET(x) offsetof(DrawTextContext, x)
161 {
"ft_load_flags",
"set font loading flags for libfreetype",
OFFSET(ft_load_flags),
AV_OPT_TYPE_FLAGS, {.dbl=FT_LOAD_DEFAULT|FT_LOAD_RENDER}, 0, INT_MAX, 0,
"ft_load_flags" },
162 {
"default",
"set default", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_DEFAULT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
163 {
"no_scale",
"set no_scale", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_NO_SCALE}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
164 {
"no_hinting",
"set no_hinting", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_NO_HINTING}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
165 {
"render",
"set render", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_RENDER}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
166 {
"no_bitmap",
"set no_bitmap", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_NO_BITMAP}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
167 {
"vertical_layout",
"set vertical_layout", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_VERTICAL_LAYOUT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
168 {
"force_autohint",
"set force_autohint", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_FORCE_AUTOHINT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
169 {
"crop_bitmap",
"set crop_bitmap", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_CROP_BITMAP}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
170 {
"pedantic",
"set pedantic", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_PEDANTIC}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
171 {
"ignore_global_advance_width",
"set ignore_global_advance_width", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
172 {
"no_recurse",
"set no_recurse", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_NO_RECURSE}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
173 {
"ignore_transform",
"set ignore_transform", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_IGNORE_TRANSFORM}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
174 {
"monochrome",
"set monochrome", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_MONOCHROME}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
175 {
"linear_design",
"set linear_design", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_LINEAR_DESIGN}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
176 {
"no_autohint",
"set no_autohint", 0,
AV_OPT_TYPE_CONST, {FT_LOAD_NO_AUTOHINT}, INT_MIN, INT_MAX, 0,
"ft_load_flags" },
191 #undef __FTERRORS_H__
192 #define FT_ERROR_START_LIST {
193 #define FT_ERRORDEF(e, v, s) { (e), (s) },
194 #define FT_ERROR_END_LIST { 0, NULL } };
203 #define FT_ERRMSG(e) ft_errors[e].err_msg
217 const Glyph *
a = key, *bb =
b;
218 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
219 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
238 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
244 if (FT_Get_Glyph(dtext->
face->glyph, glyph->glyph)) {
249 glyph->bitmap = dtext->
face->glyph->bitmap;
250 glyph->bitmap_left = dtext->
face->glyph->bitmap_left;
251 glyph->bitmap_top = dtext->
face->glyph->bitmap_top;
252 glyph->advance = dtext->
face->glyph->advance.x >> 6;
255 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
304 "Both text and text file provided. Please provide only one\n");
309 "The text file '%s' could not be read or is empty\n",
316 memcpy(dtext->
text, textbuf, textbuf_size);
317 dtext->
text[textbuf_size] = 0;
323 "Either text or a valid file must be provided\n");
345 if ((err = FT_Init_FreeType(&(dtext->
library)))) {
347 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
357 if ((err = FT_Set_Pixel_Sizes(dtext->
face, 0, dtext->
fontsize))) {
369 if ((err =
load_glyph(ctx, &glyph,
' ') < 0)) {
373 dtext->
tabsize *= glyph->advance;
375 #if !HAVE_LOCALTIME_R
419 FT_Done_Face(dtext->
face);
420 FT_Done_FreeType(dtext->
library);
422 for (i = 0; i < 4; i++) {
431 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
437 uint32_t code = 0, prev_code = 0;
438 int x = 0, y = 0, i = 0, ret;
439 int text_height, baseline;
440 char *text = dtext->
text;
443 int y_min = 32000, y_max = -32000;
445 Glyph *glyph =
NULL, *prev_glyph =
NULL;
451 time_t now =
time(0);
457 buf_size = 2*strlen(dtext->
text)+1;
459 localtime_r(&now, <ime);
463 if (strftime(buf, buf_size, dtext->
text, <ime) != 0 || *buf == 0)
488 for (i = 0, p = text; *p; i++) {
498 y_min =
FFMIN(glyph->bbox.yMin, y_min);
499 y_max =
FFMAX(glyph->bbox.yMax, y_max);
501 text_height = y_max - y_min;
506 for (i = 0, p = text; *p; i++) {
510 if (prev_code ==
'\r' && code ==
'\n')
515 str_w =
FFMAX(str_w, x - dtext->
x);
527 if (dtext->
use_kerning && prev_glyph && glyph->code) {
528 FT_Get_Kerning(dtext->
face, prev_glyph->code, glyph->code,
529 ft_kerning_default, &delta);
533 if (x + glyph->bbox.xMax >= width) {
534 str_w =
FFMAX(str_w, x);
540 dtext->
positions[i].x = x + glyph->bitmap_left;
541 dtext->
positions[i].y = y - glyph->bitmap_top + baseline;
543 else x += glyph->advance;
547 y =
FFMIN(y + text_height, height - 1);
615 #define GET_BITMAP_VAL(r, c) \
616 bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
617 (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
618 bitmap->buffer[(r) * bitmap->pitch + (c)]
620 #define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub) { \
621 luma_pos = ((x) ) + ((y) ) * picref->linesize[0]; \
622 alpha = yuva_color[3] * (val) * 129; \
623 picref->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * picref->data[0][luma_pos] ) >> 23; \
624 if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
625 chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[1]; \
626 chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * picref->linesize[2]; \
627 picref->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * picref->data[1][chroma_pos1]) >> 23; \
628 picref->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * picref->data[2][chroma_pos2]) >> 23; \
633 unsigned int y,
unsigned int width,
unsigned int height,
634 const uint8_t yuva_color[4],
int hsub,
int vsub)
637 unsigned int luma_pos, chroma_pos1, chroma_pos2;
640 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
641 for (c = 0; c < bitmap->width && c+x <
width; c++) {
647 SET_PIXEL_YUV(picref, yuva_color, src_val, c+x, y+r, hsub, vsub);
654 #define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
655 p = picref->data[0] + (x) * pixel_step + ((y) * picref->linesize[0]); \
656 alpha = rgba_color[3] * (val) * 129; \
657 *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
658 *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
659 *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
663 unsigned int x,
unsigned int y,
664 unsigned int width,
unsigned int height,
int pixel_step,
665 const uint8_t rgba_color[4],
const uint8_t rgba_map[4])
671 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
672 for (c = 0; c < bitmap->width && c+x <
width; c++) {
678 SET_PIXEL_RGB(picref, rgba_color, src_val, c+x, y+r, pixel_step,
679 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
688 uint8_t *
line[4],
int pixel_step[4], uint8_t
color[4],
689 int hsub,
int vsub,
int is_rgba_packed, uint8_t rgba_map[4])
693 if (color[3] != 0xFF) {
694 if (is_rgba_packed) {
696 for (j = 0; j <
height; j++)
697 for (i = 0; i <
width; i++)
699 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
701 unsigned int luma_pos, chroma_pos1, chroma_pos2;
702 for (j = 0; j <
height; j++)
703 for (i = 0; i <
width; i++)
708 line, pixel_step, hsub, vsub,
709 x, y, width, height);
714 int width,
int height,
const uint8_t rgbcolor[4],
const uint8_t yuvcolor[4],
int x,
int y)
722 for (i = 0, p = text; *p; i++) {
727 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
733 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
734 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
744 yuvcolor, dtext->
hsub, dtext->
vsub);
759 drawbox(picref, dtext->
x, dtext->
y, dtext->
w, dtext->
h,
765 if ((ret =
draw_glyphs(dtext, picref, width, height,
773 if ((ret =
draw_glyphs(dtext, picref, width, height,
791 }
else if (d > INT_MAX || d < INT_MIN) {
792 *n = d > INT_MAX ? INT_MAX : INT_MIN;
820 dtext->
draw = fail ? 0 :
826 if (dtext->
x < 0) dtext->
x = 0;
827 if (dtext->
y < 0) dtext->
y = 0;
828 if ((
unsigned)dtext->
x + (
unsigned)dtext->
w > inlink->
w)
829 dtext->
x = inlink->
w - dtext->
w;
830 if ((
unsigned)dtext->
y + (
unsigned)dtext->
h > inlink->
h)
831 dtext->
y = inlink->
h - dtext->
h;
833 dtext->
x &= ~((1 << dtext->
hsub) - 1);
834 dtext->
y &= ~((1 << dtext->
vsub) - 1);
836 av_dlog(ctx,
"n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
838 dtext->
x, dtext->
y, dtext->
x+dtext->
w, dtext->
y+dtext->
h);
860 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
static const char * var_names[]
int draw
set to zero to prevent drawing
planar YUV 4:2:2, 16bpp, (1 Cr & Cb sample per 2x1 Y samples)
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
#define GET_UTF8(val, GET_BYTE, ERROR)
Convert a UTF-8 character (up to 4 bytes) to its 32-bit UCS-4 encoded form.
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)
#define SET_PIXEL_YUV(picref, yuva_color, val, x, y, hsub, vsub)
uint8_t * fontfile
font to be used
int h
agreed upon image height
#define AV_LOG_WARNING
Something somehow does not look correct.
void av_opt_set_defaults(void *s)
Set the values of all AVOption fields to their default values.
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.
planar YUV 4:1:1, 12bpp, (1 Cr & Cb sample per 4x1 Y samples)
Various defines for YUV<->RGB conversion.
uint8_t * text
text to be drawn
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
uint8_t * box_line[4]
line used for filling the box background
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
#define AV_PERM_READ
can read from the buffer
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])
planar YUV 4:4:0 (1 Cr & Cb sample per 1x2 Y samples)
planar YUV 4:2:0, 12bpp, (1 Cr & Cb sample per 2x2 Y samples)
planar YUV 4:1:0, 9bpp, (1 Cr & Cb sample per 4x4 Y samples)
FT_Face face
freetype font face handle
void avfilter_end_frame(AVFilterLink *link)
Notifie the next filter that the current frame has finished.
packed BGRA 8:8:8:8, 32bpp, BGRABGRA...
static double av_q2d(AVRational a)
Convert rational to double.
static av_cold int init(AVFilterContext *ctx, const char *args, void *opaque)
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.
int ff_fill_line_with_color(uint8_t *line[4], int pixel_step[4], int w, uint8_t dst_color[4], enum PixelFormat pix_fmt, uint8_t rgba_color[4], int *is_packed_rgba, uint8_t rgba_map_ptr[4])
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(). ...
void avfilter_set_common_formats(AVFilterContext *ctx, AVFilterFormats *formats)
A helper for query_formats() which sets all links to the same list of formats.
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. ...
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,...)
AVFilterFormats * avfilter_make_format_list(const int *fmts)
Create a list of supported formats.
static int dtext_prepare_text(AVFilterContext *ctx)
#define GET_BITMAP_VAL(r, c)
const AVPixFmtDescriptor av_pix_fmt_descriptors[PIX_FMT_NB]
The array of all the pixel format descriptors.
planar YUV 4:4:4, 24bpp, (1 Cr & Cb sample per 1x1 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])
void avfilter_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Send a slice to the next filter.
#define av_dlog(pctx,...)
av_dlog macros Useful to print debug messages that shouldn't get compiled in normally.
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...
packed ARGB 8:8:8:8, 32bpp, ARGBARGB...
int pixel_step[4]
distance in bytes between the component of each pixel
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static unsigned int av_lfg_get(AVLFG *c)
Get the next random unsigned 32-bit number using an ALFG.
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
static void null_draw_slice(AVFilterLink *link, int y, int h, int slice_dir)
Describe the class of an AVClass context structure.
void avfilter_start_frame(AVFilterLink *link, AVFilterBufferRef *picref)
Notify the next filter of the start of a frame.
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
packed RGB 8:8:8, 24bpp, BGRBGR...
AVFilterBufferRef * avfilter_null_get_video_buffer(AVFilterLink *link, int perms, int w, int h)
get_video_buffer() handler for filters which simply pass video along
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
#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
int use_kerning
font kerning is used - true/false
const int av_tree_node_size
int vsub
chroma subsampling values
char * shadowcolor_string
shadow color as string
static int glyph_cmp(void *key, const void *b)
void av_cold av_lfg_init(AVLFG *c, unsigned int seed)
static int query_formats(AVFilterContext *ctx)
FT_Library library
freetype font library handle
static const uint8_t color[]
#define AV_PERM_WRITE
can write to the buffer
#define RGB_TO_Y_CCIR(r, g, b)
packed RGB 8:8:8, 24bpp, RGBRGB...
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
uint8_t * data[8]
picture/audio data for each plane
uint8_t boxcolor[4]
background color
static const char * fun2_names[]
static const eval_func2 fun2[]
AVFilterBufferRef * cur_buf
packed RGBA 8:8:8:8, 32bpp, RGBARGBA...
static void start_frame(AVFilterLink *inlink, AVFilterBufferRef *inpicref)
#define SET_PIXEL_RGB(picref, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off)
uint32_t av_get_random_seed(void)
Get random data.
packed ABGR 8:8:8:8, 32bpp, ABGRABGR...
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_*
static void end_frame(AVFilterLink *inlink)
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.
size_t nb_positions
number of elements of positions array
static int normalize_double(int *n, double d)
#define AV_NOPTS_VALUE
Undefined timestamp value.
simple arithmetic expression evaluator