ffmpeg手动添加adts头部信息

ffmpeg手动添加adts头部信息
ffmpeg adts 头部信息都要从 extradata 字段里面提取 。
aac数据包为什么adts 要加头 ,因为没有头部信息在网络传播过程中难以解码使用。
工具方法:


typedef struct

{
    int write_adts;
    int objecttype;
    int sample_rate_index;
    int channel_conf;

}ADTSContext;

int aac_decode_extradata(ADTSContext *adts, unsigned char *pbuf, int bufsize)
    {
        int aot, aotext, samfreindex;
        int i, channelconfig;
        unsigned char *p = pbuf;
        if (!adts || !pbuf || bufsize<2)
        {
            return -1;
        }
        aot = (p[0]>>3)&0x1f;
        if (aot == 31)
        {
            aotext = (p[0]<<3 | (p[1]>>5)) & 0x3f;
            aot = 32 + aotext;
            samfreindex = (p[1]>>1) & 0x0f;
            if (samfreindex == 0x0f)
            {
                channelconfig = ((p[4]<<3) | (p[5]>>5)) & 0x0f;
            }
            else
            {
                channelconfig = ((p[1]<<3)|(p[2]>>5)) & 0x0f;
            }
        }
        else
        {
            samfreindex = ((p[0]<<1)|p[1]>>7) & 0x0f;
            if (samfreindex == 0x0f)
            {
                channelconfig = (p[4]>>3) & 0x0f;
            }
            else
            {
                channelconfig = (p[1]>>3) & 0x0f;
            }
        }
#ifdef AOT_PROFILE_CTRL
        if (aot < 2) aot = 2;
#endif
        adts->objecttype = aot-1;
        adts->sample_rate_index = samfreindex;
        adts->channel_conf = channelconfig;
        adts->write_adts = 1;
        return 0;
    }

    int aac_set_adts_head(ADTSContext *acfg, unsigned char *buf, int size)
    {
        unsigned char byte;
        if (size < ADTS_HEADER_SIZE)
        {
            return -1;
        }
        buf[0] = 0xff;
        buf[1] = 0xf1;
        byte = 0;
        byte |= (acfg->objecttype & 0x03) << 6;
        byte |= (acfg->sample_rate_index & 0x0f) << 2;
        byte |= (acfg->channel_conf & 0x07) >> 2;
        buf[2] = byte;
        byte = 0;
        byte |= (acfg->channel_conf & 0x07) << 6;
        byte |= (ADTS_HEADER_SIZE + size) >> 11;
        buf[3] = byte;
        byte = 0;
        byte |= (ADTS_HEADER_SIZE + size) >> 3;
        buf[4] = byte;
        byte = 0;
        byte |= ((ADTS_HEADER_SIZE + size) & 0x7) << 5;
        byte |= (0x7ff >> 6) & 0x1f;
        buf[5] = byte;
        byte = 0;
        byte |= (0x7ff & 0x3f) << 2;
        buf[6] = byte;
        return 0;
    }

使用方法:

            ADTSContext adtsContext;
            unsigned char head[ADTS_HEADER_SIZE]={0};

            int bufSize=packet->size+ADTS_HEADER_SIZE;
            aac_decode_extradata(&adtsContext, codecContext_a->extradata, codecContext_a->extradata_size);
            aac_set_adts_head(&adtsContext, head, packet->size);
            unsigned char* buf= static_cast<unsigned char *>(malloc(
                    static_cast<size_t>(bufSize)));
            memcpy(buf, head, ADTS_HEADER_SIZE);
            memcpy(buf+ADTS_HEADER_SIZE, packet->data, static_cast<size_t>(packet->size));
			
			//使用 buf bufSize即可
			
			free(buf);

如果对你有用记得点赞!!!!!

发布了187 篇原创文章 · 获赞 65 · 访问量 26万+

猜你喜欢

转载自blog.csdn.net/mhhyoucom/article/details/104514137