--- libavcodec/libx265.c~	2014-03-23 23:07:50.000000000 +0100
+++ libavcodec/libx265.c	2014-04-05 19:28:05.566614823 +0200
@@ -38,8 +38,6 @@
 
     x265_encoder *encoder;
     x265_param   *params;
-    uint8_t      *header;
-    int           header_size;
 
     char *preset;
     char *tune;
@@ -66,7 +64,6 @@
     libx265Context *ctx = avctx->priv_data;
 
     av_frame_free(&avctx->coded_frame);
-    av_freep(&ctx->header);
 
     x265_param_free(ctx->params);
 
@@ -80,11 +77,8 @@
 {
     libx265Context *ctx = avctx->priv_data;
     x265_nal *nal;
-    uint8_t *buf;
     int sar_num, sar_den;
     int nnal;
-    int ret;
-    int i;
 
     if (avctx->strict_std_compliance > FF_COMPLIANCE_EXPERIMENTAL &&
         !av_pix_fmt_desc_get(avctx->pix_fmt)->log2_chroma_w &&
@@ -121,11 +115,11 @@
     av_reduce(&sar_num, &sar_den,
               avctx->sample_aspect_ratio.num,
               avctx->sample_aspect_ratio.den, 4096);
-    ctx->params->bEnableVuiParametersPresentFlag = 1;
-    ctx->params->bEnableAspectRatioIdc           = 1;
-    ctx->params->aspectRatioIdc                  = 255;
-    ctx->params->sarWidth                        = sar_num;
-    ctx->params->sarHeight                       = sar_den;
+    ctx->params->vui.bEnableVuiParametersPresentFlag = 1;
+    ctx->params->vui.bEnableAspectRatioIdc           = 1;
+    ctx->params->vui.aspectRatioIdc                  = 255;
+    ctx->params->vui.sarWidth                        = sar_num;
+    ctx->params->vui.sarHeight                       = sar_den;
 
     if (x265_max_bit_depth == 8)
         ctx->params->internalBitDepth = 8;
@@ -148,6 +142,9 @@
         ctx->params->rc.rateControlMode = X265_RC_ABR;
     }
 
+    if (!(avctx->flags & CODEC_FLAG_GLOBAL_HEADER))
+        ctx->params->bRepeatHeaders = 1;
+
     if (ctx->x265_opts) {
         AVDictionary *dict    = NULL;
         AVDictionaryEntry *en = NULL;
@@ -180,35 +177,23 @@
         return AVERROR_INVALIDDATA;
     }
 
-    ret = x265_encoder_headers(ctx->encoder, &nal, &nnal);
-    if (ret < 0) {
-        av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
-        libx265_encode_close(avctx);
-        return AVERROR_INVALIDDATA;
-    }
-
-    for (i = 0; i < nnal; i++)
-        ctx->header_size += nal[i].sizeBytes;
-
-    ctx->header = av_malloc(ctx->header_size + FF_INPUT_BUFFER_PADDING_SIZE);
-    if (!ctx->header) {
-        av_log(avctx, AV_LOG_ERROR,
-               "Cannot allocate HEVC header of size %d.\n", ctx->header_size);
-        libx265_encode_close(avctx);
-        return AVERROR(ENOMEM);
-    }
+    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
+        avctx->extradata_size = x265_encoder_headers(ctx->encoder, &nal, &nnal);
+        if (avctx->extradata_size <= 0) {
+            av_log(avctx, AV_LOG_ERROR, "Cannot encode headers.\n");
+            libx265_encode_close(avctx);
+            return AVERROR_INVALIDDATA;
+        }
 
-    buf = ctx->header;
-    for (i = 0; i < nnal; i++) {
-        memcpy(buf, nal[i].payload, nal[i].sizeBytes);
-        buf += nal[i].sizeBytes;
-    }
+        avctx->extradata = av_malloc(avctx->extradata_size + FF_INPUT_BUFFER_PADDING_SIZE);
+        if (!avctx->extradata) {
+            av_log(avctx, AV_LOG_ERROR,
+                   "Cannot allocate HEVC header of size %d.\n", avctx->extradata_size);
+            libx265_encode_close(avctx);
+            return AVERROR(ENOMEM);
+        }
 
-    if (avctx->flags & CODEC_FLAG_GLOBAL_HEADER) {
-        avctx->extradata_size = ctx->header_size;
-        avctx->extradata = ctx->header;
-        ctx->header_size = 0;
-        ctx->header = NULL;
+        memcpy(avctx->extradata, nal[0].payload, avctx->extradata_size);
     }
 
     return 0;
@@ -250,8 +235,6 @@
     for (i = 0; i < nnal; i++)
         payload += nal[i].sizeBytes;
 
-    payload += ctx->header_size;
-
     ret = ff_alloc_packet(pkt, payload);
     if (ret < 0) {
         av_log(avctx, AV_LOG_ERROR, "Error getting output packet.\n");
@@ -259,14 +242,6 @@
     }
     dst = pkt->data;
 
-    if (ctx->header) {
-        memcpy(dst, ctx->header, ctx->header_size);
-        dst += ctx->header_size;
-
-        av_freep(&ctx->header);
-        ctx->header_size = 0;
-    }
-
     for (i = 0; i < nnal; i++) {
         memcpy(dst, nal[i].payload, nal[i].sizeBytes);
         dst += nal[i].sizeBytes;
