diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c
index 163d0f5..292d106 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -39,21 +39,21 @@ typedef struct H264BSFContext {
 
 static int alloc_and_copy(AVPacket *out,
                           const uint8_t *sps_pps, uint32_t sps_pps_size,
-                          const uint8_t *in, uint32_t in_size)
+                          const uint8_t *in, uint32_t in_size, int ps)
 {
     uint32_t offset         = out->size;
-    uint8_t nal_header_size = offset ? 3 : 4;
+    uint8_t start_code_size = offset == 0 || ps ? 4 : 3;
     int err;
 
-    err = av_grow_packet(out, sps_pps_size + in_size + nal_header_size);
+    err = av_grow_packet(out, sps_pps_size + in_size + start_code_size);
     if (err < 0)
         return err;
 
     if (sps_pps)
         memcpy(out->data + offset, sps_pps, sps_pps_size);
-    memcpy(out->data + sps_pps_size + nal_header_size + offset, in, in_size);
-    if (!offset) {
-        AV_WB32(out->data + sps_pps_size, 1);
+    memcpy(out->data + sps_pps_size + start_code_size + offset, in, in_size);
+    if (start_code_size == 4) {
+        AV_WB32(out->data + offset + sps_pps_size, 1);
     } else {
         (out->data + offset + sps_pps_size)[0] =
         (out->data + offset + sps_pps_size)[1] = 0;
@@ -221,7 +221,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
                     if ((ret = alloc_and_copy(out,
                                          ctx->par_out->extradata + s->sps_offset,
                                          s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
-                                         buf, nal_size)) < 0)
+                                         buf, nal_size, 1)) < 0)
                         goto fail;
                     s->idr_sps_seen = 1;
                     goto next_nal;
