50 #include FT_FREETYPE_H
72 static double drand(
void *opaque,
double min,
double max)
145 #define OFFSET(x) offsetof(DrawTextContext, x)
146 #define FLAGS AV_OPT_FLAG_VIDEO_PARAM
163 {
"fix_bounds",
"if true, check and fix text coords to avoid clipping",
167 {
"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,
FLAGS,
"ft_load_flags" },
173 {
"vertical_layout",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags =
FLAGS, .unit =
"ft_load_flags" },
177 {
"ignore_global_advance_width",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags =
FLAGS, .unit =
"ft_load_flags" },
179 {
"ignore_transform",
NULL, 0,
AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags =
FLAGS, .unit =
"ft_load_flags" },
197 #undef __FTERRORS_H__
198 #define FT_ERROR_START_LIST {
199 #define FT_ERRORDEF(e, v, s) { (e), (s) },
200 #define FT_ERROR_END_LIST { 0, NULL } };
209 #define FT_ERRMSG(e) ft_errors[e].err_msg
223 const Glyph *
a = key, *bb =
b;
224 int64_t diff = (int64_t)a->code - (int64_t)bb->code;
225 return diff > 0 ? 1 : diff < 0 ? -1 : 0;
244 !(glyph->glyph =
av_mallocz(
sizeof(*glyph->glyph)))) {
250 if (FT_Get_Glyph(s->
face->glyph, glyph->glyph)) {
255 glyph->bitmap = s->
face->glyph->bitmap;
256 glyph->bitmap_left = s->
face->glyph->bitmap_left;
257 glyph->bitmap_top = s->
face->glyph->bitmap_top;
258 glyph->advance = s->
face->glyph->advance.x >> 6;
261 FT_Glyph_Get_CBox(*glyph->glyph, ft_glyph_bbox_pixels, &glyph->bbox);
299 "Both text and text file provided. Please provide only one\n");
304 "The text file '%s' could not be read or is empty\n",
311 memcpy(s->
text, textbuf, textbuf_size);
312 s->
text[textbuf_size] = 0;
318 "Either text or a valid file must be provided\n");
340 if ((err = FT_Init_FreeType(&(s->
library)))) {
342 "Could not load FreeType: %s\n",
FT_ERRMSG(err));
352 if ((err = FT_Set_Pixel_Sizes(s->
face, 0, s->
fontsize))) {
364 if ((err =
load_glyph(ctx, &glyph,
' ') < 0)) {
370 #if !HAVE_LOCALTIME_R
413 FT_Done_Face(s->
face);
416 for (i = 0; i < 4; i++) {
425 return c ==
'\n' || c ==
'\r' || c ==
'\f' || c ==
'\v';
431 uint32_t code = 0, prev_code = 0;
432 int x = 0, y = 0, i = 0, ret;
433 int text_height, baseline;
434 char *text = s->
text;
437 int y_min = 32000, y_max = -32000;
439 Glyph *glyph =
NULL, *prev_glyph =
NULL;
445 time_t now = time(0);
451 buf_size = 2*strlen(s->
text)+1;
453 localtime_r(&now, <ime);
457 if (strftime(buf, buf_size, s->
text, <ime) != 0 || *buf == 0)
482 for (i = 0, p = text; *p; i++) {
494 y_min =
FFMIN(glyph->bbox.yMin, y_min);
495 y_max =
FFMAX(glyph->bbox.yMax, y_max);
497 text_height = y_max - y_min;
502 for (i = 0, p = text; *p; i++) {
506 if (prev_code ==
'\r' && code ==
'\n')
511 str_w =
FFMAX(str_w, x - s->
x);
524 FT_Get_Kerning(s->
face, prev_glyph->code, glyph->code,
525 ft_kerning_default, &delta);
529 if (x + glyph->bbox.xMax >= width) {
530 str_w =
FFMAX(str_w, x);
536 s->
positions[i].x = x + glyph->bitmap_left;
537 s->
positions[i].y = y - glyph->bitmap_top + baseline;
539 else x += glyph->advance;
543 y =
FFMIN(y + text_height, height - 1);
616 #define GET_BITMAP_VAL(r, c) \
617 bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
618 (bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
619 bitmap->buffer[(r) * bitmap->pitch + (c)]
621 #define SET_PIXEL_YUV(frame, yuva_color, val, x, y, hsub, vsub) { \
622 luma_pos = ((x) ) + ((y) ) * frame->linesize[0]; \
623 alpha = yuva_color[3] * (val) * 129; \
624 frame->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * frame->data[0][luma_pos] ) >> 23; \
625 if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
626 chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * frame->linesize[1]; \
627 chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * frame->linesize[2]; \
628 frame->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * frame->data[1][chroma_pos1]) >> 23; \
629 frame->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * frame->data[2][chroma_pos2]) >> 23; \
634 unsigned int y,
unsigned int width,
unsigned int height,
635 const uint8_t yuva_color[4],
int hsub,
int vsub)
638 unsigned int luma_pos, chroma_pos1, chroma_pos2;
641 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
642 for (c = 0; c < bitmap->width && c+x <
width; c++) {
648 SET_PIXEL_YUV(frame, yuva_color, src_val, c+x, y+r, hsub, vsub);
655 #define SET_PIXEL_RGB(frame, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off) { \
656 p = frame->data[0] + (x) * pixel_step + ((y) * frame->linesize[0]); \
657 alpha = rgba_color[3] * (val) * 129; \
658 *(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
659 *(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
660 *(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
664 unsigned int x,
unsigned int y,
665 unsigned int width,
unsigned int height,
int pixel_step,
672 for (r = 0; r < bitmap->rows && r+y <
height; r++) {
673 for (c = 0; c < bitmap->width && c+x <
width; c++) {
679 SET_PIXEL_RGB(frame, rgba_color, src_val, c+x, y+r, pixel_step,
680 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
690 int hsub,
int vsub,
int is_rgba_packed,
uint8_t rgba_map[4])
694 if (color[3] != 0xFF) {
695 if (is_rgba_packed) {
697 for (j = 0; j <
height; j++)
698 for (i = 0; i <
width; i++)
700 rgba_map[0], rgba_map[1], rgba_map[2], rgba_map[3]);
702 unsigned int luma_pos, chroma_pos1, chroma_pos2;
703 for (j = 0; j <
height; j++)
704 for (i = 0; i <
width; i++)
709 line, pixel_step, hsub, vsub,
710 x, y, width, height);
723 for (i = 0, p = text; *p; i++) {
728 if (code ==
'\n' || code ==
'\r' || code ==
'\t')
734 if (glyph->bitmap.pixel_mode != FT_PIXEL_MODE_MONO &&
735 glyph->bitmap.pixel_mode != FT_PIXEL_MODE_GRAY)
790 }
else if (d > INT_MAX || d < INT_MIN) {
791 *n = d > INT_MAX ? INT_MAX : INT_MIN;
826 if (s->
x < 0) s->
x = 0;
827 if (s->
y < 0) s->
y = 0;
828 if ((
unsigned)s->
x + (
unsigned)s->
w > inlink->
w)
829 s->
x = inlink->
w - s->
w;
830 if ((
unsigned)s->
y + (
unsigned)s->
h > inlink->
h)
831 s->
y = inlink->
h - s->
h;
834 s->
x &= ~((1 << s->
hsub) - 1);
835 s->
y &= ~((1 << s->
vsub) - 1);
837 av_dlog(ctx,
"n:%d t:%f x:%d y:%d x+w:%d y+h:%d\n",
839 s->
x, s->
y, s->
x+s->
w, s->
y+s->
h);
871 .description =
NULL_IF_CONFIG_SMALL(
"Draw text on top of video frames using libfreetype library."),
873 .priv_class = &drawtext_class,
878 .
inputs = avfilter_vf_drawtext_inputs,
879 .
outputs = avfilter_vf_drawtext_outputs,