decodingencoding.docx
- 文档编号:8888930
- 上传时间:2023-02-02
- 格式:DOCX
- 页数:13
- 大小:18.18KB
decodingencoding.docx
《decodingencoding.docx》由会员分享,可在线阅读,更多相关《decodingencoding.docx(13页珍藏版)》请在冰豆网上搜索。
decodingencoding
/**@file
*libavcodecAPIuseexample.
*Notethatlibavcodeconlyhandlescodecs(mpeg,mpeg4,etc...),
*notfileformats(avi,vob,mp4,mov,mkv,mxf,flv,mpegts,mpegps,etc...).Seelibrary'libavformat'forthe
*formathandling
*/
#include"libavutil/imgutils.h"
#include"libavutil/opt.h"
#include"libavcodec/avcodec.h"
#include"libavutil/mathematics.h"
#include"libavutil/samplefmt.h"
#defineINBUF_SIZE4096
#defineAUDIO_INBUF_SIZE20480
#defineAUDIO_REFILL_THRESH4096//大写字母一般是常数,切记
/*Audioencodingexample*/音频编码
staticvoidaudio_encode_example(constchar*filename)//参数就是编码的文件,包括路径
{AVCodec*codec;//codec,下面的找编码器用到,是整数么?
AVCodecContext*c=NULL;//上下文环境,
intframe_size,i,j,out_size,outbuf_size;
FILE*f;
short*samples;
floatt,tincr;
uint8_t*outbuf;
printf("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;
c->sample_rate=44100;
c->channels=2;
c->sample_fmt=AV_SAMPLE_FMT_S16;
/*openit打开流文件*/
if(avcodec_open(c,codec)<0){
fprintf(stderr,"couldnotopencodec\n");exit
(1);}
/*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");//文件
if(!
f){
fprintf(stderr,"couldnotopen%s\n",filename);
exit
(1);
}
/*encodeasingletonesound编码单色声音?
核心代码吧*/
t=0;//浮点数?
tincr=2*M_PI*440.0/c->sample_rate;
for(i=0;i<200;i++){
for(j=0;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; AVCodecContext*c=NULL; intlen; FILE*f,*outfile; uint8_tinbuf[AUDIO_INBUF_SIZE+FF_INPUT_BUFFER_PADDING_SIZE]; AVPacketavpkt; AVFrame*decoded_frame=NULL; av_init_packet(&avpkt); printf("Audiodecoding\n"); /*findthempegaudiodecoder*/ codec=avcodec_find_decoder(CODEC_ID_MP2); if(! codec){ fprintf(stderr,"codecnotfound\n"); exit (1); } c=avcodec_alloc_context3(codec); /*openit*/ if(avcodec_open(c,codec)<0){ fprintf(stderr,"couldnotopencodec\n"); exit (1); } f=fopen(filename,"rb"); if(! f){ fprintf(stderr,"couldnotopen%s\n",filename); exit (1); } outfile=fopen(outfilename,"wb"); if(! outfile){ av_free(c); exit (1); } /*decodeuntileof*/ avpkt.data=inbuf; avpkt.size=fread(inbuf,1,AUDIO_INBUF_SIZE,f); while(avpkt.size>0){ intgot_frame=0; if(! decoded_frame){ if(! (decoded_frame=avcodec_alloc_frame())){ fprintf(stderr,"outofmemory\n"); exit (1); } }else avcodec_get_frame_defaults(decoded_frame); len=avcodec_decode_audio4(c,decoded_frame,&got_frame,&avpkt); if(len<0){ fprintf(stderr,"Errorwhiledecoding\n"); exit (1); } if(got_frame){ /*ifaframehasbeendecoded,outputit*/ intdata_size=av_samples_get_buffer_size(NULL,c->channels, decoded_frame->nb_samples, c->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 /*Refilltheinputbuffer,toavoidtryingtodecode *incompleteframes.Insteadofthis,onecouldalsouse *aparser,oruseapropercontainerformatthrough *libavformat.*/ memmove(inbuf,avpkt.data,avpkt.size); avpkt.data=inbuf; len=fread(avpkt.data+avpkt.size,1, AUDIO_INBUF_SIZE-avpkt.size,f); if(len>0) avpkt.size+=len; } } fclose(outfile); fclose(f); avcodec_close(c); av_free(c); av_free(decoded_frame); } /**Videoencodingexample视频编码*/ staticvoidvideo_encode_example(constchar*filename,intcodec_id) { AVCodec*codec; AVCodecContext*c=NULL; inti,out_size,size,x,y,outbuf_size; FILE*f; AVFrame*picture; uint8_t*outbuf; printf("Videoencoding\n"); /*findthempeg1videoencoder*/ codec=avcodec_find_encoder(codec_id); if(! codec){ fprintf(stderr,"codecnotfound\n"); exit (1); } c=avcodec_alloc_context3(codec); picture=avcodec_alloc_frame(); /*putsampleparameters*/ c->bit_rate=400000; /*resolutionmustbeamultipleoftwo*/ c->width=352; c->height=288; /*framespersecond*/ c->time_base=(AVRational){1,25}; c->gop_size=10;/*emitoneintraframeeverytenframes*/ c->max_b_frames=1; c->pix_fmt=PIX_FMT_YUV420P; if(codec_id==CODEC_ID_H264) av_opt_set(c->priv_data,"preset","slow",0); /*openit*/ if(avcodec_open(c,codec)<0){ fprintf(stderr,"couldnotopencodec\n"); exit (1); } f=fopen(filename,"wb"); if(! f){ fprintf(stderr,"couldnotopen%s\n",filename); exit (1); } /*allocimageandoutputbuffer*/ outbuf_size=100000; outbuf=malloc(outbuf_size); /*theimagecanbeallocatedbyanymeansandav_image_alloc()is *justthemostconvenientwayifav_malloc()istobeused*/ av_image_alloc(picture->data,picture->linesize, c->width,c->height,c->pix_fmt,1); /*encode1secondofvideo*/ for(i=0;i<25;i++){ fflush(stdout); /*prepareadummyimage*/ /*Y*/ for(y=0;y for(x=0;x picture->data[0][y*picture->linesize[0]+x]=x+y+i*3; } } /*CbandCr*/ for(y=0;y for(x=0;x picture->data[1][y*picture->linesize[1]+x]=128+y+i*2; picture->data[2][y*picture->linesize[2]+x]=64+x+i*5; } } /*encodetheimage*/ out_size=avcodec_encode_video(c,outbuf,outbuf_size,picture); printf("encodingframe%3d(size=%5d)\n",i,out_size); fwrite(outbuf,1,out_size,f); } /*getthedelayedframes*/ for(;out_size;i++){ fflush(stdout); out_size=avcodec_encode_video(c,outbuf,outbuf_size,NULL); printf("writeframe%3d(size=%5d)\n",i,out_size); fwrite(outbuf,1,out_size,f); } /*addsequenceendcodetohavearealmpegfile*/ outbuf[0]=0x00; outbuf[1]=0x00; outbuf[2]=0x01; outbuf[3]=0xb7; fwrite(outbuf,1,4,f); fclose(f); free(outbuf); avcodec_close(c); av_free(c); av_free(picture->data[0]); av_free(picture); printf("\n"); } /**Videodecodingexample*/ staticvoidpgm_save(unsignedchar*buf,intwrap,intxsize,intysize, char*filename) { FILE*f; inti; f=fopen(filename,"w"); fprintf(f,"P5\n%d%d\n%d\n",xsize,ysize,255); for(i=0;i fwrite(buf+i*wrap,1,xsize,f); fclose(f); } staticvoidvideo_decode_example(constchar*outfilename,constchar*filename) { AVCodec*codec; AVCodecContext*c=NULL; intframe,got_picture,len; FILE*f; AVFrame*picture; uint8_tinbuf[INBUF_SIZE+FF_INPUT_BUFFER_PADDING_SIZE]; charbuf[1024]; AVPacketavpkt; av_init_packet(&avpkt); /*setendofbufferto0(thisensuresthatnooverreadinghappensfordamagedmpegstreams)*/ memset(inbuf+INBUF_SIZE,0,FF_INPUT_BUFFER_PADDING_SIZE); printf("Videodecoding\n"); /*findthempeg1videodecoder*/ codec=avcodec_find_decoder(CODEC_ID_MPEG1VIDEO); if(! codec){ fprintf(stderr,"codecnotfound\n"); exit (1); } c=avcodec_alloc_context3(codec); picture=avcodec_alloc_frame(); if(codec->capabilities&CODEC_CAP_TRUNCATED) c->flags|=CODEC_FLAG_TRUNCATED;/*wedonotsendcompleteframes*/ /*Forsomecodecs,suchasmsmpeg4andmpeg4,widthandheight MUSTbeinitializedtherebecausethisinformationisnot availableinthebitstream.*/ /*openit*/ if(avcodec_open(c,codec)<0){ fprintf(stderr,"couldnotopencodec\n"); exit (1); } /*thecodecgivesustheframesize,insamples*/ f=fopen(filename,"rb"); if(! f){ fprintf(stderr,"couldnotopen%s\n",filename); exit (1); } frame=0; for(;;){ 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*/ avpkt.data=inbuf; while(avpkt.size>0){ len=avcodec_decode_video2(c,picture,&got_picture,&avpkt); if(len<0){ fprintf(stderr,"Errorwhiledecodingframe%d\n",frame); exit (1); } if(got_picture){ printf("savingframe%3d\n",frame); fflush(stdout); /*thepictureisallocatedbythedecoder.noneedto freeit*/ snprintf(buf,sizeof(buf),outfilename,frame); pgm_save(picture->data[0],picture->linesize[0], c->width,c->height,buf); frame++; } avpkt.size-=len; avpkt.data+=len; } } /*somecodecs,suchasMPEG,transmittheIandPframewitha latencyofoneframe.Youmustdothefollowingtohavea chancetogetthelastframeofthevi
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- decodingencoding
![提示](https://static.bdocx.com/images/bang_tan.gif)