如何使用 ffmpeg 从 H264 SPS 获取宽度和高度
How to get width and height from the H264 SPS using ffmpeg
我正在尝试初始化一个 FFMPEG H264 编解码器上下文,用 SPS 帧填充额外数据字段,如下所示:
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
int main()
{
const char sps[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2};
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
AVCodec *const codec = avcodec_find_decoder(CODEC_ID_H264);
if (codec != NULL)
{
AVCodecContext* ctx = avcodec_alloc_context3(codec);
ctx->debug = ~0;
ctx->extradata = (uint8_t *)av_malloc(sizeof(sps) + FF_INPUT_BUFFER_PADDING_SIZE);
ctx->extradata_size = sizeof(sps);
memcpy(ctx->extradata,sps,sizeof(sps));
memset(&ctx->extradata[ctx->extradata_size], 0, FF_INPUT_BUFFER_PADDING_SIZE);
if (avcodec_open2(ctx, codec, NULL) < 0)
{
fprintf(stderr, "Failed to open codec\n");
}
else
{
char buf[1024];
avcodec_string(buf,sizeof(buf),ctx,1);
fprintf(stderr, "%s\n", buf);
}
avcodec_close(ctx);
av_free(ctx);
}
}
程序的输出是:
[h264 @ 0xc74010] NAL 7/3 at 4/11 length 6
[h264 @ 0xc74010] sps:0 profile:66/10 poc:0 ref:0 8x6 FRM crop:0/0/0/0 420 0/0 b8 reo:-1
Video: h264, 1 reference frame, none(left), q=2-31, 200 kb/s
输出显示 sps 已使用计算宽度和高度所需的信息解码 h264_ps.c (mb_width=8, mb_height=6, crop_left= crop_right=crop_top=crop_bottom=0)。
然后我期待得到宽度和高度调用 avcodec_string。
有没有办法在不解码帧的情况下做到这一点?
它在 AVCodecContext.width 中可用,并且在 avcodec_open2() 之后的 AVCodecContext.height 中可用,前提是您正确地格式化了额外数据。
这个post会告诉你应该如何格式化
Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream
为解码器提供虚拟缓冲区可以获取在 SPS 中编码的大小。
使用空 PPS 和空的非 IDR SLICE :
int got_frame = 0;
AVPacket pkt;
av_init_packet(&pkt);
char buffer[] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xff, 0x00, 0x00, 0x00, 0x01, 0x21};
pkt.data = buffer;
pkt.size = sizeof(buffer);
AVFrame* frame = av_frame_alloc();
avcodec_decode_video2(ctx, frame, &got_frame, &pkt);
av_frame_free(&frame);
fprintf(stderr, "size: %dx%d\n", ctx->width, ctx->height);
运行 此代码打印错误,但它允许从 SPS 中提取宽度和高度:
[h264 @ 0xd0d040] Missing reference picture, default is 0
[h264 @ 0xd0d040] decode_slice_header error
size: 128x96
我正在尝试初始化一个 FFMPEG H264 编解码器上下文,用 SPS 帧填充额外数据字段,如下所示:
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
int main()
{
const char sps[] = {0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2};
av_register_all();
av_log_set_level(AV_LOG_DEBUG);
AVCodec *const codec = avcodec_find_decoder(CODEC_ID_H264);
if (codec != NULL)
{
AVCodecContext* ctx = avcodec_alloc_context3(codec);
ctx->debug = ~0;
ctx->extradata = (uint8_t *)av_malloc(sizeof(sps) + FF_INPUT_BUFFER_PADDING_SIZE);
ctx->extradata_size = sizeof(sps);
memcpy(ctx->extradata,sps,sizeof(sps));
memset(&ctx->extradata[ctx->extradata_size], 0, FF_INPUT_BUFFER_PADDING_SIZE);
if (avcodec_open2(ctx, codec, NULL) < 0)
{
fprintf(stderr, "Failed to open codec\n");
}
else
{
char buf[1024];
avcodec_string(buf,sizeof(buf),ctx,1);
fprintf(stderr, "%s\n", buf);
}
avcodec_close(ctx);
av_free(ctx);
}
}
程序的输出是:
[h264 @ 0xc74010] NAL 7/3 at 4/11 length 6
[h264 @ 0xc74010] sps:0 profile:66/10 poc:0 ref:0 8x6 FRM crop:0/0/0/0 420 0/0 b8 reo:-1
Video: h264, 1 reference frame, none(left), q=2-31, 200 kb/s
输出显示 sps 已使用计算宽度和高度所需的信息解码 h264_ps.c (mb_width=8, mb_height=6, crop_left= crop_right=crop_top=crop_bottom=0)。
然后我期待得到宽度和高度调用 avcodec_string。
有没有办法在不解码帧的情况下做到这一点?
它在 AVCodecContext.width 中可用,并且在 avcodec_open2() 之后的 AVCodecContext.height 中可用,前提是您正确地格式化了额外数据。
这个post会告诉你应该如何格式化 Possible Locations for Sequence/Picture Parameter Set(s) for H.264 Stream
为解码器提供虚拟缓冲区可以获取在 SPS 中编码的大小。
使用空 PPS 和空的非 IDR SLICE :
int got_frame = 0;
AVPacket pkt;
av_init_packet(&pkt);
char buffer[] = {0x00, 0x00, 0x00, 0x01, 0x68, 0xff, 0x00, 0x00, 0x00, 0x01, 0x21};
pkt.data = buffer;
pkt.size = sizeof(buffer);
AVFrame* frame = av_frame_alloc();
avcodec_decode_video2(ctx, frame, &got_frame, &pkt);
av_frame_free(&frame);
fprintf(stderr, "size: %dx%d\n", ctx->width, ctx->height);
运行 此代码打印错误,但它允许从 SPS 中提取宽度和高度:
[h264 @ 0xd0d040] Missing reference picture, default is 0 [h264 @ 0xd0d040] decode_slice_header error size: 128x96