]> git.sesse.net Git - ffmpeg/blobdiff - ffmpeg.c
do not fail if correct is not recognized
[ffmpeg] / ffmpeg.c
index 65cc3f4c3f6c6c00715c08fc556072620d7be10f..8d77742df1911ad4244abbcc51328a2002e0f6e5 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
 #undef NDEBUG
 #include <assert.h>
 
-#if !defined(INFINITY) && defined(HUGE_VAL)
-#define INFINITY HUGE_VAL
-#endif
-
 #undef exit
 
 static const char program_name[] = "FFmpeg";
@@ -82,7 +80,7 @@ typedef struct AVMetaDataMap {
     int in_file;
 } AVMetaDataMap;
 
-extern const OptionDef options[];
+static const OptionDef options[];
 
 #define MAX_FILES 20
 
@@ -210,7 +208,7 @@ static uint64_t limit_filesize = 0; //
 static int pgmyuv_compatibility_hack=0;
 static float dts_delta_threshold = 10;
 
-static int sws_flags = SWS_BICUBIC;
+static unsigned int sws_flags = SWS_BICUBIC;
 
 static const char **opt_names;
 static int opt_name_count;
@@ -375,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;
@@ -433,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);
     }
 }
 
@@ -471,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);
         }
     }
 
@@ -536,6 +587,7 @@ static void do_audio_out(AVFormatContext *s,
     /* now encode as many frames as possible */
     if (enc->frame_size > 1) {
         /* output resampled raw samples */
+        av_fifo_realloc(&ost->fifo, av_fifo_size(&ost->fifo) + size_out + 1);
         av_fifo_write(&ost->fifo, buftmp, size_out);
 
         frame_bytes = enc->frame_size * 2 * enc->channels;
@@ -544,6 +596,8 @@ static void do_audio_out(AVFormatContext *s,
             AVPacket pkt;
             av_init_packet(&pkt);
 
+            //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
+
             ret = avcodec_encode_audio(enc, audio_out, audio_out_size,
                                        (short *)audio_buf);
             audio_size += ret;
@@ -588,6 +642,7 @@ static void do_audio_out(AVFormatContext *s,
             size_out = size_out >> 1;
             break;
         }
+        //FIXME pass ost->sync_opts as AVFrame.pts in avcodec_encode_audio()
         ret = avcodec_encode_audio(enc, audio_out, size_out,
                                    (short *)buftmp);
         audio_size += ret;
@@ -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){
@@ -872,7 +927,6 @@ static void do_video_out(AVFormatContext *s,
 }
 
 static double psnr(double d){
-    if(d==0) return INFINITY;
     return -10.0*log(d)/log(10.0);
 }
 
@@ -888,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);
         }
     }
 
@@ -1055,13 +1109,8 @@ static int output_packet(AVInputStream *ist, int ist_index,
     AVSubtitle subtitle, *subtitle_to_free;
     int got_subtitle;
 
-    if(!pkt){
-        ist->pts= ist->next_pts; // needed for last packet if vsync=0
-    } else if (pkt->dts != AV_NOPTS_VALUE) { //FIXME seems redundant, as libavformat does this too
-        ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
-    } else {
-//        assert(ist->pts == ist->next_pts);
-    }
+    if(ist->next_pts == AV_NOPTS_VALUE)
+        ist->next_pts= ist->pts;
 
     if (pkt == NULL) {
         /* EOF handling */
@@ -1070,10 +1119,18 @@ static int output_packet(AVInputStream *ist, int ist_index,
         goto handle_eof;
     }
 
+    if(pkt->dts != AV_NOPTS_VALUE)
+        ist->next_pts = ist->pts = av_rescale_q(pkt->dts, ist->st->time_base, AV_TIME_BASE_Q);
+
     len = pkt->size;
     ptr = pkt->data;
     while (len > 0) {
     handle_eof:
+        ist->pts= ist->next_pts;
+
+        if(len && len != pkt->size && verbose>0)
+            fprintf(stderr, "Multiple frames in a packet from stream %d\n", pkt->stream_index);
+
         /* decode the packet if needed */
         data_buf = NULL; /* fail safe */
         data_size = 0;
@@ -1470,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? */
@@ -1487,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);
         }
     }
 
@@ -1526,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 {
@@ -1565,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);
                     }
                 }
             }