@@ -239,21 +239,21 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
         if (s->new_idr && unit_type == 5 && !s->idr_sps_seen && !s->idr_pps_seen) {
             if ((ret=alloc_and_copy(out,
                                ctx->par_out->extradata, ctx->par_out->extradata_size,
-                               buf, nal_size)) < 0)
+                               buf, nal_size, 1)) < 0)
                 goto fail;
             s->new_idr = 0;
         /* if only SPS has been seen, also insert PPS */
         } else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen) {
             if (s->pps_offset == -1) {
                 av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
-                if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
+                if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size, 0)) < 0)
                     goto fail;
             } else if ((ret = alloc_and_copy(out,
                                         ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
-                                        buf, nal_size)) < 0)
+                                        buf, nal_size, 1)) < 0)
                 goto fail;
         } else {
-            if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
+            if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size, unit_type == 7 || unit_type == 8)) < 0)
                 goto fail;
             if (!s->new_idr && unit_type == 1) {
                 s->new_idr = 1;
diff --git a/tests/ref/fate/h264-bsf-mp4toannexb b/tests/ref/fate/h264-bsf-mp4toannexb
index 2049f39..7cd086a 100644
--- a/tests/ref/fate/h264-bsf-mp4toannexb
+++ b/tests/ref/fate/h264-bsf-mp4toannexb
@@ -1 +1 @@
-5f04c27cc6ee8625fe2405fb0f7da9a3
+f340e7ca9a46d437af4e96f6c8de221c
diff --git a/tests/ref/fate/h264_mp4toannexb_ticket2991 b/tests/ref/fate/h264_mp4toannexb_ticket2991
index 76bdf3c..3245ef4 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket2991
+++ b/tests/ref/fate/h264_mp4toannexb_ticket2991
@@ -1,12 +1,12 @@
-05d66e60ab22ee004720e0051af0fe74 *tests/data/fate/h264_mp4toannexb_ticket2991.h264
-1985815 tests/data/fate/h264_mp4toannexb_ticket2991.h264
-#extradata 0:       47, 0x3a590d55
+dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h264
+1985823 tests/data/fate/h264_mp4toannexb_ticket2991.h264
+#extradata 0:       48, 0x47ae0d55
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1280x720
 #sar 0: 3/4
-0,          0,          0,    48000,    37126, 0xb020184c
+0,          0,          0,    48000,    37127, 0xc125184c
 0,      48000,      48000,    40040,     6920, 0x8512361a, F=0x0
 0,      88040,      88040,    40040,     7550, 0x1bc56ed4, F=0x0
 0,     128081,     128081,    40040,     8752, 0xb8c6f0a1, F=0x0
@@ -21,7 +21,7 @@
 0,     488444,     488444,    40040,    11234, 0x83cbd9fd, F=0x0
 0,     528485,     528485,    40040,    17616, 0xfdf95104, F=0x0
 0,     568525,     568525,    40040,    10689, 0x9633d32b, F=0x0
-0,     608566,     608566,    40040,    45291, 0x543c2cf6
+0,     608566,     608566,    40040,    45292, 0x66dd2cf6
 0,     648606,     648606,    40040,    20837, 0x051abfab, F=0x0
 0,     688646,     688646,    40040,    21418, 0xe2a59d70, F=0x0
 0,     728687,     728687,    40040,    15643, 0x15cf2cec, F=0x0
@@ -36,7 +36,7 @@
 0,    1089050,    1089050,    40040,    13130, 0xcbb6bb8e, F=0x0
 0,    1129091,    1129091,    40040,    16180, 0x5d188a7a, F=0x0
 0,    1169131,    1169131,    40040,    14961, 0x9ff2f463, F=0x0
-0,    1209172,    1209172,    40040,    54296, 0xe6ec30ed
+0,    1209172,    1209172,    40040,    54297, 0xf98d30ed
 0,    1249212,    1249212,    40040,    11500, 0x8c4852c9, F=0x0
 0,    1289252,    1289252,    40040,    12065, 0xfb7954c3, F=0x0
 0,    1329293,    1329293,    40040,    12532, 0xf0a935d3, F=0x0
@@ -51,7 +51,7 @@
 0,    1689656,    1689656,    40040,    13250, 0xfed0deb8, F=0x0
 0,    1729697,    1729697,    40040,    13360, 0xbf92d476, F=0x0
 0,    1769737,    1769737,    40040,    11749, 0x3041eaf1, F=0x0
-0,    1809778,    1809778,    40040,    23997, 0xdbe6d5c4
+0,    1809778,    1809778,    40040,    23998, 0xee87d5c4
 0,    1849818,    1849818,    40040,    16065, 0xe8f715b7, F=0x0
 0,    1889858,    1889858,    40040,    16441, 0x0a4e060f, F=0x0
 0,    1929899,    1929899,    40040,    17395, 0xa8edecc2, F=0x0
@@ -66,7 +66,7 @@
 0,    2290262,    2290262,    40040,    13748, 0xed26aeb4, F=0x0
 0,    2330303,    2330303,    40040,    15092, 0x3c983538, F=0x0
 0,    2370343,    2370343,    40040,    14636, 0x9b278a6c, F=0x0
-0,    2410384,    2410384,    40040,    29134, 0xf784be18
+0,    2410384,    2410384,    40040,    29135, 0x0a34be18
 0,    2450424,    2450424,    40040,    10232, 0x5408e15b, F=0x0
 0,    2490464,    2490464,    40040,     9769, 0xc93cb7f9, F=0x0
 0,    2530505,    2530505,    40040,    14454, 0x45230dbe, F=0x0
@@ -81,7 +81,7 @@
 0,    2890868,    2890868,    40040,    14801, 0x40bae016, F=0x0
 0,    2930909,    2930909,    40040,    17303, 0x9ce1fd31, F=0x0
 0,    2970949,    2970949,    40040,    17678, 0x9bd66141, F=0x0
-0,    3010990,    3010990,    40040,    48672, 0x3215ce46
+0,    3010990,    3010990,    40040,    48673, 0x44b6ce46
 0,    3051030,    3051030,    40040,    11894, 0x12e1fece, F=0x0
 0,    3091070,    3091070,    40040,    16514, 0xc57aed05, F=0x0
 0,    3131111,    3131111,    40040,    13044, 0x61914fa0, F=0x0
@@ -96,7 +96,7 @@
 0,    3491474,    3491474,    40040,    12208, 0x81a587c0, F=0x0
 0,    3531515,    3531515,    40040,    14709, 0x5dffbe04, F=0x0
 0,    3571555,    3571555,    40040,    14390, 0xbfd1e041, F=0x0
-0,    3611596,    3611596,    40040,    37236, 0xe7f924b1
+0,    3611596,    3611596,    40040,    37237, 0xfa9a24b1
 0,    3651636,    3651636,    40040,    14056, 0x24714c7c, F=0x0
 0,    3691676,    3691676,    40040,    19438, 0x0c50dcd5, F=0x0
 0,    3731717,    3731717,    40040,    21728, 0x7eea4a11, F=0x0
@@ -111,7 +111,7 @@
 0,    4092080,    4092080,    40040,    16878, 0x98efbae2, F=0x0
 0,    4132121,    4132121,    40040,    14685, 0x1bf78d65, F=0x0
 0,    4172161,    4172161,    40040,    13127, 0x0b91881d, F=0x0
-0,    4212202,    4212202,    40040,    29390, 0xf6a5ed6b
+0,    4212202,    4212202,    40040,    29391, 0x0955ed6b
 0,    4252242,    4252242,    40040,    12576, 0xe9845ded, F=0x0
 0,    4292282,    4292282,    40040,    12599, 0x96a79ab8, F=0x0
 0,    4332323,    4332323,    40040,    16134, 0xb4c36d3f, F=0x0
diff --git a/tests/ref/fate/h264_mp4toannexb_ticket5927 b/tests/ref/fate/h264_mp4toannexb_ticket5927
index 95e35c4..006ea39 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket5927
+++ b/tests/ref/fate/h264_mp4toannexb_ticket5927
@@ -1,12 +1,12 @@
-a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927.h264
-595583 tests/data/fate/h264_mp4toannexb_ticket5927.h264
-#extradata 0:       33, 0x84fe08f8
+562487bfea635cdadbc23d390322b589 *tests/data/fate/h264_mp4toannexb_ticket5927.h264
+595585 tests/data/fate/h264_mp4toannexb_ticket5927.h264
+#extradata 0:       34, 0x8df608f8
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,     -48000, -9223372036854775808,    48000,   247993, 0x1ce821ea
+0,     -48000, -9223372036854775808,    48000,   247994, 0x2e1e21ea
 0,          0, -9223372036854775808,    48000,    43354, 0xa05dca6f, F=0x0
 0,      48000, -9223372036854775808,    48000,    11423, 0x5e8086dd, F=0x0
 0,      96000, -9223372036854775808,    48000,    50798, 0x145fbe4f, F=0x0
@@ -18,4 +18,4 @@ a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927.h2
 0,     384000, -9223372036854775808,    48000,    54483, 0xefead99f, F=0x0
 0,     432000, -9223372036854775808,    48000,    13705, 0x23cd27e8, F=0x0
 0,     480000, -9223372036854775808,    48000,    22308, 0x4093b5af, F=0x0
-0,     528000, -9223372036854775808,    48000,     6369, 0x858b2aa1
+0,     528000, -9223372036854775808,    48000,     6370, 0x96c12aa1
diff --git a/tests/ref/fate/h264_mp4toannexb_ticket5927_2 b/tests/ref/fate/h264_mp4toannexb_ticket5927_2
index 8db6a7e..51432b1 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket5927_2
+++ b/tests/ref/fate/h264_mp4toannexb_ticket5927_2
@@ -1,12 +1,12 @@
-a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
-595583 tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
-#extradata 0:       33, 0x84fe08f8
+562487bfea635cdadbc23d390322b589 *tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
+595585 tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
+#extradata 0:       34, 0x8df608f8
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,     -48000, -9223372036854775808,    48000,   247993, 0x1ce821ea
+0,     -48000, -9223372036854775808,    48000,   247994, 0x2e1e21ea
 0,          0, -9223372036854775808,    48000,    43354, 0xa05dca6f, F=0x0
 0,      48000, -9223372036854775808,    48000,    11423, 0x5e8086dd, F=0x0
 0,      96000, -9223372036854775808,    48000,    50798, 0x145fbe4f, F=0x0
@@ -18,4 +18,4 @@ a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927_2.
 0,     384000, -9223372036854775808,    48000,    54483, 0xefead99f, F=0x0
 0,     432000, -9223372036854775808,    48000,    13705, 0x23cd27e8, F=0x0
 0,     480000, -9223372036854775808,    48000,    22308, 0x4093b5af, F=0x0
-0,     528000, -9223372036854775808,    48000,     6369, 0x858b2aa1
+0,     528000, -9223372036854775808,    48000,     6370, 0x96c12aa1
diff --git a/tests/ref/fate/segment-mp4-to-ts b/tests/ref/fate/segment-mp4-to-ts
index 847c1a2..b5accb6 100644
--- a/tests/ref/fate/segment-mp4-to-ts
+++ b/tests/ref/fate/segment-mp4-to-ts
@@ -1,10 +1,10 @@
-#extradata 0:       50, 0x4f1b0df9
+#extradata 0:       51, 0x5d140df9
 #tb 0: 1/90000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 640x360
 #sar 0: 1/1
-0,      -7200,          0,        0,    22630, 0x9b109541, S=1,        1, 0x00e000e0
+0,      -7200,          0,        0,    22631, 0x9cec9541, S=1,        1, 0x00e000e0
 0,      -3600,      14400,        0,     4021, 0xbf7cdb02, F=0x0, S=1,        1, 0x00e000e0
 0,          0,       7200,        0,     1096, 0x4f162690, F=0x0, S=1,        1, 0x00e000e0
 0,       3600,       3600,        0,      687, 0x00394b95, F=0x0, S=1,        1, 0x00e000e0
