2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of Libav.
6 * Libav is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * Libav is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Libav; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * libavcodec API use example.
25 * @example libavcodec/api-example.c
26 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
27 * not file formats (avi, vob, etc...). See library 'libavformat' for the
35 #ifdef HAVE_AV_CONFIG_H
36 #undef HAVE_AV_CONFIG_H
39 #include "libavcodec/avcodec.h"
40 #include "libavutil/mathematics.h"
42 #define INBUF_SIZE 4096
43 #define AUDIO_INBUF_SIZE 20480
44 #define AUDIO_REFILL_THRESH 4096
47 * Audio encoding example
49 static void audio_encode_example(const char *filename)
52 AVCodecContext *c= NULL;
53 int frame_size, i, j, out_size, outbuf_size;
59 printf("Audio encoding\n");
61 /* find the MP2 encoder */
62 codec = avcodec_find_encoder(CODEC_ID_MP2);
64 fprintf(stderr, "codec not found\n");
68 c = avcodec_alloc_context3(codec);
70 /* put sample parameters */
72 c->sample_rate = 44100;
76 if (avcodec_open(c, codec) < 0) {
77 fprintf(stderr, "could not open codec\n");
81 /* the codec gives us the frame size, in samples */
82 frame_size = c->frame_size;
83 samples = malloc(frame_size * 2 * c->channels);
85 outbuf = malloc(outbuf_size);
87 f = fopen(filename, "wb");
89 fprintf(stderr, "could not open %s\n", filename);
93 /* encode a single tone sound */
95 tincr = 2 * M_PI * 440.0 / c->sample_rate;
97 for(j=0;j<frame_size;j++) {
98 samples[2*j] = (int)(sin(t) * 10000);
99 samples[2*j+1] = samples[2*j];
102 /* encode the samples */
103 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
104 fwrite(outbuf, 1, out_size, f);
117 static void audio_decode_example(const char *outfilename, const char *filename)
120 AVCodecContext *c= NULL;
124 uint8_t inbuf[AUDIO_INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
127 av_init_packet(&avpkt);
129 printf("Audio decoding\n");
131 /* find the mpeg audio decoder */
132 codec = avcodec_find_decoder(CODEC_ID_MP2);
134 fprintf(stderr, "codec not found\n");
138 c = avcodec_alloc_context3(codec);
141 if (avcodec_open(c, codec) < 0) {
142 fprintf(stderr, "could not open codec\n");
146 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
148 f = fopen(filename, "rb");
150 fprintf(stderr, "could not open %s\n", filename);
153 outfile = fopen(outfilename, "wb");
159 /* decode until eof */
161 avpkt.size = fread(inbuf, 1, AUDIO_INBUF_SIZE, f);
163 while (avpkt.size > 0) {
164 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
165 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
167 fprintf(stderr, "Error while decoding\n");
171 /* if a frame has been decoded, output it */
172 fwrite(outbuf, 1, out_size, outfile);
176 if (avpkt.size < AUDIO_REFILL_THRESH) {
177 /* Refill the input buffer, to avoid trying to decode
178 * incomplete frames. Instead of this, one could also use
179 * a parser, or use a proper container format through
181 memmove(inbuf, avpkt.data, avpkt.size);
183 len = fread(avpkt.data + avpkt.size, 1,
184 AUDIO_INBUF_SIZE - avpkt.size, f);
199 * Video encoding example
201 static void video_encode_example(const char *filename)
204 AVCodecContext *c= NULL;
205 int i, out_size, size, x, y, outbuf_size;
208 uint8_t *outbuf, *picture_buf;
210 printf("Video encoding\n");
212 /* find the mpeg1 video encoder */
213 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
215 fprintf(stderr, "codec not found\n");
219 c = avcodec_alloc_context3(codec);
220 picture= avcodec_alloc_frame();
222 /* put sample parameters */
223 c->bit_rate = 400000;
224 /* resolution must be a multiple of two */
227 /* frames per second */
228 c->time_base= (AVRational){1,25};
229 c->gop_size = 10; /* emit one intra frame every ten frames */
231 c->pix_fmt = PIX_FMT_YUV420P;
234 if (avcodec_open(c, codec) < 0) {
235 fprintf(stderr, "could not open codec\n");
239 f = fopen(filename, "wb");
241 fprintf(stderr, "could not open %s\n", filename);
245 /* alloc image and output buffer */
246 outbuf_size = 100000;
247 outbuf = malloc(outbuf_size);
248 size = c->width * c->height;
249 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
251 picture->data[0] = picture_buf;
252 picture->data[1] = picture->data[0] + size;
253 picture->data[2] = picture->data[1] + size / 4;
254 picture->linesize[0] = c->width;
255 picture->linesize[1] = c->width / 2;
256 picture->linesize[2] = c->width / 2;
258 /* encode 1 second of video */
261 /* prepare a dummy image */
263 for(y=0;y<c->height;y++) {
264 for(x=0;x<c->width;x++) {
265 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
270 for(y=0;y<c->height/2;y++) {
271 for(x=0;x<c->width/2;x++) {
272 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
273 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
277 /* encode the image */
278 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
279 printf("encoding frame %3d (size=%5d)\n", i, out_size);
280 fwrite(outbuf, 1, out_size, f);
283 /* get the delayed frames */
284 for(; out_size; i++) {
287 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
288 printf("write frame %3d (size=%5d)\n", i, out_size);
289 fwrite(outbuf, 1, out_size, f);
292 /* add sequence end code to have a real mpeg file */
297 fwrite(outbuf, 1, 4, f);
309 * Video decoding example
312 static void pgm_save(unsigned char *buf, int wrap, int xsize, int ysize,
318 f=fopen(filename,"w");
319 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
321 fwrite(buf + i * wrap,1,xsize,f);
325 static void video_decode_example(const char *outfilename, const char *filename)
328 AVCodecContext *c= NULL;
329 int frame, got_picture, len;
332 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
336 av_init_packet(&avpkt);
338 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
339 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
341 printf("Video decoding\n");
343 /* find the mpeg1 video decoder */
344 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
346 fprintf(stderr, "codec not found\n");
350 c = avcodec_alloc_context3(codec);
351 picture= avcodec_alloc_frame();
353 if(codec->capabilities&CODEC_CAP_TRUNCATED)
354 c->flags|= CODEC_FLAG_TRUNCATED; /* we do not send complete frames */
356 /* For some codecs, such as msmpeg4 and mpeg4, width and height
357 MUST be initialized there because this information is not
358 available in the bitstream. */
361 if (avcodec_open(c, codec) < 0) {
362 fprintf(stderr, "could not open codec\n");
366 /* the codec gives us the frame size, in samples */
368 f = fopen(filename, "rb");
370 fprintf(stderr, "could not open %s\n", filename);
376 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
380 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
381 and this is the only method to use them because you cannot
382 know the compressed data size before analysing it.
384 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
385 based, so you must call them with all the data for one
386 frame exactly. You must also initialize 'width' and
387 'height' before initializing them. */
389 /* NOTE2: some codecs allow the raw parameters (frame size,
390 sample rate) to be changed at any frame. We handle this, so
391 you should also take care of it */
393 /* here, we use a stream based decoder (mpeg1video), so we
394 feed decoder and see if it could decode a frame */
396 while (avpkt.size > 0) {
397 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
399 fprintf(stderr, "Error while decoding frame %d\n", frame);
403 printf("saving frame %3d\n", frame);
406 /* the picture is allocated by the decoder. no need to
408 snprintf(buf, sizeof(buf), outfilename, frame);
409 pgm_save(picture->data[0], picture->linesize[0],
410 c->width, c->height, buf);
418 /* some codecs, such as MPEG, transmit the I and P frame with a
419 latency of one frame. You must do the following to have a
420 chance to get the last frame of the video */
423 len = avcodec_decode_video2(c, picture, &got_picture, &avpkt);
425 printf("saving last frame %3d\n", frame);
428 /* the picture is allocated by the decoder. no need to
430 snprintf(buf, sizeof(buf), outfilename, frame);
431 pgm_save(picture->data[0], picture->linesize[0],
432 c->width, c->height, buf);
444 int main(int argc, char **argv)
446 const char *filename;
448 /* must be called before using avcodec lib */
451 /* register all the codecs */
452 avcodec_register_all();
455 audio_encode_example("/tmp/test.mp2");
456 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
458 video_encode_example("/tmp/test.mpg");
459 filename = "/tmp/test.mpg";
464 // audio_decode_example("/tmp/test.sw", filename);
465 video_decode_example("/tmp/test%d.pgm", filename);