1、Audio encodingn); /* find the MP2 encoder 编码器?*/codec = avcodec_find_encoder(CODEC_ID_MP2);/返回0时表明打到了,这个API返回avcodec if (!codec) fprintf(stderr, codec not foundn exit(1); c = avcodec_alloc_context3(codec);/储存上下文环境/*设置参数,都是AVCodecContext的成员变量 put sample parameters */ c-bit_rate = 64000;sample_rate =
2、44100;channels = 2;sample_fmt = AV_SAMPLE_FMT_S16; /* open it 打开流文件*/ if (avcodec_open(c, codec) frame_size; samples = malloc(frame_size * 2 * c-channels); outbuf_size = 10000; outbuf = malloc(outbuf_size);/这些都是自己设置的么? f = fopen(filename, wb/文件f) could not open %sn, filename); /* encode a single ton
3、e sound 编码单色声音?核心代码吧*/ t = 0;/浮点数? tincr = 2 * M_PI * 440.0 / c-sample_rate; for(i=0;i200;i+) for(j=0;j int got_frame = 0;decoded_frame) (decoded_frame = avcodec_alloc_frame() out of memoryn else avcodec_get_frame_defaults(decoded_frame); len = avcodec_decode_audio4(c, decoded_frame, &got_frame, & i
4、f (len channels, decoded_frame-nb_samples,sample_fmt, 1); fwrite(decoded_frame-data0, 1, data_size, outfile); avpkt.size -= len; avpkt.data += len; avpkt.dts = avpkt.pts = AV_NOPTS_VALUE; if (avpkt.size 0) avpkt.size += len; fclose(outfile); av_free(decoded_frame);/* * Video encoding example视频编码*/st
5、atic void video_encode_example(const char *filename, int codec_id) int i, out_size, size, x, y, outbuf_size; AVFrame *picture;Video encodingn /* find the mpeg1 video encoder */ codec = avcodec_find_encoder(codec_id); picture= avcodec_alloc_frame(); /* put sample parameters */bit_rate = 400000; /* re
6、solution must be a multiple of two */width = 352;height = 288; /* frames per second */time_base= (AVRational)1,25;gop_size = 10; /* emit one intra frame every ten frames */max_b_frames=1;pix_fmt = PIX_FMT_YUV420P; if(codec_id = CODEC_ID_H264) av_opt_set(c-priv_data, preset, slow, 0); /* alloc image
7、and output buffer */ outbuf_size = 100000; /* the image can be allocated by any means and av_image_alloc() is * just the most convenient way if av_malloc() is to be used */ av_image_alloc(picture-data, picture-linesize,width, c-height, c-pix_fmt, 1); /* encode 1 second of video */25; fflush(stdout);
8、 /* prepare a dummy image */ /* Y */ for(y=0;yheight;y+) for(x=0;xdata0y * picture-linesize0 + x = x + y + i * 3; /* Cb and Cr */height/2;width/2;data1y * picture-linesize1 + x = 128 + y + i * 2;data2y * picture-linesize2 + x = 64 + x + i * 5; /* encode the image */ out_size = avcodec_encode_video(c
9、, outbuf, outbuf_size, picture);encoding frame %3d (size=%5d)n, i, out_size); /* get the delayed frames */ for(; out_size; i+) out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);write frame %3d (size=%5d)n /* add sequence end code to have a real mpeg file */ outbuf0 = 0x00; outbuf1 = 0x00
10、; outbuf2 = 0x01; outbuf3 = 0xb7; fwrite(outbuf, 1, 4, f); av_free(picture-data0); av_free(picture);n/* * Video decoding example */static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename) int i; f=fopen(filename,w fprintf(f,P5n%d %dn%dn,xsize,ysize,255);ysize;i+) fwri
11、te(buf + i * wrap,1,xsize,f);static void video_decode_example(const char *outfilename, const char *filename) int frame, got_picture, len; uint8_t inbufINBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE; char buf1024; /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams
12、) */ memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);Video decodingn /* find the mpeg1 video decoder */ codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO); if(codec-capabilities&CODEC_CAP_TRUNCATED)flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */ /* For some codecs, such
13、 as msmpeg4 and mpeg4, width and height MUST be initialized there because this information is not available in the bitstream. */ /* the codec gives us the frame size, in samples */ frame = 0;) avpkt.size = fread(inbuf, 1, INBUF_SIZE, f); if (avpkt.size = 0) break; /* NOTE1: some codecs are stream ba
14、sed (mpegvideo, mpegaudio) and this is the only method to use them because you cannot know the compressed data size before analysing it. BUT some other codecs (msmpeg4, mpeg4) are inherently frame based, so you must call them with all the data for one frame exactly. You must also initialize width an
15、d height before initializing them. */ /* NOTE2: some codecs allow the raw parameters (frame size, sample rate) to be changed at any frame. We handle this, so you should also take care of it */ /* here, we use a stream based decoder (mpeg1video), so we feed decoder and see if it could decode a frame
16、*/ len = avcodec_decode_video2(c, picture, &got_picture, &Error while decoding frame %dn, frame); if (got_picture) saving frame %3dn /* the picture is allocated by the decoder. no need to free it */ snprintf(buf, sizeof(buf), outfilename, frame); pgm_save(picture-data0, picture-linesize0,height, buf); frame+; /* some codecs, such as MPEG, transmit the I and P frame with a latency of one frame. You must do the following to have a chance to get the last frame of the vi
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1