diff mbox

[FFmpeg-devel] avformat/hlsenc: implement initial_offset

Message ID 1473299262-17974-1-git-send-email-ffmpeg@tmm1.net
State Changes Requested
Headers show

Commit Message

Aman Gupta Sept. 8, 2016, 1:47 a.m. UTC
From: Aman Gupta <aman@tmm1.net>

---
 doc/muxers.texi      |  4 ++++
 libavformat/hlsenc.c | 13 +++++++++++--
 2 files changed, 15 insertions(+), 2 deletions(-)

Comments

Steven Liu Sept. 8, 2016, 2:27 a.m. UTC | #1
2016-09-08 9:47 GMT+08:00 Aman Gupta <ffmpeg@tmm1.net>:

> From: Aman Gupta <aman@tmm1.net>
>
> ---
>  doc/muxers.texi      |  4 ++++
>  libavformat/hlsenc.c | 13 +++++++++++--
>  2 files changed, 15 insertions(+), 2 deletions(-)
>
> diff --git a/doc/muxers.texi b/doc/muxers.texi
> index fd7ee50..e88dbf8 100644
> --- a/doc/muxers.texi
> +++ b/doc/muxers.texi
> @@ -389,6 +389,10 @@ to @var{wrap}.
>  Start the playlist sequence number from @var{number}. Default value is
>  0.
>
> +@item initial_offset @var{offset}
> +Specify timestamp offset to apply to the output packet timestamps. The
> +argument must be a time duration specification, and defaults to 0.
> +
>  @item hls_allow_cache @var{allowcache}
>  Explicitly set whether the client MAY (1) or MUST NOT (0) cache media
> segments.
>
> diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
> index 1846d9d..be58db8 100644
> --- a/libavformat/hlsenc.c
> +++ b/libavformat/hlsenc.c
> @@ -90,6 +90,7 @@ typedef struct HLSContext {
>      uint32_t pl_type;      // enum PlaylistType
>      char *segment_filename;
>
> +    int64_t initial_offset; ///< initial timestamps offset, expressed in
> microseconds
>      int use_localtime;      ///< flag to expand filename with localtime
>      int use_localtime_mkdir;///< flag to mkdir dirname in timebased
> filename
>      int allowcache;
> @@ -795,7 +796,7 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>      HLSContext *hls = s->priv_data;
>      AVFormatContext *oc = NULL;
>      AVStream *st = s->streams[pkt->stream_index];
> -    int64_t end_pts = hls->recording_time * hls->number;
> +    int64_t end_pts = hls->recording_time * hls->number, offset;
>      int is_ref_pkt = 1;
>      int ret, can_split = 1;
>      int stream_index = 0;
> @@ -871,7 +872,14 @@ static int hls_write_packet(AVFormatContext *s,
> AVPacket *pkt)
>              return ret;
>      }
>
> -    ret = ff_write_chained(oc, stream_index, pkt, s, 0);
> +    /* compute new timestamps */
> +    offset = av_rescale_q(hls->initial_offset, AV_TIME_BASE_Q,
> st->time_base);
> +    if (pkt->pts != AV_NOPTS_VALUE)
> +        pkt->pts += offset;
> +    if (pkt->dts != AV_NOPTS_VALUE)
> +        pkt->dts += offset;
> +
> +    ret = ff_write_chained(oc, stream_index, pkt, s, hls->initial_offset
> ? 1 : 0);
>
>      return ret;
>  }
> @@ -939,6 +947,7 @@ static const AVOption options[] = {
>      {"event", "EVENT playlist", 0, AV_OPT_TYPE_CONST, {.i64 =
> PLAYLIST_TYPE_EVENT }, INT_MIN, INT_MAX, E, "pl_type" },
>      {"vod", "VOD playlist", 0, AV_OPT_TYPE_CONST, {.i64 =
> PLAYLIST_TYPE_VOD }, INT_MIN, INT_MAX, E, "pl_type" },
>      {"method", "set the HTTP method", OFFSET(method), AV_OPT_TYPE_STRING,
> {.str = NULL},  0, 0,    E},
> +    {"initial_offset", "set initial timestamp offset",
> OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX,
> INT64_MAX, E },
>
>      { NULL },
>  };
> --
> 2.8.1
>
>
LGTM
Michael Niedermayer Sept. 8, 2016, 9:46 a.m. UTC | #2
On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> From: Aman Gupta <aman@tmm1.net>
> 
> ---
>  doc/muxers.texi      |  4 ++++
>  libavformat/hlsenc.c | 13 +++++++++++--
>  2 files changed, 15 insertions(+), 2 deletions(-)