@@ -1590,6 +1647,8 @@ static int av_encode(AVFormatContext **output_files,
             av_strlcpy(ost->st->language, ist->st->language,
                        sizeof(ost->st->language));
 
+        ost->st->disposition = ist->st->disposition;
+
         if (ost->st->stream_copy) {
             /* if stream_copy is selected, no need to decode or encode */
             codec->codec_id = icodec->codec_id;
@@ -1621,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;
@@ -1636,7 +1695,7 @@ static int av_encode(AVFormatContext **output_files,
         } else {
             switch(codec->codec_type) {
             case CODEC_TYPE_AUDIO:
-                if (av_fifo_init(&ost->fifo, 2 * MAX_AUDIO_PACKET_SIZE))
+                if (av_fifo_init(&ost->fifo, 1024))
                     goto fail;
                 ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
                 icodec->request_channels = codec->channels;
@@ -1674,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(
@@ -1687,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);
                 }
@@ -1717,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 {
@@ -1725,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);
@@ -1733,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);
@@ -1786,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;
         }
@@ -1806,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;
@@ -1823,10 +1882,7 @@ static int av_encode(AVFormatContext **output_files,
         ist = ist_table[i];
         is = input_files[ist->file_index];
         ist->pts = 0;
-        ist->next_pts=0;
-        if(   input_files_ts_offset[ist->file_index] != -is->start_time
-           && !(is->start_time == AV_NOPTS_VALUE && input_files_ts_offset[ist->file_index]==0))
-            ist->next_pts= AV_NOPTS_VALUE;
+        ist->next_pts = AV_NOPTS_VALUE;
         ist->is_start = 1;
     }
 
@@ -2117,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);
     }
 }
 
@@ -2170,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);
-    av_log_set_level(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);
     }
 }
 
@@ -2206,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;
 }
@@ -2224,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;
 }
@@ -2242,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;
 }
@@ -2260,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;
 }
@@ -2277,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);
     }
 }
 
@@ -2322,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);
     }
 }
 
@@ -2335,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);
     }
 }
 
@@ -2349,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);
     }
 }
 
@@ -2363,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];
@@ -2387,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);
     }
 }
 
@@ -2410,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;
 }
@@ -2421,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);
     }
 }
 
@@ -2431,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);
     }
 }
 
@@ -2454,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)
@@ -2526,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
@@ -2579,35 +2639,28 @@ 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 *timestr, int is_duration)
-{
-    int64_t us = parse_date(timestr, is_duration);
-    if (us == INT64_MIN) {
-        fprintf(stderr, "Invalid %s specification: %s\n",
-                is_duration ? "duration" : "date", timestr);
-        exit(1);
-    }
-    return us;
-}
-
-static void opt_recording_time(const char *arg)
+static int opt_recording_time(const char *opt, const char *arg)
 {
-    recording_time = parse_time_or_die(arg, 1);
+    recording_time = parse_time_or_die(opt, arg, 1);
+    return 0;
 }
 
-static void opt_start_time(const char *arg)
+static int opt_start_time(const char *opt, const char *arg)
 {
-    start_time = parse_time_or_die(arg, 1);
+    start_time = parse_time_or_die(opt, arg, 1);
+    return 0;
 }
 
-static void opt_rec_timestamp(const char *arg)
+static int opt_rec_timestamp(const char *opt, const char *arg)
 {
-    rec_timestamp = parse_time_or_die(arg, 0) / 1000000;
+    rec_timestamp = parse_time_or_die(opt, arg, 0) / 1000000;
+    return 0;
 }
 
-static void opt_input_ts_offset(const char *arg)
+static int opt_input_ts_offset(const char *opt, const char *arg)
 {
-    input_ts_offset = parse_time_or_die(arg, 1);
+    input_ts_offset = parse_time_or_die(opt, arg, 1);
+    return 0;
 }
 
 static enum CodecID find_codec_or_die(const char *name, int type, int encoder)
@@ -2622,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;
 }
@@ -2679,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;
@@ -2695,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;
@@ -2850,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;
@@ -2952,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,
@@ -3005,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);
 
@@ -3074,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);
 
@@ -3112,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);
@@ -3123,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);
@@ -3134,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);
@@ -3157,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);
         }
     }
 
@@ -3170,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;
@@ -3235,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);
         }
     }
 
