Libav
Data Structures | Macros | Typedefs | Enumerations | Functions | Variables
vf_drawtext.c File Reference

drawtext filter, based on the original vhook/drawtext.c filter by Gustavo Sverzut Barbieri More...

#include "config.h"
#include <sys/types.h>
#include <sys/time.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>
#include "libavutil/colorspace.h"
#include "libavutil/common.h"
#include "libavutil/file.h"
#include "libavutil/eval.h"
#include "libavutil/opt.h"
#include "libavutil/mathematics.h"
#include "libavutil/random_seed.h"
#include "libavutil/parseutils.h"
#include "libavutil/pixdesc.h"
#include "libavutil/tree.h"
#include "libavutil/lfg.h"
#include "avfilter.h"
#include "drawutils.h"
#include "formats.h"
#include "internal.h"
#include "video.h"
#include <ft2build.h>
#include <FT_FREETYPE_H>
#include <FT_GLYPH_H>
#include <FT_ERRORS_H>

Go to the source code of this file.

Data Structures

struct  DrawTextContext
 
struct  ft_error
 

Macros

#define OFFSET(x)   offsetof(DrawTextContext, x)
 
#define FLAGS   AV_OPT_FLAG_VIDEO_PARAM
 
#define FT_ERROR_START_LIST   {
 
#define FT_ERRORDEF(e, v, s)   { (e), (s) },
 
#define FT_ERROR_END_LIST   { 0, NULL } };
 
#define FT_ERRMSG(e)   ft_errors[e].err_msg
 
#define GET_BITMAP_VAL(r, c)
 
#define SET_PIXEL_YUV(frame, yuva_color, val, x, y, hsub, vsub)
 
#define SET_PIXEL_RGB(frame, rgba_color, val, x, y, pixel_step, r_off, g_off, b_off, a_off)
 

Typedefs

typedef double(* eval_func2) (void *, double a, double b)
 

Enumerations

enum  var_name {
  VAR_E, VAR_INTERLACED, VAR_N, VAR_PHI,
  VAR_PI, VAR_PREV_INPTS, VAR_PREV_OUTPTS, VAR_PTS,
  VAR_STARTPTS, VAR_TB, VAR_RTCTIME, VAR_RTCSTART,
  VAR_S, VAR_SR, VAR_VARS_NB, VAR_E,
  VAR_PHI, VAR_PI, VAR_AVTB, VAR_INTB,
  VAR_SR, VAR_VARS_NB, VAR_PI, VAR_PHI,
  VAR_E, VAR_W, VAR_H, VAR_A,
  VAR_DAR, VAR_SAR, VAR_HSUB, VAR_VSUB,
  VARS_NB, VAR_W, VAR_H, VAR_CW,
  VAR_CH, VAR_HSUB, VAR_VSUB, VARS_NB,
  VAR_E, VAR_PHI, VAR_PI, VAR_IN_W,
  VAR_IW, VAR_IN_H, VAR_IH, VAR_OUT_W,
  VAR_OW, VAR_OUT_H, VAR_OH, VAR_X,
  VAR_Y, VAR_N, VAR_T, VAR_VARS_NB,
  VAR_E, VAR_PHI, VAR_PI, VAR_MAIN_W,
  VAR_MW, VAR_MAIN_H, VAR_MH, VAR_TEXT_W,
  VAR_TW, VAR_TEXT_H, VAR_TH, VAR_X,
  VAR_Y, VAR_N, VAR_T, VAR_VARS_NB,
  VAR_E, VAR_PHI, VAR_PI, VAR_W,
  VAR_H, VAR_VAL, VAR_MAXVAL, VAR_MINVAL,
  VAR_NEGVAL, VAR_CLIPVAL, VAR_VARS_NB, VAR_E,
  VAR_PHI, VAR_PI, VAR_MAIN_W, VAR_MW,
  VAR_MAIN_H, VAR_MH, VAR_OVERLAY_W, VAR_OW,
  VAR_OVERLAY_H, VAR_OH, VAR_VARS_NB, VAR_PI,
  VAR_PHI, VAR_E, VAR_IN_W, VAR_IW,
  VAR_IN_H, VAR_IH, VAR_OUT_W, VAR_OW,
  VAR_OUT_H, VAR_OH, VAR_X, VAR_Y,
  VAR_A, VAR_HSUB, VAR_VSUB, VARS_NB,
  VAR_PI, VAR_PHI, VAR_E, VAR_IN_W,
  VAR_IW, VAR_IN_H, VAR_IH, VAR_OUT_W,
  VAR_OW, VAR_OUT_H, VAR_OH, VAR_A,
  VAR_DAR, VAR_SAR, VAR_HSUB, VAR_VSUB,
  VARS_NB, VAR_E, VAR_PHI, VAR_PI,
  VAR_TB, VAR_PTS, VAR_START_PTS, VAR_PREV_PTS,
  VAR_PREV_SELECTED_PTS, VAR_T, VAR_START_T, VAR_PREV_T,
  VAR_PREV_SELECTED_T, VAR_PICT_TYPE, VAR_PICT_TYPE_I, VAR_PICT_TYPE_P,
  VAR_PICT_TYPE_B, VAR_PICT_TYPE_S, VAR_PICT_TYPE_SI, VAR_PICT_TYPE_SP,
  VAR_PICT_TYPE_BI, VAR_INTERLACE_TYPE, VAR_INTERLACE_TYPE_P, VAR_INTERLACE_TYPE_T,
  VAR_INTERLACE_TYPE_B, VAR_N, VAR_SELECTED_N, VAR_PREV_SELECTED_N,
  VAR_KEY, VAR_VARS_NB, VAR_E, VAR_PHI,
  VAR_PI, VAR_AVTB, VAR_VARS_NB
}
 

