解複用FLV文件(基於FFMPEG解析FLV(h264+aac))

技術在於交流、溝通,轉載請註明出處並保持作品的完整性。

原文: https://blog.csdn.net/hiwubihe/article/details/82346759

[本系列相關文章]

本篇介紹基於FFMPEG解析FLV文件,FLV由H264視頻和AAC音頻組成。FFMPEG 解複用出的音視頻AVPacket,直接寫入文件播放不了。FLV格式最常用的封裝組合就是H264+AAC,FFMPEG解複用的結果AVPacket只包含實際的壓縮音視頻數據,不包括解碼必須的信息,H264的SPS+PPS,AAC的視頻頭ADTS等。這就需要從FLV中提取SPS+PPS信息,和ADTS信息加到音視頻幀前面。

1.FLV文件AVCDecoderConfigurationRecord

封裝FLV,一般第一個VideoTag就是AVCDecoderConfigurationRecord結構,該結構就是H264的SPS+PPS信息

 AVCDecoderConfigurationRecord定義如下

aligned(8) class AVCDecoderConfigurationRecord {
	unsigned int(8) configurationVersion = 1;
	unsigned int(8) AVCProfileIndication;
	unsigned int(8) profile_compatibility;
	unsigned int(8) AVCLevelIndication; 
	bit(6) reserved = ‘111111’b;
	unsigned int(2) lengthSizeMinusOne; 
	bit(3) reserved = ‘111’b;
	unsigned int(5) numOfSequenceParameterSets;
	for (i=0; i< numOfSequenceParameterSets;  i++) {
		unsigned int(16) sequenceParameterSetLength ;
		bit(8*sequenceParameterSetLength) sequenceParameterSetNALUnit;
	}
	unsigned int(8) numOfPictureParameterSets;
	for (i=0; i< numOfPictureParameterSets;  i++) {
		unsigned int(16) pictureParameterSetLength;
		bit(8*pictureParameterSetLength) pictureParameterSetNALUnit;
	}
	bit(6) reserved = ‘111111’b;
	unsigned int(2) chroma_format;
	bit(5) reserved = ‘11111’b;
	unsigned int(3) bit_depth_luma_minus8;
	bit(5) reserved = ‘11111’b;
	unsigned int(3) bit_depth_chroma_minus8;
	unsigned int(8) numOfSequenceParameterSetExt;
	for (i=0; i< numOfSequenceParameterSetExt; i++) {
		unsigned int(16) sequenceParameterSetExtLength;
		bit(8*sequenceParameterSetExtLength) sequenceParameterSetExtNALUnit;
	}
}

2.FLV文件AudioSpecificConfig


封裝FLV,一般第一個AudioTag就是AudioSpecificConfig結構,該結構就是AAC封裝頭ADTS需要的信息,採樣率,通道數,樣本深度等信息。

3.FFMPEG處理原理

在av_read_frame函數調用中,如果讀到AVCDecoderConfigurationRecord或者AudioSpecificConfig結構時,會把信息保存在每個stream的streams[index]->codec->extradata擴展數據中。處理H264數據時,提供一個叫 “h264_mp4toannexb”比特流過濾器,在av_read_frame讀取到正常數據包時,需要調用比特流過濾器處理每個數據包,其實就是判斷是否是I幀,是I幀的話,解析AVCDecoderConfigurationRecord結構成SPS+PPS,然後把SPS+PPS保存加到I幀前面。可以參考FFMPEG源碼,版本3.2.4.r86064。下面代碼就是循環處理接受到的數據,添加SPS/PPS。

