]> git.sesse.net Git - ffmpeg/blobdiff - ffmpeg.c
remove useless title check
[ffmpeg] / ffmpeg.c
index 7c8ff4910ed3aaeeb883af2afe437777c8d0f32d..f428ba55501754f11b125a8572f3a82e81b5d6b0 100644 (file)
--- a/ffmpeg.c
+++ b/ffmpeg.c
 #include <errno.h>
 #include <signal.h>
 #include <limits.h>
-#include "avformat.h"
-#include "avdevice.h"
-#include "swscale.h"
-#include "framehook.h"
-#include "opt.h"
-#include "fifo.h"
-#include "avstring.h"
-#include "os_support.h"
-
-#if !defined(HAVE_GETRUSAGE) && defined(HAVE_GETPROCESSTIMES)
+#include "libavformat/avformat.h"
+#include "libavdevice/avdevice.h"
+#include "libswscale/swscale.h"
+#include "libavformat/framehook.h"
+#include "libavcodec/opt.h"
+#include "libavutil/fifo.h"
+#include "libavutil/avstring.h"
+#include "libavformat/os_support.h"
+
+#ifdef HAVE_SYS_RESOURCE_H
+#include <sys/types.h>
+#include <sys/resource.h>
+#elif defined(HAVE_GETPROCESSTIMES)
 #include <windows.h>
 #endif
 
@@ -46,7 +49,6 @@
 #include <sys/ioctl.h>
 #include <sys/time.h>
 #include <termios.h>
-#include <sys/resource.h>
 #elif defined(HAVE_CONIO_H)
 #include <conio.h>
 #endif
@@ -61,7 +63,7 @@
 
 #undef exit
 
-static const char program_name[] = "FFmpeg";
+const char program_name[] = "FFmpeg";
 static const int program_birth_year = 2000;
 
 /* select an input stream for an output stream */
@@ -78,7 +80,7 @@ typedef struct AVMetaDataMap {
     int in_file;
 } AVMetaDataMap;
 
-extern const OptionDef options[];
+static const OptionDef options[];
 
 #define MAX_FILES 20
 
@@ -371,6 +373,59 @@ static int decode_interrupt_cb(void)
     return q_pressed || (q_pressed = read_key() == 'q');
 }
 
