[FFmpeg-devel] [PATCH 1/3] Implement parse_preset_line() and use it in ffmpeg.c and ffserver.c.

Stefano Sabatini stefano.sabatini-lala
Sat Nov 6 15:02:39 CET 2010


---
 cmdutils.c |   24 ++++++++++++++++++++++++
 cmdutils.h |   15 +++++++++++++++
 ffmpeg.c   |   14 ++++----------
 ffserver.c |   12 ++----------
 4 files changed, 45 insertions(+), 20 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index 681ed42..40a77b1 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -778,6 +778,30 @@ FILE *get_preset_file(char *filename, size_t filename_size,
     return f;
 }
 
+int parse_preset_line(FILE *preset_file, char *preset_filename,
+                      char *line, int line_size,
+                      char *key, int key_size, char *val, int val_size)
+{
+    char tmp[1000], tmp2[1000];
+
+    while (!feof(preset_file)) {
+        int e = fscanf(preset_file, "%999[^\n]\n", tmp) - 1;
+        if (line[0] == '#' && !e)
+            continue;
+        snprintf(line, line_size, "%s", tmp);
+        e |= sscanf(line, "%999[^=]=%999[^\n]\n", tmp, tmp2) - 2;
+        snprintf(key, key_size, "%s", tmp);
+        snprintf(val, val_size, "%s", tmp2);
+        if (e) {
+            fprintf(stderr, "%s: Invalid syntax: '%s'\n", preset_filename, line);
+            return AVERROR(EINVAL);
+        }
+        return 0;
+    }
+
+    return AVERROR(EOF);
+}
+
 #if CONFIG_AVFILTER
 
 static int ffsink_init(AVFilterContext *ctx, const char *args, void *opaque)
diff --git a/cmdutils.h b/cmdutils.h
index 9fb7845..2a9bc3c 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -281,6 +281,21 @@ int64_t guess_correct_pts(PtsCorrectionContext *ctx, int64_t pts, int64_t dts);
 FILE *get_preset_file(char *filename, size_t filename_size,
                       const char *preset_name, int is_path, const char *codec_name);
 
+/**
+ * Parse line from preset_file of the form "KEY=VALUE" into key and
+ * value string.
+ * Lines starting with '#' are considered comment, and skipped.
+ *
+ * @param line buffer where to put the parsed line, with size given by line_size
+ * @param key buffer where to put the parsed key, with size given by key_size
+ * @param val buffer where to put the parsed value, with size given by val_size
+ * @return 0 in case of successfull parsing, AVERROR(EINVAL) in case
+ * of parsing error or AVERROR(EOF) when the file is terminated
+ */
+int parse_preset_line(FILE *preset_file, char *preset_filename,
+                      char *line, int line_size,
+                      char *key, int key_size, char *val, int val_size);
+
 #if CONFIG_AVFILTER
 #include "libavfilter/avfilter.h"
 
diff --git a/ffmpeg.c b/ffmpeg.c
index 8b1878b..1cd6260 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -4029,6 +4029,7 @@ static int opt_bsf(const char *opt, const char *arg)
 static int opt_preset(const char *opt, const char *arg)
 {
     FILE *f=NULL;
+    int ret;
     char filename[1000], tmp[1000], tmp2[1000], line[1000];
     char *codec_name = *opt == 'v' ? video_codec_name :
                        *opt == 'a' ? audio_codec_name :
@@ -4039,15 +4040,8 @@ static int opt_preset(const char *opt, const char *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);
-        }
+    while (!(ret = parse_preset_line(f, filename, line, sizeof(line),
+                                     tmp, sizeof(tmp), tmp2, sizeof(tmp2)))) {
         if(!strcmp(tmp, "acodec")){
             opt_audio_codec(tmp2);
         }else if(!strcmp(tmp, "vcodec")){
@@ -4062,7 +4056,7 @@ static int opt_preset(const char *opt, const char *arg)
 
     fclose(f);
 
-    return 0;
+    return ret;
 }
 
 static const OptionDef options[] = {
diff --git a/ffserver.c b/ffserver.c
index fcc3359..602d8e3 100644
--- a/ffserver.c
+++ b/ffserver.c
@@ -3981,16 +3981,8 @@ static int ffserver_opt_preset(const char *arg,
         return 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);
-            ret = 1;
-            break;
-        }
+    while (!(ret = parse_preset_line(f, filename, line, sizeof(line),
+                                     tmp, sizeof(tmp), tmp2, sizeof(tmp2)))) {
         if(!strcmp(tmp, "acodec")){
             *audio_id = opt_audio_codec(tmp2);
         }else if(!strcmp(tmp, "vcodec")){
-- 
1.7.1




More information about the ffmpeg-devel mailing list