[Ffmpeg-devel] [PATCH] split av_encode function and do related reorder

Limin Wang lance.lmwang
Mon Mar 5 03:38:43 CET 2007


Hi,

* Michael Niedermayer <michaelni at gmx.at> [2007-03-02 13:28:56 +0100]:

> Hi
> 
> well, then use fail instead, your patch used fail1 for that i just fixed the
> infinite loop i didnt check that the labels where correct, if they where not
> than of course it has to be corrected too instead of renaming all in the
> function

fixed, some place should go to fail1, will fix it after the patch is ok.


> trailing whitespace isnt allowed in svn

fixed, forget to delete-trailing-whitespace by emacs.
 
> this code chunk has been reordered why? either way it doesnt belong into this
> patch

OK, in  order to keep it untouched, I modify the ffmpeg.c directly now.

> this is not identical, it contains changes to comments and whitespace
> at least

should be fixed, I copy and paste directly.

 
> also i dont think the closing code from main() even belongs into
> av_encode_close() freeing things allocated during command line
> parsing is not av_encode_close() job it wasnt alocated in av_encode_main() or
> _init()

yes,  add av_exit() to do the other close job. Please review it again, now
it'll more readable than before.


Thanks,
Limin
-------------- next part --------------
Index: ffmpeg.c
===================================================================
--- ffmpeg.c	(revision 8239)
+++ ffmpeg.c	(working copy)
@@ -276,6 +276,12 @@
     int nb_streams;       /* nb streams we are aware of */
 } AVInputFile;
 
+static AVOutputStream **ost_table = NULL;
+static AVInputStream **ist_table = NULL;
+static AVInputFile *file_table = NULL;
+static int nb_istreams = 0;
+static int nb_ostreams = 0;
+
 #ifndef __MINGW32__
 
 /* init terminal so that we can grab keys */
@@ -1338,6 +1344,9 @@
     return -1;
 }
 
+static int av_encode_init();
+static int av_encode_main();
+static int av_encode_close();
 
 /*
  * The following code is the main loop of the file converter
@@ -1348,15 +1357,34 @@
                      int nb_input_files,
                      AVStreamMap *stream_maps, int nb_stream_maps)
 {
-    int ret, i, j, k, n, nb_istreams = 0, nb_ostreams = 0;
-    AVFormatContext *is, *os;
-    AVCodecContext *codec, *icodec;
-    AVOutputStream *ost, **ost_table = NULL;
-    AVInputStream *ist, **ist_table = NULL;
-    AVInputFile *file_table;
-    AVFormatContext *stream_no_data;
-    int key;
+    int ret;
 
+    ret = av_encode_init();
+    if( ret < 0 ) {
+        fprintf(stderr, "av_encode_init failed\n");
+        exit(1);
+    }
+
+    av_encode_main();
+
+    av_encode_close();
+
+    return 0;
+}
+
+/*
+ * The following code initialize encode
+ */
+static int av_encode_init()
+{
+    int ret=0, i, j, k, n;
+    AVOutputStream *ost;
+    AVInputStream *ist;
+    AVFormatContext *is;
+    AVFormatContext *os;
+    AVCodecContext *codec;
+    AVCodecContext *icodec;
+
     file_table= (AVInputFile*) av_mallocz(nb_input_files * sizeof(AVInputFile));
     if (!file_table)
         goto fail;
@@ -1819,6 +1847,29 @@
         fprintf(stderr, "Press [q] to stop encoding\n");
         url_set_interrupt_cb(decode_interrupt_cb);
     }
