00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <string.h>
00033
00034 #ifdef HAVE_AV_CONFIG_H
00035 #undef HAVE_AV_CONFIG_H
00036 #endif
00037
00038 #include "libavcodec/avcodec.h"
00039 #include "libavutil/mathematics.h"
00040
00041 #define INBUF_SIZE 4096
00042
00043
00044
00045
00046 static void audio_encode_example(const char *filename)
00047 {
00048 AVCodec *codec;
00049 AVCodecContext *c= NULL;
00050 int frame_size, i, j, out_size, outbuf_size;
00051 FILE *f;
00052 short *samples;
00053 float t, tincr;
00054 uint8_t *outbuf;
00055
00056 printf("Audio encoding\n");
00057
00058
00059 codec = avcodec_find_encoder(CODEC_ID_MP2);
00060 if (!codec) {
00061 fprintf(stderr, "codec not found\n");
00062 exit(1);
00063 }
00064
00065 c= avcodec_alloc_context();
00066
00067
00068 c->bit_rate = 64000;
00069 c->sample_rate = 44100;
00070 c->channels = 2;
00071
00072
00073 if (avcodec_open(c, codec) < 0) {
00074 fprintf(stderr, "could not open codec\n");
00075 exit(1);
00076 }
00077
00078
00079 frame_size = c->frame_size;
00080 samples = malloc(frame_size * 2 * c->channels);
00081 outbuf_size = 10000;
00082 outbuf = malloc(outbuf_size);
00083
00084 f = fopen(filename, "wb");
00085 if (!f) {
00086 fprintf(stderr, "could not open %s\n", filename);
00087 exit(1);
00088 }
00089
00090
00091 t = 0;
00092 tincr = 2 * M_PI * 440.0 / c->sample_rate;
00093 for(i=0;i<200;i++) {
00094 for(j=0;j<frame_size;j++) {
00095 samples[2*j] = (int)(sin(t) * 10000);
00096 samples[2*j+1] = samples[2*j];
00097 t += tincr;
00098 }
00099
00100 out_size = avcodec_encode_audio(c, outbuf, outbuf_size, samples);
00101 fwrite(outbuf, 1, out_size, f);
00102 }
00103 fclose(f);
00104 free(outbuf);
00105 free(samples);
00106
00107 avcodec_close(c);
00108 av_free(c);
00109 }
00110
00111
00112
00113
00114 static void audio_decode_example(const char *outfilename, const char *filename)
00115 {
00116 AVCodec *codec;
00117 AVCodecContext *c= NULL;
00118 int out_size, len;
00119 FILE *f, *outfile;
00120 uint8_t *outbuf;
00121 uint8_t inbuf[INBUF_SIZE + FF_INPUT_BUFFER_PADDING_SIZE];
00122 AVPacket avpkt;
00123
00124 av_init_packet(&avpkt);
00125
00126 printf("Audio decoding\n");
00127
00128
00129 codec = avcodec_find_decoder(CODEC_ID_MP2);
00130 if (!codec) {
00131 fprintf(stderr, "codec not found\n");
00132 exit(1);
00133 }
00134
00135 c= avcodec_alloc_context();
00136
00137
00138 if (avcodec_open(c, codec) < 0) {
00139 fprintf(stderr, "could not open codec\n");
00140 exit(1);
00141 }
00142
00143 outbuf = malloc(AVCODEC_MAX_AUDIO_FRAME_SIZE);
00144
00145 f = fopen(filename, "rb");
00146 if (!f) {
00147 fprintf(stderr, "could not open %s\n", filename);
00148 exit(1);
00149 }
00150 outfile = fopen(outfilename, "wb");
00151 if (!outfile) {
00152 av_free(c);
00153 exit(1);
00154 }
00155
00156
00157 avpkt.data = inbuf;
00158 for(;;) {
00159 avpkt.size = fread(inbuf, 1, INBUF_SIZE, f);
00160 if (avpkt.size == 0)
00161 break;
00162
00163 avpkt.data = inbuf;
00164 while (avpkt.size > 0) {
00165 out_size = AVCODEC_MAX_AUDIO_FRAME_SIZE;
00166 len = avcodec_decode_audio3(c, (short *)outbuf, &out_size, &avpkt);
00167 if (len < 0) {
00168 fprintf(stderr, "Error while decoding\n");
00169 exit(1);
00170 }
00171 if (out_size > 0) {
00172
00173 fwrite(outbuf, 1, out_size, outfile);
00174 }
00175 avpkt.size -= len;
00176 avpkt.data += len;
00177 }
00178 }
00179
00180 fclose(outfile);
00181 fclose(f);
00182 free(outbuf);
00183
00184 avcodec_close(c);
00185 av_free(c);
00186 }
00187
00188
00189
00190
00191 static void video_encode_example(const char *filename)
00192 {
00193 AVCodec *codec;
00194 AVCodecContext *c= NULL;
00195 int i, out_size, size, x, y, outbuf_size;
00196 FILE *f;
00197 AVFrame *picture;
00198 uint8_t *outbuf, *picture_buf;
00199
00200 printf("Video encoding\n");
00201
00202
00203 codec = avcodec_find_encoder(CODEC_ID_MPEG1VIDEO);
00204 if (!codec) {
00205 fprintf(stderr, "codec not found\n");
00206 exit(1);
00207 }
00208
00209 c= avcodec_alloc_context();
00210 picture= avcodec_alloc_frame();
00211
00212
00213 c->bit_rate = 400000;
00214
00215 c->width = 352;
00216 c->height = 288;
00217
00218 c->time_base= (AVRational){1,25};
00219 c->gop_size = 10;
00220 c->max_b_frames=1;
00221 c->pix_fmt = PIX_FMT_YUV420P;
00222
00223
00224 if (avcodec_open(c, codec) < 0) {
00225 fprintf(stderr, "could not open codec\n");
00226 exit(1);
00227 }
00228
00229 f = fopen(filename, "wb");
00230 if (!f) {
00231 fprintf(stderr, "could not open %s\n", filename);
00232 exit(1);
00233 }
00234
00235
00236 outbuf_size = 100000;
00237 outbuf = malloc(outbuf_size);
00238 size = c->width * c->height;
00239 picture_buf = malloc((size * 3) / 2);
00240
00241 picture->data[0] = picture_buf;
00242 picture->data[1] = picture->data[0] + size;
00243 picture->data[2] = picture->data[1] + size / 4;
00244 picture->linesize[0] = c->width;
00245 picture->linesize[1] = c->width / 2;
00246 picture->linesize[2] = c->width / 2;
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(picture_buf);
00290 free(outbuf);
00291
00292 avcodec_close(c);
00293 av_free(c);
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_context();
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 }