[FFmpeg-devel] [PATCH] Added rap flag to libx265 to insert VPS/SPS/PPS before each key frame.

Marcus Gustafsson mankan.gustafsson at gmail.com
Fri Mar 14 18:52:24 CET 2014


Hi all,

my first post here.
I've made a patch in order to be able to play streamed H.265 streams
regardless where in the stream you start decoding.
Use "-rap=1" to enable it as a video encoder argument.

Best regards
Marcus Gustafsson

---
 libavcodec/libx265.c | 28 ++++++++++++++++++++--------
 1 file changed, 20 insertions(+), 8 deletions(-)

diff --git a/libavcodec/libx265.c b/libavcodec/libx265.c
index 2aceac7..291692a 100644
--- a/libavcodec/libx265.c
+++ b/libavcodec/libx265.c
@@ -42,6 +42,7 @@ typedef struct libx265Context {
     int           header_size;

     char *preset;
+    int rap;
     char *tune;
     char *x265_opts;
 } libx265Context;
@@ -247,10 +248,17 @@ static int libx265_encode_frame(AVCodecContext
*avctx, AVPacket *pkt,
     if (!nnal)
         return 0;

-    for (i = 0; i < nnal; i++)
+    for (i = 0; i < nnal; i++) {
         payload += nal[i].sizeBytes;
-
-    payload += ctx->header_size;
+        if (is_keyframe(nal[i].type)) {
+            pkt->flags |= AV_PKT_FLAG_KEY;
+            if (ctx->rap) {
+                payload += ctx->header_size;
+            }
+        }
+    }
+    if (!ctx->rap)
+        payload += ctx->header_size;

     ret = ff_alloc_packet(pkt, payload);
     if (ret < 0) {
@@ -259,7 +267,7 @@ static int libx265_encode_frame(AVCodecContext *avctx,
AVPacket *pkt,
     }
     dst = pkt->data;

-    if (ctx->header) {
+    if (ctx->header && !ctx->rap) {
         memcpy(dst, ctx->header, ctx->header_size);
         dst += ctx->header_size;

@@ -267,14 +275,17 @@ static int libx265_encode_frame(AVCodecContext
*avctx, AVPacket *pkt,
         ctx->header_size = 0;
     }

+
     for (i = 0; i < nnal; i++) {
+        if (is_keyframe(nal[i].type)) {
+            if (ctx->header && ctx->rap) {
+                memcpy(dst, ctx->header, ctx->header_size);
+                dst += ctx->header_size;
+            }
+        }
         memcpy(dst, nal[i].payload, nal[i].sizeBytes);
         dst += nal[i].sizeBytes;
-
-        if (is_keyframe(nal[i].type))
-            pkt->flags |= AV_PKT_FLAG_KEY;
     }
-
     pkt->pts = x265pic_out.pts;
     pkt->dts = x265pic_out.dts;

@@ -308,6 +319,7 @@ static av_cold void libx265_encode_init_csp(AVCodec
*codec)
 #define VE AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_ENCODING_PARAM
 static const AVOption options[] = {
     { "preset",      "set the x265
preset",
OFFSET(preset),
  AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
+    { "rap",         "enable VPS/SPS/PPS insertion before every key
frame",                         OFFSET(rap),
  AV_OPT_TYPE_INT, { .i64 = 0 }, 0, 1, VE },
     { "tune",        "set the x265 tune
parameter",                                                 OFFSET(tune),
  AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
     { "x265-params", "set the x265 configuration using a :-separated list
of key=value parameters", OFFSET(x265_opts)
, AV_OPT_TYPE_STRING, { 0 }, 0, 0, VE },
     { NULL }
--
1.8.1.4


More information about the ffmpeg-devel mailing list