[FFmpeg-devel] [PATCH 1/6] libavformat: Use avcodec_copy_context for chained muxers

Martin Storsjö martin
Fri Feb 4 11:04:15 CET 2011


This avoids having the chained AVStream->codec point to the same
AVCodecContext owned by the outer AVStream. The downside is that
changes to the AVCodecContext made after calling av_write_header
cannot be detected automatically within the chained muxer.

This avoids having to manually unlink the chained AVStream->codec
by setting it to null before freeing the chained muxer via generic
freeing functions.
---
 libavformat/movenchint.c   |    9 +++++----
 libavformat/rtpenc_chain.c |    8 +++-----
 libavformat/rtsp.c         |    2 ++
 libavformat/sapenc.c       |    2 ++
 4 files changed, 12 insertions(+), 9 deletions(-)

diff --git a/libavformat/movenchint.c b/libavformat/movenchint.c
index 2aa0f17..6a9df07 100644
--- a/libavformat/movenchint.c
+++ b/libavformat/movenchint.c
@@ -57,10 +57,7 @@ int ff_mov_init_hinting(AVFormatContext *s, int index, int src_index)
     track->rtp_ctx->streams[0]->sample_aspect_ratio =
                         src_st->sample_aspect_ratio;
 
-    /* Remove the allocated codec context, link to the original one
-     * instead, to give the rtp muxer access to codec parameters. */
-    av_free(track->rtp_ctx->streams[0]->codec);
-    track->rtp_ctx->streams[0]->codec = src_st->codec;
+    avcodec_copy_context(track->rtp_ctx->streams[0]->codec, src_st->codec);
 
     if ((ret = url_open_dyn_packet_buf(&track->rtp_ctx->pb,
                                        RTP_MAX_PACKET_SIZE)) < 0)
@@ -86,6 +83,8 @@ fail:
     }
     if (track->rtp_ctx && track->rtp_ctx->streams[0]) {
         av_metadata_free(&track->rtp_ctx->streams[0]->metadata);
+        av_free(track->rtp_ctx->streams[0]->codec->extradata);
+        av_free(track->rtp_ctx->streams[0]->codec);
         av_free(track->rtp_ctx->streams[0]->info);
         av_free(track->rtp_ctx->streams[0]);
     }
@@ -491,6 +490,8 @@ void ff_mov_close_hinting(MOVTrack *track) {
     }
     av_metadata_free(&rtp_ctx->streams[0]->metadata);
     av_metadata_free(&rtp_ctx->metadata);
+    av_free(rtp_ctx->streams[0]->codec->extradata);
+    av_free(rtp_ctx->streams[0]->codec);
     av_free(rtp_ctx->streams[0]->info);
     av_free(rtp_ctx->streams[0]);
     av_freep(&rtp_ctx);
diff --git a/libavformat/rtpenc_chain.c b/libavformat/rtpenc_chain.c
index 19ea98f..09b4ebd 100644
--- a/libavformat/rtpenc_chain.c
+++ b/libavformat/rtpenc_chain.c
@@ -50,11 +50,7 @@ AVFormatContext *ff_rtp_chain_mux_open(AVFormatContext *s, AVStream *st,
     /* Set the synchronized start time. */
     rtpctx->start_time_realtime = s->start_time_realtime;
 
-    /* Remove the local codec, link to the original codec
-     * context instead, to give the rtp muxer access to
-     * codec parameters. */
-    av_free(rtpctx->streams[0]->codec);
-    rtpctx->streams[0]->codec = st->codec;
+    avcodec_copy_context(rtpctx->streams[0]->codec, st->codec);
 
     if (handle) {
         url_fdopen(&rtpctx->pb, handle);
@@ -70,6 +66,8 @@ AVFormatContext *ff_rtp_chain_mux_open(AVFormatContext *s, AVStream *st,
             url_close_dyn_buf(rtpctx->pb, &ptr);
             av_free(ptr);
         }
+        av_free(rtpctx->streams[0]->codec->extradata);
+        av_free(rtpctx->streams[0]->codec);
         av_free(rtpctx->streams[0]->info);
         av_free(rtpctx->streams[0]);
         av_free(rtpctx);
diff --git a/libavformat/rtsp.c b/libavformat/rtsp.c
index 52c686c..d31e8ee 100644
--- a/libavformat/rtsp.c
+++ b/libavformat/rtsp.c
@@ -500,6 +500,8 @@ void ff_rtsp_undo_setup(AVFormatContext *s)
                 }
                 av_metadata_free(&rtpctx->streams[0]->metadata);
                 av_metadata_free(&rtpctx->metadata);
+                av_free(rtpctx->streams[0]->codec->extradata);
+                av_free(rtpctx->streams[0]->codec);
                 av_free(rtpctx->streams[0]->info);
                 av_free(rtpctx->streams[0]);
                 av_free(rtpctx);
diff --git a/libavformat/sapenc.c b/libavformat/sapenc.c
index 088f7bd..634f033 100644
--- a/libavformat/sapenc.c
+++ b/libavformat/sapenc.c
@@ -48,6 +48,8 @@ static int sap_write_close(AVFormatContext *s)
         url_fclose(rtpctx->pb);
         av_metadata_free(&rtpctx->streams[0]->metadata);
         av_metadata_free(&rtpctx->metadata);
+        av_free(rtpctx->streams[0]->codec->extradata);
+        av_free(rtpctx->streams[0]->codec);
         av_free(rtpctx->streams[0]->info);
         av_free(rtpctx->streams[0]);
         av_free(rtpctx);
-- 
1.7.3.1




More information about the ffmpeg-devel mailing list