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 */
198 c->time_base= (AVRational){1,25};
199 c->gop_size = 10; /* emit one intra frame every ten frames */
203 if (avcodec_open(c, codec) < 0) {
204 fprintf(stderr, "could not open codec\n");
208 /* the codec gives us the frame size, in samples */
210 f = fopen(filename, "wb");
212 fprintf(stderr, "could not open %s\n", filename);
216 /* alloc image and output buffer */
217 outbuf_size = 100000;
218 outbuf = malloc(outbuf_size);
219 size = c->width * c->height;
220 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
222 picture->data[0] = picture_buf;
223 picture->data[1] = picture->data[0] + size;
224 picture->data[2] = picture->data[1] + size / 4;
225 picture->linesize[0] = c->width;
226 picture->linesize[1] = c->width / 2;
227 picture->linesize[2] = c->width / 2;
229 /* encode 1 second of video */
232 /* prepare a dummy image */
234 for(y=0;y<c->height;y++) {
235 for(x=0;x<c->width;x++) {
236 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
241 for(y=0;y<c->height/2;y++) {
242 for(x=0;x<c->width/2;x++) {
243 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
244 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
248 /* encode the image */
249 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
250 printf("encoding frame %3d (size=%5d)\n", i, out_size);
251 fwrite(outbuf, 1, out_size, f);
254 /* get the delayed frames */
255 for(; out_size; i++) {
258 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
259 printf("write frame %3d (size=%5d)\n", i, out_size);
260 fwrite(outbuf, 1, out_size, f);
263 /* add sequence end code to have a real mpeg file */
268 fwrite(outbuf, 1, 4, f);
280 * Video decoding example
283 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
288 f=fopen(filename,"w");
289 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
291 fwrite(buf + i * wrap,1,xsize,f);
295 void video_decode_example(const char *outfilename, const char *filename)
298 AVCodecContext *c= NULL;
299 int frame, size, got_picture, len;
302 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
305 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
306 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
308 printf("Video decoding\n");
310 /* find the mpeg1 video decoder */
311 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
313 fprintf(stderr, "codec not found\n");
317 c= avcodec_alloc_context();
318 picture= avcodec_alloc_frame();
320 if(codec->capabilities&CODEC_CAP_TRUNCATED)
321 c->flags|= CODEC_FLAG_TRUNCATED; /* we dont send complete frames */
323 /* for some codecs, such as msmpeg4 and mpeg4, width and height
324 MUST be initialized there because these info are not available
328 if (avcodec_open(c, codec) < 0) {
329 fprintf(stderr, "could not open codec\n");
333 /* the codec gives us the frame size, in samples */
335 f = fopen(filename, "rb");
337 fprintf(stderr, "could not open %s\n", filename);
343 size = fread(inbuf, 1, INBUF_SIZE, f);
347 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
348 and this is the only method to use them because you cannot
349 know the compressed data size before analysing it.
351 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
352 based, so you must call them with all the data for one
353 frame exactly. You must also initialize 'width' and
354 'height' before initializing them. */
356 /* NOTE2: some codecs allow the raw parameters (frame size,
357 sample rate) to be changed at any frame. We handle this, so
358 you should also take care of it */
360 /* here, we use a stream based decoder (mpeg1video), so we
361 feed decoder and see if it could decode a frame */
364 len = avcodec_decode_video(c, picture, &got_picture,
367 fprintf(stderr, "Error while decoding frame %d\n", frame);
371 printf("saving frame %3d\n", frame);
374 /* the picture is allocated by the decoder. no need to
376 snprintf(buf, sizeof(buf), outfilename, frame);
377 pgm_save(picture->data[0], picture->linesize[0],
378 c->width, c->height, buf);
386 /* some codecs, such as MPEG, transmit the I and P frame with a
387 latency of one frame. You must do the following to have a
388 chance to get the last frame of the video */
389 len = avcodec_decode_video(c, picture, &got_picture,
392 printf("saving last frame %3d\n", frame);
395 /* the picture is allocated by the decoder. no need to
397 snprintf(buf, sizeof(buf), outfilename, frame);
398 pgm_save(picture->data[0], picture->linesize[0],
399 c->width, c->height, buf);
411 int main(int argc, char **argv)
413 const char *filename;
415 /* must be called before using avcodec lib */
418 /* register all the codecs (you can also register only the codec
419 you wish to have smaller code */
420 avcodec_register_all();
423 audio_encode_example("/tmp/test.mp2");
424 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
426 video_encode_example("/tmp/test.mpg");
427 filename = "/tmp/test.mpg";
432 // audio_decode_example("/tmp/test.sw", filename);
433 video_decode_example("/tmp/test%d.pgm", filename);