static int sws_flags = SWS_BICUBIC;
+typedef struct MyAVPacketList {
+ AVPacket pkt;
+ struct MyAVPacketList *next;
+ int serial;
+} MyAVPacketList;
+
typedef struct PacketQueue {
- AVPacketList *first_pkt, *last_pkt;
+ MyAVPacketList *first_pkt, *last_pkt;
int nb_packets;
int size;
int abort_request;
+ int serial;
SDL_mutex *mutex;
SDL_cond *cond;
} PacketQueue;
#define SUBPICTURE_QUEUE_SIZE 4
typedef struct VideoPicture {
- double pts; ///< presentation time stamp for this picture
- int64_t pos; ///< byte position in file
+ double pts; // presentation timestamp for this picture
+ int64_t pos; // byte position in file
int skip;
SDL_Overlay *bmp;
int width, height; /* source height & width */
AVRational sample_aspect_ratio;
int allocated;
int reallocate;
+ int serial;
#if CONFIG_AVFILTER
AVFilterBufferRef *picref;
int audio_stream;
int av_sync_type;
- double external_clock; /* external clock base */
- int64_t external_clock_time;
+ double external_clock; ///< external clock base
+ double external_clock_drift; ///< external clock base - time (av_gettime) at which we updated external_clock
+ int64_t external_clock_time; ///< last reference time
double audio_clock;
double audio_diff_cum; /* used for AV difference average computation */
int audio_write_buf_size;
AVPacket audio_pkt_temp;
AVPacket audio_pkt;
+ int audio_pkt_temp_serial;
struct AudioParams audio_src;
struct AudioParams audio_tgt;
struct SwrContext *swr_ctx;
double frame_last_returned_time;
double frame_last_filter_delay;
int64_t frame_last_dropped_pos;
- double video_clock; ///< pts of last decoded frame / predicted pts of next decoded frame
+ double video_clock; // pts of last decoded frame / predicted pts of next decoded frame
int video_stream;
AVStream *video_st;
PacketQueue videoq;
- double video_current_pts; ///< current displayed pts (different from video_clock if frame fifos are used)
- double video_current_pts_drift; ///< video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
- int64_t video_current_pos; ///< current displayed file pos
+ double video_current_pts; // current displayed pts (different from video_clock if frame fifos are used)
+ double video_current_pts_drift; // video_current_pts - time (av_gettime) at which we updated video_current_pts - used to have running video pts
+ int64_t video_current_pos; // current displayed file pos
VideoPicture pictq[VIDEO_PICTURE_QUEUE_SIZE];
int pictq_size, pictq_rindex, pictq_windex;
SDL_mutex *pictq_mutex;
int step;
#if CONFIG_AVFILTER
- AVFilterContext *in_video_filter; ///< the first filter in the video chain
- AVFilterContext *out_video_filter; ///< the last filter in the video chain
+ AVFilterContext *in_video_filter; // the first filter in the video chain
+ AVFilterContext *out_video_filter; // the last filter in the video chain
int use_dr1;
FrameBuffer *buffer_pool;
#endif
static int packet_queue_put_private(PacketQueue *q, AVPacket *pkt)
{
- AVPacketList *pkt1;
+ MyAVPacketList *pkt1;
if (q->abort_request)
return -1;
- pkt1 = av_malloc(sizeof(AVPacketList));
+ pkt1 = av_malloc(sizeof(MyAVPacketList));
if (!pkt1)
return -1;
pkt1->pkt = *pkt;
pkt1->next = NULL;
+ if (pkt == &flush_pkt)
+ q->serial++;
+ pkt1->serial = q->serial;
if (!q->last_pkt)
q->first_pkt = pkt1;
static void packet_queue_flush(PacketQueue *q)
{
- AVPacketList *pkt, *pkt1;
+ MyAVPacketList *pkt, *pkt1;
SDL_LockMutex(q->mutex);
for (pkt = q->first_pkt; pkt != NULL; pkt = pkt1) {
}
/* return < 0 if aborted, 0 if no packet and > 0 if packet. */
-static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block)
+static int packet_queue_get(PacketQueue *q, AVPacket *pkt, int block, int *serial)
{
- AVPacketList *pkt1;
+ MyAVPacketList *pkt1;
int ret;
SDL_LockMutex(q->mutex);
q->nb_packets--;
q->size -= pkt1->pkt.size + sizeof(*pkt1);
*pkt = pkt1->pkt;
+ if (serial)
+ *serial = pkt1->serial;
av_free(pkt1);
ret = 1;
break;
/* get the current external clock value */
static double get_external_clock(VideoState *is)
{
- int64_t ti;
- ti = av_gettime();
- return is->external_clock + ((ti - is->external_clock_time) * 1e-6);
+ if (is->paused) {
+ return is->external_clock;
+ } else {
+ return is->external_clock_drift + av_gettime() / 1000000.0;
+ }
}
-/* get the current master clock value */
-static double get_master_clock(VideoState *is)
-{
- double val;
-
+static int get_master_sync_type(VideoState *is) {
if (is->av_sync_type == AV_SYNC_VIDEO_MASTER) {
if (is->video_st)
- val = get_video_clock(is);
+ return AV_SYNC_VIDEO_MASTER;
else
- val = get_audio_clock(is);
+ return AV_SYNC_AUDIO_MASTER;
} else if (is->av_sync_type == AV_SYNC_AUDIO_MASTER) {
if (is->audio_st)
- val = get_audio_clock(is);
+ return AV_SYNC_AUDIO_MASTER;
else
- val = get_video_clock(is);
+ return AV_SYNC_EXTERNAL_CLOCK;
} else {
- val = get_external_clock(is);
+ return AV_SYNC_EXTERNAL_CLOCK;
+ }
+}
+
+/* get the current master clock value */
+static double get_master_clock(VideoState *is)
+{
+ double val;
+
+ switch (get_master_sync_type(is)) {
+ case AV_SYNC_VIDEO_MASTER:
+ val = get_video_clock(is);
+ break;
+ case AV_SYNC_AUDIO_MASTER:
+ val = get_audio_clock(is);
+ break;
+ default:
+ val = get_external_clock(is);
+ break;
}
return val;
}
+static void update_external_clock_pts(VideoState *is, double pts)
+{
+ is->external_clock_time = av_gettime();
+ is->external_clock = pts;
+ is->external_clock_drift = pts - is->external_clock_time / 1000000.0;
+}
+
+static void check_external_clock_sync(VideoState *is, double pts) {
+ if (fabs(get_external_clock(is) - pts) > AV_NOSYNC_THRESHOLD) {
+ update_external_clock_pts(is, pts);
+ }
+}
+
/* seek in the stream */
static void stream_seek(VideoState *is, int64_t pos, int64_t rel, int seek_by_bytes)
{
}
is->video_current_pts_drift = is->video_current_pts - av_gettime() / 1000000.0;
}
+ update_external_clock_pts(is, get_external_clock(is));
is->paused = !is->paused;
}
double sync_threshold, diff;
/* update delay to follow master synchronisation source */
- if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) ||
- is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
+ if (get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER) {
/* if video is slave, we try to correct big delays by
duplicating or deleting a frame */
diff = get_video_clock(is) - get_master_clock(is);
}
}
-static void update_video_pts(VideoState *is, double pts, int64_t pos) {
+static void update_video_pts(VideoState *is, double pts, int64_t pos, int serial) {
double time = av_gettime() / 1000000.0;
/* update current video pts */
is->video_current_pts = pts;
is->video_current_pts_drift = is->video_current_pts - time;
is->video_current_pos = pos;
is->frame_last_pts = pts;
+ if (is->videoq.serial == serial)
+ check_external_clock_sync(is, is->video_current_pts);
}
/* called to display each frame */
if (is->pictq_size == 0) {
SDL_LockMutex(is->pictq_mutex);
if (is->frame_last_dropped_pts != AV_NOPTS_VALUE && is->frame_last_dropped_pts > is->frame_last_pts) {
- update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos);
+ update_video_pts(is, is->frame_last_dropped_pts, is->frame_last_dropped_pos, 0);
is->frame_last_dropped_pts = AV_NOPTS_VALUE;
}
SDL_UnlockMutex(is->pictq_mutex);
is->frame_timer += delay * FFMAX(1, floor((time-is->frame_timer) / delay));
SDL_LockMutex(is->pictq_mutex);
- update_video_pts(is, vp->pts, vp->pos);
+ update_video_pts(is, vp->pts, vp->pos, vp->serial);
SDL_UnlockMutex(is->pictq_mutex);
if (is->pictq_size > 1) {
VideoPicture *nextvp = &is->pictq[(is->pictq_rindex + 1) % VIDEO_PICTURE_QUEUE_SIZE];
duration = nextvp->pts - vp->pts;
- if((framedrop>0 || (framedrop && is->audio_st)) && time > is->frame_timer + duration){
+ if((framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) && time > is->frame_timer + duration){
is->frame_drops_late++;
pictq_next_picture(is);
goto retry;
SDL_UnlockMutex(is->pictq_mutex);
}
-static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos)
+static int queue_picture(VideoState *is, AVFrame *src_frame, double pts1, int64_t pos, int serial)
{
VideoPicture *vp;
double frame_delay, pts = pts1;
vp->pts = pts;
vp->pos = pos;
vp->skip = 0;
+ vp->serial = serial;
/* now we can update the picture count */
if (++is->pictq_windex == VIDEO_PICTURE_QUEUE_SIZE)
return 0;
}
-static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt)
+static int get_video_frame(VideoState *is, AVFrame *frame, int64_t *pts, AVPacket *pkt, int *serial)
{
int got_picture, i;
- if (packet_queue_get(&is->videoq, pkt, 1) < 0)
+ if (packet_queue_get(&is->videoq, pkt, 1, serial) < 0)
return -1;
if (pkt->data == flush_pkt.data) {
*pts = 0;
}
- if (((is->av_sync_type == AV_SYNC_AUDIO_MASTER && is->audio_st) || is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK) &&
- (framedrop>0 || (framedrop && is->audio_st))) {
+ if (framedrop>0 || (framedrop && get_master_sync_type(is) != AV_SYNC_VIDEO_MASTER)) {
SDL_LockMutex(is->pictq_mutex);
if (is->frame_last_pts != AV_NOPTS_VALUE && *pts) {
double clockdiff = get_video_clock(is) - get_master_clock(is);
"video_size=%dx%d:pix_fmt=%d:time_base=%d/%d:pixel_aspect=%d/%d",
codec->width, codec->height, codec->pix_fmt,
is->video_st->time_base.num, is->video_st->time_base.den,
- codec->sample_aspect_ratio.num, codec->sample_aspect_ratio.den);
+ codec->sample_aspect_ratio.num, FFMAX(codec->sample_aspect_ratio.den, 1));
if ((ret = avfilter_graph_create_filter(&filt_src,
avfilter_get_by_name("buffer"),
int64_t pts_int = AV_NOPTS_VALUE, pos = -1;
double pts;
int ret;
+ int serial = 0;
#if CONFIG_AVFILTER
AVCodecContext *codec = is->video_st->codec;
avcodec_get_frame_defaults(frame);
av_free_packet(&pkt);
- ret = get_video_frame(is, frame, &pts_int, &pkt);
+ ret = get_video_frame(is, frame, &pts_int, &pkt, &serial);
if (ret < 0)
goto the_end;
is->video_st->time_base.num, is->video_st->time_base.den, pts_int);
}
pts = pts_int * av_q2d(is->video_st->time_base);
- ret = queue_picture(is, frame, pts, pos);
+ ret = queue_picture(is, frame, pts, pos, serial);
}
#else
pts = pts_int * av_q2d(is->video_st->time_base);
- ret = queue_picture(is, frame, pts, pkt.pos);
+ ret = queue_picture(is, frame, pts, pkt.pos, serial);
#endif
if (ret < 0)
while (is->paused && !is->subtitleq.abort_request) {
SDL_Delay(10);
}
- if (packet_queue_get(&is->subtitleq, pkt, 1) < 0)
+ if (packet_queue_get(&is->subtitleq, pkt, 1, NULL) < 0)
break;
if (pkt->data == flush_pkt.data) {
int wanted_nb_samples = nb_samples;
/* if not master, then we try to remove or add samples to correct the clock */
- if (((is->av_sync_type == AV_SYNC_VIDEO_MASTER && is->video_st) ||
- is->av_sync_type == AV_SYNC_EXTERNAL_CLOCK)) {
+ if (get_master_sync_type(is) != AV_SYNC_AUDIO_MASTER) {
double diff, avg_diff;
int min_nb_samples, max_nb_samples;
diff = get_audio_clock(is) - get_master_clock(is);
- if (diff < AV_NOSYNC_THRESHOLD) {
+ if (fabs(diff) < AV_NOSYNC_THRESHOLD) {
is->audio_diff_cum = diff + is->audio_diff_avg_coef * is->audio_diff_cum;
if (is->audio_diff_avg_count < AUDIO_DIFF_AVG_NB) {
/* not enough measures to have a correct estimate */
flush_complete = 1;
continue;
}
- data_size = av_samples_get_buffer_size(NULL, dec->channels,
+ data_size = av_samples_get_buffer_size(NULL, is->frame->channels,
is->frame->nb_samples,
- dec->sample_fmt, 1);
+ is->frame->format, 1);
dec_channel_layout =
- (dec->channel_layout && dec->channels == av_get_channel_layout_nb_channels(dec->channel_layout)) ?
- dec->channel_layout : av_get_default_channel_layout(dec->channels);
+ (is->frame->channel_layout && is->frame->channels == av_get_channel_layout_nb_channels(is->frame->channel_layout)) ?
+ is->frame->channel_layout : av_get_default_channel_layout(is->frame->channels);
wanted_nb_samples = synchronize_audio(is, is->frame->nb_samples);
- if (dec->sample_fmt != is->audio_src.fmt ||
- dec_channel_layout != is->audio_src.channel_layout ||
- dec->sample_rate != is->audio_src.freq ||
- (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
+ if (is->frame->format != is->audio_src.fmt ||
+ dec_channel_layout != is->audio_src.channel_layout ||
+ is->frame->sample_rate != is->audio_src.freq ||
+ (wanted_nb_samples != is->frame->nb_samples && !is->swr_ctx)) {
swr_free(&is->swr_ctx);
is->swr_ctx = swr_alloc_set_opts(NULL,
is->audio_tgt.channel_layout, is->audio_tgt.fmt, is->audio_tgt.freq,
- dec_channel_layout, dec->sample_fmt, dec->sample_rate,
+ dec_channel_layout, is->frame->format, is->frame->sample_rate,
0, NULL);
if (!is->swr_ctx || swr_init(is->swr_ctx) < 0) {
fprintf(stderr, "Cannot create sample rate converter for conversion of %d Hz %s %d channels to %d Hz %s %d channels!\n",
- dec->sample_rate, av_get_sample_fmt_name(dec->sample_fmt), dec->channels,
+ is->frame->sample_rate, av_get_sample_fmt_name(is->frame->format), (int)is->frame->channels,
is->audio_tgt.freq, av_get_sample_fmt_name(is->audio_tgt.fmt), is->audio_tgt.channels);
break;
}
is->audio_src.channel_layout = dec_channel_layout;
- is->audio_src.channels = dec->channels;
- is->audio_src.freq = dec->sample_rate;
- is->audio_src.fmt = dec->sample_fmt;
+ is->audio_src.channels = is->frame->channels;
+ is->audio_src.freq = is->frame->sample_rate;
+ is->audio_src.fmt = is->frame->format;
}
if (is->swr_ctx) {
uint8_t *out[] = {is->audio_buf2};
int out_count = sizeof(is->audio_buf2) / is->audio_tgt.channels / av_get_bytes_per_sample(is->audio_tgt.fmt);
if (wanted_nb_samples != is->frame->nb_samples) {
- if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / dec->sample_rate,
- wanted_nb_samples * is->audio_tgt.freq / dec->sample_rate) < 0) {
+ if (swr_set_compensation(is->swr_ctx, (wanted_nb_samples - is->frame->nb_samples) * is->audio_tgt.freq / is->frame->sample_rate,
+ wanted_nb_samples * is->audio_tgt.freq / is->frame->sample_rate) < 0) {
fprintf(stderr, "swr_set_compensation() failed\n");
break;
}
pts = is->audio_clock;
*pts_ptr = pts;
is->audio_clock += (double)data_size /
- (dec->channels * dec->sample_rate * av_get_bytes_per_sample(dec->sample_fmt));
+ (is->frame->channels * is->frame->sample_rate * av_get_bytes_per_sample(is->frame->format));
#ifdef DEBUG
{
static double last_clock;
SDL_CondSignal(is->continue_read_thread);
/* read next packet */
- if ((new_packet = packet_queue_get(&is->audioq, pkt, 1)) < 0)
+ if ((new_packet = packet_queue_get(&is->audioq, pkt, 1, &is->audio_pkt_temp_serial)) < 0)
return -1;
if (pkt->data == flush_pkt.data) {
/* Let's assume the audio driver that is used by SDL has two periods. */
is->audio_current_pts = is->audio_clock - (double)(2 * is->audio_hw_buf_size + is->audio_write_buf_size) / bytes_per_sec;
is->audio_current_pts_drift = is->audio_current_pts - audio_callback_time / 1000000.0;
+ if (is->audioq.serial == is->audio_pkt_temp_serial)
+ check_external_clock_sync(is, is->audio_current_pts);
}
static int audio_open(void *opaque, int64_t wanted_channel_layout, int wanted_nb_channels, int wanted_sample_rate, struct AudioParams *audio_hw_params)
packet_queue_put(&is->videoq, &flush_pkt);
}
}
+ update_external_clock_pts(is, (seek_target + ic->start_time) / (double)AV_TIME_BASE);
is->seek_req = 0;
eof = 0;
}
is->continue_read_thread = SDL_CreateCond();
+ update_external_clock_pts(is, 0.0);
+ is->audio_current_pts_drift = -av_gettime() / 1000000.0;
+ is->video_current_pts_drift = is->audio_current_pts_drift;
is->av_sync_type = av_sync_type;
is->read_tid = SDL_CreateThread(read_thread, is);
if (!is->read_tid) {