h264转MP4源码编译通过_第1页
h264转MP4源码编译通过_第2页
h264转MP4源码编译通过_第3页
h264转MP4源码编译通过_第4页
h264转MP4源码编译通过_第5页
已阅读5页,还剩27页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

本文格式为Word版,下载可任意编辑——h264转MP4源码,编译通过#ifndefH264_CONFIG_H#defineH264_CONFIG_H#include#include#include\externintopen_h264();externvoidrewind_h264();

externuint8_t*read_nal_from_h264(int*size);externvoidclose_h264();#endif/*H264_CONFIG_H*/

#ifndefMP4_CONFIG_H#defineMP4_CONFIG_H

//typedeflongint64_t;

typedefunsignedlonguint64_t;typedefunsignedintuint32_t;typedefunsignedcharuint8_t;

#defineUINT32_MAX0xffffffff#defineINT32_MAX0x7fffffff#defineH264_LANGUAGE0x55C4/*

mvhd:timescale:1000//一秒分1000份,表时间单位为1毫秒duration:1556tkhd:duration:1554了时长

mdhd:

//这个值对播放器很重要,具体时间还和mvhd的timescale相关,用来指定

timescale:24000//决定了播放速度,不过他与duration的相乘后还是可以对应上面的mvhd设置duration:37037samp:entry_count:1

sample_count:37//上的duration时间,duration指整个mdat中video时长,37却为chunks数目

sample_delta:1001//1001*37=37037sample_delta*sample_count=duration*/

/*mvhd*/

#defineMOV_TIMESCALE(1000)

//#defineMOV_MAX_TRACK_DURATION(0)//timescale:MOV_TIMESCALE//duration://FPS=MEDIA_TIMESCALE/MEDIA_SAMPLE_DURATION//duration=帧数/FPS*MOV_TIMESCALE//=帧数*MEDIA_SAMPLE_DURATION*MOV_TIMESCALE/MEDIA_TIMESCALE

/*tkhd*///duration://FPS=MEDIA_TIMESCALE/MEDIA_SAMPLE_DURATION//duration=帧数/FPS*MOV_TIMESCALE//=帧数*MEDIA_SAMPLE_DURATION*MOV_TIMESCALE/MEDIA_TIMESCALE

#defineMOV_TIMESCALE_TO_MEDIA_TIMESCALE(30)/*mdhd*/

#defineMEDIA_TIMESCALE2(MOV_TIMESCALE*MOV_TIMESCALE_TO_MEDIA_TIMESCALE)//使用在mdhd中

//timescale:MEDIA_TIMESCALE

//duration:MEDIA_SAMPLE_DURATION*帧数

/*sample*/

//#defineMEDIA_SAMPLE_DURATION(1001)

//unsignedintMEDIA_SAMPLE_DURATION=1001;该数值放在Mp4中便于多路互不影响

//entry_count:1

//sample_count:帧数

//sample_delta:MEDIA_SAMPLE_DURATION

//如上可得:

//MOV_TIMESCALE及MEDIA_SAMPLE_DURATION为定值,不影响播放//MEDIA_TIMESCALE/MEDIA_SAMPLE_DURATION决定了FPS:29.9帧率

#endif/*MP4_CONFIG_H*/

#include#include#include

#include\#include\

//FIXMEsupport64bitvariantwithwideplaceholdersstaticint64_tupdateSize(FILE*pb,int64_tpos){

int64_tcurpos=mp4_ftell(pb);mp4_fseek(pb,pos,SEEK_SET);

put_be32(pb,curpos-pos);/*rewritesize*/mp4_fseek(pb,curpos,SEEK_SET);returncurpos-pos;}

//获取H.264的avcc参数

staticintff_isom_write_avcc(FILE*pfile,Mp4Context*mov){

if(mov->sps!=NULL

put_buffer(pfile,mov->sps,mov->sps_len);put_byte(pfile,01);

put_be16(pfile,4);//mov->pps_len);

put_buffer(pfile,mov->pps,mov->pps_len);return0;}else{

return-1;}}