Isnt this redundant with
-output_ts_offset
?
or how do they differ ?

[...]
Steven Liu Sept. 8, 2016, 9:55 a.m. UTC | #3
2016-09-08 17:46 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc>:

> On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> > From: Aman Gupta <aman@tmm1.net>
> >
> > ---
> >  doc/muxers.texi      |  4 ++++
> >  libavformat/hlsenc.c | 13 +++++++++++--
> >  2 files changed, 15 insertions(+), 2 deletions(-)
>
> Isnt this redundant with
> -output_ts_offset
> ?
> or how do they differ ?
>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> Into a blind darkness they enter who follow after the Ignorance,
> they as if into a greater darkness enter who devote themselves
> to the Knowledge alone. -- Isha Upanishad
>
>
>
Maybe just copy the code from segment.c to hlsenc.c .
Aman Gupta Sept. 8, 2016, 12:10 p.m. UTC | #4
On Thu, Sep 8, 2016 at 2:55 AM, Steven Liu <lingjiujianke@gmail.com> wrote:

> 2016-09-08 17:46 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc>:
>
> > On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> > > From: Aman Gupta <aman@tmm1.net>
> > >
> > > ---
> > >  doc/muxers.texi      |  4 ++++
> > >  libavformat/hlsenc.c | 13 +++++++++++--
> > >  2 files changed, 15 insertions(+), 2 deletions(-)
> >
> > Isnt this redundant with
> > -output_ts_offset
> > ?
> > or how do they differ ?
> >
> > [...]
> > --
> > Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
> >
> > Into a blind darkness they enter who follow after the Ignorance,
> > they as if into a greater darkness enter who devote themselves
> > to the Knowledge alone. -- Isha Upanishad
> >
> >
> >
> Maybe just copy the code from segment.c to hlsenc.c .
>

Yes, I copied directly from segment.c because I wanted the same feature in
the hls encoder.

Was not aware of -output_ts_offset. Perhaps that supersedes the
-initial_offset option?

Aman



> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel@ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>
Michael Niedermayer Sept. 8, 2016, 12:31 p.m. UTC | #5
On Thu, Sep 08, 2016 at 05:10:14AM -0700, Aman Gupta wrote:
> On Thu, Sep 8, 2016 at 2:55 AM, Steven Liu <lingjiujianke@gmail.com> wrote:
> 
> > 2016-09-08 17:46 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc>:
> >
> > > On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> > > > From: Aman Gupta <aman@tmm1.net>
> > > >
> > > > ---
> > > >  doc/muxers.texi      |  4 ++++
> > > >  libavformat/hlsenc.c | 13 +++++++++++--
> > > >  2 files changed, 15 insertions(+), 2 deletions(-)
> > >
> > > Isnt this redundant with
> > > -output_ts_offset
> > > ?
> > > or how do they differ ?
> > >
> > > [...]
> > > --
> > > Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
> > >
> > > Into a blind darkness they enter who follow after the Ignorance,
> > > they as if into a greater darkness enter who devote themselves
> > > to the Knowledge alone. -- Isha Upanishad
> > >
> > >
> > >
> > Maybe just copy the code from segment.c to hlsenc.c .
> >
> 
> Yes, I copied directly from segment.c because I wanted the same feature in
> the hls encoder.
> 
> Was not aware of -output_ts_offset. Perhaps that supersedes the
> -initial_offset option?

