[FFmpeg-devel] ffmpeg: simplify init_output_stream_streamcopy()

Submitted by James Almer on Nov. 14, 2016, 4:45 p.m.

Details

Message ID 20161114164524.4536-1-jamrial@gmail.com
State New
Headers show

Commit Message

James Almer Nov. 14, 2016, 4:45 p.m.
Skip the intermediary codecpar struct and instead copy everything to the
destination codecpar struct directly.

Signed-off-by: James Almer <jamrial@gmail.com>
---
 ffmpeg.c     | 17 +++++++----------
 ffmpeg.h     |  1 -
 ffmpeg_opt.c |  6 ------
 3 files changed, 7 insertions(+), 17 deletions(-)

Patch hide | download patch | download mbox

diff --git a/ffmpeg.c b/ffmpeg.c
index a6e9476..8a6978d 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -536,7 +536,6 @@  static void ffmpeg_cleanup(int ret)
         av_dict_free(&ost->sws_dict);
 
         avcodec_free_context(&ost->enc_ctx);
-        avcodec_parameters_free(&ost->ref_par);
 
         while (ost->muxing_queue && av_fifo_size(ost->muxing_queue)) {
             AVPacket pkt;
@@ -2878,31 +2877,29 @@  static int init_output_stream_streamcopy(OutputStream *ost)
     OutputFile *of = output_files[ost->file_index];
     InputStream *ist = get_input_stream(ost);
     AVCodecParameters *par_dst = ost->st->codecpar;
-    AVCodecParameters *par_src = ost->ref_par;
+    AVCodecParameters *par_src = ist->st->codecpar;
     AVRational sar;
     int i, ret;
     uint32_t codec_tag = par_dst->codec_tag;
 
     av_assert0(ist && !ost->filter);
 
-    avcodec_parameters_to_context(ost->enc_ctx, ist->st->codecpar);
+    avcodec_parameters_to_context(ost->enc_ctx, par_src);
     ret = av_opt_set_dict(ost->enc_ctx, &ost->encoder_opts);
     if (ret < 0) {
         av_log(NULL, AV_LOG_FATAL,
                "Error setting up codec context options.\n");
         return ret;
     }
-    avcodec_parameters_from_context(par_src, ost->enc_ctx);
 
     if (!codec_tag) {
-        unsigned int codec_tag_tmp;
         if (!of->ctx->oformat->codec_tag ||
-            av_codec_get_id (of->ctx->oformat->codec_tag, par_src->codec_tag) == par_src->codec_id ||
-            !av_codec_get_tag2(of->ctx->oformat->codec_tag, par_src->codec_id, &codec_tag_tmp))
-            codec_tag = par_src->codec_tag;
+            av_codec_get_id (of->ctx->oformat->codec_tag, ost->enc_ctx->codec_tag) == ost->enc_ctx->codec_id ||
+            av_codec_get_tag(of->ctx->oformat->codec_tag, ost->enc_ctx->codec_id) <= 0)
+            codec_tag = ost->enc_ctx->codec_tag;
     }
 
-    ret = avcodec_parameters_copy(par_dst, par_src);
+    ret = avcodec_parameters_from_context(par_dst, ost->enc_ctx);
     if (ret < 0)
         return ret;
 
@@ -2973,7 +2970,7 @@  static int init_output_stream_streamcopy(OutputStream *ost)
         else if (ist->st->sample_aspect_ratio.num)
             sar = ist->st->sample_aspect_ratio;
         else
-            sar = par_src->sample_aspect_ratio;
+            sar = par_dst->sample_aspect_ratio;
         ost->st->sample_aspect_ratio = par_dst->sample_aspect_ratio = sar;
         ost->st->avg_frame_rate = ist->st->avg_frame_rate;
         ost->st->r_frame_rate = ist->st->r_frame_rate;
diff --git a/ffmpeg.h b/ffmpeg.h
index ebe5bf0..1256b1f 100644
--- a/ffmpeg.h
+++ b/ffmpeg.h
@@ -450,7 +450,6 @@  typedef struct OutputStream {
     AVBSFContext            **bsf_ctx;
 
     AVCodecContext *enc_ctx;
-    AVCodecParameters *ref_par; /* associated input codec parameters with encoders options applied */
     AVCodec *enc;
     int64_t max_frames;
     AVFrame *filtered_frame;
diff --git a/ffmpeg_opt.c b/ffmpeg_opt.c
index ddda3f2..6b802ca 100644
--- a/ffmpeg_opt.c
+++ b/ffmpeg_opt.c
@@ -1266,12 +1266,6 @@  static OutputStream *new_output_stream(OptionsContext *o, AVFormatContext *oc, e
     }
     ost->enc_ctx->codec_type = type;
 
-    ost->ref_par = avcodec_parameters_alloc();
-    if (!ost->ref_par) {
-        av_log(NULL, AV_LOG_ERROR, "Error allocating the encoding parameters.\n");
-        exit_program(1);
-    }
-
     if (ost->enc) {
         AVIOContext *s = NULL;
         char *buf = NULL, *arg = NULL, *preset = NULL;