博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
FFmpeg—— Bitstream Filters 作用
阅读量:4984 次
发布时间:2019-06-12

本文共 4744 字,大约阅读时间需要 15 分钟。

原文链接:

 

Let me explain by example. FFmpeg video decoders typically work by converting one video frame per call to avcodec_decode_video2. So the input is expected to be "one image" worth of bitstream data. Let's consider this issue of going from a file (an array of bytes of disk) to images for a second.

For "raw" (annexb) H264 (.h264/.bin/.264 files), the individual nal unit data (sps/pps header bitstreams or cabac-encoded frame data) is concatenated in a sequence of nal units, with a start code (00 00 01 XX) in between, where XX is the nal unit type. (In order to prevent the nal data itself to have 00 00 01 data, it is RBSP escaped.) So a can simply cut the file at start code markers. They search for successive packets that start with and including 00 00 01, until and excluding the next occurence of 00 00 01. Then they parse the nal unit type and slice header to find which frame each packet belongs to, and return a set of nal units making up one frame as input to the .

H264 data in .mp4 files is different, though. You can imagine that the 00 00 01 start code can be considered redundant if the muxing format already has length markers in it, as is the case for mp4. So, to save 3 bytes per frame, they remove the 00 00 01 prefix. They also put the PPS/SPS in the file header instead of prepending it before the first frame, and these also miss their 00 00 01 prefixes. So, if I were to input this into the h264 decoder, which expects the prefixes for all nal units, it wouldn't work. The bitstream filter fixes this, by identifying the pps/sps in the extracted parts of the file header (ffmpeg calls this "extradata"), prepending this and each nal from individual frame packets with the start code, and concatenating them back together before inputting them in the h264 decoder.

You might now feel that there's a very fine line distinction between a "parser" and a "bitstream filter". This is true. I think the official definition is that a parser takes a sequence of input data and splits it in frames without discarding any data or adding any data. The only thing a parser does is change packet boundaries. A bitstream filter, on the other hand, is allowed to actually modify the data. I'm not sure this definition is entirely true (see e.g. vp9 below), but it's the conceptual reason mp4toannexb is a BSF, not a parser (because it adds 00 00 01 prefixes).

Other cases where such "bitstream tweaks" help keep decoders simple and uniform, but allow us to support all files variants that happen to exist in the wild:

  • mpeg4 (divx) (to get B-frames sequences like IBP, which are coded as IPB, in AVI and get timestamps correct, people came up with this concept of B-frame packing where I-B-P / I-P-B is packed in frames as , i.e. the third packet is empty and the second has two frames. This means the timestamp associated with the P and B frame at the decoding phase is correct. It also means you have two frames worth of input data for one packet, which violates ffmpeg's one-frame-in-one-frame-out concept, so we wrote a bsf to split the packet back in two - along with deleting the marker that says that the packet contains two frames, hence a BSF and not a parser - before inputting it into the decoder. In practice, this solves otherwise hard problems with frame multithreading. VP9 does the same thing (called superframes), but splits frames in the , so the parser/BSF split isn't always theoretically perfect; maybe VP9's should be called a BSF)
  • hevc mp4 to annexb conversion (same story as above, but for hevc)
  • aac conversion (this is basically the same as h264/hevc annexb vs. mp4, but for aac audio)

 

分离某些封装格式(例如MP4/FLV/MKV等)中的H.264的时候,需要首先写入SPS和PPS,否则会导致分离出来的数据没有SPS、PPS而无法播放。H.264码流的SPS和PPS信息存储在AVCodecContext结构体的extradata中。
需要使用FFmpeg中名称为 “h264_mp4toannexb" 等的 Bitstream Filter 处理。
 
旧的API已经被弃用,如
AVBitStreamFilterContext *av_bitstream_filter_init(const char *name);int av_bitstream_filter_filter(AVBitStreamFilterContext *bsfc,                               AVCodecContext *avctx, const char *args,                               uint8_t **poutbuf, int *poutbuf_size,                               const uint8_t *buf, int buf_size, int keyframe);

 

新版需要使用如下API实现功能:

// Get filterconst AVBitStreamFilter *av_bsf_next(void **opaque);const AVBitStreamFilter *av_bsf_get_by_name(const char *name);// Init filterint av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx);int avcodec_parameters_copy(AVCodecParameters *dst, const AVCodecParameters *src);int av_bsf_init(AVBSFContext *ctx);// Use filterint av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt);int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt);// Freevoid av_bsf_free(AVBSFContext **ctx);

 

 
 

转载于:https://www.cnblogs.com/zoneofmine/p/10795025.html

你可能感兴趣的文章
Druid连接池错误(数据库版本问题)
查看>>
console对象-转
查看>>
洛谷 4216 BZOJ 4448 [SCOI2015]情报传递
查看>>
清北学堂2018DP&图论精讲班 DP部分学习笔记
查看>>
css3 2D变换 transform
查看>>
Fastjson获取天气信息封装bean
查看>>
不同编码字符所占大小
查看>>
使用迭代器优化代码
查看>>
JavaScript 获取随机数
查看>>
线程学习的几个实例
查看>>
dom4j读取XML文件内容
查看>>
Java虚拟机10:Client模式和Server模式的区别
查看>>
Blog搬家吧
查看>>
2017-2018-1 20155306 20155315《信息安全系统设计基础》实验二 固件程序设计
查看>>
自定义连接池
查看>>
MySQL 索引
查看>>
应用程序不能全然结束的原因探秘及调试方法
查看>>
单元文件结构
查看>>
DOM、SAX、DOM4J、JDOM、StAX生成XML并返回XML字符串形式
查看>>
60. Permutation Sequence
查看>>