does output_ts_offset work as replacement for initial_offset ?
a generic solution working with all muxer would be better/simper

[...]
Steven Liu Sept. 8, 2016, 12:42 p.m. UTC | #6
2016-09-08 20:31 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc>:

> On Thu, Sep 08, 2016 at 05:10:14AM -0700, Aman Gupta wrote:
> > On Thu, Sep 8, 2016 at 2:55 AM, Steven Liu <lingjiujianke@gmail.com>
> wrote:
> >
> > > 2016-09-08 17:46 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc
> >:
> > >
> > > > On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> > > > > From: Aman Gupta <aman@tmm1.net>
> > > > >
> > > > > ---
> > > > >  doc/muxers.texi      |  4 ++++
> > > > >  libavformat/hlsenc.c | 13 +++++++++++--
> > > > >  2 files changed, 15 insertions(+), 2 deletions(-)
> > > >
> > > > Isnt this redundant with
> > > > -output_ts_offset
> > > > ?
> > > > or how do they differ ?
> > > >
> > > > [...]
> > > > --
> > > > Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC7
> 87040B0FAB
> > > >
> > > > Into a blind darkness they enter who follow after the Ignorance,
> > > > they as if into a greater darkness enter who devote themselves
> > > > to the Knowledge alone. -- Isha Upanishad
> > > >
> > > >
> > > >
> > > Maybe just copy the code from segment.c to hlsenc.c .
> > >
> >
> > Yes, I copied directly from segment.c because I wanted the same feature
> in
> > the hls encoder.
> >
> > Was not aware of -output_ts_offset. Perhaps that supersedes the
> > -initial_offset option?
>
> does output_ts_offset work as replacement for initial_offset ?
> a generic solution working with all muxer would be better/simper
>
> [...]
> --
> Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
>
> The bravest are surely those who have the clearest vision
> of what is before them, glory and danger alike, and yet
> notwithstanding go out to meet it. -- Thucydides
>
>
Aha, new version have output_ts_offset in mux.c, perhaps this option should
be ignored.

And segment.c give a deprecated message and replace by output_ts_offset?
Michael Niedermayer Sept. 8, 2016, 1:05 p.m. UTC | #7
On Thu, Sep 08, 2016 at 08:42:05PM +0800, Steven Liu wrote:
> 2016-09-08 20:31 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc>:
> 
> > On Thu, Sep 08, 2016 at 05:10:14AM -0700, Aman Gupta wrote:
> > > On Thu, Sep 8, 2016 at 2:55 AM, Steven Liu <lingjiujianke@gmail.com>
> > wrote:
> > >
> > > > 2016-09-08 17:46 GMT+08:00 Michael Niedermayer <michael@niedermayer.cc
> > >:
> > > >
> > > > > On Thu, Sep 08, 2016 at 10:47:42AM +0900, Aman Gupta wrote:
> > > > > > From: Aman Gupta <aman@tmm1.net>
> > > > > >
> > > > > > ---
> > > > > >  doc/muxers.texi      |  4 ++++
> > > > > >  libavformat/hlsenc.c | 13 +++++++++++--
> > > > > >  2 files changed, 15 insertions(+), 2 deletions(-)
> > > > >
> > > > > Isnt this redundant with
> > > > > -output_ts_offset
> > > > > ?
> > > > > or how do they differ ?
> > > > >
> > > > > [...]
> > > > > --
> > > > > Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC7
> > 87040B0FAB
> > > > >
> > > > > Into a blind darkness they enter who follow after the Ignorance,
> > > > > they as if into a greater darkness enter who devote themselves
> > > > > to the Knowledge alone. -- Isha Upanishad
> > > > >
> > > > >
> > > > >
> > > > Maybe just copy the code from segment.c to hlsenc.c .
> > > >
> > >
> > > Yes, I copied directly from segment.c because I wanted the same feature
> > in
> > > the hls encoder.
> > >
> > > Was not aware of -output_ts_offset. Perhaps that supersedes the
> > > -initial_offset option?
> >
> > does output_ts_offset work as replacement for initial_offset ?
> > a generic solution working with all muxer would be better/simper
> >
> > [...]
> > --
> > Michael     GnuPG fingerprint: 9FF2128B147EF6730BADF133611EC787040B0FAB
> >
> > The bravest are surely those who have the clearest vision
> > of what is before them, glory and danger alike, and yet
> > notwithstanding go out to meet it. -- Thucydides
> >
> >
> Aha, new version have output_ts_offset in mux.c, perhaps this option should
> be ignored.
> 

