vf_drawtext.c 70 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914
  1. /*
  2. * Copyright (c) 2023 Francesco Carusi
  3. * Copyright (c) 2011 Stefano Sabatini
  4. * Copyright (c) 2010 S.N. Hemanth Meenakshisundaram
  5. * Copyright (c) 2003 Gustavo Sverzut Barbieri <gsbarbieri@yahoo.com.br>
  6. *
  7. * This file is part of FFmpeg.
  8. *
  9. * FFmpeg is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2.1 of the License, or (at your option) any later version.
  13. *
  14. * FFmpeg is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with FFmpeg; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. /**
  24. * @file
  25. * drawtext filter, based on the original vhook/drawtext.c
  26. * filter by Gustavo Sverzut Barbieri
  27. */
  28. #include "config.h"
  29. #if HAVE_SYS_TIME_H
  30. #include <sys/time.h>
  31. #endif
  32. #include <sys/types.h>
  33. #include <sys/stat.h>
  34. #include <time.h>
  35. #if HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include <fenv.h>
  39. #if CONFIG_LIBFONTCONFIG
  40. #include <fontconfig/fontconfig.h>
  41. #endif
  42. #include "libavutil/avstring.h"
  43. #include "libavutil/bprint.h"
  44. #include "libavutil/common.h"
  45. #include "libavutil/eval.h"
  46. #include "libavutil/mem.h"
  47. #include "libavutil/opt.h"
  48. #include "libavutil/random_seed.h"
  49. #include "libavutil/parseutils.h"
  50. #include "libavutil/time.h"
  51. #include "libavutil/timecode.h"
  52. #include "libavutil/time_internal.h"
  53. #include "libavutil/tree.h"
  54. #include "libavutil/lfg.h"
  55. #include "libavutil/detection_bbox.h"
  56. #include "avfilter.h"
  57. #include "drawutils.h"
  58. #include "filters.h"
  59. #include "formats.h"
  60. #include "textutils.h"
  61. #include "video.h"
  62. #if CONFIG_LIBFRIBIDI
  63. #include <fribidi.h>
  64. #endif
  65. #include <ft2build.h>
  66. #include FT_FREETYPE_H
  67. #include FT_GLYPH_H
  68. #include FT_STROKER_H
  69. #include <hb.h>
  70. #include <hb-ft.h>
  71. // Ceiling operation for positive integers division
  72. #define POS_CEIL(x, y) ((x)/(y) + ((x)%(y) != 0))
  73. static const char *const var_names[] = {
  74. "dar",
  75. "hsub", "vsub",
  76. "line_h", "lh", ///< line height
  77. "main_h", "h", "H", ///< height of the input video
  78. "main_w", "w", "W", ///< width of the input video
  79. "max_glyph_a", "ascent", ///< max glyph ascender
  80. "max_glyph_d", "descent", ///< min glyph descender
  81. "max_glyph_h", ///< max glyph height
  82. "max_glyph_w", ///< max glyph width
  83. "font_a", ///< font-defined ascent
  84. "font_d", ///< font-defined descent
  85. "top_a", ///< max glyph ascender of the top line
  86. "bottom_d", ///< max glyph descender of the bottom line
  87. "n", ///< number of frame
  88. "sar",
  89. "t", ///< timestamp expressed in seconds
  90. "text_h", "th", ///< height of the rendered text
  91. "text_w", "tw", ///< width of the rendered text
  92. "x",
  93. "y",
  94. "pict_type",
  95. "duration",
  96. NULL
  97. };
  98. static const char *const fun2_names[] = {
  99. "rand"
  100. };
  101. static double drand(void *opaque, double min, double max)
  102. {
  103. return min + (max-min) / UINT_MAX * av_lfg_get(opaque);
  104. }
  105. typedef double (*eval_func2)(void *, double a, double b);
  106. static const eval_func2 fun2[] = {
  107. drand,
  108. NULL
  109. };
  110. enum var_name {
  111. VAR_DAR,
  112. VAR_HSUB, VAR_VSUB,
  113. VAR_LINE_H, VAR_LH,
  114. VAR_MAIN_H, VAR_h, VAR_H,
  115. VAR_MAIN_W, VAR_w, VAR_W,
  116. VAR_MAX_GLYPH_A, VAR_ASCENT,
  117. VAR_MAX_GLYPH_D, VAR_DESCENT,
  118. VAR_MAX_GLYPH_H,
  119. VAR_MAX_GLYPH_W,
  120. VAR_FONT_A,
  121. VAR_FONT_D,
  122. VAR_TOP_A,
  123. VAR_BOTTOM_D,
  124. VAR_N,
  125. VAR_SAR,
  126. VAR_T,
  127. VAR_TEXT_H, VAR_TH,
  128. VAR_TEXT_W, VAR_TW,
  129. VAR_X,
  130. VAR_Y,
  131. VAR_PICT_TYPE,
  132. VAR_DURATION,
  133. VAR_VARS_NB
  134. };
  135. enum expansion_mode {
  136. EXP_NONE,
  137. EXP_NORMAL,
  138. EXP_STRFTIME,
  139. };
  140. enum y_alignment {
  141. YA_TEXT,
  142. YA_BASELINE,
  143. YA_FONT,
  144. };
  145. enum text_alignment {
  146. TA_LEFT = (1 << 0),
  147. TA_RIGHT = (1 << 1),
  148. TA_TOP = (1 << 2),
  149. TA_BOTTOM = (1 << 3),
  150. };
  151. typedef struct HarfbuzzData {
  152. hb_buffer_t* buf;
  153. hb_font_t* font;
  154. unsigned int glyph_count;
  155. hb_glyph_info_t* glyph_info;
  156. hb_glyph_position_t* glyph_pos;
  157. } HarfbuzzData;
  158. /** Information about a single glyph in a text line */
  159. typedef struct GlyphInfo {
  160. uint32_t code; ///< the glyph code point
  161. int x; ///< the x position of the glyph
  162. int y; ///< the y position of the glyph
  163. int shift_x64; ///< the horizontal shift of the glyph in 26.6 units
  164. int shift_y64; ///< the vertical shift of the glyph in 26.6 units
  165. } GlyphInfo;
  166. /** Information about a single line of text */
  167. typedef struct TextLine {
  168. int offset_left64; ///< offset between the origin and
  169. /// the leftmost pixel of the first glyph
  170. int offset_right64; ///< maximum offset between the origin and
  171. /// the rightmost pixel of the last glyph
  172. int width64; ///< width of the line
  173. HarfbuzzData hb_data; ///< libharfbuzz data of this text line
  174. GlyphInfo* glyphs; ///< array of glyphs in this text line
  175. int cluster_offset; ///< the offset at which this line begins
  176. } TextLine;
  177. /** A glyph as loaded and rendered using libfreetype */
  178. typedef struct Glyph {
  179. FT_Glyph glyph;
  180. FT_Glyph border_glyph;
  181. uint32_t code;
  182. unsigned int fontsize;
  183. /** Glyph bitmaps with 1/4 pixel precision in both directions */
  184. FT_BitmapGlyph bglyph[16];
  185. /** Outlined glyph bitmaps with 1/4 pixel precision in both directions */
  186. FT_BitmapGlyph border_bglyph[16];
  187. FT_BBox bbox;
  188. } Glyph;
  189. /** Global text metrics */
  190. typedef struct TextMetrics {
  191. int offset_top64; ///< ascender amount of the first line (in 26.6 units)
  192. int offset_bottom64; ///< descender amount of the last line (in 26.6 units)
  193. int offset_left64; ///< maximum offset between the origin and
  194. /// the leftmost pixel of the first glyph
  195. /// of each line (in 26.6 units)
  196. int offset_right64; ///< maximum offset between the origin and
  197. /// the rightmost pixel of the last glyph
  198. /// of each line (in 26.6 units)
  199. int line_height64; ///< the font-defined line height
  200. int width; ///< width of the longest line - ceil(width64/64)
  201. int height; ///< total height of the text - ceil(height64/64)
  202. int min_y64; ///< minimum value of bbox.yMin among glyphs (in 26.6 units)
  203. int max_y64; ///< maximum value of bbox.yMax among glyphs (in 26.6 units)
  204. int min_x64; ///< minimum value of bbox.xMin among glyphs (in 26.6 units)
  205. int max_x64; ///< maximum value of bbox.xMax among glyphs (in 26.6 units)
  206. // Position of the background box (without borders)
  207. int rect_x; ///< x position of the box
  208. int rect_y; ///< y position of the box
  209. } TextMetrics;
  210. typedef struct DrawTextContext {
  211. const AVClass *class;
  212. int exp_mode; ///< expansion mode to use for the text
  213. FFExpandTextContext expand_text; ///< expand text in case exp_mode == NORMAL
  214. int reinit; ///< tells if the filter is being reinited
  215. #if CONFIG_LIBFONTCONFIG
  216. uint8_t *font; ///< font to be used
  217. #endif
  218. uint8_t *fontfile; ///< font to be used
  219. uint8_t *text; ///< text to be drawn
  220. AVBPrint expanded_text; ///< used to contain the expanded text
  221. uint8_t *fontcolor_expr; ///< fontcolor expression to evaluate
  222. AVBPrint expanded_fontcolor; ///< used to contain the expanded fontcolor spec
  223. int ft_load_flags; ///< flags used for loading fonts, see FT_LOAD_*
  224. char *textfile; ///< file with text to be drawn
  225. double x; ///< x position to start drawing text
  226. double y; ///< y position to start drawing text
  227. int max_glyph_w; ///< max glyph width
  228. int max_glyph_h; ///< max glyph height
  229. int shadowx, shadowy;
  230. int borderw; ///< border width
  231. char *fontsize_expr; ///< expression for fontsize
  232. AVExpr *fontsize_pexpr; ///< parsed expressions for fontsize
  233. unsigned int fontsize; ///< font size to use
  234. unsigned int default_fontsize; ///< default font size to use
  235. int line_spacing; ///< lines spacing in pixels
  236. short int draw_box; ///< draw box around text - true or false
  237. char *boxborderw; ///< box border width (padding)
  238. /// allowed formats: "all", "vert|oriz", "top|right|bottom|left"
  239. int bb_top; ///< the size of the top box border
  240. int bb_right; ///< the size of the right box border
  241. int bb_bottom; ///< the size of the bottom box border
  242. int bb_left; ///< the size of the left box border
  243. int box_width; ///< the width of box
  244. int box_height; ///< the height of box
  245. int tabsize; ///< tab size
  246. int fix_bounds; ///< do we let it go out of frame bounds - t/f
  247. FFDrawContext dc;
  248. FFDrawColor fontcolor; ///< foreground color
  249. FFDrawColor shadowcolor; ///< shadow color
  250. FFDrawColor bordercolor; ///< border color
  251. FFDrawColor boxcolor; ///< background color
  252. FT_Library library; ///< freetype font library handle
  253. FT_Face face; ///< freetype font face handle
  254. FT_Stroker stroker; ///< freetype stroker handle
  255. struct AVTreeNode *glyphs; ///< rendered glyphs, stored using the UTF-32 char code
  256. char *x_expr; ///< expression for x position
  257. char *y_expr; ///< expression for y position
  258. AVExpr *x_pexpr, *y_pexpr; ///< parsed expressions for x and y
  259. int64_t basetime; ///< base pts time in the real world for display
  260. double var_values[VAR_VARS_NB];
  261. char *a_expr;
  262. AVExpr *a_pexpr;
  263. int alpha;
  264. AVLFG prng; ///< random
  265. char *tc_opt_string; ///< specified timecode option string
  266. AVRational tc_rate; ///< frame rate for timecode
  267. AVTimecode tc; ///< timecode context
  268. int tc24hmax; ///< 1 if timecode is wrapped to 24 hours, 0 otherwise
  269. int reload; ///< reload text file at specified frame interval
  270. int start_number; ///< starting frame number for n/frame_num var
  271. char *text_source_string; ///< the string to specify text data source
  272. enum AVFrameSideDataType text_source;
  273. #if CONFIG_LIBFRIBIDI
  274. int text_shaping; ///< 1 to shape the text before drawing it
  275. #endif
  276. AVDictionary *metadata;
  277. int boxw; ///< the value of the boxw parameter
  278. int boxh; ///< the value of the boxh parameter
  279. int text_align; ///< the horizontal and vertical text alignment
  280. int y_align; ///< the value of the y_align parameter
  281. TextLine *lines; ///< computed information about text lines
  282. int line_count; ///< the number of text lines
  283. uint32_t *tab_clusters; ///< the position of tab characters in the text
  284. int tab_count; ///< the number of tab characters
  285. int blank_advance64; ///< the size of the space character
  286. int tab_warning_printed; ///< ensure the tab warning to be printed only once
  287. } DrawTextContext;
  288. #define OFFSET(x) offsetof(DrawTextContext, x)
  289. #define FLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM
  290. #define TFLAGS AV_OPT_FLAG_FILTERING_PARAM|AV_OPT_FLAG_VIDEO_PARAM|AV_OPT_FLAG_RUNTIME_PARAM
  291. static const AVOption drawtext_options[]= {
  292. {"fontfile", "set font file", OFFSET(fontfile), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS},
  293. {"text", "set text", OFFSET(text), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, TFLAGS},
  294. {"textfile", "set text file", OFFSET(textfile), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS},
  295. {"fontcolor", "set foreground color", OFFSET(fontcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, TFLAGS},
  296. {"fontcolor_expr", "set foreground color expression", OFFSET(fontcolor_expr), AV_OPT_TYPE_STRING, {.str=""}, 0, 0, FLAGS},
  297. {"boxcolor", "set box color", OFFSET(boxcolor.rgba), AV_OPT_TYPE_COLOR, {.str="white"}, 0, 0, TFLAGS},
  298. {"bordercolor", "set border color", OFFSET(bordercolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, TFLAGS},
  299. {"shadowcolor", "set shadow color", OFFSET(shadowcolor.rgba), AV_OPT_TYPE_COLOR, {.str="black"}, 0, 0, TFLAGS},
  300. {"box", "set box", OFFSET(draw_box), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, TFLAGS},
  301. {"boxborderw", "set box borders width", OFFSET(boxborderw), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
  302. {"line_spacing", "set line spacing in pixels", OFFSET(line_spacing), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, TFLAGS},
  303. {"fontsize", "set font size", OFFSET(fontsize_expr), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, TFLAGS},
  304. {"text_align", "set text alignment", OFFSET(text_align), AV_OPT_TYPE_FLAGS, {.i64=0}, 0, (TA_LEFT|TA_RIGHT|TA_TOP|TA_BOTTOM), TFLAGS, .unit = "text_align"},
  305. { "left", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_LEFT }, .flags = TFLAGS, .unit = "text_align" },
  306. { "L", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_LEFT }, .flags = TFLAGS, .unit = "text_align" },
  307. { "right", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_RIGHT }, .flags = TFLAGS, .unit = "text_align" },
  308. { "R", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_RIGHT }, .flags = TFLAGS, .unit = "text_align" },
  309. { "center", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_LEFT|TA_RIGHT) }, .flags = TFLAGS, .unit = "text_align" },
  310. { "C", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_LEFT|TA_RIGHT) }, .flags = TFLAGS, .unit = "text_align" },
  311. { "top", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_TOP }, .flags = TFLAGS, .unit = "text_align" },
  312. { "T", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_TOP }, .flags = TFLAGS, .unit = "text_align" },
  313. { "bottom", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_BOTTOM }, .flags = TFLAGS, .unit = "text_align" },
  314. { "B", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = TA_BOTTOM }, .flags = TFLAGS, .unit = "text_align" },
  315. { "middle", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_TOP|TA_BOTTOM) }, .flags = TFLAGS, .unit = "text_align" },
  316. { "M", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = (TA_TOP|TA_BOTTOM) }, .flags = TFLAGS, .unit = "text_align" },
  317. {"x", "set x expression", OFFSET(x_expr), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
  318. {"y", "set y expression", OFFSET(y_expr), AV_OPT_TYPE_STRING, {.str="0"}, 0, 0, TFLAGS},
  319. {"boxw", "set box width", OFFSET(boxw), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, TFLAGS},
  320. {"boxh", "set box height", OFFSET(boxh), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, TFLAGS},
  321. {"shadowx", "set shadow x offset", OFFSET(shadowx), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, TFLAGS},
  322. {"shadowy", "set shadow y offset", OFFSET(shadowy), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, TFLAGS},
  323. {"borderw", "set border width", OFFSET(borderw), AV_OPT_TYPE_INT, {.i64=0}, INT_MIN, INT_MAX, TFLAGS},
  324. {"tabsize", "set tab size", OFFSET(tabsize), AV_OPT_TYPE_INT, {.i64=4}, 0, INT_MAX, TFLAGS},
  325. {"basetime", "set base time", OFFSET(basetime), AV_OPT_TYPE_INT64, {.i64=AV_NOPTS_VALUE}, INT64_MIN, INT64_MAX, FLAGS},
  326. #if CONFIG_LIBFONTCONFIG
  327. { "font", "Font name", OFFSET(font), AV_OPT_TYPE_STRING, { .str = "Sans" }, .flags = FLAGS },
  328. #endif
  329. {"expansion", "set the expansion mode", OFFSET(exp_mode), AV_OPT_TYPE_INT, {.i64=EXP_NORMAL}, 0, 2, FLAGS, .unit = "expansion"},
  330. {"none", "set no expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NONE}, 0, 0, FLAGS, .unit = "expansion"},
  331. {"normal", "set normal expansion", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_NORMAL}, 0, 0, FLAGS, .unit = "expansion"},
  332. {"strftime", "set strftime expansion (deprecated)", OFFSET(exp_mode), AV_OPT_TYPE_CONST, {.i64=EXP_STRFTIME}, 0, 0, FLAGS, .unit = "expansion"},
  333. {"y_align", "set the y alignment", OFFSET(y_align), AV_OPT_TYPE_INT, {.i64=YA_TEXT}, 0, 2, TFLAGS, .unit = "y_align"},
  334. {"text", "y is referred to the top of the first text line", OFFSET(y_align), AV_OPT_TYPE_CONST, {.i64=YA_TEXT}, 0, 0, FLAGS, .unit = "y_align"},
  335. {"baseline", "y is referred to the baseline of the first line", OFFSET(y_align), AV_OPT_TYPE_CONST, {.i64=YA_BASELINE}, 0, 0, FLAGS, .unit = "y_align"},
  336. {"font", "y is referred to the font defined line metrics", OFFSET(y_align), AV_OPT_TYPE_CONST, {.i64=YA_FONT}, 0, 0, FLAGS, .unit = "y_align"},
  337. {"timecode", "set initial timecode", OFFSET(tc_opt_string), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 0, FLAGS},
  338. {"tc24hmax", "set 24 hours max (timecode only)", OFFSET(tc24hmax), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  339. {"timecode_rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  340. {"r", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  341. {"rate", "set rate (timecode only)", OFFSET(tc_rate), AV_OPT_TYPE_RATIONAL, {.dbl=0}, 0, INT_MAX, FLAGS},
  342. {"reload", "reload text file at specified frame interval", OFFSET(reload), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
  343. {"alpha", "apply alpha while rendering", OFFSET(a_expr), AV_OPT_TYPE_STRING, {.str = "1"}, .flags = TFLAGS},
  344. {"fix_bounds", "check and fix text coords to avoid clipping", OFFSET(fix_bounds), AV_OPT_TYPE_BOOL, {.i64=0}, 0, 1, FLAGS},
  345. {"start_number", "start frame number for n/frame_num variable", OFFSET(start_number), AV_OPT_TYPE_INT, {.i64=0}, 0, INT_MAX, FLAGS},
  346. {"text_source", "the source of text", OFFSET(text_source_string), AV_OPT_TYPE_STRING, {.str=NULL}, 0, 1, FLAGS },
  347. #if CONFIG_LIBFRIBIDI
  348. {"text_shaping", "attempt to shape text before drawing", OFFSET(text_shaping), AV_OPT_TYPE_BOOL, {.i64=1}, 0, 1, FLAGS},
  349. #endif
  350. /* FT_LOAD_* flags */
  351. { "ft_load_flags", "set font loading flags for libfreetype", OFFSET(ft_load_flags), AV_OPT_TYPE_FLAGS, { .i64 = FT_LOAD_DEFAULT }, 0, INT_MAX, FLAGS, .unit = "ft_load_flags" },
  352. { "default", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_DEFAULT }, .flags = FLAGS, .unit = "ft_load_flags" },
  353. { "no_scale", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_SCALE }, .flags = FLAGS, .unit = "ft_load_flags" },
  354. { "no_hinting", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_HINTING }, .flags = FLAGS, .unit = "ft_load_flags" },
  355. { "render", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_RENDER }, .flags = FLAGS, .unit = "ft_load_flags" },
  356. { "no_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
  357. { "vertical_layout", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_VERTICAL_LAYOUT }, .flags = FLAGS, .unit = "ft_load_flags" },
  358. { "force_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_FORCE_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
  359. { "crop_bitmap", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_CROP_BITMAP }, .flags = FLAGS, .unit = "ft_load_flags" },
  360. { "pedantic", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_PEDANTIC }, .flags = FLAGS, .unit = "ft_load_flags" },
  361. { "ignore_global_advance_width", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_GLOBAL_ADVANCE_WIDTH }, .flags = FLAGS, .unit = "ft_load_flags" },
  362. { "no_recurse", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_RECURSE }, .flags = FLAGS, .unit = "ft_load_flags" },
  363. { "ignore_transform", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_IGNORE_TRANSFORM }, .flags = FLAGS, .unit = "ft_load_flags" },
  364. { "monochrome", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_MONOCHROME }, .flags = FLAGS, .unit = "ft_load_flags" },
  365. { "linear_design", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_LINEAR_DESIGN }, .flags = FLAGS, .unit = "ft_load_flags" },
  366. { "no_autohint", NULL, 0, AV_OPT_TYPE_CONST, { .i64 = FT_LOAD_NO_AUTOHINT }, .flags = FLAGS, .unit = "ft_load_flags" },
  367. { NULL }
  368. };
  369. AVFILTER_DEFINE_CLASS(drawtext);
  370. #undef __FTERRORS_H__
  371. #define FT_ERROR_START_LIST {
  372. #define FT_ERRORDEF(e, v, s) { (e), (s) },
  373. #define FT_ERROR_END_LIST { 0, NULL } };
  374. static const struct ft_error {
  375. int err;
  376. const char *err_msg;
  377. } ft_errors[] =
  378. #include FT_ERRORS_H
  379. #define FT_ERRMSG(e) ft_errors[e].err_msg
  380. static int glyph_cmp(const void *key, const void *b)
  381. {
  382. const Glyph *a = key, *bb = b;
  383. int64_t diff = (int64_t)a->code - (int64_t)bb->code;
  384. if (diff != 0)
  385. return diff > 0 ? 1 : -1;
  386. else
  387. return FFDIFFSIGN((int64_t)a->fontsize, (int64_t)bb->fontsize);
  388. }
  389. static av_cold int set_fontsize(AVFilterContext *ctx, unsigned int fontsize)
  390. {
  391. int err;
  392. DrawTextContext *s = ctx->priv;
  393. if ((err = FT_Set_Pixel_Sizes(s->face, 0, fontsize))) {
  394. av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n",
  395. fontsize, FT_ERRMSG(err));
  396. return AVERROR(EINVAL);
  397. }
  398. // Whenever the underlying FT_Face changes, harfbuzz has to be notified of the change.
  399. for (int line = 0; line < s->line_count; line++) {
  400. TextLine *cur_line = &s->lines[line];
  401. hb_ft_font_changed(cur_line->hb_data.font);
  402. }
  403. s->fontsize = fontsize;
  404. return 0;
  405. }
  406. static av_cold int parse_fontsize(AVFilterContext *ctx)
  407. {
  408. DrawTextContext *s = ctx->priv;
  409. int err;
  410. if (s->fontsize_pexpr)
  411. return 0;
  412. if (s->fontsize_expr == NULL)
  413. return AVERROR(EINVAL);
  414. if ((err = av_expr_parse(&s->fontsize_pexpr, s->fontsize_expr, var_names,
  415. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0)
  416. return err;
  417. return 0;
  418. }
  419. static av_cold int update_fontsize(AVFilterContext *ctx)
  420. {
  421. DrawTextContext *s = ctx->priv;
  422. unsigned int fontsize = s->default_fontsize;
  423. int err;
  424. double size, roundedsize;
  425. // if no fontsize specified use the default
  426. if (s->fontsize_expr != NULL) {
  427. if ((err = parse_fontsize(ctx)) < 0)
  428. return err;
  429. size = av_expr_eval(s->fontsize_pexpr, s->var_values, &s->prng);
  430. if (!isnan(size)) {
  431. roundedsize = round(size);
  432. // test for overflow before cast
  433. if (!(roundedsize > INT_MIN && roundedsize < INT_MAX)) {
  434. av_log(ctx, AV_LOG_ERROR, "fontsize overflow\n");
  435. return AVERROR(EINVAL);
  436. }
  437. fontsize = roundedsize;
  438. }
  439. }
  440. if (fontsize == 0)
  441. fontsize = 1;
  442. // no change
  443. if (fontsize == s->fontsize)
  444. return 0;
  445. return set_fontsize(ctx, fontsize);
  446. }
  447. static int load_font_file(AVFilterContext *ctx, const char *path, int index)
  448. {
  449. DrawTextContext *s = ctx->priv;
  450. int err;
  451. err = FT_New_Face(s->library, path, index, &s->face);
  452. if (err) {
  453. #if !CONFIG_LIBFONTCONFIG
  454. av_log(ctx, AV_LOG_ERROR, "Could not load font \"%s\": %s\n",
  455. s->fontfile, FT_ERRMSG(err));
  456. #endif
  457. return AVERROR(EINVAL);
  458. }
  459. return 0;
  460. }
  461. #if CONFIG_LIBFONTCONFIG
  462. static int load_font_fontconfig(AVFilterContext *ctx)
  463. {
  464. DrawTextContext *s = ctx->priv;
  465. FcConfig *fontconfig;
  466. FcPattern *pat, *best;
  467. FcResult result = FcResultMatch;
  468. FcChar8 *filename;
  469. int index;
  470. double size;
  471. int err = AVERROR(ENOENT);
  472. int parse_err;
  473. fontconfig = FcInitLoadConfigAndFonts();
  474. if (!fontconfig) {
  475. av_log(ctx, AV_LOG_ERROR, "impossible to init fontconfig\n");
  476. return AVERROR_UNKNOWN;
  477. }
  478. pat = FcNameParse(s->fontfile ? s->fontfile :
  479. (uint8_t *)(intptr_t)"default");
  480. if (!pat) {
  481. av_log(ctx, AV_LOG_ERROR, "could not parse fontconfig pat");
  482. return AVERROR(EINVAL);
  483. }
  484. FcPatternAddString(pat, FC_FAMILY, s->font);
  485. parse_err = parse_fontsize(ctx);
  486. if (!parse_err) {
  487. double size = av_expr_eval(s->fontsize_pexpr, s->var_values, &s->prng);
  488. if (isnan(size)) {
  489. av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
  490. return AVERROR(EINVAL);
  491. }
  492. FcPatternAddDouble(pat, FC_SIZE, size);
  493. }
  494. FcDefaultSubstitute(pat);
  495. if (!FcConfigSubstitute(fontconfig, pat, FcMatchPattern)) {
  496. av_log(ctx, AV_LOG_ERROR, "could not substitute fontconfig options"); /* very unlikely */
  497. FcPatternDestroy(pat);
  498. return AVERROR(ENOMEM);
  499. }
  500. best = FcFontMatch(fontconfig, pat, &result);
  501. FcPatternDestroy(pat);
  502. if (!best || result != FcResultMatch) {
  503. av_log(ctx, AV_LOG_ERROR,
  504. "Cannot find a valid font for the family %s\n",
  505. s->font);
  506. goto fail;
  507. }
  508. if (
  509. FcPatternGetInteger(best, FC_INDEX, 0, &index ) != FcResultMatch ||
  510. FcPatternGetDouble (best, FC_SIZE, 0, &size ) != FcResultMatch) {
  511. av_log(ctx, AV_LOG_ERROR, "impossible to find font information");
  512. return AVERROR(EINVAL);
  513. }
  514. if (FcPatternGetString(best, FC_FILE, 0, &filename) != FcResultMatch) {
  515. av_log(ctx, AV_LOG_ERROR, "No file path for %s\n",
  516. s->font);
  517. goto fail;
  518. }
  519. av_log(ctx, AV_LOG_VERBOSE, "Using \"%s\"\n", filename);
  520. if (parse_err)
  521. s->default_fontsize = size + 0.5;
  522. err = load_font_file(ctx, filename, index);
  523. if (err)
  524. return err;
  525. FcConfigDestroy(fontconfig);
  526. fail:
  527. FcPatternDestroy(best);
  528. return err;
  529. }
  530. #endif
  531. static int load_font(AVFilterContext *ctx)
  532. {
  533. DrawTextContext *s = ctx->priv;
  534. int err;
  535. /* load the face, and set up the encoding, which is by default UTF-8 */
  536. err = load_font_file(ctx, s->fontfile, 0);
  537. if (!err)
  538. return 0;
  539. #if CONFIG_LIBFONTCONFIG
  540. err = load_font_fontconfig(ctx);
  541. if (!err)
  542. return 0;
  543. #endif
  544. return err;
  545. }
  546. #if CONFIG_LIBFRIBIDI
  547. static int shape_text(AVFilterContext *ctx)
  548. {
  549. DrawTextContext *s = ctx->priv;
  550. uint8_t *tmp;
  551. int ret = AVERROR(ENOMEM);
  552. static const FriBidiFlags flags = FRIBIDI_FLAGS_DEFAULT |
  553. FRIBIDI_FLAGS_ARABIC;
  554. FriBidiChar *unicodestr = NULL;
  555. FriBidiStrIndex len;
  556. FriBidiParType direction = FRIBIDI_PAR_LTR;
  557. FriBidiStrIndex line_start = 0;
  558. FriBidiStrIndex line_end = 0;
  559. FriBidiLevel *embedding_levels = NULL;
  560. FriBidiArabicProp *ar_props = NULL;
  561. FriBidiCharType *bidi_types = NULL;
  562. FriBidiStrIndex i,j;
  563. len = strlen(s->text);
  564. if (!(unicodestr = av_malloc_array(len, sizeof(*unicodestr)))) {
  565. goto out;
  566. }
  567. len = fribidi_charset_to_unicode(FRIBIDI_CHAR_SET_UTF8,
  568. s->text, len, unicodestr);
  569. bidi_types = av_malloc_array(len, sizeof(*bidi_types));
  570. if (!bidi_types) {
  571. goto out;
  572. }
  573. fribidi_get_bidi_types(unicodestr, len, bidi_types);
  574. embedding_levels = av_malloc_array(len, sizeof(*embedding_levels));
  575. if (!embedding_levels) {
  576. goto out;
  577. }
  578. if (!fribidi_get_par_embedding_levels(bidi_types, len, &direction,
  579. embedding_levels)) {
  580. goto out;
  581. }
  582. ar_props = av_malloc_array(len, sizeof(*ar_props));
  583. if (!ar_props) {
  584. goto out;
  585. }
  586. fribidi_get_joining_types(unicodestr, len, ar_props);
  587. fribidi_join_arabic(bidi_types, len, embedding_levels, ar_props);
  588. fribidi_shape(flags, embedding_levels, len, ar_props, unicodestr);
  589. for (line_end = 0, line_start = 0; line_end < len; line_end++) {
  590. if (ff_is_newline(unicodestr[line_end]) || line_end == len - 1) {
  591. if (!fribidi_reorder_line(flags, bidi_types,
  592. line_end - line_start + 1, line_start,
  593. direction, embedding_levels, unicodestr,
  594. NULL)) {
  595. goto out;
  596. }
  597. line_start = line_end + 1;
  598. }
  599. }
  600. /* Remove zero-width fill chars put in by libfribidi */
  601. for (i = 0, j = 0; i < len; i++)
  602. if (unicodestr[i] != FRIBIDI_CHAR_FILL)
  603. unicodestr[j++] = unicodestr[i];
  604. len = j;
  605. if (!(tmp = av_realloc(s->text, (len * 4 + 1) * sizeof(*s->text)))) {
  606. /* Use len * 4, as a unicode character can be up to 4 bytes in UTF-8 */
  607. goto out;
  608. }
  609. s->text = tmp;
  610. len = fribidi_unicode_to_charset(FRIBIDI_CHAR_SET_UTF8,
  611. unicodestr, len, s->text);
  612. ret = 0;
  613. out:
  614. av_free(unicodestr);
  615. av_free(embedding_levels);
  616. av_free(ar_props);
  617. av_free(bidi_types);
  618. return ret;
  619. }
  620. #endif
  621. static enum AVFrameSideDataType text_source_string_parse(const char *text_source_string)
  622. {
  623. av_assert0(text_source_string);
  624. if (!strcmp(text_source_string, "side_data_detection_bboxes")) {
  625. return AV_FRAME_DATA_DETECTION_BBOXES;
  626. } else {
  627. return AVERROR(EINVAL);
  628. }
  629. }
  630. static inline int get_subpixel_idx(int shift_x64, int shift_y64)
  631. {
  632. int idx = (shift_x64 >> 2) + (shift_y64 >> 4);
  633. return idx;
  634. }
  635. // Loads and (optionally) renders a glyph
  636. static int load_glyph(AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code, int8_t shift_x64, int8_t shift_y64)
  637. {
  638. DrawTextContext *s = ctx->priv;
  639. Glyph dummy = { 0 };
  640. Glyph *glyph;
  641. FT_Vector shift;
  642. struct AVTreeNode *node = NULL;
  643. int ret = 0;
  644. /* get glyph */
  645. dummy.code = code;
  646. dummy.fontsize = s->fontsize;
  647. glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
  648. if (!glyph) {
  649. if (FT_Load_Glyph(s->face, code, s->ft_load_flags)) {
  650. return AVERROR(EINVAL);
  651. }
  652. glyph = av_mallocz(sizeof(*glyph));
  653. if (!glyph) {
  654. ret = AVERROR(ENOMEM);
  655. goto error;
  656. }
  657. glyph->code = code;
  658. glyph->fontsize = s->fontsize;
  659. if (FT_Get_Glyph(s->face->glyph, &glyph->glyph)) {
  660. ret = AVERROR(EINVAL);
  661. goto error;
  662. }
  663. if (s->borderw) {
  664. glyph->border_glyph = glyph->glyph;
  665. if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0)) {
  666. ret = AVERROR_EXTERNAL;
  667. goto error;
  668. }
  669. }
  670. /* measure text height to calculate text_height (or the maximum text height) */
  671. FT_Glyph_Get_CBox(glyph->glyph, FT_GLYPH_BBOX_SUBPIXELS, &glyph->bbox);
  672. /* cache the newly created glyph */
  673. if (!(node = av_tree_node_alloc())) {
  674. ret = AVERROR(ENOMEM);
  675. goto error;
  676. }
  677. av_tree_insert(&s->glyphs, glyph, glyph_cmp, &node);
  678. } else {
  679. if (s->borderw && !glyph->border_glyph) {
  680. glyph->border_glyph = glyph->glyph;
  681. if (FT_Glyph_StrokeBorder(&glyph->border_glyph, s->stroker, 0, 0)) {
  682. ret = AVERROR_EXTERNAL;
  683. goto error;
  684. }
  685. }
  686. }
  687. // Check if a bitmap is needed
  688. if (shift_x64 >= 0 && shift_y64 >= 0) {
  689. // Get the bitmap subpixel index (0 -> 15)
  690. int idx = get_subpixel_idx(shift_x64, shift_y64);
  691. shift.x = shift_x64;
  692. shift.y = shift_y64;
  693. if (!glyph->bglyph[idx]) {
  694. FT_Glyph tmp_glyph = glyph->glyph;
  695. if (FT_Glyph_To_Bitmap(&tmp_glyph, FT_RENDER_MODE_NORMAL, &shift, 0)) {
  696. ret = AVERROR_EXTERNAL;
  697. goto error;
  698. }
  699. glyph->bglyph[idx] = (FT_BitmapGlyph)tmp_glyph;
  700. if (glyph->bglyph[idx]->bitmap.pixel_mode == FT_PIXEL_MODE_MONO) {
  701. av_log(ctx, AV_LOG_ERROR, "Monocromatic (1bpp) fonts are not supported.\n");
  702. ret = AVERROR(EINVAL);
  703. goto error;
  704. }
  705. }
  706. if (s->borderw && !glyph->border_bglyph[idx]) {
  707. FT_Glyph tmp_glyph = glyph->border_glyph;
  708. if (FT_Glyph_To_Bitmap(&tmp_glyph, FT_RENDER_MODE_NORMAL, &shift, 0)) {
  709. ret = AVERROR_EXTERNAL;
  710. goto error;
  711. }
  712. glyph->border_bglyph[idx] = (FT_BitmapGlyph)tmp_glyph;
  713. }
  714. }
  715. if (glyph_ptr) {
  716. *glyph_ptr = glyph;
  717. }
  718. return 0;
  719. error:
  720. if (glyph && glyph->glyph)
  721. FT_Done_Glyph(glyph->glyph);
  722. av_freep(&glyph);
  723. av_freep(&node);
  724. return ret;
  725. }
  726. // Convert a string formatted as "n1|n2|...|nN" into an integer array
  727. static int string_to_array(const char *source, int *result, int result_size)
  728. {
  729. int counter = 0, size = strlen(source) + 1;
  730. char *saveptr, *curval, *dup = av_malloc(size);
  731. if (!dup)
  732. return 0;
  733. av_strlcpy(dup, source, size);
  734. if (result_size > 0 && (curval = av_strtok(dup, "|", &saveptr))) {
  735. do {
  736. result[counter++] = atoi(curval);
  737. } while ((curval = av_strtok(NULL, "|", &saveptr)) && counter < result_size);
  738. }
  739. av_free(dup);
  740. return counter;
  741. }
  742. static int func_pict_type(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  743. {
  744. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  745. av_bprintf(bp, "%c", av_get_picture_type_char(s->var_values[VAR_PICT_TYPE]));
  746. return 0;
  747. }
  748. static int func_pts(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  749. {
  750. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  751. const char *fmt;
  752. const char *strftime_fmt = NULL;
  753. const char *delta = NULL;
  754. double pts = s->var_values[VAR_T];
  755. // argv: pts, FMT, [DELTA, 24HH | strftime_fmt]
  756. fmt = argc >= 1 ? argv[0] : "flt";
  757. if (argc >= 2) {
  758. delta = argv[1];
  759. }
  760. if (argc >= 3) {
  761. if (!strcmp(fmt, "hms")) {
  762. if (!strcmp(argv[2], "24HH")) {
  763. av_log(ctx, AV_LOG_WARNING, "pts third argument 24HH is deprecated, use pts:hms24hh instead\n");
  764. fmt = "hms24";
  765. } else {
  766. av_log(ctx, AV_LOG_ERROR, "Invalid argument '%s', '24HH' was expected\n", argv[2]);
  767. return AVERROR(EINVAL);
  768. }
  769. } else {
  770. strftime_fmt = argv[2];
  771. }
  772. }
  773. return ff_print_pts(ctx, bp, pts, delta, fmt, strftime_fmt);
  774. }
  775. static int func_frame_num(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  776. {
  777. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  778. av_bprintf(bp, "%d", (int)s->var_values[VAR_N]);
  779. return 0;
  780. }
  781. static int func_metadata(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  782. {
  783. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  784. AVDictionaryEntry *e = av_dict_get(s->metadata, argv[0], NULL, 0);
  785. if (e && e->value)
  786. av_bprintf(bp, "%s", e->value);
  787. else if (argc >= 2)
  788. av_bprintf(bp, "%s", argv[1]);
  789. return 0;
  790. }
  791. static int func_strftime(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  792. {
  793. const char *strftime_fmt = argc ? argv[0] : NULL;
  794. return ff_print_time(ctx, bp, strftime_fmt, !strcmp(function_name, "localtime"));
  795. }
  796. static int func_eval_expr(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  797. {
  798. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  799. return ff_print_eval_expr(ctx, bp, argv[0],
  800. fun2_names, fun2,
  801. var_names, s->var_values, &s->prng);
  802. }
  803. static int func_eval_expr_int_format(void *ctx, AVBPrint *bp, const char *function_name, unsigned argc, char **argv)
  804. {
  805. DrawTextContext *s = ((AVFilterContext *)ctx)->priv;
  806. int ret;
  807. int positions = -1;
  808. /*
  809. * argv[0] expression to be converted to `int`
  810. * argv[1] format: 'x', 'X', 'd' or 'u'
  811. * argv[2] positions printed (optional)
  812. */
  813. if (argc == 3) {
  814. ret = sscanf(argv[2], "%u", &positions);
  815. if (ret != 1) {
  816. av_log(ctx, AV_LOG_ERROR, "expr_int_format(): Invalid number of positions"
  817. " to print: '%s'\n", argv[2]);
  818. return AVERROR(EINVAL);
  819. }
  820. }
  821. return ff_print_formatted_eval_expr(ctx, bp, argv[0],
  822. fun2_names, fun2,
  823. var_names, s->var_values,
  824. &s->prng,
  825. argv[1][0], positions);
  826. }
  827. static const FFExpandTextFunction expand_text_functions[] = {
  828. { "e", 1, 1, func_eval_expr },
  829. { "eif", 2, 3, func_eval_expr_int_format },
  830. { "expr", 1, 1, func_eval_expr },
  831. { "expr_int_format", 2, 3, func_eval_expr_int_format },
  832. { "frame_num", 0, 0, func_frame_num },
  833. { "gmtime", 0, 1, func_strftime },
  834. { "localtime", 0, 1, func_strftime },
  835. { "metadata", 1, 2, func_metadata },
  836. { "n", 0, 0, func_frame_num },
  837. { "pict_type", 0, 0, func_pict_type },
  838. { "pts", 0, 3, func_pts }
  839. };
  840. static av_cold int init(AVFilterContext *ctx)
  841. {
  842. int err;
  843. DrawTextContext *s = ctx->priv;
  844. av_expr_free(s->fontsize_pexpr);
  845. s->fontsize_pexpr = NULL;
  846. s->fontsize = 0;
  847. s->default_fontsize = 16;
  848. if (!s->fontfile && !CONFIG_LIBFONTCONFIG) {
  849. av_log(ctx, AV_LOG_ERROR, "No font filename provided\n");
  850. return AVERROR(EINVAL);
  851. }
  852. if (s->textfile) {
  853. if (s->text) {
  854. av_log(ctx, AV_LOG_ERROR,
  855. "Both text and text file provided. Please provide only one\n");
  856. return AVERROR(EINVAL);
  857. }
  858. if ((err = ff_load_textfile(ctx, (const char *)s->textfile, &s->text, NULL)) < 0)
  859. return err;
  860. }
  861. if (s->reload && !s->textfile)
  862. av_log(ctx, AV_LOG_WARNING, "No file to reload\n");
  863. if (s->tc_opt_string) {
  864. int ret = av_timecode_init_from_string(&s->tc, s->tc_rate,
  865. s->tc_opt_string, ctx);
  866. if (ret < 0)
  867. return ret;
  868. if (s->tc24hmax)
  869. s->tc.flags |= AV_TIMECODE_FLAG_24HOURSMAX;
  870. if (!s->text)
  871. s->text = av_strdup("");
  872. }
  873. if (s->text_source_string) {
  874. s->text_source = text_source_string_parse(s->text_source_string);
  875. if ((int)s->text_source < 0) {
  876. av_log(ctx, AV_LOG_ERROR, "Error text source: %s\n", s->text_source_string);
  877. return AVERROR(EINVAL);
  878. }
  879. }
  880. if (s->text_source == AV_FRAME_DATA_DETECTION_BBOXES) {
  881. if (s->text) {
  882. av_log(ctx, AV_LOG_WARNING, "Multiple texts provided, will use text_source only\n");
  883. av_free(s->text);
  884. }
  885. s->text = av_mallocz((AV_DETECTION_BBOX_LABEL_NAME_MAX_SIZE + 1) *
  886. (AV_NUM_DETECTION_BBOX_CLASSIFY + 1));
  887. if (!s->text)
  888. return AVERROR(ENOMEM);
  889. }
  890. if (!s->text) {
  891. av_log(ctx, AV_LOG_ERROR,
  892. "Either text, a valid file, a timecode or text source must be provided\n");
  893. return AVERROR(EINVAL);
  894. }
  895. s->expand_text = (FFExpandTextContext) {
  896. .log_ctx = ctx,
  897. .functions = expand_text_functions,
  898. .functions_nb = FF_ARRAY_ELEMS(expand_text_functions)
  899. };
  900. #if CONFIG_LIBFRIBIDI
  901. if (s->text_shaping)
  902. if ((err = shape_text(ctx)) < 0)
  903. return err;
  904. #endif
  905. if ((err = FT_Init_FreeType(&(s->library)))) {
  906. av_log(ctx, AV_LOG_ERROR,
  907. "Could not load FreeType: %s\n", FT_ERRMSG(err));
  908. return AVERROR(EINVAL);
  909. }
  910. if ((err = load_font(ctx)) < 0)
  911. return err;
  912. if ((err = update_fontsize(ctx)) < 0)
  913. return err;
  914. // Always init the stroker, may be needed if borderw is set via command
  915. if (FT_Stroker_New(s->library, &s->stroker)) {
  916. av_log(ctx, AV_LOG_ERROR, "Could not init FT stroker\n");
  917. return AVERROR_EXTERNAL;
  918. }
  919. if (s->borderw) {
  920. FT_Stroker_Set(s->stroker, s->borderw << 6, FT_STROKER_LINECAP_ROUND,
  921. FT_STROKER_LINEJOIN_ROUND, 0);
  922. }
  923. /* load the fallback glyph with code 0 */
  924. load_glyph(ctx, NULL, 0, 0, 0);
  925. if (s->exp_mode == EXP_STRFTIME &&
  926. (strchr(s->text, '%') || strchr(s->text, '\\')))
  927. av_log(ctx, AV_LOG_WARNING, "expansion=strftime is deprecated.\n");
  928. av_bprint_init(&s->expanded_text, 0, AV_BPRINT_SIZE_UNLIMITED);
  929. av_bprint_init(&s->expanded_fontcolor, 0, AV_BPRINT_SIZE_UNLIMITED);
  930. return 0;
  931. }
  932. static int query_formats(const AVFilterContext *ctx,
  933. AVFilterFormatsConfig **cfg_in,
  934. AVFilterFormatsConfig **cfg_out)
  935. {
  936. return ff_set_common_formats2(ctx, cfg_in, cfg_out,
  937. ff_draw_supported_pixel_formats(0));
  938. }
  939. static int glyph_enu_border_free(void *opaque, void *elem)
  940. {
  941. Glyph *glyph = elem;
  942. if (glyph->border_glyph != NULL) {
  943. for (int t = 0; t < 16; ++t) {
  944. if (glyph->border_bglyph[t] != NULL) {
  945. FT_Done_Glyph((FT_Glyph)glyph->border_bglyph[t]);
  946. glyph->border_bglyph[t] = NULL;
  947. }
  948. }
  949. FT_Done_Glyph(glyph->border_glyph);
  950. glyph->border_glyph = NULL;
  951. }
  952. return 0;
  953. }
  954. static int glyph_enu_free(void *opaque, void *elem)
  955. {
  956. Glyph *glyph = elem;
  957. FT_Done_Glyph(glyph->glyph);
  958. FT_Done_Glyph(glyph->border_glyph);
  959. for (int t = 0; t < 16; ++t) {
  960. if (glyph->bglyph[t] != NULL) {
  961. FT_Done_Glyph((FT_Glyph)glyph->bglyph[t]);
  962. }
  963. if (glyph->border_bglyph[t] != NULL) {
  964. FT_Done_Glyph((FT_Glyph)glyph->border_bglyph[t]);
  965. }
  966. }
  967. av_free(elem);
  968. return 0;
  969. }
  970. static av_cold void uninit(AVFilterContext *ctx)
  971. {
  972. DrawTextContext *s = ctx->priv;
  973. av_expr_free(s->x_pexpr);
  974. av_expr_free(s->y_pexpr);
  975. av_expr_free(s->a_pexpr);
  976. av_expr_free(s->fontsize_pexpr);
  977. s->x_pexpr = s->y_pexpr = s->a_pexpr = s->fontsize_pexpr = NULL;
  978. av_tree_enumerate(s->glyphs, NULL, NULL, glyph_enu_free);
  979. av_tree_destroy(s->glyphs);
  980. s->glyphs = NULL;
  981. FT_Done_Face(s->face);
  982. FT_Stroker_Done(s->stroker);
  983. FT_Done_FreeType(s->library);
  984. av_bprint_finalize(&s->expanded_text, NULL);
  985. av_bprint_finalize(&s->expanded_fontcolor, NULL);
  986. }
  987. static int config_input(AVFilterLink *inlink)
  988. {
  989. AVFilterContext *ctx = inlink->dst;
  990. DrawTextContext *s = ctx->priv;
  991. char *expr;
  992. int ret;
  993. ret = ff_draw_init_from_link(&s->dc, inlink, FF_DRAW_PROCESS_ALPHA);
  994. if (ret < 0) {
  995. av_log(ctx, AV_LOG_ERROR, "Failed to initialize FFDrawContext\n");
  996. return ret;
  997. }
  998. ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
  999. ff_draw_color(&s->dc, &s->shadowcolor, s->shadowcolor.rgba);
  1000. ff_draw_color(&s->dc, &s->bordercolor, s->bordercolor.rgba);
  1001. ff_draw_color(&s->dc, &s->boxcolor, s->boxcolor.rgba);
  1002. s->var_values[VAR_w] = s->var_values[VAR_W] = s->var_values[VAR_MAIN_W] = inlink->w;
  1003. s->var_values[VAR_h] = s->var_values[VAR_H] = s->var_values[VAR_MAIN_H] = inlink->h;
  1004. s->var_values[VAR_SAR] = inlink->sample_aspect_ratio.num ? av_q2d(inlink->sample_aspect_ratio) : 1;
  1005. s->var_values[VAR_DAR] = (double)inlink->w / inlink->h * s->var_values[VAR_SAR];
  1006. s->var_values[VAR_HSUB] = 1 << s->dc.hsub_max;
  1007. s->var_values[VAR_VSUB] = 1 << s->dc.vsub_max;
  1008. s->var_values[VAR_X] = NAN;
  1009. s->var_values[VAR_Y] = NAN;
  1010. s->var_values[VAR_T] = NAN;
  1011. av_lfg_init(&s->prng, av_get_random_seed());
  1012. av_expr_free(s->x_pexpr);
  1013. av_expr_free(s->y_pexpr);
  1014. av_expr_free(s->a_pexpr);
  1015. s->x_pexpr = s->y_pexpr = s->a_pexpr = NULL;
  1016. if ((ret = av_expr_parse(&s->x_pexpr, expr = s->x_expr, var_names,
  1017. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
  1018. (ret = av_expr_parse(&s->y_pexpr, expr = s->y_expr, var_names,
  1019. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0 ||
  1020. (ret = av_expr_parse(&s->a_pexpr, expr = s->a_expr, var_names,
  1021. NULL, NULL, fun2_names, fun2, 0, ctx)) < 0) {
  1022. av_log(ctx, AV_LOG_ERROR, "Failed to parse expression: %s \n", expr);
  1023. return AVERROR(EINVAL);
  1024. }
  1025. return 0;
  1026. }
  1027. static int command(AVFilterContext *ctx, const char *cmd, const char *arg, char *res, int res_len, int flags)
  1028. {
  1029. DrawTextContext *old = ctx->priv;
  1030. DrawTextContext *new = NULL;
  1031. int ret;
  1032. if (!strcmp(cmd, "reinit")) {
  1033. new = av_mallocz(sizeof(DrawTextContext));
  1034. if (!new)
  1035. return AVERROR(ENOMEM);
  1036. new->class = &drawtext_class;
  1037. ret = av_opt_copy(new, old);
  1038. if (ret < 0)
  1039. goto fail;
  1040. ctx->priv = new;
  1041. ret = av_set_options_string(ctx, arg, "=", ":");
  1042. if (ret < 0) {
  1043. ctx->priv = old;
  1044. goto fail;
  1045. }
  1046. ret = init(ctx);
  1047. if (ret < 0) {
  1048. uninit(ctx);
  1049. ctx->priv = old;
  1050. goto fail;
  1051. }
  1052. new->reinit = 1;
  1053. ctx->priv = old;
  1054. uninit(ctx);
  1055. av_opt_free(old);
  1056. av_freep(&old);
  1057. ctx->priv = new;
  1058. return config_input(ctx->inputs[0]);
  1059. } else {
  1060. int old_borderw = old->borderw;
  1061. if ((ret = ff_filter_process_command(ctx, cmd, arg, res, res_len, flags)) < 0) {
  1062. return ret;
  1063. }
  1064. if (old->borderw != old_borderw) {
  1065. FT_Stroker_Set(old->stroker, old->borderw << 6, FT_STROKER_LINECAP_ROUND,
  1066. FT_STROKER_LINEJOIN_ROUND, 0);
  1067. // Dispose the old border glyphs
  1068. av_tree_enumerate(old->glyphs, NULL, NULL, glyph_enu_border_free);
  1069. } else if (strcmp(cmd, "fontsize") == 0) {
  1070. av_expr_free(old->fontsize_pexpr);
  1071. old->fontsize_pexpr = NULL;
  1072. old->blank_advance64 = 0;
  1073. }
  1074. return config_input(ctx->inputs[0]);
  1075. }
  1076. fail:
  1077. av_log(ctx, AV_LOG_ERROR, "Failed to process command. Continuing with existing parameters.\n");
  1078. av_freep(&new);
  1079. return ret;
  1080. }
  1081. static void update_color_with_alpha(DrawTextContext *s, FFDrawColor *color, const FFDrawColor incolor)
  1082. {
  1083. *color = incolor;
  1084. color->rgba[3] = (color->rgba[3] * s->alpha) / 255;
  1085. ff_draw_color(&s->dc, color, color->rgba);
  1086. }
  1087. static void update_alpha(DrawTextContext *s)
  1088. {
  1089. double alpha = av_expr_eval(s->a_pexpr, s->var_values, &s->prng);
  1090. if (isnan(alpha))
  1091. return;
  1092. if (alpha >= 1.0)
  1093. s->alpha = 255;
  1094. else if (alpha <= 0)
  1095. s->alpha = 0;
  1096. else
  1097. s->alpha = 256 * alpha;
  1098. }
  1099. static int draw_glyphs(AVFilterContext *ctx, AVFrame *frame,
  1100. FFDrawColor *color,
  1101. TextMetrics *metrics,
  1102. int x, int y, int borderw)
  1103. {
  1104. DrawTextContext *s = ctx->priv;
  1105. int g, l, x1, y1, w1, h1, idx;
  1106. int dx = 0, dy = 0, pdx = 0;
  1107. GlyphInfo *info;
  1108. Glyph dummy = { 0 }, *glyph;
  1109. FT_Bitmap bitmap;
  1110. FT_BitmapGlyph b_glyph;
  1111. uint8_t j_left = 0, j_right = 0, j_top = 0, j_bottom = 0;
  1112. int line_w, offset_y = 0;
  1113. int clip_x = 0, clip_y = 0;
  1114. j_left = !!(s->text_align & TA_LEFT);
  1115. j_right = !!(s->text_align & TA_RIGHT);
  1116. j_top = !!(s->text_align & TA_TOP);
  1117. j_bottom = !!(s->text_align & TA_BOTTOM);
  1118. if (j_top && j_bottom) {
  1119. offset_y = (s->box_height - metrics->height) / 2;
  1120. } else if (j_bottom) {
  1121. offset_y = s->box_height - metrics->height;
  1122. }
  1123. if ((!j_left || j_right) && !s->tab_warning_printed && s->tab_count > 0) {
  1124. s->tab_warning_printed = 1;
  1125. av_log(ctx, AV_LOG_WARNING, "Tab characters are only supported with left horizontal alignment\n");
  1126. }
  1127. clip_x = FFMIN(metrics->rect_x + s->box_width + s->bb_right, frame->width);
  1128. clip_y = FFMIN(metrics->rect_y + s->box_height + s->bb_bottom, frame->height);
  1129. for (l = 0; l < s->line_count; ++l) {
  1130. TextLine *line = &s->lines[l];
  1131. line_w = POS_CEIL(line->width64, 64);
  1132. for (g = 0; g < line->hb_data.glyph_count; ++g) {
  1133. info = &line->glyphs[g];
  1134. dummy.fontsize = s->fontsize;
  1135. dummy.code = info->code;
  1136. glyph = av_tree_find(s->glyphs, &dummy, glyph_cmp, NULL);
  1137. if (!glyph) {
  1138. return AVERROR(EINVAL);
  1139. }
  1140. idx = get_subpixel_idx(info->shift_x64, info->shift_y64);
  1141. b_glyph = borderw ? glyph->border_bglyph[idx] : glyph->bglyph[idx];
  1142. bitmap = b_glyph->bitmap;
  1143. x1 = x + info->x + b_glyph->left;
  1144. y1 = y + info->y - b_glyph->top + offset_y;
  1145. w1 = bitmap.width;
  1146. h1 = bitmap.rows;
  1147. if (j_left && j_right) {
  1148. x1 += (s->box_width - line_w) / 2;
  1149. } else if (j_right) {
  1150. x1 += s->box_width - line_w;
  1151. }
  1152. // Offset of the glyph's bitmap in the visible region
  1153. dx = dy = 0;
  1154. if (x1 < metrics->rect_x - s->bb_left) {
  1155. dx = metrics->rect_x - s->bb_left - x1;
  1156. x1 = metrics->rect_x - s->bb_left;
  1157. }
  1158. if (y1 < metrics->rect_y - s->bb_top) {
  1159. dy = metrics->rect_y - s->bb_top - y1;
  1160. y1 = metrics->rect_y - s->bb_top;
  1161. }
  1162. // check if the glyph is empty or out of the clipping region
  1163. if (dx >= w1 || dy >= h1 || x1 >= clip_x || y1 >= clip_y) {
  1164. continue;
  1165. }
  1166. pdx = dx + dy * bitmap.pitch;
  1167. w1 = FFMIN(clip_x - x1, w1 - dx);
  1168. h1 = FFMIN(clip_y - y1, h1 - dy);
  1169. ff_blend_mask(&s->dc, color, frame->data, frame->linesize, clip_x, clip_y,
  1170. bitmap.buffer + pdx, bitmap.pitch, w1, h1, 3, 0, x1, y1);
  1171. }
  1172. }
  1173. return 0;
  1174. }
  1175. // Shapes a line of text using libharfbuzz
  1176. static int shape_text_hb(DrawTextContext *s, HarfbuzzData* hb, const char* text, int textLen)
  1177. {
  1178. hb->buf = hb_buffer_create();
  1179. if(!hb_buffer_allocation_successful(hb->buf)) {
  1180. return AVERROR(ENOMEM);
  1181. }
  1182. hb_buffer_set_direction(hb->buf, HB_DIRECTION_LTR);
  1183. hb_buffer_set_script(hb->buf, HB_SCRIPT_LATIN);
  1184. hb_buffer_set_language(hb->buf, hb_language_from_string("en", -1));
  1185. hb_buffer_guess_segment_properties(hb->buf);
  1186. hb->font = hb_ft_font_create_referenced(s->face);
  1187. if(hb->font == NULL) {
  1188. return AVERROR(ENOMEM);
  1189. }
  1190. hb_buffer_add_utf8(hb->buf, text, textLen, 0, -1);
  1191. hb_shape(hb->font, hb->buf, NULL, 0);
  1192. hb->glyph_info = hb_buffer_get_glyph_infos(hb->buf, &hb->glyph_count);
  1193. hb->glyph_pos = hb_buffer_get_glyph_positions(hb->buf, &hb->glyph_count);
  1194. return 0;
  1195. }
  1196. static void hb_destroy(HarfbuzzData *hb)
  1197. {
  1198. hb_font_destroy(hb->font);
  1199. hb_buffer_destroy(hb->buf);
  1200. hb->buf = NULL;
  1201. hb->font = NULL;
  1202. hb->glyph_info = NULL;
  1203. hb->glyph_pos = NULL;
  1204. }
  1205. static int measure_text(AVFilterContext *ctx, TextMetrics *metrics)
  1206. {
  1207. DrawTextContext *s = ctx->priv;
  1208. char *text = s->expanded_text.str;
  1209. char *textdup = NULL;
  1210. int width64 = 0, w64 = 0;
  1211. int cur_min_y64 = 0, first_max_y64 = -32000;
  1212. int first_min_x64 = 32000, last_max_x64 = -32000;
  1213. int min_y64 = 32000, max_y64 = -32000, min_x64 = 32000, max_x64 = -32000;
  1214. int line_count = 0;
  1215. uint32_t code = 0;
  1216. Glyph *glyph = NULL;
  1217. int i, tab_idx = 0, last_tab_idx = 0, line_offset = 0;
  1218. uint8_t *start, *p;
  1219. int ret = 0;
  1220. // Count the lines and the tab characters
  1221. s->tab_count = 0;
  1222. for (i = 0, p = text; 1; i++) {
  1223. GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_failed;);
  1224. continue_on_failed:
  1225. if (ff_is_newline(code) || code == 0) {
  1226. ++line_count;
  1227. if (code == 0) {
  1228. break;
  1229. }
  1230. } else if (code == '\t') {
  1231. ++s->tab_count;
  1232. }
  1233. }
  1234. // Evaluate the width of the space character if needed to replace tabs
  1235. if (s->tab_count > 0 && !s->blank_advance64) {
  1236. HarfbuzzData hb_data;
  1237. ret = shape_text_hb(s, &hb_data, " ", 1);
  1238. if(ret != 0) {
  1239. goto done;
  1240. }
  1241. s->blank_advance64 = hb_data.glyph_pos[0].x_advance;
  1242. hb_destroy(&hb_data);
  1243. }
  1244. s->line_count = line_count;
  1245. s->lines = av_mallocz(line_count * sizeof(TextLine));
  1246. s->tab_clusters = av_mallocz(s->tab_count * sizeof(uint32_t));
  1247. for (i = 0; i < s->tab_count; ++i) {
  1248. s->tab_clusters[i] = -1;
  1249. }
  1250. start = textdup = av_strdup(text);
  1251. if (textdup == NULL) {
  1252. ret = AVERROR(ENOMEM);
  1253. goto done;
  1254. }
  1255. line_count = 0;
  1256. for (i = 0, p = textdup; 1; i++) {
  1257. if (*p == '\t') {
  1258. s->tab_clusters[tab_idx++] = i;
  1259. *p = ' ';
  1260. }
  1261. GET_UTF8(code, *p ? *p++ : 0, code = 0xfffd; goto continue_on_failed2;);
  1262. continue_on_failed2:
  1263. if (ff_is_newline(code) || code == 0) {
  1264. TextLine *cur_line = &s->lines[line_count];
  1265. HarfbuzzData *hb = &cur_line->hb_data;
  1266. cur_line->cluster_offset = line_offset;
  1267. ret = shape_text_hb(s, hb, start, p - start);
  1268. if (ret != 0) {
  1269. goto done;
  1270. }
  1271. w64 = 0;
  1272. cur_min_y64 = 32000;
  1273. for (int t = 0; t < hb->glyph_count; ++t) {
  1274. uint8_t is_tab = last_tab_idx < s->tab_count &&
  1275. hb->glyph_info[t].cluster == s->tab_clusters[last_tab_idx] - line_offset;
  1276. if (is_tab) {
  1277. ++last_tab_idx;
  1278. }
  1279. ret = load_glyph(ctx, &glyph, hb->glyph_info[t].codepoint, -1, -1);
  1280. if (ret != 0) {
  1281. goto done;
  1282. }
  1283. if (line_count == 0) {
  1284. first_max_y64 = FFMAX(glyph->bbox.yMax, first_max_y64);
  1285. }
  1286. if (t == 0) {
  1287. cur_line->offset_left64 = glyph->bbox.xMin;
  1288. first_min_x64 = FFMIN(glyph->bbox.xMin, first_min_x64);
  1289. }
  1290. if (t == hb->glyph_count - 1) {
  1291. // The following code measures the width of the line up to the last
  1292. // character's horizontal advance
  1293. int last_char_width = hb->glyph_pos[t].x_advance;
  1294. // The following code measures the width of the line up to the rightmost
  1295. // visible pixel of the last character
  1296. // int last_char_width = glyph->bbox.xMax;
  1297. w64 += last_char_width;
  1298. last_max_x64 = FFMAX(last_char_width, last_max_x64);
  1299. cur_line->offset_right64 = last_char_width;
  1300. } else {
  1301. if (is_tab) {
  1302. int size = s->blank_advance64 * s->tabsize;
  1303. w64 = (w64 / size + 1) * size;
  1304. } else {
  1305. w64 += hb->glyph_pos[t].x_advance;
  1306. }
  1307. }
  1308. cur_min_y64 = FFMIN(glyph->bbox.yMin, cur_min_y64);
  1309. min_y64 = FFMIN(glyph->bbox.yMin, min_y64);
  1310. max_y64 = FFMAX(glyph->bbox.yMax, max_y64);
  1311. min_x64 = FFMIN(glyph->bbox.xMin, min_x64);
  1312. max_x64 = FFMAX(glyph->bbox.xMax, max_x64);
  1313. }
  1314. cur_line->width64 = w64;
  1315. av_log(ctx, AV_LOG_DEBUG, " Line: %d -- glyphs count: %d - width64: %d - offset_left64: %d - offset_right64: %d)\n",
  1316. line_count, hb->glyph_count, cur_line->width64, cur_line->offset_left64, cur_line->offset_right64);
  1317. if (w64 > width64) {
  1318. width64 = w64;
  1319. }
  1320. start = p;
  1321. ++line_count;
  1322. line_offset = i + 1;
  1323. }
  1324. if (code == 0) break;
  1325. }
  1326. metrics->line_height64 = s->face->size->metrics.height;
  1327. metrics->width = POS_CEIL(width64, 64);
  1328. if (s->y_align == YA_FONT) {
  1329. metrics->height = POS_CEIL(metrics->line_height64 * line_count, 64);
  1330. } else {
  1331. int height64 = (metrics->line_height64 + s->line_spacing * 64) *
  1332. (FFMAX(0, line_count - 1)) + first_max_y64 - cur_min_y64;
  1333. metrics->height = POS_CEIL(height64, 64);
  1334. }
  1335. metrics->offset_top64 = first_max_y64;
  1336. metrics->offset_right64 = last_max_x64;
  1337. metrics->offset_bottom64 = cur_min_y64;
  1338. metrics->offset_left64 = first_min_x64;
  1339. metrics->min_x64 = min_x64;
  1340. metrics->min_y64 = min_y64;
  1341. metrics->max_x64 = max_x64;
  1342. metrics->max_y64 = max_y64;
  1343. done:
  1344. av_free(textdup);
  1345. return ret;
  1346. }
  1347. static int draw_text(AVFilterContext *ctx, AVFrame *frame)
  1348. {
  1349. DrawTextContext *s = ctx->priv;
  1350. AVFilterLink *inlink = ctx->inputs[0];
  1351. FilterLink *inl = ff_filter_link(inlink);
  1352. int x = 0, y = 0, ret;
  1353. int shift_x64, shift_y64;
  1354. int x64, y64;
  1355. Glyph *glyph = NULL;
  1356. time_t now = time(0);
  1357. struct tm ltime;
  1358. AVBPrint *bp = &s->expanded_text;
  1359. FFDrawColor fontcolor;
  1360. FFDrawColor shadowcolor;
  1361. FFDrawColor bordercolor;
  1362. FFDrawColor boxcolor;
  1363. int width = frame->width;
  1364. int height = frame->height;
  1365. int rec_x = 0, rec_y = 0, rec_width = 0, rec_height = 0;
  1366. int is_outside = 0;
  1367. int last_tab_idx = 0;
  1368. TextMetrics metrics;
  1369. av_bprint_clear(bp);
  1370. if (s->basetime != AV_NOPTS_VALUE)
  1371. now= frame->pts*av_q2d(ctx->inputs[0]->time_base) + s->basetime/1000000;
  1372. switch (s->exp_mode) {
  1373. case EXP_NONE:
  1374. av_bprintf(bp, "%s", s->text);
  1375. break;
  1376. case EXP_NORMAL:
  1377. if ((ret = ff_expand_text(&s->expand_text, s->text, &s->expanded_text)) < 0)
  1378. return ret;
  1379. break;
  1380. case EXP_STRFTIME:
  1381. localtime_r(&now, &ltime);
  1382. av_bprint_strftime(bp, s->text, &ltime);
  1383. break;
  1384. }
  1385. if (s->tc_opt_string) {
  1386. char tcbuf[AV_TIMECODE_STR_SIZE];
  1387. av_timecode_make_string(&s->tc, tcbuf, inl->frame_count_out);
  1388. av_bprint_clear(bp);
  1389. av_bprintf(bp, "%s%s", s->text, tcbuf);
  1390. }
  1391. if (!av_bprint_is_complete(bp))
  1392. return AVERROR(ENOMEM);
  1393. if (s->fontcolor_expr[0]) {
  1394. /* If expression is set, evaluate and replace the static value */
  1395. av_bprint_clear(&s->expanded_fontcolor);
  1396. if ((ret = ff_expand_text(&s->expand_text, s->fontcolor_expr, &s->expanded_fontcolor)) < 0)
  1397. return ret;
  1398. if (!av_bprint_is_complete(&s->expanded_fontcolor))
  1399. return AVERROR(ENOMEM);
  1400. av_log(ctx, AV_LOG_DEBUG, "Evaluated fontcolor is '%s'\n", s->expanded_fontcolor.str);
  1401. ret = av_parse_color(s->fontcolor.rgba, s->expanded_fontcolor.str, -1, s);
  1402. if (ret)
  1403. return ret;
  1404. ff_draw_color(&s->dc, &s->fontcolor, s->fontcolor.rgba);
  1405. }
  1406. if ((ret = update_fontsize(ctx)) < 0) {
  1407. return ret;
  1408. }
  1409. if ((ret = measure_text(ctx, &metrics)) < 0) {
  1410. return ret;
  1411. }
  1412. s->max_glyph_h = POS_CEIL(metrics.max_y64 - metrics.min_y64, 64);
  1413. s->max_glyph_w = POS_CEIL(metrics.max_x64 - metrics.min_x64, 64);
  1414. s->var_values[VAR_TW] = s->var_values[VAR_TEXT_W] = metrics.width;
  1415. s->var_values[VAR_TH] = s->var_values[VAR_TEXT_H] = metrics.height;
  1416. s->var_values[VAR_MAX_GLYPH_W] = s->max_glyph_w;
  1417. s->var_values[VAR_MAX_GLYPH_H] = s->max_glyph_h;
  1418. s->var_values[VAR_MAX_GLYPH_A] = s->var_values[VAR_ASCENT] = POS_CEIL(metrics.max_y64, 64);
  1419. s->var_values[VAR_FONT_A] = s->face->size->metrics.ascender / 64;
  1420. s->var_values[VAR_MAX_GLYPH_D] = s->var_values[VAR_DESCENT] = POS_CEIL(metrics.min_y64, 64);
  1421. s->var_values[VAR_FONT_D] = -s->face->size->metrics.descender / 64;
  1422. s->var_values[VAR_TOP_A] = POS_CEIL(metrics.offset_top64, 64);
  1423. s->var_values[VAR_BOTTOM_D] = -POS_CEIL(metrics.offset_bottom64, 64);
  1424. s->var_values[VAR_LINE_H] = s->var_values[VAR_LH] = metrics.line_height64 / 64.;
  1425. if (s->text_source == AV_FRAME_DATA_DETECTION_BBOXES) {
  1426. s->var_values[VAR_X] = s->x;
  1427. s->var_values[VAR_Y] = s->y;
  1428. } else {
  1429. s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
  1430. s->y = s->var_values[VAR_Y] = av_expr_eval(s->y_pexpr, s->var_values, &s->prng);
  1431. /* It is necessary if x is expressed from y */
  1432. s->x = s->var_values[VAR_X] = av_expr_eval(s->x_pexpr, s->var_values, &s->prng);
  1433. }
  1434. update_alpha(s);
  1435. update_color_with_alpha(s, &fontcolor , s->fontcolor );
  1436. update_color_with_alpha(s, &shadowcolor, s->shadowcolor);
  1437. update_color_with_alpha(s, &bordercolor, s->bordercolor);
  1438. update_color_with_alpha(s, &boxcolor , s->boxcolor );
  1439. if (s->draw_box && s->boxborderw) {
  1440. int bbsize[4];
  1441. int count;
  1442. count = string_to_array(s->boxborderw, bbsize, 4);
  1443. if (count == 1) {
  1444. s->bb_top = s->bb_right = s->bb_bottom = s->bb_left = bbsize[0];
  1445. } else if (count == 2) {
  1446. s->bb_top = s->bb_bottom = bbsize[0];
  1447. s->bb_right = s->bb_left = bbsize[1];
  1448. } else if (count == 3) {
  1449. s->bb_top = bbsize[0];
  1450. s->bb_right = s->bb_left = bbsize[1];
  1451. s->bb_bottom = bbsize[2];
  1452. } else if (count == 4) {
  1453. s->bb_top = bbsize[0];
  1454. s->bb_right = bbsize[1];
  1455. s->bb_bottom = bbsize[2];
  1456. s->bb_left = bbsize[3];
  1457. }
  1458. } else {
  1459. s->bb_top = s->bb_right = s->bb_bottom = s->bb_left = 0;
  1460. }
  1461. if (s->fix_bounds) {
  1462. /* calculate footprint of text effects */
  1463. int borderoffset = s->borderw ? FFMAX(s->borderw, 0) : 0;
  1464. int offsetleft = FFMAX3(FFMAX(s->bb_left, 0), borderoffset,
  1465. (s->shadowx < 0 ? FFABS(s->shadowx) : 0));
  1466. int offsettop = FFMAX3(FFMAX(s->bb_top, 0), borderoffset,
  1467. (s->shadowy < 0 ? FFABS(s->shadowy) : 0));
  1468. int offsetright = FFMAX3(FFMAX(s->bb_right, 0), borderoffset,
  1469. (s->shadowx > 0 ? s->shadowx : 0));
  1470. int offsetbottom = FFMAX3(FFMAX(s->bb_bottom, 0), borderoffset,
  1471. (s->shadowy > 0 ? s->shadowy : 0));
  1472. if (s->x - offsetleft < 0) s->x = offsetleft;
  1473. if (s->y - offsettop < 0) s->y = offsettop;
  1474. if (s->x + metrics.width + offsetright > width)
  1475. s->x = FFMAX(width - metrics.width - offsetright, 0);
  1476. if (s->y + metrics.height + offsetbottom > height)
  1477. s->y = FFMAX(height - metrics.height - offsetbottom, 0);
  1478. }
  1479. x = 0;
  1480. y = 0;
  1481. x64 = (int)(s->x * 64.);
  1482. if (s->y_align == YA_FONT) {
  1483. y64 = (int)(s->y * 64. + s->face->size->metrics.ascender);
  1484. } else if (s->y_align == YA_BASELINE) {
  1485. y64 = (int)(s->y * 64.);
  1486. } else {
  1487. y64 = (int)(s->y * 64. + metrics.offset_top64);
  1488. }
  1489. for (int l = 0; l < s->line_count; ++l) {
  1490. TextLine *line = &s->lines[l];
  1491. HarfbuzzData *hb = &line->hb_data;
  1492. line->glyphs = av_mallocz(hb->glyph_count * sizeof(GlyphInfo));
  1493. for (int t = 0; t < hb->glyph_count; ++t) {
  1494. GlyphInfo *g_info = &line->glyphs[t];
  1495. uint8_t is_tab = last_tab_idx < s->tab_count &&
  1496. hb->glyph_info[t].cluster == s->tab_clusters[last_tab_idx] - line->cluster_offset;
  1497. int true_x, true_y;
  1498. if (is_tab) {
  1499. ++last_tab_idx;
  1500. }
  1501. true_x = x + hb->glyph_pos[t].x_offset;
  1502. true_y = y + hb->glyph_pos[t].y_offset;
  1503. shift_x64 = (((x64 + true_x) >> 4) & 0b0011) << 4;
  1504. shift_y64 = ((4 - (((y64 + true_y) >> 4) & 0b0011)) & 0b0011) << 4;
  1505. ret = load_glyph(ctx, &glyph, hb->glyph_info[t].codepoint, shift_x64, shift_y64);
  1506. if (ret != 0) {
  1507. return ret;
  1508. }
  1509. g_info->code = hb->glyph_info[t].codepoint;
  1510. g_info->x = (x64 + true_x) >> 6;
  1511. g_info->y = ((y64 + true_y) >> 6) + (shift_y64 > 0 ? 1 : 0);
  1512. g_info->shift_x64 = shift_x64;
  1513. g_info->shift_y64 = shift_y64;
  1514. if (!is_tab) {
  1515. x += hb->glyph_pos[t].x_advance;
  1516. } else {
  1517. int size = s->blank_advance64 * s->tabsize;
  1518. x = (x / size + 1) * size;
  1519. }
  1520. y += hb->glyph_pos[t].y_advance;
  1521. }
  1522. y += metrics.line_height64 + s->line_spacing * 64;
  1523. x = 0;
  1524. }
  1525. metrics.rect_x = s->x;
  1526. if (s->y_align == YA_BASELINE) {
  1527. metrics.rect_y = s->y - metrics.offset_top64 / 64;
  1528. } else {
  1529. metrics.rect_y = s->y;
  1530. }
  1531. s->box_width = s->boxw == 0 ? metrics.width : s->boxw;
  1532. s->box_height = s->boxh == 0 ? metrics.height : s->boxh;
  1533. if (!s->draw_box) {
  1534. // Create a border for the clipping region to take into account subpixel
  1535. // errors in text measurement and effects.
  1536. int borderoffset = s->borderw ? FFMAX(s->borderw, 0) : 0;
  1537. s->bb_left = borderoffset + (s->shadowx < 0 ? FFABS(s->shadowx) : 0) + 1;
  1538. s->bb_top = borderoffset + (s->shadowy < 0 ? FFABS(s->shadowy) : 0) + 1;
  1539. s->bb_right = borderoffset + (s->shadowx > 0 ? s->shadowx : 0) + 1;
  1540. s->bb_bottom = borderoffset + (s->shadowy > 0 ? s->shadowy : 0) + 1;
  1541. }
  1542. /* Check if the whole box is out of the frame */
  1543. is_outside = metrics.rect_x - s->bb_left >= width ||
  1544. metrics.rect_y - s->bb_top >= height ||
  1545. metrics.rect_x + s->box_width + s->bb_right <= 0 ||
  1546. metrics.rect_y + s->box_height + s->bb_bottom <= 0;
  1547. if (!is_outside) {
  1548. /* draw box */
  1549. if (s->draw_box) {
  1550. rec_x = metrics.rect_x - s->bb_left;
  1551. rec_y = metrics.rect_y - s->bb_top;
  1552. rec_width = s->box_width + s->bb_right + s->bb_left;
  1553. rec_height = s->box_height + s->bb_bottom + s->bb_top;
  1554. ff_blend_rectangle(&s->dc, &boxcolor,
  1555. frame->data, frame->linesize, width, height,
  1556. rec_x, rec_y, rec_width, rec_height);
  1557. }
  1558. if (s->shadowx || s->shadowy) {
  1559. if ((ret = draw_glyphs(ctx, frame, &shadowcolor, &metrics,
  1560. s->shadowx, s->shadowy, s->borderw)) < 0) {
  1561. return ret;
  1562. }
  1563. }
  1564. if (s->borderw) {
  1565. if ((ret = draw_glyphs(ctx, frame, &bordercolor, &metrics,
  1566. 0, 0, s->borderw)) < 0) {
  1567. return ret;
  1568. }
  1569. }
  1570. if ((ret = draw_glyphs(ctx, frame, &fontcolor, &metrics, 0,
  1571. 0, 0)) < 0) {
  1572. return ret;
  1573. }
  1574. }
  1575. // FREE data structures
  1576. for (int l = 0; l < s->line_count; ++l) {
  1577. TextLine *line = &s->lines[l];
  1578. av_freep(&line->glyphs);
  1579. hb_destroy(&line->hb_data);
  1580. }
  1581. av_freep(&s->lines);
  1582. av_freep(&s->tab_clusters);
  1583. return 0;
  1584. }
  1585. static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
  1586. {
  1587. FilterLink *inl = ff_filter_link(inlink);
  1588. AVFilterContext *ctx = inlink->dst;
  1589. AVFilterLink *outlink = ctx->outputs[0];
  1590. DrawTextContext *s = ctx->priv;
  1591. int ret;
  1592. const AVDetectionBBoxHeader *header = NULL;
  1593. const AVDetectionBBox *bbox;
  1594. AVFrameSideData *sd;
  1595. int loop = 1;
  1596. if (s->text_source == AV_FRAME_DATA_DETECTION_BBOXES) {
  1597. sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
  1598. if (sd) {
  1599. header = (AVDetectionBBoxHeader *)sd->data;
  1600. loop = header->nb_bboxes;
  1601. } else {
  1602. av_log(ctx, AV_LOG_WARNING, "No detection bboxes.\n");
  1603. return ff_filter_frame(outlink, frame);
  1604. }
  1605. }
  1606. if (s->reload && !(inl->frame_count_out % s->reload)) {
  1607. if ((ret = ff_load_textfile(ctx, (const char *)s->textfile, &s->text, NULL)) < 0) {
  1608. av_frame_free(&frame);
  1609. return ret;
  1610. }
  1611. #if CONFIG_LIBFRIBIDI
  1612. if (s->text_shaping)
  1613. if ((ret = shape_text(ctx)) < 0) {
  1614. av_frame_free(&frame);
  1615. return ret;
  1616. }
  1617. #endif
  1618. }
  1619. s->var_values[VAR_N] = inl->frame_count_out + s->start_number;
  1620. s->var_values[VAR_T] = frame->pts == AV_NOPTS_VALUE ?
  1621. NAN : frame->pts * av_q2d(inlink->time_base);
  1622. s->var_values[VAR_PICT_TYPE] = frame->pict_type;
  1623. s->var_values[VAR_DURATION] = frame->duration * av_q2d(inlink->time_base);
  1624. s->metadata = frame->metadata;
  1625. for (int i = 0; i < loop; i++) {
  1626. if (header) {
  1627. bbox = av_get_detection_bbox(header, i);
  1628. strcpy(s->text, bbox->detect_label);
  1629. for (int j = 0; j < bbox->classify_count; j++) {
  1630. strcat(s->text, ", ");
  1631. strcat(s->text, bbox->classify_labels[j]);
  1632. }
  1633. s->x = bbox->x;
  1634. s->y = bbox->y - s->fontsize;
  1635. }
  1636. draw_text(ctx, frame);
  1637. }
  1638. return ff_filter_frame(outlink, frame);
  1639. }
  1640. static const AVFilterPad avfilter_vf_drawtext_inputs[] = {
  1641. {
  1642. .name = "default",
  1643. .type = AVMEDIA_TYPE_VIDEO,
  1644. .flags = AVFILTERPAD_FLAG_NEEDS_WRITABLE,
  1645. .filter_frame = filter_frame,
  1646. .config_props = config_input,
  1647. },
  1648. };
  1649. const FFFilter ff_vf_drawtext = {
  1650. .p.name = "drawtext",
  1651. .p.description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
  1652. .p.priv_class = &drawtext_class,
  1653. .p.flags = AVFILTER_FLAG_SUPPORT_TIMELINE_GENERIC,
  1654. .priv_size = sizeof(DrawTextContext),
  1655. .init = init,
  1656. .uninit = uninit,
  1657. FILTER_INPUTS(avfilter_vf_drawtext_inputs),
  1658. FILTER_OUTPUTS(ff_video_default_filterpad),
  1659. FILTER_QUERY_FUNC2(query_formats),
  1660. .process_command = command,
  1661. };