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 'libav' 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, "w");
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], *inbuf_ptr;
103 printf("Audio decoding\n");
105 /* find the mpeg audio decoder */
106 codec = avcodec_find_decoder(CODEC_ID_MP2);
108 fprintf(stderr, "codec not found\n");
112 c= avcodec_alloc_context();
115 if (avcodec_open(c, codec) < 0) {
116 fprintf(stderr, "could not open codec\n");
120 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
122 f = fopen(filename, "r");
124 fprintf(stderr, "could not open %s\n", filename);
127 outfile = fopen(outfilename, "w");
133 /* decode until eof */
136 size = fread(inbuf, 1, INBUF_SIZE, f);
142 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
145 fprintf(stderr, "Error while decoding\n");
149 /* if a frame has been decoded, output it */
150 fwrite(outbuf, 1, out_size, outfile);
166 * Video encoding example
168 void video_encode_example(const char *filename)
171 AVCodecContext *c= NULL;
172 int i, out_size, size, x, y, outbuf_size;
175 uint8_t *outbuf, *picture_buf;
177 printf("Video encoding\n");
179 /* find the mpeg1 video encoder */
180 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
182 fprintf(stderr, "codec not found\n");
186 c= avcodec_alloc_context();
187 picture= avcodec_alloc_frame();
189 /* put sample parameters */
190 c->bit_rate = 400000;
191 /* resolution must be a multiple of two */
194 /* frames per second */
196 c->frame_rate_base= 1;
197 c->gop_size = 10; /* emit one intra frame every ten frames */
201 if (avcodec_open(c, codec) < 0) {
202 fprintf(stderr, "could not open codec\n");
206 /* the codec gives us the frame size, in samples */
208 f = fopen(filename, "w");
210 fprintf(stderr, "could not open %s\n", filename);
214 /* alloc image and output buffer */
215 outbuf_size = 100000;
216 outbuf = malloc(outbuf_size);
217 size = c->width * c->height;
218 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
220 picture->data[0] = picture_buf;
221 picture->data[1] = picture->data[0] + size;
222 picture->data[2] = picture->data[1] + size / 4;
223 picture->linesize[0] = c->width;
224 picture->linesize[1] = c->width / 2;
225 picture->linesize[2] = c->width / 2;
227 /* encode 1 second of video */
230 /* prepare a dummy image */
232 for(y=0;y<c->height;y++) {
233 for(x=0;x<c->width;x++) {
234 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
239 for(y=0;y<c->height/2;y++) {
240 for(x=0;x<c->width/2;x++) {
241 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
242 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
246 /* encode the image */
247 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
248 printf("encoding frame %3d (size=%5d)\n", i, out_size);
249 fwrite(outbuf, 1, out_size, f);
252 /* get the delayed frames */
253 for(; out_size; i++) {
256 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
257 printf("write frame %3d (size=%5d)\n", i, out_size);
258 fwrite(outbuf, 1, out_size, f);
261 /* add sequence end code to have a real mpeg file */
266 fwrite(outbuf, 1, 4, f);
278 * Video decoding example
281 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
286 f=fopen(filename,"w");
287 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
289 fwrite(buf + i * wrap,1,xsize,f);
293 void video_decode_example(const char *outfilename, const char *filename)
296 AVCodecContext *c= NULL;
297 int frame, size, got_picture, len;
300 uint8_t inbuf[INBUF_SIZE], *inbuf_ptr;
303 printf("Video decoding\n");
305 /* find the mpeg1 video decoder */
306 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
308 fprintf(stderr, "codec not found\n");
312 c= avcodec_alloc_context();
313 picture= avcodec_alloc_frame();
315 if(codec->capabilities&CODEC_CAP_TRUNCATED)
316 c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
318 /* for some codecs, such as msmpeg4 and mpeg4, width and height
319 MUST be initialized there because these info are not available
323 if (avcodec_open(c, codec) < 0) {
324 fprintf(stderr, "could not open codec\n");
328 /* the codec gives us the frame size, in samples */
330 f = fopen(filename, "r");
332 fprintf(stderr, "could not open %s\n", filename);
338 size = fread(inbuf, 1, INBUF_SIZE, f);
342 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
343 and this is the only method to use them because you cannot
344 know the compressed data size before analysing it.
346 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
347 based, so you must call them with all the data for one
348 frame exactly. You must also initialize 'width' and
349 'height' before initializing them. */
351 /* NOTE2: some codecs allow the raw parameters (frame size,
352 sample rate) to be changed at any frame. We handle this, so
353 you should also take care of it */
355 /* here, we use a stream based decoder (mpeg1video), so we
356 feed decoder and see if it could decode a frame */
359 len = avcodec_decode_video(c, picture, &got_picture,
362 fprintf(stderr, "Error while decoding frame %d\n", frame);
366 printf("saving frame %3d\n", frame);
369 /* the picture is allocated by the decoder. no need to
371 snprintf(buf, sizeof(buf), outfilename, frame);
372 pgm_save(picture->data[0], picture->linesize[0],
373 c->width, c->height, buf);
381 /* some codecs, such as MPEG, transmit the I and P frame with a
382 latency of one frame. You must do the following to have a
383 chance to get the last frame of the video */
384 len = avcodec_decode_video(c, picture, &got_picture,
387 printf("saving last frame %3d\n", frame);
390 /* the picture is allocated by the decoder. no need to
392 snprintf(buf, sizeof(buf), outfilename, frame);
393 pgm_save(picture->data[0], picture->linesize[0],
394 c->width, c->height, buf);
406 // simple example how the options could be used
407 int options_example(int argc, char* argv[])
409 AVCodec* codec = avcodec_find_encoder_by_name((argc > 1) ? argv[2] : "mpeg4");
411 AVCodecContext* avctx;
412 char* def = av_malloc(5000);
413 const char* col = "";
419 avctx = avcodec_alloc_context();
423 const AVOption *stack[FF_OPT_MAX_DEPTH];
429 c = (const AVOption*)c->help;
437 int t = c->type & FF_OPT_TYPE_MASK;
438 printf("Config %s %s\n",
439 t == FF_OPT_TYPE_BOOL ? "bool " :
440 t == FF_OPT_TYPE_DOUBLE ? "double " :
441 t == FF_OPT_TYPE_INT ? "integer" :
442 t == FF_OPT_TYPE_STRING ? "string " :
443 "unknown??", c->name);
445 case FF_OPT_TYPE_BOOL:
446 i += sprintf(def + i, "%s%s=%s",
448 c->defval != 0. ? "on" : "off");
450 case FF_OPT_TYPE_DOUBLE:
451 i += sprintf(def + i, "%s%s=%f",
452 col, c->name, c->defval);
454 case FF_OPT_TYPE_INT:
455 i += sprintf(def + i, "%s%s=%d",
456 col, c->name, (int) c->defval);
458 case FF_OPT_TYPE_STRING:
460 char* d = av_strdup(c->defstr);
461 char* f = strchr(d, ',');
464 i += sprintf(def + i, "%s%s=%s",
475 printf("Default Options: %s\n", def);
481 int main(int argc, char **argv)
483 const char *filename;
485 /* must be called before using avcodec lib */
488 /* register all the codecs (you can also register only the codec
489 you wish to have smaller code */
490 avcodec_register_all();
493 options_example(argc, argv);
496 audio_encode_example("/tmp/test.mp2");
497 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
499 video_encode_example("/tmp/test.mpg");
500 filename = "/tmp/test.mpg";
505 // audio_decode_example("/tmp/test.sw", filename);
506 video_decode_example("/tmp/test%d.pgm", filename);