---
 ffmpeg.c |  110 ++++++++++++++++++++++----------------------------------------
 1 files changed, 39 insertions(+), 71 deletions(-)

diff --git a/ffmpeg.c b/ffmpeg.c
index fe8b344..70411b8 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -640,10 +640,16 @@ static void choose_pixel_fmt(AVStream *st, AVCodec *codec)
     }
 }
 
-static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx)
+static OutputStream *new_output_stream(AVFormatContext *oc, int file_idx, 
AVCodec *codec)
 {
-    int idx = oc->nb_streams - 1;
     OutputStream *ost;
+    AVStream *st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? 
streamid_map[oc->nb_streams] : 0);
+    int idx      = oc->nb_streams - 1;
+
+    if (!st) {
+        av_log(NULL, AV_LOG_ERROR, "Could not alloc stream.\n");
+        ffmpeg_exit(1);
+    }
 
     output_streams_for_file[file_idx] =
         grow_array(output_streams_for_file[file_idx],
@@ -658,6 +664,10 @@ static OutputStream *new_output_stream(AVFormatContext 
*oc, int file_idx)
     }
     ost->file_index = file_idx;
     ost->index = idx;
+    ost->st    = st;
+    ost->enc   = codec;
+
+    avcodec_get_context_defaults3(st->codec, codec);
 
     ost->sws_flags = av_get_int(sws_opts, "sws_flags", NULL);
     return ost;
@@ -673,27 +683,20 @@ static int read_ffserver_streams(AVFormatContext *s, 
const char *filename)
     if (err < 0)
         return err;
     /* copy stream format */
