[FFmpeg-devel] [PATCH] Split huge transcode() function into transcode_init(), transcode(), and transcode_deinit().

Stefano Sabatini stefano.sabatini-lala
Sat Oct 16 13:22:09 CEST 2010


---
 ffmpeg.c |  104 ++++++++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 68 insertions(+), 36 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 9513c7f..fbc8415 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1935,30 +1935,31 @@ static int copy_chapters(int infile, int outfile)
     return 0;
 }
 
+static int nb_istreams = 0, nb_ostreams = 0;
+static AVInputStream **ist_table = NULL;
+static AVOutputStream **ost_table = NULL;
+static AVInputFile *file_table;
+
 /*
  * The following code is the main loop of the file converter
  */
-static int transcode(AVFormatContext **output_files,
+static int transcode_init(AVFormatContext **output_files,
                      int nb_output_files,
                      AVFormatContext **input_files,
                      int nb_input_files,
                      AVStreamMap *stream_maps, int nb_stream_maps)
 {
-    int ret = 0, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
+    int ret = 0, i, j, k, n;
     AVFormatContext *is, *os;
     AVCodecContext *codec, *icodec;
-    AVOutputStream *ost, **ost_table = NULL;
-    AVInputStream *ist, **ist_table = NULL;
-    AVInputFile *file_table;
+    AVOutputStream *ost;
+    AVInputStream *ist;
     char error[1024];
-    int key;
     int want_sdp = 1;
-    uint8_t no_packet[MAX_FILES]={0};
-    int no_packet_count=0;
 
     file_table= av_mallocz(nb_input_files * sizeof(AVInputFile));
     if (!file_table)
-        goto fail;
+        return AVERROR(ENOMEM);
 
     /* input stream init */
     j = 0;
@@ -1972,12 +1973,12 @@ static int transcode(AVFormatContext **output_files,
 
     ist_table = av_mallocz(nb_istreams * sizeof(AVInputStream *));
     if (!ist_table)
-        goto fail;
+        return AVERROR(ENOMEM);
 
     for(i=0;i<nb_istreams;i++) {
         ist = av_mallocz(sizeof(AVInputStream));
         if (!ist)
-            goto fail;
+            return AVERROR(ENOMEM);
         ist_table[i] = ist;
     }
     j = 0;
@@ -2004,15 +2005,13 @@ static int transcode(AVFormatContext **output_files,
         if (!os->nb_streams) {
             dump_format(output_files[i], i, output_files[i]->filename, 1);
             fprintf(stderr, "Output file #%d does not contain any stream\n", i);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
         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");
-        ret = AVERROR(EINVAL);
-        goto fail;
+        return AVERROR(EINVAL);
     }
 
     /* Sanity check the mapping args -- do the input files & streams exist? */
@@ -2023,22 +2022,20 @@ static int transcode(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);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
         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);
-            ret = AVERROR(EINVAL);
-            goto fail;
+            return AVERROR(EINVAL);
         }
     }
 
     ost_table = av_mallocz(sizeof(AVOutputStream *) * nb_ostreams);
     if (!ost_table)
-        goto fail;
+        return AVERROR(ENOMEM);
     n = 0;
     for(k=0;k<nb_output_files;k++) {
         os = output_files[k];
@@ -2141,7 +2138,7 @@ static int transcode(AVFormatContext **output_files,
             uint64_t extra_size = (uint64_t)icodec->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE;
 
             if (extra_size > INT_MAX)
-                goto fail;
+                return AVERROR(EINVAL);
 
             /* if stream_copy is selected, no need to decode or encode */
             codec->codec_id = icodec->codec_id;
@@ -2159,7 +2156,7 @@ static int transcode(AVFormatContext **output_files,
             codec->rc_buffer_size = icodec->rc_buffer_size;
             codec->extradata= av_mallocz(extra_size);
             if (!codec->extradata)
-                goto fail;
+                return AVERROR(ENOMEM);
             memcpy(codec->extradata, icodec->extradata, icodec->extradata_size);
             codec->extradata_size= icodec->extradata_size;
             if(av_q2d(icodec->time_base)*icodec->ticks_per_frame > av_q2d(ist->st->time_base) && av_q2d(ist->st->time_base) < 1.0/1000){
@@ -2203,7 +2200,7 @@ static int transcode(AVFormatContext **output_files,
             case AVMEDIA_TYPE_AUDIO:
                 ost->fifo= av_fifo_alloc(1024);
                 if(!ost->fifo)
-                    goto fail;
+                    return AVERROR(ENOMEM);
                 ost->reformat_pair = MAKE_SFMT_PAIR(SAMPLE_FMT_NONE,SAMPLE_FMT_NONE);
                 ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
                 icodec->request_channels = codec->channels;
@@ -2313,8 +2310,7 @@ static int transcode(AVFormatContext **output_files,
     if (!bit_buffer) {
         fprintf(stderr, "Cannot allocate %d bytes output buffer\n",
                 bit_buffer_size);
-        ret = AVERROR(ENOMEM);
-        goto fail;
+        return AVERROR(ENOMEM);
     }
 
     /* open each encoder */
@@ -2457,13 +2453,31 @@ static int transcode(AVFormatContext **output_files,
 
     if (ret) {
         fprintf(stderr, "%s\n", error);
-        goto fail;
     }
 
     if (want_sdp) {
         print_sdp(output_files, nb_output_files);
     }
 
+    return 0;
+}
+
+/*
+ * The following code is the main loop of the file converter
+ */
+static int transcode(AVFormatContext **output_files,
+                     int nb_output_files,
+                     AVFormatContext **input_files,
+                     int nb_input_files,
+                     AVStreamMap *stream_maps, int nb_stream_maps)
+{
+    AVFormatContext *is, *os;
+    AVOutputStream *ost;
+    AVInputStream *ist;
+    int key, i, ret;
+    uint8_t no_packet[MAX_FILES]={0};
+    int no_packet_count=0;
+
     if (!using_stdin && verbose >= 0) {
         fprintf(stderr, "Press [q] to stop encoding\n");
         url_set_interrupt_cb(decode_interrupt_cb);
@@ -2600,13 +2614,12 @@ static int transcode(AVFormatContext **output_files,
         }
 
         //fprintf(stderr,"read #%d.%d size=%d\n", ist->file_index, ist->index, pkt.size);
-        if (output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt) < 0) {
-
+        if ((ret = output_packet(ist, ist_index, ost_table, nb_ostreams, &pkt)) < 0) {
             if (verbose >= 0)
                 fprintf(stderr, "Error while decoding stream #%d.%d\n",
                         ist->file_index, ist->index);
             if (exit_on_error)
-                ffmpeg_exit(1);
+                return ret;
             av_free_packet(&pkt);
             goto redo;
         }
@@ -2637,6 +2650,19 @@ static int transcode(AVFormatContext **output_files,
     /* dump report by using the first video and audio streams */
     print_report(output_files, ost_table, nb_ostreams, 1);
 
+    return 0;
+}
+
+static void transcode_deinit(AVFormatContext **output_files,
+                             int nb_output_files,
+                             AVFormatContext **input_files,
+                             int nb_input_files,
+                             AVStreamMap *stream_maps, int nb_stream_maps)
+{
+    int i;
+    AVOutputStream *ost;
+    AVInputStream *ist;
+
     /* close each encoder */
     for(i=0;i<nb_ostreams;i++) {
         ost = ost_table[i];
@@ -2660,10 +2686,6 @@ static int transcode(AVFormatContext **output_files,
     }
 #endif
 
-    /* finished ! */
-    ret = 0;
-
- fail:
     av_freep(&bit_buffer);
     av_free(file_table);
 
@@ -2698,7 +2720,6 @@ static int transcode(AVFormatContext **output_files,
         }
         av_free(ost_table);
     }
-    return ret;
 }
 
 static void opt_format(const char *arg)
@@ -4322,6 +4343,7 @@ static const OptionDef options[] = {
 int main(int argc, char **argv)
 {
     int64_t ti;
+    int ret;
 
     av_log_set_flags(AV_LOG_SKIP_REPEATED);
 
@@ -4364,9 +4386,19 @@ int main(int argc, char **argv)
     }
 
     ti = getutime();
-    if (transcode(output_files, nb_output_files, input_files, nb_input_files,
-                  stream_maps, nb_stream_maps) < 0)
+
+    ret = transcode_init(output_files, nb_output_files, input_files, nb_input_files,
+                         stream_maps, nb_stream_maps);
+
+    if (ret >= 0)
+        ret = transcode(output_files, nb_output_files, input_files, nb_input_files,
+                        stream_maps, nb_stream_maps);
+
+    transcode_deinit(output_files, nb_output_files, input_files, nb_input_files,
+                     stream_maps, nb_stream_maps);
+    if (ret < 0)
         ffmpeg_exit(1);
+
     ti = getutime() - ti;
     if (do_benchmark) {
         int maxrss = getmaxrss() / 1024;
-- 
1.7.1




More information about the ffmpeg-devel mailing list