avformat/utils: Move internal stream timebase stuff to avformat.c

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2022-05-07 05:46:40 +02:00
parent b516302cfe
commit 217f2bfb49
2 changed files with 66 additions and 65 deletions

View File

@ -20,6 +20,8 @@
*/
#include "libavutil/avassert.h"
#include "libavutil/avstring.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/mem.h"
#include "libavutil/opt.h"
#include "libavcodec/avcodec.h"
@ -254,3 +256,67 @@ void av_program_add_stream_index(AVFormatContext *ac, int progid, unsigned idx)
return;
}
}
int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
AVStream *ost, const AVStream *ist,
enum AVTimebaseSource copy_tb)
{
const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
AVCodecContext *const enc_ctx = ffstream(ost)->avctx;
enc_ctx->time_base = ist->time_base;
/*
* Avi is a special case here because it supports variable fps but
* having the fps and timebase differe significantly adds quite some
* overhead
*/
if (!strcmp(ofmt->name, "avi")) {
#if FF_API_R_FRAME_RATE
if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
&& av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
&& 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
&& 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx->time_base)
&& av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_R_FRAMERATE) {
enc_ctx->time_base.num = ist->r_frame_rate.den;
enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
enc_ctx->ticks_per_frame = 2;
} else
#endif
if (copy_tb == AVFMT_TBCF_AUTO && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->time_base)
&& av_q2d(ist->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_DECODER) {
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
enc_ctx->time_base.den *= 2;
enc_ctx->ticks_per_frame = 2;
}
} else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
&& !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->time_base.den
&& av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->time_base)
&& av_q2d(ist->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_DECODER) {
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
}
}
if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
&& dec_ctx->time_base.num < dec_ctx->time_base.den
&& dec_ctx->time_base.num > 0
&& 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
enc_ctx->time_base = dec_ctx->time_base;
}
av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
return 0;
}
AVRational av_stream_get_codec_timebase(const AVStream *st)
{
// See avformat_transfer_internal_stream_timing_info() TODO.
return cffstream(st)->avctx->time_base;
}

View File

@ -28,7 +28,6 @@
#include "libavutil/bprint.h"
#include "libavutil/dict.h"
#include "libavutil/internal.h"
#include "libavutil/intreadwrite.h"
#include "libavutil/pixfmt.h"
#include "libavutil/thread.h"
#include "libavutil/time.h"
@ -1079,70 +1078,6 @@ int ff_bprint_to_codecpar_extradata(AVCodecParameters *par, struct AVBPrint *buf
return 0;
}
int avformat_transfer_internal_stream_timing_info(const AVOutputFormat *ofmt,
AVStream *ost, const AVStream *ist,
enum AVTimebaseSource copy_tb)
{
const AVCodecContext *const dec_ctx = cffstream(ist)->avctx;
AVCodecContext *const enc_ctx = ffstream(ost)->avctx;
enc_ctx->time_base = ist->time_base;
/*
* Avi is a special case here because it supports variable fps but
* having the fps and timebase differe significantly adds quite some
* overhead
*/
if (!strcmp(ofmt->name, "avi")) {
#if FF_API_R_FRAME_RATE
if (copy_tb == AVFMT_TBCF_AUTO && ist->r_frame_rate.num
&& av_q2d(ist->r_frame_rate) >= av_q2d(ist->avg_frame_rate)
&& 0.5/av_q2d(ist->r_frame_rate) > av_q2d(ist->time_base)
&& 0.5/av_q2d(ist->r_frame_rate) > av_q2d(dec_ctx->time_base)
&& av_q2d(ist->time_base) < 1.0/500 && av_q2d(dec_ctx->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_R_FRAMERATE) {
enc_ctx->time_base.num = ist->r_frame_rate.den;
enc_ctx->time_base.den = 2*ist->r_frame_rate.num;
enc_ctx->ticks_per_frame = 2;
} else
#endif
if (copy_tb == AVFMT_TBCF_AUTO && av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > 2*av_q2d(ist->time_base)
&& av_q2d(ist->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_DECODER) {
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
enc_ctx->time_base.den *= 2;
enc_ctx->ticks_per_frame = 2;
}
} else if (!(ofmt->flags & AVFMT_VARIABLE_FPS)
&& !av_match_name(ofmt->name, "mov,mp4,3gp,3g2,psp,ipod,ismv,f4v")) {
if (copy_tb == AVFMT_TBCF_AUTO && dec_ctx->time_base.den
&& av_q2d(dec_ctx->time_base)*dec_ctx->ticks_per_frame > av_q2d(ist->time_base)
&& av_q2d(ist->time_base) < 1.0/500
|| copy_tb == AVFMT_TBCF_DECODER) {
enc_ctx->time_base = dec_ctx->time_base;
enc_ctx->time_base.num *= dec_ctx->ticks_per_frame;
}
}
if ((enc_ctx->codec_tag == AV_RL32("tmcd") || ost->codecpar->codec_tag == AV_RL32("tmcd"))
&& dec_ctx->time_base.num < dec_ctx->time_base.den
&& dec_ctx->time_base.num > 0
&& 121LL*dec_ctx->time_base.num > dec_ctx->time_base.den) {
enc_ctx->time_base = dec_ctx->time_base;
}
av_reduce(&enc_ctx->time_base.num, &enc_ctx->time_base.den,
enc_ctx->time_base.num, enc_ctx->time_base.den, INT_MAX);
return 0;
}
AVRational av_stream_get_codec_timebase(const AVStream *st)
{
// See avformat_transfer_internal_stream_timing_info() TODO.
return cffstream(st)->avctx->time_base;
}
void ff_format_set_url(AVFormatContext *s, char *url)
{
av_assert0(url);