Functions

static double drand (void *opaque, double min, double max)
 
static const char * drawtext_get_name (void *ctx)
 
static int glyph_cmp (void *key, const void *b)
 
static int load_glyph (AVFilterContext *ctx, Glyph **glyph_ptr, uint32_t code)
 Load glyphs corresponding to the UTF-32 codepoint code. More...
 
static int parse_font (AVFilterContext *ctx)
 
static av_cold int init (AVFilterContext *ctx)
 
static int query_formats (AVFilterContext *ctx)
 
static int glyph_enu_free (void *opaque, void *elem)
 
static av_cold void uninit (AVFilterContext *ctx)
 
static int is_newline (uint32_t c)
 
static int dtext_prepare_text (AVFilterContext *ctx)
 
static int config_input (AVFilterLink *inlink)
 
static int draw_glyph_yuv (AVFrame *frame, 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)
 
static int draw_glyph_rgb (AVFrame *frame, 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])
 
static void drawbox (AVFrame *frame, 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])
 
static int draw_glyphs (DrawTextContext *s, AVFrame *frame, int width, int height, const uint8_t rgbcolor[4], const uint8_t yuvcolor[4], int x, int y)
 
static int draw_text (AVFilterContext *ctx, AVFrame *frame, int width, int height)
 
static int normalize_double (int *n, double d)
 
static int filter_frame (AVFilterLink *inlink, AVFrame *frame)
 

Variables

static const char *const var_names []
 
static const char *const fun2_names []
 
static const eval_func2 fun2 []
 
static const AVOption drawtext_options []
 
static const AVClass drawtext_class
 
struct ft_error ft_errors []
 
static const AVFilterPad avfilter_vf_drawtext_inputs []
 
static const AVFilterPad avfilter_vf_drawtext_outputs []
 
AVFilter ff_vf_drawtext
 

Detailed Description

drawtext filter, based on the original vhook/drawtext.c filter by Gustavo Sverzut Barbieri

Definition in file vf_drawtext.c.

Macro Definition Documentation

