[FFmpeg-devel] How to get width/height of an H264 stream using its SPS ?

Ronald S. Bultje rsbultje at gmail.com
Thu Jul 30 14:52:06 CEST 2015


Hi,

On Thu, Jul 30, 2015 at 5:16 AM, PROMONET Michel <
michel.promonet at thalesgroup.com> wrote:

>             Hi,
>
> I don't know if this is the right place to ask for this kind of
> information.
> Basically I am trying to get width and height from the H264 SPS using
> ffmpeg.
>
> Using a reduced sample like this :
>
> #include <libavcodec/avcodec.h>
> #include <libavformat/avformat.h>
>
> int main()
> {
>             const char sps[] = {0, 0, 0, 1, 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);
>             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); // zero padding
>
> avcodec_open2(ctx, codec, NULL);
>             char buf[1024];
>             avcodec_string(buf,sizeof(buf),ctx,1);
>             fprintf(stderr, "%s\n", buf);
> fprintf(stderr, "size:%dx%d\n", ctx->width, ctx->height);
>
>             avcodec_close(ctx);
>             av_free(ctx);
> }
>
> The output of this program is :
> [h264 @ 0x68a010] NAL 7/3 at 4/11 length 6
> [h264 @ 0x68a010] 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
> size:0x0 0x0
>
> We can see the SPS is decoded and have the needed information to compute
> width and height (it should be 128x96).
> However the avcodec_open2 only fill the opaque H264Context, but does not
> fill the AVCodecContext information.
>
> Is there a way to decode the SPS, without decoding a frame ?


No, avctx->width is set upon slice header decoding. You'll have to decode
one frame.

Ronald


More information about the ffmpeg-devel mailing list