[PATCH 1/3] Implement cmdutils.c:read_file(), and use it for reading the second pass encoding log file.

Stefano Sabatini stefano.sabatini-lala
Sun Mar 28 16:38:42 CEST 2010


---
 cmdutils.c |   23 +++++++++++++++++++++++
 cmdutils.h |   11 +++++++++++
 ffmpeg.c   |   16 ++--------------
 3 files changed, 36 insertions(+), 14 deletions(-)

diff --git a/cmdutils.c b/cmdutils.c
index 1ffe2e8..f561316 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -639,3 +639,26 @@ int read_yesno(void)
 
     return yesno;
 }
+
+int read_file(const char *filename, char **bufptr, int *size)
+{
+    FILE *f = fopen(filename, "r");
+
+    if (!f) {
+        fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
+        return AVERROR(errno);
+    }
+    fseek(f, 0, SEEK_END);
+    *size = ftell(f) + 1;
+    fseek(f, 0, SEEK_SET);
+    *bufptr = av_malloc(*size);
+    if (!*bufptr) {
+        fprintf(stderr, "Could not allocate file buffer\n");
+        return AVERROR(ENOMEM);
+    }
+    fread(*bufptr, 1, *size-1, f);
+    fclose(f);
+    (*bufptr)[*size-1] = '\0';
+
+    return 0;
+}
diff --git a/cmdutils.h b/cmdutils.h
index 9190a81..6d12a97 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -200,4 +200,15 @@ void show_pix_fmts(void);
  */
 int read_yesno(void);
 
+/**
+ * Reads the file with name filename, and puts its content in a newly
+ * allocated 0-terminated buffer.
+ *
+ * @param bufptr puts here the pointer to the newly allocated buffer
+ * @param size puts here the size of the newly allocated buffer
+ * @return 0 in case of success, a negative value corresponding to an
+ * AVERROR error code in case of failure.
+ */
+int read_file(const char *filename, char **bufptr, int *size);
+
 #endif /* FFMPEG_CMDUTILS_H */
diff --git a/ffmpeg.c b/ffmpeg.c
index 951b0da..c8afb73 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -2048,22 +2048,10 @@ static int av_encode(AVFormatContext **output_files,
                     ost->logfile = f;
                 } else {
                     /* read the log file */
-                    f = fopen(logfilename, "r");
-                    if (!f) {
-                        fprintf(stderr, "Cannot read log file '%s' for pass-2 encoding: %s\n", logfilename, strerror(errno));
-                        av_exit(1);
-                    }
-                    fseek(f, 0, SEEK_END);
-                    size = ftell(f);
-                    fseek(f, 0, SEEK_SET);
-                    logbuffer = av_malloc(size + 1);
-                    if (!logbuffer) {
-                        fprintf(stderr, "Could not allocate log buffer\n");
+                    if (read_file(logfilename, &logbuffer, &size) < 0) {
+                        fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
                         av_exit(1);
                     }
-                    size = fread(logbuffer, 1, size, f);
-                    fclose(f);
-                    logbuffer[size] = '\0';
                     codec->stats_in = logbuffer;
                 }
             }
-- 
1.7.0


--vGgW1X5XWziG23Ko--



More information about the ffmpeg-devel mailing list