#define OFFSET (   x)    offsetof(DrawTextContext, x)

Definition at line 157 of file vf_drawtext.c.

#define FLAGS   AV_OPT_FLAG_VIDEO_PARAM

Definition at line 158 of file vf_drawtext.c.

#define FT_ERROR_START_LIST   {

Definition at line 213 of file vf_drawtext.c.

#define FT_ERRORDEF (   e,
  v,
 
)    { (e), (s) },

Definition at line 214 of file vf_drawtext.c.

#define FT_ERROR_END_LIST   { 0, NULL } };

Definition at line 215 of file vf_drawtext.c.

#define FT_ERRMSG (   e)    ft_errors[e].err_msg

Referenced by init().

#define GET_BITMAP_VAL (   r,
 
)
Value:
bitmap->pixel_mode == FT_PIXEL_MODE_MONO ? \
(bitmap->buffer[(r) * bitmap->pitch + ((c)>>3)] & (0x80 >> ((c)&7))) * 255 : \
bitmap->buffer[(r) * bitmap->pitch + (c)]
#define r
Definition: input.c:51

Definition at line 711 of file vf_drawtext.c.

Referenced by draw_glyph_rgb(), and draw_glyph_yuv().

#define SET_PIXEL_YUV (   frame,
  yuva_color,
  val,
  x,
  y,
  hsub,
  vsub 
)
Value:
{ \
luma_pos = ((x) ) + ((y) ) * frame->linesize[0]; \
alpha = yuva_color[3] * (val) * 129; \
frame->data[0][luma_pos] = (alpha * yuva_color[0] + (255*255*129 - alpha) * frame->data[0][luma_pos] ) >> 23; \
if (((x) & ((1<<(hsub)) - 1)) == 0 && ((y) & ((1<<(vsub)) - 1)) == 0) {\
chroma_pos1 = ((x) >> (hsub)) + ((y) >> (vsub)) * frame->linesize[1]; \
chroma_pos2 = ((x) >> (hsub)) + ((y) >> (vsub)) * frame->linesize[2]; \
frame->data[1][chroma_pos1] = (alpha * yuva_color[1] + (255*255*129 - alpha) * frame->data[1][chroma_pos1]) >> 23; \
frame->data[2][chroma_pos2] = (alpha * yuva_color[2] + (255*255*129 - alpha) * frame->data[2][chroma_pos2]) >> 23; \
}\
}
if(ac->has_optimized_func)

Definition at line 716 of file vf_drawtext.c.

Referenced by draw_glyph_yuv(), and drawbox().

#define SET_PIXEL_RGB (   frame,
  rgba_color,
  val,
  x,
  y,
  pixel_step,
  r_off,
  g_off,
  b_off,
  a_off 
)
Value:
{ \
p = frame->data[0] + (x) * pixel_step + ((y) * frame->linesize[0]); \
alpha = rgba_color[3] * (val) * 129; \
*(p+r_off) = (alpha * rgba_color[0] + (255*255*129 - alpha) * *(p+r_off)) >> 23; \
*(p+g_off) = (alpha * rgba_color[1] + (255*255*129 - alpha) * *(p+g_off)) >> 23; \
*(p+b_off) = (alpha * rgba_color[2] + (255*255*129 - alpha) * *(p+b_off)) >> 23; \
}

Definition at line 750 of file vf_drawtext.c.

Referenced by draw_glyph_rgb(), and drawbox().

Typedef Documentation

typedef double(* eval_func2) (void *, double a, double b)

Definition at line 86 of file vf_drawtext.c.

Enumeration Type Documentation

