Browse code

Implement cmdutils.c:read_file(), and use it in ffmpeg.c for reading the second pass encoding log file.

Originally committed as revision 22769 to svn://svn.ffmpeg.org/ffmpeg/trunk

Stefano Sabatini authored on 2010/04/02 07:34:22
Showing 3 changed files
... ...
@@ -639,3 +639,27 @@ int read_yesno(void)
639 639
 
640 640
     return yesno;
641 641
 }
642
+
643
+int read_file(const char *filename, char **bufptr, size_t *size)
644
+{
645
+    FILE *f = fopen(filename, "r");
646
+
647
+    if (!f) {
648
+        fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
649
+        return AVERROR(errno);
650
+    }
651
+    fseek(f, 0, SEEK_END);
652
+    *size = ftell(f);
653
+    fseek(f, 0, SEEK_SET);
654
+    *bufptr = av_malloc(*size + 1);
655
+    if (!*bufptr) {
656
+        fprintf(stderr, "Could not allocate file buffer\n");
657
+        fclose(f);
658
+        return AVERROR(ENOMEM);
659
+    }
660
+    fread(*bufptr, 1, *size, f);
661
+    (*bufptr)[*size++] = '\0';
662
+
663
+    fclose(f);
664
+    return 0;
665
+}
... ...
@@ -200,4 +200,15 @@ void show_pix_fmts(void);
200 200
  */
201 201
 int read_yesno(void);
202 202
 
203
+/**
204
+ * Reads the file with name filename, and puts its content in a newly
205
+ * allocated 0-terminated buffer.
206
+ *
207
+ * @param bufptr puts here the pointer to the newly allocated buffer
208
+ * @param size puts here the size of the newly allocated buffer
209
+ * @return 0 in case of success, a negative value corresponding to an
210
+ * AVERROR error code in case of failure.
211
+ */
212
+int read_file(const char *filename, char **bufptr, size_t *size);
213
+
203 214
 #endif /* FFMPEG_CMDUTILS_H */
... ...
@@ -2072,8 +2072,6 @@ static int av_encode(AVFormatContext **output_files,
2072 2072
                 (codec->flags & (CODEC_FLAG_PASS1 | CODEC_FLAG_PASS2))) {
2073 2073
                 char logfilename[1024];
2074 2074
                 FILE *f;
2075
-                int size;
2076
-                char *logbuffer;
2077 2075
 
2078 2076
                 snprintf(logfilename, sizeof(logfilename), "%s-%d.log",
2079 2077
                          pass_logfilename_prefix ? pass_logfilename_prefix : DEFAULT_PASS_LOGFILENAME_PREFIX,
... ...
@@ -2086,23 +2084,12 @@ static int av_encode(AVFormatContext **output_files,
2086 2086
                     }
2087 2087
                     ost->logfile = f;
2088 2088
                 } else {
2089
-                    /* read the log file */
2090
-                    f = fopen(logfilename, "r");
2091
-                    if (!f) {
2092
-                        fprintf(stderr, "Cannot read log file '%s' for pass-2 encoding: %s\n", logfilename, strerror(errno));
2093
-                        av_exit(1);
2094
-                    }
2095
-                    fseek(f, 0, SEEK_END);
2096
-                    size = ftell(f);
2097
-                    fseek(f, 0, SEEK_SET);
2098
-                    logbuffer = av_malloc(size + 1);
2099
-                    if (!logbuffer) {
2100
-                        fprintf(stderr, "Could not allocate log buffer\n");
2089
+                    char  *logbuffer;
2090
+                    size_t logbuffer_size;
2091
+                    if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
2092
+                        fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
2101 2093
                         av_exit(1);
2102 2094
                     }
2103
-                    size = fread(logbuffer, 1, size, f);
2104
-                    fclose(f);
2105
-                    logbuffer[size] = '\0';
2106 2095
                     codec->stats_in = logbuffer;
2107 2096
                 }
2108 2097
             }