2 * copyright (c) 2001 Fabrice Bellard
4 * This file is part of FFmpeg.
6 * FFmpeg 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 * FFmpeg 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 FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * avcodec API use example.
25 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
26 * not file formats (avi, vob, etc...). See library 'libavformat' for the
34 #ifdef HAVE_AV_CONFIG_H
35 #undef HAVE_AV_CONFIG_H
39 #include "libavutil/mathematics.h"
41 #define INBUF_SIZE 4096
44 * Audio encoding example
46 void audio_encode_example(const char *filename
)
49 AVCodecContext
*c
= NULL
;
50 int frame_size
, i
, j
, out_size
, outbuf_size
;
56 printf("Audio encoding\n");
58 /* find the MP2 encoder */
59 codec
= avcodec_find_encoder(CODEC_ID_MP2
);
61 fprintf(stderr
, "codec not found\n");
65 c
= avcodec_alloc_context();
67 /* put sample parameters */
69 c
->sample_rate
= 44100;
73 if (avcodec_open(c
, codec
) < 0) {
74 fprintf(stderr
, "could not open codec\n");
78 /* the codec gives us the frame size, in samples */
79 frame_size
= c
->frame_size
;
80 samples
= malloc(frame_size
* 2 * c
->channels
);
82 outbuf
= malloc(outbuf_size
);
84 f
= fopen(filename
, "wb");
86 fprintf(stderr
, "could not open %s\n", filename
);
90 /* encode a single tone sound */
92 tincr
= 2 * M_PI
* 440.0 / c
->sample_rate
;
94 for(j
=0;j
<frame_size
;j
++) {
95 samples
[2*j
] = (int)(sin(t
) * 10000);
96 samples
[2*j
+1] = samples
[2*j
];
99 /* encode the samples */
100 out_size
= avcodec_encode_audio(c
, outbuf
, outbuf_size
, samples
);
101 fwrite(outbuf
, 1, out_size
, f
);
114 void audio_decode_example(const char *outfilename
, const char *filename
)
117 AVCodecContext
*c
= NULL
;
118 int out_size
, size
, len
;
121 uint8_t inbuf
[INBUF_SIZE
+ FF_INPUT_BUFFER_PADDING_SIZE
], *inbuf_ptr
;
123 printf("Audio decoding\n");
125 /* find the mpeg audio decoder */
126 codec
= avcodec_find_decoder(CODEC_ID_MP2
);
128 fprintf(stderr
, "codec not found\n");
132 c
= avcodec_alloc_context();
135 if (avcodec_open(c
, codec
) < 0) {
136 fprintf(stderr
, "could not open codec\n");
140 outbuf
= malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE
);
142 f
= fopen(filename
, "rb");
144 fprintf(stderr
, "could not open %s\n", filename
);
147 outfile
= fopen(outfilename
, "wb");
153 /* decode until eof */
156 size
= fread(inbuf
, 1, INBUF_SIZE
, f
);
162 len
= avcodec_decode_audio(c
, (short *)outbuf
, &out_size
,
165 fprintf(stderr
, "Error while decoding\n");
169 /* if a frame has been decoded, output it */
170 fwrite(outbuf
, 1, out_size
, outfile
);
186 * Video encoding example
188 void video_encode_example(const char *filename
)
191 AVCodecContext
*c
= NULL
;
192 int i
, out_size
, size
, x
, y
, outbuf_size
;
195 uint8_t *outbuf
, *picture_buf
;
197 printf("Video encoding\n");
199 /* find the mpeg1 video encoder */
200 codec
= avcodec_find_encoder(CODEC_ID_MPEG1VIDEO
);
202 fprintf(stderr
, "codec not found\n");
206 c
= avcodec_alloc_context();
207 picture
= avcodec_alloc_frame();
209 /* put sample parameters */
210 c
->bit_rate
= 400000;
211 /* resolution must be a multiple of two */
214 /* frames per second */
215 c
->time_base
= (AVRational
){1,25};
216 c
->gop_size
= 10; /* emit one intra frame every ten frames */
218 c
->pix_fmt
= PIX_FMT_YUV420P
;
221 if (avcodec_open(c
, codec
) < 0) {
222 fprintf(stderr
, "could not open codec\n");
226 f
= fopen(filename
, "wb");
228 fprintf(stderr
, "could not open %s\n", filename
);
232 /* alloc image and output buffer */
233 outbuf_size
= 100000;
234 outbuf
= malloc(outbuf_size
);
235 size
= c
->width
* c
->height
;
236 picture_buf
= malloc((size
* 3) / 2); /* size for YUV 420 */
238 picture
->data
[0] = picture_buf
;
239 picture
->data
[1] = picture
->data
[0] + size
;
240 picture
->data
[2] = picture
->data
[1] + size
/ 4;
241 picture
->linesize
[0] = c
->width
;
242 picture
->linesize
[1] = c
->width
/ 2;
243 picture
->linesize
[2] = c
->width
/ 2;
245 /* encode 1 second of video */
248 /* prepare a dummy image */
250 for(y
=0;y
<c
->height
;y
++) {
251 for(x
=0;x
<c
->width
;x
++) {
252 picture
->data
[0][y
* picture
->linesize
[0] + x
] = x
+ y
+ i
* 3;
257 for(y
=0;y
<c
->height
/2;y
++) {
258 for(x
=0;x
<c
->width
/2;x
++) {
259 picture
->data
[1][y
* picture
->linesize
[1] + x
] = 128 + y
+ i
* 2;
260 picture
->data
[2][y
* picture
->linesize
[2] + x
] = 64 + x
+ i
* 5;
264 /* encode the image */
265 out_size
= avcodec_encode_video(c
, outbuf
, outbuf_size
, picture
);
266 printf("encoding frame %3d (size=%5d)\n", i
, out_size
);
267 fwrite(outbuf
, 1, out_size
, f
);
270 /* get the delayed frames */
271 for(; out_size
; i
++) {
274 out_size
= avcodec_encode_video(c
, outbuf
, outbuf_size
, NULL
);
275 printf("write frame %3d (size=%5d)\n", i
, out_size
);
276 fwrite(outbuf
, 1, out_size
, f
);
279 /* add sequence end code to have a real mpeg file */
284 fwrite(outbuf
, 1, 4, f
);
296 * Video decoding example
299 void pgm_save(unsigned char *buf
,int wrap
, int xsize
,int ysize
,char *filename
)
304 f
=fopen(filename
,"w");
305 fprintf(f
,"P5\n%d %d\n%d\n",xsize
,ysize
,255);
307 fwrite(buf
+ i
* wrap
,1,xsize
,f
);
311 void video_decode_example(const char *outfilename
, const char *filename
)
314 AVCodecContext
*c
= NULL
;
315 int frame
, size
, got_picture
, len
;
318 uint8_t inbuf
[INBUF_SIZE
+ FF_INPUT_BUFFER_PADDING_SIZE
], *inbuf_ptr
;
321 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */
322 memset(inbuf
+ INBUF_SIZE
, 0, FF_INPUT_BUFFER_PADDING_SIZE
);
324 printf("Video decoding\n");
326 /* find the mpeg1 video decoder */
327 codec
= avcodec_find_decoder(CODEC_ID_MPEG1VIDEO
);
329 fprintf(stderr
, "codec not found\n");
333 c
= avcodec_alloc_context();
334 picture
= avcodec_alloc_frame();
336 if(codec
->capabilities
&CODEC_CAP_TRUNCATED
)
337 c
->flags
|= CODEC_FLAG_TRUNCATED
; /* we do not send complete frames */
339 /* For some codecs, such as msmpeg4 and mpeg4, width and height
340 MUST be initialized there because this information is not
341 available in the bitstream. */
344 if (avcodec_open(c
, codec
) < 0) {
345 fprintf(stderr
, "could not open codec\n");
349 /* the codec gives us the frame size, in samples */
351 f
= fopen(filename
, "rb");
353 fprintf(stderr
, "could not open %s\n", filename
);
359 size
= fread(inbuf
, 1, INBUF_SIZE
, f
);
363 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
364 and this is the only method to use them because you cannot
365 know the compressed data size before analysing it.
367 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
368 based, so you must call them with all the data for one
369 frame exactly. You must also initialize 'width' and
370 'height' before initializing them. */
372 /* NOTE2: some codecs allow the raw parameters (frame size,
373 sample rate) to be changed at any frame. We handle this, so
374 you should also take care of it */
376 /* here, we use a stream based decoder (mpeg1video), so we
377 feed decoder and see if it could decode a frame */
380 len
= avcodec_decode_video(c
, picture
, &got_picture
,
383 fprintf(stderr
, "Error while decoding frame %d\n", frame
);
387 printf("saving 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
);
402 /* some codecs, such as MPEG, transmit the I and P frame with a
403 latency of one frame. You must do the following to have a
404 chance to get the last frame of the video */
405 len
= avcodec_decode_video(c
, picture
, &got_picture
,
408 printf("saving last frame %3d\n", frame
);
411 /* the picture is allocated by the decoder. no need to
413 snprintf(buf
, sizeof(buf
), outfilename
, frame
);
414 pgm_save(picture
->data
[0], picture
->linesize
[0],
415 c
->width
, c
->height
, buf
);
427 int main(int argc
, char **argv
)
429 const char *filename
;
431 /* must be called before using avcodec lib */
434 /* register all the codecs */
435 avcodec_register_all();
438 audio_encode_example("/tmp/test.mp2");
439 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
441 video_encode_example("/tmp/test.mpg");
442 filename
= "/tmp/test.mpg";
447 // audio_decode_example("/tmp/test.sw", filename);
448 video_decode_example("/tmp/test%d.pgm", filename
);