vpxenc: add warning()/fatal() helpers

Cosmetic. Allows exiting with an error message without opening a new
scope.

Change-Id: If227b29b825f0241acea79dd38f19e524552ee18
This commit is contained in:
John Koleszar 2012-02-14 09:37:44 -08:00
parent efd54f8f41
commit c535025c12

114
vpxenc.c
View File

@ -84,15 +84,38 @@ static const struct codec_item
static void usage_exit(); static void usage_exit();
#define LOG_ERROR(label) do \
{\
const char *l=label;\
va_list ap;\
va_start(ap, fmt);\
if(l)\
fprintf(stderr, "%s: ", l);\
vfprintf(stderr, fmt, ap);\
fprintf(stderr, "\n");\
va_end(ap);\
} while(0)
void die(const char *fmt, ...) void die(const char *fmt, ...)
{ {
va_list ap; LOG_ERROR(NULL);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
fprintf(stderr, "\n");
usage_exit(); usage_exit();
} }
void fatal(const char *fmt, ...)
{
LOG_ERROR("Fatal");
exit(EXIT_FAILURE);
}
void warn(const char *fmt, ...)
{
LOG_ERROR("Warning");
}
static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s) static void ctx_exit_on_error(vpx_codec_ctx_t *ctx, const char *s)
{ {
if (ctx->err) if (ctx->err)
@ -153,10 +176,7 @@ int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
stats->file = fopen(fpf, "rb"); stats->file = fopen(fpf, "rb");
if (fseek(stats->file, 0, SEEK_END)) if (fseek(stats->file, 0, SEEK_END))
{ fatal("First-pass stats file must be seekable!");
fprintf(stderr, "First-pass stats file must be seekable!\n");
exit(EXIT_FAILURE);
}
stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file); stats->buf.sz = stats->buf_alloc_sz = ftell(stats->file);
rewind(stats->file); rewind(stats->file);
@ -164,11 +184,8 @@ int stats_open_file(stats_io_t *stats, const char *fpf, int pass)
stats->buf.buf = malloc(stats->buf_alloc_sz); stats->buf.buf = malloc(stats->buf_alloc_sz);
if (!stats->buf.buf) if (!stats->buf.buf)
{ fatal("Failed to allocate first-pass stats buffer (%lu bytes)",
fprintf(stderr, "Failed to allocate first-pass stats buffer (%lu bytes)\n",
(unsigned long)stats->buf_alloc_sz); (unsigned long)stats->buf_alloc_sz);
exit(EXIT_FAILURE);
}
nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file); nbytes = fread(stats->buf.buf, 1, stats->buf.sz, stats->file);
res = (nbytes == stats->buf.sz); res = (nbytes == stats->buf.sz);
@ -240,11 +257,7 @@ void stats_write(stats_io_t *stats, const void *pkt, size_t len)
stats->buf_alloc_sz = new_sz; stats->buf_alloc_sz = new_sz;
} }
else else
{ fatal("Failed to realloc firstpass stats buffer.");
fprintf(stderr,
"\nFailed to realloc firstpass stats buffer.\n");
exit(EXIT_FAILURE);
}
} }
memcpy(stats->buf_ptr, pkt, len); memcpy(stats->buf_ptr, pkt, len);
@ -391,8 +404,8 @@ unsigned int file_is_ivf(FILE *infile,
is_ivf = 1; is_ivf = 1;
if (mem_get_le16(raw_hdr + 4) != 0) if (mem_get_le16(raw_hdr + 4) != 0)
fprintf(stderr, "Error: Unrecognized IVF version! This file may not" warn("Unrecognized IVF version! This file may not decode "
" decode properly."); "properly.");
*fourcc = mem_get_le32(raw_hdr + 8); *fourcc = mem_get_le32(raw_hdr + 8);
} }
@ -762,10 +775,7 @@ write_webm_block(EbmlGlobal *glob,
if(new_cue_list) if(new_cue_list)
glob->cue_list = new_cue_list; glob->cue_list = new_cue_list;
else else
{ fatal("Failed to realloc cue list.");
fprintf(stderr, "\nFailed to realloc cue list.\n");
exit(EXIT_FAILURE);
}
cue = &glob->cue_list[glob->cues]; cue = &glob->cue_list[glob->cues];
cue->time = glob->cluster_timecode; cue->time = glob->cluster_timecode;
@ -1568,7 +1578,7 @@ static void parse_global_config(struct global_config *global, char **argv)
/* DWIM: Assume the user meant passes=2 if pass=2 is specified */ /* DWIM: Assume the user meant passes=2 if pass=2 is specified */
if (global->pass > global->passes) if (global->pass > global->passes)
{ {
fprintf(stderr, "Warning: Assuming --pass=%d implies --passes=%d\n", warn("Assuming --pass=%d implies --passes=%d\n",
global->pass, global->pass); global->pass, global->pass);
global->passes = global->pass; global->passes = global->pass;
} }
@ -1631,11 +1641,7 @@ int main(int argc, const char **argv_)
global.usage); global.usage);
if (res) if (res)
{ fatal("Failed to get config: %s", vpx_codec_err_to_string(res));
fprintf(stderr, "Failed to get config: %s\n",
vpx_codec_err_to_string(res));
return EXIT_FAILURE;
}
/* Change the default timebase to a high enough value so that the encoder /* Change the default timebase to a high enough value so that the encoder
* will always create strictly increasing timestamps. * will always create strictly increasing timestamps.
@ -1701,27 +1707,21 @@ int main(int argc, const char **argv_)
cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg); cfg.rc_2pass_vbr_bias_pct = arg_parse_uint(&arg);
if (global.passes < 2) if (global.passes < 2)
fprintf(stderr, warn("option %s ignored in one-pass mode.\n", arg.name);
"Warning: option %s ignored in one-pass mode.\n",
arg.name);
} }
else if (arg_match(&arg, &minsection_pct, argi)) else if (arg_match(&arg, &minsection_pct, argi))
{ {
cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg); cfg.rc_2pass_vbr_minsection_pct = arg_parse_uint(&arg);
if (global.passes < 2) if (global.passes < 2)
fprintf(stderr, warn("option %s ignored in one-pass mode.\n", arg.name);
"Warning: option %s ignored in one-pass mode.\n",
arg.name);
} }
else if (arg_match(&arg, &maxsection_pct, argi)) else if (arg_match(&arg, &maxsection_pct, argi))
{ {
cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg); cfg.rc_2pass_vbr_maxsection_pct = arg_parse_uint(&arg);
if (global.passes < 2) if (global.passes < 2)
fprintf(stderr, warn("option %s ignored in one-pass mode.\n", arg.name);
"Warning: option %s ignored in one-pass mode.\n",
arg.name);
} }
else if (arg_match(&arg, &kf_min_dist, argi)) else if (arg_match(&arg, &kf_min_dist, argi))
cfg.kf_min_dist = arg_parse_uint(&arg); cfg.kf_min_dist = arg_parse_uint(&arg);
@ -1808,10 +1808,7 @@ int main(int argc, const char **argv_)
: set_binary_mode(stdin); : set_binary_mode(stdin);
if (!infile) if (!infile)
{ fatal("Failed to open input file");
fprintf(stderr, "Failed to open input file\n");
return EXIT_FAILURE;
}
/* For RAW input sources, these bytes will applied on the first frame /* For RAW input sources, these bytes will applied on the first frame
* in read_frame(). * in read_frame().
@ -1839,10 +1836,7 @@ int main(int argc, const char **argv_)
global.use_i420 = 0; global.use_i420 = 0;
} }
else else
{ fatal("Unsupported Y4M stream.");
fprintf(stderr, "Unsupported Y4M stream.\n");
return EXIT_FAILURE;
}
} }
else if (detect.buf_read == 4 && else if (detect.buf_read == 4 &&
file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, &detect)) file_is_ivf(infile, &fourcc, &cfg.g_w, &cfg.g_h, &detect))
@ -1857,8 +1851,7 @@ int main(int argc, const char **argv_)
global.use_i420 = 1; global.use_i420 = 1;
break; break;
default: default:
fprintf(stderr, "Unsupported fourcc (%08x) in IVF\n", fourcc); fatal("Unsupported fourcc (%08x) in IVF", fourcc);
return EXIT_FAILURE;
} }
} }
else else
@ -1867,11 +1860,8 @@ int main(int argc, const char **argv_)
} }
if(!cfg.g_w || !cfg.g_h) if(!cfg.g_w || !cfg.g_h)
{ fatal("Specify stream dimensions with --width (-w) "
fprintf(stderr, "Specify stream dimensions with --width (-w) "
" and --height (-h).\n"); " and --height (-h).\n");
return EXIT_FAILURE;
}
#define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field) #define SHOW(field) fprintf(stderr, " %-28s = %d\n", #field, cfg.field)
@ -1932,32 +1922,20 @@ int main(int argc, const char **argv_)
: set_binary_mode(stdout); : set_binary_mode(stdout);
if (!outfile) if (!outfile)
{ fatal("Failed to open output file");
fprintf(stderr, "Failed to open output file\n");
return EXIT_FAILURE;
}
if(global.write_webm && fseek(outfile, 0, SEEK_CUR)) if(global.write_webm && fseek(outfile, 0, SEEK_CUR))
{ fatal("WebM output to pipes not supported.");
fprintf(stderr, "WebM output to pipes not supported.\n");
return EXIT_FAILURE;
}
if (global.stats_fn) if (global.stats_fn)
{ {
if (!stats_open_file(&stats, global.stats_fn, pass)) if (!stats_open_file(&stats, global.stats_fn, pass))
{ fatal("Failed to open statistics store");
fprintf(stderr, "Failed to open statistics store\n");
return EXIT_FAILURE;
}
} }
else else
{ {
if (!stats_open_mem(&stats, pass)) if (!stats_open_mem(&stats, pass))
{ fatal("Failed to open statistics store");
fprintf(stderr, "Failed to open statistics store\n");
return EXIT_FAILURE;
}
} }
cfg.g_pass = global.passes == 2 cfg.g_pass = global.passes == 2