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 */
200 if (avcodec_open(c, codec) < 0) {
201 fprintf(stderr, "could not open codec\n");
205 /* the codec gives us the frame size, in samples */
207 f = fopen(filename, "w");
209 fprintf(stderr, "could not open %s\n", filename);
213 /* alloc image and output buffer */
214 outbuf_size = 100000;
215 outbuf = malloc(outbuf_size);
216 size = c->width * c->height;
217 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
219 picture->data[0] = picture_buf;
220 picture->data[1] = picture->data[0] + size;
221 picture->data[2] = picture->data[1] + size / 4;
222 picture->linesize[0] = c->width;
223 picture->linesize[1] = c->width / 2;
224 picture->linesize[2] = c->width / 2;
226 /* encode 1 second of video */
228 printf("encoding frame %3d\r", i);
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 fwrite(outbuf, 1, out_size, f);
251 /* add sequence end code to have a real mpeg file */
256 fwrite(outbuf, 1, 4, f);
268 * Video decoding example
271 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
276 f=fopen(filename,"w");
277 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
279 fwrite(buf + i * wrap,1,xsize,f);
283 void video_decode_example(const char *outfilename, const char *filename)
286 AVCodecContext *c= NULL;
287 int frame, size, got_picture, len;
290 uint8_t inbuf[INBUF_SIZE], *inbuf_ptr;
293 printf("Video decoding\n");
295 /* find the mpeg1 video decoder */
296 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
298 fprintf(stderr, "codec not found\n");
302 c= avcodec_alloc_context();
303 picture= avcodec_alloc_frame();
305 if(codec->capabilities&CODEC_CAP_TRUNCATED)
306 c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
308 /* for some codecs, such as msmpeg4 and mpeg4, width and height
309 MUST be initialized there because these info are not available
313 if (avcodec_open(c, codec) < 0) {
314 fprintf(stderr, "could not open codec\n");
318 /* the codec gives us the frame size, in samples */
320 f = fopen(filename, "r");
322 fprintf(stderr, "could not open %s\n", filename);
328 size = fread(inbuf, 1, INBUF_SIZE, f);
332 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
333 and this is the only method to use them because you cannot
334 know the compressed data size before analysing it.
336 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
337 based, so you must call them with all the data for one
338 frame exactly. You must also initialize 'width' and
339 'height' before initializing them. */
341 /* NOTE2: some codecs allow the raw parameters (frame size,
342 sample rate) to be changed at any frame. We handle this, so
343 you should also take care of it */
345 /* here, we use a stream based decoder (mpeg1video), so we
346 feed decoder and see if it could decode a frame */
349 len = avcodec_decode_video(c, picture, &got_picture,
352 fprintf(stderr, "Error while decoding frame %d\n", frame);
356 printf("saving frame %3d\r", frame);
359 /* the picture is allocated by the decoder. no need to
361 snprintf(buf, sizeof(buf), outfilename, frame);
362 pgm_save(picture->data[0], picture->linesize[0],
363 c->width, c->height, buf);
371 /* some codecs, such as MPEG, transmit the I and P frame with a
372 latency of one frame. You must do the following to have a
373 chance to get the last frame of the video */
374 len = avcodec_decode_video(c, picture, &got_picture,
377 printf("saving frame %3d\r", frame);
380 /* the picture is allocated by the decoder. no need to
382 snprintf(buf, sizeof(buf), outfilename, frame);
383 pgm_save(picture->data[0], picture->linesize[0],
384 c->width, c->height, buf);
396 // simple example how the options could be used
397 int options_example(int argc, char* argv[])
399 AVCodec* codec = avcodec_find_encoder_by_name((argc > 1) ? argv[2] : "mpeg4");
401 AVCodecContext* avctx;
402 char* def = av_malloc(5000);
403 const char* col = "";
409 avctx = avcodec_alloc_context();
413 const AVOption *stack[FF_OPT_MAX_DEPTH];
419 c = (const AVOption*)c->help;
427 int t = c->type & FF_OPT_TYPE_MASK;
428 printf("Config %s %s\n",
429 t == FF_OPT_TYPE_BOOL ? "bool " :
430 t == FF_OPT_TYPE_DOUBLE ? "double " :
431 t == FF_OPT_TYPE_INT ? "integer" :
432 t == FF_OPT_TYPE_STRING ? "string " :
433 "unknown??", c->name);
435 case FF_OPT_TYPE_BOOL:
436 i += sprintf(def + i, "%s%s=%s",
438 c->defval != 0. ? "on" : "off");
440 case FF_OPT_TYPE_DOUBLE:
441 i += sprintf(def + i, "%s%s=%f",
442 col, c->name, c->defval);
444 case FF_OPT_TYPE_INT:
445 i += sprintf(def + i, "%s%s=%d",
446 col, c->name, (int) c->defval);
448 case FF_OPT_TYPE_STRING:
450 char* d = av_strdup(c->defstr);
451 char* f = strchr(d, ',');
454 i += sprintf(def + i, "%s%s=%s",
465 printf("Default Options: %s\n", def);
471 int main(int argc, char **argv)
473 const char *filename;
475 /* must be called before using avcodec lib */
478 /* register all the codecs (you can also register only the codec
479 you wish to have smaller code */
480 avcodec_register_all();
483 options_example(argc, argv);
486 audio_encode_example("/tmp/test.mp2");
487 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
489 video_encode_example("/tmp/test.mpg");
490 filename = "/tmp/test.mpg";
495 // audio_decode_example("/tmp/test.sw", filename);
496 video_decode_example("/tmp/test%d.pgm", filename);