[FFmpeg-devel] [PATCH] avformat/pcmdec: add pts and dts calculation for pcmdec

Shiqi Zhu hiccupzhu at gmail.com
Tue May 14 14:33:14 EEST 2024


Signed-off-by: Shiqi Zhu <hiccupzhu at gmail.com>
---
 libavformat/pcmdec.c | 37 +++++++++++++++++++++++++++++++++++--
 1 file changed, 35 insertions(+), 2 deletions(-)

diff --git a/libavformat/pcmdec.c b/libavformat/pcmdec.c
index 2f6508b75a..d879aefaad 100644
--- a/libavformat/pcmdec.c
+++ b/libavformat/pcmdec.c
@@ -36,6 +36,7 @@ typedef struct PCMAudioDemuxerContext {
     AVClass *class;
     int sample_rate;
     AVChannelLayout ch_layout;
+    int64_t nb_samples;
 } PCMAudioDemuxerContext;
 
 static int pcm_read_header(AVFormatContext *s)
@@ -46,6 +47,7 @@ static int pcm_read_header(AVFormatContext *s)
     uint8_t *mime_type = NULL;
     int ret;
 
+    s1->nb_samples = 0;
     st = avformat_new_stream(s, NULL);
     if (!st)
         return AVERROR(ENOMEM);
@@ -104,6 +106,37 @@ static int pcm_read_header(AVFormatContext *s)
     return 0;
 }
 
+static int pcm_dec_read_packet(AVFormatContext *s, AVPacket *pkt)
+{
+    PCMAudioDemuxerContext *s1 = s->priv_data;
+    AVCodecParameters *par = s->streams[0]->codecpar;
+    int ret;
+
+    ret = ff_pcm_read_packet(s, pkt);
+    if (ret < 0)
+        return ret;
+
+    pkt->time_base = s->streams[0]->time_base;
+    pkt->dts = pkt->pts = s1->nb_samples;
+    s1->nb_samples += pkt->size / par->block_align;
+
+    return ret;
+}
+
+static int pcm_dec_read_seek(AVFormatContext *s,
+                             int stream_index, int64_t timestamp, int flags)
+{
+    PCMAudioDemuxerContext *s1 = s->priv_data;
+    int ret;
+
+    ret = ff_pcm_read_seek(s, stream_index, timestamp, flags);
+    if (ret < 0)
+        return ret;
+
+    s1->nb_samples = ffstream(s->streams[0])->cur_dts;
+    return ret;
+}
+
 static const AVOption pcm_options[] = {
     { "sample_rate", "", offsetof(PCMAudioDemuxerContext, sample_rate), AV_OPT_TYPE_INT, {.i64 = 44100}, 0, INT_MAX, AV_OPT_FLAG_DECODING_PARAM },
     { "ch_layout",   "", offsetof(PCMAudioDemuxerContext, ch_layout),   AV_OPT_TYPE_CHLAYOUT, {.str = "mono"}, 0, 0, AV_OPT_FLAG_DECODING_PARAM },
@@ -126,8 +159,8 @@ const FFInputFormat ff_pcm_ ## name_ ## _demuxer = {        \
     .p.priv_class   = &pcm_demuxer_class,                   \
     .priv_data_size = sizeof(PCMAudioDemuxerContext),       \
     .read_header    = pcm_read_header,                      \
-    .read_packet    = ff_pcm_read_packet,                   \
-    .read_seek      = ff_pcm_read_seek,                     \
+    .read_packet    = pcm_dec_read_packet,                  \
+    .read_seek      = pcm_dec_read_seek,                    \
     .raw_codec_id   = codec,                                \
     __VA_ARGS__                                             \
 };
-- 
2.34.1



More information about the ffmpeg-devel mailing list