[FFmpeg-devel] [PATCH 6/6] h264_mp4toannexb_bsf: Try to avoid four byte startcodes

Andreas Rheinhardt andreas.rheinhardt at gmail.com
Wed Jul 24 20:15:57 EEST 2019


According to the H.264 specifications, the only NAL units that need to
have four byte startcodes in H.264 Annex B format are SPS/PPS units and
units that start a new access unit. Before af7e953a, the first of these
conditions wasn't upheld as already existing in-band parameter sets
would not automatically be written with a four byte startcode, but only
when they already were at the beginning of their input packets. But it
made four byte startcodes be used too often as every unit that is written
together with a parameter set that is inserted from extradata received a
four byte startcode although a three byte start code would suffice
unless the unit itself were a parameter set.

FATE has been updated to reflect the changes. Although the patch leaves
the extradata unchanged, the size of the extradata according to the FATE
reports changes. This is due to a quirk in ff_h2645_packet_split which
is used by extract_extradata: If the input is Annex B, the first zero of
a four byte startcode is considered a part of the last unit (if any).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at gmail.com>
---
 libavcodec/h264_mp4toannexb_bsf.c            |  6 +++---
 tests/ref/fate/h264-bsf-mp4toannexb          |  2 +-
 tests/ref/fate/h264_mp4toannexb_ticket2991   | 22 ++++++++++----------
 tests/ref/fate/h264_mp4toannexb_ticket5927   | 10 ++++-----
 tests/ref/fate/h264_mp4toannexb_ticket5927_2 | 10 ++++-----
 tests/ref/fate/segment-mp4-to-ts             |  4 ++--
 6 files changed, 27 insertions(+), 27 deletions(-)

diff --git a/libavcodec/h264_mp4toannexb_bsf.c b/libavcodec/h264_mp4toannexb_bsf.c
index 5d9b71000e..caf690e3e2 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -44,7 +44,7 @@ static int alloc_and_copy(AVPacket *out,
                           const uint8_t *in, uint32_t in_size, int ps)
 {
     uint32_t offset         = out->size;
-    uint8_t start_code_size = offset == 0 || ps ? 4 : 3;
+    uint8_t start_code_size = offset == 0 && sps_pps_size == 0 || ps ? 4 : 3;
     int err;
 
     err = av_grow_packet(out, sps_pps_size + in_size + start_code_size);
@@ -238,7 +238,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
         if (s->new_idr && unit_type == H264_NAL_IDR_SLICE && !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, 1)) < 0)
+                               buf, nal_size, 0)) < 0)
                 goto fail;
             s->new_idr = 0;
         /* if only SPS has been seen, also insert PPS */
@@ -249,7 +249,7 @@ static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
                     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, 1)) < 0)
+                                        buf, nal_size, 0)) < 0)
                 goto fail;
         } else {
             if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size, unit_type == H264_NAL_SPS || unit_type == H264_NAL_PPS)) < 0)
diff --git a/tests/ref/fate/h264-bsf-mp4toannexb b/tests/ref/fate/h264-bsf-mp4toannexb
index 7cd086a268..2049f39701 100644
--- a/tests/ref/fate/h264-bsf-mp4toannexb
+++ b/tests/ref/fate/h264-bsf-mp4toannexb
@@ -1 +1 @@
-f340e7ca9a46d437af4e96f6c8de221c
+5f04c27cc6ee8625fe2405fb0f7da9a3
diff --git a/tests/ref/fate/h264_mp4toannexb_ticket2991 b/tests/ref/fate/h264_mp4toannexb_ticket2991
index 3245ef442c..76bdf3cae7 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket2991
+++ b/tests/ref/fate/h264_mp4toannexb_ticket2991
@@ -1,12 +1,12 @@
-dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h264
-1985823 tests/data/fate/h264_mp4toannexb_ticket2991.h264
-#extradata 0:       48, 0x47ae0d55
+05d66e60ab22ee004720e0051af0fe74 *tests/data/fate/h264_mp4toannexb_ticket2991.h264
+1985815 tests/data/fate/h264_mp4toannexb_ticket2991.h264
+#extradata 0:       47, 0x3a590d55
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1280x720
 #sar 0: 3/4
