Skip to content

Commit 569ad28

Browse files
committed
avformat/options: Only allocate AVCodecContext for demuxers
The muxer's AVCodecContext is currently used for exactly one thing: To store a time base in it that has been derived via heuristics in avformat_transfer_internal_stream_timing_info(); said time base can then be read back via av_stream_get_codec_timebase(). But one does not need a whole AVCodecContext for that, a simple AVRational is enough. Signed-off-by: Andreas Rheinhardt <[email protected]>
1 parent 76ef2b9 commit 569ad28

File tree

4 files changed

+47
-36
lines changed

4 files changed

+47
-36
lines changed

libavformat/avformat.c

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -729,8 +729,6 @@ AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *f
729729
{
730730
AVRational fr = st->r_frame_rate;
731731
const AVCodecDescriptor *desc = cffstream(st)->codec_desc;
732-
AVCodecContext *const avctx = ffstream(st)->avctx;
733-
AVRational codec_fr = avctx->framerate;
734732
AVRational avg_fr = st->avg_frame_rate;
735733

736734
if (avg_fr.num > 0 && avg_fr.den > 0 && fr.num > 0 && fr.den > 0 &&
@@ -739,6 +737,9 @@ AVRational av_guess_frame_rate(AVFormatContext *format, AVStream *st, AVFrame *f
739737
}
740738

741739
if (desc && (desc->props & AV_CODEC_PROP_FIELDS)) {
740+
const AVCodecContext *const avctx = ffstream(st)->avctx;
741+
AVRational codec_fr = avctx->framerate;
742+
742743
if ( codec_fr.num > 0 && codec_fr.den > 0 &&
743744
(fr.num == 0 || av_q2d(codec_fr) < av_q2d(fr)*0.7 && fabs(1.0 - av_q2d(av_div_q(avg_fr, fr))) > 0.1))
744745
fr = codec_fr;
@@ -753,13 +754,19 @@ int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
753754
{
754755
const AVCodecDescriptor *desc = cffstream(ist)->codec_desc;
755756
const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
756-
AVCodecContext *const enc_ctx = ffstream(ost)->avctx;
757757

758758
AVRational mul = (AVRational){ desc && (desc->props & AV_CODEC_PROP_FIELDS) ? 2 : 1, 1 };
759-
AVRational dec_ctx_tb = dec_ctx->framerate.num ? av_inv_q(av_mul_q(dec_ctx->framerate, mul))
759+
AVRational dec_ctx_framerate = dec_ctx ? dec_ctx->framerate : (AVRational){ 0, 0 };
760+
AVRational dec_ctx_tb = dec_ctx_framerate.num ? av_inv_q(av_mul_q(dec_ctx_framerate, mul))
760761
: (ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO ? (AVRational){0, 1}
761762
: ist->time_base);
762-
enc_ctx->time_base = ist->time_base;
763+
AVRational enc_tb = ist->time_base;
764+
#if FF_API_TICKS_PER_FRAME
765+
FF_DISABLE_DEPRECATION_WARNINGS
766+
int ticks_per_frame = dec_ctx ? dec_ctx->ticks_per_frame : 1;
767+
FF_ENABLE_DEPRECATION_WARNINGS
768+
#endif
769+
763770
/*
764771
* Avi is a special case here because it supports variable fps but
765772
* having the fps and timebase differe significantly adds quite some
@@ -773,35 +780,31 @@ int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
773780
&& 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx_tb)
774781
&& av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx_tb) < 1.0/500
775782
|| copy_tb == AVFMT_TBCF_R_FRAMERATE) {
776-
enc_ctx->time_base.num = ist->r_frame_rate.den;
777-
enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
783+
enc_tb.num = ist->r_frame_rate.den;
784+
enc_tb.den = 2*ist->r_frame_rate.num;
778785
} else
779786
#endif
780-
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->framerate.num &&
781-
av_q2d(av_inv_q(dec_ctx->framerate)) > 2*av_q2d(ist->time_base)
787+
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num &&
788+
av_q2d(av_inv_q(dec_ctx_framerate)) > 2*av_q2d(ist->time_base)
782789
&& av_q2d(ist->time_base) < 1.0/500
783790
|| (copy_tb == AVFMT_TBCF_DECODER &&
784-
(dec_ctx->framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
785-
enc_ctx->time_base = dec_ctx_tb;
786-
enc_ctx->time_base.den *= 2;
791+
(dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
792+
enc_tb = dec_ctx_tb;
793+
enc_tb.den *= 2;
787794
#if FF_API_TICKS_PER_FRAME
788-
FF_DISABLE_DEPRECATION_WARNINGS
789-
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
790-
FF_ENABLE_DEPRECATION_WARNINGS
795+
enc_tb.num *= ticks_per_frame;
791796
#endif
792797
}
793798
} else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
794799
&& !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
795-
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->framerate.num
796-
&& av_q2d(av_inv_q(dec_ctx->framerate)) > av_q2d(ist->time_base)
800+
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx_framerate.num
801+
&& av_q2d(av_inv_q(dec_ctx_framerate)) > av_q2d(ist->time_base)
797802
&& av_q2d(ist->time_base) < 1.0/500
798803
|| (copy_tb == AVFMT_TBCF_DECODER &&
799-
(dec_ctx->framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
800-
enc_ctx->time_base = dec_ctx_tb;
804+
(dec_ctx_framerate.num || ist->codecpar->codec_type == AVMEDIA_TYPE_AUDIO))) {
805+
enc_tb = dec_ctx_tb;
801806
#if FF_API_TICKS_PER_FRAME
802-
FF_DISABLE_DEPRECATION_WARNINGS
803-
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
804-
FF_ENABLE_DEPRECATION_WARNINGS
807+
enc_tb.num *= ticks_per_frame;
805808
#endif
806809
}
807810
}
@@ -810,18 +813,19 @@ FF_ENABLE_DEPRECATION_WARNINGS
810813
&& dec_ctx_tb.num < dec_ctx_tb.den
811814
&& dec_ctx_tb.num > 0
812815
&& 121LL*dec_ctx_tb.num > dec_ctx_tb.den) {
813-
enc_ctx->time_base = dec_ctx_tb;
816+
enc_tb = dec_ctx_tb;
814817
}
815818

816-
av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
817-
enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
819+
av_reduce(&ffstream(ost)->transferred_mux_tb.num,
820+
&ffstream(ost)->transferred_mux_tb.den,
821+
enc_tb.num, enc_tb.den, INT_MAX);
818822

819823
return 0;
820824
}
821825

822826
AVRational av_stream_get_codec_timebase(const AVStream *st)
823827
{
824-
return cffstream(st)->avctx->time_base;
828+
return cffstream(st)->avctx ? cffstream(st)->avctx->time_base : cffstream(st)->transferred_mux_tb;
825829
}
826830

827831
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
@@ -846,7 +850,8 @@ void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits,
846850
return;
847851
}
848852
st->time_base = new_tb;
849-
sti->avctx->pkt_timebase = new_tb;
853+
if (sti->avctx)
854+
sti->avctx->pkt_timebase = new_tb;
850855
st->pts_wrap_bits = pts_wrap_bits;
851856
}
852857

libavformat/dump.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,12 +565,14 @@ static void dump_stream_format(const AVFormatContext *ic, int i,
565565
}
566566

567567
// Fields which are missing from AVCodecParameters need to be taken from the AVCodecContext
568-
avctx->properties = sti->avctx->properties;
569-
avctx->codec = sti->avctx->codec;
570-
avctx->qmin = sti->avctx->qmin;
571-
avctx->qmax = sti->avctx->qmax;
572-
avctx->coded_width = sti->avctx->coded_width;
573-
avctx->coded_height = sti->avctx->coded_height;
568+
if (sti->avctx) {
569+
avctx->properties = sti->avctx->properties;
570+
avctx->codec = sti->avctx->codec;
571+
avctx->qmin = sti->avctx->qmin;
572+
avctx->qmax = sti->avctx->qmax;
573+
avctx->coded_width = sti->avctx->coded_width;
574+
avctx->coded_height = sti->avctx->coded_height;
575+
}
574576

575577
if (separator)
576578
av_opt_set(avctx, "dump_separator", separator, 0);

libavformat/internal.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,8 @@ typedef struct FFStream {
416416
int64_t cur_dts;
417417

418418
const struct AVCodecDescriptor *codec_desc;
419+
420+
AVRational transferred_mux_tb;
419421
} FFStream;
420422

421423
static av_always_inline FFStream *ffstream(AVStream *st)

libavformat/options.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -283,11 +283,12 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
283283
goto fail;
284284

285285
sti->fmtctx = s;
286-
sti->avctx = avcodec_alloc_context3(NULL);
287-
if (!sti->avctx)
288-
goto fail;
289286

290287
if (s->iformat) {
288+
sti->avctx = avcodec_alloc_context3(NULL);
289+
if (!sti->avctx)
290+
goto fail;
291+
291292
sti->info = av_mallocz(sizeof(*sti->info));
292293
if (!sti->info)
293294
goto fail;
@@ -323,6 +324,7 @@ AVStream *avformat_new_stream(AVFormatContext *s, const AVCodec *c)
323324
sti->pts_buffer[i] = AV_NOPTS_VALUE;
324325

325326
st->sample_aspect_ratio = (AVRational) { 0, 1 };
327+
sti->transferred_mux_tb = (AVRational) { 0, 1 };;
326328

327329
#if FF_API_AVSTREAM_SIDE_DATA
328330
sti->inject_global_side_data = si->inject_global_side_data;

0 commit comments

Comments
 (0)