在使用
ffmpeg -i test.mp4 -ss 0 -t 10 -acodec copy -vcodec copy -vbsf h264_mp4toannexb -y test.ts
命令的时候, 部分视频会出现异常:
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
Failed to open bitstream filter h264_mp4toannexb for stream 0 with codec copy: Invalid argument
[mpegts @ 0x14eb0980] H.264 bitstream malformed, no startcode found, use the h264_mp4toannexb bitstream filter
av_interleaved_write_frame(): Invalid data found when processing input
需要调整
libavcodec/h264_mp4toannexb_bsf.c
解决。
调整为:
/* retrieve length coded size */
ctx->length_size = (*extradata++ & 0x3) + 1;
//@alex
// if (ctx->length_size == 3)
// return AVERROR(EINVAL);
以及:
*poutbuf_size = 0;
*poutbuf = NULL;
do {
ret= AVERROR(EINVAL);
if (buf + ctx->length_size > buf_end)
goto fail;
if (ctx->length_size == 1) {
nal_size = buf[0];
} else if (ctx->length_size == 2) {
nal_size = AV_RB16(buf);
//@alex
// } else
// nal_size = AV_RB32(buf);
} else {
for(nal_size = 0, unit_type = 0; unit_type < ctx->length_size; unit_type++)
nal_size = (nal_size << 8) | buf[unit_type];
}
参考:
http://ffmpeg-users.933282.n4.nabble.com/MPEG-TS-trouble-td4491337.html
there is a bug in libavcodec/h264_mp4toannexb_bsf.c
when the ctx->length_size == 3 , the filter will report the error
"Invalid argument"
diff --git a/libavcodec/h264_mp4toannexb_bsf.c
b/libavcodec/h264_mp4toannexb_bsf.c
index 5085ecb..fa16f2a 100644
--- a/libavcodec/h264_mp4toannexb_bsf.c
+++ b/libavcodec/h264_mp4toannexb_bsf.c
@@ -82,8 +82,8 @@ static int
h264_mp4toannexb_filter(AVBitStreamFilterContext *bsfc,
/* retrieve length coded size */
ctx->length_size = (*extradata++ & 0x3) + 1;
- if (ctx->length_size == 3)
- return AVERROR(EINVAL);
+ // if (ctx->length_size == 3)
+ // return AVERROR(EINVAL);
/* retrieve sps and pps unit(s) */
unit_nb = *extradata++ & 0x1f; /* number of sps unit(s) */
@@ -146,8 +146,10 @@ pps:
nal_size = buf[0];
} else if (ctx->length_size == 2) {
nal_size = AV_RB16(buf);
- } else
- nal_size = AV_RB32(buf);
+ } else {
+ for(nal_size = 0, unit_type = 0;
unit_type<ctx->length_size; unit_type++)
+ nal_size = (nal_size << 8) | buf[unit_type];
+ }
buf += ctx->length_size;
unit_type = *buf & 0x1f;