static int h264_mp4toannexb_filter(AVBSFContext *ctx, AVPacket *out)
{
    H264BSFContext *s = ctx->priv_data;

    AVPacket *in;
    uint8_t unit_type;
    int32_t nal_size;
    uint32_t cumul_size    = 0;
    const uint8_t *buf;
    const uint8_t *buf_end;
    int            buf_size;
    int ret = 0, i;

    //讀取的數據包複製到in
    ret = ff_bsf_get_packet(ctx, &in);
    if (ret < 0)
        return ret;

    /* nothing to filter */
    //不需要過濾 直接賦值
    if (!s->extradata_parsed)
    {
        av_packet_move_ref(out, in);
        av_packet_free(&in);
        return 0;
    }

    buf      = in->data;
    buf_size = in->size;
    buf_end  = in->data + in->size;

    do
    {
        ret= AVERROR(EINVAL);
        //長度錯誤
        if (buf + s->length_size > buf_end)
            goto fail;
        //獲取NALU長度
        for (nal_size = 0, i = 0; i<s->length_size; i++)
            nal_size = (nal_size << 8) | buf[i];
        //向後移動s->length_size
        buf += s->length_size;
        //NALU type
        unit_type = *buf & 0x1f;
        //長度出錯
        if (nal_size > buf_end - buf || nal_size < 0)
            goto fail;
        //SPS數據 新的IDR標誌 SPS直接拷貝加到緩存中
        if (unit_type == 7)
            s->idr_sps_seen = s->new_idr = 1;
        //PPS數據 新的IDR標誌
        else if (unit_type == 8)
        {
            s->idr_pps_seen = s->new_idr = 1;
            /* if SPS has not been seen yet, prepend the AVCC one to PPS */
            //當前是收到pps包 前面卻沒有SPS包,需要從擴展數據中把SPS加上
            //如果有sps 直接走到 一般數據拷貝else
            if (!s->idr_sps_seen)
            {
                if (s->sps_offset == -1)
                    av_log(ctx, AV_LOG_WARNING, "SPS not present in the stream, nor in AVCC, stream may be unreadable\n");
                else
                {
                    //把sps和pps 都拷貝到數據緩存中
                    if ((ret = alloc_and_copy(out,
                                              ctx->par_out->extradata + s->sps_offset,
                                              s->pps_offset != -1 ? s->pps_offset : ctx->par_out->extradata_size - s->sps_offset,
                                              buf, nal_size)) < 0)
                        goto fail;
                    s->idr_sps_seen = 1;
                    goto next_nal;
                }
            }
        }

        /* if this is a new IDR picture following an IDR picture, reset the idr flag.
         * Just check first_mb_in_slice to be 0 as this is the simplest solution.
         * This could be checking idr_pic_id instead, but would complexify the parsing. */
        if (!s->new_idr && unit_type == 5 && (buf[1] & 0x80))
            s->new_idr = 1;

        /* prepend only to the first type 5 NAL unit of an IDR picture, if no sps/pps are already present */
        //新的IDR
        if (s->new_idr && unit_type == 5 && !s->idr_sps_seen && !s->idr_pps_seen)
        {
            if ((ret=alloc_and_copy(out,
                                    ctx->par_out->extradata, ctx->par_out->extradata_size,
                                    buf, nal_size)) < 0)
                goto fail;
            s->new_idr = 0;
            /* if only SPS has been seen, also insert PPS */
        }
        else if (s->new_idr && unit_type == 5 && s->idr_sps_seen && !s->idr_pps_seen)
        {
            if (s->pps_offset == -1)
            {
                av_log(ctx, AV_LOG_WARNING, "PPS not present in the stream, nor in AVCC, stream may be unreadable\n");
                if ((ret = alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
                    goto fail;
            }
            else if ((ret = alloc_and_copy(out,
                                           ctx->par_out->extradata + s->pps_offset, ctx->par_out->extradata_size - s->pps_offset,
                                           buf, nal_size)) < 0)
                goto fail;
        }
        else
        {
            if ((ret=alloc_and_copy(out, NULL, 0, buf, nal_size)) < 0)
                goto fail;
            if (!s->new_idr && unit_type == 1)
            {
                s->new_idr = 1;
                s->idr_sps_seen = 0;
                s->idr_pps_seen = 0;
            }
        }

next_nal:
        buf        += nal_size;
        cumul_size += nal_size + s->length_size;
    }
    while (cumul_size < buf_size);

    ret = av_packet_copy_props(out, in);
    if (ret < 0)
        goto fail;

fail:
    if (ret < 0)
        av_packet_unref(out);
    av_packet_free(&in);

    return ret;
}

4.解析AudioSpecificConfig

AAC也會把AudioSpecificConfig保存到streams[index]->codec->extradata擴展數據中,但是FFMPEG 沒有提供加ADTS的比特流過濾器,需要自己解析AudioSpecificConfig,然後添加到AAC幀前面。

//解析AudioSpecificConfig
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;  
}  

5.DEMO程序

程序運用ffmpeg把h264+aac封裝的FLV,解複用成兩個文件,h264文件和AAC文件。

/*******************************************************************************
Copyright (c) wubihe Tech. Co., Ltd. All rights reserved.
--------------------------------------------------------------------------------

Date Created:	2014-10-25
Author:			wubihe QQ:1269122125 Email:[email protected]
Description:	解複用flv保存成h264文件和aac文件
--------------------------------------------------------------------------------
Modification History
DATE          AUTHOR          DESCRIPTION
--------------------------------------------------------------------------------

********************************************************************************/

#include <stdio.h>

#define __STDC_CONSTANT_MACROS


extern "C"
{
#include "libavformat/avformat.h"
};



//封裝格式MKV/MP4/FLV中如果有AAC的情況,首先這些封裝格式中包含AudioSpecificConfig,
//保存在音頻流的AVCodecContext->extradata 裏面,需要解析然後封裝ADTS即可



#define DEMUXER_AAC 1
#define DEMUXER_MP3 0

#define  ADTS_HEADER_SIZE (7)

//FLV封裝音視頻 AAC封裝在第一個AAC TAG會封裝一個AudioSpecificConfig結構 
//AudioSpecificConfig解析結果保存在該結構體中
typedef struct  
{
	int write_adts;  
	int objecttype;  
	int sample_rate_index;  
	int channel_conf;  

}ADTSContext;  


//解析AudioSpecificConfig
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;  
}  

