1 /* avcodec API use example.
3 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
4 * not file formats (avi, vob, etc...). See library 'libav' for the
14 #define INBUF_SIZE 4096
17 * Audio encoding example
19 void audio_encode_example(const char *filename)
22 AVCodecContext codec_context, *c = &codec_context;
23 int frame_size, i, j, out_size, outbuf_size;
29 printf("Audio encoding\n");
31 /* find the MP2 encoder */
32 codec = avcodec_find_encoder(CODEC_ID_MP2);
34 fprintf(stderr, "codec not found\n");
38 /* put default values */
39 memset(c, 0, sizeof(*c));
41 /* put sample parameters */
43 c->sample_rate = 44100;
47 if (avcodec_open(c, codec) < 0) {
48 fprintf(stderr, "could not open codec\n");
52 /* the codec gives us the frame size, in samples */
53 frame_size = c->frame_size;
54 samples = malloc(frame_size * 2 * c->channels);
56 outbuf = malloc(outbuf_size);
58 f = fopen(filename, "w");
60 fprintf(stderr, "could not open %s\n", filename);
64 /* encode a single tone sound */
66 tincr = 2 * M_PI * 440.0 / c->sample_rate;
68 for(j=0;j<frame_size;j++) {
69 samples[2*j] = (int)(sin(t) * 10000);
70 samples[2*j+1] = samples[2*j];
73 /* encode the samples */
74 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
75 fwrite(outbuf, 1, out_size, f);
87 void audio_decode_example(const char *outfilename, const char *filename)
90 AVCodecContext codec_context, *c = &codec_context;
91 int out_size, size, len;
94 UINT8 inbuf[INBUF_SIZE], *inbuf_ptr;
96 printf("Audio decoding\n");
98 /* find the mpeg audio decoder */
99 codec = avcodec_find_decoder(CODEC_ID_MP2);
101 fprintf(stderr, "codec not found\n");
105 /* put default values */
106 memset(c, 0, sizeof(*c));
109 if (avcodec_open(c, codec) < 0) {
110 fprintf(stderr, "could not open codec\n");
114 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
116 f = fopen(filename, "r");
118 fprintf(stderr, "could not open %s\n", filename);
121 outfile = fopen(outfilename, "w");
123 fprintf(stderr, "could not open %s\n", outfilename);
127 /* decode until eof */
130 size = fread(inbuf, 1, INBUF_SIZE, f);
136 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
139 fprintf(stderr, "Error while decoding\n");
143 /* if a frame has been decoded, output it */
144 fwrite(outbuf, 1, out_size, outfile);
159 * Video encoding example
161 void video_encode_example(const char *filename)
164 AVCodecContext codec_context, *c = &codec_context;
165 int i, out_size, size, x, y, outbuf_size;
168 UINT8 *outbuf, *picture_buf;
170 printf("Video encoding\n");
172 /* find the mpeg1 video encoder */
173 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
175 fprintf(stderr, "codec not found\n");
179 /* put default values */
180 memset(c, 0, sizeof(*c));
182 /* put sample parameters */
183 c->bit_rate = 400000;
184 /* resolution must be a multiple of two */
187 /* frames per second */
188 c->frame_rate = 25 * FRAME_RATE_BASE;
189 c->gop_size = 10; /* emit one intra frame every ten frames */
192 if (avcodec_open(c, codec) < 0) {
193 fprintf(stderr, "could not open codec\n");
197 /* the codec gives us the frame size, in samples */
199 f = fopen(filename, "w");
201 fprintf(stderr, "could not open %s\n", filename);
205 /* alloc image and output buffer */
206 outbuf_size = 100000;
207 outbuf = malloc(outbuf_size);
208 size = c->width * c->height;
209 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
211 picture.data[0] = picture_buf;
212 picture.data[1] = picture.data[0] + size;
213 picture.data[2] = picture.data[1] + size / 4;
214 picture.linesize[0] = c->width;
215 picture.linesize[1] = c->width / 2;
216 picture.linesize[2] = c->width / 2;
218 /* encode 1 second of video */
220 printf("encoding frame %3d\r", i);
222 /* prepare a dummy image */
224 for(y=0;y<c->height;y++) {
225 for(x=0;x<c->width;x++) {
226 picture.data[0][y * picture.linesize[0] + x] = x + y + i * 3;
231 for(y=0;y<c->height/2;y++) {
232 for(x=0;x<c->width/2;x++) {
233 picture.data[1][y * picture.linesize[1] + x] = 128 + y + i * 2;
234 picture.data[2][y * picture.linesize[2] + x] = 64 + x + i * 5;
238 /* encode the image */
239 out_size = avcodec_encode_video(c, outbuf, outbuf_size, &picture);
240 fwrite(outbuf, 1, out_size, f);
243 /* add sequence end code to have a real mpeg file */
248 fwrite(outbuf, 1, 4, f);
258 * Video decoding example
261 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
266 f=fopen(filename,"w");
267 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
269 fwrite(buf + i * wrap,1,xsize,f);
273 void video_decode_example(const char *outfilename, const char *filename)
276 AVCodecContext codec_context, *c = &codec_context;
277 int frame, size, got_picture, len;
280 UINT8 inbuf[INBUF_SIZE], *inbuf_ptr;
283 printf("Video decoding\n");
285 /* find the mpeg1 video decoder */
286 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
288 fprintf(stderr, "codec not found\n");
292 /* put default values */
293 memset(c, 0, sizeof(*c));
295 /* for some codecs, such as msmpeg4 and mpeg4, width and height
296 MUST be initialized there because these info are not available
300 if (avcodec_open(c, codec) < 0) {
301 fprintf(stderr, "could not open codec\n");
305 /* the codec gives us the frame size, in samples */
307 f = fopen(filename, "r");
309 fprintf(stderr, "could not open %s\n", filename);
315 size = fread(inbuf, 1, INBUF_SIZE, f);
319 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
320 and this is the only method to use them because you cannot
321 know the compressed data size before analysing it.
323 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
324 based, so you must call them with all the data for one
325 frame exactly. You must also initialize 'width' and
326 'height' before initializing them. */
328 /* NOTE2: some codecs allow the raw parameters (frame size,
329 sample rate) to be changed at any frame. We handle this, so
330 you should also take care of it */
332 /* here, we use a stream based decoder (mpeg1video), so we
333 feed decoder and see if it could decode a frame */
336 len = avcodec_decode_video(c, &picture, &got_picture,
339 fprintf(stderr, "Error while decoding frame %d\n", frame);
343 printf("saving frame %3d\r", frame);
346 /* the picture is allocated by the decoder. no need to
348 snprintf(buf, sizeof(buf), outfilename, frame);
349 pgm_save(picture.data[0], picture.linesize[0],
350 c->width, c->height, buf);
358 /* some codecs, such as MPEG, transmit the I and P frame with a
359 latency of one frame. You must do the following to have a
360 chance to get the last frame of the video */
361 len = avcodec_decode_video(c, &picture, &got_picture,
364 printf("saving frame %3d\r", frame);
367 /* the picture is allocated by the decoder. no need to
369 snprintf(buf, sizeof(buf), outfilename, frame);
370 pgm_save(picture.data[0], picture.linesize[0],
371 c->width, c->height, buf);
382 int main(int argc, char **argv)
384 const char *filename;
386 /* must be called before using avcodec lib */
389 /* register all the codecs (you can also register only the codec
390 you wish to have smaller code */
391 avcodec_register_all();
394 audio_encode_example("/tmp/test.mp2");
395 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
397 video_encode_example("/tmp/test.mpg");
398 filename = "/tmp/test.mpg";
403 // audio_decode_example("/tmp/test.sw", filename);
404 video_decode_example("/tmp/test%d.pgm", filename);