[FFmpeg-devel] [PATCH 1/2] lavd/alsa: fix timestamp calculation

Lukasz Marek lukasz.m.luki at gmail.com
Sat Oct 26 01:40:07 CEST 2013


Current implementation didn't include duration of
last processed packet.
Device may return negative timestamps without
this correction.

Signed-off-by: Lukasz Marek <lukasz.m.luki at gmail.com>
---
 libavdevice/alsa-audio-enc.c |   12 +++++++++---
 libavdevice/alsa-audio.h     |    1 +
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/libavdevice/alsa-audio-enc.c b/libavdevice/alsa-audio-enc.c
index 0f4e4a2..4d0e17b 100644
--- a/libavdevice/alsa-audio-enc.c
+++ b/libavdevice/alsa-audio-enc.c
@@ -79,6 +79,13 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
     int size     = pkt->size;
     uint8_t *buf = pkt->data;
 
+    if (!(s->timestamp_diff = pkt->duration)) {
+        AVStream *st = s1->streams[0];
+        AVCodecContext *codec_ctx = st->codec;
+        /*XXX: no need to recalculate: 1/sample_rate == avprinv_set_pts_info() */
+        s->timestamp_diff = pkt->size / (av_get_bytes_per_sample(codec_ctx->sample_fmt) * codec_ctx->channels);
+    }
+
     size /= s->frame_size;
     if (s->reorder_func) {
         if (size > s->reorder_buf_size)
@@ -104,15 +111,14 @@ static int audio_write_packet(AVFormatContext *s1, AVPacket *pkt)
     return 0;
 }
 
-static void
-audio_get_output_timestamp(AVFormatContext *s1, int stream,
+static void audio_get_output_timestamp(AVFormatContext *s1, int stream,
     int64_t *dts, int64_t *wall)
 {
     AlsaData *s  = s1->priv_data;
     snd_pcm_sframes_t delay = 0;
     *wall = av_gettime();
     snd_pcm_delay(s->h, &delay);
-    *dts = s1->streams[0]->cur_dts - delay;
+    *dts = s1->streams[0]->cur_dts + s->timestamp_diff - delay;
 }
 
 AVOutputFormat ff_alsa_muxer = {
diff --git a/libavdevice/alsa-audio.h b/libavdevice/alsa-audio.h
index 44b7c72..b9670b9 100644
--- a/libavdevice/alsa-audio.h
+++ b/libavdevice/alsa-audio.h
@@ -57,6 +57,7 @@ typedef struct AlsaData {
     void (*reorder_func)(const void *, void *, int);
     void *reorder_buf;
     int reorder_buf_size; ///< in frames
+    int64_t timestamp_diff; ///< duration of last packet, need to calculate timestamp
 } AlsaData;
 
 /**
-- 
1.7.10.4



More information about the ffmpeg-devel mailing list