[FFmpeg-devel] [PATCH] Define a set_preset() function shared between ffmpeg.c and ffserver.c, factorize.

Stefano Sabatini stefano.sabatini-lala
Fri Oct 29 04:21:39 CEST 2010


---
 cmdutils.c |   51 +++++++++++++++++++++++++++++++++++++++++
 cmdutils.h |    3 ++
 ffmpeg.c   |   71 ++++++++++++++-------------------------------------------
 ffserver.c |   73 +++++++++++++++++------------------------------------------
 4 files changed, 93 insertions(+), 105 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index 49c6ad5..86fd5da 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -748,6 +748,57 @@ int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t reordered_pts, int6
     return pts;
 }
 
+int set_preset( const char *codec_name, const char *preset_name, int is_filename,
+               int (*set_preset_opt)(const char *opt, const char *arg, void *priv), void *priv)
+{
+    FILE *file = NULL;
+    char filename[1000], tmp[1000], tmp2[1000], line[1000];
+    int i, ret = 0;
+    const char *base[3] = { getenv("FFMPEG_DATADIR"), getenv("HOME"), FFMPEG_DATADIR, };
+
+    if (!is_filename) {
+        for (i = 0; i < 3 && !file; i++) {
+            if (!base[i])
+                continue;
+            snprintf(filename, sizeof(filename), "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", preset_name);
+            file = fopen(filename, "r");
+            if (!file) {
+                snprintf(filename, sizeof(filename), "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.ffmpeg", codec_name, preset_name);
+                file = fopen(filename, "r");
+            }
+        }
+    } else {
+        av_strlcpy(filename, preset_name, sizeof(filename));
+        file = fopen(filename, "r");
+    }
+
+    if (!file) {
+        fprintf(stderr, "File for preset '%s' not found\n", preset_name);
+        return AVERROR(ENOENT);
+    }
+
+    while (!feof(file)) {
+        int e = fscanf(file, "%999[^\n]\n", line) - 1;
+        if (line[0] == '#' && !e)
+            continue;
+        e |= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
+        if (e) {
+            fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
+            ret = AVERROR(EINVAL);
+            goto end;
+        }
+        if ((ret = set_preset_opt(tmp, tmp2, priv) < 0)) {
+            fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n",
+                    filename, line, tmp, tmp2);
+            goto end;
+        }
+    }
+
+end:
+    fclose(file);
+    return ret;
+}
+
 #if CONFIG_AVFILTER
 
 static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
diff --git a/cmdutils.h b/cmdutils.h
index b431b2e..fead82f 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -261,6 +261,9 @@ void init_pts_correction(PtsCorrectionContext *ctx);
  */
 int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts);
 
+int set_preset(const char *codec_name, const char *preset_name, int is_filename,
+               int (*set_preset_opt)(const char *opt, const char *arg, void *priv), void *priv);
+
 #if CONFIG_AVFILTER
 #include "libavfilter/avfilter.h"
 
diff --git a/ffmpeg.c b/ffmpeg.c
index 3c92a26..ced70a6 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -3971,64 +3971,29 @@ static int opt_bsf(const char *opt, const char *arg)
     return 0;
 }
 
