3 * avcodec API use example.
5 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
6 * not file formats (avi, vob, etc...). See library 'libavformat' for the
15 #ifdef HAVE_AV_CONFIG_H
16 #undef HAVE_AV_CONFIG_H
21 #define INBUF_SIZE 4096
24 * Audio encoding example
26 void audio_encode_example(const char *filename)
29 AVCodecContext *c= NULL;
30 int frame_size, i, j, out_size, outbuf_size;
36 printf("Audio encoding\n");
38 /* find the MP2 encoder */
39 codec = avcodec_find_encoder(CODEC_ID_MP2);
41 fprintf(stderr, "codec not found\n");
45 c= avcodec_alloc_context();
47 /* put sample parameters */
49 c->sample_rate = 44100;
53 if (avcodec_open(c, codec) < 0) {
54 fprintf(stderr, "could not open codec\n");
58 /* the codec gives us the frame size, in samples */
59 frame_size = c->frame_size;
60 samples = malloc(frame_size * 2 * c->channels);
62 outbuf = malloc(outbuf_size);
64 f = fopen(filename, "wb");
66 fprintf(stderr, "could not open %s\n", filename);
70 /* encode a single tone sound */
72 tincr = 2 * M_PI * 440.0 / c->sample_rate;
74 for(j=0;j<frame_size;j++) {
75 samples[2*j] = (int)(sin(t) * 10000);
76 samples[2*j+1] = samples[2*j];
79 /* encode the samples */
80 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
81 fwrite(outbuf, 1, out_size, f);
94 void audio_decode_example(const char *outfilename, const char *filename)
97 AVCodecContext *c= NULL;
98 int out_size, size, len;
101 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
103 printf("Audio decoding\n");
105 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
106 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
108 /* find the mpeg audio decoder */
109 codec = avcodec_find_decoder(CODEC_ID_MP2);
111 fprintf(stderr, "codec not found\n");
115 c= avcodec_alloc_context();
118 if (avcodec_open(c, codec) < 0) {
119 fprintf(stderr, "could not open codec\n");
123 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
125 f = fopen(filename, "rb");
127 fprintf(stderr, "could not open %s\n", filename);
130 outfile = fopen(outfilename, "wb");
136 /* decode until eof */
139 size = fread(inbuf, 1, INBUF_SIZE, f);
145 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
148 fprintf(stderr, "Error while decoding\n");
152 /* if a frame has been decoded, output it */
153 fwrite(outbuf, 1, out_size, outfile);
169 * Video encoding example
171 void video_encode_example(const char *filename)
174 AVCodecContext *c= NULL;
175 int i, out_size, size, x, y, outbuf_size;
178 uint8_t *outbuf, *picture_buf;
180 printf("Video encoding\n");
182 /* find the mpeg1 video encoder */
183 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
185 fprintf(stderr, "codec not found\n");
189 c= avcodec_alloc_context();
190 picture= avcodec_alloc_frame();
192 /* put sample parameters */
193 c->bit_rate = 400000;
194 /* resolution must be a multiple of two */
197 /* frames per second */
199 c->frame_rate_base= 1;
200 c->gop_size = 10; /* emit one intra frame every ten frames */
204 if (avcodec_open(c, codec) < 0) {
205 fprintf(stderr, "could not open codec\n");
209 /* the codec gives us the frame size, in samples */
211 f = fopen(filename, "wb");
213 fprintf(stderr, "could not open %s\n", filename);
217 /* alloc image and output buffer */
218 outbuf_size = 100000;
219 outbuf = malloc(outbuf_size);
220 size = c->width * c->height;
221 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
223 picture->data[0] = picture_buf;
224 picture->data[1] = picture->data[0] + size;
225 picture->data[2] = picture->data[1] + size / 4;
226 picture->linesize[0] = c->width;
227 picture->linesize[1] = c->width / 2;
228 picture->linesize[2] = c->width / 2;
230 /* encode 1 second of video */
233 /* prepare a dummy image */
235 for(y=0;y<c->height;y++) {
236 for(x=0;x<c->width;x++) {
237 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
242 for(y=0;y<c->height/2;y++) {
243 for(x=0;x<c->width/2;x++) {
244 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
245 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
249 /* encode the image */
250 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
251 printf("encoding frame %3d (size=%5d)\n", i, out_size);
252 fwrite(outbuf, 1, out_size, f);
255 /* get the delayed frames */
256 for(; out_size; i++) {
259 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
260 printf("write frame %3d (size=%5d)\n", i, out_size);
261 fwrite(outbuf, 1, out_size, f);
264 /* add sequence end code to have a real mpeg file */
269 fwrite(outbuf, 1, 4, f);
281 * Video decoding example
284 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
289 f=fopen(filename,"w");
290 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
292 fwrite(buf + i * wrap,1,xsize,f);
296 void video_decode_example(const char *outfilename, const char *filename)
299 AVCodecContext *c= NULL;
300 int frame, size, got_picture, len;
303 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
306 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
307 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
309 printf("Video decoding\n");
311 /* find the mpeg1 video decoder */
312 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
314 fprintf(stderr, "codec not found\n");
318 c= avcodec_alloc_context();
319 picture= avcodec_alloc_frame();
321 if(codec->capabilities&CODEC_CAP_TRUNCATED)
322 c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
324 /* for some codecs, such as msmpeg4 and mpeg4, width and height
325 MUST be initialized there because these info are not available
329 if (avcodec_open(c, codec) < 0) {
330 fprintf(stderr, "could not open codec\n");
334 /* the codec gives us the frame size, in samples */
336 f = fopen(filename, "rb");
338 fprintf(stderr, "could not open %s\n", filename);
344 size = fread(inbuf, 1, INBUF_SIZE, f);
348 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
349 and this is the only method to use them because you cannot
350 know the compressed data size before analysing it.
352 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
353 based, so you must call them with all the data for one
354 frame exactly. You must also initialize 'width' and
355 'height' before initializing them. */
357 /* NOTE2: some codecs allow the raw parameters (frame size,
358 sample rate) to be changed at any frame. We handle this, so
359 you should also take care of it */
361 /* here, we use a stream based decoder (mpeg1video), so we
362 feed decoder and see if it could decode a frame */
365 len = avcodec_decode_video(c, picture, &got_picture,
368 fprintf(stderr, "Error while decoding frame %d\n", frame);
372 printf("saving frame %3d\n", frame);
375 /* the picture is allocated by the decoder. no need to
377 snprintf(buf, sizeof(buf), outfilename, frame);
378 pgm_save(picture->data[0], picture->linesize[0],
379 c->width, c->height, buf);
387 /* some codecs, such as MPEG, transmit the I and P frame with a
388 latency of one frame. You must do the following to have a
389 chance to get the last frame of the video */
390 len = avcodec_decode_video(c, picture, &got_picture,
393 printf("saving last frame %3d\n", frame);
396 /* the picture is allocated by the decoder. no need to
398 snprintf(buf, sizeof(buf), outfilename, frame);
399 pgm_save(picture->data[0], picture->linesize[0],
400 c->width, c->height, buf);
412 // simple example how the options could be used
413 int options_example(int argc, char* argv[])
415 AVCodec* codec = avcodec_find_encoder_by_name((argc > 1) ? argv[2] : "mpeg4");
417 AVCodecContext* avctx;
418 char* def = av_malloc(5000);
419 const char* col = "";
425 avctx = avcodec_alloc_context();
429 const AVOption *stack[FF_OPT_MAX_DEPTH];
435 c = (const AVOption*)c->help;
443 int t = c->type & FF_OPT_TYPE_MASK;
444 printf("Config %s %s\n",
445 t == FF_OPT_TYPE_BOOL ? "bool " :
446 t == FF_OPT_TYPE_DOUBLE ? "double " :
447 t == FF_OPT_TYPE_INT ? "integer" :
448 t == FF_OPT_TYPE_STRING ? "string " :
449 "unknown??", c->name);
451 case FF_OPT_TYPE_BOOL:
452 i += sprintf(def + i, "%s%s=%s",
454 c->defval != 0. ? "on" : "off");
456 case FF_OPT_TYPE_DOUBLE:
457 i += sprintf(def + i, "%s%s=%f",
458 col, c->name, c->defval);
460 case FF_OPT_TYPE_INT:
461 i += sprintf(def + i, "%s%s=%d",
462 col, c->name, (int) c->defval);
464 case FF_OPT_TYPE_STRING:
466 char* d = av_strdup(c->defstr);
467 char* f = strchr(d, ',');
470 i += sprintf(def + i, "%s%s=%s",
481 printf("Default Options: %s\n", def);
487 int main(int argc, char **argv)
489 const char *filename;
491 /* must be called before using avcodec lib */
494 /* register all the codecs (you can also register only the codec
495 you wish to have smaller code */
496 avcodec_register_all();
499 options_example(argc, argv);
502 audio_encode_example("/tmp/test.mp2");
503 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
505 video_encode_example("/tmp/test.mpg");
506 filename = "/tmp/test.mpg";
511 // audio_decode_example("/tmp/test.sw", filename);
512 video_decode_example("/tmp/test%d.pgm", filename);