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