[FFmpeg-devel,1/2] flvenc: Factorize timestamp writing

Submitted by Alex Converse on May 11, 2018, 1:40 a.m.

Details

Message ID 20180511014008.1676-1-alex.converse@gmail.com
State New
Headers show

Commit Message

Alex Converse May 11, 2018, 1:40 a.m.
From: Alex Converse <alexconv@twitch.tv>

The code is trivial but the semantics in the spec are ambiguous. This
should help keep parts of the muxer interpreting them consistently.
---
 libavformat/flvenc.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)

Comments

Michael Niedermayer May 13, 2018, 12:09 a.m.
On Thu, May 10, 2018 at 06:40:07PM -0700, Alex Converse wrote:
> From: Alex Converse <alexconv@twitch.tv>
> 
> The code is trivial but the semantics in the spec are ambiguous. This
> should help keep parts of the muxer interpreting them consistently.
> ---
>  libavformat/flvenc.c | 12 ++++++++----
>  1 file changed, 8 insertions(+), 4 deletions(-)

LGTM

thx

Patch hide | download patch | download mbox

diff --git a/libavformat/flvenc.c b/libavformat/flvenc.c
index e8af48cb64..9b7cdfe7db 100644
--- a/libavformat/flvenc.c
+++ b/libavformat/flvenc.c
@@ -230,12 +230,17 @@  static void put_amf_string(AVIOContext *pb, const char *str)
     avio_write(pb, str, len);
 }
 
+// FLV timestamps are 32 bits signed, RTMP timestamps should be 32-bit unsigned
+static void put_timestamp(AVIOContext *pb, unsigned ts) {
+    avio_wb24(pb, ts & 0xFFFFFF);
+    avio_w8(pb, (ts >> 24) & 0x7F);
+}
+
 static void put_avc_eos_tag(AVIOContext *pb, unsigned ts)
 {
     avio_w8(pb, FLV_TAG_TYPE_VIDEO);
     avio_wb24(pb, 5);               /* Tag Data Size */
-    avio_wb24(pb, ts);              /* lower 24 bits of timestamp in ms */
-    avio_w8(pb, (ts >> 24) & 0x7F); /* MSB of ts in ms */
+    put_timestamp(pb, ts);
     avio_wb24(pb, 0);               /* StreamId = 0 */
     avio_w8(pb, 23);                /* ub[4] FrameType = 1, ub[4] CodecId = 7 */
     avio_w8(pb, 2);                 /* AVC end of sequence */
@@ -978,8 +983,7 @@  static int flv_write_packet(AVFormatContext *s, AVPacket *pkt)
     }
 
     avio_wb24(pb, size + flags_size);
-    avio_wb24(pb, ts & 0xFFFFFF);
-    avio_w8(pb, (ts >> 24) & 0x7F); // timestamps are 32 bits _signed_
+    put_timestamp(pb, ts);
     avio_wb24(pb, flv->reserved);
 
     if (par->codec_type == AVMEDIA_TYPE_DATA ||