//添加ADTS頭
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;  
}  






int main(int argc, char* argv[])
{

	AVFormatContext *ifmt_ctx = NULL;
	AVPacket pkt;
	int ret, i;
	int videoindex=-1,audioindex=-1;
	#if DEMUXER_AAC
	const char *in_filename    = "titanic.flv";		//Input file URL
	const char *out_filename_v = "titanic.h264";	//Output file URL
	const char *out_filename_a = "titanic.aac";
	#endif

	#if DEMUXER_MP3
	const char *in_filename    = "titanic.flv";		//Input file URL
	const char *out_filename_v = "titanic.h264";	//Output file URL
	const char *out_filename_a = "titanic.mp3";
	#endif




	av_register_all();
	//Input
	if ((ret = avformat_open_input(&ifmt_ctx, in_filename, 0, 0)) < 0) 
	{
		printf( "Could not open input file.");
		return -1;
	}
	if ((ret = avformat_find_stream_info(ifmt_ctx, 0)) < 0) 
	{
		printf( "Failed to retrieve input stream information");
		return -1;
	}

	videoindex=-1;
	for(i=0; i<ifmt_ctx->nb_streams; i++) 
	{
		if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_VIDEO)
		{
			videoindex=i;
		}else if(ifmt_ctx->streams[i]->codec->codec_type==AVMEDIA_TYPE_AUDIO)
		{
			audioindex=i;
		}
	}

	FILE *fp_audio=fopen(out_filename_a,"wb+");  
	FILE *fp_video=fopen(out_filename_v,"wb+");  

	//FLV/MP4/MKV等結構中,h264需要h264_mp4toannexb處理。添加SPS/PPS等信息。FLV封裝時,可以把
	//多個NALU放在一個VIDEO TAG中,結構爲4B NALU長度+NALU1+4B NALU長度+NALU2+...,需要做的處理把4B
	//長度換成00000001或者000001

	AVBitStreamFilterContext* h264bsfc =  av_bitstream_filter_init("h264_mp4toannexb"); 

#if DEMUXER_AAC
	ADTSContext stADTSContext;
	unsigned char pAdtsHead[7];
#endif

	while(av_read_frame(ifmt_ctx, &pkt)>=0)
	{
		if(pkt.stream_index==videoindex)
		{

			av_bitstream_filter_filter(h264bsfc, ifmt_ctx->streams[videoindex]->codec, NULL, &pkt.data, &pkt.size, pkt.data, pkt.size, 0);

			printf("Write Video Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
			fwrite(pkt.data,1,pkt.size,fp_video);
		}
		else if(pkt.stream_index==audioindex)
		{
			//AAC在封裝結構MKV/FLV/MP4結構中,需要手動添加ADTS
	#if DEMUXER_AAC
			aac_decode_extradata(&stADTSContext, ifmt_ctx->streams[audioindex]->codec->extradata, ifmt_ctx->streams[audioindex]->codec->extradata_size);
			aac_set_adts_head(&stADTSContext, pAdtsHead, pkt.size);
			fwrite(pAdtsHead, 1, 7, fp_audio);
	#endif
			//一般結構如MP3直接寫文件即可
			
			printf("Write Audio Packet. size:%d\tpts:%lld\n",pkt.size,pkt.pts);
			fwrite(pkt.data,1,pkt.size,fp_audio);
		}
		av_free_packet(&pkt);
	}


	av_bitstream_filter_close(h264bsfc);  


	fclose(fp_video);
	fclose(fp_audio);

	avformat_close_input(&ifmt_ctx);

	if (ret < 0 && ret != AVERROR_EOF) 
	{
		printf( "Error occurred.\n");
		return -1;
	}
	return 0;
}


6.運行結果

生成titanic.h264和titanic.aac兩個文件,用ffplay.exe可以驗證播放。


編譯環境:   Win7_64bit+VS2008

DEMO下載地址:https://download.csdn.net/download/hiwubihe/10643142

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章