ffmpeg读取H264的sps、pps




sps、pps在avcC中,保存为:
AVFormatContext->streams[H264Index]->codec->extradata

1、读取代码
     for(int i=0; i<ifmt_ctx->nb_streams; i++)
    {
        AVCodecContext *codec = ifmt_ctx->streams[i]->codec;
        if(codec->codec_type==AVMEDIA_TYPE_VIDEO && codec->codec_id==AV_CODEC_ID_H264 )
        {
            int j;
            if(codec->extradata_size==0 || codec->extradata_size >184) 
                continue;   // some h264 extradata is null and extradata_size is error

            for (j=0; j<codec->extradata_size; j++)  
            {  
                printf("%02x ", codec->extradata[j]);  
            }
            printf("\n   codec id=%d, extradata_size=%d\n", codec->codec_id, codec->extradata_size);
        }
    }
    打印 范例 文件( saiche_h264.ts )的avcC,结果如下:
00 00 00 01 09 f0 00 00 00 01 67 64 00 33 ac d9 40 3c 00 43 ec 04 40 00 00 fa 40
 00 3a 98 1a 38 00 3d 09 00 07 a1 39 a8 c0 1e 30 63 2c 00 00 00 01 68 ea ef 2c

2、相关数据结构

struct AVFormatContext 
{
    const AVClass *av_class;

    struct AVInputFormat *iformat;
    struct AVOutputFormat *oformat;

    /**
     * Format private data. This is an AVOptions-enabled struct
     * if and only if iformat/oformat.priv_class is not NULL.
     *
     * - muxing: set by avformat_write_header()
     * - demuxing: set by avformat_open_input()
     */
    void *priv_data;

    AVIOContext *pb;  // I/O context.

    unsigned int nb_streams; //Number of elements in AVFormatContext.streams
     AVStream **streams;
};

struct AVStream
{
    int index;    //< stream index in AVFormatContext
    int id;       //Format-specific stream ID.
     AVCodecContext *codec;
    void *priv_data;

    AVRational  time_base;
    AVRational avg_frame_rate;
    AVRational r_frame_rate;
    int stream_identifier;
    ...
};

struct AVCodecContext
{
    const AVClass *av_class;
    int log_level_offset;

     enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */
    const struct AVCodec  *codec;
     enum AVCodecID     codec_id; /* see AV_CODEC_ID_xxx */

    /**
     * fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
     * This is used to work around some encoder bugs.
     * A demuxer should set this to what is stored in the field used to identify the codec.
     * If there are multiple such fields in a container then the demuxer should choose the one
     * which maximizes the information about the used codec.
     * If the codec tag field in a container is larger than 32 bits then the demuxer should
     * remap the longer ID to 32 bits with a table or other structure. Alternatively a new
     * extra_codec_tag + size could be added but for this a clear advantage must be demonstrated
     * first.
     * - encoding: Set by user, if not then the default based on codec_id will be used.
     * - decoding: Set by user, will be converted to uppercase by libavcodec during init.
     */
    unsigned int codec_tag;

    /**
     * fourcc from the AVI stream header (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
     * This is used to work around some encoder bugs.
     * - encoding: unused
     * - decoding: Set by user, will be converted to uppercase by libavcodec during init.
     */
    unsigned int stream_codec_tag;

    ...

     /**
     * Private data of the user, can be used to carry app specific stuff.
     * - encoding: Set by user.
     * - decoding: Set by user.
     */
    void *opaque;

    /**
     * the average bitrate
     * - encoding: Set by user; unused for constant quantizer encoding.
     * - decoding: Set by libavcodec. 0 or some bitrate if this info is available in the stream.
     */
    int  bit_rate;

    /**
     * some codecs need / can use extradata like Huffman tables.
     * mjpeg: Huffman tables
     * rv10: additional flags
     * mpeg4: global headers (they can be in the bitstream or here)
     * The allocated memory should be FF_INPUT_BUFFER_PADDING_SIZE bytes larger
     * than extradata_size to avoid problems if it is read with the bitstream reader.
     * The bytewise contents of extradata must not depend on the architecture or CPU endianness.
     * - encoding: Set/allocated/freed by libavcodec.
     * - decoding: Set/allocated/freed by user.
     */
     uint8_t *extradata;
     int extradata_size;
};


sps、pps在avcC中,保存为:
AVFormatContext->streams[H264Index]->codec->extradata

