int main(void) { AVBPrint b; char buf[256]; struct tm testtime = { .tm_year = 100, .tm_mon = 11, .tm_mday = 20 }; av_bprint_init(&b, 0, -1); bprint_pascal(&b, 5); printf("Short text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); printf("%s\n", b.str); av_bprint_finalize(&b, NULL); av_bprint_init(&b, 0, -1); bprint_pascal(&b, 25); printf("Long text in unlimited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); av_bprint_finalize(&b, NULL); av_bprint_init(&b, 0, 2048); bprint_pascal(&b, 25); printf("Long text in limited buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); av_bprint_finalize(&b, NULL); av_bprint_init(&b, 0, 1); bprint_pascal(&b, 5); printf("Short text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); av_bprint_init(&b, 0, 1); bprint_pascal(&b, 25); printf("Long text in automatic buffer: %u/%u\n", (unsigned)strlen(b.str)/8*8, b.len); /* Note that the size of the automatic buffer is arch-dependent. */ av_bprint_init(&b, 0, 0); bprint_pascal(&b, 25); printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(b.str), b.len); av_bprint_init_for_buffer(&b, buf, sizeof(buf)); bprint_pascal(&b, 25); printf("Long text count only buffer: %u/%u\n", (unsigned)strlen(buf), b.len); av_bprint_init(&b, 0, -1); av_bprint_strftime(&b, "%Y-%m-%d", &testtime); printf("strftime full: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str); av_bprint_finalize(&b, NULL); av_bprint_init(&b, 0, 8); av_bprint_strftime(&b, "%Y-%m-%d", &testtime); printf("strftime truncated: %u/%u \"%s\"\n", (unsigned)strlen(buf), b.len, b.str); return 0; }
static int func_pts(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag) { DrawTextContext *s = ctx->priv; const char *fmt; double pts = s->var_values[VAR_T]; int ret; fmt = argc >= 1 ? argv[0] : "flt"; if (argc >= 2) { int64_t delta; if ((ret = av_parse_time(&delta, argv[1], 1)) < 0) { av_log(ctx, AV_LOG_ERROR, "Invalid delta '%s'\n", argv[1]); return ret; } pts += (double)delta / AV_TIME_BASE; } if (!strcmp(fmt, "flt")) { av_bprintf(bp, "%.6f", pts); } else if (!strcmp(fmt, "hms")) { if (isnan(pts)) { av_bprintf(bp, " ??:??:??.???"); } else { int64_t ms = llrint(pts * 1000); char sign = ' '; if (ms < 0) { sign = '-'; ms = -ms; } av_bprintf(bp, "%c%02d:%02d:%02d.%03d", sign, (int)(ms / (60 * 60 * 1000)), (int)(ms / (60 * 1000)) % 60, (int)(ms / 1000) % 60, (int)(ms % 1000)); } } else if (!strcmp(fmt, "localtime") || !strcmp(fmt, "gmtime")) { struct tm tm; time_t ms = (time_t)pts; const char *timefmt = argc >= 3 ? argv[2] : "%Y-%m-%d %H:%M:%S"; if (!strcmp(fmt, "localtime")) localtime_r(&ms, &tm); else gmtime_r(&ms, &tm); av_bprint_strftime(bp, timefmt, &tm); } else { av_log(ctx, AV_LOG_ERROR, "Invalid format '%s'\n", fmt); return AVERROR(EINVAL); } return 0; }
static int func_strftime(AVFilterContext *ctx, AVBPrint *bp, char *fct, unsigned argc, char **argv, int tag) { const char *fmt = argc ? argv[0] : "%Y-%m-%d %H:%M:%S"; time_t now; struct tm tm; time(&now); if (tag == 'L') localtime_r(&now, &tm); else tm = *gmtime_r(&now, &tm); av_bprint_strftime(bp, fmt, &tm); return 0; }