[FFmpeg-devel] [PATCH v1] avcodec/h264_metadata_bsf: Fix user data failed to insert in case no SPSs NAL for global headers

lance.lmwang at gmail.com lance.lmwang at gmail.com
Thu Dec 26 02:55:51 EET 2019


From: Limin Wang <lance.lmwang at gmail.com>

FLV, MP4... will enable global_header default and place SPSs headers in extradata
instead of every keyframe. So it'll failed to insert user data unregisted
for no SPSs NAL after first AU without the patch.

Please test it with below command:
./ffmpeg -f lavfi -i testsrc -c:v libx264 -g 25 \
    -bsf:v h264_metadata=sei_user_data=086f3693-b7b3-4f2c-9653-21492feee5b8+hello \
    -frames:v 150 test.mp4

After applied the patch, you'll get the user data for every keyframe with below command:
./ffmpeg -i test.mp4 -vf showinfo -frames:v 150 -f null -

Signed-off-by: Limin Wang <lance.lmwang at gmail.com>
---
 libavcodec/h264_metadata_bsf.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/libavcodec/h264_metadata_bsf.c b/libavcodec/h264_metadata_bsf.c
index 5de74be9d6..9690ca433b 100644
--- a/libavcodec/h264_metadata_bsf.c
+++ b/libavcodec/h264_metadata_bsf.c
@@ -279,7 +279,7 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
 {
     H264MetadataContext *ctx = bsf->priv_data;
     CodedBitstreamFragment *au = &ctx->access_unit;
-    int err, i, j, has_sps;
+    int err, i, j, has_sps, is_keyframe = 0;
     H264RawAUD aud;
 
     err = ff_bsf_get_packet_ref(bsf, pkt);
@@ -359,11 +359,13 @@ static int h264_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
                 goto fail;
             has_sps = 1;
         }
+        if (au->units[i].type == H264_NAL_IDR_SLICE)
+            is_keyframe = 1;
     }
 
     // Only insert the SEI in access units containing SPSs, and also
     // unconditionally in the first access unit we ever see.
-    if (ctx->sei_user_data && (has_sps || !ctx->done_first_au)) {
+    if (ctx->sei_user_data && (has_sps || !ctx->done_first_au || is_keyframe)) {
         H264RawSEIPayload payload = {
             .payload_type = H264_SEI_TYPE_USER_DATA_UNREGISTERED,
         };
-- 
2.21.0



More information about the ffmpeg-devel mailing list