視音頻數(shù)據(jù)處理入門(mén):FLV封裝格式解析

了解FLV格式的基礎(chǔ)可以參照
https://wuyuans.com/2012/08/flv-format/
前兩篇文章介紹了音頻碼流處理程序和視頻碼流處理程序,本文介紹將他們打包到一起后的數(shù)據(jù)——封裝格式數(shù)據(jù)的處理程序。封裝格式數(shù)據(jù)在視頻播放器中的位置如下所示。


本文中的程序是一個(gè)FLV封裝格式解析程序。該程序可以從FLV中分析得到它的基本單元Tag,并且可以簡(jiǎn)單解析Tag首部的字段。通過(guò)修改該程序可以實(shí)現(xiàn)不同的FLV格式數(shù)據(jù)處理功能。

原理

FLV封裝格式是由一個(gè)FLV Header文件頭和一個(gè)一個(gè)的Tag組成的。Tag中包含了音頻數(shù)據(jù)以及視頻數(shù)據(jù)。FLV的結(jié)構(gòu)如下圖所示。


有關(guān)FLV的格式本文不再做記錄??梢詤⒖嘉恼?br> http://blog.csdn.net/leixiaohua1020/article/details/17934487。
本文的程序?qū)崿F(xiàn)了FLV中的FLV Header和Tag的解析,并可以分離出其中的音頻流。

代碼

整個(gè)程序位于simplest_flv_parser()函數(shù)中,如下所示。

/** 
 * 最簡(jiǎn)單的視音頻數(shù)據(jù)處理示例 
 * Simplest MediaData Test 
 * 
 * 雷霄驊 Lei Xiaohua 
 * leixiaohua1020@126.com 
 * 中國(guó)傳媒大學(xué)/數(shù)字電視技術(shù) 
 * Communication University of China / Digital TV Technology 
 * http://blog.csdn.net/leixiaohua1020 
 * 
 * 本項(xiàng)目包含如下幾種視音頻測(cè)試示例: 
 *  (1)像素?cái)?shù)據(jù)處理程序。包含RGB和YUV像素格式處理的函數(shù)。 
 *  (2)音頻采樣數(shù)據(jù)處理程序。包含PCM音頻采樣格式處理的函數(shù)。 
 *  (3)H.264碼流分析程序??梢苑蛛x并解析NALU。 
 *  (4)AAC碼流分析程序??梢苑蛛x并解析ADTS幀。 
 *  (5)FLV封裝格式分析程序??梢詫LV中的MP3音頻碼流分離出來(lái)。 
 *  (6)UDP-RTP協(xié)議分析程序??梢詫⒎治鯱DP/RTP/MPEG-TS數(shù)據(jù)包。 
 * 
 * This project contains following samples to handling multimedia data: 
 *  (1) Video pixel data handling program. It contains several examples to handle RGB and YUV data. 
 *  (2) Audio sample data handling program. It contains several examples to handle PCM data. 
 *  (3) H.264 stream analysis program. It can parse H.264 bitstream and analysis NALU of stream. 
 *  (4) AAC stream analysis program. It can parse AAC bitstream and analysis ADTS frame of stream. 
 *  (5) FLV format analysis program. It can analysis FLV file and extract MP3 audio stream. 
 *  (6) UDP-RTP protocol analysis program. It can analysis UDP/RTP/MPEG-TS Packet. 
 * 
 */  
#include <stdio.h>  
#include <stdlib.h>  
#include <string.h>  
  
//Important!  
#pragma pack(1)  
  
  
#define TAG_TYPE_SCRIPT 18  
#define TAG_TYPE_AUDIO  8  
#define TAG_TYPE_VIDEO  9  
  
typedef unsigned char byte;  
typedef unsigned int uint;  
  
typedef struct {  
    byte Signature[3];  
    byte Version;  
    byte Flags;  
    uint DataOffset;  
} FLV_HEADER;  
  
typedef struct {  
    byte TagType;  
    byte DataSize[3];  
    byte Timestamp[3];  
    uint Reserved;  
} TAG_HEADER;  
  
  
//reverse_bytes - turn a BigEndian byte array into a LittleEndian integer  
uint reverse_bytes(byte *p, char c) {  
    int r = 0;  
    int i;  
    for (i=0; i<c; i++)   
        r |= ( *(p+i) << (((c-1)*8)-8*i));  
    return r;  
}  
  
/** 
 * Analysis FLV file 
 * @param url    Location of input FLV file. 
 */  
  
