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."),