static av_cold int color_init(AVFilterContext *ctx, const char *args) { ColorContext *color = ctx->priv; char color_string[128] = "black"; char frame_size [128] = "320x240"; char frame_rate [128] = "25"; AVRational frame_rate_q; int ret; if (args) sscanf(args, "%127[^:]:%127[^:]:%127s", color_string, frame_size, frame_rate); if (av_parse_video_size(&color->w, &color->h, frame_size) < 0) { av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", frame_size); return AVERROR(EINVAL); } if (av_parse_video_rate(&frame_rate_q, frame_rate) < 0 || frame_rate_q.den <= 0 || frame_rate_q.num <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", frame_rate); return AVERROR(EINVAL); } color->time_base.num = frame_rate_q.den; color->time_base.den = frame_rate_q.num; if ((ret = av_parse_color(color->color, color_string, -1, ctx)) < 0) return ret; return 0; }
static int html_color_parse(void *log_ctx, const char *str) { uint8_t rgba[4]; if (av_parse_color(rgba, str, strcspn(str, "\" >"), log_ctx) < 0) return -1; return rgba[0] | rgba[1] << 8 | rgba[2] << 16; }
static int html_color_parse(AVCodecContext *avctx, const char *str) { uint8_t rgba[4]; if (av_parse_color(rgba, str, strcspn(str, "\" >"), avctx) < 0) return -1; return rgba[0] | rgba[1] << 8 | rgba[2] << 16; }
static void test_av_parse_color(void) { int i; uint8_t rgba[4]; static const char *const color_names[] = { "bikeshed", "RaNdOm", "foo", "red", "Red ", "RED", "Violet", "Yellow", "Red", "0x000000", "0x0000000", "0xff000000", "0x3e34ff", "0x3e34ffaa", "0xffXXee", "0xfoobar", "0xffffeeeeeeee", "#ff0000", "#ffXX00", "ff0000", "ffXX00", "red@foo", "random@10", "[email protected]", "red@", "red@0xfff", "red@0xf", "red@2", "[email protected]", "red@-1", "[email protected]", "[email protected]", "red@256", "red@10foo", "[email protected]", "[email protected]", }; av_log_set_level(AV_LOG_DEBUG); for (i = 0; i < FF_ARRAY_ELEMS(color_names); i++) { if (av_parse_color(rgba, color_names[i], -1, NULL) >= 0) printf("%s -> R(%d) G(%d) B(%d) A(%d)\n", color_names[i], rgba[0], rgba[1], rgba[2], rgba[3]); else printf("%s -> error\n", color_names[i]); } }
static int set_param(AVFilterContext *ctx, f0r_param_info_t info, int index, char *param) { Frei0rContext *frei0r = ctx->priv; union { double d; f0r_param_color_t col; f0r_param_position_t pos; } val; char *tail; uint8_t rgba[4]; switch (info.type) { case F0R_PARAM_BOOL: if (!strcmp(param, "y")) val.d = 1.0; else if (!strcmp(param, "n")) val.d = 0.0; else goto fail; break; case F0R_PARAM_DOUBLE: val.d = strtod(param, &tail); if (*tail || val.d == HUGE_VAL) goto fail; break; case F0R_PARAM_COLOR: if (sscanf(param, "%f/%f/%f", &val.col.r, &val.col.g, &val.col.b) != 3) { if (av_parse_color(rgba, param, -1, ctx) < 0) goto fail; val.col.r = rgba[0] / 255.0; val.col.g = rgba[1] / 255.0; val.col.b = rgba[2] / 255.0; } break; case F0R_PARAM_POSITION: if (sscanf(param, "%lf/%lf", &val.pos.x, &val.pos.y) != 2) goto fail; break; } frei0r->set_param_value(frei0r->instance, &val, index); return 0; fail: av_log(ctx, AV_LOG_ERROR, "Invalid value '%s' for parameter '%s'\n", param, info.name); return AVERROR(EINVAL); }
static av_cold int color_init(AVFilterContext *ctx) { ColorContext *color = ctx->priv; AVRational frame_rate_q; int ret; if (av_parse_video_size(&color->w, &color->h, color->size_str) < 0) { av_log(ctx, AV_LOG_ERROR, "Invalid frame size: %s\n", color->size_str); return AVERROR(EINVAL); } if (av_parse_video_rate(&frame_rate_q, color->framerate_str) < 0 || frame_rate_q.den <= 0 || frame_rate_q.num <= 0) { av_log(ctx, AV_LOG_ERROR, "Invalid frame rate: %s\n", color->framerate_str); return AVERROR(EINVAL); } color->time_base.num = frame_rate_q.den; color->time_base.den = frame_rate_q.num; if ((ret = av_parse_color(color->color, color->color_str, -1, ctx)) < 0) return ret; return 0; }
static av_cold int init(AVFilterContext *ctx) { int err; DrawTextContext *s = ctx->priv; Glyph *glyph; if ((err = parse_font(ctx)) < 0) return err; if (s->textfile) { uint8_t *textbuf; size_t textbuf_size; if (s->text) { av_log(ctx, AV_LOG_ERROR, "Both text and text file provided. Please provide only one\n"); return AVERROR(EINVAL); } if ((err = av_file_map(s->textfile, &textbuf, &textbuf_size, 0, ctx)) < 0) { av_log(ctx, AV_LOG_ERROR, "The text file '%s' could not be read or is empty\n", s->textfile); return err; } if (textbuf_size > SIZE_MAX - 1 || !(s->text = av_malloc(textbuf_size + 1))) { av_file_unmap(textbuf, textbuf_size); return AVERROR(ENOMEM); } memcpy(s->text, textbuf, textbuf_size); s->text[textbuf_size] = 0; av_file_unmap(textbuf, textbuf_size); } if (!s->text) { av_log(ctx, AV_LOG_ERROR, "Either text or a valid file must be provided\n"); return AVERROR(EINVAL); } if ((err = av_parse_color(s->fontcolor_rgba, s->fontcolor_string, -1, ctx))) { av_log(ctx, AV_LOG_ERROR, "Invalid font color '%s'\n", s->fontcolor_string); return err; } if ((err = av_parse_color(s->boxcolor_rgba, s->boxcolor_string, -1, ctx))) { av_log(ctx, AV_LOG_ERROR, "Invalid box color '%s'\n", s->boxcolor_string); return err; } if ((err = av_parse_color(s->shadowcolor_rgba, s->shadowcolor_string, -1, ctx))) { av_log(ctx, AV_LOG_ERROR, "Invalid shadow color '%s'\n", s->shadowcolor_string); return err; } if ((err = FT_Init_FreeType(&(s->library)))) { av_log(ctx, AV_LOG_ERROR, "Could not load FreeType: %s\n", FT_ERRMSG(err)); return AVERROR(EINVAL); } /* load the face, and set up the encoding, which is by default UTF-8 */ if ((err = FT_New_Face(s->library, s->fontfile, 0, &s->face))) { av_log(ctx, AV_LOG_ERROR, "Could not load fontface from file '%s': %s\n", s->fontfile, FT_ERRMSG(err)); return AVERROR(EINVAL); } if ((err = FT_Set_Pixel_Sizes(s->face, 0, s->fontsize))) { av_log(ctx, AV_LOG_ERROR, "Could not set font size to %d pixels: %s\n", s->fontsize, FT_ERRMSG(err)); return AVERROR(EINVAL); } s->use_kerning = FT_HAS_KERNING(s->face); /* load the fallback glyph with code 0 */ load_glyph(ctx, NULL, 0); /* set the tabsize in pixels */ if ((err = load_glyph(ctx, &glyph, ' ') < 0)) { av_log(ctx, AV_LOG_ERROR, "Could not set tabsize.\n"); return err; } s->tabsize *= glyph->advance; #if !HAVE_LOCALTIME_R av_log(ctx, AV_LOG_WARNING, "strftime() expansion unavailable!\n"); #endif return 0; }
static int plot_freqs(AVFilterLink *inlink, AVFrame *in) { AVFilterContext *ctx = inlink->dst; AVFilterLink *outlink = ctx->outputs[0]; ShowFreqsContext *s = ctx->priv; const int win_size = s->win_size; char *colors, *color, *saveptr = NULL; AVFrame *out; int ch, n; out = ff_get_video_buffer(outlink, outlink->w, outlink->h); if (!out) return AVERROR(ENOMEM); for (n = 0; n < outlink->h; n++) memset(out->data[0] + out->linesize[0] * n, 0, outlink->w * 4); /* fill FFT input with the number of samples available */ for (ch = 0; ch < s->nb_channels; ch++) { const float *p = (float *)in->extended_data[ch]; for (n = 0; n < in->nb_samples; n++) { s->fft_data[ch][n].re = p[n] * s->window_func_lut[n]; s->fft_data[ch][n].im = 0; } for (; n < win_size; n++) { s->fft_data[ch][n].re = 0; s->fft_data[ch][n].im = 0; } } /* run FFT on each samples set */ for (ch = 0; ch < s->nb_channels; ch++) { av_fft_permute(s->fft, s->fft_data[ch]); av_fft_calc(s->fft, s->fft_data[ch]); } #define RE(x, ch) s->fft_data[ch][x].re #define IM(x, ch) s->fft_data[ch][x].im #define M(a, b) (sqrt((a) * (a) + (b) * (b))) colors = av_strdup(s->colors); if (!colors) { av_frame_free(&out); return AVERROR(ENOMEM); } for (ch = 0; ch < s->nb_channels; ch++) { uint8_t fg[4] = { 0xff, 0xff, 0xff, 0xff }; int prev_y = -1, f; double a; color = av_strtok(ch == 0 ? colors : NULL, " |", &saveptr); if (color) av_parse_color(fg, color, -1, ctx); a = av_clipd(M(RE(0, ch), 0) / s->scale, 0, 1); plot_freq(s, ch, a, 0, fg, &prev_y, out, outlink); for (f = 1; f < s->nb_freq; f++) { a = av_clipd(M(RE(f, ch), IM(f, ch)) / s->scale, 0, 1); plot_freq(s, ch, a, f, fg, &prev_y, out, outlink); } } av_free(colors); out->pts = in->pts; return ff_filter_frame(outlink, out); }