decodingencoding文档格式.docx
《decodingencoding文档格式.docx》由会员分享,可在线阅读,更多相关《decodingencoding文档格式.docx(13页珍藏版)》请在冰豆网上搜索。
Audioencoding\n"
);
/*findtheMP2encoder编码器?
*/
codec=avcodec_find_encoder(CODEC_ID_MP2);
//返回0时表明打到了,这个API返回avcodec
if(!
codec){
fprintf(stderr,"
codecnotfound\n"
exit
(1);
}
c=avcodec_alloc_context3(codec);
//储存上下文环境
/*设置参数,都是AVCodecContext的成员变量putsampleparameters*/
c->
bit_rate=64000;
sample_rate=44100;
channels=2;
sample_fmt=AV_SAMPLE_FMT_S16;
/*openit打开流文件*/
if(avcodec_open(c,codec)<
0){
couldnotopencodec\n"
/*thecodecgivesustheframesize帧尺寸,insamples*/
frame_size=c->
frame_size;
samples=malloc(frame_size*2*c->
channels);
outbuf_size=10000;
outbuf=malloc(outbuf_size);
//这些都是自己设置的么?
f=fopen(filename,"
wb"
//文件
f){
couldnotopen%s\n"
filename);
/*encodeasingletonesound编码单色声音?
核心代码吧*/
t=0;
//浮点数?
tincr=2*M_PI*440.0/c->
sample_rate;
for(i=0;
i<
200;
i++){
for(j=0;
j<
j++){
samples[2*j]=(int)(sin(t)*10000);
samples[2*j+1]=samples[2*j];
t+=tincr;
/*encodethesamples*/
out_size=avcodec_encode_audio(c,outbuf,outbuf_size,samples);
fwrite(outbuf,1,out_size,f);
fclose(f);
free(outbuf);
free(samples);
avcodec_close(c);
av_free(c);
}
/**Audiodecoding解码.*/
staticvoidaudio_decode_example(constchar*outfilename,constchar*filename)//解码文件
{
AVCodec*codec;
intlen;
FILE*f,*outfile;
uint8_tinbuf[AUDIO_INBUF_SIZE+FF_INPUT_BUFFER_PADDING_SIZE];
AVPacketavpkt;
AVFrame*decoded_frame=NULL;
av_init_packet(&
avpkt);
Audiodecoding\n"
/*findthempegaudiodecoder*/
codec=avcodec_find_decoder(CODEC_ID_MP2);
/*openit*/
rb"
outfile=fopen(outfilename,"
outfile){
/*decodeuntileof*/
avpkt.data=inbuf;
avpkt.size=fread(inbuf,1,AUDIO_INBUF_SIZE,f);
while(avpkt.size>
intgot_frame=0;
decoded_frame){
(decoded_frame=avcodec_alloc_frame())){
outofmemory\n"
}else
avcodec_get_frame_defaults(decoded_frame);
len=avcodec_decode_audio4(c,decoded_frame,&
got_frame,&
if(len<
Errorwhiledecoding\n"
if(got_frame){
/*ifaframehasbeendecoded,outputit*/
intdata_size=av_samples_get_buffer_size(NULL,c->
channels,
decoded_frame->
nb_samples,
sample_fmt,1);
fwrite(decoded_frame->
data[0],1,data_size,outfile);
avpkt.size-=len;
avpkt.data+=len;
avpkt.dts=
avpkt.pts=AV_NOPTS_VALUE;
if(avpkt.size<
AUDIO_REFILL_THRESH){
/*Refilltheinputbuffer,toavoidtryingtodecode
*incompleteframes.Insteadofthis,onecouldalsouse
*aparser,oruseapropercontainerformatthrough
*libavformat.*/
memmove(inbuf,avpkt.data,avpkt.size);
len=fread(avpkt.data+avpkt.size,1,
AUDIO_INBUF_SIZE-avpkt.size,f);
if(len>
0)
avpkt.size+=len;
fclose(outfile);
av_free(decoded_frame);
/**Videoencodingexample视频编码*/
staticvoidvideo_encode_example(constchar*filename,intcodec_id)
inti,out_size,size,x,y,outbuf_size;
AVFrame*picture;
Videoencoding\n"
/*findthempeg1videoencoder*/
codec=avcodec_find_encoder(codec_id);
picture=avcodec_alloc_frame();
/*putsampleparameters*/
bit_rate=400000;
/*resolutionmustbeamultipleoftwo*/
width=352;
height=288;
/*framespersecond*/
time_base=(AVRational){1,25};
gop_size=10;
/*emitoneintraframeeverytenframes*/
max_b_frames=1;
pix_fmt=PIX_FMT_YUV420P;
if(codec_id==CODEC_ID_H264)
av_opt_set(c->
priv_data,"
preset"
"
slow"
0);
/*allocimageandoutputbuffer*/
outbuf_size=100000;
/*theimagecanbeallocatedbyanymeansandav_image_alloc()is
*justthemostconvenientwayifav_malloc()istobeused*/
av_image_alloc(picture->
data,picture->
linesize,
width,c->
height,c->
pix_fmt,1);
/*encode1secondofvideo*/
25;
fflush(stdout);
/*prepareadummyimage*/
/*Y*/
for(y=0;
y<
c->
height;
y++){
for(x=0;
x<
width;
x++){
picture->
data[0][y*picture->
linesize[0]+x]=x+y+i*3;
/*CbandCr*/
height/2;
width/2;
data[1][y*picture->
linesize[1]+x]=128+y+i*2;
data[2][y*picture->
linesize[2]+x]=64+x+i*5;
/*encodetheimage*/
out_size=avcodec_encode_video(c,outbuf,outbuf_size,picture);
encodingframe%3d(size=%5d)\n"
i,out_size);
/*getthedelayedframes*/
for(;
out_size;
i++){
out_size=avcodec_encode_video(c,outbuf,outbuf_size,NULL);
writeframe%3d(size=%5d)\n"
/*addsequenceendcodetohavearealmpegfile*/
outbuf[0]=0x00;
outbuf[1]=0x00;
outbuf[2]=0x01;
outbuf[3]=0xb7;
fwrite(outbuf,1,4,f);
av_free(picture->
data[0]);
av_free(picture);
\n"
/**Videodecodingexample*/
staticvoidpgm_save(unsignedchar*buf,intwrap,intxsize,intysize,
char*filename)
inti;
f=fopen(filename,"
w"
fprintf(f,"
P5\n%d%d\n%d\n"
xsize,ysize,255);
ysize;
i++)
fwrite(buf+i*wrap,1,xsize,f);
staticvoidvideo_decode_example(constchar*outfilename,constchar*filename)
intframe,got_picture,len;
uint8_tinbuf[INBUF_SIZE+FF_INPUT_BUFFER_PADDING_SIZE];
charbuf[1024];
/*setendofbufferto0(thisensuresthatnooverreadinghappensfordamagedmpegstreams)*/
memset(inbuf+INBUF_SIZE,0,FF_INPUT_BUFFER_PADDING_SIZE);
Videodecoding\n"
/*findthempeg1videodecoder*/
codec=avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
if(codec->
capabilities&
CODEC_CAP_TRUNCATED)
flags|=CODEC_FLAG_TRUNCATED;
/*wedonotsendcompleteframes*/
/*Forsomecodecs,suchasmsmpeg4andmpeg4,widthandheight
MUSTbeinitializedtherebecausethisinformationisnot
availableinthebitstream.*/
/*thecodecgivesustheframesize,insamples*/
frame=0;
;
){
avpkt.size=fread(inbuf,1,INBUF_SIZE,f);
if(avpkt.size==0)
break;
/*NOTE1:
somecodecsarestreambased(mpegvideo,mpegaudio)
andthisistheonlymethodtousethembecauseyoucannot
knowthecompresseddatasizebeforeanalysingit.
BUTsomeothercodecs(msmpeg4,mpeg4)areinherentlyframe
based,soyoumustcallthemwithallthedataforone
frameexactly.Youmustalsoinitialize'
width'
and
'
height'
beforeinitializingthem.*/
/*NOTE2:
somecodecsallowtherawparameters(framesize,
samplerate)tobechangedatanyframe.Wehandlethis,so
youshouldalsotakecareofit*/
/*here,weuseastreambaseddecoder(mpeg1video),sowe
feeddecoderandseeifitcoulddecodeaframe*/
len=avcodec_decode_video2(c,picture,&
got_picture,&
Errorwhiledecodingframe%d\n"
frame);
if(got_picture){
savingframe%3d\n"
/*thepictureisallocatedbythedecoder.noneedto
freeit*/
snprintf(buf,sizeof(buf),outfilename,frame);
pgm_save(picture->
data[0],picture->
linesize[0],
height,buf);
frame++;
/*somecodecs,suchasMPEG,transmittheIandPframewitha
latencyofoneframe.Youmustdothefollowingtohavea
chancetogetthelastframeofthevi