staticintmov_write_tkhd_tag(FILE*pb,Mp4Context*mov){

int64_tduration=MOV_TIMESCALE;intversion=0;

(version==1)?put_be32(pb,104):put_be32(pb,92);/*size*/put_tag(pb,\put_byte(pb,version);

put_be24(pb,0xf);/*flags(trackenabled)*/if(version==1){

put_be64(pb,mov->time);

put_be64(pb,mov->time);}else{

put_be32(pb,mov->time);/*creationtime*/put_be32(pb,mov->time);/*modificationtime*/}

put_be32(pb,1);/*track-id*/put_be32(pb,0);/*reserved*/

//这个值对播放器很重要,具体时间还和mvhd的timescale相关,用来指定了时长(version==1)?put_be64(pb,mov->video_track_size*mov->MEDIA_SAMPLE_DURATION/MOV_TIMESCALE_TO_MEDIA_TIMESCALE):\\

put_be32(pb,

mov->video_track_size*mov->MEDIA_SAMPLE_DURATION/MOV_TIMESCALE_TO_MEDIA_TIMESCALE);

put_be32(pb,0);/*reserved*/put_be32(pb,0);/*reserved*/

put_be32(pb,0x0);/*reserved(Layer//else

put_be16(pb,0);

put_be16(pb,0);/*reserved*/

/*Matrixstructure*/

put_be32(pb,0x00010000);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/

put_be32(pb,0x00010000);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/

put_be32(pb,0x40000000);/*reserved*/

/*Trackwidthandheight,forvisualonly*/

//if(st

//注意以后的音

//if(!sample_aspect_ratio||track->height!=track->enc->height)//sample_aspect_ratio=1;

put_be32(pb,mov->width/*sample_aspect_ratio*track->enc->width*0x10000*/);put_be32(pb,mov->height/*track->height*0x10000*/);return0x5c;}

staticintmov_write_mdhd_tag(FILE*pb,Mp4Context*mov){

intversion=0;

(version==1)?put_be32(pb,44):put_be32(pb,32);/*size*/put_tag(pb,\put_byte(pb,version);

put_be24(pb,0);/*flags*/if(version==1){

put_be64(pb,mov->time);//track->time);put_be64(pb,mov->time);//track->time);}else{

put_be32(pb,mov->time);//track->time);/*creationtime*/put_be32(pb,mov->time);//track->time);/*modificationtime*/}

put_be32(pb,MEDIA_TIMESCALE2);//track->timescale);/*timescale(samplerateforaudio)*/

(version==1)?put_be64(pb,mov->MEDIA_SAMPLE_DURATION*mov->video_track_size):\\put_be32(pb,mov->MEDIA_SAMPLE_DURATION*mov->video_track_size);/*duration*/put_be16(pb,H264_LANGUAGE);//track->language);/*language*/put_be16(pb,0);/*reserved(quality)*/

return32;}

staticintmov_write_hdlr_tag(FILE*pb,Mp4Context*mov){

constchar*hdlr,*descr=NULL,*hdlr_type=NULL;int64_tpos=mp4_ftell(pb);

//if(!mov){/*nomedia-->datahandler*///hdlr=\

//hdlr_type=\

//descr=\//}else{

//hdlr=(track->mode==MODE_MOV)?\hdlr=\

//if(track->enc->codec_type==AVMEDIA_TYPE_VIDEO){hdlr_type=\

descr=\

put_be32(pb,0);/*size*/put_tag(pb,\

put_be32(pb,0);/*Version/*handler*/

put_tag(pb,hdlr_type);/*handlertype*/put_be32(pb,0);/*reserved*/put_be32(pb,0);/*reserved*/put_be32(pb,0);/*reserved*/

//if(!track||track->mode==MODE_MOV)//put_byte(pb,strlen(descr));/*pascalstring*/

put_buffer(pb,descr,strlen(descr));/*handlerdescription*/put_byte(pb,0);//输出字符终止符\\0//if(track/*cstring*/returnupdateSize(pb,pos);}

staticintmov_write_vmhd_tag(FILE*pb){

put_be32(pb,0x14);/*size(always0x14)*/put_tag(pb,\

put_be32(pb,0x01);/*version/*reserved(graphicsmode=copy)*/return0x14;}

staticintmov_write_dref_tag(FILE*pb){

put_be32(pb,28);/*size*/put_tag(pb,\

put_be32(pb,0);/*version/*entrycount*/

put_be32(pb,0xc);/*size*/

put_tag(pb,\

put_be32(pb,1);/*version}

staticintmov_write_dinf_tag(FILE*pb){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

mov_write_dref_tag(pb);returnupdateSize(pb,pos);}

/*Timetosampleatom*/

staticintmov_write_stts_tag(FILE*pb,Mp4Context*mov){

//MOVStts*stts_entries;uint32_tentries=-1;uint32_tatom_size;uint32_ti;

entries=1;//mov->video_track_size;atom_size=16+(entries*8);

put_be32(pb,atom_size);/*size*/put_tag(pb,\

put_be32(pb,0);/*version/*entrycount*///这个实例中记录所有sample的durationfor(i=0;ivideo_track_size);//H264_TRACK_ENTITY);//stts_entries[i].count);put_be32(pb,mov->MEDIA_SAMPLE_DURATION);//stts_entries[i].duration);}

//av_free(stts_entries);returnatom_size;}

/*Syncsampleatom*/

staticintmov_write_stss_tag(FILE*pb,Mp4Context*mov)//*nal_list*track*/,uint32_tflag){

int64_tcurpos,entryPos;

inti,index=0;

int64_tpos=mp4_ftell(pb);

nal_list*head=mov->video_track;put_be32(pb,0);//size

//put_tag(pb,flag==MOV_SYNC_SAMPLE?\put_tag(pb,\

put_be32(pb,0);//version

//put_be32(pb,track->entry);//entrycountput_be32(pb,1);//entrycount

i=0;

while(head!=NULL){

if(head->nal_type==1){

put_be32(pb,i+1);index++;}i++;

head=head->next;}

curpos=mp4_ftell(pb);

mp4_fseek(pb,entryPos,SEEK_SET);put_be32(pb,index);//rewritesizemp4_fseek(pb,curpos,SEEK_SET);returnupdateSize(pb,pos);}

/*Sampletochunkatom*/

staticintmov_write_stsc_tag(FILE*pb,Mp4Context*mov){

intindex=0,oldval=-1;int64_tentryPos,curpos;

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

put_be32(pb,0);//version

put_be32(pb,mov->video_track_size);//track->entry);//entrycount//for(i=0;icluster[i].samplesInChunk)

//{

put_be32(pb,1);//firstchunk

put_be32(pb,1);//track->cluster[i].samplesInChunk);//samplesperchunkput_be32(pb,0x1);//sampledescriptionindex//oldval=track->cluster[i].samplesInChunk;//index++;//}//}

index=1;

curpos=mp4_ftell(pb);

mp4_fseek(pb,entryPos,SEEK_SET);put_be32(pb,index);//rewritesizemp4_fseek(pb,curpos,SEEK_SET);returnupdateSize(pb,pos);}

/*Samplesizeatom*/

staticintmov_write_stsz_tag(FILE*pb,Mp4Context*mov){

intequalChunks=1;uint32_ti;

//intj,entries=0,tst=-1,oldtst=-1;nal_list*track=mov->video_track;int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

put_be32(pb,0);/*version//samplesize

put_be32(pb,mov->video_track_size);//samplecount

for(i=0;ivideo_track_size;i++){

//if(one_node->nal_type!=2track=track->next;//}}

returnupdateSize(pb,pos);}

/*Chunkoffsetatom*/

staticintmov_write_stco_tag(FILE*pb,Mp4Context*mov)

{

uint32_ti;

intmode64=0;//use32bitsizevariantifpossible

uint32_toffset_begin=mov->mdat_pos+8;//获取mdat起始文件偏移int64_tpos=mp4_ftell(pb);

nal_list*track=mov->video_track;put_be32(pb,0);/*size*/if(pos>UINT32_MAX){

mode64=1;

put_tag(pb,\}else

put_tag(pb,\

put_be32(pb,0);/*version/*entrycount*/for(i=0;ivideo_track_size;i++){

if(mode64==1)

put_be64(pb,offset_begin+track->nal_offset);//文件偏移地址else

put_be32(pb,offset_begin+track->nal_offset);track=track->next;}

returnupdateSize(pb,pos);}

staticintmov_write_avcc_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);put_tag(pb,\

ff_isom_write_avcc(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_video_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);charcompressor_name[32];

put_be32(pb,0);/*size*/

//put_le32(pb,H264_TRACK_TAG);//storeitbyteswapped

put_tag(pb,\

put_be32(pb,0);/*Reserved*/put_be16(pb,0);/*Reserved*/

put_be16(pb,1);/*Data-referenceindex*/

put_be16(pb,0);/*Codecstreamversion*/

put_be16(pb,0);/*Codecstreamrevision(=0)*///if(track->mode==MODE_MOV){//put_tag(pb,\

//if(track->enc->codec_id==CODEC_ID_RAWVIDEO){//put_be32(pb,0);/*TemporalQuality*/

//put_be32(pb,0x400);/*SpatialQuality=lossless*///}else{

//put_be32(pb,0x200);/*TemporalQuality=normal*///put_be32(pb,0x200);/*SpatialQuality=normal*///}

//}else{

put_be32(pb,0);/*Reserved*/put_be32(pb,0);/*Reserved*/put_be32(pb,0);/*Reserved*///}

put_be16(pb,mov->width/*track->enc->width*/);/*Videowidth*/put_be16(pb,mov->height/*track->height*/);/*Videoheight*/put_be32(pb,0x00480000);/*Horizontalresolution72dpi*/put_be32(pb,0x00480000);/*Verticalresolution72dpi*/put_be32(pb,0);/*Datasize(=0)*/put_be16(pb,1);/*Framecount(=1)*/

memset(compressor_name,0,32);

/*FIXMEnotsure,ISO14496-1draftwhereitshallbesetto0*/

//if(track->mode==MODE_MOV//有问题put_byte(pb,strlen(compressor_name));put_buffer(pb,compressor_name,31);

//if(track->mode==MODE_MOV//else

put_be16(pb,0x18);/*Reserved*/put_be16(pb,0xffff);/*Reserved*///if(track->tag==MKTAG('m','p','4','v'))//mov_write_esds_tag(pb,mov);

//elseif(track->enc->codec_id==CODEC_ID_H263)//mov_write_d263_tag(pb);

//elseif(track->enc->codec_id==CODEC_ID_SVQ3)//mov_write_svq3_tag(pb);

//elseif(track->enc->codec_id==CODEC_ID_DNXHD)//mov_write_avid_tag(pb,mov);

//elseif(track->enc->codec_id==CODEC_ID_H264){mov_write_avcc_tag(pb,mov);//if(track->mode==MODE_IPOD)//mov_write_uuid_tag_ipod(pb);//}elseif(track->vosLen>0)//mov_write_glbl_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_stsd_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

put_be32(pb,0);/*version/*entrycount*/

//if(track->enc->codec_type==AVMEDIA_TYPE_VIDEO)mov_write_video_tag(pb,mov);

//elseif(track->enc->codec_type==AVMEDIA_TYPE_AUDIO)//mov_write_audio_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_stbl_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

mov_write_stsd_tag(pb,mov);mov_write_stts_tag(pb,mov);mov_write_stss_tag(pb,mov);mov_write_stsc_tag(pb,mov);mov_write_stsz_tag(pb,mov);mov_write_stco_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_minf_tag(FILE*pb,Mp4Context*mov){

//目前仅做视频

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

/*if(track->enc->codec_type==AVMEDIA_TYPE_VIDEO)*/mov_write_vmhd_tag(pb);mov_write_dinf_tag(pb);

mov_write_stbl_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_mdia_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/put_tag(pb,\

mov_write_mdhd_tag(pb,mov);mov_write_hdlr_tag(pb,mov);mov_write_minf_tag(pb,mov);returnupdateSize(pb,pos);}

//Thisboxseemsimportantforthepspplayback...withoutitthemovieseemstohangstaticintmov_write_edts_tag(FILE*pb,Mp4Context*mov){

put_be32(pb,0x24);/*size*/put_tag(pb,\

put_be32(pb,0x1c);/*size*/put_tag(pb,\put_be32(pb,0x0);put_be32(pb,0x1);

/*duration...doesn'tseemtoeffectpsp*/

put_be32(pb,mov->MEDIA_SAMPLE_DURATION*mov->video_track_size);//put_be32(pb,track->cluster[0].cts);/*firstptsisctssincedtsis0*/put_be32(pb,0);

put_be32(pb,0x00010000);return0x24;}

staticintmov_write_trak_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);put_be32(pb,0);/*size*/

put_tag(pb,\

mov_write_tkhd_tag(pb,mov);

//if(track->mode==MODE_PSP||track->flags//PSPMoviesrequireedtsbox//if(track->tref_tag)

//mov_write_tref_tag(pb,mov);mov_write_mdia_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_mvhd_tag(FILE*pb,Mp4Context*mov){

intmaxTrackID=1;

int64_tmaxTrackDuration=0;intversion;

//视频的总时间=一个track的总时间

maxTrackDuration=mov->video_track_size*mov->MEDIA_SAMPLE_DURATION/MOV_TIMESCALE_TO_MEDIA_TIMESCALE;

//version=maxTrackDurationtime);put_be64(pb,mov->time);}else{

put_be32(pb,mov->time);/*creationtime*///时间的格式HFS时间put_be32(pb,mov->time);/*modificationtime*/}

put_be32(pb,MOV_TIMESCALE);

(version==1)?put_be64(pb,maxTrackDuration):put_be32(pb,maxTrackDuration);/*durationoflongesttrack*/

put_be32(pb,0x00010000);/*reserved(preferredrate)1.0=normal*/put_be16(pb,0x0100);/*reserved(preferredvolume)1.0=normal*/put_be16(pb,0);/*reserved*/put_be32(pb,0);/*reserved*/

put_be32(pb,0);/*reserved*/

/*Matrixstructure*/

put_be32(pb,0x00010000);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/

put_be32(pb,0x00010000);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/put_be32(pb,0x0);/*reserved*/

put_be32(pb,0x40000000);/*reserved*/

put_be32(pb,0);/*reserved(previewtime)*/put_be32(pb,0);/*reserved(previewduration)*/put_be32(pb,0);/*reserved(postertime)*/put_be32(pb,0);/*reserved(selectiontime)*/put_be32(pb,0);/*reserved(selectionduration)*/put_be32(pb,0);/*reserved(currenttime)*/put_be32(pb,maxTrackID+1);/*Nexttrackid*/return0x6c;}

staticintmov_write_moov_tag(FILE*pb,Mp4Context*mov){

int64_tpos=mp4_ftell(pb);

put_be32(pb,0);/*sizeplaceholder*/put_tag(pb,\

mov_write_mvhd_tag(pb,mov);mov_write_trak_tag(pb,mov);returnupdateSize(pb,pos);}

staticintmov_write_trailer(FILE*pb,Mp4Context*mov){

intres=0;

int64_tmoov_pos=mp4_ftell(pb);/*Writesizeofmdattag*/

if(mov->mdat_size+8mdat_pos,SEEK_SET);put_be32(pb,mov->mdat_size+8);

next=NULL;mov->video_track_size++;

if(mov->video_track==NULL)

个帧的问题{

mov->video_track=new_node;}else{

mov->tail->next=new_node;}

mov->tail=new_node;return;}

uint32_tnal_list_get_size(Mp4Context*mov){

returnmov->video_track_size;}

nal_list*nal_list_get_head(Mp4Context*mov){

returnmov->video_track;}

voidnal_list_free(Mp4Context*mov){

while(mov->video_track!=NULL){

nal_list*p1=mov->video_track;

mov->video_track=mov->video_track->next;free(p1);}

mov->video_track=mov->tail=NULL;

//处理第一

//清空头与尾

return;}

//主函数测试#if1

#defineDATA_MAX3000000

intmain(intargc,char**argv){

Mp4Context*mov=malloc(sizeof(Mp4Context));FILE*fp=mp4_fopen(\

unsignedchardata[DATA_MAX];

intnal_list_len=0;intnal_list_it=0;

nal_list*nal_list_head=NULL;uint64_tsize=0;

uint64_tsum_size=0;uint8_t*buf=NULL;

nal_list*one_node=NULL;//uint32_tmax_nal=6;

memset(mov,0,sizeof(Mp4Context));

mov->mp4_fp=fp;

mov->time=0xC6BFDE31;

mov_write_header(mov->mp4_fp,mov);//写了Mp4的头部与mdat的头部

intb_eof=0;inti_data=0;

FILE*fin=fopen(\/*开启要转换的h264文件流*/if(fin==NULL){

fprintf(stderr,\return-1;}

/*splitframe*/

while(1){

uint8_t*p,*p_next,*end;inti_size=0;

/*fillbuffer*/

if(i_data=end-3){

fprintf(stderr,\i_data=0;continue;}

/*SearchendofNAL*/p_next=p+3;

while(p_next0){

continue;}else{

memset(mp4ContextSet+instance_it,0,sizeof(Mp4Context));//而清空内部数据放在GetInstance时做

mp4ContextSet[instance_it].isUsed=1;

mp4ContextSet[instance_it].MEDIA_SAMPLE_DURATION=MEDIA_TIMESCALE2/25;//设置帧率的默认值returninstance_it;//返回lHandle:0~31break;}}

returnINSTANCE_ERROR;//返回错误}

LONGReleaseInstance(LONGlHandle){

Mp4Context*mov=mp4ContextSet+lHandle;if(mp4ContextSet[lHandle].isUsed==1){

mp4ContextSet[lHandle].isUsed=0;//只有Stop后可以调用ReleaseInstance}else{

returnINSTANCE_ERROR;}

nal_list_free(mov);

//memset(mov,0,sizeof(Mp4Context));//清空数据//不做Memset,防止isUsed=0是正好分派相应路的解码任务//而清空内部数据放在GetInstance时做mov->isUsed=0;

returnINSTANCE_OK;}

LONGSetVideoFrameRate(LONGlHandle,LONGlFrameRate){

Mp4Context*mov=mp4ContextSet+lHandle;if(0==lFrameRate){

returnINSTANCE_ERROR;}

//返回成功

//获取句柄数据//除零错误

//返回失败

if(mp4ContextSet[lHandle].isUsed==0)//只有非状态0的可以调用SetVideoFrameRate{

returnINSTANCE_ERROR;}else{

mp4ContextSet[lHandle].MEDIA_SAMPLE_DURATION=MEDIA_TIMESCALE2/lFrameRate;}

returnINSTANCE_OK;}

LONGStartWrite(LONGlHandle,CHAR*pszFileName){

Mp4Context*mov=mp4ContextSet+lHandle;FILE*fp=NULL;

if(mp4ContextSet[lHandle].isUsed==1)StartWrite{

mp4ContextSet[lHandle].isUsed=2;}else{

returnINSTANCE_ERROR;}

mov->time=0;

mov->mdat_pos=0;mov->mdat_size=0;mov->sps=NULL;mov->sps_len=0;mov->pps=NULL;mov->pps_len=0;

mov->video_track=NULL;

//返回成功

//获取句柄数据

//只有初始化的可以调用

//记录帧的首部

mov->tail=NULL;mov->video_track_size=0;mov->width=0;mov->height=0;

fp=mp4_fopen(pszFileName,\mov->mp4_fp=fp;

mov->time=0xC6BFDE31;

//记录帧的尾部

mov_write_header(mov->mp4_fp,mov);mdat的头部

returnINSTANCE_OK;}

LONGInsertVideoBuffer(LONGlHandle,VOID*pBuf,ULONGlBufLen){

Mp4Context*mov=mp4ContextSet+lHandle;uint8_t*bufAllNal=(uint8_t*)pBuf;nal_list*one_node=NULL;intnal_type=0;intbuf_it=0;

//分开各组带起始码的Nal数据uint64_tlOffset=0;uint8_t*buf=NULL;uint64_tlNalSize=0;

uint64_tlNalTypeOffset=-1;

//写了Mp4的头部与//返回成功

//获取句柄数据//类型转换

if(mp4ContextSet[lHandle].isUsed==2)//只有Start后可以调用InsertVideoBuffer{;}else{

returnINSTANCE_ERROR;}

while((buf=bufferToNal(bufAllNal,lBufLen,//返回错误}

switch(buf[lNalTypeOffset]

//SPS信息

if(0==mov->sps_len)//记录一次SPS{mov->sps_len=lNalSize-lNalTypeOffset;mov->sps=malloc(mov->sps_len);memcpy(mov->sps,buf+lNalTypeOffset,mov->sps_len);}

break;case8://PPS信息nal_type=3;

if(0==mov->pps_len)//记录一次PPS{mov->pps_len=lNalSize-lNalTypeOffset;mov->pps=malloc(mov->pps_len);memcpy(mov->pps,buf+lNalTypeOffset,mov->pps_len);}

break;

case5://IDR帧信息nal_type=1;break;case6://SEI信息nal_type=4;break;case1://B,P帧默认为0nal_type=0;break;default://未识别帧nal_type=-1;break;}

one_node=(nal_list*)malloc(sizeof(structnal_list));one_node->nal_type=nal_type;

one_node->nal_offset=mov->mdat_size;one_node->nal_size=lNalSize;one_node->next=NULL;

//one_node->nal_type!=2

}

nal_list_insert_end(one_node,mov);

put_h264_buffer(mov->mp4_fp,buf+(lNalTypeOffset-4),lNalSize-(lNalTypeOffset-4));/

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论