avformat/mux, utils: Use dedicated pointer for AVStreamInternal

This gets rid of ugly "->internal" and is in preparation for removing
AVStreamInternal altogether.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
This commit is contained in:
Andreas Rheinhardt 2021-08-24 19:07:51 +02:00
parent fed0282508
commit 9f05b3ba60
2 changed files with 542 additions and 474 deletions

View File

@ -253,6 +253,7 @@ static int init_muxer(AVFormatContext *s, AVDictionary **options)
for (unsigned i = 0; i < s->nb_streams; i++) {
AVStream *const st = s->streams[i];
AVStreamInternal *const sti = st->internal;
AVCodecParameters *const par = st->codecpar;
const AVCodecDescriptor *desc;
@ -303,9 +304,9 @@ static int init_muxer(AVFormatContext *s, AVDictionary **options)
desc = avcodec_descriptor_get(par->codec_id);
if (desc && desc->props & AV_CODEC_PROP_REORDER)
st->internal->reorder = 1;
sti->reorder = 1;
st->internal->is_intra_only = ff_is_intra_only(par->codec_id);
sti->is_intra_only = ff_is_intra_only(par->codec_id);
if (of->codec_tag) {
if ( par->codec_tag
@ -385,6 +386,7 @@ static int init_pts(AVFormatContext *s)
/* init PTS generation */
for (unsigned i = 0; i < s->nb_streams; i++) {
AVStream *const st = s->streams[i];
AVStreamInternal *const sti = st->internal;
int64_t den = AV_NOPTS_VALUE;
switch (st->codecpar->codec_type) {
@ -398,16 +400,16 @@ static int init_pts(AVFormatContext *s)
break;
}
if (!st->internal->priv_pts)
st->internal->priv_pts = av_mallocz(sizeof(*st->internal->priv_pts));
if (!st->internal->priv_pts)
if (!sti->priv_pts)
sti->priv_pts = av_mallocz(sizeof(*sti->priv_pts));
if (!sti->priv_pts)
return AVERROR(ENOMEM);
if (den != AV_NOPTS_VALUE) {
if (den <= 0)
return AVERROR_INVALIDDATA;
frac_init(st->internal->priv_pts, 0, 0, den);
frac_init(sti->priv_pts, 0, 0, den);
}
}
@ -507,6 +509,7 @@ FF_DISABLE_DEPRECATION_WARNINGS
static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
FFFormatContext *const si = ffformatcontext(s);
AVStreamInternal *const sti = st->internal;
int delay = st->codecpar->video_delay;
int frame_size;
@ -523,7 +526,7 @@ static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *
if (s->debug & FF_FDEBUG_TS)
av_log(s, AV_LOG_DEBUG, "compute_muxer_pkt_fields: pts:%s dts:%s cur_dts:%s b:%d size:%d st:%d\n",
av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(st->internal->cur_dts), delay, pkt->size, pkt->stream_index);
av_ts2str(pkt->pts), av_ts2str(pkt->dts), av_ts2str(sti->cur_dts), delay, pkt->size, pkt->stream_index);
if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE && delay == 0)
pkt->pts = pkt->dts;
@ -537,28 +540,28 @@ static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *
}
pkt->dts =
// pkt->pts= st->cur_dts;
pkt->pts = st->internal->priv_pts->val;
pkt->pts = sti->priv_pts->val;
}
//calculate dts from pts
if (pkt->pts != AV_NOPTS_VALUE && pkt->dts == AV_NOPTS_VALUE && delay <= MAX_REORDER_DELAY) {
st->internal->pts_buffer[0] = pkt->pts;
for (int i = 1; i < delay + 1 && st->internal->pts_buffer[i] == AV_NOPTS_VALUE; i++)
st->internal->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
for (int i = 0; i<delay && st->internal->pts_buffer[i] > st->internal->pts_buffer[i + 1]; i++)
FFSWAP(int64_t, st->internal->pts_buffer[i], st->internal->pts_buffer[i + 1]);
sti->pts_buffer[0] = pkt->pts;
for (int i = 1; i < delay + 1 && sti->pts_buffer[i] == AV_NOPTS_VALUE; i++)
sti->pts_buffer[i] = pkt->pts + (i - delay - 1) * pkt->duration;
for (int i = 0; i<delay && sti->pts_buffer[i] > sti->pts_buffer[i + 1]; i++)
FFSWAP(int64_t, sti->pts_buffer[i], sti->pts_buffer[i + 1]);
pkt->dts = st->internal->pts_buffer[0];
pkt->dts = sti->pts_buffer[0];
}
if (st->internal->cur_dts && st->internal->cur_dts != AV_NOPTS_VALUE &&
if (sti->cur_dts && sti->cur_dts != AV_NOPTS_VALUE &&
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) &&
st->codecpar->codec_type != AVMEDIA_TYPE_SUBTITLE &&
st->codecpar->codec_type != AVMEDIA_TYPE_DATA &&
st->internal->cur_dts >= pkt->dts) || st->internal->cur_dts > pkt->dts)) {
sti->cur_dts >= pkt->dts) || sti->cur_dts > pkt->dts)) {
av_log(s, AV_LOG_ERROR,
"Application provided invalid, non monotonically increasing dts to muxer in stream %d: %s >= %s\n",
st->index, av_ts2str(st->internal->cur_dts), av_ts2str(pkt->dts));
st->index, av_ts2str(sti->cur_dts), av_ts2str(pkt->dts));
return AVERROR(EINVAL);
}
if (pkt->dts != AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE && pkt->pts < pkt->dts) {
@ -573,8 +576,8 @@ static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *
av_log(s, AV_LOG_DEBUG, "av_write_frame: pts2:%s dts2:%s\n",
av_ts2str(pkt->pts), av_ts2str(pkt->dts));
st->internal->cur_dts = pkt->dts;
st->internal->priv_pts->val = pkt->dts;
sti->cur_dts = pkt->dts;
sti->priv_pts->val = pkt->dts;
/* update pts */
switch (st->codecpar->codec_type) {
@ -586,12 +589,12 @@ static int compute_muxer_pkt_fields(AVFormatContext *s, AVStream *st, AVPacket *
/* HACK/FIXME, we skip the initial 0 size packets as they are most
* likely equal to the encoder delay, but it would be better if we
* had the real timestamps from the encoder */
if (frame_size >= 0 && (pkt->size || st->internal->priv_pts->num != st->internal->priv_pts->den >> 1 || st->internal->priv_pts->val)) {
frac_add(st->internal->priv_pts, (int64_t)st->time_base.den * frame_size);
if (frame_size >= 0 && (pkt->size || sti->priv_pts->num != sti->priv_pts->den >> 1 || sti->priv_pts->val)) {
frac_add(sti->priv_pts, (int64_t)st->time_base.den * frame_size);
}
break;
case AVMEDIA_TYPE_VIDEO:
frac_add(st->internal->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
frac_add(sti->priv_pts, (int64_t)st->time_base.den * st->time_base.num);
break;
}
return 0;
@ -642,6 +645,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
{
FFFormatContext *const si = ffformatcontext(s);
AVStream *const st = s->streams[pkt->stream_index];
AVStreamInternal *const sti = st->internal;
int ret;
// If the timestamp offsetting below is adjusted, adjust
@ -656,7 +660,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
}
if (s->avoid_negative_ts > 0) {
int64_t offset = st->internal->mux_ts_offset;
int64_t offset = sti->mux_ts_offset;
int64_t ts = si->avoid_negative_ts_use_pts ? pkt->pts : pkt->dts;
if (si->offset == AV_NOPTS_VALUE && ts != AV_NOPTS_VALUE &&
@ -666,7 +670,7 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt)
}
if (si->offset != AV_NOPTS_VALUE && !offset) {
offset = st->internal->mux_ts_offset =
offset = sti->mux_ts_offset =
av_rescale_q_rnd(si->offset,
si->offset_timebase,
st->time_base,
@ -739,13 +743,14 @@ static int check_packet(AVFormatContext *s, AVPacket *pkt)
static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
{
AVStreamInternal *const sti = st->internal;
#if !FF_API_COMPUTE_PKT_FIELDS2
/* sanitize the timestamps */
if (!(s->oformat->flags & AVFMT_NOTIMESTAMPS)) {
/* when there is no reordering (so dts is equal to pts), but
* only one of them is set, set the other as well */
if (!st->internal->reorder) {
if (!sti->reorder) {
if (pkt->pts == AV_NOPTS_VALUE && pkt->dts != AV_NOPTS_VALUE)
pkt->pts = pkt->dts;
if (pkt->dts == AV_NOPTS_VALUE && pkt->pts != AV_NOPTS_VALUE)
@ -761,13 +766,13 @@ static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
/* check that the dts are increasing (or at least non-decreasing,
* if the format allows it */
if (st->internal->cur_dts != AV_NOPTS_VALUE &&
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && st->internal->cur_dts >= pkt->dts) ||
st->internal->cur_dts > pkt->dts)) {
if (sti->cur_dts != AV_NOPTS_VALUE &&
((!(s->oformat->flags & AVFMT_TS_NONSTRICT) && sti->cur_dts >= pkt->dts) ||
sti->cur_dts > pkt->dts)) {
av_log(s, AV_LOG_ERROR,
"Application provided invalid, non monotonically increasing "
"dts to muxer in stream %d: %" PRId64 " >= %" PRId64 "\n",
st->index, st->internal->cur_dts, pkt->dts);
st->index, sti->cur_dts, pkt->dts);
return AVERROR(EINVAL);
}
@ -779,7 +784,7 @@ static int prepare_input_packet(AVFormatContext *s, AVStream *st, AVPacket *pkt)
}
#endif
/* update flags */
if (st->internal->is_intra_only)
if (sti->is_intra_only)
pkt->flags |= AV_PKT_FLAG_KEY;
return 0;
@ -794,6 +799,7 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
FFFormatContext *const si = ffformatcontext(s);
PacketList **next_point, *this_pktl;
AVStream *st = s->streams[pkt->stream_index];
AVStreamInternal *const sti = st->internal;
int chunked = s->max_chunk_size || s->max_chunk_duration;
this_pktl = av_malloc(sizeof(PacketList));
@ -810,27 +816,27 @@ int ff_interleave_add_packet(AVFormatContext *s, AVPacket *pkt,
av_packet_move_ref(&this_pktl->pkt, pkt);
pkt = &this_pktl->pkt;
if (st->internal->last_in_packet_buffer) {
next_point = &(st->internal->last_in_packet_buffer->next);
if (sti->last_in_packet_buffer) {
next_point = &(sti->last_in_packet_buffer->next);
} else {
next_point = &si->packet_buffer;
}
if (chunked) {
uint64_t max= av_rescale_q_rnd(s->max_chunk_duration, AV_TIME_BASE_Q, st->time_base, AV_ROUND_UP);
st->internal->interleaver_chunk_size += pkt->size;
st->internal->interleaver_chunk_duration += pkt->duration;
if ( (s->max_chunk_size && st->internal->interleaver_chunk_size > s->max_chunk_size)
|| (max && st->internal->interleaver_chunk_duration > max)) {
st->internal->interleaver_chunk_size = 0;
sti->interleaver_chunk_size += pkt->size;
sti->interleaver_chunk_duration += pkt->duration;
if ( (s->max_chunk_size && sti->interleaver_chunk_size > s->max_chunk_size)
|| (max && sti->interleaver_chunk_duration > max)) {
sti->interleaver_chunk_size = 0;
pkt->flags |= CHUNK_START;
if (max && st->internal->interleaver_chunk_duration > max) {
if (max && sti->interleaver_chunk_duration > max) {
int64_t syncoffset = (st->codecpar->codec_type == AVMEDIA_TYPE_VIDEO)*max/2;
int64_t syncto = av_rescale(pkt->dts + syncoffset, 1, max)*max - syncoffset;
st->internal->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
sti->interleaver_chunk_duration += (pkt->dts - syncto)/8 - max;
} else
st->internal->interleaver_chunk_duration = 0;
sti->interleaver_chunk_duration = 0;
}
}
if (*next_point) {
@ -855,7 +861,7 @@ next_non_null:
this_pktl->next = *next_point;
st->internal->last_in_packet_buffer = *next_point = this_pktl;
sti->last_in_packet_buffer = *next_point = this_pktl;
return 0;
}
@ -905,11 +911,14 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
}
for (unsigned i = 0; i < s->nb_streams; i++) {
if (s->streams[i]->internal->last_in_packet_buffer) {
const AVStream *const st = s->streams[i];
const AVStreamInternal *const sti = st->internal;
const AVCodecParameters *const par = st->codecpar;
if (sti->last_in_packet_buffer) {
++stream_count;
} else if (s->streams[i]->codecpar->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP8 &&
s->streams[i]->codecpar->codec_id != AV_CODEC_ID_VP9) {
} else if (par->codec_type != AVMEDIA_TYPE_ATTACHMENT &&
par->codec_id != AV_CODEC_ID_VP8 &&
par->codec_id != AV_CODEC_ID_VP9) {
++noninterleaved_count;
}
}
@ -929,14 +938,16 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
AV_TIME_BASE_Q);
for (unsigned i = 0; i < s->nb_streams; i++) {
const AVStream *const st = s->streams[i];
const AVStreamInternal *const sti = st->internal;
const PacketList *last = sti->last_in_packet_buffer;
int64_t last_dts;
const PacketList *last = s->streams[i]->internal->last_in_packet_buffer;
if (!last)
continue;
last_dts = av_rescale_q(last->pkt.dts,
s->streams[i]->time_base,
st->time_base,
AV_TIME_BASE_Q);
delta_dts = FFMAX(delta_dts, last_dts - top_dts);
}
@ -966,6 +977,7 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
PacketList *pktl = si->packet_buffer;
AVPacket *const top_pkt = &pktl->pkt;
AVStream *const st = s->streams[top_pkt->stream_index];
AVStreamInternal *const sti = st->internal;
int64_t top_dts = av_rescale_q(top_pkt->dts, st->time_base,
AV_TIME_BASE_Q);
@ -976,8 +988,8 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
if (!si->packet_buffer)
si->packet_buffer_end = NULL;
if (st->internal->last_in_packet_buffer == pktl)
st->internal->last_in_packet_buffer = NULL;
if (sti->last_in_packet_buffer == pktl)
sti->last_in_packet_buffer = NULL;
av_packet_unref(&pktl->pkt);
av_freep(&pktl);
@ -988,14 +1000,16 @@ int ff_interleave_packet_per_dts(AVFormatContext *s, AVPacket *out,
if (stream_count && flush) {
PacketList *pktl = si->packet_buffer;
AVStream *const st = s->streams[pktl->pkt.stream_index];
AVStreamInternal *const sti = st->internal;
*out = pktl->pkt;
si->packet_buffer = pktl->next;
if (!si->packet_buffer)
si->packet_buffer_end = NULL;
if (st->internal->last_in_packet_buffer == pktl)
st->internal->last_in_packet_buffer = NULL;
if (sti->last_in_packet_buffer == pktl)
sti->last_in_packet_buffer = NULL;
av_freep(&pktl);
return 1;
@ -1050,7 +1064,7 @@ static int interleave_packet(AVFormatContext *s, AVPacket *out, AVPacket *in, in
return ff_interleave_packet_per_dts(s, out, in, flush);
}
static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
static int check_bitstream(AVFormatContext *s, AVStreamInternal *sti, AVPacket *pkt)
{
int ret;
@ -1058,11 +1072,11 @@ static int check_bitstream(AVFormatContext *s, AVStream *st, AVPacket *pkt)
return 1;
if (s->oformat->check_bitstream) {
if (!st->internal->bitstream_checked) {
if (!sti->bitstream_checked) {
if ((ret = s->oformat->check_bitstream(s, pkt)) < 0)
return ret;
else if (ret == 1)
st->internal->bitstream_checked = 1;
sti->bitstream_checked = 1;
}
}
@ -1114,7 +1128,8 @@ static int write_packet_common(AVFormatContext *s, AVStream *st, AVPacket *pkt,
static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *pkt, int interleaved)
{
AVBSFContext *bsfc = st->internal->bsfc;
AVStreamInternal *const sti = st->internal;
AVBSFContext *const bsfc = sti->bsfc;
int ret;
if ((ret = av_bsf_send_packet(bsfc, pkt)) < 0) {
@ -1147,20 +1162,22 @@ static int write_packets_from_bsfs(AVFormatContext *s, AVStream *st, AVPacket *p
static int write_packets_common(AVFormatContext *s, AVPacket *pkt, int interleaved)
{
AVStream *st;
AVStreamInternal *sti;
int ret = check_packet(s, pkt);
if (ret < 0)
return ret;
st = s->streams[pkt->stream_index];
sti = st->internal;
ret = prepare_input_packet(s, st, pkt);
if (ret < 0)
return ret;
ret = check_bitstream(s, st, pkt);
ret = check_bitstream(s, sti, pkt);
if (ret < 0)
return ret;
if (st->internal->bsfc) {
if (sti->bsfc) {
return write_packets_from_bsfs(s, st, pkt, interleaved);
} else {
return write_packet_common(s, st, pkt, interleaved);

File diff suppressed because it is too large Load Diff