+static int av_exit(int ret)
+{
+    int i;
+
+    /* close files */
+    for(i=0;i<nb_output_files;i++) {
+        /* maybe av_close_output_file ??? */
+        AVFormatContext *s = output_files[i];
+        int j;
+        if (!(s->oformat->flags & AVFMT_NOFILE) && s->pb)
+            url_fclose(s->pb);
+        for(j=0;j<s->nb_streams;j++) {
+            av_free(s->streams[j]->codec);
+            av_free(s->streams[j]);
+        }
+        av_free(s);
+    }
+    for(i=0;i<nb_input_files;i++)
+        av_close_input_file(input_files[i]);
+
+    av_free_static();
+
+    av_free(intra_matrix);
+    av_free(inter_matrix);
+
+    if (vstats_file)
+        fclose(vstats_file);
+    av_free(vstats_filename);
+
+    av_free(opt_names);
+
+    av_free(video_codec_name);
+    av_free(audio_codec_name);
+    av_free(subtitle_codec_name);
+
+    av_free(video_standard);
+
+#ifdef CONFIG_POWERPC_PERF
+    extern void powerpc_display_perf_report(void);
+    powerpc_display_perf_report();
+#endif /* CONFIG_POWERPC_PERF */
+
+    if (received_sigterm) {
+        fprintf(stderr,
+            "Received signal %d: terminating.\n",
+            (int) received_sigterm);
+        exit (255);
+    }
+
+    exit(ret); /* not all OS-es handle main() return value */
+    return ret;
+}
+
 static int read_ffserver_streams(AVFormatContext *s, const char *filename)
 {
     int i, err;
@@ -429,7 +484,7 @@ static void write_frame(AVFormatContext *s, AVPacket *pkt, AVCodecContext *avctx
     ret= av_interleaved_write_frame(s, pkt);
     if(ret < 0){
         print_error("av_interleaved_write_frame()", ret);
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -467,7 +522,7 @@ static void do_audio_out(AVFormatContext *s,
             fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
                     dec->channels, dec->sample_rate,
                     enc->channels, enc->sample_rate);
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -841,7 +896,7 @@ static void do_video_out(AVFormatContext *s,
                                        &big_picture);
             if (ret == -1) {
                 fprintf(stderr, "Video encoding failed\n");
-                exit(1);
+                av_exit(1);
             }
             //enc->frame_number = enc->real_pict_num;
             if(ret>0){
@@ -887,7 +942,7 @@ static void do_video_stats(AVFormatContext *os, AVOutputStream *ost,
         vstats_file = fopen(vstats_filename, "w");
         if (!vstats_file) {
             perror("fopen");
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -1011,7 +1066,7 @@ static void print_report(AVFormatContext **output_files,
         bitrate = (double)(total_size * 8) / ti1 / 1000.0;
 
         snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
-            "size=%8.0fkB time=%0.1f bitrate=%6.1fkbits/s",
+            "size=%8.0fkB time=%0.2f bitrate=%6.1fkbits/s",
             (double)total_size / 1024, ti1, bitrate);
 
         if (verbose > 1)
@@ -1472,13 +1527,13 @@ static int av_encode(AVFormatContext **output_files,
         os = output_files[i];
         if (!os->nb_streams) {
             fprintf(stderr, "Output file does not contain any stream\n");
-            exit(1);
+            av_exit(1);
         }
         nb_ostreams += os->nb_streams;
     }
     if (nb_stream_maps > 0 && nb_stream_maps != nb_ostreams) {
         fprintf(stderr, "Number of stream maps must match number of output streams\n");
-        exit(1);
+        av_exit(1);
     }
 
     /* Sanity check the mapping args -- do the input files & streams exist? */
@@ -1489,14 +1544,14 @@ static int av_encode(AVFormatContext **output_files,
         if (fi < 0 || fi > nb_input_files - 1 ||
             si < 0 || si > file_table[fi].nb_streams - 1) {
             fprintf(stderr,"Could not find input stream #%d.%d\n", fi, si);
-            exit(1);
+            av_exit(1);
         }
         fi = stream_maps[i].sync_file_index;
         si = stream_maps[i].sync_stream_index;
         if (fi < 0 || fi > nb_input_files - 1 ||
             si < 0 || si > file_table[fi].nb_streams - 1) {
             fprintf(stderr,"Could not find sync stream #%d.%d\n", fi, si);
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -1528,7 +1583,7 @@ static int av_encode(AVFormatContext **output_files,
                     fprintf(stderr, "Codec type mismatch for mapping #%d.%d -> #%d.%d\n",
                         stream_maps[n-1].file_index, stream_maps[n-1].stream_index,
                         ost->file_index, ost->index);
-                    exit(1);
+                    av_exit(1);
                 }
 
             } else {
@@ -1567,7 +1622,7 @@ static int av_encode(AVFormatContext **output_files,
                     if (!found) {
                         fprintf(stderr, "Could not find input stream matching output stream #%d.%d\n",
                                 ost->file_index, ost->index);
-                        exit(1);
+                        av_exit(1);
                     }
                 }
             }
@@ -1625,7 +1680,7 @@ static int av_encode(AVFormatContext **output_files,
             case CODEC_TYPE_VIDEO:
                 if(using_vhook) {
                     fprintf(stderr,"-vcodec copy and -vhook are incompatible (frames are not decoded)\n");
-                    exit(1);
+                    av_exit(1);
                 }
                 codec->pix_fmt = icodec->pix_fmt;
                 codec->width = icodec->width;
@@ -1678,7 +1733,7 @@ static int av_encode(AVFormatContext **output_files,
                     if( avpicture_alloc( (AVPicture*)&ost->pict_tmp, codec->pix_fmt,
                                          codec->width, codec->height ) ) {
                         fprintf(stderr, "Cannot allocate temp picture, check pix fmt\n");
-                        exit(1);
+                        av_exit(1);
                     }
                     sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
                     ost->img_resample_ctx = sws_getContext(
@@ -1691,7 +1746,7 @@ static int av_encode(AVFormatContext **output_files,
                             sws_flags, NULL, NULL, NULL);
                     if (ost->img_resample_ctx == NULL) {
                         fprintf(stderr, "Cannot get resampling context\n");
-                        exit(1);
+                        av_exit(1);
                     }
                     ost->resample_height = icodec->height - (frame_topBand + frame_bottomBand);
                 }
@@ -1721,7 +1776,7 @@ static int av_encode(AVFormatContext **output_files,
                     f = fopen(logfilename, "w");
                     if (!f) {
                         perror(logfilename);
-                        exit(1);
+                        av_exit(1);
                     }
                     ost->logfile = f;
                 } else {
@@ -1729,7 +1784,7 @@ static int av_encode(AVFormatContext **output_files,
                     f = fopen(logfilename, "r");
                     if (!f) {
                         perror(logfilename);
-                        exit(1);
+                        av_exit(1);
                     }
                     fseek(f, 0, SEEK_END);
                     size = ftell(f);
@@ -1737,7 +1792,7 @@ static int av_encode(AVFormatContext **output_files,
                     logbuffer = av_malloc(size + 1);
                     if (!logbuffer) {
                         fprintf(stderr, "Could not allocate log buffer\n");
-                        exit(1);
+                        av_exit(1);
                     }
                     size = fread(logbuffer, 1, size, f);
                     fclose(f);
@@ -1790,12 +1845,12 @@ static int av_encode(AVFormatContext **output_files,
             if (!codec) {
                 fprintf(stderr, "Unsupported codec for output stream #%d.%d\n",
                         ost->file_index, ost->index);
-                exit(1);
+                av_exit(1);
             }
             if (avcodec_open(ost->st->codec, codec) < 0) {
                 fprintf(stderr, "Error while opening codec for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
                         ost->file_index, ost->index);
-                exit(1);
+                av_exit(1);
             }
             extra_size += ost->st->codec->extradata_size;
         }
@@ -1810,12 +1865,12 @@ static int av_encode(AVFormatContext **output_files,
             if (!codec) {
                 fprintf(stderr, "Unsupported codec (id=%d) for input stream #%d.%d\n",
                         ist->st->codec->codec_id, ist->file_index, ist->index);
-                exit(1);
+                av_exit(1);
             }
             if (avcodec_open(ist->st->codec, codec) < 0) {
                 fprintf(stderr, "Error while opening codec for input stream #%d.%d\n",
                         ist->file_index, ist->index);
-                exit(1);
+                av_exit(1);
             }
             //if (ist->st->codec->codec_type == CODEC_TYPE_VIDEO)
             //    ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
@@ -2118,7 +2173,7 @@ static void opt_format(const char *arg)
     file_oformat = guess_format(arg, NULL, NULL);
     if (!file_iformat && !file_oformat) {
         fprintf(stderr, "Unknown input or output format: %s\n", arg);
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2171,22 +2226,24 @@ static void opt_video_rc_override_string(const char *arg)
     video_rc_override_string = arg;
 }
 
-static void opt_me_threshold(const char *arg)
+static int opt_me_threshold(const char *opt, const char *arg)
 {
-    me_threshold = atoi(arg);
+    me_threshold = parse_number_or_die(opt, arg, OPT_INT64, INT_MIN, INT_MAX);
+    return 0;
 }
 
-static void opt_verbose(const char *arg)
+static int opt_verbose(const char *opt, const char *arg)
 {
-    verbose = atoi(arg);
+    verbose = parse_number_or_die(opt, arg, OPT_INT64, AV_LOG_QUIET, AV_LOG_DEBUG);
     av_log_set_level(verbose);
+    return 0;
 }
 
 static void opt_frame_rate(const char *arg)
 {
     if (av_parse_video_frame_rate(&frame_rate, arg) < 0) {
         fprintf(stderr, "Incorrect frame rate\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2207,15 +2264,15 @@ static void opt_frame_crop_top(const char *arg)
     frame_topBand = atoi(arg);
     if (frame_topBand < 0) {
         fprintf(stderr, "Incorrect top crop size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_topBand % 2) != 0) {
         fprintf(stderr, "Top crop size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_topBand) >= frame_height){
         fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
-        exit(1);
+        av_exit(1);
     }
     frame_height -= frame_topBand;
 }
@@ -2225,15 +2282,15 @@ static void opt_frame_crop_bottom(const char *arg)
     frame_bottomBand = atoi(arg);
     if (frame_bottomBand < 0) {
         fprintf(stderr, "Incorrect bottom crop size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_bottomBand % 2) != 0) {
         fprintf(stderr, "Bottom crop size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_bottomBand) >= frame_height){
         fprintf(stderr, "Vertical crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
-        exit(1);
+        av_exit(1);
     }
     frame_height -= frame_bottomBand;
 }
@@ -2243,15 +2300,15 @@ static void opt_frame_crop_left(const char *arg)
     frame_leftBand = atoi(arg);
     if (frame_leftBand < 0) {
         fprintf(stderr, "Incorrect left crop size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_leftBand % 2) != 0) {
         fprintf(stderr, "Left crop size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_leftBand) >= frame_width){
         fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
-        exit(1);
+        av_exit(1);
     }
     frame_width -= frame_leftBand;
 }
@@ -2261,15 +2318,15 @@ static void opt_frame_crop_right(const char *arg)
     frame_rightBand = atoi(arg);
     if (frame_rightBand < 0) {
         fprintf(stderr, "Incorrect right crop size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_rightBand % 2) != 0) {
         fprintf(stderr, "Right crop size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_rightBand) >= frame_width){
         fprintf(stderr, "Horizontal crop dimensions are outside the range of the original image.\nRemember to crop first and scale second.\n");
-        exit(1);
+        av_exit(1);
     }
     frame_width -= frame_rightBand;
 }
@@ -2278,11 +2335,11 @@ static void opt_frame_size(const char *arg)
 {
     if (av_parse_video_frame_size(&frame_width, &frame_height, arg) < 0) {
         fprintf(stderr, "Incorrect frame size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_width % 2) != 0 || (frame_height % 2) != 0) {
         fprintf(stderr, "Frame size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2323,11 +2380,11 @@ static void opt_frame_pad_top(const char *arg)
     frame_padtop = atoi(arg);
     if (frame_padtop < 0) {
         fprintf(stderr, "Incorrect top pad size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_padtop % 2) != 0) {
         fprintf(stderr, "Top pad size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2336,11 +2393,11 @@ static void opt_frame_pad_bottom(const char *arg)
     frame_padbottom = atoi(arg);
     if (frame_padbottom < 0) {
         fprintf(stderr, "Incorrect bottom pad size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_padbottom % 2) != 0) {
         fprintf(stderr, "Bottom pad size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2350,11 +2407,11 @@ static void opt_frame_pad_left(const char *arg)
     frame_padleft = atoi(arg);
     if (frame_padleft < 0) {
         fprintf(stderr, "Incorrect left pad size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_padleft % 2) != 0) {
         fprintf(stderr, "Left pad size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2364,15 +2421,15 @@ static void opt_frame_pad_right(const char *arg)
     frame_padright = atoi(arg);
     if (frame_padright < 0) {
         fprintf(stderr, "Incorrect right pad size\n");
-        exit(1);
+        av_exit(1);
     }
     if ((frame_padright % 2) != 0) {
         fprintf(stderr, "Right pad size must be a multiple of 2\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
-void list_pix_fmts(void)
+static void list_pix_fmts(void)
 {
     int i;
     char pix_fmt_str[128];
@@ -2388,7 +2445,7 @@ static void opt_frame_pix_fmt(const char *arg)
         frame_pix_fmt = avcodec_get_pix_fmt(arg);
     else {
         list_pix_fmts();
-        exit(0);
+        av_exit(0);
     }
 }
 
@@ -2411,7 +2468,7 @@ static void opt_frame_aspect_ratio(const char *arg)
 
     if (!ar) {
         fprintf(stderr, "Incorrect aspect ratio specification.\n");
-        exit(1);
+        av_exit(1);
     }
     frame_aspect_ratio = ar;
 }
@@ -2422,7 +2479,7 @@ static void opt_qscale(const char *arg)
     if (video_qscale <= 0 ||
         video_qscale > 255) {
         fprintf(stderr, "qscale must be > 0.0 and <= 255\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2432,7 +2489,7 @@ static void opt_qdiff(const char *arg)
     if (video_qdiff < 0 ||
         video_qdiff > 31) {
         fprintf(stderr, "qdiff must be >= 1 and <= 31\n");
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -2455,14 +2512,16 @@ static void opt_thread_count(const char *arg)
 #endif
 }
 
-static void opt_audio_rate(const char *arg)
+static int opt_audio_rate(const char *opt, const char *arg)
 {
-    audio_sample_rate = atoi(arg);
+    audio_sample_rate = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
+    return 0;
 }
 
-static void opt_audio_channels(const char *arg)
+static int opt_audio_channels(const char *opt, const char *arg)
 {
-    audio_channels = atoi(arg);
+    audio_channels = parse_number_or_die(opt, arg, OPT_INT64, 0, INT_MAX);
+    return 0;
 }
 
 static void opt_video_channel(const char *arg)
@@ -2527,7 +2586,7 @@ static void add_frame_hooker(const char *arg)
 
     if (i != 0) {
         fprintf(stderr, "Failed to add video hook function: %s\n", arg);
-        exit(1);
+        av_exit(1);
     }
 }
 #endif
@@ -2580,17 +2639,6 @@ static void opt_map_meta_data(const char *arg)
     m->in_file = strtol(p, &p, 0);
 }
 
-static int64_t parse_time_or_die(const char *context, const char *timestr, int is_duration)
-{
-    int64_t us = parse_date(timestr, is_duration);
-    if (us == INT64_MIN) {
-        fprintf(stderr, "Invalid %s specification for %s: %s\n",
-                is_duration ? "duration" : "date", context, timestr);
-        exit(1);
-    }
-    return us;
-}
-
 static int opt_recording_time(const char *opt, const char *arg)
 {
     recording_time = parse_time_or_die(opt, arg, 1);
@@ -2627,11 +2675,11 @@ static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
         avcodec_find_decoder_by_name(name);
     if(!codec) {
         av_log(NULL, AV_LOG_ERROR, "Unknown %s '%s'\n", codec_string, name);
-        exit(1);
+        av_exit(1);
     }
     if(codec->type != type) {
         av_log(NULL, AV_LOG_ERROR, "Invalid %s type '%s'\n", codec_string, name);
-        exit(1);
+        av_exit(1);
     }
     return codec->id;
 }
@@ -2684,7 +2732,7 @@ static void opt_input_file(const char *filename)
     err = av_open_input_file(&ic, filename, file_iformat, 0, ap);
     if (err < 0) {
         print_error(filename, err);
-        exit(1);
+        av_exit(1);
     }
     if(opt_programid) {
         int i;
@@ -2700,7 +2748,7 @@ static void opt_input_file(const char *filename)
     ret = av_find_stream_info(ic);
     if (ret < 0 && verbose >= 0) {
         fprintf(stderr, "%s: could not find codec parameters\n", filename);
-        exit(1);
+        av_exit(1);
     }
 
     timestamp = start_time;
@@ -2855,7 +2903,7 @@ static void new_video_stream(AVFormatContext *oc)
     st = av_new_stream(oc, oc->nb_streams);
     if (!st) {
         fprintf(stderr, "Could not alloc stream\n");
-        exit(1);
+        av_exit(1);
     }
     avcodec_get_context_defaults2(st->codec, CODEC_TYPE_VIDEO);
     bitstream_filters[nb_output_files][oc->nb_streams - 1]= video_bitstream_filters;
@@ -2957,7 +3005,7 @@ static void new_video_stream(AVFormatContext *oc)
             int e=sscanf(p, "%d,%d,%d", &start, &end, &q);
             if(e!=3){
                 fprintf(stderr, "error parsing rc_override\n");
-                exit(1);
+                av_exit(1);
             }
             video_enc->rc_override=
                 av_realloc(video_enc->rc_override,
@@ -3010,7 +3058,7 @@ static void new_audio_stream(AVFormatContext *oc)
     st = av_new_stream(oc, oc->nb_streams);
     if (!st) {
         fprintf(stderr, "Could not alloc stream\n");
-        exit(1);
+        av_exit(1);
     }
     avcodec_get_context_defaults2(st->codec, CODEC_TYPE_AUDIO);
 
@@ -3079,7 +3127,7 @@ static void new_subtitle_stream(AVFormatContext *oc)
     st = av_new_stream(oc, oc->nb_streams);
     if (!st) {
         fprintf(stderr, "Could not alloc stream\n");
-        exit(1);
+        av_exit(1);
     }
     avcodec_get_context_defaults2(st->codec, CODEC_TYPE_SUBTITLE);
 
@@ -3117,7 +3165,7 @@ static void opt_new_audio_stream(void)
     AVFormatContext *oc;
     if (nb_output_files <= 0) {
         fprintf(stderr, "At least one output file must be specified\n");
-        exit(1);
+        av_exit(1);
     }
     oc = output_files[nb_output_files - 1];
     new_audio_stream(oc);
@@ -3128,7 +3176,7 @@ static void opt_new_video_stream(void)
     AVFormatContext *oc;
     if (nb_output_files <= 0) {
         fprintf(stderr, "At least one output file must be specified\n");
-        exit(1);
+        av_exit(1);
     }
     oc = output_files[nb_output_files - 1];
     new_video_stream(oc);
@@ -3139,7 +3187,7 @@ static void opt_new_subtitle_stream(void)
     AVFormatContext *oc;
     if (nb_output_files <= 0) {
         fprintf(stderr, "At least one output file must be specified\n");
-        exit(1);
+        av_exit(1);
     }
     oc = output_files[nb_output_files - 1];
     new_subtitle_stream(oc);
@@ -3162,7 +3210,7 @@ static void opt_output_file(const char *filename)
         if (!file_oformat) {
             fprintf(stderr, "Unable to find a suitable output format for '%s'\n",
                     filename);
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -3175,7 +3223,7 @@ static void opt_output_file(const char *filename)
            parameters from ffserver */
         if (read_ffserver_streams(oc, filename) < 0) {
             fprintf(stderr, "Could not read stream parameters from '%s'\n", filename);
-            exit(1);
+            av_exit(1);
         }
     } else {
         use_video = file_oformat->video_codec != CODEC_ID_NONE || video_stream_copy || video_codec_name;
@@ -3240,7 +3288,7 @@ static void opt_output_file(const char *filename)
     if (oc->oformat->flags & AVFMT_NEEDNUMBER) {
         if (!av_filename_number_test(oc->filename)) {
             print_error(oc->filename, AVERROR_NUMEXPECTED);
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -3259,12 +3307,12 @@ static void opt_output_file(const char *filename)
                     c = getchar();
                     if (toupper(c) != 'Y') {
                         fprintf(stderr, "Not overwriting - exiting\n");
-                        exit(1);
+                        av_exit(1);
                     }
                                 }
                                 else {
                     fprintf(stderr,"File '%s' already exists. Exiting.\n", filename);
-                    exit(1);
+                    av_exit(1);
                                 }
             }
         }
@@ -3272,7 +3320,7 @@ static void opt_output_file(const char *filename)
         /* open the file */
         if (url_fopen(&oc->pb, filename, URL_WRONLY) < 0) {
             fprintf(stderr, "Could not open '%s'\n", filename);
-            exit(1);
+            av_exit(1);
         }
     }
 
@@ -3280,7 +3328,7 @@ static void opt_output_file(const char *filename)
     if (av_set_parameters(oc, ap) < 0) {
         fprintf(stderr, "%s: Invalid encoding parameters\n",
                 oc->filename);
-        exit(1);
+        av_exit(1);
     }
 
     oc->preload= (int)(mux_preload*AV_TIME_BASE);
@@ -3307,7 +3355,7 @@ static void opt_pass(const char *pass_str)
     pass = atoi(pass_str);
     if (pass != 1 && pass != 2) {
         fprintf(stderr, "pass number can be only 1 or 2\n");
-        exit(1);
+        av_exit(1);
     }
     do_pass = pass;
 }
@@ -3330,129 +3378,6 @@ static int64_t getutime(void)
 #endif
 }
 
-static void opt_show_formats(void)
-{
-    AVInputFormat *ifmt=NULL;
-    AVOutputFormat *ofmt=NULL;
-    URLProtocol *up=NULL;
-    AVCodec *p=NULL, *p2;
-    AVBitStreamFilter *bsf=NULL;
-    const char *last_name;
-
-    printf("File formats:\n");
-    last_name= "000";
-    for(;;){
-        int decode=0;
-        int encode=0;
-        const char *name=NULL;
-        const char *long_name=NULL;
-
-        while((ofmt= av_oformat_next(ofmt))) {
-            if((name == NULL || strcmp(ofmt->name, name)<0) &&
-                strcmp(ofmt->name, last_name)>0){
-                name= ofmt->name;
-                long_name= ofmt->long_name;
-                encode=1;
-            }
-        }
-        while((ifmt= av_iformat_next(ifmt))) {
-            if((name == NULL || strcmp(ifmt->name, name)<0) &&
-                strcmp(ifmt->name, last_name)>0){
-                name= ifmt->name;
-                long_name= ifmt->long_name;
-                encode=0;
-            }
-            if(name && strcmp(ifmt->name, name)==0)
-                decode=1;
-        }
-        if(name==NULL)
-            break;
-        last_name= name;
-
-        printf(
-            " %s%s %-15s %s\n",
-            decode ? "D":" ",
-            encode ? "E":" ",
-            name,
-            long_name ? long_name:" ");
-    }
-    printf("\n");
-
-    printf("Codecs:\n");
-    last_name= "000";
-    for(;;){
-        int decode=0;
-        int encode=0;
-        int cap=0;
-        const char *type_str;
-
-        p2=NULL;
-        while((p= av_codec_next(p))) {
-            if((p2==NULL || strcmp(p->name, p2->name)<0) &&
-                strcmp(p->name, last_name)>0){
-                p2= p;
-                decode= encode= cap=0;
-            }
-            if(p2 && strcmp(p->name, p2->name)==0){
-                if(p->decode) decode=1;
-                if(p->encode) encode=1;
-                cap |= p->capabilities;
-            }
-        }
-        if(p2==NULL)
-            break;
-        last_name= p2->name;
-
-        switch(p2->type) {
-        case CODEC_TYPE_VIDEO:
-            type_str = "V";
-            break;
-        case CODEC_TYPE_AUDIO:
-            type_str = "A";
-            break;
-        case CODEC_TYPE_SUBTITLE:
-            type_str = "S";
-            break;
-        default:
-            type_str = "?";
-            break;
-        }
-        printf(
-            " %s%s%s%s%s%s %s",
-            decode ? "D": (/*p2->decoder ? "d":*/" "),
-            encode ? "E":" ",
-            type_str,
-            cap & CODEC_CAP_DRAW_HORIZ_BAND ? "S":" ",
-            cap & CODEC_CAP_DR1 ? "D":" ",
-            cap & CODEC_CAP_TRUNCATED ? "T":" ",
-            p2->name);
-       /* if(p2->decoder && decode==0)
-            printf(" use %s for decoding", p2->decoder->name);*/
-        printf("\n");
-    }
-    printf("\n");
-
-    printf("Bitstream filters:\n");
-    while((bsf = av_bitstream_filter_next(bsf)))
-        printf(" %s", bsf->name);
-    printf("\n");
-
-    printf("Supported file protocols:\n");
-    while((up = av_protocol_next(up)))
-        printf(" %s:", up->name);
-    printf("\n");
-
-    printf("Frame size, frame rate abbreviations:\n ntsc pal qntsc qpal sntsc spal film ntsc-film sqcif qcif cif 4cif\n");
-    printf("\n");
-    printf(
-"Note, the names of encoders and decoders do not always match, so there are\n"
-"several cases where the above table shows encoder only or decoder only entries\n"
-"even though both encoding and decoding are supported. For example, the h263\n"
-"decoder corresponds to the h263 and h263p encoders, for file formats it is even\n"
-"worse.\n");
-    exit(0);
-}
-
 static void parse_matrix_coeffs(uint16_t *dest, const char *str)
 {
     int i;
@@ -3464,7 +3389,7 @@ static void parse_matrix_coeffs(uint16_t *dest, const char *str)
         p = strchr(p, ',');
         if(!p) {
             fprintf(stderr, "Syntax error in matrix \"%s\" at coeff %d\n", str, i);
-            exit(1);
+            av_exit(1);
         }
         p++;
     }
@@ -3525,12 +3450,6 @@ static void show_help(void)
     av_opt_show(sws_opts, NULL);
 }
 
-static void opt_show_help(void)
-{
-    show_help();
-    exit(0);
-}
-
 static void opt_target(const char *arg)
 {
     int norm = -1;
@@ -3584,7 +3503,7 @@ static void opt_target(const char *arg)
         fprintf(stderr, "Could not determine norm (PAL/NTSC/NTSC-Film) for target.\n");
         fprintf(stderr, "Please prefix target with \"pal-\", \"ntsc-\" or \"film-\",\n");
         fprintf(stderr, "or set a framerate with \"-r xxx\".\n");
-        exit(1);
+        av_exit(1);
     }
 
     if(!strcmp(arg, "vcd")) {
@@ -3672,7 +3591,7 @@ static void opt_target(const char *arg)
 
     } else {
         fprintf(stderr, "Unknown target: %s\n", arg);
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -3700,7 +3619,7 @@ static int opt_bsf(const char *opt, const char *arg)
 
     if(!bsfc){
         fprintf(stderr, "Unknown bitstream filter %s\n", arg);
-        exit(1);
+        av_exit(1);
     }
 
     bsfp= *opt == 'v' ? &video_bitstream_filters :
@@ -3714,24 +3633,12 @@ static int opt_bsf(const char *opt, const char *arg)
     return 0;
 }
 
-static void opt_show_license(void)
-{
-    show_license();
-    exit(0);
-}
-
-static void opt_show_version(void)
-{
-    show_version(program_name);
-    exit(0);
-}
-
-const OptionDef options[] = {
+static const OptionDef options[] = {
     /* main options */
-    { "L", 0, {(void*)opt_show_license}, "show license" },
-    { "h", 0, {(void*)opt_show_help}, "show help" },
-    { "version", 0, {(void*)opt_show_version}, "show version" },
-    { "formats", 0, {(void*)opt_show_formats}, "show available formats, codecs, protocols, ..." },
+    { "L", OPT_EXIT, {(void*)show_license}, "show license" },
+    { "h", OPT_EXIT, {(void*)show_help}, "show help" },
+    { "version", OPT_EXIT, {(void*)show_version}, "show version" },
+    { "formats", OPT_EXIT, {(void*)show_formats}, "show available formats, codecs, protocols, ..." },
     { "f", HAS_ARG, {(void*)opt_format}, "force format", "fmt" },
     { "i", HAS_ARG, {(void*)opt_input_file}, "input file name", "filename" },
     { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
@@ -3757,21 +3664,21 @@ const OptionDef options[] = {
     { "re", OPT_BOOL | OPT_EXPERT, {(void*)&rate_emu}, "read input at native frame rate", "" },
     { "loop_input", OPT_BOOL | OPT_EXPERT, {(void*)&loop_input}, "loop (current only works with images)" },
     { "loop_output", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&loop_output}, "number of times to loop output in formats that support looping (0 loops forever)", "" },
-    { "v", HAS_ARG, {(void*)opt_verbose}, "set the logging verbosity level", "number" },
+    { "v", HAS_ARG | OPT_FUNC2, {(void*)opt_verbose}, "set the logging verbosity level", "number" },
     { "target", HAS_ARG, {(void*)opt_target}, "specify target file type (\"vcd\", \"svcd\", \"dvd\", \"dv\", \"dv50\", \"pal-vcd\", \"ntsc-svcd\", ...)", "type" },
     { "threads", HAS_ARG | OPT_EXPERT, {(void*)opt_thread_count}, "thread count", "count" },
     { "vsync", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_sync_method}, "video sync method", "" },
     { "async", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&audio_sync_method}, "audio sync method", "" },
-    { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&audio_drift_threshold}, "audio drift threshold", "" },
+    { "adrift_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&audio_drift_threshold}, "audio drift threshold", "threshold" },
     { "vglobal", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&video_global_header}, "video global header storage type", "" },
     { "copyts", OPT_BOOL | OPT_EXPERT, {(void*)&copy_ts}, "copy timestamps" },
     { "shortest", OPT_BOOL | OPT_EXPERT, {(void*)&opt_shortest}, "finish encoding within shortest input" }, //
-    { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "" },
+    { "dts_delta_threshold", HAS_ARG | OPT_FLOAT | OPT_EXPERT, {(void*)&dts_delta_threshold}, "timestamp discontinuity delta threshold", "threshold" },
     { "programid", HAS_ARG | OPT_INT | OPT_EXPERT, {(void*)&opt_programid}, "desired program number", "" },
 
     /* video options */
-    { "b", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "" },
-    { "vb", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "" },
+    { "b", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "bitrate" },
+    { "vb", OPT_FUNC2 | HAS_ARG | OPT_VIDEO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "bitrate" },
     { "vframes", OPT_INT | HAS_ARG | OPT_VIDEO, {(void*)&max_frames[CODEC_TYPE_VIDEO]}, "set the number of video frames to record", "number" },
     { "dframes", OPT_INT | HAS_ARG, {(void*)&max_frames[CODEC_TYPE_DATA]}, "set the number of data frames to record", "number" },
     { "r", HAS_ARG | OPT_VIDEO, {(void*)opt_frame_rate}, "set frame rate (Hz value, fraction or abbreviation)", "rate" },
@@ -3794,7 +3701,7 @@ const OptionDef options[] = {
     { "qdiff", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_qdiff}, "max difference between the quantizer scale (VBR)", "q" },
     { "rc_override", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_video_rc_override_string}, "rate control override for specific intervals", "override" },
     { "vcodec", HAS_ARG | OPT_VIDEO, {(void*)opt_video_codec}, "force video codec ('copy' to copy stream)", "codec" },
-    { "me_threshold", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_threshold}, "motion estimaton threshold",  "" },
+    { "me_threshold", HAS_ARG | OPT_FUNC2 | OPT_EXPERT | OPT_VIDEO, {(void*)opt_me_threshold}, "motion estimaton threshold",  "threshold" },
     { "strict", HAS_ARG | OPT_EXPERT | OPT_VIDEO, {(void*)opt_strict}, "how strictly to follow the standards", "strictness" },
     { "sameq", OPT_BOOL | OPT_VIDEO, {(void*)&same_quality},
       "use same video quality as source (implies VBR)" },
@@ -3817,11 +3724,11 @@ const OptionDef options[] = {
     { "qphist", OPT_BOOL | OPT_EXPERT | OPT_VIDEO, { (void *)&qp_hist }, "show QP histogram" },
 
     /* audio options */
-    { "ab", OPT_FUNC2 | HAS_ARG | OPT_AUDIO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "" },
+    { "ab", OPT_FUNC2 | HAS_ARG | OPT_AUDIO, {(void*)opt_bitrate}, "set bitrate (in bits/s)", "bitrate" },
     { "aframes", OPT_INT | HAS_ARG | OPT_AUDIO, {(void*)&max_frames[CODEC_TYPE_AUDIO]}, "set the number of audio frames to record", "number" },
     { "aq", OPT_FLOAT | HAS_ARG | OPT_AUDIO, {(void*)&audio_qscale}, "set audio quality (codec-specific)", "quality", },
-    { "ar", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
-    { "ac", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
+    { "ar", HAS_ARG | OPT_FUNC2 | OPT_AUDIO, {(void*)opt_audio_rate}, "set audio sampling rate (in Hz)", "rate" },
+    { "ac", HAS_ARG | OPT_FUNC2 | OPT_AUDIO, {(void*)opt_audio_channels}, "set number of audio channels", "channels" },
     { "an", OPT_BOOL | OPT_AUDIO, {(void*)&audio_disable}, "disable audio" },
     { "acodec", HAS_ARG | OPT_AUDIO, {(void*)opt_audio_codec}, "force audio codec ('copy' to copy stream)", "codec" },
     { "atag", HAS_ARG | OPT_EXPERT | OPT_AUDIO, {(void*)opt_audio_tag}, "force audio tag/fourcc", "fourcc/tag" },
@@ -3852,59 +3759,6 @@ const OptionDef options[] = {
     { NULL, },
 };
 
-static int av_exit()
-{
-    int i;
-
-    /* close files */
-    for(i=0;i<nb_output_files;i++) {
-        /* maybe av_close_output_file ??? */
-        AVFormatContext *s = output_files[i];
-        int j;
-        if (!(s->oformat->flags & AVFMT_NOFILE))
-            url_fclose(s->pb);
-        for(j=0;j<s->nb_streams;j++) {
-            av_free(s->streams[j]->codec);
-            av_free(s->streams[j]);
-        }
-        av_free(s);
-    }
-    for(i=0;i<nb_input_files;i++)
-        av_close_input_file(input_files[i]);
-
-    av_free_static();
-
-    av_free(intra_matrix);
-    av_free(inter_matrix);
-
-    if (vstats_file)
-        fclose(vstats_file);
-    av_free(vstats_filename);
-
-    av_free(opt_names);
-
-    av_free(video_codec_name);
-    av_free(audio_codec_name);
-    av_free(subtitle_codec_name);
-
-    av_free(video_standard);
-
-#ifdef CONFIG_POWERPC_PERF
-    extern void powerpc_display_perf_report(void);
-    powerpc_display_perf_report();
-#endif /* CONFIG_POWERPC_PERF */
-
-    if (received_sigterm) {
-        fprintf(stderr,
-            "Received signal %d: terminating.\n",
-            (int) received_sigterm);
-        exit (255);
-    }
-
-    exit(0); /* not all OS-es handle main() return value */
-    return 0;
-}
-
 int main(int argc, char **argv)
 {
     int i;
@@ -3923,7 +3777,7 @@ int main(int argc, char **argv)
     show_banner(program_name, program_birth_year);
     if (argc <= 1) {
         show_help();
-        exit(1);
+        av_exit(1);
     }
 
     /* parse options */
@@ -3932,12 +3786,12 @@ int main(int argc, char **argv)
     /* file converter / grab */
     if (nb_output_files <= 0) {
         fprintf(stderr, "Must supply at least one output file\n");
-        exit(1);
+        av_exit(1);
     }
 
     if (nb_input_files == 0) {
         fprintf(stderr, "Must supply at least one input file\n");
-        exit(1);
+        av_exit(1);
     }
 
     ti = getutime();
@@ -3948,5 +3802,5 @@ int main(int argc, char **argv)
         printf("bench: utime=%0.3fs\n", ti / 1000000.0);
     }
 
-    return av_exit();
+    return av_exit(0);
 }