[FFmpeg-devel] [PATCH] mpegts: mux and demux asynchronous SMPTE 336M Key-Length-Value (KLV) metadata

Peter Ross pross at xvid.org
Sun Oct 14 11:33:37 CEST 2012


---
Hi, 

I am seeking ideas on how to incorporate SMPTE 336M/KLV support into FFmpeg.
KLV is a standard providing frame-based metadata. It is used in MPEG-TS although
there are proposals to add it to other containers. Samples here /incoming/mpegts-klv.
The goal is for FFmpeg to transcode mpegts files and not discard the metadata.

KLV packes do not normally provide PTS, so the transcoding application must
associate them with the most recent video frame. How best this could be handled
in libavformat?

The patch below enables '-dcodec copy' support, but because the KLV packets have
NOPTS, the muxer writes them out at the beginning of the file, rather than
interleaving them with the video.

 Changelog               |    1 +
 libavcodec/avcodec.h    |    1 +
 libavcodec/codec_desc.c |    6 ++++++
 libavformat/mpegts.c    |    1 +
 libavformat/mpegtsenc.c |   12 ++++++++++--
 5 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/Changelog b/Changelog
index 2fd2239..dffe2ef 100644
--- a/Changelog
+++ b/Changelog
@@ -11,6 +11,7 @@ version next:
 - TAK demuxer, decoder and parser
 - DTS-HD demuxer
 - remove -same_quant, it hasn't worked for years
+- SMPTE 336M Key-Length-Value (KLV) stream copying
 
 
 version 1.0:
diff --git a/libavcodec/avcodec.h b/libavcodec/avcodec.h
index 27e249f..18fd3a2 100644
--- a/libavcodec/avcodec.h
+++ b/libavcodec/avcodec.h
@@ -460,6 +460,7 @@ enum AVCodecID {
     AV_CODEC_ID_XBIN       = MKBETAG('X','B','I','N'),
     AV_CODEC_ID_IDF        = MKBETAG( 0 ,'I','D','F'),
     AV_CODEC_ID_OTF        = MKBETAG( 0 ,'O','T','F'),
+    AV_CODEC_ID_SMPTE_KLV  = MKBETAG('K','L','V','A'),
 
     AV_CODEC_ID_PROBE = 0x19000, ///< codec_id is not known (like AV_CODEC_ID_NONE) but lavf should attempt to identify it
 
diff --git a/libavcodec/codec_desc.c b/libavcodec/codec_desc.c
index d411981..03dc132 100644
--- a/libavcodec/codec_desc.c
+++ b/libavcodec/codec_desc.c
@@ -2370,6 +2370,12 @@ static const AVCodecDescriptor codec_descriptors[] = {
         .long_name = NULL_IF_CONFIG_SMALL("WebVTT subtitle"),
     },
     {
+        .id        = AV_CODEC_ID_SMPTE_KLV,
+        .type      = AVMEDIA_TYPE_SUBTITLE,
+        .name      = "klv",
+        .long_name = NULL_IF_CONFIG_SMALL("SMPTE 336M Key-Length-Value (KLV) metadata"),
+    },
+    {
         .id        = AV_CODEC_ID_BINTEXT,
         .type      = AVMEDIA_TYPE_VIDEO,
         .name      = "bintext",
diff --git a/libavformat/mpegts.c b/libavformat/mpegts.c
index 33a0eaf..eb50c28 100644
--- a/libavformat/mpegts.c
+++ b/libavformat/mpegts.c
@@ -580,6 +580,7 @@ static const StreamType REGD_types[] = {
     { MKTAG('D','T','S','1'), AVMEDIA_TYPE_AUDIO,   AV_CODEC_ID_DTS },
     { MKTAG('D','T','S','2'), AVMEDIA_TYPE_AUDIO,   AV_CODEC_ID_DTS },
     { MKTAG('D','T','S','3'), AVMEDIA_TYPE_AUDIO,   AV_CODEC_ID_DTS },
+    { MKTAG('K','L','V','A'), AVMEDIA_TYPE_DATA,    AV_CODEC_ID_SMPTE_KLV },
     { MKTAG('V','C','-','1'), AVMEDIA_TYPE_VIDEO,   AV_CODEC_ID_VC1 },
     { 0 },
 };
diff --git a/libavformat/mpegtsenc.c b/libavformat/mpegtsenc.c
index 1b66bab..21f8a0e 100644
--- a/libavformat/mpegtsenc.c
+++ b/libavformat/mpegtsenc.c
@@ -25,6 +25,7 @@
 #include "libavutil/mathematics.h"
 #include "libavutil/opt.h"
 #include "libavutil/avassert.h"
+#include "libavcodec/bytestream.h"
 #include "libavcodec/mpegvideo.h"
 #include "avformat.h"
 #include "internal.h"
@@ -393,6 +394,13 @@ static void mpegts_write_pmt(AVFormatContext *s, MpegTSService *service)
                 *q++ = 'c';
             }
             break;
+        case AVMEDIA_TYPE_DATA:
+            if (st->codec->codec_id == AV_CODEC_ID_SMPTE_KLV) {
+                *q++ = 0x05; /* MPEG-2 registration descriptor */
+                *q++ = 4;
+                bytestream_put_buffer(&q, "KLVA", 4);
+            }
+            break;
         }
 
         val = 0xf000 | (q - desc_length_ptr - 2);
@@ -973,8 +981,8 @@ static void mpegts_write_pes(AVFormatContext *s, AVStream *st,
             *q++ = len >> 8;
             *q++ = len;
             val = 0x80;
-            /* data alignment indicator is required for subtitle data */
-            if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE)
+            /* data alignment indicator is required for subtitle and data streams */
+            if (st->codec->codec_type == AVMEDIA_TYPE_SUBTITLE || st->codec->codec_type == AVMEDIA_TYPE_DATA)
                 val |= 0x04;
             *q++ = val;
             *q++ = flags;
-- 
1.7.10.4

-- Peter
(A907 E02F A6E5 0CD2 34CD 20D2 6760 79C5 AC40 DD6B)
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 198 bytes
Desc: Digital signature
URL: <http://ffmpeg.org/pipermail/ffmpeg-devel/attachments/20121014/c6107f7e/attachment.asc>


More information about the ffmpeg-devel mailing list