-0,          0,          0,    48000,    37127, 0xc125184c
+0,          0,          0,    48000,    37126, 0xb020184c
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    45292, 0x66dd2cf6
+0,     608566,     608566,    40040,    45291, 0x543c2cf6
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    54297, 0xf98d30ed
+0,    1209172,    1209172,    40040,    54296, 0xe6ec30ed
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    23998, 0xee87d5c4
+0,    1809778,    1809778,    40040,    23997, 0xdbe6d5c4
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    29135, 0x0a34be18
+0,    2410384,    2410384,    40040,    29134, 0xf784be18
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    48673, 0x44b6ce46
+0,    3010990,    3010990,    40040,    48672, 0x3215ce46
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    37237, 0xfa9a24b1
+0,    3611596,    3611596,    40040,    37236, 0xe7f924b1
 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 @@ dba672c154b41414cf26aae967c27eef *tests/data/fate/h264_mp4toannexb_ticket2991.h2
 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,    29391, 0x0955ed6b
+0,    4212202,    4212202,    40040,    29390, 0xf6a5ed6b
 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 006ea398fd..95e35c4d80 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket5927
+++ b/tests/ref/fate/h264_mp4toannexb_ticket5927
@@ -1,12 +1,12 @@
-562487bfea635cdadbc23d390322b589 *tests/data/fate/h264_mp4toannexb_ticket5927.h264
-595585 tests/data/fate/h264_mp4toannexb_ticket5927.h264
-#extradata 0:       34, 0x8df608f8
+a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927.h264
+595583 tests/data/fate/h264_mp4toannexb_ticket5927.h264
+#extradata 0:       33, 0x84fe08f8
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,     -48000, -9223372036854775808,    48000,   247994, 0x2e1e21ea
+0,     -48000, -9223372036854775808,    48000,   247993, 0x1ce821ea
 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 @@
 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,     6370, 0x96c12aa1
+0,     528000, -9223372036854775808,    48000,     6369, 0x858b2aa1
diff --git a/tests/ref/fate/h264_mp4toannexb_ticket5927_2 b/tests/ref/fate/h264_mp4toannexb_ticket5927_2
index 51432b1535..8db6a7e54a 100644
--- a/tests/ref/fate/h264_mp4toannexb_ticket5927_2
+++ b/tests/ref/fate/h264_mp4toannexb_ticket5927_2
@@ -1,12 +1,12 @@
-562487bfea635cdadbc23d390322b589 *tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
-595585 tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
-#extradata 0:       34, 0x8df608f8
+a3b02fd09392e01619cebc959d4d9ff2 *tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
+595583 tests/data/fate/h264_mp4toannexb_ticket5927_2.h264
+#extradata 0:       33, 0x84fe08f8
 #tb 0: 1/1200000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 1920x1080
 #sar 0: 0/1
-0,     -48000, -9223372036854775808,    48000,   247994, 0x2e1e21ea
+0,     -48000, -9223372036854775808,    48000,   247993, 0x1ce821ea
 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 @@
 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,     6370, 0x96c12aa1
+0,     528000, -9223372036854775808,    48000,     6369, 0x858b2aa1
diff --git a/tests/ref/fate/segment-mp4-to-ts b/tests/ref/fate/segment-mp4-to-ts
index b5accb60f7..847c1a297d 100644
--- a/tests/ref/fate/segment-mp4-to-ts
+++ b/tests/ref/fate/segment-mp4-to-ts
@@ -1,10 +1,10 @@
-#extradata 0:       51, 0x5d140df9
+#extradata 0:       50, 0x4f1b0df9
 #tb 0: 1/90000
 #media_type 0: video
 #codec_id 0: h264
 #dimensions 0: 640x360
 #sar 0: 1/1
-0,      -7200,          0,        0,    22631, 0x9cec9541, S=1,        1, 0x00e000e0
+0,      -7200,          0,        0,    22630, 0x9b109541, 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
-- 
2.21.0



More information about the ffmpeg-devel mailing list