diff --git a/encoder/encoder.c b/encoder/encoder.c
index 7637dc2..90a2799 100644
--- a/encoder/encoder.c
+++ b/encoder/encoder.c
@@ -796,6 +796,15 @@ static void x264_nal_end( x264_t *h )
     h->out.i_nal++;
 }
 
+static void x264_nal_padding_append( x264_t *h, int padding_bytes )
+{
+    if( padding_bytes > 0 ) {
+        x264_nal_start( h, NAL_FILLER, NAL_PRIORITY_DISPOSABLE );
+        x264_padding_data_write( h, &h->out.bs, padding_bytes );
+        x264_nal_end( h );
+    }
+}
+
 /****************************************************************************
  * x264_encoder_headers:
  ****************************************************************************/
@@ -1599,6 +1608,7 @@ static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
 {
     int i;
     char psz_message[80];
+    int padding_bytes;
 
     if( h->b_thread_active )
     {
@@ -1613,11 +1623,6 @@ static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
 
     x264_frame_push_unused( thread_current, h->fenc );
 
-    /* End bitstream, set output  */
-    *pi_nal = h->out.i_nal;
-    *pp_nal = h->out.nal;
-    h->out.i_nal = 0;
-
     /* Set output picture properties */
     if( h->sh.i_type == SLICE_TYPE_I )
         pic_out->i_type = h->i_nal_type == NAL_SLICE_IDR ? X264_TYPE_IDR : X264_TYPE_I;
@@ -1637,7 +1642,17 @@ static void x264_encoder_frame_end( x264_t *h, x264_t *thread_current,
 
     /* update rc */
     x264_cpu_restore( h->param.cpu );
-    x264_ratecontrol_end( h, h->out.i_frame_size * 8 );
+    padding_bytes = x264_ratecontrol_end( h, h->out.i_frame_size * 8 );
+
+    x264_nal_padding_append( h, padding_bytes );
+
+    /* count in the padding bytes */
+    h->out.i_frame_size += padding_bytes;
+
+    /* End bitstream, set output  */
+    *pi_nal = h->out.i_nal;
+    *pp_nal = h->out.nal;
+    h->out.i_nal = 0;
 
     /* restore CPU state (before using float again) */
     x264_cpu_restore( h->param.cpu );
diff --git a/encoder/ratecontrol.c b/encoder/ratecontrol.c
index bb10f5c..2ae65af 100644
--- a/encoder/ratecontrol.c
+++ b/encoder/ratecontrol.c
@@ -141,7 +141,7 @@ struct x264_ratecontrol_t
 static int parse_zones( x264_t *h );
 static int init_pass2(x264_t *);
 static float rate_estimate_qscale( x264_t *h );
-static void update_vbv( x264_t *h, int bits );
+static int update_vbv( x264_t *h, int bits );
 static void update_vbv_plan( x264_t *h );
 static double predict_size( predictor_t *p, double q, double var );
 static void update_predictor( predictor_t *p, double q, double var, double bits );
@@ -986,7 +986,7 @@ int x264_ratecontrol_slice_type( x264_t *h, int frame_num )
 }
 
 /* After encoding one frame, save stats and update ratecontrol state */
-void x264_ratecontrol_end( x264_t *h, int bits )
+int x264_ratecontrol_end( x264_t *h, int bits )
 {
     x264_ratecontrol_t *rc = h->rc;
     const int *mbs = h->stat.frame.i_mb_count;
@@ -1063,7 +1063,7 @@ void x264_ratecontrol_end( x264_t *h, int bits )
         }
     }
 
-    update_vbv( h, bits );
+    return update_vbv( h, bits );
 }
 
 /****************************************************************************
@@ -1241,21 +1241,29 @@ static void update_predictor( predictor_t *p, double q, double var, double bits
 }
 
 // update VBV after encoding a frame
-static void update_vbv( x264_t *h, int bits )
+static int update_vbv( x264_t *h, int bits )
 {
     x264_ratecontrol_t *rcc = h->rc;
     x264_ratecontrol_t *rct = h->thread[0]->rc;
+    int padding_bits=0;
 
     if( rcc->last_satd >= h->mb.i_mb_count )
         update_predictor( &rct->pred[h->sh.i_type], qp2qscale(rcc->qpa_rc), rcc->last_satd, bits );
 
     if( !rcc->b_vbv )
-        return;
+        return 0;
 
     rct->buffer_fill_final += rct->buffer_rate - bits;
     if( rct->buffer_fill_final < 0 && !rct->b_2pass )
         x264_log( h, X264_LOG_WARNING, "VBV underflow (%.0f bits)\n", rct->buffer_fill_final );
+
+    if( rcc->b_vbv_min_rate && rct->buffer_fill_final > rct->buffer_size && !rct->b_2pass ) {
+        padding_bits = (int)(rct->buffer_fill_final - rct->buffer_size);
+        //x264_log( h, X264_LOG_INFO, "VBV overflow (%d bits)\n", padding_bits );
+    }
+
     rct->buffer_fill_final = x264_clip3f( rct->buffer_fill_final, 0, rct->buffer_size );
+    return padding_bits/8;
 }
 
 // provisionally update VBV according to the planned size of all frames currently in progress
diff --git a/encoder/ratecontrol.h b/encoder/ratecontrol.h
index 2aabda7..e3fec1d 100644
--- a/encoder/ratecontrol.h
+++ b/encoder/ratecontrol.h
@@ -32,7 +32,7 @@ void x264_ratecontrol_start( x264_t *, int i_force_qp );
 int  x264_ratecontrol_slice_type( x264_t *, int i_frame );
 void x264_ratecontrol_mb( x264_t *, int bits );
 int  x264_ratecontrol_qp( x264_t * );
-void x264_ratecontrol_end( x264_t *, int bits );
+int  x264_ratecontrol_end( x264_t *, int bits );
 void x264_ratecontrol_summary( x264_t * );
 void x264_adaptive_quant( x264_t * );
 
diff --git a/encoder/set.c b/encoder/set.c
index 6cf8773..c895ff3 100644
--- a/encoder/set.c
+++ b/encoder/set.c
@@ -469,6 +469,18 @@ void x264_pps_write( bs_t *s, x264_pps_t *pps )
     bs_rbsp_trailing( s );
 }
 
+void x264_padding_data_write( x264_t *h, bs_t *s, int length )
+{
+    int i;
+
+    /* padding byte */
+    for( i = 0; i < length; i++ ) {
+        bs_write( s, 8, 0xff );
+    }
+
+    bs_rbsp_trailing( s );
+}
+
 void x264_sei_version_write( x264_t *h, bs_t *s )
 {
     int i;
diff --git a/encoder/set.h b/encoder/set.h
index dac2d2f..a01624e 100644
--- a/encoder/set.h
+++ b/encoder/set.h
@@ -28,6 +28,7 @@ void x264_sps_init( x264_sps_t *sps, int i_id, x264_param_t *param );
 void x264_sps_write( bs_t *s, x264_sps_t *sps );
 void x264_pps_init( x264_pps_t *pps, int i_id, x264_param_t *param, x264_sps_t *sps );
 void x264_pps_write( bs_t *s, x264_pps_t *pps );
+void x264_padding_data_write( x264_t *h, bs_t *s, int length );
 void x264_sei_version_write( x264_t *h, bs_t *s );
 void x264_validate_levels( x264_t *h );
 
diff --git a/x264.h b/x264.h
index e49f1a4..54caa8c 100644
--- a/x264.h
+++ b/x264.h
@@ -372,6 +372,7 @@ enum nal_unit_type_e
     NAL_SPS         = 7,
     NAL_PPS         = 8,
     NAL_AUD         = 9,
+    NAL_FILLER      = 12,
     /* ref_idc == 0 for 6,9,10,11,12 */
 };
 enum nal_priority_e
