[FFmpeg-devel] [PATCH 2/2] Create an open_codec() function. Factorize.

Stefano Sabatini stefano.sabatini-lala
Sat Oct 16 19:18:06 CEST 2010


---
 ffmpeg.c |   57 +++++++++++++++++++++++++++++++--------------------------
 1 files changed, 31 insertions(+), 26 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index c8ec7e0..596f604 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -1929,6 +1929,31 @@ static int copy_chapters(int infile, int outfile)
     return 0;
 }
 
+static int open_codec(AVCodec **codec, AVCodecContext *avctx, enum CodecID codec_id,
+                      int file_index, int stream_index, int is_output,
+                      char *error, int error_size)
+{
+    int ret;
+
+    if (!*codec)
+        *codec = is_output ? avcodec_find_encoder(codec_id) : avcodec_find_decoder(codec_id);
+    if (!*codec) {
+        snprintf(error, error_size, "%s (codec id %d) not found for %s stream #%d.%d",
+                 is_output ? "encoder" : "decoder", codec_id,
+                 is_output ? "output" : "input", file_index, stream_index);
+        return AVERROR(EINVAL);
+    }
+    if ((ret = avcodec_open(avctx, *codec)) < 0) {
+        snprintf(error, error_size,
+                 "Error while opening %s for %s stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
+                 is_output ? "encoder" : "decoder", is_output ? "output" : "input",
+                 file_index, stream_index);
+        return ret;
+    }
+
+    return 0;
+}
+
 /*
  * The following code is the main loop of the file converter
  */
@@ -2316,20 +2341,10 @@ static int transcode(AVFormatContext **output_files,
         ost = ost_table[i];
         if (ost->encoding_needed) {
             AVCodec *codec = i < nb_output_codecs ? output_codecs[i] : NULL;
-            if (!codec)
-                codec = avcodec_find_encoder(ost->st->codec->codec_id);
-            if (!codec) {
-                snprintf(error, sizeof(error), "Encoder (codec id %d) not found for output stream #%d.%d",
-                         ost->st->codec->codec_id, ost->file_index, ost->index);
-                ret = AVERROR(EINVAL);
+            ret = open_codec(&codec, ost->st->codec, ost->st->codec->codec_id,
+                             ost->file_index, ost->index, 1, error, sizeof(error));
+            if (ret < 0)
                 goto dump_format;
-            }
-            if (avcodec_open(ost->st->codec, codec) < 0) {
-                snprintf(error, sizeof(error), "Error while opening encoder for output stream #%d.%d - maybe incorrect parameters such as bit_rate, rate, width or height",
-                        ost->file_index, ost->index);
-                ret = AVERROR(EINVAL);
-                goto dump_format;
-            }
             extra_size += ost->st->codec->extradata_size;
         }
     }
@@ -2339,20 +2354,10 @@ static int transcode(AVFormatContext **output_files,
         ist = ist_table[i];
         if (ist->decoding_needed) {
             AVCodec *codec = i < nb_input_codecs ? input_codecs[i] : NULL;
-            if (!codec)
-                codec = avcodec_find_decoder(ist->st->codec->codec_id);
-            if (!codec) {
-                snprintf(error, sizeof(error), "Decoder (codec id %d) not found for input stream #%d.%d",
-                        ist->st->codec->codec_id, ist->file_index, ist->index);
-                ret = AVERROR(EINVAL);
+            ret = open_codec(&codec, ist->st->codec, ist->st->codec->codec_id,
+                             ist->file_index, ist->index, 0, error, sizeof(error));
+            if (ret < 0)
                 goto dump_format;
-            }
-            if (avcodec_open(ist->st->codec, codec) < 0) {
-                snprintf(error, sizeof(error), "Error while opening decoder for input stream #%d.%d",
-                        ist->file_index, ist->index);
-                ret = AVERROR(EINVAL);
-                goto dump_format;
-            }
             //if (ist->st->codec->codec_type == AVMEDIA_TYPE_VIDEO)
             //    ist->st->codec->flags |= CODEC_FLAG_REPEAT_FIELD;
         }
-- 
1.7.1




More information about the ffmpeg-devel mailing list