enum var_name
Enumerator
VAR_E 
VAR_INTERLACED 
VAR_N 
VAR_PHI 
VAR_PI 
VAR_PREV_INPTS 
VAR_PREV_OUTPTS 
VAR_PTS 
VAR_STARTPTS 
VAR_TB 
VAR_RTCTIME 
VAR_RTCSTART 
VAR_S 
VAR_SR 
VAR_VARS_NB 
VAR_E 
VAR_PHI 
VAR_PI 
VAR_AVTB 
VAR_INTB 
VAR_SR 
VAR_VARS_NB 
VAR_PI 
VAR_PHI 
VAR_E 
VAR_W 
VAR_H 
VAR_A 
VAR_DAR 
VAR_SAR 
VAR_HSUB 
VAR_VSUB 
VARS_NB 
VAR_W 
VAR_H 
VAR_CW 
VAR_CH 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E_B 
VAR_PICT_TYPE_S 
VAR_PICT_TYPE_SI 
VAR_PICT_TYPE_SP 
VAR_PICT_TYPE_BI 
VAR_INTERLACE_TYPE 
VAR_INTERLACE_TYPE_P 
VAR_INTERLACE_TYPE_T 
VAR_INTERLACE_TYPE_B 
VAR_N 
VAR_SELECTED_N 
VAR_PREV_SELECTED_N 
VAR_KEY 
VAR_VARS_NB 
VAR_E 
VAR_PHI 
VAR_PI 
VAR_AVTB 
VAR_VARS_NB 

Definition at line 93 of file vf_drawtext.c.

Function Documentation

static double drand ( void opaque,
double  min,
double  max 
)
static

Definition at line 81 of file vf_drawtext.c.

static const char* drawtext_get_name ( void ctx)
static

Definition at line 201 of file vf_drawtext.c.

static int glyph_cmp ( void key,
const void b 
)
static

Definition at line 236 of file vf_drawtext.c.

Referenced by draw_glyphs(), dtext_prepare_text(), and load_glyph().

static int load_glyph ( AVFilterContext ctx,
Glyph **  glyph_ptr,
uint32_t  code 
)
static

Load glyphs corresponding to the UTF-32 codepoint code.

Definition at line 246 of file vf_drawtext.c.

Referenced by dtext_prepare_text(), and init().

static int parse_font ( AVFilterContext ctx)
static

Definition at line 297 of file vf_drawtext.c.

Referenced by init().

static av_cold int init ( AVFilterContext ctx)
static

Definition at line 376 of file vf_drawtext.c.

static int query_formats ( AVFilterContext ctx)
static

Definition at line 472 of file vf_drawtext.c.

static int glyph_enu_free ( void opaque,
void elem 
)
static

Definition at line 488 of file vf_drawtext.c.

Referenced by uninit().

static av_cold void uninit ( AVFilterContext ctx)
static

Definition at line 494 of file vf_drawtext.c.

static int is_newline ( uint32_t  c)
inlinestatic

Definition at line 518 of file vf_drawtext.c.

Referenced by dtext_prepare_text().

static int dtext_prepare_text ( AVFilterContext ctx)
static

Definition at line 523 of file vf_drawtext.c.

Referenced by config_input(), and filter_frame().

static int config_input ( AVFilterLink inlink)
static

Definition at line 649 of file vf_drawtext.c.

static int draw_glyph_yuv ( AVFrame frame,
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 
)
inlinestatic

Definition at line 728 of file vf_drawtext.c.

Referenced by draw_glyphs().

static int draw_glyph_rgb ( AVFrame frame,
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] 
)
inlinestatic

Definition at line 758 of file vf_drawtext.c.

Referenced by draw_glyphs().

static void drawbox ( AVFrame frame,
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] 
)
inlinestatic

Definition at line 782 of file vf_drawtext.c.

Referenced by avfilter_register_all(), and draw_text().

static int draw_glyphs ( DrawTextContext s,
AVFrame frame,
int  width,
int  height,
const uint8_t  rgbcolor[4],
const uint8_t  yuvcolor[4],
int  x,
int  y 
)
static

Definition at line 809 of file vf_drawtext.c.

Referenced by draw_text().

