Skip to content

Commit

Permalink
avformat: Avoid allocation for AVFormatInternal
Browse files Browse the repository at this point in the history
Do this by allocating AVFormatContext together with the data that is
currently in AVFormatInternal; or rather: Put AVFormatContext at the
beginning of a new structure called FFFormatContext (which encompasses
more than just the internal fields and is a proper context in its own
right, hence the name) and remove AVFormatInternal altogether.

The biggest simplifications occured in avformat_alloc_context(), where
one can now simply call avformat_free_context() in case of errors.

Signed-off-by: Andreas Rheinhardt <[email protected]>
  • Loading branch information
mkver committed Sep 17, 2021
1 parent dfbf417 commit fed0282
Show file tree
Hide file tree
Showing 32 changed files with 135 additions and 138 deletions.
13 changes: 7 additions & 6 deletions libavformat/asfdec_f.c
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ static int asf_read_header(AVFormatContext *s)
} else {
if (!s->keylen) {
if (!ff_guidcmp(&g, &ff_asf_content_encryption)) {
AVPacket *pkt = s->internal->parse_pkt;
AVPacket *const pkt = ffformatcontext(s)->parse_pkt;
unsigned int len;
av_log(s, AV_LOG_WARNING,
"DRM protected stream detected, decoding will likely fail!\n");
Expand Down Expand Up @@ -884,7 +884,7 @@ static int asf_get_packet(AVFormatContext *s, AVIOContext *pb)
if (asf->no_resync_search)
off = 3;
// else if (s->packet_size > 0 && !asf->uses_std_ecc)
// off = (avio_tell(pb) - s->internal->data_offset) % s->packet_size + 3;
// off = (avio_tell(pb) - ffformatcontext(s)->data_offset) % s->packet_size + 3;

c = d = e = -1;
while (off-- > 0) {
Expand Down Expand Up @@ -1429,6 +1429,7 @@ static int asf_read_close(AVFormatContext *s)
static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
int64_t *ppos, int64_t pos_limit)
{
FFFormatContext *const si = ffformatcontext(s);
ASFContext *asf = s->priv_data;
AVPacket pkt1, *pkt = &pkt1;
ASFStream *asf_st;
Expand All @@ -1441,9 +1442,9 @@ static int64_t asf_read_pts(AVFormatContext *s, int stream_index,
start_pos[i] = pos;

if (s->packet_size > 0)
pos = (pos + s->packet_size - 1 - s->internal->data_offset) /
pos = (pos + s->packet_size - 1 - si->data_offset) /
s->packet_size * s->packet_size +
s->internal->data_offset;
si->data_offset;
*ppos = pos;
if (avio_seek(s->pb, pos, SEEK_SET) < 0)
return AV_NOPTS_VALUE;
Expand Down Expand Up @@ -1525,7 +1526,7 @@ static int asf_build_simple_index(AVFormatContext *s, int stream_index)
for (i = 0; i < ict; i++) {
int pktnum = avio_rl32(s->pb);
int pktct = avio_rl16(s->pb);
int64_t pos = s->internal->data_offset + s->packet_size * (int64_t)pktnum;
int64_t pos = ffformatcontext(s)->data_offset + s->packet_size * (int64_t)pktnum;
int64_t index_pts = FFMAX(av_rescale(itime, i, 10000) - asf->hdr.preroll, 0);

if (avio_feof(s->pb)) {
Expand Down Expand Up @@ -1573,7 +1574,7 @@ static int asf_read_seek(AVFormatContext *s, int stream_index,
/* explicitly handle the case of seeking to 0 */
if (!pts) {
asf_reset_header(s);
avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
avio_seek(s->pb, ffformatcontext(s)->data_offset, SEEK_SET);
return 0;
}

Expand Down
8 changes: 0 additions & 8 deletions libavformat/avformat.h
Original file line number Diff line number Diff line change
Expand Up @@ -1090,8 +1090,6 @@ enum AVDurationEstimationMethod {
AVFMT_DURATION_FROM_BITRATE ///< Duration estimated from bitrate (less accurate)
};

typedef struct AVFormatInternal AVFormatInternal;

/**
* Format I/O context.
* New fields can be added to the end with minor version bumps.
Expand Down Expand Up @@ -1560,12 +1558,6 @@ typedef struct AVFormatContext {
*/
char *format_whitelist;

/**
* An opaque field for libavformat internal usage.
* Must not be accessed in any way by callers.
*/
AVFormatInternal *internal;

/**
* IO repositioned flag.
* This is set by avformat when the underlaying IO context read pointer
Expand Down
6 changes: 4 additions & 2 deletions libavformat/boadec.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ static int probe(const AVProbeData *p)
static int read_header(AVFormatContext *s)
{
AVStream *st = avformat_new_stream(s, NULL);
uint32_t data_offset;

if (!st)
return AVERROR(ENOMEM);

Expand All @@ -56,14 +58,14 @@ static int read_header(AVFormatContext *s)
st->codecpar->channels = avio_rl32(s->pb);
if (st->codecpar->channels > FF_SANE_NB_CHANNELS || st->codecpar->channels <= 0)
return AVERROR(ENOSYS);
s->internal->data_offset = avio_rl32(s->pb);
ffformatcontext(s)->data_offset = data_offset = avio_rl32(s->pb);
avio_r8(s->pb);
st->codecpar->block_align = avio_rl32(s->pb);
if (st->codecpar->block_align > INT_MAX / FF_SANE_NB_CHANNELS || st->codecpar->block_align <= 0)
return AVERROR_INVALIDDATA;
st->codecpar->block_align *= st->codecpar->channels;

avio_seek(s->pb, s->internal->data_offset, SEEK_SET);
avio_seek(s->pb, data_offset, SEEK_SET);

return 0;
}
Expand Down
3 changes: 1 addition & 2 deletions libavformat/codec2.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static int codec2_read_header(AVFormatContext *s)
return AVERROR_PATCHWELCOME;
}

s->internal->data_offset = CODEC2_HEADER_SIZE;
ffformatcontext(s)->data_offset = CODEC2_HEADER_SIZE;

return codec2_read_header_common(s, st);
}
Expand Down Expand Up @@ -255,7 +255,6 @@ static int codec2raw_read_header(AVFormatContext *s)
return ret;
}

s->internal->data_offset = 0;
codec2_make_extradata(st->codecpar->extradata, c2->mode);

return codec2_read_header_common(s, st);
Expand Down
8 changes: 4 additions & 4 deletions libavformat/dsfdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ static int dsf_read_header(AVFormatContext *s)
return AVERROR_INVALIDDATA;
dsf->data_size = avio_rl64(pb) - 12;
dsf->data_end += dsf->data_size + 12;
s->internal->data_offset = avio_tell(pb);

return 0;
}

static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
{
FFFormatContext *const si = ffformatcontext(s);
DSFContext *dsf = s->priv_data;
AVIOContext *pb = s->pb;
AVStream *st = s->streams[0];
Expand All @@ -161,7 +161,7 @@ static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
int last_packet = pos == (dsf->data_end - st->codecpar->block_align);

if (last_packet) {
int64_t data_pos = pos - s->internal->data_offset;
int64_t data_pos = pos - si->data_offset;
int64_t packet_size = dsf->audio_size - data_pos;
int64_t skip_size = dsf->data_size - data_pos - packet_size;
uint8_t *dst;
Expand All @@ -184,7 +184,7 @@ static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)

pkt->pos = pos;
pkt->stream_index = 0;
pkt->pts = (pos - s->internal->data_offset) / st->codecpar->channels;
pkt->pts = (pos - si->data_offset) / st->codecpar->channels;
pkt->duration = packet_size / st->codecpar->channels;
return 0;
}
Expand All @@ -194,7 +194,7 @@ static int dsf_read_packet(AVFormatContext *s, AVPacket *pkt)
return ret;

pkt->stream_index = 0;
pkt->pts = (pos - s->internal->data_offset) / st->codecpar->channels;
pkt->pts = (pos - si->data_offset) / st->codecpar->channels;
pkt->duration = st->codecpar->block_align / st->codecpar->channels;

return 0;
Expand Down
5 changes: 3 additions & 2 deletions libavformat/dv.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,9 +441,10 @@ static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
int64_t timestamp, int flags)
{
// FIXME: sys may be wrong if last dv_read_packet() failed (buffer is junk)
FFFormatContext *const si = ffformatcontext(s);
const int frame_size = c->sys->frame_size;
int64_t offset;
int64_t size = avio_size(s->pb) - s->internal->data_offset;
int64_t size = avio_size(s->pb) - si->data_offset;
int64_t max_offset = ((size - 1) / frame_size) * frame_size;

offset = frame_size * timestamp;
Expand All @@ -453,7 +454,7 @@ static int64_t dv_frame_offset(AVFormatContext *s, DVDemuxContext *c,
else if (offset < 0)
offset = 0;

return offset + s->internal->data_offset;
return offset + si->data_offset;
}

void ff_dv_offset_reset(DVDemuxContext *c, int64_t frame_offset)
Expand Down
3 changes: 2 additions & 1 deletion libavformat/flacdec.c
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ static int flac_probe(const AVProbeData *p)
static av_unused int64_t flac_read_timestamp(AVFormatContext *s, int stream_index,
int64_t *ppos, int64_t pos_limit)
{
AVPacket *pkt = s->internal->parse_pkt;
FFFormatContext *const si = ffformatcontext(s);
AVPacket *const pkt = si->parse_pkt;
AVStream *st = s->streams[stream_index];
AVCodecParserContext *parser;
int ret;
Expand Down
2 changes: 1 addition & 1 deletion libavformat/fsb.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ static int fsb_read_header(AVFormatContext *s)
}

avio_skip(pb, offset - avio_tell(pb));
s->internal->data_offset = avio_tell(pb);
ffformatcontext(s)->data_offset = avio_tell(pb);

avpriv_set_pts_info(st, 64, 1, par->sample_rate);

Expand Down
12 changes: 6 additions & 6 deletions libavformat/hca.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,22 +45,22 @@ static int hca_read_header(AVFormatContext *s)
uint32_t chunk;
uint16_t version;
uint32_t block_count;
uint16_t block_size;
uint16_t block_size, data_offset;
int ret;

avio_skip(pb, 4);
version = avio_rb16(pb);

s->internal->data_offset = avio_rb16(pb);
if (s->internal->data_offset <= 8)
data_offset = avio_rb16(pb);
if (data_offset <= 8)
return AVERROR_INVALIDDATA;

st = avformat_new_stream(s, NULL);
if (!st)
return AVERROR(ENOMEM);

par = st->codecpar;
ret = ff_alloc_extradata(par, s->internal->data_offset);
ret = ff_alloc_extradata(par, data_offset);
if (ret < 0)
return ret;

Expand All @@ -69,7 +69,7 @@ static int hca_read_header(AVFormatContext *s)
return AVERROR(EIO);
AV_WL32(par->extradata, MKTAG('H', 'C', 'A', 0));
AV_WB16(par->extradata + 4, version);
AV_WB16(par->extradata + 6, s->internal->data_offset);
AV_WB16(par->extradata + 6, data_offset);

bytestream2_init(&gb, par->extradata + 8, par->extradata_size - 8);

Expand Down Expand Up @@ -97,7 +97,7 @@ static int hca_read_header(AVFormatContext *s)
par->block_align = block_size;
st->duration = 1024 * block_count;

avio_seek(pb, s->internal->data_offset, SEEK_SET);
avio_seek(pb, data_offset, SEEK_SET);
avpriv_set_pts_info(st, 64, 1, par->sample_rate);

return 0;
Expand Down
14 changes: 12 additions & 2 deletions libavformat/internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,12 @@ typedef struct FFFrac {
} FFFrac;


struct AVFormatInternal {
typedef struct FFFormatContext {
/**
* The public context.
*/
AVFormatContext pub;

/**
* Number of streams relevant for interleaving.
* Muxing only.
Expand Down Expand Up @@ -173,7 +178,12 @@ struct AVFormatInternal {
* Set if chapter ids are strictly monotonic.
*/
int chapter_ids_monotonic;
};
} FFFormatContext;

static av_always_inline FFFormatContext *ffformatcontext(AVFormatContext *s)
{
return (FFFormatContext*)s;
}

struct AVStreamInternal {
/**
Expand Down
2 changes: 1 addition & 1 deletion libavformat/ipmovie.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ static int ipmovie_read_header(AVFormatContext *s)

if (chunk_type == CHUNK_VIDEO)
ipmovie->audio_type = AV_CODEC_ID_NONE; /* no audio */
else if (process_ipmovie_chunk(ipmovie, pb, s->internal->parse_pkt) != CHUNK_INIT_AUDIO) {
else if (process_ipmovie_chunk(ipmovie, pb, ffformatcontext(s)->parse_pkt) != CHUNK_INIT_AUDIO) {
return AVERROR_INVALIDDATA;
}

Expand Down
7 changes: 4 additions & 3 deletions libavformat/matroskadec.c
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ typedef struct MatroskaDemuxContext {
/* byte position of the segment inside the stream */
int64_t segment_start;

/* This packet coincides with AVFormatInternal.parse_pkt
/* This packet coincides with FFFormatContext.parse_pkt
* and is not owned by us. */
AVPacket *pkt;

Expand Down Expand Up @@ -2898,6 +2898,7 @@ static int matroska_parse_tracks(AVFormatContext *s)

static int matroska_read_header(AVFormatContext *s)
{
FFFormatContext *const si = ffformatcontext(s);
MatroskaDemuxContext *matroska = s->priv_data;
EbmlList *attachments_list = &matroska->attachments;
EbmlList *chapters_list = &matroska->chapters;
Expand Down Expand Up @@ -2944,7 +2945,7 @@ static int matroska_read_header(AVFormatContext *s)
}
ebml_free(ebml_syntax, &ebml);

matroska->pkt = s->internal->parse_pkt;
matroska->pkt = si->parse_pkt;

/* The next thing is a segment. */
pos = avio_tell(matroska->ctx->pb);
Expand All @@ -2961,7 +2962,7 @@ static int matroska_read_header(AVFormatContext *s)
}
/* Set data_offset as it might be needed later by seek_frame_generic. */
if (matroska->current_id == MATROSKA_ID_CLUSTER)
s->internal->data_offset = avio_tell(matroska->ctx->pb) - 4;
si->data_offset = avio_tell(matroska->ctx->pb) - 4;
matroska_execute_seekhead(matroska);

if (!matroska->time_scale)
Expand Down
5 changes: 3 additions & 2 deletions libavformat/matroskaenc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,7 @@ static int mkv_write_track(AVFormatContext *s, MatroskaMuxContext *mkv,
// if there is no mkv-specific codec ID, use VFW mode
put_ebml_string(pb, MATROSKA_ID_CODECID, "V_MS/VFW/FOURCC");
track->write_dts = 1;
s->internal->avoid_negative_ts_use_pts = 0;
ffformatcontext(s)->avoid_negative_ts_use_pts = 0;
}

subinfo = start_ebml_master(pb, MATROSKA_ID_TRACKVIDEO, 0);
Expand Down Expand Up @@ -2653,6 +2653,7 @@ static uint64_t mkv_get_uid(const mkv_track *tracks, int i, AVLFG *c)

static int mkv_init(struct AVFormatContext *s)
{
FFFormatContext *const si = ffformatcontext(s);
MatroskaMuxContext *mkv = s->priv_data;
AVLFG c;
unsigned nb_tracks = 0;
Expand All @@ -2674,7 +2675,7 @@ static int mkv_init(struct AVFormatContext *s)

if (s->avoid_negative_ts < 0) {
s->avoid_negative_ts = 1;
s->internal->avoid_negative_ts_use_pts = 1;
si->avoid_negative_ts_use_pts = 1;
}

if (!strcmp(s->oformat->name, "webm")) {
Expand Down
Loading

0 comments on commit fed0282

Please sign in to comment.