-    s->nb_streams = 0;
-    s->streams = av_mallocz(sizeof(AVStream *) * ic->nb_streams);
     for(i=0;i<ic->nb_streams;i++) {
         AVStream *st;
+        OutputStream *ost;
         AVCodec *codec;
 
-        s->nb_streams++;
+        codec = avcodec_find_encoder(ic->streams[i]->codec->codec_id);
+        ost   = new_output_stream(s, nb_output_files, codec);
+        st    = ost->st;
 
         // FIXME: a more elegant solution is needed
-        st = av_mallocz(sizeof(AVStream));
         memcpy(st, ic->streams[i], sizeof(AVStream));
         st->info = NULL;
-        st->codec = avcodec_alloc_context();
-        if (!st->codec) {
-            print_error(filename, AVERROR(ENOMEM));
-            ffmpeg_exit(1);
-        }
         avcodec_copy_context(st->codec, ic->streams[i]->codec);
-        s->streams[i] = st;
 
-        codec = avcodec_find_encoder(st->codec->codec_id);
         if (st->codec->codec_type == AVMEDIA_TYPE_AUDIO) {
             if (audio_stream_copy) {
                 st->stream_copy = 1;
@@ -708,8 +711,6 @@ static int read_ffserver_streams(AVFormatContext *s, const 
char *filename)
 
         if(st->codec->flags & CODEC_FLAG_BITEXACT)
             nopts = 1;
-
-        new_output_stream(s, nb_output_files);
     }
 
     if (!nopts)
@@ -2003,7 +2004,6 @@ static int transcode(AVFormatContext **output_files,
         for(i=0;i<os->nb_streams;i++,n++) {
             int found;
             ost = ost_table[n] = output_streams_for_file[k][i];
-            ost->st = os->streams[i];
             if (nb_stream_maps > 0) {
                 ost->source_index = 
input_files[stream_maps[n].file_index].ist_index +
                     stream_maps[n].stream_index;
@@ -3422,30 +3422,24 @@ static void check_inputs(int *has_video_ptr,
 
 static void new_video_stream(AVFormatContext *oc, int file_idx)
 {
-    AVStream *st;
     OutputStream *ost;
     AVCodecContext *video_enc;
     enum CodecID codec_id = CODEC_ID_NONE;
     AVCodec *codec= NULL;
 
-    st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? 
streamid_map[oc->nb_streams] : 0);
-    if (!st) {
-        fprintf(stderr, "Could not alloc stream\n");
-        ffmpeg_exit(1);
-    }
-    ost = new_output_stream(oc, file_idx);
-
     if(!video_stream_copy){
         if (video_codec_name) {
             codec_id = find_codec_or_die(video_codec_name, AVMEDIA_TYPE_VIDEO, 
1,
                                          
avcodec_opts[AVMEDIA_TYPE_VIDEO]->strict_std_compliance);
             codec = avcodec_find_encoder_by_name(video_codec_name);
-            ost->enc = codec;
         } else {
             codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, 
AVMEDIA_TYPE_VIDEO);
             codec = avcodec_find_encoder(codec_id);
         }
+    }
 
+    ost = new_output_stream(oc, file_idx, codec);
+    if (!video_stream_copy) {
         ost->frame_aspect_ratio = frame_aspect_ratio;
         frame_aspect_ratio = 0;
 #if CONFIG_AVFILTER
@@ -3454,13 +3448,12 @@ static void new_video_stream(AVFormatContext *oc, int 
file_idx)
 #endif
     }
 
-    avcodec_get_context_defaults3(st->codec, codec);
     ost->bitstream_filters = video_bitstream_filters;
     video_bitstream_filters= NULL;
 
-    st->codec->thread_count= thread_count;
+    ost->st->codec->thread_count = thread_count;
 
-    video_enc = st->codec;
+    video_enc = ost->st->codec;
 
     if(video_codec_tag)
         video_enc->codec_tag= video_codec_tag;
@@ -3471,10 +3464,10 @@ static void new_video_stream(AVFormatContext *oc, int 
file_idx)
     }
 
     if (video_stream_copy) {
-        st->stream_copy = 1;
+        ost->st->stream_copy = 1;
         video_enc->codec_type = AVMEDIA_TYPE_VIDEO;
         video_enc->sample_aspect_ratio =
-        st->sample_aspect_ratio = 
av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
+        ost->st->sample_aspect_ratio = 
av_d2q(frame_aspect_ratio*frame_height/frame_width, 255);
     } else {
         const char *p;
         int i;
@@ -3487,7 +3480,7 @@ static void new_video_stream(AVFormatContext *oc, int 
file_idx)
         video_enc->width = frame_width;
         video_enc->height = frame_height;
         video_enc->pix_fmt = frame_pix_fmt;
-        st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
+        ost->st->sample_aspect_ratio = video_enc->sample_aspect_ratio;
 
         if (intra_only)
             video_enc->gop_size = 0;
@@ -3547,7 +3540,7 @@ static void new_video_stream(AVFormatContext *oc, int 
file_idx)
             parse_forced_key_frames(forced_key_frames, ost, video_enc);
     }
     if (video_language) {
-        av_dict_set(&st->metadata, "language", video_language, 0);
+        av_dict_set(&ost->st->metadata, "language", video_language, 0);
         av_freep(&video_language);
     }
 
@@ -3561,39 +3554,29 @@ static void new_video_stream(AVFormatContext *oc, int 
file_idx)
 
 static void new_audio_stream(AVFormatContext *oc, int file_idx)
 {
-    AVStream *st;
     OutputStream *ost;
     AVCodec *codec= NULL;
     AVCodecContext *audio_enc;
     enum CodecID codec_id = CODEC_ID_NONE;
 
-    st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? 
streamid_map[oc->nb_streams] : 0);
-    if (!st) {
-        fprintf(stderr, "Could not alloc stream\n");
-        ffmpeg_exit(1);
-    }
-    ost = new_output_stream(oc, file_idx);
-
     if(!audio_stream_copy){
         if (audio_codec_name) {
             codec_id = find_codec_or_die(audio_codec_name, AVMEDIA_TYPE_AUDIO, 
1,
                                          
avcodec_opts[AVMEDIA_TYPE_AUDIO]->strict_std_compliance);
             codec = avcodec_find_encoder_by_name(audio_codec_name);
-            ost->enc = codec;
         } else {
             codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, 
AVMEDIA_TYPE_AUDIO);
             codec = avcodec_find_encoder(codec_id);
         }
     }
-
-    avcodec_get_context_defaults3(st->codec, codec);
+    ost = new_output_stream(oc, file_idx, codec);
 
     ost->bitstream_filters = audio_bitstream_filters;
     audio_bitstream_filters= NULL;
 
-    st->codec->thread_count= thread_count;
+    ost->st->codec->thread_count = thread_count;
 
-    audio_enc = st->codec;
+    audio_enc = ost->st->codec;
     audio_enc->codec_type = AVMEDIA_TYPE_AUDIO;
 
     if(audio_codec_tag)
@@ -3604,7 +3587,7 @@ static void new_audio_stream(AVFormatContext *oc, int 
file_idx)
         avcodec_opts[AVMEDIA_TYPE_AUDIO]->flags|= CODEC_FLAG_GLOBAL_HEADER;
     }
     if (audio_stream_copy) {
-        st->stream_copy = 1;
+        ost->st->stream_copy = 1;
     } else {
         audio_enc->codec_id = codec_id;
         set_context_opts(audio_enc, avcodec_opts[AVMEDIA_TYPE_AUDIO], 
AV_OPT_FLAG_AUDIO_PARAM | AV_OPT_FLAG_ENCODING_PARAM, codec);
@@ -3619,10 +3602,10 @@ static void new_audio_stream(AVFormatContext *oc, int 
file_idx)
         if (audio_sample_rate)
             audio_enc->sample_rate = audio_sample_rate;
         audio_enc->channel_layout = channel_layout;
-        choose_sample_fmt(st, codec);
+        choose_sample_fmt(ost->st, codec);
     }
     if (audio_language) {
-        av_dict_set(&st->metadata, "language", audio_language, 0);
+        av_dict_set(&ost->st->metadata, "language", audio_language, 0);
         av_freep(&audio_language);
     }
 
@@ -3634,22 +3617,15 @@ static void new_audio_stream(AVFormatContext *oc, int 
file_idx)
 
 static void new_data_stream(AVFormatContext *oc, int file_idx)
 {
-    AVStream *st;
-    AVCodec *codec=NULL;
+    OutputStream *ost;
     AVCodecContext *data_enc;
 
-    st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? 
streamid_map[oc->nb_streams] : 0);
-    if (!st) {
-        fprintf(stderr, "Could not alloc stream\n");
-        ffmpeg_exit(1);
-    }
-    new_output_stream(oc, file_idx);
-    data_enc = st->codec;
+    ost = new_output_stream(oc, file_idx, NULL);
+    data_enc = ost->st->codec;
     if (!data_stream_copy) {
         fprintf(stderr, "Data stream encoding not supported yet (only 
streamcopy)\n");
         ffmpeg_exit(1);
     }
-    avcodec_get_context_defaults3(st->codec, codec);
 
     data_enc->codec_type = AVMEDIA_TYPE_DATA;
 
@@ -3661,7 +3637,7 @@ static void new_data_stream(AVFormatContext *oc, int 
file_idx)
         avcodec_opts[AVMEDIA_TYPE_DATA]->flags |= CODEC_FLAG_GLOBAL_HEADER;
     }
     if (data_stream_copy) {
-        st->stream_copy = 1;
+        ost->st->stream_copy = 1;
     }
 
     data_disable = 0;
@@ -3671,31 +3647,23 @@ static void new_data_stream(AVFormatContext *oc, int 
file_idx)
 
 static void new_subtitle_stream(AVFormatContext *oc, int file_idx)
 {
-    AVStream *st;
     OutputStream *ost;
     AVCodec *codec=NULL;
     AVCodecContext *subtitle_enc;
     enum CodecID codec_id = CODEC_ID_NONE;
 
-    st = av_new_stream(oc, oc->nb_streams < nb_streamid_map ? 
streamid_map[oc->nb_streams] : 0);
-    if (!st) {
-        fprintf(stderr, "Could not alloc stream\n");
-        ffmpeg_exit(1);
-    }
-    ost = new_output_stream(oc, file_idx);
-    subtitle_enc = st->codec;
     if(!subtitle_stream_copy){
         if (subtitle_codec_name) {
             codec_id = find_codec_or_die(subtitle_codec_name, 
AVMEDIA_TYPE_SUBTITLE, 1,
                                          
avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->strict_std_compliance);
             codec = avcodec_find_encoder_by_name(subtitle_codec_name);
-            ost->enc = codec;
         } else {
             codec_id = av_guess_codec(oc->oformat, NULL, oc->filename, NULL, 
AVMEDIA_TYPE_SUBTITLE);
             codec = avcodec_find_encoder(codec_id);
         }
     }
-    avcodec_get_context_defaults3(st->codec, codec);
+    ost = new_output_stream(oc, file_idx, codec);
+    subtitle_enc = ost->st->codec;
 
     ost->bitstream_filters = subtitle_bitstream_filters;
     subtitle_bitstream_filters= NULL;
@@ -3710,14 +3678,14 @@ static void new_subtitle_stream(AVFormatContext *oc, 
int file_idx)
         avcodec_opts[AVMEDIA_TYPE_SUBTITLE]->flags |= CODEC_FLAG_GLOBAL_HEADER;
     }
     if (subtitle_stream_copy) {
-        st->stream_copy = 1;
+        ost->st->stream_copy = 1;
     } else {
         subtitle_enc->codec_id = codec_id;
         set_context_opts(avcodec_opts[AVMEDIA_TYPE_SUBTITLE], subtitle_enc, 
AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_ENCODING_PARAM, codec);
     }
 
     if (subtitle_language) {
-        av_dict_set(&st->metadata, "language", subtitle_language, 0);
+        av_dict_set(&ost->st->metadata, "language", subtitle_language, 0);
         av_freep(&subtitle_language);
     }
 
-- 
1.7.5.3

_______________________________________________
libav-devel mailing list
[email protected]
https://lists.libav.org/mailman/listinfo/libav-devel

Reply via email to