FFMpeg测试例子x学习ffplayer.c

  1 #include "../libavcodec/avcodec.h"
2 #include "../libavformat/avformat.h"
3
4 void SaveFrame(AVFrame *pFrame, int width, int height, int iFrame) {
5 FILE *pFile;
6 char szFilename[32];
7 int y;
8
9 // Open file
10 sprintf(szFilename, "frame%d.ppm", iFrame);
11 pFile=fopen(szFilename, "wb");
12 if(pFile==NULL)
13 return;
14
15 // Write header
16 fprintf(pFile, "P6\n%d %d\n255\n", width, height);
17
18 // Write pixel data
19 for(y=0; y<height; y++)
20 fwrite(pFrame->data[0]+y*pFrame->linesize[0], 1, width*3, pFile);
21
22 // Close file
23 fclose(pFile);
24 }
25
26 int main(int argc, char *argv[]) {
27 int i;
28 AVCodecContext *pCodecCtx;
29 AVFrame *pFrame;
30 AVCodec *pCodec;
31 AVFormatContext *pFormatCtx;
32
33 av_register_all();
34
35 // Open video file
36 if(av_open_input_file(&pFormatCtx, argv[1], NULL, 0, NULL)!=0)
37 return -1; // Couldn't open file
38 // Retrieve stream information
39 if(av_find_stream_info(pFormatCtx)<0)
40 return -1; // Couldn't find stream information
41
42 // Find the first video stream
43 int videoStream=-1;
44 for(i=0; i<pFormatCtx->nb_streams; i++)
45 if(pFormatCtx->streams[i]->codec->codec_type==CODEC_TYPE_VIDEO) {
46 videoStream=i;
47 break;
48 }
49 if(videoStream==-1)
50 return -1; // Didn't find a video stream
51
52 // Get a pointer to the codec context for the video stream
53 pCodecCtx=pFormatCtx->streams[videoStream]->codec;
54
55 // Find the decoder for the video stream
56 pCodec=avcodec_find_decoder(pCodecCtx->codec_id);
57 if(pCodec==NULL) {
58 fprintf(stderr, "Unsupported codec!\n");
59 return -1; // Codec not found
60 }
61 // Open codec
62 if(avcodec_open(pCodecCtx, pCodec)<0)
63 return -1; // Could not open codec
64
65 // Allocate video frame
66 pFrame=avcodec_alloc_frame();
67
68 // Allocate an AVFrame structure
69 AVFrame *pFrameRGB=avcodec_alloc_frame();
70 if(pFrameRGB==NULL)
71 return -1;
72
73 uint8_t *buffer;
74 int numBytes;
75 // Determine required buffer size and allocate buffer
76 numBytes=avpicture_get_size(PIX_FMT_RGB24, pCodecCtx->width,
77 pCodecCtx->height);
78 buffer=(uint8_t *)av_malloc(numBytes*sizeof(uint8_t));
79
80 // Assign appropriate parts of buffer to image planes in pFrameRGB
81 // Note that pFrameRGB is an AVFrame, but AVFrame is a superset
82 // of AVPicture
83 avpicture_fill((AVPicture *)pFrameRGB, buffer, PIX_FMT_RGB24,
84 pCodecCtx->width, pCodecCtx->height);
85
86 int frameFinished;
87 AVPacket packet;
88
89 i=0;
90 while(av_read_frame(pFormatCtx, &packet)>=0) {
91 // Is this a packet from the video stream?
92 if(packet.stream_index==videoStream) {
93 // Decode video frame
94 avcodec_decode_video(pCodecCtx, pFrame, &frameFinished,
95 packet.data, packet.size);
96
97 // Did we get a video frame?
98 if(frameFinished) {
99 // Convert the image from its native format to RGB
100 img_convert((AVPicture *)pFrameRGB, PIX_FMT_RGB24,
101 (AVPicture*)pFrame, pCodecCtx->pix_fmt,
102 pCodecCtx->width, pCodecCtx->height);
103
104 // Save the frame to disk
105 if(++i<=5)
106 SaveFrame(pFrameRGB, pCodecCtx->width,
107 pCodecCtx->height, i);
108 }
109 }
110
111 // Free the packet that was allocated by av_read_frame
112 av_free_packet(&packet);
113 }
114
115 // Free the RGB image
116 av_free(buffer);
117 av_free(pFrameRGB);
118
119 // Free the YUV frame
120 av_free(pFrame);
121
122 // Close the codec
123 avcodec_close(pCodecCtx);
124
125 // Close the video file
126 av_close_input_file(pFormatCtx);
127
128 return 0;
129 }
posted @ 2012-03-23 11:13  Jeffery-Zou  阅读(3100)  评论(10编辑  收藏  举报