00001
00010 #include <stdlib.h>
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <math.h>
00014
00015 #ifdef HAVE_AV_CONFIG_H
00016 #undef HAVE_AV_CONFIG_H
00017 #endif
00018
00019 #include "avcodec.h"
00020
00021 #define INBUF_SIZE 4096
00022
00023
00024
00025
00026 void audio_encode_example(const char *filename)
00027 {
00028 AVCodec *codec;
00029 AVCodecContext *c= NULL;
00030 int frame_size, i, j, out_size, outbuf_size;
00031 FILE *f;
00032 short *samples;
00033 float t, tincr;
00034 uint8_t *outbuf;
00035
00036 printf("Audio encoding\n");
00037
00038
00039 codec = avcodec_find_encoder(CODEC_ID_MP2);
00040 if (!codec) {
00041 fprintf(stderr, "codec not found\n");
00042 exit(1);
00043 }
00044
00045 c= avcodec_alloc_context();
00046
00047
00048 c->bit_rate = 64000;
00049 c->sample_rate = 44100;
00050 c->channels = 2;
00051
00052
00053 if (avcodec_open(c, codec) < 0) {
00054 fprintf(stderr, "could not open codec\n");
00055 exit(1);
00056 }
00057
00058
00059 frame_size = c->frame_size;
00060 samples = malloc(frame_size * 2 * c->channels);
00061 outbuf_size = 10000;
00062 outbuf = malloc(outbuf_size);
00063
00064 f = fopen(filename, "wb");
00065 if (!f) {
00066 fprintf(stderr, "could not open %s\n", filename);
00067 exit(1);
00068 }
00069
00070
00071 t = 0;
00072 tincr = 2 * M_PI * 440.0 / c->sample_rate;
00073 for(i=0;i<200;i++) {
00074 for(j=0;j<frame_size;j++) {
00075 samples[2*j] = (int)(sin(t) * 10000);
00076 samples[2*j+1] = samples[2*j];
00077 t += tincr;
00078 }
00079
00080 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
00081 fwrite(outbuf, 1, out_size, f);
00082 }
00083 fclose(f);
00084 free(outbuf);
00085 free(samples);
00086
00087 avcodec_close(c);
00088 av_free(c);
00089 }
00090
00091
00092
00093
00094 void audio_decode_example(const char *outfilename, const char *filename)
00095 {
00096 AVCodec *codec;
00097 AVCodecContext *c= NULL;
00098 int out_size, size, len;
00099 FILE *f, *outfile;
00100 uint8_t *outbuf;
00101 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
00102
00103 printf("Audio decoding\n");
00104
00105
00106 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00107
00108
00109 codec = avcodec_find_decoder(CODEC_ID_MP2);
00110 if (!codec) {
00111 fprintf(stderr, "codec not found\n");
00112 exit(1);
00113 }
00114
00115 c= avcodec_alloc_context();
00116
00117
00118 if (avcodec_open(c, codec) < 0) {
00119 fprintf(stderr, "could not open codec\n");
00120 exit(1);
00121 }
00122
00123 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
00124
00125 f = fopen(filename, "rb");
00126 if (!f) {
00127 fprintf(stderr, "could not open %s\n", filename);
00128 exit(1);
00129 }
00130 outfile = fopen(outfilename, "wb");
00131 if (!outfile) {
00132 av_free(c);
00133 exit(1);
00134 }
00135
00136
00137 inbuf_ptr = inbuf;
00138 for(;;) {
00139 size = fread(inbuf, 1, INBUF_SIZE, f);
00140 if (size == 0)
00141 break;
00142
00143 inbuf_ptr = inbuf;
00144 while (size > 0) {
00145 len = avcodec_decode_audio(c, (short *)outbuf, &out_size,
00146 inbuf_ptr, size);
00147 if (len < 0) {
00148 fprintf(stderr, "Error while decoding\n");
00149 exit(1);
00150 }
00151 if (out_size > 0) {
00152
00153 fwrite(outbuf, 1, out_size, outfile);
00154 }
00155 size -= len;
00156 inbuf_ptr += len;
00157 }
00158 }
00159
00160 fclose(outfile);
00161 fclose(f);
00162 free(outbuf);
00163
00164 avcodec_close(c);
00165 av_free(c);
00166 }
00167
00168
00169
00170
00171 void video_encode_example(const char *filename)
00172 {
00173 AVCodec *codec;
00174 AVCodecContext *c= NULL;
00175 int i, out_size, size, x, y, outbuf_size;
00176 FILE *f;
00177 AVFrame *picture;
00178 uint8_t *outbuf, *picture_buf;
00179
00180 printf("Video encoding\n");
00181
00182
00183 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
00184 if (!codec) {
00185 fprintf(stderr, "codec not found\n");
00186 exit(1);
00187 }
00188
00189 c= avcodec_alloc_context();
00190 picture= avcodec_alloc_frame();
00191
00192
00193 c->bit_rate = 400000;
00194
00195 c->width = 352;
00196 c->height = 288;
00197
00198 c->time_base= (AVRational){1,25};
00199 c->gop_size = 10;
00200 c->max_b_frames=1;
00201 c->pix_fmt = PIX_FMT_YUV420P;
00202
00203
00204 if (avcodec_open(c, codec) < 0) {
00205 fprintf(stderr, "could not open codec\n");
00206 exit(1);
00207 }
00208
00209
00210
00211 f = fopen(filename, "wb");
00212 if (!f) {
00213 fprintf(stderr, "could not open %s\n", filename);
00214 exit(1);
00215 }
00216
00217
00218 outbuf_size = 100000;
00219 outbuf = malloc(outbuf_size);
00220 size = c->width * c->height;
00221 picture_buf = malloc((size * 3) / 2);
00222
00223 picture->data[0] = picture_buf;
00224 picture->data[1] = picture->data[0] + size;
00225 picture->data[2] = picture->data[1] + size / 4;
00226 picture->linesize[0] = c->width;
00227 picture->linesize[1] = c->width / 2;
00228 picture->linesize[2] = c->width / 2;
00229
00230
00231 for(i=0;i<25;i++) {
00232 fflush(stdout);
00233
00234
00235 for(y=0;y<c->height;y++) {
00236 for(x=0;x<c->width;x++) {
00237 picture->data[0][y * picture->linesize[0] + x] = x + y + i * 3;
00238 }
00239 }
00240
00241
00242 for(y=0;y<c->height/2;y++) {
00243 for(x=0;x<c->width/2;x++) {
00244 picture->data[1][y * picture->linesize[1] + x] = 128 + y + i * 2;
00245 picture->data[2][y * picture->linesize[2] + x] = 64 + x + i * 5;
00246 }
00247 }
00248
00249
00250 out_size = avcodec_encode_video(c, outbuf, outbuf_size, picture);
00251 printf("encoding frame %3d (size=%5d)\n", i, out_size);
00252 fwrite(outbuf, 1, out_size, f);
00253 }
00254
00255
00256 for(; out_size; i++) {
00257 fflush(stdout);
00258
00259 out_size = avcodec_encode_video(c, outbuf, outbuf_size, NULL);
00260 printf("write frame %3d (size=%5d)\n", i, out_size);
00261 fwrite(outbuf, 1, out_size, f);
00262 }
00263
00264
00265 outbuf[0] = 0x00;
00266 outbuf[1] = 0x00;
00267 outbuf[2] = 0x01;
00268 outbuf[3] = 0xb7;
00269 fwrite(outbuf, 1, 4, f);
00270 fclose(f);
00271 free(picture_buf);
00272 free(outbuf);
00273
00274 avcodec_close(c);
00275 av_free(c);
00276 av_free(picture);
00277 printf("\n");
00278 }
00279
00280
00281
00282
00283
00284 void pgm_save(unsigned char *buf,int wrap, int xsize,int ysize,char *filename)
00285 {
00286 FILE *f;
00287 int i;
00288
00289 f=fopen(filename,"w");
00290 fprintf(f,"P5\n%d %d\n%d\n",xsize,ysize,255);
00291 for(i=0;i<ysize;i++)
00292 fwrite(buf + i * wrap,1,xsize,f);
00293 fclose(f);
00294 }
00295
00296 void video_decode_example(const char *outfilename, const char *filename)
00297 {
00298 AVCodec *codec;
00299 AVCodecContext *c= NULL;
00300 int frame, size, got_picture, len;
00301 FILE *f;
00302 AVFrame *picture;
00303 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE], *inbuf_ptr;
00304 char buf[1024];
00305
00306
00307 memset(inbuf + INBUF_SIZE, 0, FF_INPUT_BUFFER_PADDING_SIZE);
00308
00309 printf("Video decoding\n");
00310
00311
00312 codec = avcodec_find_decoder(CODEC_ID_MPEG1VIDEO);
00313 if (!codec) {
00314 fprintf(stderr, "codec not found\n");
00315 exit(1);
00316 }
00317
00318 c= avcodec_alloc_context();
00319 picture= avcodec_alloc_frame();
00320
00321 if(codec->capabilities&CODEC_CAP_TRUNCATED)
00322 c->flags|= CODEC_FLAG_TRUNCATED;
00323
00324
00325
00326
00327
00328
00329 if (avcodec_open(c, codec) < 0) {
00330 fprintf(stderr, "could not open codec\n");
00331 exit(1);
00332 }
00333
00334
00335
00336 f = fopen(filename, "rb");
00337 if (!f) {
00338 fprintf(stderr, "could not open %s\n", filename);
00339 exit(1);
00340 }
00341
00342 frame = 0;
00343 for(;;) {
00344 size = fread(inbuf, 1, INBUF_SIZE, f);
00345 if (size == 0)
00346 break;
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363 inbuf_ptr = inbuf;
00364 while (size > 0) {
00365 len = avcodec_decode_video(c, picture, &got_picture,
00366 inbuf_ptr, size);
00367 if (len < 0) {
00368 fprintf(stderr, "Error while decoding frame %d\n", frame);
00369 exit(1);
00370 }
00371 if (got_picture) {
00372 printf("saving frame %3d\n", frame);
00373 fflush(stdout);
00374
00375
00376
00377 snprintf(buf, sizeof(buf), outfilename, frame);
00378 pgm_save(picture->data[0], picture->linesize[0],
00379 c->width, c->height, buf);
00380 frame++;
00381 }
00382 size -= len;
00383 inbuf_ptr += len;
00384 }
00385 }
00386
00387
00388
00389
00390 len = avcodec_decode_video(c, picture, &got_picture,
00391 NULL, 0);
00392 if (got_picture) {
00393 printf("saving last frame %3d\n", frame);
00394 fflush(stdout);
00395
00396
00397
00398 snprintf(buf, sizeof(buf), outfilename, frame);
00399 pgm_save(picture->data[0], picture->linesize[0],
00400 c->width, c->height, buf);
00401 frame++;
00402 }
00403
00404 fclose(f);
00405
00406 avcodec_close(c);
00407 av_free(c);
00408 av_free(picture);
00409 printf("\n");
00410 }
00411
00412 int main(int argc, char **argv)
00413 {
00414 const char *filename;
00415
00416
00417 avcodec_init();
00418
00419
00420
00421 avcodec_register_all();
00422
00423 if (argc <= 1) {
00424 audio_encode_example("/tmp/test.mp2");
00425 audio_decode_example("/tmp/test.sw", "/tmp/test.mp2");
00426
00427 video_encode_example("/tmp/test.mpg");
00428 filename = "/tmp/test.mpg";
00429 } else {
00430 filename = argv[1];
00431 }
00432
00433
00434 video_decode_example("/tmp/test%d.pgm", filename);
00435
00436 return 0;
00437 }