[FFmpeg-devel] [PATCH 4/7] ffmpeg: dynamically allocate output_codecs

Aurélien Jacobs aurel
Fri Aug 13 20:24:17 CEST 2010


From: Aurelien Jacobs <aurel at gnuage.org>

---
 ffmpeg.c |   19 ++++++++++---------
 1 files changed, 10 insertions(+), 9 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index 98d5c35..dcaaecf 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -113,9 +113,9 @@ static int nb_input_codecs = 0;
 static int nb_input_files_ts_scale[MAX_FILES] = {0};
 
 static AVFormatContext *output_files[MAX_FILES];
-static AVCodec *output_codecs[MAX_FILES*MAX_STREAMS];
+static AVCodec **output_codecs = NULL;
 static int nb_output_files = 0;
-static int nb_ocodecs;
+static int nb_output_codecs = 0;
 
 static AVStreamMap stream_maps[MAX_FILES*MAX_STREAMS];
 static int nb_stream_maps;
@@ -621,6 +621,7 @@ static int ffmpeg_exit(int ret)
     av_free(opt_names);
     av_free(streamid_map);
     av_free(input_codecs);
+    av_free(output_codecs);
 
     av_free(video_codec_name);
     av_free(audio_codec_name);
@@ -2352,7 +2353,7 @@ static int transcode(AVFormatContext **output_files,
     for(i=0;i<nb_ostreams;i++) {
         ost = ost_table[i];
         if (ost->encoding_needed) {
-            AVCodec *codec = output_codecs[i];
+            AVCodec *codec = i < nb_output_codecs ? output_codecs[i] : NULL;
             if (!codec)
                 codec = avcodec_find_encoder(ost->st->codec->codec_id);
             if (!codec) {
@@ -3405,6 +3406,7 @@ static void new_video_stream(AVFormatContext *oc)
         avcodec_opts[AVMEDIA_TYPE_VIDEO]->flags2|= CODEC_FLAG2_LOCAL_HEADER;
     }
 
+    GROW_ARRAY(output_codecs, nb_output_codecs+1);
     if (video_stream_copy) {
         st->stream_copy = 1;
         video_enc->codec_type = AVMEDIA_TYPE_VIDEO;
@@ -3420,7 +3422,7 @@ static void new_video_stream(AVFormatContext *oc)
             codec_id = find_codec_or_die(video_codec_name, AVMEDIA_TYPE_VIDEO, 1,
                                          video_enc->strict_std_compliance);
             codec = avcodec_find_encoder_by_name(video_codec_name);
-            output_codecs[nb_ocodecs] = codec;
+            output_codecs[nb_output_codecs-1] = codec;
         } else {
             codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_VIDEO);
             codec = avcodec_find_encoder(codec_id);
@@ -3498,7 +3500,6 @@ static void new_video_stream(AVFormatContext *oc)
             }
         }
     }
-    nb_ocodecs++;
     if (video_language) {
         av_metadata_set2(&st->metadata, "language", video_language, 0);
         av_freep(&video_language);
@@ -3539,6 +3540,7 @@ static void new_audio_stream(AVFormatContext *oc)
         audio_enc->flags |= CODEC_FLAG_GLOBAL_HEADER;
         avcodec_opts[AVMEDIA_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER;
     }
+    GROW_ARRAY(output_codecs, nb_output_codecs+1);
     if (audio_stream_copy) {
         st->stream_copy = 1;
         audio_enc->channels = audio_channels;
@@ -3552,7 +3554,7 @@ static void new_audio_stream(AVFormatContext *oc)
             codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 1,
                                          audio_enc->strict_std_compliance);
             codec = avcodec_find_encoder_by_name(audio_codec_name);
-            output_codecs[nb_ocodecs] = codec;
+            output_codecs[nb_output_codecs-1] = codec;
         } else {
             codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, AVMEDIA_TYPE_AUDIO);
             codec = avcodec_find_encoder(codec_id);
@@ -3572,7 +3574,6 @@ static void new_audio_stream(AVFormatContext *oc)
         choose_sample_fmt(st, codec);
         choose_sample_rate(st, codec);
     }
-    nb_ocodecs++;
     audio_enc->time_base= (AVRational){1, audio_sample_rate};
     if (audio_language) {
         av_metadata_set2(&st->metadata, "language", audio_language, 0);
@@ -3606,15 +3607,15 @@ static void new_subtitle_stream(AVFormatContext *oc)
     if(subtitle_codec_tag)
         subtitle_enc->codec_tag= subtitle_codec_tag;
 
+    GROW_ARRAY(output_codecs, nb_output_codecs+1);
     if (subtitle_stream_copy) {
         st->stream_copy = 1;
     } else {
         set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM);
         subtitle_enc->codec_id = find_codec_or_die(subtitle_codec_name, AVMEDIA_TYPE_SUBTITLE, 1,
                                                    subtitle_enc->strict_std_compliance);
-        output_codecs[nb_ocodecs] = avcodec_find_encoder_by_name(subtitle_codec_name);
+        output_codecs[nb_output_codecs-1] = avcodec_find_encoder_by_name(subtitle_codec_name);
     }
-    nb_ocodecs++;
 
     if (subtitle_language) {
         av_metadata_set2(&st->metadata, "language", subtitle_language, 0);
-- 
1.7.1




More information about the ffmpeg-devel mailing list