int simplest_flv_parser(char *url){  
  
    //whether output audio/video stream  
    int output_a=1;  
    int output_v=1;  
    //-------------  
    FILE *ifh=NULL,*vfh=NULL, *afh = NULL;  
  
    //FILE *myout=fopen("output_log.txt","wb+");  
    FILE *myout=stdout;  
  
    FLV_HEADER flv;  
    TAG_HEADER tagheader;  
    uint previoustagsize, previoustagsize_z=0;  
    uint ts=0, ts_new=0;  
  
    ifh = fopen(url, "rb+");  
    if ( ifh== NULL) {  
        printf("Failed to open files!");  
        return -1;  
    }  
  
    //FLV file header  
    fread((char *)&flv,1,sizeof(FLV_HEADER),ifh);  
  
    fprintf(myout,"============== FLV Header ==============\n");  
    fprintf(myout,"Signature:  0x %c %c %c\n",flv.Signature[0],flv.Signature[1],flv.Signature[2]);  
    fprintf(myout,"Version:    0x %X\n",flv.Version);  
    fprintf(myout,"Flags  :    0x %X\n",flv.Flags);  
    fprintf(myout,"HeaderSize: 0x %X\n",reverse_bytes((byte *)&flv.DataOffset, sizeof(flv.DataOffset)));  
    fprintf(myout,"========================================\n");  
  
    //move the file pointer to the end of the header  
    fseek(ifh, reverse_bytes((byte *)&flv.DataOffset, sizeof(flv.DataOffset)), SEEK_SET);  
  
    //process each tag  
    do {  
  
        previoustagsize = _getw(ifh);  
  
        fread((void *)&tagheader,sizeof(TAG_HEADER),1,ifh);  
  
        //int temp_datasize1=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize));  
        int tagheader_datasize=tagheader.DataSize[0]*65536+tagheader.DataSize[1]*256+tagheader.DataSize[2];  
        int tagheader_timestamp=tagheader.Timestamp[0]*65536+tagheader.Timestamp[1]*256+tagheader.Timestamp[2];  
  
        char tagtype_str[10];  
        switch(tagheader.TagType){  
        case TAG_TYPE_AUDIO:sprintf(tagtype_str,"AUDIO");break;  
        case TAG_TYPE_VIDEO:sprintf(tagtype_str,"VIDEO");break;  
        case TAG_TYPE_SCRIPT:sprintf(tagtype_str,"SCRIPT");break;  
        default:sprintf(tagtype_str,"UNKNOWN");break;  
        }  
        fprintf(myout,"[%6s] %6d %6d |",tagtype_str,tagheader_datasize,tagheader_timestamp);  
  
        //if we are not past the end of file, process the tag  
        if (feof(ifh)) {  
            break;  
        }  
  
        //process tag by type  
        switch (tagheader.TagType) {  
  
        case TAG_TYPE_AUDIO:{   
            char audiotag_str[100]={0};  
            strcat(audiotag_str,"| ");  
            char tagdata_first_byte;  
            tagdata_first_byte=fgetc(ifh);  
            int x=tagdata_first_byte&0xF0;  
            x=x>>4;  
            switch (x)  
            {  
            case 0:strcat(audiotag_str,"Linear PCM, platform endian");break;  
            case 1:strcat(audiotag_str,"ADPCM");break;  
            case 2:strcat(audiotag_str,"MP3");break;  
            case 3:strcat(audiotag_str,"Linear PCM, little endian");break;  
            case 4:strcat(audiotag_str,"Nellymoser 16-kHz mono");break;  
            case 5:strcat(audiotag_str,"Nellymoser 8-kHz mono");break;  
            case 6:strcat(audiotag_str,"Nellymoser");break;  
            case 7:strcat(audiotag_str,"G.711 A-law logarithmic PCM");break;  
            case 8:strcat(audiotag_str,"G.711 mu-law logarithmic PCM");break;  
            case 9:strcat(audiotag_str,"reserved");break;  
            case 10:strcat(audiotag_str,"AAC");break;  
            case 11:strcat(audiotag_str,"Speex");break;  
            case 14:strcat(audiotag_str,"MP3 8-Khz");break;  
            case 15:strcat(audiotag_str,"Device-specific sound");break;  
            default:strcat(audiotag_str,"UNKNOWN");break;  
            }  
            strcat(audiotag_str,"| ");  
            x=tagdata_first_byte&0x0C;  
            x=x>>2;  
            switch (x)  
            {  
            case 0:strcat(audiotag_str,"5.5-kHz");break;  
            case 1:strcat(audiotag_str,"1-kHz");break;  
            case 2:strcat(audiotag_str,"22-kHz");break;  
            case 3:strcat(audiotag_str,"44-kHz");break;  
            default:strcat(audiotag_str,"UNKNOWN");break;  
            }  
            strcat(audiotag_str,"| ");  
            x=tagdata_first_byte&0x02;  
            x=x>>1;  
            switch (x)  
            {  
            case 0:strcat(audiotag_str,"8Bit");break;  
            case 1:strcat(audiotag_str,"16Bit");break;  
            default:strcat(audiotag_str,"UNKNOWN");break;  
            }  
            strcat(audiotag_str,"| ");  
            x=tagdata_first_byte&0x01;  
            switch (x)  
            {  
            case 0:strcat(audiotag_str,"Mono");break;  
            case 1:strcat(audiotag_str,"Stereo");break;  
            default:strcat(audiotag_str,"UNKNOWN");break;  
            }  
            fprintf(myout,"%s",audiotag_str);  
  
            //if the output file hasn't been opened, open it.  
            if(output_a!=0&&afh == NULL){  
                afh = fopen("output.mp3", "wb");  
            }  
  
            //TagData - First Byte Data  
            int data_size=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize))-1;  
            if(output_a!=0){  
                //TagData+1  
                for (int i=0; i<data_size; i++)  
                    fputc(fgetc(ifh),afh);  
  
            }else{  
                for (int i=0; i<data_size; i++)  
                    fgetc(ifh);  
            }  
            break;  
        }  
        case TAG_TYPE_VIDEO:{  
            char videotag_str[100]={0};  
            strcat(videotag_str,"| ");  
            char tagdata_first_byte;  
            tagdata_first_byte=fgetc(ifh);  
            int x=tagdata_first_byte&0xF0;  
            x=x>>4;  
            switch (x)  
            {  
            case 1:strcat(videotag_str,"key frame  ");break;  
            case 2:strcat(videotag_str,"inter frame");break;  
            case 3:strcat(videotag_str,"disposable inter frame");break;  
            case 4:strcat(videotag_str,"generated keyframe");break;  
            case 5:strcat(videotag_str,"video info/command frame");break;  
            default:strcat(videotag_str,"UNKNOWN");break;  
            }  
            strcat(videotag_str,"| ");  
            x=tagdata_first_byte&0x0F;  
            switch (x)  
            {  
            case 1:strcat(videotag_str,"JPEG (currently unused)");break;  
            case 2:strcat(videotag_str,"Sorenson H.263");break;  
            case 3:strcat(videotag_str,"Screen video");break;  
            case 4:strcat(videotag_str,"On2 VP6");break;  
            case 5:strcat(videotag_str,"On2 VP6 with alpha channel");break;  
            case 6:strcat(videotag_str,"Screen video version 2");break;  
            case 7:strcat(videotag_str,"AVC");break;  
            default:strcat(videotag_str,"UNKNOWN");break;  
            }  
            fprintf(myout,"%s",videotag_str);  
  
            fseek(ifh, -1, SEEK_CUR);  
            //if the output file hasn't been opened, open it.  
            if (vfh == NULL&&output_v!=0) {  
                //write the flv header (reuse the original file's hdr) and first previoustagsize  
                    vfh = fopen("output.flv", "wb");  
                    fwrite((char *)&flv,1, sizeof(flv),vfh);  
                    fwrite((char *)&previoustagsize_z,1,sizeof(previoustagsize_z),vfh);  
            }  
#if 0  
            //Change Timestamp  
            //Get Timestamp  
            ts = reverse_bytes((byte *)&tagheader.Timestamp, sizeof(tagheader.Timestamp));  
            ts=ts*2;  
            //Writeback Timestamp  
            ts_new = reverse_bytes((byte *)&ts, sizeof(ts));  
            memcpy(&tagheader.Timestamp, ((char *)&ts_new) + 1, sizeof(tagheader.Timestamp));  
#endif  
  
  
            //TagData + Previous Tag Size  
            int data_size=reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize))+4;  
            if(output_v!=0){  
                //TagHeader  
                fwrite((char *)&tagheader,1, sizeof(tagheader),vfh);  
                //TagData  
                for (int i=0; i<data_size; i++)  
                    fputc(fgetc(ifh),vfh);  
            }else{  
                for (int i=0; i<data_size; i++)  
                    fgetc(ifh);  
            }  
            //rewind 4 bytes, because we need to read the previoustagsize again for the loop's sake  
            fseek(ifh, -4, SEEK_CUR);  
  
            break;  
            }  
        default:  
  
            //skip the data of this tag  
            fseek(ifh, reverse_bytes((byte *)&tagheader.DataSize, sizeof(tagheader.DataSize)), SEEK_CUR);  
        }  
  
        fprintf(myout,"\n");  
  
    } while (!feof(ifh));  
  
  
    _fcloseall();  
  
    return 0;  
}

結(jié)果

本程序的輸入為一個(gè)FLV的文件路徑,輸出為FLV的統(tǒng)計(jì)數(shù)據(jù),如下圖所示。



此外本程序還可以分離FLV中的視頻碼流和音頻碼流。需要注意的是本程序并不能分離一些特定類型的音頻(例如AAC)和視頻.
Ps:非原創(chuàng),原作者雷霄驊

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容