@@ -3254,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);
                                 }
             }
         }
@@ -3267,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);
         }
     }
 
@@ -3275,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);
@@ -3302,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;
 }
@@ -3413,14 +3466,15 @@ static void opt_show_formats(void)
             break;
         }
         printf(
-            " %s%s%s%s%s%s %s",
+            " %s%s%s%s%s%s %-15s %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);
+            p2->name,
+            p2->long_name ? p2->long_name : "");
        /* if(p2->decoder && decode==0)
             printf(" use %s for decoding", p2->decoder->name);*/
         printf("\n");
@@ -3445,7 +3499,7 @@ static void opt_show_formats(void)
 "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);
+    av_exit(0);
 }
 
 static void parse_matrix_coeffs(uint16_t *dest, const char *str)
@@ -3459,7 +3513,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++;
     }
@@ -3523,7 +3577,7 @@ static void show_help(void)
 static void opt_show_help(void)
 {
     show_help();
-    exit(0);
+    av_exit(0);
 }
 
 static void opt_target(const char *arg)
@@ -3579,7 +3633,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")) {
@@ -3667,7 +3721,7 @@ static void opt_target(const char *arg)
 
     } else {
         fprintf(stderr, "Unknown target: %s\n", arg);
-        exit(1);
+        av_exit(1);
     }
 }
 
@@ -3695,7 +3749,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 :
@@ -3712,16 +3766,16 @@ static int opt_bsf(const char *opt, const char *arg)
 static void opt_show_license(void)
 {
     show_license();
-    exit(0);
+    av_exit(0);
 }
 
 static void opt_show_version(void)
 {
     show_version(program_name);
-    exit(0);
+    av_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" },
@@ -3732,12 +3786,12 @@ const OptionDef options[] = {
     { "y", OPT_BOOL, {(void*)&file_overwrite}, "overwrite output files" },
     { "map", HAS_ARG | OPT_EXPERT, {(void*)opt_map}, "set input stream mapping", "file:stream[:syncfile:syncstream]" },
     { "map_meta_data", HAS_ARG | OPT_EXPERT, {(void*)opt_map_meta_data}, "set meta data information of outfile from infile", "outfile:infile" },
-    { "t", HAS_ARG, {(void*)opt_recording_time}, "record or transcode \"duration\" seconds of audio/video", "duration" },
+    { "t", OPT_FUNC2 | HAS_ARG, {(void*)opt_recording_time}, "record or transcode \"duration\" seconds of audio/video", "duration" },
     { "fs", HAS_ARG | OPT_INT64, {(void*)&limit_filesize}, "set the limit file size in bytes", "limit_size" }, //
-    { "ss", HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
-    { "itsoffset", HAS_ARG, {(void*)opt_input_ts_offset}, "set the input ts offset", "time_off" },
+    { "ss", OPT_FUNC2 | HAS_ARG, {(void*)opt_start_time}, "set the start time offset", "time_off" },
+    { "itsoffset", OPT_FUNC2 | HAS_ARG, {(void*)opt_input_ts_offset}, "set the input ts offset", "time_off" },
     { "title", HAS_ARG | OPT_STRING, {(void*)&str_title}, "set the title", "string" },
-    { "timestamp", HAS_ARG, {(void*)&opt_rec_timestamp}, "set the timestamp", "time" },
+    { "timestamp", OPT_FUNC2 | HAS_ARG, {(void*)&opt_rec_timestamp}, "set the timestamp", "time" },
     { "author", HAS_ARG | OPT_STRING, {(void*)&str_author}, "set the author", "string" },
     { "copyright", HAS_ARG | OPT_STRING, {(void*)&str_copyright}, "set the copyright", "string" },
     { "comment", HAS_ARG | OPT_STRING, {(void*)&str_comment}, "set the comment", "string" },
@@ -3752,21 +3806,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}, "control amount of logging", "verbose" },
+    { "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" },
@@ -3789,7 +3843,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)" },
@@ -3812,11 +3866,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" },
@@ -3847,59 +3901,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;
@@ -3918,7 +3919,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 */
@@ -3927,12 +3928,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();
@@ -3943,5 +3944,5 @@ int main(int argc, char **argv)
         printf("bench: utime=%0.3fs\n", ti / 1000000.0);
     }
 
-    return av_exit();
+    return av_exit(0);
 }