1、读取代码
     for(int i=0; i<ifmt_ctx->nb_streams; i++)
    {
        AVCodecContext *codec = ifmt_ctx->streams[i]->codec;
        if(codec->codec_type==AVMEDIA_TYPE_VIDEO && codec->codec_id==AV_CODEC_ID_H264 )
        {
            int j;
            if(codec->extradata_size==0 || codec->extradata_size >184) 
                continue;   // some h264 extradata is null and extradata_size is error

            for (j=0; j<codec->extradata_size; j++)  
            {  
                printf("%02x ", codec->extradata[j]);  
            }
            printf("\n   codec id=%d, extradata_size=%d\n", codec->codec_id, codec->extradata_size);
        }
    }
    打印 范例 文件( saiche_h264.ts )的avcC,结果如下:
00 00 00 01 09 f0 00 00 00 01 67 64 00 33 ac d9 40 3c 00 43 ec 04 40 00 00 fa 40
 00 3a 98 1a 38 00 3d 09 00 07 a1 39 a8 c0 1e 30 63 2c 00 00 00 01 68 ea ef 2c

2、相关数据结构

struct AVFormatContext 
{
    const AVClass *av_class;

    struct AVInputFormat *iformat;
    struct AVOutputFormat *oformat;

    /**
     * Format private data. This is an AVOptions-enabled struct
     * if and only if iformat/oformat.priv_class is not NULL.
     *
     * - muxing: set by avformat_write_header()
     * - demuxing: set by avformat_open_input()
     */
    void *priv_data;

    AVIOContext *pb;  // I/O context.

    unsigned int nb_streams; //Number of elements in AVFormatContext.streams
     AVStream **streams;
};

struct AVStream
{
    int index;    //< stream index in AVFormatContext
    int id;       //Format-specific stream ID.
     AVCodecContext *codec;
    void *priv_data;

    AVRational  time_base;
    AVRational avg_frame_rate;
    AVRational r_frame_rate;
    int stream_identifier;
    ...
};

struct AVCodecContext
{
    const AVClass *av_class;
    int log_level_offset;

     enum AVMediaType codec_type; /* see AVMEDIA_TYPE_xxx */
    const struct AVCodec  *codec;
     enum AVCodecID     codec_id; /* see AV_CODEC_ID_xxx */

    /**
     * fourcc (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
     * This is used to work around some encoder bugs.
     * A demuxer should set this to what is stored in the field used to identify the codec.
     * If there are multiple such fields in a container then the demuxer should choose the one
     * which maximizes the information about the used codec.
     * If the codec tag field in a container is larger than 32 bits then the demuxer should
     * remap the longer ID to 32 bits with a table or other structure. Alternatively a new
     * extra_codec_tag + size could be added but for this a clear advantage must be demonstrated
     * first.
     * - encoding: Set by user, if not then the default based on codec_id will be used.
     * - decoding: Set by user, will be converted to uppercase by libavcodec during init.
     */
    unsigned int codec_tag;

    /**
     * fourcc from the AVI stream header (LSB first, so "ABCD" -> ('D'<<24) + ('C'<<16) + ('B'<<8) + 'A').
     * This is used to work around some encoder bugs.
     * - encoding: unused
     * - decoding: Set by user, will be converted to uppercase by libavcodec during init.
     */
    unsigned int stream_codec_tag;

    ...

     /**
     * Private data of the user, can be used to carry app specific stuff.
     * - encoding: Set by user.
     * - decoding: Set by user.
     */
    void *opaque;

    /**
     * the average bitrate
     * - encoding: Set by user; unused for constant quantizer encoding.
     * - decoding: Set by libavcodec. 0 or some bitrate if this info is available in the stream.
     */
    int  bit_rate;

    /**
     * some codecs need / can use extradata like Huffman tables.
     * mjpeg: Huffman tables
     * rv10: additional flags
     * mpeg4: global headers (they can be in the bitstream or here)
     * The allocated memory should be FF_INPUT_BUFFER_PADDING_SIZE bytes larger
     * than extradata_size to avoid problems if it is read with the bitstream reader.
     * The bytewise contents of extradata must not depend on the architecture or CPU endianness.
     * - encoding: Set/allocated/freed by libavcodec.
     * - decoding: Set/allocated/freed by user.
     */
     uint8_t *extradata;
     int extradata_size;
};

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值