FFmpeg里的Bitstream Filter

Bitstream Filter

Bitstream Filter: A bitstream filter operates on the encoded stream data, and performs bitstream level modifications without performing decoding.

  1. 比特流滤波器作用在编码后的数据上,而不是未压缩的数据。
  2. 在不解码的基础上执行比特流级别的修改。

示例1:

ffmpeg -i input.mp4 -codec copy -bsf:v h264_mp4toannexb output.ts

这个是把mp4容器格式改为ts容器格式,需要把h.264的封装格式从mp4改成annexb。

h.264编码有两种封装格式

  1. 一种是annexb模式, 它是传统模式, 有startcode, SPS和PPS在Element Stream中.
  2. 另一种是mp4模式, 一般Mp4, MKV, AVI都没有startcode, SPS和PPS以及其他信息被封装容器中. 每一帧前面是这一帧的长度值, 很多解码器只支持annexb模式, 因此需要对Mp4模式做转换.

再说下为什么另一种封装格式叫annexb,是因为这种封装格式定义在the Annex B of the ITU-T H.264 specification

示例2:

ffmpeg -i in.264 -c copy -bsf:v trace_headers -f null - 2> NALUS.txt

支持 AV1,H.264, H.265, (M)JPEG, MPEG-2, VP9(VP8不支持)

Log trace output containing all syntax elements in the coded stream headers (everything above the level of individual coded blocks). This can be useful for debugging low-level stream issues.
可以用于底层码流调试。

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现bitstream filter可以使用FFmpeg中的AVBitStreamFilter结构体,它定义了一些函数指针,可以实现对码流数据的修改、删除和添加等操作。 对于h264和hevc码流数据的处理,可以使用相应的bitstream filter,如h264_mp4toannexb、h264_annexbtomp4、hevc_mp4toannexb、hevc_annexbtomp4等,这些filter可以将码流数据转换为不同的格式,或者将码流数据中的特定信息进行删除或添加,从而产生多种类型的错误码流。 具体实现步骤如下: 1. 创建AVBitStreamFilterContext对象,并分别打开h264_mp4toannexb和hevc_mp4toannexb bitstream filter。 ``` AVBitStreamFilterContext *h264bsfc = av_bitstream_filter_init("h264_mp4toannexb"); AVBitStreamFilterContext *hevcbsfc = av_bitstream_filter_init("hevc_mp4toannexb"); ``` 2. 读取输入码流数据,调用对应的bitstream filter进行处理。 ``` AVPacket pkt; while (av_read_frame(input_fmt_ctx, &pkt) >= 0) { if (pkt.stream_index == video_stream_index) { if (pkt.codec_id == AV_CODEC_ID_H264) { av_bitstream_filter_filter(h264bsfc, input_codec_ctx, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0); } else if (pkt.codec_id == AV_CODEC_ID_HEVC) { av_bitstream_filter_filter(hevcbsfc, input_codec_ctx, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0); } // ... } av_packet_unref(&pkt); } ``` 3. 将处理后的码流数据写入输出文件。 ``` AVPacket filtered_pkt; av_init_packet(&filtered_pkt); filtered_pkt.data = pkt.data; filtered_pkt.size = pkt.size; if (pkt.codec_id == AV_CODEC_ID_H264) { av_bitstream_filter_filter(h264bsfc, output_codec_ctx, NULL, &filtered_pkt.data, &filtered_pkt.size, filtered_pkt.data, filtered_pkt.size, 0); } else if (pkt.codec_id == AV_CODEC_ID_HEVC) { av_bitstream_filter_filter(hevcbsfc, output_codec_ctx, NULL, &filtered_pkt.data, &filtered_pkt.size, filtered_pkt.data, filtered_pkt.size, 0); } av_interleaved_write_frame(output_fmt_ctx, &filtered_pkt); ``` 注意,为了产生不同类型的错误码流,可以在调用bitstream filter时传递不同的参数,例如改变输出码流的分辨率、帧率、码率等等。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值