+
+done:
+    return ret;
+fail:
+    ret = AVERROR(ENOMEM);
+fail1:
+    av_encode_close();
+    goto done;
+}
+
+/*
+ * The following code is the main loop of the file converter
+ */
+static int av_encode_main()
+{
+    int i;
+    AVFormatContext *stream_no_data;
+    AVOutputStream *ost;
+    AVInputStream *ist;
+    AVFormatContext *is;
+    AVFormatContext *os;
+    int key;
+
     term_init();
 
     stream_no_data = 0;
@@ -1960,6 +2011,19 @@
     /* dump report by using the first video and audio streams */
     print_report(output_files, ost_table, nb_ostreams, 1);
 
+    return 0;
+}
+
+/*
+ * The following code close the av encode resource
+ */
+static int av_encode_close()
+{
+    int ret = 0;
+    int i;
+    AVOutputStream *ost;
+    AVInputStream *ist;
+
     /* close each encoder */
     for(i=0;i<nb_ostreams;i++) {
         ost = ost_table[i];
@@ -1977,10 +2041,6 @@
         }
     }
 
-    /* finished ! */
-
-    ret = 0;
- fail1:
     av_freep(&bit_buffer);
     av_free(file_table);
 
@@ -2012,9 +2072,6 @@
         av_free(ost_table);
     }
     return ret;
- fail:
-    ret = AVERROR(ENOMEM);
-    goto fail1;
 }
 
 #if 0
@@ -3760,6 +3817,47 @@
     opt_output_file(filename);
 }
 
+static int av_exit()
+{
+    /* close files */
+    for(i=0;i<nb_output_files;i++) {
+        /* maybe av_close_output_file ??? */
+        AVFormatContext *s = output_files[i];
+        int j;
+        if (!(s->oformat->flags & AVFMT_NOFILE))
+            url_fclose(&s->pb);
+        for(j=0;j<s->nb_streams;j++) {
+            av_free(s->streams[j]->codec);
+            av_free(s->streams[j]);
+        }
+        av_free(s);
+    }
+    for(i=0;i<nb_input_files;i++)
+        av_close_input_file(input_files[i]);
+
+    av_free_static();
+
+    if(intra_matrix)
+        av_free(intra_matrix);
+    if(inter_matrix)
+        av_free(inter_matrix);
+
+#ifdef CONFIG_POWERPC_PERF
+    extern void powerpc_display_perf_report(void);
+    powerpc_display_perf_report();
+#endif /* CONFIG_POWERPC_PERF */
+
+    if (received_sigterm) {
+        fprintf(stderr,
+            "Received signal %d: terminating.\n",
+            (int) received_sigterm);
+        exit (255);
+    }
+
+    exit(0); /* not all OS-es handle main() return value */
+    return 0;
+}
+
 int main(int argc, char **argv)
 {
     int i;
@@ -3798,41 +3896,7 @@
         printf("bench: utime=%0.3fs\n", ti / 1000000.0);
     }
 
-    /* close files */
-    for(i=0;i<nb_output_files;i++) {
-        /* maybe av_close_output_file ??? */
-        AVFormatContext *s = output_files[i];
-        int j;
-        if (!(s->oformat->flags & AVFMT_NOFILE))
-            url_fclose(&s->pb);
-        for(j=0;j<s->nb_streams;j++) {
-            av_free(s->streams[j]->codec);
-            av_free(s->streams[j]);
-        }
-        av_free(s);
-    }
-    for(i=0;i<nb_input_files;i++)
-        av_close_input_file(input_files[i]);
+    av_exit();
 
-    av_free_static();
-
-    if(intra_matrix)
-        av_free(intra_matrix);
-    if(inter_matrix)
-        av_free(inter_matrix);
-
-#ifdef CONFIG_POWERPC_PERF
-    extern void powerpc_display_perf_report(void);
-    powerpc_display_perf_report();
-#endif /* CONFIG_POWERPC_PERF */
-
-    if (received_sigterm) {
-        fprintf(stderr,
-            "Received signal %d: terminating.\n",
-            (int) received_sigterm);
-        exit (255);
-    }
-
-    exit(0); /* not all OS-es handle main() return value */
     return 0;
 }
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 481 bytes
Desc: not available
URL: <http://lists.mplayerhq.hu/pipermail/ffmpeg-devel/attachments/20070305/d1cbb50d/attachment.pgp>



More information about the ffmpeg-devel mailing list