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

Limin Wang lance.lmwang
Wed Feb 28 04:56:00 CET 2007


Hi,

> could you reorder the code so that  the patch size is minimized?
> like i suggested above, that is ..._close() after ..._main()

Have fixed in the new patch.
 
> 
> [...]
> >      if (!file_table)
> > -        goto fail;
> > +        goto fail_nomem;
> >  
> 
> that is nice and i would like it in svn but its a seperate issue and
> should be in a seperate patch and commit

fixed

> 
> [...]
> 
> > @@ -1458,7 +1572,8 @@
> >                      stream_maps[n-1].stream_index;
> >  
> >                  /* Sanity check that the stream types match */
> > -                if (ist_table[ost->source_index]->st->codec->codec_type != ost->st->codec->codec_type) {
> > +                if (ist_table[ost->source_index]->st->codec->codec_type
> > +                    != ost->st->codec->codec_type) {
> 
> cosmetic change
> 
> 
> [...]
> > @@ -1728,7 +1843,8 @@
> >                  exit(1);
> >              }
> >              if (avcodec_open(ost->st->codec, codec) < 0) {
> > -                fprintf(stderr, "Error while opening codec for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height\n",
> > +                fprintf(stderr, "Error while opening codec for output stream #%d.%d "
> > +                        "- maybe incorrect parameters such as bit_rate, rate, width or height\n",
> >                          ost->file_index, ost->index);
> 
> another cosmetic change, and there are more, cosmetic changes must be
> in seperate patches then functional changes
>
> [...]
> > +done:
> > +    return ret;
> > +
> > +fail1:
> > +    av_encode_close();
> > +    goto done;
> > +
> > +fail_nomem:
> > +    ret = AVERROR(ret);
> > +    goto fail1;
> > +}
fixed.

Please review the new patch again.


Thanks,
Limin
-------------- next part --------------
Index: ffmpeg.c
===================================================================
--- ffmpeg.c	(revision 8156)
+++ ffmpeg.c	(working copy)
@@ -277,6 +277,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 */
@@ -1339,24 +1345,20 @@
     return -1;
 }
 
+static int av_encode_close();
 
 /*
- * The following code is the main loop of the file converter
+ * The following code initialize encode
  */
-static int av_encode(AVFormatContext **output_files,
-                     int nb_output_files,
-                     AVFormatContext **input_files,
-                     int nb_input_files,
-                     AVStreamMap *stream_maps, int nb_stream_maps)
+static int av_encode_init()
 {
-    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=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)
@@ -1820,6 +1822,32 @@
         fprintf(stderr, "Press [q] to stop encoding\n");
         url_set_interrupt_cb(decode_interrupt_cb);
     }
+
+done:
+    return ret;
+
+fail:
+    av_encode_close();
+    goto done;
+
+fail1:
+    ret = AVERROR(ENOMEM);
+    goto fail1;
+}
+
+/*
+ * 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;
@@ -1961,37 +1989,43 @@
     /* 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;
+    ByteIOContext *pb;
+
     /* close each encoder */
     for(i=0;i<nb_ostreams;i++) {
         ost = ost_table[i];
-        if (ost->encoding_needed) {
-            av_freep(&ost->st->codec->stats_in);
-            avcodec_close(ost->st->codec);
+        if (ost && ost->encoding_needed) {
+            if( ost->st->codec->stats_in)
+                av_freep(&ost->st->codec->stats_in);
+            if( ost->st->codec )
+                avcodec_close(ost->st->codec);
         }
     }
 
     /* close each decoder */
     for(i=0;i<nb_istreams;i++) {
         ist = ist_table[i];
-        if (ist->decoding_needed) {
-            avcodec_close(ist->st->codec);
+        if (ist && ist->decoding_needed) {
+            if( ist->st->codec )
+                avcodec_close(ist->st->codec);
         }
     }
 
-    /* finished ! */
+    if( bit_buffer )
+        av_freep(&bit_buffer);
 
-    ret = 0;
- fail1:
-    av_freep(&bit_buffer);
-    av_free(file_table);
-
-    if (ist_table) {
-        for(i=0;i<nb_istreams;i++) {
-            ist = ist_table[i];
-            av_free(ist);
-        }
-        av_free(ist_table);
-    }
     if (ost_table) {
         for(i=0;i<nb_ostreams;i++) {
             ost = ost_table[i];
@@ -2012,12 +2046,87 @@
         }
         av_free(ost_table);
     }
+
+   if (ist_table) {
+        for(i=0;i<nb_istreams;i++) {
+            ist = ist_table[i];
+            if( ist )
+                av_free(ist);
+        }
+        av_free(ist_table);
+    }
+
+    if( file_table )
+        av_free(file_table);
+
+     /* close output files which opened in opt_output_file */
+    for(i=0;i<nb_output_files;i++) {
+        /* maybe av_close_output_file ??? */
+        AVFormatContext *s = output_files[i];
+        int j;
+
+        pb = &s->pb;
+        if (!(s->oformat->flags & AVFMT_NOFILE) && pb )
+            url_fclose(pb);
+        for(j=0;j<s->nb_streams;j++) {
+            if( s->streams[j]->codec )
+                av_free(s->streams[j]->codec);
+            if( s->streams[j])
+                av_free(s->streams[j]);
+        }
+        av_free(s);
+    }
+
+    /* close input files which opened in opt_input_file */
+    for(i=0;i<nb_input_files;i++)
+        av_close_input_file(input_files[i]);
+
+    /* free memory which allocated in opt_intra_matrix */
+    if(intra_matrix)
+        av_free(intra_matrix);
+
+    /* free memory which allocated in opt_inter_matrix */
+    if(inter_matrix)
+        av_free(inter_matrix);
+
+    av_free_static();
+
+#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);
+    }
+
     return ret;
- fail:
-    ret = AVERROR(ENOMEM);
-    goto fail1;
 }
 
+
+/*
+ * The following code is to start the file converter
+ */
+static int av_encode()
+{
+    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;
+}
+
 #if 0
 int file_read(const char *filename)
 {
@@ -3763,7 +3872,6 @@
 
 int main(int argc, char **argv)
 {
-    int i;
     int64_t ti;
 
     av_register_all();
@@ -3791,48 +3899,15 @@
     }
 
     ti = getutime();
-    av_encode(output_files, nb_output_files, input_files, nb_input_files,
-              stream_maps, nb_stream_maps);
+
+    /* start the file converter */
+    av_encode();
+
     ti = getutime() - ti;
     if (do_benchmark) {
         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_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/20070228/9060402a/attachment.pgp>



More information about the ffmpeg-devel mailing list