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