comparison apiexample.c @ 2967:ef2149182f1c libavcodec

COSMETICS: Remove all trailing whitespace.
author diego
date Sat, 17 Dec 2005 18:14:38 +0000
parents dfcfaeed1679
children c537a97eec66
comparison
equal deleted inserted replaced
2966:564788471dd4 2967:ef2149182f1c
2 * @file apiexample.c 2 * @file apiexample.c
3 * avcodec API use example. 3 * avcodec API use example.
4 * 4 *
5 * Note that this library only handles codecs (mpeg, mpeg4, etc...), 5 * Note that this library only handles codecs (mpeg, mpeg4, etc...),
6 * not file formats (avi, vob, etc...). See library 'libavformat' for the 6 * not file formats (avi, vob, etc...). See library 'libavformat' for the
7 * format handling 7 * format handling
8 */ 8 */
9 9
10 #include <stdlib.h> 10 #include <stdlib.h>
11 #include <stdio.h> 11 #include <stdio.h>
12 #include <string.h> 12 #include <string.h>
19 #include "avcodec.h" 19 #include "avcodec.h"
20 20
21 #define INBUF_SIZE 4096 21 #define INBUF_SIZE 4096
22 22
23 /* 23 /*
24 * Audio encoding example 24 * Audio encoding example
25 */ 25 */
26 void audio_encode_example(const char *filename) 26 void audio_encode_example(const char *filename)
27 { 27 {
28 AVCodec *codec; 28 AVCodec *codec;
29 AVCodecContext *c= NULL; 29 AVCodecContext *c= NULL;
41 fprintf(stderr, "codec not found\n"); 41 fprintf(stderr, "codec not found\n");
42 exit(1); 42 exit(1);
43 } 43 }
44 44
45 c= avcodec_alloc_context(); 45 c= avcodec_alloc_context();
46 46
47 /* put sample parameters */ 47 /* put sample parameters */
48 c->bit_rate = 64000; 48 c->bit_rate = 64000;
49 c->sample_rate = 44100; 49 c->sample_rate = 44100;
50 c->channels = 2; 50 c->channels = 2;
51 51
52 /* open it */ 52 /* open it */
53 if (avcodec_open(c, codec) < 0) { 53 if (avcodec_open(c, codec) < 0) {
54 fprintf(stderr, "could not open codec\n"); 54 fprintf(stderr, "could not open codec\n");
55 exit(1); 55 exit(1);
56 } 56 }
57 57
58 /* the codec gives us the frame size, in samples */ 58 /* the codec gives us the frame size, in samples */
59 frame_size = c->frame_size; 59 frame_size = c->frame_size;
60 samples = malloc(frame_size * 2 * c->channels); 60 samples = malloc(frame_size * 2 * c->channels);
61 outbuf_size = 10000; 61 outbuf_size = 10000;
62 outbuf = malloc(outbuf_size); 62 outbuf = malloc(outbuf_size);
64 f = fopen(filename, "wb"); 64 f = fopen(filename, "wb");
65 if (!f) { 65 if (!f) {
66 fprintf(stderr, "could not open %s\n", filename); 66 fprintf(stderr, "could not open %s\n", filename);
67 exit(1); 67 exit(1);
68 } 68 }
69 69
70 /* encode a single tone sound */ 70 /* encode a single tone sound */
71 t = 0; 71 t = 0;
72 tincr = 2 * M_PI * 440.0 / c->sample_rate; 72 tincr = 2 * M_PI * 440.0 / c->sample_rate;
73 for(i=0;i<200;i++) { 73 for(i=0;i<200;i++) {
74 for(j=0;j<frame_size;j++) { 74 for(j=0;j<frame_size;j++) {
87 avcodec_close(c); 87 avcodec_close(c);
88 av_free(c); 88 av_free(c);
89 } 89 }
90 90
91 /* 91 /*
92 * Audio decoding. 92 * Audio decoding.
93 */ 93 */
94 void audio_decode_example(const char *outfilename, const char *filename) 94 void audio_decode_example(const char *outfilename, const char *filename)
95 { 95 {
96 AVCodec *codec; 96 AVCodec *codec;
97 AVCodecContext *c= NULL; 97 AVCodecContext *c= NULL;
99 FILE *f, *outfile; 99 FILE *f, *outfile;
100 uint8_t *outbuf; 100 uint8_t *outbuf;
101 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr; 101 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
102 102
103 printf("Audio decoding\n"); 103 printf("Audio decoding\n");
104 104
105 /* set end of buffer to 0 (this ensures that no overreading happens for damaged mpeg streams) */ 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); 106 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
107 107
108 /* find the mpeg audio decoder */ 108 /* find the mpeg audio decoder */
109 codec = avcodec_find_decoder(CODEC_ID_MP2); 109 codec = avcodec_find_decoder(CODEC_ID_MP2);
117 /* open it */ 117 /* open it */
118 if (avcodec_open(c, codec) < 0) { 118 if (avcodec_open(c, codec) < 0) {
119 fprintf(stderr, "could not open codec\n"); 119 fprintf(stderr, "could not open codec\n");
120 exit(1); 120 exit(1);
121 } 121 }
122 122
123 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE); 123 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
124 124
125 f = fopen(filename, "rb"); 125 f = fopen(filename, "rb");
126 if (!f) { 126 if (!f) {
127 fprintf(stderr, "could not open %s\n", filename); 127 fprintf(stderr, "could not open %s\n", filename);
130 outfile = fopen(outfilename, "wb"); 130 outfile = fopen(outfilename, "wb");
131 if (!outfile) { 131 if (!outfile) {
132 av_free(c); 132 av_free(c);
133 exit(1); 133 exit(1);
134 } 134 }
135 135
136 /* decode until eof */ 136 /* decode until eof */
137 inbuf_ptr = inbuf; 137 inbuf_ptr = inbuf;
138 for(;;) { 138 for(;;) {
139 size = fread(inbuf, 1, INBUF_SIZE, f); 139 size = fread(inbuf, 1, INBUF_SIZE, f);
140 if (size == 0) 140 if (size == 0)
141 break; 141 break;
142 142
143 inbuf_ptr = inbuf; 143 inbuf_ptr = inbuf;
144 while (size > 0) { 144 while (size > 0) {
145 len = avcodec_decode_audio(c, (short *)outbuf, &out_size, 145 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
146 inbuf_ptr, size); 146 inbuf_ptr, size);
147 if (len < 0) { 147 if (len < 0) {
148 fprintf(stderr, "Error while decoding\n"); 148 fprintf(stderr, "Error while decoding\n");
149 exit(1); 149 exit(1);
150 } 150 }
164 avcodec_close(c); 164 avcodec_close(c);
165 av_free(c); 165 av_free(c);
166 } 166 }
167 167
168 /* 168 /*
169 * Video encoding example 169 * Video encoding example
170 */ 170 */
171 void video_encode_example(const char *filename) 171 void video_encode_example(const char *filename)
172 { 172 {
173 AVCodec *codec; 173 AVCodec *codec;
174 AVCodecContext *c= NULL; 174 AVCodecContext *c= NULL;
186 exit(1); 186 exit(1);
187 } 187 }
188 188
189 c= avcodec_alloc_context(); 189 c= avcodec_alloc_context();
190 picture= avcodec_alloc_frame(); 190 picture= avcodec_alloc_frame();
191 191
192 /* put sample parameters */ 192 /* put sample parameters */
193 c->bit_rate = 400000; 193 c->bit_rate = 400000;
194 /* resolution must be a multiple of two */ 194 /* resolution must be a multiple of two */
195 c->width = 352; 195 c->width = 352;
196 c->height = 288; 196 c->height = 288;
197 /* frames per second */ 197 /* frames per second */
198 c->time_base= (AVRational){1,25}; 198 c->time_base= (AVRational){1,25};
199 c->gop_size = 10; /* emit one intra frame every ten frames */ 199 c->gop_size = 10; /* emit one intra frame every ten frames */
200 c->max_b_frames=1; 200 c->max_b_frames=1;
203 /* open it */ 203 /* open it */
204 if (avcodec_open(c, codec) < 0) { 204 if (avcodec_open(c, codec) < 0) {
205 fprintf(stderr, "could not open codec\n"); 205 fprintf(stderr, "could not open codec\n");
206 exit(1); 206 exit(1);
207 } 207 }
208 208
209 /* the codec gives us the frame size, in samples */ 209 /* the codec gives us the frame size, in samples */
210 210
211 f = fopen(filename, "wb"); 211 f = fopen(filename, "wb");
212 if (!f) { 212 if (!f) {
213 fprintf(stderr, "could not open %s\n", filename); 213 fprintf(stderr, "could not open %s\n", filename);
214 exit(1); 214 exit(1);
215 } 215 }
216 216
217 /* alloc image and output buffer */ 217 /* alloc image and output buffer */
218 outbuf_size = 100000; 218 outbuf_size = 100000;
219 outbuf = malloc(outbuf_size); 219 outbuf = malloc(outbuf_size);
220 size = c->width * c->height; 220 size = c->width * c->height;
221 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */ 221 picture_buf = malloc((size * 3) / 2); /* size for YUV 420 */
222 222
223 picture->data[0] = picture_buf; 223 picture->data[0] = picture_buf;
224 picture->data[1] = picture->data[0] + size; 224 picture->data[1] = picture->data[0] + size;
225 picture->data[2] = picture->data[1] + size / 4; 225 picture->data[2] = picture->data[1] + size / 4;
226 picture->linesize[0] = c->width; 226 picture->linesize[0] = c->width;
227 picture->linesize[1] = c->width / 2; 227 picture->linesize[1] = c->width / 2;
253 } 253 }
254 254
255 /* get the delayed frames */ 255 /* get the delayed frames */
256 for(; out_size; i++) { 256 for(; out_size; i++) {
257 fflush(stdout); 257 fflush(stdout);
258 258
259 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL); 259 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
260 printf("write frame %3d (size=%5d)\n", i, out_size); 260 printf("write frame %3d (size=%5d)\n", i, out_size);
261 fwrite(outbuf, 1, out_size, f); 261 fwrite(outbuf, 1, out_size, f);
262 } 262 }
263 263
276 av_free(picture); 276 av_free(picture);
277 printf("\n"); 277 printf("\n");
278 } 278 }
279 279
280 /* 280 /*
281 * Video decoding example 281 * Video decoding example
282 */ 282 */
283 283
284 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename) 284 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
285 { 285 {
286 FILE *f; 286 FILE *f;
287 int i; 287 int i;
288 288
289 f=fopen(filename,"w"); 289 f=fopen(filename,"w");
328 /* open it */ 328 /* open it */
329 if (avcodec_open(c, codec) < 0) { 329 if (avcodec_open(c, codec) < 0) {
330 fprintf(stderr, "could not open codec\n"); 330 fprintf(stderr, "could not open codec\n");
331 exit(1); 331 exit(1);
332 } 332 }
333 333
334 /* the codec gives us the frame size, in samples */ 334 /* the codec gives us the frame size, in samples */
335 335
336 f = fopen(filename, "rb"); 336 f = fopen(filename, "rb");
337 if (!f) { 337 if (!f) {
338 fprintf(stderr, "could not open %s\n", filename); 338 fprintf(stderr, "could not open %s\n", filename);
339 exit(1); 339 exit(1);
340 } 340 }
341 341
342 frame = 0; 342 frame = 0;
343 for(;;) { 343 for(;;) {
344 size = fread(inbuf, 1, INBUF_SIZE, f); 344 size = fread(inbuf, 1, INBUF_SIZE, f);
345 if (size == 0) 345 if (size == 0)
346 break; 346 break;
347 347
348 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio) 348 /* NOTE1: some codecs are stream based (mpegvideo, mpegaudio)
349 and this is the only method to use them because you cannot 349 and this is the only method to use them because you cannot
350 know the compressed data size before analysing it. 350 know the compressed data size before analysing it.
351 351
352 BUT some other codecs (msmpeg4, mpeg4) are inherently frame 352 BUT some other codecs (msmpeg4, mpeg4) are inherently frame
353 based, so you must call them with all the data for one 353 based, so you must call them with all the data for one
354 frame exactly. You must also initialize 'width' and 354 frame exactly. You must also initialize 'width' and
355 'height' before initializing them. */ 355 'height' before initializing them. */
360 360
361 /* here, we use a stream based decoder (mpeg1video), so we 361 /* here, we use a stream based decoder (mpeg1video), so we
362 feed decoder and see if it could decode a frame */ 362 feed decoder and see if it could decode a frame */
363 inbuf_ptr = inbuf; 363 inbuf_ptr = inbuf;
364 while (size > 0) { 364 while (size > 0) {
365 len = avcodec_decode_video(c, picture, &got_picture, 365 len = avcodec_decode_video(c, picture, &got_picture,
366 inbuf_ptr, size); 366 inbuf_ptr, size);
367 if (len < 0) { 367 if (len < 0) {
368 fprintf(stderr, "Error while decoding frame %d\n", frame); 368 fprintf(stderr, "Error while decoding frame %d\n", frame);
369 exit(1); 369 exit(1);
370 } 370 }
373 fflush(stdout); 373 fflush(stdout);
374 374
375 /* the picture is allocated by the decoder. no need to 375 /* the picture is allocated by the decoder. no need to
376 free it */ 376 free it */
377 snprintf(buf, sizeof(buf), outfilename, frame); 377 snprintf(buf, sizeof(buf), outfilename, frame);
378 pgm_save(picture->data[0], picture->linesize[0], 378 pgm_save(picture->data[0], picture->linesize[0],
379 c->width, c->height, buf); 379 c->width, c->height, buf);
380 frame++; 380 frame++;
381 } 381 }
382 size -= len; 382 size -= len;
383 inbuf_ptr += len; 383 inbuf_ptr += len;
385 } 385 }
386 386
387 /* some codecs, such as MPEG, transmit the I and P frame with a 387 /* some codecs, such as MPEG, transmit the I and P frame with a
388 latency of one frame. You must do the following to have a 388 latency of one frame. You must do the following to have a
389 chance to get the last frame of the video */ 389 chance to get the last frame of the video */
390 len = avcodec_decode_video(c, picture, &got_picture, 390 len = avcodec_decode_video(c, picture, &got_picture,
391 NULL, 0); 391 NULL, 0);
392 if (got_picture) { 392 if (got_picture) {
393 printf("saving last frame %3d\n", frame); 393 printf("saving last frame %3d\n", frame);
394 fflush(stdout); 394 fflush(stdout);
395 395
396 /* the picture is allocated by the decoder. no need to 396 /* the picture is allocated by the decoder. no need to
397 free it */ 397 free it */
398 snprintf(buf, sizeof(buf), outfilename, frame); 398 snprintf(buf, sizeof(buf), outfilename, frame);
399 pgm_save(picture->data[0], picture->linesize[0], 399 pgm_save(picture->data[0], picture->linesize[0],
400 c->width, c->height, buf); 400 c->width, c->height, buf);
401 frame++; 401 frame++;
402 } 402 }
403 403
404 fclose(f); 404 fclose(f);
405 405
406 avcodec_close(c); 406 avcodec_close(c);
407 av_free(c); 407 av_free(c);
408 av_free(picture); 408 av_free(picture);