static int draw_text ( AVFilterContext ctx,
AVFrame frame,
int  width,
int  height 
)
static

Definition at line 847 of file vf_drawtext.c.

Referenced by filter_frame().

static int normalize_double ( int *  n,
double  d 
)
inlinestatic

Definition at line 879 of file vf_drawtext.c.

Referenced by filter_frame().

static int filter_frame ( AVFilterLink inlink,
AVFrame frame 
)
static

Definition at line 894 of file vf_drawtext.c.

Variable Documentation

const char* const var_names[]
static
Initial value:
= {
"E",
"PHI",
"PI",
"main_w", "W",
"main_h", "H",
"text_w", "w",
"text_h", "h",
"x",
"y",
"n",
"t",
}
NULL
Definition: eval.c:55

Definition at line 62 of file vf_drawtext.c.

Referenced by config_input().

const char* const fun2_names[]
static
Initial value:
= {
"rand"
}

Definition at line 77 of file vf_drawtext.c.

Referenced by config_input().

const eval_func2 fun2[]
static
Initial value:
= {
}
static double drand(void *opaque, double min, double max)
Definition: vf_drawtext.c:81
NULL
Definition: eval.c:55

Definition at line 88 of file vf_drawtext.c.

Referenced by config_input().

const AVOption drawtext_options[]
static

Definition at line 160 of file vf_drawtext.c.

const AVClass drawtext_class
static
Initial value:
= {
"DrawTextContext",
}
static const AVOption drawtext_options[]
Definition: vf_drawtext.c:160
static const char * drawtext_get_name(void *ctx)
Definition: vf_drawtext.c:201

Definition at line 206 of file vf_drawtext.c.

struct ft_error ft_errors[]
Initial value:
=
#define FT_ERRMSG(e)
typedef struct Glyph {
FT_Glyph *glyph;
uint32_t code;
FT_Bitmap bitmap;
FT_BBox bbox;
int advance;
int bitmap_left;
int bitmap_top;
} Glyph
const AVFilterPad avfilter_vf_drawtext_inputs[]
static
Initial value:
= {
{
.name = "default",
.get_video_buffer = ff_null_get_video_buffer,
.filter_frame = filter_frame,
.config_props = config_input,
.needs_writable = 1,
},
{ NULL }
}
AVFrame * ff_null_get_video_buffer(AVFilterLink *link, int w, int h)
Definition: video.c:30
NULL
Definition: eval.c:55
static int config_input(AVFilterLink *inlink)
Definition: vf_drawtext.c:649
static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
Definition: vf_drawtext.c:894

Definition at line 944 of file vf_drawtext.c.

const AVFilterPad avfilter_vf_drawtext_outputs[]
static
Initial value:
= {
{
.name = "default",
},
{ NULL }
}
NULL
Definition: eval.c:55

Definition at line 956 of file vf_drawtext.c.

AVFilter ff_vf_drawtext
Initial value:
= {
.name = "drawtext",
.description = NULL_IF_CONFIG_SMALL("Draw text on top of video frames using libfreetype library."),
.priv_size = sizeof(DrawTextContext),
.priv_class = &drawtext_class,
.init = init,
}
static const AVFilterPad outputs[]
Definition: af_ashowinfo.c:232
static av_cold int init(AVFilterContext *ctx)
Definition: vf_drawtext.c:376
static const AVFilterPad avfilter_vf_drawtext_inputs[]
Definition: vf_drawtext.c:944
static av_cold void uninit(AVFilterContext *ctx)
Definition: vf_drawtext.c:494
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:150
static const AVFilterPad inputs[]
Definition: af_ashowinfo.c:221
static const AVClass drawtext_class
Definition: vf_drawtext.c:206
static int query_formats(AVFilterContext *ctx)
Definition: vf_drawtext.c:472
static const AVFilterPad avfilter_vf_drawtext_outputs[]
Definition: vf_drawtext.c:956

Definition at line 964 of file vf_drawtext.c.