> And segment.c give a deprecated message and replace by output_ts_offset?

yes if t works

[...]
diff mbox

Patch

diff --git a/doc/muxers.texi b/doc/muxers.texi
index fd7ee50..e88dbf8 100644
--- a/doc/muxers.texi
+++ b/doc/muxers.texi
@@ -389,6 +389,10 @@  to @var{wrap}.
 Start the playlist sequence number from @var{number}. Default value is
 0.
 
+@item initial_offset @var{offset}
+Specify timestamp offset to apply to the output packet timestamps. The
+argument must be a time duration specification, and defaults to 0.
+
 @item hls_allow_cache @var{allowcache}
 Explicitly set whether the client MAY (1) or MUST NOT (0) cache media segments.
 
diff --git a/libavformat/hlsenc.c b/libavformat/hlsenc.c
index 1846d9d..be58db8 100644
--- a/libavformat/hlsenc.c
+++ b/libavformat/hlsenc.c
@@ -90,6 +90,7 @@  typedef struct HLSContext {
     uint32_t pl_type;      // enum PlaylistType
     char *segment_filename;
 
+    int64_t initial_offset; ///< initial timestamps offset, expressed in microseconds
     int use_localtime;      ///< flag to expand filename with localtime
     int use_localtime_mkdir;///< flag to mkdir dirname in timebased filename
     int allowcache;
@@ -795,7 +796,7 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
     HLSContext *hls = s->priv_data;
     AVFormatContext *oc = NULL;
     AVStream *st = s->streams[pkt->stream_index];
-    int64_t end_pts = hls->recording_time * hls->number;
+    int64_t end_pts = hls->recording_time * hls->number, offset;
     int is_ref_pkt = 1;
     int ret, can_split = 1;
     int stream_index = 0;
@@ -871,7 +872,14 @@  static int hls_write_packet(AVFormatContext *s, AVPacket *pkt)
             return ret;
     }
 
-    ret = ff_write_chained(oc, stream_index, pkt, s, 0);
+    /* compute new timestamps */
+    offset = av_rescale_q(hls->initial_offset, AV_TIME_BASE_Q, st->time_base);
+    if (pkt->pts != AV_NOPTS_VALUE)
+        pkt->pts += offset;
+    if (pkt->dts != AV_NOPTS_VALUE)
+        pkt->dts += offset;
+
+    ret = ff_write_chained(oc, stream_index, pkt, s, hls->initial_offset ? 1 : 0);
 
     return ret;
 }
@@ -939,6 +947,7 @@  static const AVOption options[] = {
     {"event", "EVENT playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_EVENT }, INT_MIN, INT_MAX, E, "pl_type" },
     {"vod", "VOD playlist", 0, AV_OPT_TYPE_CONST, {.i64 = PLAYLIST_TYPE_VOD }, INT_MIN, INT_MAX, E, "pl_type" },
     {"method", "set the HTTP method", OFFSET(method), AV_OPT_TYPE_STRING, {.str = NULL},  0, 0,    E},
+    {"initial_offset", "set initial timestamp offset", OFFSET(initial_offset), AV_OPT_TYPE_DURATION, {.i64 = 0}, -INT64_MAX, INT64_MAX, E },
 
     { NULL },
 };