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

Andreas Rheinhardt andreas.rheinhardt at outlook.com
Tue May 14 14:40:48 EEST 2024


Shiqi Zhu:
> 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__                                             \
>  };

A quick test shows that PTS and DTS are already set generically for pcm
formats (unless the AVFMT_FLAG_NOFILLIN flag is set). If it is not in
your usecase, then you should provide details about this (preferably by
opening a ticket on trac).

- Andreas



More information about the ffmpeg-devel mailing list