1
mirror of https://github.com/mpv-player/mpv synced 2024-10-02 16:25:33 +02:00

demux: simplify handling of filepos field

demuxer->filepos contains the byte offset of the last read packet. This
is so that the player can estimate the current playback position, if no
proper timestamps are available. Simplify it to use demux_packet->pos in
the generic demuxer code, instead of bothering every demuxer
implementation about it.

(Note that this is still a bit incorrect: it relfects the position of
the last packet read by the demuxer, not that returned to the user. But
that was already broken, and is not that trivial to fix.)
This commit is contained in:
wm4 2013-11-16 21:04:28 +01:00
parent 0cdbc6db6e
commit a2a24b957e
5 changed files with 13 additions and 10 deletions

View File

@ -133,6 +133,7 @@ static struct demux_packet *create_packet(size_t len)
.pts = MP_NOPTS_VALUE,
.duration = -1,
.stream_pts = MP_NOPTS_VALUE,
.pos = -1,
};
return dp;
}
@ -339,6 +340,9 @@ int demuxer_add_packet(demuxer_t *demuxer, struct sh_stream *stream,
* appear. */
ds->eof = 0;
if (dp->pos >= 0)
demuxer->filepos = dp->pos;
mp_dbg(MSGT_DEMUXER, MSGL_DBG2,
"DEMUX: Append packet to %s, len=%d pts=%5.3f pos=%"PRIu64" "
"[packs: A=%d V=%d S=%d]\n", stream_type_name(stream->type),

View File

@ -707,8 +707,6 @@ static int read_more_av_packets(demuxer_t *demux)
if (priv->num_packets >= MAX_PKT_QUEUE)
return -1;
demux->filepos = stream_tell(demux->stream);
AVPacket *pkt = talloc(NULL, AVPacket);
if (av_read_frame(priv->avfc, pkt) < 0) {
talloc_free(pkt);
@ -795,7 +793,7 @@ static int demux_lavf_fill_buffer(demuxer_t *demux)
if (pkt->convergence_duration > 0)
dp->duration = pkt->convergence_duration * av_q2d(st->time_base);
}
dp->pos = demux->filepos;
dp->pos = pkt->pos;
dp->keyframe = pkt->flags & AV_PKT_FLAG_KEY;
// Use only one stream for stream_pts, otherwise PTS might be jumpy.
if (stream->type == STREAM_VIDEO) {

View File

@ -2004,7 +2004,7 @@ static void handle_realvideo(demuxer_t *demuxer, mkv_track_t *track,
track->stream->video->bih->biCompression,
&track->rv_kf_base, &track->rv_kf_pts, NULL);
}
dp->pos = demuxer->filepos;
dp->pos = mkv_d->last_filepos;
dp->keyframe = keyframe;
demuxer_add_packet(demuxer, track->stream, dp);
@ -2083,7 +2083,7 @@ static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track,
(track->ra_pts == mkv_d->last_pts) ? 0 : (mkv_d->last_pts);
track->ra_pts = mkv_d->last_pts;
if (track->sub_packet_cnt == 0)
track->audio_filepos = demuxer->filepos;
track->audio_filepos = mkv_d->last_filepos;
if (++(track->sub_packet_cnt) == sph) {
int apk_usize = track->stream->audio->wf->nBlockAlign;
track->sub_packet_cnt = 0;
@ -2108,7 +2108,7 @@ static void handle_realaudio(demuxer_t *demuxer, mkv_track_t *track,
dp->pts = mkv_d->last_pts;
track->ra_pts = mkv_d->last_pts;
dp->pos = demuxer->filepos;
dp->pos = mkv_d->last_filepos;
dp->keyframe = keyframe;
demuxer_add_packet(demuxer, track->stream, dp);
}
@ -2225,6 +2225,7 @@ struct block_info {
mkv_track_t *track;
bstr data;
void *alloc;
int64_t filepos;
};
static void free_block(struct block_info *block)
@ -2260,7 +2261,7 @@ static int read_block(demuxer_t *demuxer, struct block_info *block)
if (!block->alloc)
goto exit;
block->data = (bstr){block->alloc, length};
demuxer->filepos = stream_tell(s);
block->filepos = stream_tell(s);
if (stream_read(s, block->data.start, block->data.len) != block->data.len)
goto exit;
@ -2350,7 +2351,7 @@ static int handle_block(demuxer_t *demuxer, struct block_info *block_info)
if (use_this_block) {
mkv_d->last_pts = current_pts;
mkv_d->last_filepos = demuxer->filepos;
mkv_d->last_filepos = block_info->filepos;
for (int i = 0; i < laces; i++) {
bstr block = bstr_splice(data, 0, lace_size[i]);

View File

@ -28,7 +28,7 @@ typedef struct demux_packet {
double pts;
double duration;
double stream_pts;
int64_t pos; // position in index (AVI) or file (MPG)
int64_t pos; // position in source file byte stream
unsigned char *buffer;
bool keyframe;
struct demux_packet *next;

View File

@ -458,7 +458,7 @@ double get_current_pos_ratio(struct MPContext *mpctx, bool use_range)
} else {
struct stream *s = demuxer->stream;
int64_t size = s->end_pos - s->start_pos;
int64_t fpos = demuxer->filepos > 0 ?
int64_t fpos = demuxer->filepos >= 0 ?
demuxer->filepos : stream_tell(demuxer->stream);
if (size > 0)
ans = MPCLAMP((double)(fpos - s->start_pos) / size, 0, 1);