美文网首页FFmpeg笔记
ffmpeg 添加 sps pps

ffmpeg 添加 sps pps

作者: YellowLayne | 来源:发表于2018-03-22 10:23 被阅读0次

分离某些封装格式(例如MP4/FLV/MKV等)中的H.264的时候,需要首先写入SPS和PPS,否则会导致分离出来的数据没有SPS、PPS而无法播放。H.264码流的SPS和PPS信息存储在AVCodecContext结构体的extradata中。需要使用ffmpeg中名称为“h264_mp4toannexb”的bitstream filter处理。
原有的API已被弃用,新的API如下:

// Query
const AVBitStreamFilter *av_bsf_next(void **opaque);
const AVBitStreamFilter *av_bsf_get_by_name(const char *name);

// Setup
int av_bsf_alloc(const AVBitStreamFilter *filter, AVBSFContext **ctx);
int av_bsf_init(AVBSFContext *ctx);

// Usage
int av_bsf_send_packet(AVBSFContext *ctx, AVPacket *pkt);
int av_bsf_receive_packet(AVBSFContext *ctx, AVPacket *pkt);

// Cleanup
void av_bsf_free(AVBSFContext **ctx);

Query

You can enumerate the available filters

void *state = NULL;

const AVBitStreamFilter *bsf;

while ((bsf = av_bsf_next(&state)) {
    av_log(NULL, AV_LOG_INFO, "%s\n", bsf->name);
}
or directly pick the one you need by name:

const AVBitStreamFilter *bsf = av_bsf_get_by_name("hevc_mp4toannexb");

Setup

A bsf may use some codec parameters and time_base and provide updated ones.

AVBSFContext *ctx;

ret = av_bsf_alloc(bsf, &ctx);
if (ret < 0)
    return ret;

ret = avcodec_parameters_copy(ctx->par_in, in->codecpar);
if (ret < 0)
    goto fail;

ctx->time_base_in = in->time_base;

ret = av_bsf_init(ctx);
if (ret < 0)
    goto fail;

ret = avcodec_parameters_copy(out->codecpar, ctx->par_out);
if (ret < 0)
    goto fail;

out->time_base = ctx->time_base_out;

Usage

Multiple AVPackets may be consumed before an AVPacket is emitted or multiple AVPackets may be produced out of a single input one.

AVPacket *pkt;

while (got_new_packet(&pkt)) {
    ret = av_bsf_send_packet(ctx, pkt);
    if (ret < 0)
        goto fail;

    while ((ret = av_bsf_receive_packet(ctx, pkt)) == 0) {
        yield_packet(pkt);
    }

    if (ret == AVERROR(EAGAIN)
        continue;
    IF (ret == AVERROR_EOF)
        goto end;
    if (ret < 0)
        goto fail;
}

// Flush
ret = av_bsf_send_packet(ctx, NULL);
if (ret < 0)
    goto fail;

while ((ret = av_bsf_receive_packet(ctx, pkt)) == 0) {
    yield_packet(pkt);
}

if (ret != AVERROR_EOF)
    goto fail;
In order to signal the end of stream a NULL pkt should be fed to send_packet.

Cleanup

The cleanup function matches the av_freep signature so it takes the address of the AVBSFContext pointer.

    av_bsf_free(&ctx);
All the memory is freed and the ctx pointer is set to NULL.

ps: FFmpeg给出的例子中并未while循环调用av_bsf_receive_packet,也未对其flush。

https://blogs.gentoo.org/lu_zero/2016/03/21/bitstream-filtering/

相关文章

  • ffmpeg 添加 sps pps

    分离某些封装格式(例如MP4/FLV/MKV等)中的H.264的时候,需要首先写入SPS和PPS,否则会导致分离出...

  • ffmpeg 解码 nalu

    sps + pps +IDR +IDR 是不行的每一个IDR前面必须添加sps和pps(to feed ffmpe...

  • openH264 注意事项

    sps pps openH264 默认sps和pps不是固定的,默认为INCREASING_ID,SPS/PPS ...

  • [FFMPEG]SPS和PPS

    使用RTP传输H264的时候,需要用到sdp协议描述,其中有两项:Sequence Parameter Sets ...

  • ffmpeg为AVPacket添加解码头信息

    FFmpeg解码获得的AVPacket只包含视频压缩数据,并没有包含相关的解码信息(比如:h264的sps pps...

  • SPS、PPS

    在 H.264 流中,有两种 NALU 极其重要 序列参数集 (Sequence Paramater Set, S...

  • 将mp4 转flv

    视频 sps pps 宽度 高度 写头 写尾

  • swift 视频硬解码

    swift 视频硬解码 代码如下: 根据sps , pps 获取解码参数 letsps_count = sps.c...

  • Android 音视频02 --- H264的基本原理02

    一. 音视频编码中的sps与pps的设计 SPS和PPS ,包含了初始化H.264解码器所需要的信息参数,包括编码...

  • 从 AVCodecContext中获取sps,pps(FFmpe

    我一开始以为FFMPEG的这个方法会直接获取到SPS和PPS,谁知道只是替换掉开始码。 这里我们需要明白我们需要的...

网友评论

    本文标题:ffmpeg 添加 sps pps

    本文链接:https://www.haomeiwen.com/subject/tjgoqftx.html