Skip to content

Commit

Permalink
tools_common.h: add VPX_TOOLS_FORMAT_PRINTF
Browse files Browse the repository at this point in the history
and use it to set the format attribute for printf like functions. this
allows the examples to be built with -Wformat-nonliteral without
producing warnings.

Bug: webm:1744
Change-Id: I26b4c41c9a42790053b1ae0e4a678af8f2cd1d82
Fixed: webm:1744
  • Loading branch information
jzern committed Nov 3, 2021
1 parent 340f605 commit dd10ac8
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 40 deletions.
1 change: 1 addition & 0 deletions configure
Original file line number Diff line number Diff line change
Expand Up @@ -621,6 +621,7 @@ process_toolchain() {
check_add_cflags -Wdeclaration-after-statement
check_add_cflags -Wdisabled-optimization
check_add_cflags -Wfloat-conversion
check_add_cflags -Wformat=2
check_add_cflags -Wparentheses-equality
check_add_cflags -Wpointer-arith
check_add_cflags -Wtype-limits
Expand Down
17 changes: 10 additions & 7 deletions examples/svc_encodeframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <stdlib.h>
#include <string.h>
#define VPX_DISABLE_CTRL_TYPECHECKS 1
#include "../tools_common.h"
#include "./vpx_config.h"
#include "./svc_context.h"
#include "vpx/vp8cx.h"
Expand Down Expand Up @@ -95,8 +96,9 @@ static const SvcInternal_t *get_const_svc_internal(const SvcContext *svc_ctx) {
return (const SvcInternal_t *)svc_ctx->internal;
}

static int svc_log(SvcContext *svc_ctx, SVC_LOG_LEVEL level, const char *fmt,
...) {
static VPX_TOOLS_FORMAT_PRINTF(3, 4) int svc_log(SvcContext *svc_ctx,
SVC_LOG_LEVEL level,
const char *fmt, ...) {
char buf[512];
int retval = 0;
va_list ap;
Expand Down Expand Up @@ -264,7 +266,7 @@ static vpx_codec_err_t parse_options(SvcContext *svc_ctx, const char *options) {
if (alt_ref_enabled > REF_FRAMES - svc_ctx->spatial_layers) {
svc_log(svc_ctx, SVC_LOG_ERROR,
"svc: auto alt ref: Maxinum %d(REF_FRAMES - layers) layers could"
"enabled auto alt reference frame, but % layers are enabled\n",
"enabled auto alt reference frame, but %d layers are enabled\n",
REF_FRAMES - svc_ctx->spatial_layers, alt_ref_enabled);
res = VPX_CODEC_INVALID_PARAM;
}
Expand Down Expand Up @@ -456,10 +458,11 @@ vpx_codec_err_t vpx_svc_init(SvcContext *svc_ctx, vpx_codec_ctx_t *codec_ctx,
svc_ctx->temporal_layers = VPX_TS_MAX_LAYERS;

if (svc_ctx->temporal_layers * svc_ctx->spatial_layers > VPX_MAX_LAYERS) {
svc_log(svc_ctx, SVC_LOG_ERROR,
"spatial layers * temporal layers exceeds the maximum number of "
"allowed layers of %d\n",
svc_ctx->spatial_layers * svc_ctx->temporal_layers, VPX_MAX_LAYERS);
svc_log(
svc_ctx, SVC_LOG_ERROR,
"spatial layers * temporal layers (%d) exceeds the maximum number of "
"allowed layers of %d\n",
svc_ctx->spatial_layers * svc_ctx->temporal_layers, VPX_MAX_LAYERS);
return VPX_CODEC_INVALID_PARAM;
}
res = assign_layer_bitrates(svc_ctx, enc_cfg);
Expand Down
2 changes: 1 addition & 1 deletion examples/twopass_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ int main(int argc, char **argv) {
die("Invalid frame size: %dx%d", w, h);

if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, w, h, 1))
die("Failed to allocate image", w, h);
die("Failed to allocate image (%dx%d)", w, h);

printf("Using %s\n", vpx_codec_iface_name(encoder->codec_interface()));

Expand Down
6 changes: 3 additions & 3 deletions examples/vp8_multi_resolution_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ int main(int argc, char **argv) {
framerate = (int)strtol(argv[3], NULL, 0);

if (width < 16 || width % 2 || height < 16 || height % 2)
die("Invalid resolution: %ldx%ld", width, height);
die("Invalid resolution: %dx%d", width, height);

/* Open input video file for encoding */
if (!(infile = fopen(argv[4], "rb")))
Expand Down Expand Up @@ -380,7 +380,7 @@ int main(int argc, char **argv) {
(int)strtol(argv[2 * NUM_ENCODERS + 5 + i], NULL, 0);
if (num_temporal_layers[i] < 1 || num_temporal_layers[i] > 3)
die("Invalid temporal layers: %d, Must be 1, 2, or 3. \n",
num_temporal_layers);
num_temporal_layers[i]);
}

/* Open file to write out each spatially downsampled input stream. */
Expand Down Expand Up @@ -468,7 +468,7 @@ int main(int argc, char **argv) {
/* Allocate image for each encoder */
for (i = 0; i < NUM_ENCODERS; i++)
if (!vpx_img_alloc(&raw[i], VPX_IMG_FMT_I420, cfg[i].g_w, cfg[i].g_h, 32))
die("Failed to allocate image", cfg[i].g_w, cfg[i].g_h);
die("Failed to allocate image (%dx%d)", cfg[i].g_w, cfg[i].g_h);

if (raw[0].stride[VPX_PLANE_Y] == (int)raw[0].d_w)
read_frame_p = mulres_read_frame;
Expand Down
4 changes: 2 additions & 2 deletions examples/vpx_temporal_svc_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -687,14 +687,14 @@ int main(int argc, char **argv) {
&raw,
bit_depth == VPX_BITS_8 ? VPX_IMG_FMT_I420 : VPX_IMG_FMT_I42016,
width, height, 32)) {
die("Failed to allocate image", width, height);
die("Failed to allocate image (%dx%d)", width, height);
}
}
#else
// Y4M reader has its own allocation.
if (input_ctx.file_type != FILE_TYPE_Y4M) {
if (!vpx_img_alloc(&raw, VPX_IMG_FMT_I420, width, height, 32)) {
die("Failed to allocate image", width, height);
die("Failed to allocate image (%dx%d)", width, height);
}
}
#endif // CONFIG_VP9_HIGHBITDEPTH
Expand Down
35 changes: 18 additions & 17 deletions rate_hist.c
Original file line number Diff line number Diff line change
Expand Up @@ -193,40 +193,40 @@ static int merge_hist_buckets(struct hist_bucket *bucket, int max_buckets,

static void show_histogram(const struct hist_bucket *bucket, int buckets,
int total, int scale) {
const char *pat1, *pat2;
int width1, width2;
int i;

assert(bucket != NULL);

switch ((int)(log(bucket[buckets - 1].high) / log(10)) + 1) {
case 1:
case 2:
pat1 = "%4d %2s: ";
pat2 = "%4d-%2d: ";
width1 = 4;
width2 = 2;
break;
case 3:
pat1 = "%5d %3s: ";
pat2 = "%5d-%3d: ";
width1 = 5;
width2 = 3;
break;
case 4:
pat1 = "%6d %4s: ";
pat2 = "%6d-%4d: ";
width1 = 6;
width2 = 4;
break;
case 5:
pat1 = "%7d %5s: ";
pat2 = "%7d-%5d: ";
width1 = 7;
width2 = 5;
break;
case 6:
pat1 = "%8d %6s: ";
pat2 = "%8d-%6d: ";
width1 = 8;
width2 = 6;
break;
case 7:
pat1 = "%9d %7s: ";
pat2 = "%9d-%7d: ";
width1 = 9;
width2 = 7;
break;
default:
pat1 = "%12d %10s: ";
pat2 = "%12d-%10d: ";
width1 = 12;
width2 = 10;
break;
}

Expand All @@ -241,9 +241,10 @@ static void show_histogram(const struct hist_bucket *bucket, int buckets,
assert(len <= HIST_BAR_MAX);

if (bucket[i].low == bucket[i].high)
fprintf(stderr, pat1, bucket[i].low, "");
fprintf(stderr, "%*d %*s: ", width1, bucket[i].low, width2, "");
else
fprintf(stderr, pat2, bucket[i].low, bucket[i].high);
fprintf(stderr, "%*d-%*d: ", width1, bucket[i].low, width2,
bucket[i].high);

for (j = 0; j < HIST_BAR_MAX; j++) fprintf(stderr, j < len ? "=" : " ");
fprintf(stderr, "\t%5d (%6.2f%%)\n", bucket[i].count, pct);
Expand Down
18 changes: 15 additions & 3 deletions tools_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,24 @@ extern "C" {
#define VPX_NO_RETURN
#endif

// Tells the compiler to perform `printf` format string checking if the
// compiler supports it; see the 'format' attribute in
// <https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html>.
#define VPX_TOOLS_FORMAT_PRINTF(string_index, first_to_check)
#if defined(__has_attribute)
#if __has_attribute(format)
#undef VPX_TOOLS_FORMAT_PRINTF
#define VPX_TOOLS_FORMAT_PRINTF(string_index, first_to_check) \
__attribute__((__format__(__printf__, string_index, first_to_check)))
#endif
#endif

/* Sets a stdio stream into binary mode */
FILE *set_binary_mode(FILE *stream);

VPX_NO_RETURN void die(const char *fmt, ...);
VPX_NO_RETURN void fatal(const char *fmt, ...);
void warn(const char *fmt, ...);
VPX_NO_RETURN void die(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);
VPX_NO_RETURN void fatal(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);
void warn(const char *fmt, ...) VPX_TOOLS_FORMAT_PRINTF(1, 2);

VPX_NO_RETURN void die_codec(vpx_codec_ctx_t *ctx, const char *s);

Expand Down
12 changes: 7 additions & 5 deletions vpxenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ static size_t wrap_fwrite(const void *ptr, size_t size, size_t nmemb,

static const char *exec_name;

static void warn_or_exit_on_errorv(vpx_codec_ctx_t *ctx, int fatal,
const char *s, va_list ap) {
static VPX_TOOLS_FORMAT_PRINTF(3, 0) void warn_or_exit_on_errorv(
vpx_codec_ctx_t *ctx, int fatal, const char *s, va_list ap) {
if (ctx->err) {
const char *detail = vpx_codec_error_detail(ctx);

Expand All @@ -72,16 +72,18 @@ static void warn_or_exit_on_errorv(vpx_codec_ctx_t *ctx, int fatal,
}
}

static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s, ...) {
static VPX_TOOLS_FORMAT_PRINTF(2,
3) void ctx_exit_on_error(vpx_codec_ctx_t *ctx,
const char *s, ...) {
va_list ap;

va_start(ap, s);
warn_or_exit_on_errorv(ctx, 1, s, ap);
va_end(ap);
}

static void warn_or_exit_on_error(vpx_codec_ctx_t *ctx, int fatal,
const char *s, ...) {
static VPX_TOOLS_FORMAT_PRINTF(3, 4) void warn_or_exit_on_error(
vpx_codec_ctx_t *ctx, int fatal, const char *s, ...) {
va_list ap;

va_start(ap, s);
Expand Down
2 changes: 1 addition & 1 deletion vpxstats.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ int stats_open_file(stats_io_t *stats, const char *fpf, int pass) {
stats->buf.buf = malloc(stats->buf_alloc_sz);

if (!stats->buf.buf)
fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
fatal("Failed to allocate first-pass stats buffer (%u bytes)",
(unsigned int)stats->buf_alloc_sz);

nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
Expand Down
2 changes: 1 addition & 1 deletion warnings.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void check_encoder_config(int disable_prompt,
/* Count and print warnings. */
for (warning = warning_list.warning_node; warning != NULL;
warning = warning->next_warning, ++num_warnings) {
warn(warning->warning_string);
warn("%s", warning->warning_string);
}

free_warning_list(&warning_list);
Expand Down

0 comments on commit dd10ac8

Please sign in to comment.