-static int opt_preset(const char *opt, const char *arg)
+static int set_preset_opt(const char *opt, const char *arg, void *priv)
 {
-    FILE *f=NULL;
-    char filename[1000], tmp[1000], tmp2[1000], line[1000];
-    int i;
-    const char *base[3]= { getenv("FFMPEG_DATADIR"),
-                           getenv("HOME"),
-                           FFMPEG_DATADIR,
-                         };
-
-    if (*opt != 'f') {
-        for(i=0; i<3 && !f; i++){
-            if(!base[i])
-                continue;
-            snprintf(filename, sizeof(filename), "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", arg);
-            f= fopen(filename, "r");
-            if(!f){
-                char *codec_name= *opt == 'v' ? video_codec_name :
-                                  *opt == 'a' ? audio_codec_name :
-                                                subtitle_codec_name;
-                snprintf(filename, sizeof(filename), "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.ffmpeg", codec_name, arg);
-                f= fopen(filename, "r");
-            }
-        }
+    int ret = 0;
+
+    if (!strcmp(opt, "acodec")) {
+        opt_audio_codec(arg);
+    } else if (!strcmp(opt, "vcodec")) {
+        opt_video_codec(arg);
+    } else if (!strcmp(opt, "scodec")) {
+        opt_subtitle_codec(arg);
     } else {
-        av_strlcpy(filename, arg, sizeof(filename));
-        f= fopen(filename, "r");
-    }
-
-    if(!f){
-        fprintf(stderr, "File for preset '%s' not found\n", arg);
-        ffmpeg_exit(1);
-    }
-
-    while(!feof(f)){
-        int e= fscanf(f, "%999[^\n]\n", line) - 1;
-        if(line[0] == '#' && !e)
-            continue;
-        e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
-        if(e){
-            fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
-            ffmpeg_exit(1);
-        }
-        if(!strcmp(tmp, "acodec")){
-            opt_audio_codec(tmp2);
-        }else if(!strcmp(tmp, "vcodec")){
-            opt_video_codec(tmp2);
-        }else if(!strcmp(tmp, "scodec")){
-            opt_subtitle_codec(tmp2);
-        }else if(opt_default(tmp, tmp2) < 0){
-            fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
-            ffmpeg_exit(1);
-        }
+        ret = opt_default(opt, arg);
     }
 
-    fclose(f);
+    return ret;
+}
 
-    return 0;
+static int opt_preset(const char *opt, const char *arg)
+{
+    char *codec_name= *opt == 'v' ? video_codec_name :
+                      *opt == 'a' ? audio_codec_name :
+                                    subtitle_codec_name;
+    return set_preset(codec_name, arg, *opt == 'f', set_preset_opt, NULL);
 }
 
 static const OptionDef options[] = {
diff --git a/ffserver.c b/ffserver.c
index 63bd267..5da6b5c 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -3966,63 +3966,32 @@ static int ffserver_opt_default(const char *opt, const char *arg,
     return ret;
 }
 
-static int ffserver_opt_preset(const char *arg,
-                       AVCodecContext *avctx, int type,
-                       enum CodecID *audio_id, enum CodecID *video_id)
+struct presets_context { AVCodecContext *avctx; int type; enum CodecID *audio_id, *video_id; };
+
+static int set_preset_opt(const char *opt, const char *arg, void *priv)
 {
-    FILE *f=NULL;
-    char filename[1000], tmp[1000], tmp2[1000], line[1000];
-    int i, ret = 0;
-    const char *base[3]= { getenv("FFMPEG_DATADIR"),
-                           getenv("HOME"),
-                           FFMPEG_DATADIR,
-                         };
-
-    for(i=0; i<3 && !f; i++){
-        if(!base[i])
-            continue;
-        snprintf(filename, sizeof(filename), "%s%s/%s.ffpreset", base[i], i != 1 ? "" : "/.ffmpeg", arg);
-        f= fopen(filename, "r");
-        if(!f){
-            AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
-            if (codec) {
-                snprintf(filename, sizeof(filename), "%s%s/%s-%s.ffpreset", base[i],  i != 1 ? "" : "/.ffmpeg", codec->name, arg);
-                f= fopen(filename, "r");
-            }
-        }
-    }
+    int ret = 0;
+    struct presets_context *ctx = priv;
 
-    if(!f){
-        fprintf(stderr, "File for preset '%s' not found\n", arg);
-        return 1;
+    if (!strcmp(opt, "acodec")){
+        *ctx->audio_id = opt_audio_codec(arg);
+    } else if (!strcmp(opt, "vcodec")){
+        *ctx->video_id = opt_video_codec(arg);
+    } else {
+        ret = ffserver_opt_default(opt, arg, ctx->avctx, ctx->type);
     }
 
-    while(!feof(f)){
-        int e= fscanf(f, "%999[^\n]\n", line) - 1;
-        if(line[0] == '#' && !e)
-            continue;
-        e|= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
-        if(e){
-            fprintf(stderr, "%s: Invalid syntax: '%s'\n", filename, line);
-            ret = 1;
-            break;
-        }
-        if(!strcmp(tmp, "acodec")){
-            *audio_id = opt_audio_codec(tmp2);
-        }else if(!strcmp(tmp, "vcodec")){
-            *video_id = opt_video_codec(tmp2);
-        }else if(!strcmp(tmp, "scodec")){
-            /* opt_subtitle_codec(tmp2); */
-        }else if(ffserver_opt_default(tmp, tmp2, avctx, type) < 0){
-            fprintf(stderr, "%s: Invalid option or argument: '%s', parsed as '%s' = '%s'\n", filename, line, tmp, tmp2);
-            ret = 1;
-            break;
-        }
-    }
+    return ret;
+}
 
-    fclose(f);
+static int ffserver_opt_preset(const char *arg,
+                               AVCodecContext *avctx, int type,
+                               enum CodecID *audio_id, enum CodecID *video_id)
+{
+    AVCodec *codec = avcodec_find_encoder(avctx->codec_id);
+    struct presets_context ctx = { avctx, type, audio_id, video_id };
 
-    return ret;
+    return set_preset(codec->name, arg, 0, set_preset_opt, &ctx);
 }
 
 static AVOutputFormat *ffserver_guess_format(const char *short_name, const char *filename,
-- 
1.7.1




More information about the ffmpeg-devel mailing list